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