/[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 71 Revision 118
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/* Very inefficient, but good enough for now. By implementing it ourselves,
30 we do not have to link with -lm */
31psiconv_float_t pow2(int n)
32{
33 psiconv_float_t res=1.0;
34 int i;
35
36 for (i = 0; i < (n<0?-n:n); i++)
37 res *= 2.0;
38
39 return n<0?1/res:res;
40}
28psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, 41psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off,
29 int *status) 42 int *status)
30{ 43{
31 psiconv_u8 *ptr; 44 psiconv_u8 *ptr;
32 ptr = psiconv_list_get(buf,off); 45 ptr = psiconv_buffer_get(buf,off);
33 if (!ptr) { 46 if (!ptr) {
34 psiconv_warn(lev,off,"Trying byte read past the end of the file"); 47 psiconv_warn(lev,off,"Trying byte read past the end of the file");
35 if (status) 48 if (status)
36 *status = -PSICONV_E_PARSE; 49 *status = -PSICONV_E_PARSE;
37 return 0; 50 return 0;
43 56
44psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off, 57psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off,
45 int *status) 58 int *status)
46{ 59{
47 psiconv_u8 *ptr0,*ptr1; 60 psiconv_u8 *ptr0,*ptr1;
48 ptr0 = psiconv_list_get(buf,off); 61 ptr0 = psiconv_buffer_get(buf,off);
49 ptr1 = psiconv_list_get(buf,off+1); 62 ptr1 = psiconv_buffer_get(buf,off+1);
50 if (!ptr0 || !ptr1) { 63 if (!ptr0 || !ptr1) {
51 psiconv_warn(lev,off,"Trying word read past the end of the file"); 64 psiconv_warn(lev,off,"Trying word read past the end of the file");
52 if (status) 65 if (status)
53 *status = -PSICONV_E_PARSE; 66 *status = -PSICONV_E_PARSE;
54 return 0; 67 return 0;
60 73
61psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off, 74psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off,
62 int *status) 75 int *status)
63{ 76{
64 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 77 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
65 ptr0 = psiconv_list_get(buf,off); 78 ptr0 = psiconv_buffer_get(buf,off);
66 ptr1 = psiconv_list_get(buf,off+1); 79 ptr1 = psiconv_buffer_get(buf,off+1);
67 ptr2 = psiconv_list_get(buf,off+2); 80 ptr2 = psiconv_buffer_get(buf,off+2);
68 ptr3 = psiconv_list_get(buf,off+3); 81 ptr3 = psiconv_buffer_get(buf,off+3);
69 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 82 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
70 psiconv_warn(lev,off,"Trying long read past the end of the file"); 83 psiconv_warn(lev,off,"Trying long read past the end of the file");
71 if (status) 84 if (status)
72 *status = -PSICONV_E_PARSE; 85 *status = -PSICONV_E_PARSE;
73 return 0; 86 return 0;
301 *status = localstatus; 314 *status = localstatus;
302 if (length) 315 if (length)
303 *length = 0; 316 *length = 0;
304 return NULL; 317 return NULL;
305} 318}
319
320psiconv_string_t psiconv_read_short_string(const psiconv_buffer buf,
321 int lev,
322 psiconv_u32 off,int *length, int *status)
323{
324 int stringlen,i,len,localstatus;
325 psiconv_string_t result;
326 char *res_copy;
327
328 psiconv_progress(lev+1,off,"Going to read a short string");
329
330 stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus);
331 if (localstatus)
332 goto ERROR1;
333 psiconv_debug(lev+2,off,"Length: %i",stringlen);
334 len = 1;
335
336 result = malloc(stringlen + 1);
337 if (!result)
338 goto ERROR1;
339 for (i = 0; (i < stringlen) && !localstatus; i++)
340 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus);
341 if (localstatus)
342 goto ERROR2;
343 result[stringlen] = 0;
344 len += stringlen;
345
346 res_copy = psiconv_make_printable(result);
347 if (!res_copy)
348 goto ERROR2;
349 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
350 free(res_copy);
351
352 if (length)
353 *length = len;
354
355 if (status)
356 *status = 0;
357
358 psiconv_progress(lev+1,off+len-1,"End of short string (total length: %08x)",
359 len);
360
361 return result;
362
363
364ERROR2:
365 free(result);
366ERROR1:
367 psiconv_warn(lev+1,off,"Reading of short string failed");
368 if (status)
369 *status = localstatus;
370 if (length)
371 *length = 0;
372 return NULL;
373}
374
375psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev,
376 psiconv_u32 off, int *length, int *status)
377{
378 psiconv_float_t result,bitvalue;
379 int res,bit;
380 psiconv_u32 temp=0;
381
382 psiconv_progress(lev+1,off,"Going to read a float");
383
384 bitvalue = 0.5;
385 result = 1.0;
386 for (bit = 0x33; bit > 0; bit--) {
387 if ((bit == 0x33) || ((bit & 0x07) == 0x07))
388 temp = psiconv_read_u8(buf,lev+2,off+ (bit >> 3),&res);
389 if (res)
390 goto ERROR;
391 if (temp & (0x01 << (bit & 0x07)))
392 result += bitvalue;
393 bitvalue /= 2.0;
394 }
395 temp = psiconv_read_u16(buf,lev+2,off+6,&res);
396 if (res)
397 goto ERROR;
398 if (temp & 0x8000)
399 result = -result;
400 temp = (temp & 0x7ff0) >> 4;
401 result *= pow2(((int) temp)-0x3ff);
402 psiconv_debug(lev+1,off,"Float value: %f",result);
403 if (length)
404 *length = 8;
405 if (*status)
406 *status = res;
407 return result;
408ERROR:
409 psiconv_warn(lev+1,off,"Reading of float failed");
410 if (length)
411 *length = 0;
412 if (*status)
413 *status = res;
414 return 0.0;
415}
416

Legend:
Removed from v.71  
changed lines
  Added in v.118

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