/[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 101 Revision 118
24#include <math.h> 24#include <math.h>
25 25
26#include "parse_routines.h" 26#include "parse_routines.h"
27#include "error.h" 27#include "error.h"
28 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}
29psiconv_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,
30 int *status) 42 int *status)
31{ 43{
32 psiconv_u8 *ptr; 44 psiconv_u8 *ptr;
33 ptr = psiconv_buffer_get(buf,off); 45 ptr = psiconv_buffer_get(buf,off);
303 if (length) 315 if (length)
304 *length = 0; 316 *length = 0;
305 return NULL; 317 return NULL;
306} 318}
307 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
308psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev, 375psiconv_float_t psiconv_read_float(const psiconv_buffer buf, int lev,
309 psiconv_u32 off, int *length, int *status) 376 psiconv_u32 off, int *length, int *status)
310{ 377{
311 psiconv_float_t result,bitvalue; 378 psiconv_float_t result,bitvalue;
312 int res,bit; 379 int res,bit;
329 if (res) 396 if (res)
330 goto ERROR; 397 goto ERROR;
331 if (temp & 0x8000) 398 if (temp & 0x8000)
332 result = -result; 399 result = -result;
333 temp = (temp & 0x7ff0) >> 4; 400 temp = (temp & 0x7ff0) >> 4;
334 result *= pow(2.0,((int) temp)-0x3ff); 401 result *= pow2(((int) temp)-0x3ff);
335 psiconv_debug(lev+1,off,"Float value: %f",result); 402 psiconv_debug(lev+1,off,"Float value: %f",result);
336 if (length) 403 if (length)
337 *length = 8; 404 *length = 8;
338 if (*status) 405 if (*status)
339 *status = res; 406 *status = res;
344 *length = 0; 411 *length = 0;
345 if (*status) 412 if (*status)
346 *status = res; 413 *status = res;
347 return 0.0; 414 return 0.0;
348} 415}
416

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

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