/[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 171 Revision 217
1/* 1/*
2 parse_simple.c - Part of psiconv, a PSION 5 file formats converter 2 parse_simple.c - Part of psiconv, a PSION 5 file formats converter
3 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl>
4 4
5 This program is free software; you can redistribute it and/or modify 5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by 6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version. 8 (at your option) any later version.
23#include <stdlib.h> 23#include <stdlib.h>
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#include "unicode.h"
28 29
29#ifdef DMALLOC 30#ifdef DMALLOC
30#include <dmalloc.h> 31#include <dmalloc.h>
31#endif 32#endif
32 33
33static psiconv_float_t pow2(int n); 34static psiconv_float_t pow2(int n);
35static psiconv_string_t psiconv_read_string_aux(const psiconv_config config,
36 const psiconv_buffer buf,int lev,
37 psiconv_u32 off,int *length, int *status,
38 int kind);
34 39
35/* Very inefficient, but good enough for now. By implementing it ourselves, 40/* Very inefficient, but good enough for now. By implementing it ourselves,
36 we do not have to link with -lm */ 41 we do not have to link with -lm */
37psiconv_float_t pow2(int n) 42psiconv_float_t pow2(int n)
38{ 43{
48 int *status) 53 int *status)
49{ 54{
50 psiconv_u8 *ptr; 55 psiconv_u8 *ptr;
51 ptr = psiconv_buffer_get(buf,off); 56 ptr = psiconv_buffer_get(buf,off);
52 if (!ptr) { 57 if (!ptr) {
53 psiconv_warn(config,lev,off,"Trying byte read past the end of the file"); 58 psiconv_error(config,lev,off,"Trying byte read past the end of the file");
54 if (status) 59 if (status)
55 *status = -PSICONV_E_PARSE; 60 *status = -PSICONV_E_PARSE;
56 return 0; 61 return 0;
57 } 62 }
58 if (status) 63 if (status)
65{ 70{
66 psiconv_u8 *ptr0,*ptr1; 71 psiconv_u8 *ptr0,*ptr1;
67 ptr0 = psiconv_buffer_get(buf,off); 72 ptr0 = psiconv_buffer_get(buf,off);
68 ptr1 = psiconv_buffer_get(buf,off+1); 73 ptr1 = psiconv_buffer_get(buf,off+1);
69 if (!ptr0 || !ptr1) { 74 if (!ptr0 || !ptr1) {
70 psiconv_warn(config,lev,off,"Trying word read past the end of the file"); 75 psiconv_error(config,lev,off,"Trying word read past the end of the file");
71 if (status) 76 if (status)
72 *status = -PSICONV_E_PARSE; 77 *status = -PSICONV_E_PARSE;
73 return 0; 78 return 0;
74 } 79 }
75 if (status) 80 if (status)
84 ptr0 = psiconv_buffer_get(buf,off); 89 ptr0 = psiconv_buffer_get(buf,off);
85 ptr1 = psiconv_buffer_get(buf,off+1); 90 ptr1 = psiconv_buffer_get(buf,off+1);
86 ptr2 = psiconv_buffer_get(buf,off+2); 91 ptr2 = psiconv_buffer_get(buf,off+2);
87 ptr3 = psiconv_buffer_get(buf,off+3); 92 ptr3 = psiconv_buffer_get(buf,off+3);
88 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 93 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
89 psiconv_warn(config,lev,off,"Trying long read past the end of the file"); 94 psiconv_error(config,lev,off,"Trying long read past the end of the file");
90 if (status) 95 if (status)
91 *status = -PSICONV_E_PARSE; 96 *status = -PSICONV_E_PARSE;
92 return 0; 97 return 0;
93 } 98 }
94 if (status) 99 if (status)
109 *length = localstatus?0:4; 114 *length = localstatus?0:4;
110 115
111 return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1); 116 return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1);
112} 117}
113 118
114psiconv_S_t psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, 119psiconv_u32 psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
115 int *length,int *status) 120 int *length,int *status)
116{ 121{
117 psiconv_u8 temp; 122 psiconv_u8 temp;
118 psiconv_S_t res; 123 psiconv_u32 res;
119 int len,localstatus; 124 int len,localstatus;
120 125
121 psiconv_progress(config,lev+1,off,"Going to read a S length indicator"); 126 psiconv_progress(config,lev+1,off,"Going to read a S length indicator");
122 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus); 127 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
123 if (localstatus) 128 if (localstatus)
133 if (localstatus) 138 if (localstatus)
134 goto ERROR; 139 goto ERROR;
135 len = 2; 140 len = 2;
136 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res); 141 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
137 } else { 142 } else {
138 psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!"); 143 psiconv_error(config,lev+2,off,"S indicator: unknown encoding!");
139 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp); 144 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
140 goto ERROR; 145 goto ERROR;
141 } 146 }
142 147
143 if (length) 148 if (length)
149 "End of S length indicator (total length: %08x)", len); 154 "End of S length indicator (total length: %08x)", len);
150 155
151 return res; 156 return res;
152 157
153ERROR: 158ERROR:
154 psiconv_warn(config,lev+1,off,"Reading of S indicator failed"); 159 psiconv_error(config,lev+1,off,"Reading of S indicator failed");
155 if (status) 160 if (status)
156 *status = localstatus; 161 *status = localstatus;
157 if (length) 162 if (length)
158 *length = 0; 163 *length = 0;
159 return 0; 164 return 0;
160} 165}
161 166
162psiconv_X_t psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, 167psiconv_u32 psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
163 int *length, int *status) 168 int *length, int *status)
164{ 169{
165 psiconv_u8 temp; 170 psiconv_u8 temp;
166 psiconv_X_t res; 171 psiconv_u32 res;
167 int len,localstatus; 172 int len,localstatus;
168 173
169 psiconv_progress(config,lev+1,off,"Going to read a X length indicator"); 174 psiconv_progress(config,lev+1,off,"Going to read a X length indicator");
170 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus); 175 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
171 if (localstatus) 176 if (localstatus)
187 if (localstatus) 192 if (localstatus)
188 goto ERROR; 193 goto ERROR;
189 len = 4; 194 len = 4;
190 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res); 195 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res);
191 } else { 196 } else {
192 psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!"); 197 psiconv_error(config,lev+2,off,"X indicator: unknown encoding!");
193 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp); 198 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
194 goto ERROR; 199 goto ERROR;
195 } 200 }
196 201
197 if (length) 202 if (length)
203 "End of X length indicator (total length: %08x)", len); 208 "End of X length indicator (total length: %08x)", len);
204 209
205 return res; 210 return res;
206 211
207ERROR: 212ERROR:
208 psiconv_warn(config,lev+1,off,"Reading of X indicator failed"); 213 psiconv_error(config,lev+1,off,"Reading of X indicator failed");
209 if (status) 214 if (status)
210 *status = localstatus; 215 *status = localstatus;
211 if (length) 216 if (length)
212 *length = 0; 217 *length = 0;
213 return 0; 218 return 0;
220 int localstatus; 225 int localstatus;
221 226
222 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(config,buf,lev,off, 227 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,
223 &localstatus)); 228 &localstatus));
224 if (localstatus) { 229 if (localstatus) {
225 psiconv_warn(config,lev+1,off,"Reading of length failed"); 230 psiconv_error(config,lev+1,off,"Reading of length failed");
226 if (length) 231 if (length)
227 *length = 0; 232 *length = 0;
228 if (status) 233 if (status)
229 *status = localstatus; 234 *status = localstatus;
230 return 0; 235 return 0;
242{ 247{
243 psiconv_size_t res; 248 psiconv_size_t res;
244 int localstatus; 249 int localstatus;
245 res = ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,&localstatus)) / 20.0; 250 res = ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,&localstatus)) / 20.0;
246 if (localstatus) { 251 if (localstatus) {
247 psiconv_warn(config,lev+1,off,"Reading of size failed"); 252 psiconv_error(config,lev+1,off,"Reading of size failed");
248 if (length) 253 if (length)
249 *length = 0; 254 *length = 0;
250 if (status) 255 if (status)
251 *status = localstatus; 256 *status = localstatus;
252 return 0; 257 return 0;
264{ 269{
265 psiconv_u8 temp; 270 psiconv_u8 temp;
266 int localstatus; 271 int localstatus;
267 temp = psiconv_read_u8(config,buf,lev,off,&localstatus); 272 temp = psiconv_read_u8(config,buf,lev,off,&localstatus);
268 if (localstatus) { 273 if (localstatus) {
269 psiconv_warn(config,lev+1,off,"Reading of bool failed"); 274 psiconv_error(config,lev+1,off,"Reading of bool failed");
270 if (length) 275 if (length)
271 *length = 0; 276 *length = 0;
272 return localstatus; 277 return localstatus;
273 } 278 }
274 if (length) 279 if (length)
284 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp); 289 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp);
285 *result = psiconv_bool_true; 290 *result = psiconv_bool_true;
286 return 0; 291 return 0;
287} 292}
288 293
289psiconv_string_t psiconv_read_string(const psiconv_config config,const psiconv_buffer buf,int lev, 294psiconv_string_t psiconv_read_string(const psiconv_config config,
295 const psiconv_buffer buf,int lev,
290 psiconv_u32 off,int *length, int *status) 296 psiconv_u32 off,int *length, int *status)
291{ 297{
298 return psiconv_read_string_aux(config,buf,lev,off,length,status,-1);
299}
300
301psiconv_string_t psiconv_read_short_string(const psiconv_config config,
302 const psiconv_buffer buf,int lev,
303 psiconv_u32 off,int *length, int *status)
304{
305 return psiconv_read_string_aux(config,buf,lev,off,length,status,-2);
306}
307
308psiconv_string_t psiconv_read_charlist(const psiconv_config config,
309 const psiconv_buffer buf, int lev,
310 psiconv_u32 off, int nrofchars,
311 int *status)
312{
313 int length;
314 if (nrofchars <= 0) {
315 psiconv_error(config,lev,off,
316 "psiconv_read_charlist called with non-positive nrofchars");
317 if (status)
318 *status = -PSICONV_E_OTHER;
319 return NULL;
320 }
321 return psiconv_read_string_aux(config,buf,lev,off,&length,status,nrofchars);
322}
323
324
325psiconv_string_t psiconv_read_string_aux(const psiconv_config config,
326 const psiconv_buffer buf,int lev,
327 psiconv_u32 off,int *length, int *status,
328 int kind)
329{
292 int stringlen,i,leng,len,localstatus; 330 int bytecount,i,leng,len,localstatus;
293 psiconv_string_t result; 331 psiconv_string_t result;
294 char *res_copy; 332 char *res_copy;
333 psiconv_list string;
334 psiconv_ucs2 nextchar;
335 psiconv_ucs2 *nextcharptr;
295 336
296 psiconv_progress(config,lev+1,off,"Going to read a string"); 337 psiconv_progress(config,lev+1,off,"Going to read a string");
297 338
339 if (kind == -1)
298 stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus); 340 bytecount = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus);
341 else if (kind == -2) {
342 bytecount = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
343 leng = 1;
344 } else {
345 bytecount = kind;
346 leng = 0;
347 localstatus = 0;
348 }
299 if (localstatus) 349 if (localstatus)
300 goto ERROR1; 350 goto ERROR1;
301 psiconv_debug(config,lev+2,off,"Length: %i",stringlen); 351 psiconv_debug(config,lev+2,off,"Length: %i",bytecount);
302 len = leng; 352 len = leng;
303 353
304 result = malloc(stringlen + 1); 354 if (!(string = psiconv_list_new(sizeof(*result))))
305 if (!result)
306 goto ERROR1; 355 goto ERROR1;
307 for (i = 0; (i < stringlen) && !localstatus; i++) 356
308 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus); 357 /* Read the string into a temporary list */
358 i = 0;
359 while (i < bytecount) {
360 nextchar = psiconv_unicode_read_char(config,buf,lev,off+i+len,
361 &leng,&localstatus);
309 if (localstatus) 362 if (localstatus)
363 goto ERROR2;
364 if ((localstatus = psiconv_list_add(string,&nextchar)))
365 goto ERROR2;
366 i += leng;
367 }
368 if (i > bytecount) {
369 psiconv_error(config,lev,off+i+len,"Malformed string");
370 localstatus = PSICONV_E_PARSE;
310 goto ERROR2; 371 goto ERROR2;
372 }
373 len += bytecount;
374
375 /* Copy the list to the actual string */
376 if (!(result = malloc(sizeof(*result) * (psiconv_list_length(string) + 1))))
377 goto ERROR2;
378 for (i = 0; i < psiconv_list_length(string); i++) {
379 if (!(nextcharptr = psiconv_list_get(string,i))) {
380 psiconv_error(config,lev,off+i+len,"Internal data corruption");
381 goto ERROR3;
382 }
383 result[i] = *nextcharptr;
384 }
311 result[stringlen] = 0; 385 result[i] = 0;
312 len += stringlen;
313 386
314 res_copy = psiconv_make_printable(result); 387 res_copy = psiconv_make_printable(config,result);
315 if (!res_copy) 388 if (!res_copy)
316 goto ERROR2; 389 goto ERROR3;
317 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy); 390 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
318 free(res_copy); 391 free(res_copy);
319 392
393 psiconv_list_free(string);
394
320 if (length) 395 if (length)
321 *length = len; 396 *length = len;
322 397
323 if (status) 398 if (status)
324 *status = 0; 399 *status = 0;
325 400
326 psiconv_progress(config,lev+1,off+len-1,"End of string (total length: %08x)",len); 401 psiconv_progress(config,lev+1,off+len-1,"End of string (total length: %08x)",len);
327 402
328 return result; 403 return result;
329 404
405ERROR3:
406 free(result);
330ERROR2: 407ERROR2:
331 free(result); 408 psiconv_list_free(string);
332ERROR1: 409ERROR1:
333 psiconv_warn(config,lev+1,off,"Reading of string failed"); 410 psiconv_error(config,lev+1,off,"Reading of string failed");
334 if (status)
335 *status = localstatus;
336 if (length)
337 *length = 0;
338 return NULL;
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) 411 if (status)
390 *status = localstatus; 412 *status = localstatus;
391 if (length) 413 if (length)
392 *length = 0; 414 *length = 0;
393 return NULL; 415 return NULL;
425 *length = 8; 447 *length = 8;
426 if (*status) 448 if (*status)
427 *status = res; 449 *status = res;
428 return result; 450 return result;
429ERROR: 451ERROR:
430 psiconv_warn(config,lev+1,off,"Reading of float failed"); 452 psiconv_error(config,lev+1,off,"Reading of float failed");
431 if (length) 453 if (length)
432 *length = 0; 454 *length = 0;
433 if (*status) 455 if (*status)
434 *status = res; 456 *status = res;
435 return 0.0; 457 return 0.0;

Legend:
Removed from v.171  
changed lines
  Added in v.217

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