[1.5][JPIP] added get_filesize()
[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 <sys/stat.h>
36 #include "byte_manager.h"
37
38 #ifdef SERVER
39 #include "fcgi_stdio.h"
40 #define logstream FCGI_stdout
41 #else
42 #define FCGI_stdout stdout
43 #define FCGI_stderr stderr
44 #define logstream stderr
45 #endif //SERVER
46
47
48 Byte_t * fetch_bytes( int fd, long offset, int size)
49 {
50   Byte_t *data;
51
52   if( lseek( fd, offset, SEEK_SET)==-1){
53     fprintf( FCGI_stdout, "Reason: Target broken (fseek error)\r\n");
54     fprintf( FCGI_stderr, "Error: error in fetch_bytes( %d, %ld, %d)\n", fd, offset, size);
55     return NULL;
56   }
57   
58   data = (Byte_t *)malloc( size);
59
60   if( read( fd, data, size) != size){
61     free( data);
62     fprintf( FCGI_stdout, "Reason: Target broken (read error)\r\n");
63     fprintf( FCGI_stderr, "Error: error in fetch_bytes( %d, %ld, %d)\n", fd, offset, size);
64     return NULL;
65   }
66   return data;
67 }
68
69 Byte_t fetch_1byte( int fd, long offset)
70 {
71   Byte_t code;
72
73   if( lseek( fd, offset, SEEK_SET)==-1){
74     fprintf( FCGI_stdout, "Reason: Target broken (seek error)\r\n");
75     fprintf( FCGI_stderr, "Error: error in fetch_1byte( %d, %ld)\n", fd, offset);
76     return 0;
77   }
78    
79   if( read( fd, &code, 1) != 1){
80     fprintf( FCGI_stdout, "Reason: Target broken (read error)\r\n");
81     fprintf( FCGI_stderr, "Error: error in fetch_bytes( %d, %ld)\n", fd, offset);
82     return 0;
83   }
84   return code;
85 }
86
87 Byte2_t fetch_2bytebigendian( int fd, long offset)
88 {
89   Byte_t *data;
90   Byte2_t code;
91
92   if(!(data = fetch_bytes( fd, offset, 2))){
93     fprintf( FCGI_stderr, "Error: error in fetch_2bytebigendian( %d, %ld)\n", fd, offset);
94     return 0;
95   }
96   code = big2(data);
97   free( data);
98
99   return code;
100 }
101
102 Byte4_t fetch_4bytebigendian( int fd, long offset)
103 {
104   Byte_t *data;
105   Byte4_t code;
106
107   if(!(data = fetch_bytes( fd, offset, 4))){
108     fprintf( FCGI_stderr, "Error: error in fetch_4bytebigendian( %d, %ld)\n", fd, offset);
109     return 0;
110   }
111   code = big4(data);
112   free( data);
113
114   return code;
115 }
116
117 Byte8_t fetch_8bytebigendian( int fd, long offset)
118 {
119   Byte_t *data;
120   Byte8_t code;
121
122   if(!(data = fetch_bytes( fd, offset, 8))){
123     fprintf( FCGI_stderr, "Error: error in fetch_8bytebigendian( %d, %ld)\n", fd, offset);
124     return 0;
125   }
126   code = big8(data);
127   free( data);
128
129   return code;
130 }
131
132
133 Byte2_t big2( Byte_t *buf)
134 {
135   return (((Byte2_t) buf[0]) << 8) + ((Byte2_t) buf[1]);
136 }
137
138 Byte4_t big4( Byte_t *buf)
139 {
140   return (((((((Byte4_t) buf[0]) << 8) + ((Byte4_t) buf[1])) << 8)
141            + ((Byte4_t) buf[2])) << 8) + ((Byte4_t) buf[3]);
142 }
143
144 Byte8_t big8( Byte_t *buf)
145 {
146   return (((Byte8_t) big4 (buf)) << 32)
147         + ((Byte8_t) big4 (buf + 4));
148 }
149
150 void modify_4Bytecode( Byte4_t code, Byte_t *stream)
151 {
152   *stream     = (Byte_t) ((Byte4_t)(code & 0xff000000) >> 24);
153   *(stream+1) = (Byte_t) ((Byte4_t)(code & 0x00ff0000) >> 16);
154   *(stream+2) = (Byte_t) ((Byte4_t)(code & 0x0000ff00) >> 8);
155   *(stream+3) = (Byte_t) (code & 0x000000ff);
156 }
157
158 Byte8_t get_filesize( int fd)
159 {
160   struct stat sb;
161     
162   if( fstat( fd, &sb) == -1){
163     fprintf( FCGI_stdout, "Reason: Target broken (fstat error)\r\n");
164     fprintf( FCGI_stderr, "Error: error in get_filesize( %d)\n", fd);
165     return 0;
166   }
167   return (Byte8_t)sb.st_size;
168 }