/[public]/psiconv/trunk/lib/psiconv/parse_simple.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/parse_simple.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 64 Revision 171
16 along with this program; if not, write to the Free Software 16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/ 18*/
19 19
20#include "config.h" 20#include "config.h"
21#include "compat.h"
22
21#include <stdlib.h> 23#include <stdlib.h>
24#include <math.h>
22 25
23#include "parse_routines.h" 26#include "parse_routines.h"
27#include "error.h"
24 28
29#ifdef DMALLOC
30#include <dmalloc.h>
31#endif
32
33static psiconv_float_t pow2(int n);
34
35/* Very inefficient, but good enough for now. By implementing it ourselves,
36 we do not have to link with -lm */
37psiconv_float_t pow2(int n)
38{
39 psiconv_float_t res=1.0;
40 int i;
41
42 for (i = 0; i < (n<0?-n:n); i++)
43 res *= 2.0;
44
45 return n<0?1/res:res;
46}
25psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, 47psiconv_u8 psiconv_read_u8(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
26 int *status) 48 int *status)
27{ 49{
28 psiconv_u8 *ptr; 50 psiconv_u8 *ptr;
29 ptr = psiconv_list_get(buf,off); 51 ptr = psiconv_buffer_get(buf,off);
30 if (!ptr) { 52 if (!ptr) {
31 psiconv_warn(lev,off,"Trying byte read past the end of the file"); 53 psiconv_warn(config,lev,off,"Trying byte read past the end of the file");
32 if (status) 54 if (status)
33 *status = -PSICONV_E_PARSE; 55 *status = -PSICONV_E_PARSE;
34 return 0; 56 return 0;
35 } 57 }
36 if (status) 58 if (status)
37 *status = 0; 59 *status = 0;
38 return *ptr; 60 return *ptr;
39} 61}
40 62
41psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off, 63psiconv_u16 psiconv_read_u16(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
42 int *status) 64 int *status)
43{ 65{
44 psiconv_u8 *ptr0,*ptr1; 66 psiconv_u8 *ptr0,*ptr1;
45 ptr0 = psiconv_list_get(buf,off); 67 ptr0 = psiconv_buffer_get(buf,off);
46 ptr1 = psiconv_list_get(buf,off+1); 68 ptr1 = psiconv_buffer_get(buf,off+1);
47 if (!ptr0 || !ptr1) { 69 if (!ptr0 || !ptr1) {
48 psiconv_warn(lev,off,"Trying word read past the end of the file"); 70 psiconv_warn(config,lev,off,"Trying word read past the end of the file");
49 if (status) 71 if (status)
50 *status = -PSICONV_E_PARSE; 72 *status = -PSICONV_E_PARSE;
51 return 0; 73 return 0;
52 } 74 }
53 if (status) 75 if (status)
54 *status = 0; 76 *status = 0;
55 return *ptr0 + (*ptr1 << 8); 77 return *ptr0 + (*ptr1 << 8);
56} 78}
57 79
58psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off, 80psiconv_u32 psiconv_read_u32(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
59 int *status) 81 int *status)
60{ 82{
61 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 83 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
62 ptr0 = psiconv_list_get(buf,off); 84 ptr0 = psiconv_buffer_get(buf,off);
63 ptr1 = psiconv_list_get(buf,off+1); 85 ptr1 = psiconv_buffer_get(buf,off+1);
64 ptr2 = psiconv_list_get(buf,off+2); 86 ptr2 = psiconv_buffer_get(buf,off+2);
65 ptr3 = psiconv_list_get(buf,off+3); 87 ptr3 = psiconv_buffer_get(buf,off+3);
66 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 88 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
67 psiconv_warn(lev,off,"Trying long read past the end of the file"); 89 psiconv_warn(config,lev,off,"Trying long read past the end of the file");
68 if (status) 90 if (status)
69 *status = -PSICONV_E_PARSE; 91 *status = -PSICONV_E_PARSE;
70 return 0; 92 return 0;
71 } 93 }
72 if (status) 94 if (status)
73 *status = 0; 95 *status = 0;
74 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); 96 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
75} 97}
76 98
99psiconv_s32 psiconv_read_sint(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
100 int *length,int *status)
101{
102 int localstatus;
103 psiconv_u32 temp;
104
105 temp=psiconv_read_u32(config,buf,lev,off,&localstatus);
106 if (status)
107 *status = localstatus;
108 if (length)
109 *length = localstatus?0:4;
110
111 return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1);
112}
113
77psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, 114psiconv_S_t psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
78 int *length,int *status) 115 int *length,int *status)
79{ 116{
80 psiconv_u8 temp; 117 psiconv_u8 temp;
81 psiconv_S_t res; 118 psiconv_S_t res;
82 int len,localstatus; 119 int len,localstatus;
83 120
84 psiconv_progress(lev+1,off,"Going to read a S length indicator"); 121 psiconv_progress(config,lev+1,off,"Going to read a S length indicator");
85 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 122 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
86 if (localstatus) 123 if (localstatus)
87 goto ERROR; 124 goto ERROR;
88 if ((temp & 0x03) == 0x02) { 125 if ((temp & 0x03) == 0x02) {
89 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; 126 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 2;
90 if (localstatus) 127 if (localstatus)
91 goto ERROR; 128 goto ERROR;
92 len = 1; 129 len = 1;
93 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 130 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
94 } else if ((temp & 0x07) == 0x03) { 131 } else if ((temp & 0x07) == 0x05) {
95 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; 132 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 3;
96 if (localstatus) 133 if (localstatus)
97 goto ERROR; 134 goto ERROR;
98 len = 2; 135 len = 2;
99 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 136 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
100 } else { 137 } else {
101 psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); 138 psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!");
102 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 139 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
103 goto ERROR; 140 goto ERROR;
104 } 141 }
105 142
106 if (length) 143 if (length)
107 *length = len; 144 *length = len;
108 if (status) 145 if (status)
109 *status = 0; 146 *status = 0;
110 147
111 psiconv_progress(lev+1,off+len-1, 148 psiconv_progress(config,lev+1,off+len-1,
112 "End of S length indicator (total length: %08x)", len); 149 "End of S length indicator (total length: %08x)", len);
113 150
114 return res; 151 return res;
115 152
116ERROR: 153ERROR:
117 psiconv_warn(lev+1,off,"Reading of S indicator failed"); 154 psiconv_warn(config,lev+1,off,"Reading of S indicator failed");
118 if (status) 155 if (status)
119 *status = localstatus; 156 *status = localstatus;
120 if (length) 157 if (length)
121 *length = 0; 158 *length = 0;
122 return 0; 159 return 0;
123} 160}
124 161
125psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off, 162psiconv_X_t psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
126 int *length, int *status) 163 int *length, int *status)
127{ 164{
128 psiconv_u8 temp; 165 psiconv_u8 temp;
129 psiconv_X_t res; 166 psiconv_X_t res;
130 int len,localstatus; 167 int len,localstatus;
131 168
132 psiconv_progress(lev+1,off,"Going to read a X length indicator"); 169 psiconv_progress(config,lev+1,off,"Going to read a X length indicator");
133 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 170 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
134 if (localstatus) 171 if (localstatus)
135 goto ERROR; 172 goto ERROR;
136 if ((temp & 0x01) == 0x00) { 173 if ((temp & 0x01) == 0x00) {
137 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 1; 174 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 1;
138 if (localstatus) 175 if (localstatus)
139 goto ERROR; 176 goto ERROR;
140 len = 1; 177 len = 1;
141 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 178 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
142 } else if ((temp & 0x03) == 0x01) { 179 } else if ((temp & 0x03) == 0x01) {
143 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 2; 180 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 2;
144 if (localstatus) 181 if (localstatus)
145 goto ERROR; 182 goto ERROR;
146 len = 2; 183 len = 2;
147 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 184 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
148 } else if ((temp & 0x07) == 0x03) { 185 } else if ((temp & 0x07) == 0x03) {
149 res = psiconv_read_u32(buf,lev+2,off,&localstatus) >> 3; 186 res = psiconv_read_u32(config,buf,lev+2,off,&localstatus) >> 3;
150 if (localstatus) 187 if (localstatus)
151 goto ERROR; 188 goto ERROR;
152 len = 4; 189 len = 4;
153 psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); 190 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res);
154 } else { 191 } else {
155 psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); 192 psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!");
156 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 193 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
157 goto ERROR; 194 goto ERROR;
158 } 195 }
159 196
160 if (length) 197 if (length)
161 *length = len; 198 *length = len;
162 if (status) 199 if (status)
163 *status = 0; 200 *status = 0;
164 201
165 psiconv_progress(lev+1,off+len-1, 202 psiconv_progress(config,lev+1,off+len-1,
166 "End of X length indicator (total length: %08x)", len); 203 "End of X length indicator (total length: %08x)", len);
167 204
168 return res; 205 return res;
169 206
170ERROR: 207ERROR:
171 psiconv_warn(lev+1,off,"Reading of X indicator failed"); 208 psiconv_warn(config,lev+1,off,"Reading of X indicator failed");
172 if (status) 209 if (status)
173 *status = localstatus; 210 *status = localstatus;
174 if (length) 211 if (length)
175 *length = 0; 212 *length = 0;
176 return 0; 213 return 0;
177} 214}
178 215
179psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev, 216psiconv_length_t psiconv_read_length(const psiconv_config config,const psiconv_buffer buf, int lev,
180 psiconv_u32 off, int *length, int *status) 217 psiconv_u32 off, int *length, int *status)
181{ 218{
182 psiconv_length_t res; 219 psiconv_length_t res;
183 int localstatus; 220 int localstatus;
184 221
185 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off, 222 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,
186 &localstatus)); 223 &localstatus));
187 if (localstatus) { 224 if (localstatus) {
188 psiconv_warn(lev+1,off,"Reading of length failed"); 225 psiconv_warn(config,lev+1,off,"Reading of length failed");
189 if (length) 226 if (length)
190 *length = 0; 227 *length = 0;
191 if (status) 228 if (status)
192 *status = localstatus; 229 *status = localstatus;
193 return 0; 230 return 0;
194 } 231 }
195 psiconv_debug(lev+1,off,"Length: %f",res); 232 psiconv_debug(config,lev+1,off,"Length: %f",res);
196 if (length) 233 if (length)
197 *length = 4; 234 *length = 4;
198 if (status) 235 if (status)
199 *status = 0; 236 *status = 0;
200 return res; 237 return res;
201} 238}
202 239
203psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev, 240psiconv_size_t psiconv_read_size(const psiconv_config config,const psiconv_buffer buf, int lev,
204 psiconv_u32 off, int *length, int *status) 241 psiconv_u32 off, int *length, int *status)
205{ 242{
206 psiconv_size_t res; 243 psiconv_size_t res;
207 int localstatus; 244 int localstatus;
208 res = ((psiconv_s32) psiconv_read_u32(buf,lev,off,&localstatus)) / 20.0; 245 res = ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,&localstatus)) / 20.0;
209 if (localstatus) { 246 if (localstatus) {
210 psiconv_warn(lev+1,off,"Reading of size failed"); 247 psiconv_warn(config,lev+1,off,"Reading of size failed");
211 if (length) 248 if (length)
212 *length = 0; 249 *length = 0;
213 if (status) 250 if (status)
214 *status = localstatus; 251 *status = localstatus;
215 return 0; 252 return 0;
216 } 253 }
217 psiconv_debug(lev+1,off,"Size: %f",res); 254 psiconv_debug(config,lev+1,off,"Size: %f",res);
218 if (status) 255 if (status)
219 *status = 0; 256 *status = 0;
220 if (length) 257 if (length)
221 *length = 4; 258 *length = 4;
222 return res; 259 return res;
223} 260}
224 261
225int psiconv_parse_bool(const psiconv_buffer buf, int lev, psiconv_u32 off, 262int psiconv_parse_bool(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
226 int *length, psiconv_bool_t *result) 263 int *length, psiconv_bool_t *result)
227{ 264{
228 psiconv_u8 temp; 265 psiconv_u8 temp;
229 int localstatus; 266 int localstatus;
230 temp = psiconv_read_u8(buf,lev,off,&localstatus); 267 temp = psiconv_read_u8(config,buf,lev,off,&localstatus);
231 if (localstatus) { 268 if (localstatus) {
232 psiconv_warn(lev+1,off,"Reading of bool failed"); 269 psiconv_warn(config,lev+1,off,"Reading of bool failed");
233 if (length) 270 if (length)
234 *length = 0; 271 *length = 0;
235 return localstatus; 272 return localstatus;
236 } 273 }
237 if (length) 274 if (length)
241 return 0; 278 return 0;
242 } else if (temp == 1) { 279 } else if (temp == 1) {
243 *result = psiconv_bool_true; 280 *result = psiconv_bool_true;
244 return 0; 281 return 0;
245 } 282 }
246 psiconv_warn(lev+1,off,"Unknown value for boolean"); 283 psiconv_warn(config,lev+1,off,"Unknown value for boolean");
247 psiconv_debug(lev+1,off,"Boolean value: %02x",temp); 284 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp);
248 *result = psiconv_bool_true; 285 *result = psiconv_bool_true;
249 return 0; 286 return 0;
250} 287}
251 288
252psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev, 289psiconv_string_t psiconv_read_string(const psiconv_config config,const psiconv_buffer buf,int lev,
253 psiconv_u32 off,int *length, int *status) 290 psiconv_u32 off,int *length, int *status)
254{ 291{
255 int stringlen,i,leng,len,localstatus; 292 int stringlen,i,leng,len,localstatus;
256 psiconv_string_t result; 293 psiconv_string_t result;
257 char *res_copy; 294 char *res_copy;
258 295
259 psiconv_progress(lev+1,off,"Going to read a string"); 296 psiconv_progress(config,lev+1,off,"Going to read a string");
260 297
261 stringlen = psiconv_read_S(buf,lev+2,off,&leng,&localstatus); 298 stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus);
262 if (localstatus) 299 if (localstatus)
263 goto ERROR1; 300 goto ERROR1;
264 psiconv_debug(lev+2,off,"Length: %i",stringlen); 301 psiconv_debug(config,lev+2,off,"Length: %i",stringlen);
265 len = leng; 302 len = leng;
266 303
267 result = malloc(stringlen + 1); 304 result = malloc(stringlen + 1);
268 if (!result) 305 if (!result)
269 goto ERROR1; 306 goto ERROR1;
270 for (i = 0; (i < stringlen) && !localstatus; i++) 307 for (i = 0; (i < stringlen) && !localstatus; i++)
271 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); 308 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus);
272 if (localstatus) 309 if (localstatus)
273 goto ERROR2; 310 goto ERROR2;
274 result[stringlen] = 0; 311 result[stringlen] = 0;
275 len += stringlen; 312 len += stringlen;
276 313
277 res_copy = psiconv_make_printable(result); 314 res_copy = psiconv_make_printable(result);
278 if (!res_copy) 315 if (!res_copy)
279 goto ERROR2; 316 goto ERROR2;
280 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); 317 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
281 free(res_copy); 318 free(res_copy);
282 319
283 if (length) 320 if (length)
284 *length = len; 321 *length = len;
285 322
286 if (status) 323 if (status)
287 *status = 0; 324 *status = 0;
288 325
289 psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len); 326 psiconv_progress(config,lev+1,off+len-1,"End of string (total length: %08x)",len);
290 327
291 return result; 328 return result;
292 329
293ERROR2: 330ERROR2:
294 free(result); 331 free(result);
295ERROR1: 332ERROR1:
296 psiconv_warn(lev+1,off,"Reading of string failed"); 333 psiconv_warn(config,lev+1,off,"Reading of string failed");
297 if (status) 334 if (status)
298 *status = localstatus; 335 *status = localstatus;
299 if (length) 336 if (length)
300 *length = 0; 337 *length = 0;
301 return NULL; 338 return NULL;
302} 339}
340
341psiconv_string_t psiconv_read_short_string(const psiconv_config config, const psiconv_buffer buf,
342 int lev,
343 psiconv_u32 off,int *length, int *status)
344{
345 int stringlen,i,len,localstatus;
346 psiconv_string_t result;
347 char *res_copy;
348
349 psiconv_progress(config,lev+1,off,"Going to read a short string");
350
351 stringlen = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
352 if (localstatus)
353 goto ERROR1;
354 psiconv_debug(config,lev+2,off,"Length: %i",stringlen);
355 len = 1;
356
357 result = malloc(stringlen + 1);
358 if (!result)
359 goto ERROR1;
360 for (i = 0; (i < stringlen) && !localstatus; i++)
361 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus);
362 if (localstatus)
363 goto ERROR2;
364 result[stringlen] = 0;
365 len += stringlen;
366
367 res_copy = psiconv_make_printable(result);
368 if (!res_copy)
369 goto ERROR2;
370 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
371 free(res_copy);
372
373 if (length)
374 *length = len;
375
376 if (status)
377 *status = 0;
378
379 psiconv_progress(config,lev+1,off+len-1,"End of short string (total length: %08x)",
380 len);
381
382 return result;
383
384
385ERROR2:
386 free(result);
387ERROR1:
388 psiconv_warn(config,lev+1,off,"Reading of short string failed");
389 if (status)
390 *status = localstatus;
391 if (length)
392 *length = 0;
393 return NULL;
394}
395
396psiconv_float_t psiconv_read_float(const psiconv_config config,const psiconv_buffer buf, int lev,
397 psiconv_u32 off, int *length, int *status)
398{
399 psiconv_float_t result,bitvalue;
400 int res,bit;
401 psiconv_u32 temp=0;
402
403 psiconv_progress(config,lev+1,off,"Going to read a float");
404
405 bitvalue = 0.5;
406 result = 1.0;
407 for (bit = 0x33; bit > 0; bit--) {
408 if ((bit == 0x33) || ((bit & 0x07) == 0x07))
409 temp = psiconv_read_u8(config,buf,lev+2,off+ (bit >> 3),&res);
410 if (res)
411 goto ERROR;
412 if (temp & (0x01 << (bit & 0x07)))
413 result += bitvalue;
414 bitvalue /= 2.0;
415 }
416 temp = psiconv_read_u16(config,buf,lev+2,off+6,&res);
417 if (res)
418 goto ERROR;
419 if (temp & 0x8000)
420 result = -result;
421 temp = (temp & 0x7ff0) >> 4;
422 result *= pow2(((int) temp)-0x3ff);
423 psiconv_debug(config,lev+1,off,"Float value: %f",result);
424 if (length)
425 *length = 8;
426 if (*status)
427 *status = res;
428 return result;
429ERROR:
430 psiconv_warn(config,lev+1,off,"Reading of float failed");
431 if (length)
432 *length = 0;
433 if (*status)
434 *status = res;
435 return 0.0;
436}
437

Legend:
Removed from v.64  
changed lines
  Added in v.171

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26