/[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 142
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
33/* Very inefficient, but good enough for now. By implementing it ourselves,
34 we do not have to link with -lm */
35psiconv_float_t pow2(int n)
36{
37 psiconv_float_t res=1.0;
38 int i;
39
40 for (i = 0; i < (n<0?-n:n); i++)
41 res *= 2.0;
42
43 return n<0?1/res:res;
44}
25psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, 45psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off,
26 int *status) 46 int *status)
27{ 47{
28 psiconv_u8 *ptr; 48 psiconv_u8 *ptr;
29 ptr = psiconv_list_get(buf,off); 49 ptr = psiconv_buffer_get(buf,off);
30 if (!ptr) { 50 if (!ptr) {
31 psiconv_warn(lev,off,"Trying byte read past the end of the file"); 51 psiconv_warn(lev,off,"Trying byte read past the end of the file");
32 if (status) 52 if (status)
33 *status = -PSICONV_E_PARSE; 53 *status = -PSICONV_E_PARSE;
34 return 0; 54 return 0;
40 60
41psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off, 61psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off,
42 int *status) 62 int *status)
43{ 63{
44 psiconv_u8 *ptr0,*ptr1; 64 psiconv_u8 *ptr0,*ptr1;
45 ptr0 = psiconv_list_get(buf,off); 65 ptr0 = psiconv_buffer_get(buf,off);
46 ptr1 = psiconv_list_get(buf,off+1); 66 ptr1 = psiconv_buffer_get(buf,off+1);
47 if (!ptr0 || !ptr1) { 67 if (!ptr0 || !ptr1) {
48 psiconv_warn(lev,off,"Trying word read past the end of the file"); 68 psiconv_warn(lev,off,"Trying word read past the end of the file");
49 if (status) 69 if (status)
50 *status = -PSICONV_E_PARSE; 70 *status = -PSICONV_E_PARSE;
51 return 0; 71 return 0;
57 77
58psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off, 78psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off,
59 int *status) 79 int *status)
60{ 80{
61 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 81 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
62 ptr0 = psiconv_list_get(buf,off); 82 ptr0 = psiconv_buffer_get(buf,off);
63 ptr1 = psiconv_list_get(buf,off+1); 83 ptr1 = psiconv_buffer_get(buf,off+1);
64 ptr2 = psiconv_list_get(buf,off+2); 84 ptr2 = psiconv_buffer_get(buf,off+2);
65 ptr3 = psiconv_list_get(buf,off+3); 85 ptr3 = psiconv_buffer_get(buf,off+3);
66 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 86 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
67 psiconv_warn(lev,off,"Trying long read past the end of the file"); 87 psiconv_warn(lev,off,"Trying long read past the end of the file");
68 if (status) 88 if (status)
69 *status = -PSICONV_E_PARSE; 89 *status = -PSICONV_E_PARSE;
70 return 0; 90 return 0;
72 if (status) 92 if (status)
73 *status = 0; 93 *status = 0;
74 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); 94 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
75} 95}
76 96
97psiconv_s32 psiconv_read_sint(const psiconv_buffer buf,int lev,psiconv_u32 off,
98 int *length,int *status)
99{
100 int localstatus;
101 psiconv_u32 temp;
102
103 temp=psiconv_read_u32(buf,lev,off,&localstatus);
104 if (status)
105 *status = localstatus;
106 if (length)
107 *length = localstatus?0:4;
108
109 return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1);
110}
111
77psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, 112psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off,
78 int *length,int *status) 113 int *length,int *status)
79{ 114{
80 psiconv_u8 temp; 115 psiconv_u8 temp;
81 psiconv_S_t res; 116 psiconv_S_t res;
89 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; 124 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2;
90 if (localstatus) 125 if (localstatus)
91 goto ERROR; 126 goto ERROR;
92 len = 1; 127 len = 1;
93 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 128 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res);
94 } else if ((temp & 0x07) == 0x03) { 129 } else if ((temp & 0x07) == 0x05) {
95 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; 130 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3;
96 if (localstatus) 131 if (localstatus)
97 goto ERROR; 132 goto ERROR;
98 len = 2; 133 len = 2;
99 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 134 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res);
298 *status = localstatus; 333 *status = localstatus;
299 if (length) 334 if (length)
300 *length = 0; 335 *length = 0;
301 return NULL; 336 return NULL;
302} 337}
338
339psiconv_string_t psiconv_read_short_string(const psiconv_buffer buf,
340 int lev,
341 psiconv_u32 off,int *length, int *status)
342{
343 int stringlen,i,len,localstatus;
344 psiconv_string_t result;
345 char *res_copy;
346
347 psiconv_progress(lev+1,off,"Going to read a short string");
348
349 stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus);
350 if (localstatus)
351 goto ERROR1;
352 psiconv_debug(lev+2,off,"Length: %i",stringlen);
353 len = 1;
354
355 result = malloc(stringlen + 1);
356 if (!result)
357 goto ERROR1;
358 for (i = 0; (i < stringlen) && !localstatus; i++)
359 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus);
360 if (localstatus)
361 goto ERROR2;
362 result[stringlen] = 0;
363 len += stringlen;
364
365 res_copy = psiconv_make_printable(result);
366 if (!res_copy)
367 goto ERROR2;
368 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
369 free(res_copy);
370
371 if (length)
372 *length = len;
373
374 if (status)
375 *status = 0;
376
377 psiconv_progress(lev+1,off+len-1,"End of short string (total length: %08x)",
378 len);
379
380 return result;
381
382
383ERROR2:
384 free(result);
385ERROR1:
386 psiconv_warn(lev+1,off,"Reading of short string failed");
387 if (status)
388 *status = localstatus;
389 if (length)
390 *length = 0;
391 return NULL;
392}
393
394psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev,
395 psiconv_u32 off, int *length, int *status)
396{
397 psiconv_float_t result,bitvalue;
398 int res,bit;
399 psiconv_u32 temp=0;
400
401 psiconv_progress(lev+1,off,"Going to read a float");
402
403 bitvalue = 0.5;
404 result = 1.0;
405 for (bit = 0x33; bit > 0; bit--) {
406 if ((bit == 0x33) || ((bit & 0x07) == 0x07))
407 temp = psiconv_read_u8(buf,lev+2,off+ (bit >> 3),&res);
408 if (res)
409 goto ERROR;
410 if (temp & (0x01 << (bit & 0x07)))
411 result += bitvalue;
412 bitvalue /= 2.0;
413 }
414 temp = psiconv_read_u16(buf,lev+2,off+6,&res);
415 if (res)
416 goto ERROR;
417 if (temp & 0x8000)
418 result = -result;
419 temp = (temp & 0x7ff0) >> 4;
420 result *= pow2(((int) temp)-0x3ff);
421 psiconv_debug(lev+1,off,"Float value: %f",result);
422 if (length)
423 *length = 8;
424 if (*status)
425 *status = res;
426 return result;
427ERROR:
428 psiconv_warn(lev+1,off,"Reading of float failed");
429 if (length)
430 *length = 0;
431 if (*status)
432 *status = res;
433 return 0.0;
434}
435

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

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