backport r882:893 to openjpeg-1.5 branch
[openjpeg.git] / applications / jpip / libopenjpip / byte_manager.c
1 /*
2  * $Id: byte_manager.c 44 2011-02-15 12:32:29Z kaori $
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include "byte_manager.h"
36
37 #ifdef SERVER
38 #include "fcgi_stdio.h"
39 #define logstream FCGI_stdout
40 #else
41 #define FCGI_stdout stdout
42 #define FCGI_stderr stderr
43 #define logstream stderr
44 #endif //SERVER
45
46
47 Byte_t * fetch_bytes( int fd, long offset, int size)
48 {
49   Byte_t *data;
50
51   if( lseek( fd, offset, SEEK_SET)==-1){
52     fprintf( FCGI_stdout, "Reason: Target broken (fseek error)\r\n");
53     fprintf( FCGI_stderr, "Error: error in fetch_bytes( %d, %ld, %d)\n", fd, offset, size);
54     return NULL;
55   }
56   
57   data = (Byte_t *)malloc( size);
58
59   if( read( fd, data, size) != size){
60     free( data);
61     fprintf( FCGI_stdout, "Reason: Target broken (read error)\r\n");
62     fprintf( FCGI_stderr, "Error: error in fetch_bytes( %d, %ld, %d)\n", fd, offset, size);
63     return NULL;
64   }
65   return data;
66 }
67
68 Byte_t fetch_1byte( int fd, long offset)
69 {
70   Byte_t code;
71
72   if( lseek( fd, offset, SEEK_SET)==-1){
73     fprintf( FCGI_stdout, "Reason: Target broken (seek error)\r\n");
74     fprintf( FCGI_stderr, "Error: error in fetch_1byte( %d, %ld)\n", fd, offset);
75     return 0;
76   }
77    
78   if( read( fd, &code, 1) != 1){
79     fprintf( FCGI_stdout, "Reason: Target broken (read error)\r\n");
80     fprintf( FCGI_stderr, "Error: error in fetch_bytes( %d, %ld)\n", fd, offset);
81     return 0;
82   }
83   return code;
84 }
85
86 Byte2_t fetch_2bytebigendian( int fd, long offset)
87 {
88   Byte_t *data;
89   Byte2_t code;
90
91   if(!(data = fetch_bytes( fd, offset, 2))){
92     fprintf( FCGI_stderr, "Error: error in fetch_2bytebigendian( %d, %ld)\n", fd, offset);
93     return 0;
94   }
95   code = big2(data);
96   free( data);
97
98   return code;
99 }
100
101 Byte4_t fetch_4bytebigendian( int fd, long offset)
102 {
103   Byte_t *data;
104   Byte4_t code;
105
106   if(!(data = fetch_bytes( fd, offset, 4))){
107     fprintf( FCGI_stderr, "Error: error in fetch_4bytebigendian( %d, %ld)\n", fd, offset);
108     return 0;
109   }
110   code = big4(data);
111   free( data);
112
113   return code;
114 }
115
116 Byte8_t fetch_8bytebigendian( int fd, long offset)
117 {
118   Byte_t *data;
119   Byte8_t code;
120
121   if(!(data = fetch_bytes( fd, offset, 8))){
122     fprintf( FCGI_stderr, "Error: error in fetch_8bytebigendian( %d, %ld)\n", fd, offset);
123     return 0;
124   }
125   code = big8(data);
126   free( data);
127
128   return code;
129 }
130
131
132 Byte2_t big2( Byte_t *buf)
133 {
134   return (((Byte2_t) buf[0]) << 8) + ((Byte2_t) buf[1]);
135 }
136
137 Byte4_t big4( Byte_t *buf)
138 {
139   return (((((((Byte4_t) buf[0]) << 8) + ((Byte4_t) buf[1])) << 8)
140            + ((Byte4_t) buf[2])) << 8) + ((Byte4_t) buf[3]);
141 }
142
143 Byte8_t big8( Byte_t *buf)
144 {
145   return (((Byte8_t) big4 (buf)) << 32)
146         + ((Byte8_t) big4 (buf + 4));
147 }