/[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 79 Revision 171
19 19
20#include "config.h" 20#include "config.h"
21#include "compat.h" 21#include "compat.h"
22 22
23#include <stdlib.h> 23#include <stdlib.h>
24#include <math.h>
24 25
25#include "parse_routines.h" 26#include "parse_routines.h"
26#include "error.h" 27#include "error.h"
27 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}
28psiconv_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,
29 int *status) 48 int *status)
30{ 49{
31 psiconv_u8 *ptr; 50 psiconv_u8 *ptr;
32 ptr = psiconv_buffer_get(buf,off); 51 ptr = psiconv_buffer_get(buf,off);
33 if (!ptr) { 52 if (!ptr) {
34 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");
35 if (status) 54 if (status)
36 *status = -PSICONV_E_PARSE; 55 *status = -PSICONV_E_PARSE;
37 return 0; 56 return 0;
38 } 57 }
39 if (status) 58 if (status)
40 *status = 0; 59 *status = 0;
41 return *ptr; 60 return *ptr;
42} 61}
43 62
44psiconv_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,
45 int *status) 64 int *status)
46{ 65{
47 psiconv_u8 *ptr0,*ptr1; 66 psiconv_u8 *ptr0,*ptr1;
48 ptr0 = psiconv_buffer_get(buf,off); 67 ptr0 = psiconv_buffer_get(buf,off);
49 ptr1 = psiconv_buffer_get(buf,off+1); 68 ptr1 = psiconv_buffer_get(buf,off+1);
50 if (!ptr0 || !ptr1) { 69 if (!ptr0 || !ptr1) {
51 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");
52 if (status) 71 if (status)
53 *status = -PSICONV_E_PARSE; 72 *status = -PSICONV_E_PARSE;
54 return 0; 73 return 0;
55 } 74 }
56 if (status) 75 if (status)
57 *status = 0; 76 *status = 0;
58 return *ptr0 + (*ptr1 << 8); 77 return *ptr0 + (*ptr1 << 8);
59} 78}
60 79
61psiconv_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,
62 int *status) 81 int *status)
63{ 82{
64 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 83 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
65 ptr0 = psiconv_buffer_get(buf,off); 84 ptr0 = psiconv_buffer_get(buf,off);
66 ptr1 = psiconv_buffer_get(buf,off+1); 85 ptr1 = psiconv_buffer_get(buf,off+1);
67 ptr2 = psiconv_buffer_get(buf,off+2); 86 ptr2 = psiconv_buffer_get(buf,off+2);
68 ptr3 = psiconv_buffer_get(buf,off+3); 87 ptr3 = psiconv_buffer_get(buf,off+3);
69 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 88 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
70 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");
71 if (status) 90 if (status)
72 *status = -PSICONV_E_PARSE; 91 *status = -PSICONV_E_PARSE;
73 return 0; 92 return 0;
74 } 93 }
75 if (status) 94 if (status)
76 *status = 0; 95 *status = 0;
77 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); 96 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
78} 97}
79 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
80psiconv_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,
81 int *length,int *status) 115 int *length,int *status)
82{ 116{
83 psiconv_u8 temp; 117 psiconv_u8 temp;
84 psiconv_S_t res; 118 psiconv_S_t res;
85 int len,localstatus; 119 int len,localstatus;
86 120
87 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");
88 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 122 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
89 if (localstatus) 123 if (localstatus)
90 goto ERROR; 124 goto ERROR;
91 if ((temp & 0x03) == 0x02) { 125 if ((temp & 0x03) == 0x02) {
92 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; 126 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 2;
93 if (localstatus) 127 if (localstatus)
94 goto ERROR; 128 goto ERROR;
95 len = 1; 129 len = 1;
96 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 130 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
97 } else if ((temp & 0x07) == 0x03) { 131 } else if ((temp & 0x07) == 0x05) {
98 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; 132 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 3;
99 if (localstatus) 133 if (localstatus)
100 goto ERROR; 134 goto ERROR;
101 len = 2; 135 len = 2;
102 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 136 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
103 } else { 137 } else {
104 psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); 138 psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!");
105 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 139 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
106 goto ERROR; 140 goto ERROR;
107 } 141 }
108 142
109 if (length) 143 if (length)
110 *length = len; 144 *length = len;
111 if (status) 145 if (status)
112 *status = 0; 146 *status = 0;
113 147
114 psiconv_progress(lev+1,off+len-1, 148 psiconv_progress(config,lev+1,off+len-1,
115 "End of S length indicator (total length: %08x)", len); 149 "End of S length indicator (total length: %08x)", len);
116 150
117 return res; 151 return res;
118 152
119ERROR: 153ERROR:
120 psiconv_warn(lev+1,off,"Reading of S indicator failed"); 154 psiconv_warn(config,lev+1,off,"Reading of S indicator failed");
121 if (status) 155 if (status)
122 *status = localstatus; 156 *status = localstatus;
123 if (length) 157 if (length)
124 *length = 0; 158 *length = 0;
125 return 0; 159 return 0;
126} 160}
127 161
128psiconv_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,
129 int *length, int *status) 163 int *length, int *status)
130{ 164{
131 psiconv_u8 temp; 165 psiconv_u8 temp;
132 psiconv_X_t res; 166 psiconv_X_t res;
133 int len,localstatus; 167 int len,localstatus;
134 168
135 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");
136 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 170 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
137 if (localstatus) 171 if (localstatus)
138 goto ERROR; 172 goto ERROR;
139 if ((temp & 0x01) == 0x00) { 173 if ((temp & 0x01) == 0x00) {
140 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 1; 174 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 1;
141 if (localstatus) 175 if (localstatus)
142 goto ERROR; 176 goto ERROR;
143 len = 1; 177 len = 1;
144 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 178 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
145 } else if ((temp & 0x03) == 0x01) { 179 } else if ((temp & 0x03) == 0x01) {
146 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 2; 180 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 2;
147 if (localstatus) 181 if (localstatus)
148 goto ERROR; 182 goto ERROR;
149 len = 2; 183 len = 2;
150 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 184 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
151 } else if ((temp & 0x07) == 0x03) { 185 } else if ((temp & 0x07) == 0x03) {
152 res = psiconv_read_u32(buf,lev+2,off,&localstatus) >> 3; 186 res = psiconv_read_u32(config,buf,lev+2,off,&localstatus) >> 3;
153 if (localstatus) 187 if (localstatus)
154 goto ERROR; 188 goto ERROR;
155 len = 4; 189 len = 4;
156 psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); 190 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res);
157 } else { 191 } else {
158 psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); 192 psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!");
159 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 193 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
160 goto ERROR; 194 goto ERROR;
161 } 195 }
162 196
163 if (length) 197 if (length)
164 *length = len; 198 *length = len;
165 if (status) 199 if (status)
166 *status = 0; 200 *status = 0;
167 201
168 psiconv_progress(lev+1,off+len-1, 202 psiconv_progress(config,lev+1,off+len-1,
169 "End of X length indicator (total length: %08x)", len); 203 "End of X length indicator (total length: %08x)", len);
170 204
171 return res; 205 return res;
172 206
173ERROR: 207ERROR:
174 psiconv_warn(lev+1,off,"Reading of X indicator failed"); 208 psiconv_warn(config,lev+1,off,"Reading of X indicator failed");
175 if (status) 209 if (status)
176 *status = localstatus; 210 *status = localstatus;
177 if (length) 211 if (length)
178 *length = 0; 212 *length = 0;
179 return 0; 213 return 0;
180} 214}
181 215
182psiconv_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,
183 psiconv_u32 off, int *length, int *status) 217 psiconv_u32 off, int *length, int *status)
184{ 218{
185 psiconv_length_t res; 219 psiconv_length_t res;
186 int localstatus; 220 int localstatus;
187 221
188 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,
189 &localstatus)); 223 &localstatus));
190 if (localstatus) { 224 if (localstatus) {
191 psiconv_warn(lev+1,off,"Reading of length failed"); 225 psiconv_warn(config,lev+1,off,"Reading of length failed");
192 if (length) 226 if (length)
193 *length = 0; 227 *length = 0;
194 if (status) 228 if (status)
195 *status = localstatus; 229 *status = localstatus;
196 return 0; 230 return 0;
197 } 231 }
198 psiconv_debug(lev+1,off,"Length: %f",res); 232 psiconv_debug(config,lev+1,off,"Length: %f",res);
199 if (length) 233 if (length)
200 *length = 4; 234 *length = 4;
201 if (status) 235 if (status)
202 *status = 0; 236 *status = 0;
203 return res; 237 return res;
204} 238}
205 239
206psiconv_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,
207 psiconv_u32 off, int *length, int *status) 241 psiconv_u32 off, int *length, int *status)
208{ 242{
209 psiconv_size_t res; 243 psiconv_size_t res;
210 int localstatus; 244 int localstatus;
211 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;
212 if (localstatus) { 246 if (localstatus) {
213 psiconv_warn(lev+1,off,"Reading of size failed"); 247 psiconv_warn(config,lev+1,off,"Reading of size failed");
214 if (length) 248 if (length)
215 *length = 0; 249 *length = 0;
216 if (status) 250 if (status)
217 *status = localstatus; 251 *status = localstatus;
218 return 0; 252 return 0;
219 } 253 }
220 psiconv_debug(lev+1,off,"Size: %f",res); 254 psiconv_debug(config,lev+1,off,"Size: %f",res);
221 if (status) 255 if (status)
222 *status = 0; 256 *status = 0;
223 if (length) 257 if (length)
224 *length = 4; 258 *length = 4;
225 return res; 259 return res;
226} 260}
227 261
228int 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,
229 int *length, psiconv_bool_t *result) 263 int *length, psiconv_bool_t *result)
230{ 264{
231 psiconv_u8 temp; 265 psiconv_u8 temp;
232 int localstatus; 266 int localstatus;
233 temp = psiconv_read_u8(buf,lev,off,&localstatus); 267 temp = psiconv_read_u8(config,buf,lev,off,&localstatus);
234 if (localstatus) { 268 if (localstatus) {
235 psiconv_warn(lev+1,off,"Reading of bool failed"); 269 psiconv_warn(config,lev+1,off,"Reading of bool failed");
236 if (length) 270 if (length)
237 *length = 0; 271 *length = 0;
238 return localstatus; 272 return localstatus;
239 } 273 }
240 if (length) 274 if (length)
244 return 0; 278 return 0;
245 } else if (temp == 1) { 279 } else if (temp == 1) {
246 *result = psiconv_bool_true; 280 *result = psiconv_bool_true;
247 return 0; 281 return 0;
248 } 282 }
249 psiconv_warn(lev+1,off,"Unknown value for boolean"); 283 psiconv_warn(config,lev+1,off,"Unknown value for boolean");
250 psiconv_debug(lev+1,off,"Boolean value: %02x",temp); 284 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp);
251 *result = psiconv_bool_true; 285 *result = psiconv_bool_true;
252 return 0; 286 return 0;
253} 287}
254 288
255psiconv_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,
256 psiconv_u32 off,int *length, int *status) 290 psiconv_u32 off,int *length, int *status)
257{ 291{
258 int stringlen,i,leng,len,localstatus; 292 int stringlen,i,leng,len,localstatus;
259 psiconv_string_t result; 293 psiconv_string_t result;
260 char *res_copy; 294 char *res_copy;
261 295
262 psiconv_progress(lev+1,off,"Going to read a string"); 296 psiconv_progress(config,lev+1,off,"Going to read a string");
263 297
264 stringlen = psiconv_read_S(buf,lev+2,off,&leng,&localstatus); 298 stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus);
265 if (localstatus) 299 if (localstatus)
266 goto ERROR1; 300 goto ERROR1;
267 psiconv_debug(lev+2,off,"Length: %i",stringlen); 301 psiconv_debug(config,lev+2,off,"Length: %i",stringlen);
268 len = leng; 302 len = leng;
269 303
270 result = malloc(stringlen + 1); 304 result = malloc(stringlen + 1);
271 if (!result) 305 if (!result)
272 goto ERROR1; 306 goto ERROR1;
273 for (i = 0; (i < stringlen) && !localstatus; i++) 307 for (i = 0; (i < stringlen) && !localstatus; i++)
274 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); 308 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus);
275 if (localstatus) 309 if (localstatus)
276 goto ERROR2; 310 goto ERROR2;
277 result[stringlen] = 0; 311 result[stringlen] = 0;
278 len += stringlen; 312 len += stringlen;
279 313
280 res_copy = psiconv_make_printable(result); 314 res_copy = psiconv_make_printable(result);
281 if (!res_copy) 315 if (!res_copy)
282 goto ERROR2; 316 goto ERROR2;
283 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); 317 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
284 free(res_copy); 318 free(res_copy);
285 319
286 if (length) 320 if (length)
287 *length = len; 321 *length = len;
288 322
289 if (status) 323 if (status)
290 *status = 0; 324 *status = 0;
291 325
292 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);
293 327
294 return result; 328 return result;
295 329
296ERROR2: 330ERROR2:
297 free(result); 331 free(result);
298ERROR1: 332ERROR1:
299 psiconv_warn(lev+1,off,"Reading of string failed"); 333 psiconv_warn(config,lev+1,off,"Reading of string failed");
300 if (status) 334 if (status)
301 *status = localstatus; 335 *status = localstatus;
302 if (length) 336 if (length)
303 *length = 0; 337 *length = 0;
304 return NULL; 338 return NULL;
305} 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.79  
changed lines
  Added in v.171

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