/[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 110
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
28psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, 29psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off,
29 int *status) 30 int *status)
30{ 31{
31 psiconv_u8 *ptr; 32 psiconv_u8 *ptr;
32 ptr = psiconv_list_get(buf,off); 33 ptr = psiconv_buffer_get(buf,off);
33 if (!ptr) { 34 if (!ptr) {
34 psiconv_warn(lev,off,"Trying byte read past the end of the file"); 35 psiconv_warn(lev,off,"Trying byte read past the end of the file");
35 if (status) 36 if (status)
36 *status = -PSICONV_E_PARSE; 37 *status = -PSICONV_E_PARSE;
37 return 0; 38 return 0;
43 44
44psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off, 45psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off,
45 int *status) 46 int *status)
46{ 47{
47 psiconv_u8 *ptr0,*ptr1; 48 psiconv_u8 *ptr0,*ptr1;
48 ptr0 = psiconv_list_get(buf,off); 49 ptr0 = psiconv_buffer_get(buf,off);
49 ptr1 = psiconv_list_get(buf,off+1); 50 ptr1 = psiconv_buffer_get(buf,off+1);
50 if (!ptr0 || !ptr1) { 51 if (!ptr0 || !ptr1) {
51 psiconv_warn(lev,off,"Trying word read past the end of the file"); 52 psiconv_warn(lev,off,"Trying word read past the end of the file");
52 if (status) 53 if (status)
53 *status = -PSICONV_E_PARSE; 54 *status = -PSICONV_E_PARSE;
54 return 0; 55 return 0;
60 61
61psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off, 62psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off,
62 int *status) 63 int *status)
63{ 64{
64 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 65 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
65 ptr0 = psiconv_list_get(buf,off); 66 ptr0 = psiconv_buffer_get(buf,off);
66 ptr1 = psiconv_list_get(buf,off+1); 67 ptr1 = psiconv_buffer_get(buf,off+1);
67 ptr2 = psiconv_list_get(buf,off+2); 68 ptr2 = psiconv_buffer_get(buf,off+2);
68 ptr3 = psiconv_list_get(buf,off+3); 69 ptr3 = psiconv_buffer_get(buf,off+3);
69 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 70 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
70 psiconv_warn(lev,off,"Trying long read past the end of the file"); 71 psiconv_warn(lev,off,"Trying long read past the end of the file");
71 if (status) 72 if (status)
72 *status = -PSICONV_E_PARSE; 73 *status = -PSICONV_E_PARSE;
73 return 0; 74 return 0;
301 *status = localstatus; 302 *status = localstatus;
302 if (length) 303 if (length)
303 *length = 0; 304 *length = 0;
304 return NULL; 305 return NULL;
305} 306}
307
308psiconv_string_t psiconv_read_short_string(const psiconv_buffer buf,
309 int lev,
310 psiconv_u32 off,int *length, int *status)
311{
312 int stringlen,i,len,localstatus;
313 psiconv_string_t result;
314 char *res_copy;
315
316 psiconv_progress(lev+1,off,"Going to read a short string");
317
318 stringlen = psiconv_read_u8(buf,lev+2,off,&localstatus);
319 if (localstatus)
320 goto ERROR1;
321 psiconv_debug(lev+2,off,"Length: %i",stringlen);
322 len = 1;
323
324 result = malloc(stringlen + 1);
325 if (!result)
326 goto ERROR1;
327 for (i = 0; (i < stringlen) && !localstatus; i++)
328 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus);
329 if (localstatus)
330 goto ERROR2;
331 result[stringlen] = 0;
332 len += stringlen;
333
334 res_copy = psiconv_make_printable(result);
335 if (!res_copy)
336 goto ERROR2;
337 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
338 free(res_copy);
339
340 if (length)
341 *length = len;
342
343 if (status)
344 *status = 0;
345
346 psiconv_progress(lev+1,off+len-1,"End of short string (total length: %08x)",
347 len);
348
349 return result;
350
351
352ERROR2:
353 free(result);
354ERROR1:
355 psiconv_warn(lev+1,off,"Reading of short string failed");
356 if (status)
357 *status = localstatus;
358 if (length)
359 *length = 0;
360 return NULL;
361}
362
363psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev,
364 psiconv_u32 off, int *length, int *status)
365{
366 psiconv_float_t result,bitvalue;
367 int res,bit;
368 psiconv_u32 temp=0;
369
370 psiconv_progress(lev+1,off,"Going to read a float");
371
372 bitvalue = 0.5;
373 result = 1.0;
374 for (bit = 0x33; bit > 0; bit--) {
375 if ((bit == 0x33) || ((bit & 0x07) == 0x07))
376 temp = psiconv_read_u8(buf,lev+2,off+ (bit >> 3),&res);
377 if (res)
378 goto ERROR;
379 if (temp & (0x01 << (bit & 0x07)))
380 result += bitvalue;
381 bitvalue /= 2.0;
382 }
383 temp = psiconv_read_u16(buf,lev+2,off+6,&res);
384 if (res)
385 goto ERROR;
386 if (temp & 0x8000)
387 result = -result;
388 temp = (temp & 0x7ff0) >> 4;
389 result *= pow(2.0,((int) temp)-0x3ff);
390 psiconv_debug(lev+1,off,"Float value: %f",result);
391 if (length)
392 *length = 8;
393 if (*status)
394 *status = res;
395 return result;
396ERROR:
397 psiconv_warn(lev+1,off,"Reading of float failed");
398 if (length)
399 *length = 0;
400 if (*status)
401 *status = res;
402 return 0.0;
403}

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

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