… | |
… | |
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 | |
… | |
… | |
52 | int *status) |
53 | int *status) |
53 | { |
54 | { |
54 | psiconv_u8 *ptr; |
55 | psiconv_u8 *ptr; |
55 | ptr = psiconv_buffer_get(buf,off); |
56 | ptr = psiconv_buffer_get(buf,off); |
56 | if (!ptr) { |
57 | if (!ptr) { |
57 | 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"); |
58 | if (status) |
59 | if (status) |
59 | *status = -PSICONV_E_PARSE; |
60 | *status = -PSICONV_E_PARSE; |
60 | return 0; |
61 | return 0; |
61 | } |
62 | } |
62 | if (status) |
63 | if (status) |
… | |
… | |
69 | { |
70 | { |
70 | psiconv_u8 *ptr0,*ptr1; |
71 | psiconv_u8 *ptr0,*ptr1; |
71 | ptr0 = psiconv_buffer_get(buf,off); |
72 | ptr0 = psiconv_buffer_get(buf,off); |
72 | ptr1 = psiconv_buffer_get(buf,off+1); |
73 | ptr1 = psiconv_buffer_get(buf,off+1); |
73 | if (!ptr0 || !ptr1) { |
74 | if (!ptr0 || !ptr1) { |
74 | 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"); |
75 | if (status) |
76 | if (status) |
76 | *status = -PSICONV_E_PARSE; |
77 | *status = -PSICONV_E_PARSE; |
77 | return 0; |
78 | return 0; |
78 | } |
79 | } |
79 | if (status) |
80 | if (status) |
… | |
… | |
88 | ptr0 = psiconv_buffer_get(buf,off); |
89 | ptr0 = psiconv_buffer_get(buf,off); |
89 | ptr1 = psiconv_buffer_get(buf,off+1); |
90 | ptr1 = psiconv_buffer_get(buf,off+1); |
90 | ptr2 = psiconv_buffer_get(buf,off+2); |
91 | ptr2 = psiconv_buffer_get(buf,off+2); |
91 | ptr3 = psiconv_buffer_get(buf,off+3); |
92 | ptr3 = psiconv_buffer_get(buf,off+3); |
92 | if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { |
93 | if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { |
93 | 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"); |
94 | if (status) |
95 | if (status) |
95 | *status = -PSICONV_E_PARSE; |
96 | *status = -PSICONV_E_PARSE; |
96 | return 0; |
97 | return 0; |
97 | } |
98 | } |
98 | if (status) |
99 | if (status) |
… | |
… | |
113 | *length = localstatus?0:4; |
114 | *length = localstatus?0:4; |
114 | |
115 | |
115 | return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1); |
116 | return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1); |
116 | } |
117 | } |
117 | |
118 | |
118 | psiconv_S_t psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, |
119 | psiconv_u32 psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, |
119 | int *length,int *status) |
120 | int *length,int *status) |
120 | { |
121 | { |
121 | psiconv_u8 temp; |
122 | psiconv_u8 temp; |
122 | psiconv_S_t res; |
123 | psiconv_u32 res; |
123 | int len,localstatus; |
124 | int len,localstatus; |
124 | |
125 | |
125 | 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"); |
126 | temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus); |
127 | temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus); |
127 | if (localstatus) |
128 | if (localstatus) |
… | |
… | |
137 | if (localstatus) |
138 | if (localstatus) |
138 | goto ERROR; |
139 | goto ERROR; |
139 | len = 2; |
140 | len = 2; |
140 | psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res); |
141 | psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res); |
141 | } else { |
142 | } else { |
142 | psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!"); |
143 | psiconv_error(config,lev+2,off,"S indicator: unknown encoding!"); |
143 | 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); |
144 | goto ERROR; |
145 | goto ERROR; |
145 | } |
146 | } |
146 | |
147 | |
147 | if (length) |
148 | if (length) |
… | |
… | |
153 | "End of S length indicator (total length: %08x)", len); |
154 | "End of S length indicator (total length: %08x)", len); |
154 | |
155 | |
155 | return res; |
156 | return res; |
156 | |
157 | |
157 | ERROR: |
158 | ERROR: |
158 | psiconv_warn(config,lev+1,off,"Reading of S indicator failed"); |
159 | psiconv_error(config,lev+1,off,"Reading of S indicator failed"); |
159 | if (status) |
160 | if (status) |
160 | *status = localstatus; |
161 | *status = localstatus; |
161 | if (length) |
162 | if (length) |
162 | *length = 0; |
163 | *length = 0; |
163 | return 0; |
164 | return 0; |
164 | } |
165 | } |
165 | |
166 | |
166 | psiconv_X_t psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, |
167 | psiconv_u32 psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off, |
167 | int *length, int *status) |
168 | int *length, int *status) |
168 | { |
169 | { |
169 | psiconv_u8 temp; |
170 | psiconv_u8 temp; |
170 | psiconv_X_t res; |
171 | psiconv_u32 res; |
171 | int len,localstatus; |
172 | int len,localstatus; |
172 | |
173 | |
173 | 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"); |
174 | temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus); |
175 | temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus); |
175 | if (localstatus) |
176 | if (localstatus) |
… | |
… | |
191 | if (localstatus) |
192 | if (localstatus) |
192 | goto ERROR; |
193 | goto ERROR; |
193 | len = 4; |
194 | len = 4; |
194 | psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res); |
195 | psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res); |
195 | } else { |
196 | } else { |
196 | psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!"); |
197 | psiconv_error(config,lev+2,off,"X indicator: unknown encoding!"); |
197 | 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); |
198 | goto ERROR; |
199 | goto ERROR; |
199 | } |
200 | } |
200 | |
201 | |
201 | if (length) |
202 | if (length) |
… | |
… | |
207 | "End of X length indicator (total length: %08x)", len); |
208 | "End of X length indicator (total length: %08x)", len); |
208 | |
209 | |
209 | return res; |
210 | return res; |
210 | |
211 | |
211 | ERROR: |
212 | ERROR: |
212 | psiconv_warn(config,lev+1,off,"Reading of X indicator failed"); |
213 | psiconv_error(config,lev+1,off,"Reading of X indicator failed"); |
213 | if (status) |
214 | if (status) |
214 | *status = localstatus; |
215 | *status = localstatus; |
215 | if (length) |
216 | if (length) |
216 | *length = 0; |
217 | *length = 0; |
217 | return 0; |
218 | return 0; |
… | |
… | |
224 | int localstatus; |
225 | int localstatus; |
225 | |
226 | |
226 | 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, |
227 | &localstatus)); |
228 | &localstatus)); |
228 | if (localstatus) { |
229 | if (localstatus) { |
229 | psiconv_warn(config,lev+1,off,"Reading of length failed"); |
230 | psiconv_error(config,lev+1,off,"Reading of length failed"); |
230 | if (length) |
231 | if (length) |
231 | *length = 0; |
232 | *length = 0; |
232 | if (status) |
233 | if (status) |
233 | *status = localstatus; |
234 | *status = localstatus; |
234 | return 0; |
235 | return 0; |
… | |
… | |
246 | { |
247 | { |
247 | psiconv_size_t res; |
248 | psiconv_size_t res; |
248 | int localstatus; |
249 | int localstatus; |
249 | 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; |
250 | if (localstatus) { |
251 | if (localstatus) { |
251 | psiconv_warn(config,lev+1,off,"Reading of size failed"); |
252 | psiconv_error(config,lev+1,off,"Reading of size failed"); |
252 | if (length) |
253 | if (length) |
253 | *length = 0; |
254 | *length = 0; |
254 | if (status) |
255 | if (status) |
255 | *status = localstatus; |
256 | *status = localstatus; |
256 | return 0; |
257 | return 0; |
… | |
… | |
268 | { |
269 | { |
269 | psiconv_u8 temp; |
270 | psiconv_u8 temp; |
270 | int localstatus; |
271 | int localstatus; |
271 | temp = psiconv_read_u8(config,buf,lev,off,&localstatus); |
272 | temp = psiconv_read_u8(config,buf,lev,off,&localstatus); |
272 | if (localstatus) { |
273 | if (localstatus) { |
273 | psiconv_warn(config,lev+1,off,"Reading of bool failed"); |
274 | psiconv_error(config,lev+1,off,"Reading of bool failed"); |
274 | if (length) |
275 | if (length) |
275 | *length = 0; |
276 | *length = 0; |
276 | return localstatus; |
277 | return localstatus; |
277 | } |
278 | } |
278 | if (length) |
279 | if (length) |
… | |
… | |
292 | |
293 | |
293 | psiconv_string_t psiconv_read_string(const psiconv_config config, |
294 | psiconv_string_t psiconv_read_string(const psiconv_config config, |
294 | const psiconv_buffer buf,int lev, |
295 | const psiconv_buffer buf,int lev, |
295 | psiconv_u32 off,int *length, int *status) |
296 | psiconv_u32 off,int *length, int *status) |
296 | { |
297 | { |
297 | return psiconv_read_string_aux(config,buf,lev,off,length,status,0); |
298 | return psiconv_read_string_aux(config,buf,lev,off,length,status,-1); |
298 | } |
299 | } |
299 | |
300 | |
300 | psiconv_string_t psiconv_read_short_string(const psiconv_config config, |
301 | psiconv_string_t psiconv_read_short_string(const psiconv_config config, |
301 | const psiconv_buffer buf,int lev, |
302 | const psiconv_buffer buf,int lev, |
302 | psiconv_u32 off,int *length, int *status) |
303 | psiconv_u32 off,int *length, int *status) |
303 | { |
304 | { |
304 | return psiconv_read_string_aux(config,buf,lev,off,length,status,1); |
305 | return psiconv_read_string_aux(config,buf,lev,off,length,status,-2); |
305 | } |
306 | } |
|
|
307 | |
|
|
308 | psiconv_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 | |
306 | |
324 | |
307 | psiconv_string_t psiconv_read_string_aux(const psiconv_config config, |
325 | psiconv_string_t psiconv_read_string_aux(const psiconv_config config, |
308 | const psiconv_buffer buf,int lev, |
326 | const psiconv_buffer buf,int lev, |
309 | psiconv_u32 off,int *length, int *status, |
327 | psiconv_u32 off,int *length, int *status, |
310 | int kind) |
328 | int kind) |
311 | { |
329 | { |
312 | int stringlen,i,leng,len,localstatus; |
330 | int stringlen,i,leng,len,localstatus; |
313 | psiconv_string_t result; |
331 | psiconv_string_t result; |
314 | char *res_copy; |
332 | char *res_copy; |
|
|
333 | psiconv_u8 temp; |
315 | |
334 | |
316 | psiconv_progress(config,lev+1,off,"Going to read a string"); |
335 | psiconv_progress(config,lev+1,off,"Going to read a string"); |
317 | |
336 | |
318 | if (kind == 0) |
337 | if (kind == -1) |
319 | stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus); |
338 | stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus); |
320 | else { |
339 | else if (kind == -2) { |
321 | stringlen = psiconv_read_u8(config,buf,lev+2,off,&localstatus); |
340 | stringlen = psiconv_read_u8(config,buf,lev+2,off,&localstatus); |
322 | leng = 1; |
341 | leng = 1; |
|
|
342 | } else { |
|
|
343 | stringlen = kind; |
|
|
344 | leng = 0; |
|
|
345 | localstatus = 0; |
323 | } |
346 | } |
324 | |
|
|
325 | if (localstatus) |
347 | if (localstatus) |
326 | goto ERROR1; |
348 | goto ERROR1; |
327 | psiconv_debug(config,lev+2,off,"Length: %i",stringlen); |
349 | psiconv_debug(config,lev+2,off,"Length: %i",stringlen); |
328 | len = leng; |
350 | len = leng; |
329 | |
351 | |
330 | result = malloc(stringlen + 1); |
352 | result = malloc(sizeof(*result) * (stringlen + 1)); |
331 | if (!result) |
353 | if (!result) |
332 | goto ERROR1; |
354 | goto ERROR1; |
333 | for (i = 0; (i < stringlen) && !localstatus; i++) |
355 | for (i = 0; i < stringlen; i++) { |
334 | result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus); |
356 | temp = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus); |
335 | if (localstatus) |
357 | if (localstatus) |
336 | goto ERROR2; |
358 | goto ERROR2; |
|
|
359 | result[i] = psiconv_unicode_from_char(config,temp); |
|
|
360 | } |
337 | result[stringlen] = 0; |
361 | result[stringlen] = 0; |
338 | len += stringlen; |
362 | len += stringlen; |
339 | |
363 | |
340 | res_copy = psiconv_make_printable(result); |
364 | res_copy = psiconv_make_printable(config,result); |
341 | if (!res_copy) |
365 | if (!res_copy) |
342 | goto ERROR2; |
366 | goto ERROR2; |
343 | psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy); |
367 | psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy); |
344 | free(res_copy); |
368 | free(res_copy); |
345 | |
369 | |
… | |
… | |
354 | return result; |
378 | return result; |
355 | |
379 | |
356 | ERROR2: |
380 | ERROR2: |
357 | free(result); |
381 | free(result); |
358 | ERROR1: |
382 | ERROR1: |
359 | psiconv_warn(config,lev+1,off,"Reading of string failed"); |
383 | psiconv_error(config,lev+1,off,"Reading of string failed"); |
360 | if (status) |
384 | if (status) |
361 | *status = localstatus; |
385 | *status = localstatus; |
362 | if (length) |
386 | if (length) |
363 | *length = 0; |
387 | *length = 0; |
364 | return NULL; |
388 | return NULL; |
… | |
… | |
396 | *length = 8; |
420 | *length = 8; |
397 | if (*status) |
421 | if (*status) |
398 | *status = res; |
422 | *status = res; |
399 | return result; |
423 | return result; |
400 | ERROR: |
424 | ERROR: |
401 | psiconv_warn(config,lev+1,off,"Reading of float failed"); |
425 | psiconv_error(config,lev+1,off,"Reading of float failed"); |
402 | if (length) |
426 | if (length) |
403 | *length = 0; |
427 | *length = 0; |
404 | if (*status) |
428 | if (*status) |
405 | *status = res; |
429 | *status = res; |
406 | return 0.0; |
430 | return 0.0; |