/[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 63 Revision 182
16 along with this program; if not, write to the Free Software 16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18*/ 18*/
19 19
20#include "config.h" 20#include "config.h"
21#include "compat.h"
22
21#include <stdlib.h> 23#include <stdlib.h>
24#include <math.h>
22 25
23#include "parse_routines.h" 26#include "parse_routines.h"
27#include "error.h"
24 28
29#ifdef DMALLOC
30#include <dmalloc.h>
31#endif
32
33static psiconv_float_t pow2(int n);
34static psiconv_string_t psiconv_read_string_aux(const psiconv_config config,
35 const psiconv_buffer buf,int lev,
36 psiconv_u32 off,int *length, int *status,
37 int kind);
38
39/* Very inefficient, but good enough for now. By implementing it ourselves,
40 we do not have to link with -lm */
41psiconv_float_t pow2(int n)
42{
43 psiconv_float_t res=1.0;
44 int i;
45
46 for (i = 0; i < (n<0?-n:n); i++)
47 res *= 2.0;
48
49 return n<0?1/res:res;
50}
25psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off) 51psiconv_u8 psiconv_read_u8(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
52 int *status)
26{ 53{
27 psiconv_u8 *ptr; 54 psiconv_u8 *ptr;
28 ptr = psiconv_list_get(buf,off); 55 ptr = psiconv_buffer_get(buf,off);
29 if (!ptr) 56 if (!ptr) {
30 psiconv_fatal(lev,off,"Trying byte read past the end of the file"); 57 psiconv_warn(config,lev,off,"Trying byte read past the end of the file");
58 if (status)
59 *status = -PSICONV_E_PARSE;
60 return 0;
61 }
62 if (status)
63 *status = 0;
31 return *ptr; 64 return *ptr;
32} 65}
33 66
34psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off) 67psiconv_u16 psiconv_read_u16(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
68 int *status)
35{ 69{
36 psiconv_u8 *ptr0,*ptr1; 70 psiconv_u8 *ptr0,*ptr1;
37 ptr0 = psiconv_list_get(buf,off); 71 ptr0 = psiconv_buffer_get(buf,off);
38 ptr1 = psiconv_list_get(buf,off+1); 72 ptr1 = psiconv_buffer_get(buf,off+1);
39 if (!ptr0 || !ptr1) 73 if (!ptr0 || !ptr1) {
40 psiconv_fatal(lev,off,"Trying word read past the end of the file"); 74 psiconv_warn(config,lev,off,"Trying word read past the end of the file");
75 if (status)
76 *status = -PSICONV_E_PARSE;
77 return 0;
78 }
79 if (status)
80 *status = 0;
41 return *ptr0 + (*ptr1 << 8); 81 return *ptr0 + (*ptr1 << 8);
42} 82}
43 83
44psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off) 84psiconv_u32 psiconv_read_u32(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
85 int *status)
45{ 86{
46 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 87 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
47 ptr0 = psiconv_list_get(buf,off); 88 ptr0 = psiconv_buffer_get(buf,off);
48 ptr1 = psiconv_list_get(buf,off+1); 89 ptr1 = psiconv_buffer_get(buf,off+1);
49 ptr2 = psiconv_list_get(buf,off+2); 90 ptr2 = psiconv_buffer_get(buf,off+2);
50 ptr3 = psiconv_list_get(buf,off+3); 91 ptr3 = psiconv_buffer_get(buf,off+3);
51 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) 92 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
52 psiconv_fatal(lev,off,"Trying long read past the end of the file"); 93 psiconv_warn(config,lev,off,"Trying long read past the end of the file");
94 if (status)
95 *status = -PSICONV_E_PARSE;
96 return 0;
97 }
98 if (status)
99 *status = 0;
53 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); 100 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
54} 101}
55 102
103psiconv_s32 psiconv_read_sint(const psiconv_config config,const psiconv_buffer buf,int lev,psiconv_u32 off,
104 int *length,int *status)
105{
106 int localstatus;
107 psiconv_u32 temp;
108
109 temp=psiconv_read_u32(config,buf,lev,off,&localstatus);
110 if (status)
111 *status = localstatus;
112 if (length)
113 *length = localstatus?0:4;
114
115 return localstatus?0:(temp & 0x7fffffff)*(temp&0x80000000?-1:1);
116}
117
56psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, 118psiconv_S_t psiconv_read_S(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
57 int *length) 119 int *length,int *status)
58{ 120{
59 psiconv_u8 temp; 121 psiconv_u8 temp;
60 psiconv_S_t res; 122 psiconv_S_t res;
61 int len; 123 int len,localstatus;
62 124
63 psiconv_progress(lev+1,off,"Going to read a S length indicator"); 125 psiconv_progress(config,lev+1,off,"Going to read a S length indicator");
64 temp = psiconv_read_u8(buf,lev+2,off); 126 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
127 if (localstatus)
128 goto ERROR;
65 if ((temp & 0x03) == 0x02) { 129 if ((temp & 0x03) == 0x02) {
66 res = psiconv_read_u8(buf,lev+2,off) >> 2; 130 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 2;
131 if (localstatus)
132 goto ERROR;
67 len = 1; 133 len = 1;
68 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 134 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
69 } else if ((temp & 0x07) == 0x03) { 135 } else if ((temp & 0x07) == 0x05) {
70 res = psiconv_read_u16(buf,lev+2,off) >> 3; 136 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 3;
137 if (localstatus)
138 goto ERROR;
71 len = 2; 139 len = 2;
72 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 140 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
73 } else { 141 } else {
74 psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); 142 psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!");
75 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 143 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
76 res = 0; 144 goto ERROR;
77 len = 1;
78 } 145 }
79 146
80 if (length) 147 if (length)
81 *length = len; 148 *length = len;
149 if (status)
150 *status = 0;
82 151
83 psiconv_progress(lev+1,off+len-1, 152 psiconv_progress(config,lev+1,off+len-1,
84 "End of S length indicator (total length: %08x)", len); 153 "End of S length indicator (total length: %08x)", len);
85 154
86 return res; 155 return res;
87}
88 156
157ERROR:
158 psiconv_warn(config,lev+1,off,"Reading of S indicator failed");
159 if (status)
160 *status = localstatus;
161 if (length)
162 *length = 0;
163 return 0;
164}
165
89psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off, 166psiconv_X_t psiconv_read_X(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
90 int *length) 167 int *length, int *status)
91{ 168{
92 psiconv_u8 temp; 169 psiconv_u8 temp;
93 psiconv_X_t res; 170 psiconv_X_t res;
94 int len; 171 int len,localstatus;
95 172
96 psiconv_progress(lev+1,off,"Going to read a X length indicator"); 173 psiconv_progress(config,lev+1,off,"Going to read a X length indicator");
97 temp = psiconv_read_u8(buf,lev+2,off); 174 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
175 if (localstatus)
176 goto ERROR;
98 if ((temp & 0x01) == 0x00) { 177 if ((temp & 0x01) == 0x00) {
99 res = psiconv_read_u8(buf,lev+2,off) >> 1; 178 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 1;
179 if (localstatus)
180 goto ERROR;
100 len = 1; 181 len = 1;
101 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 182 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
102 } else if ((temp & 0x03) == 0x01) { 183 } else if ((temp & 0x03) == 0x01) {
103 res = psiconv_read_u16(buf,lev+2,off) >> 2; 184 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 2;
185 if (localstatus)
186 goto ERROR;
104 len = 2; 187 len = 2;
105 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 188 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
106 } else if ((temp & 0x07) == 0x03) { 189 } else if ((temp & 0x07) == 0x03) {
107 res = psiconv_read_u32(buf,lev+2,off) >> 3; 190 res = psiconv_read_u32(config,buf,lev+2,off,&localstatus) >> 3;
191 if (localstatus)
192 goto ERROR;
108 len = 4; 193 len = 4;
109 psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); 194 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res);
110 } else { 195 } else {
111 psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); 196 psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!");
112 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 197 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
113 res = 0; 198 goto ERROR;
114 len = 1;
115 } 199 }
116 200
117 if (length) 201 if (length)
118 *length = len; 202 *length = len;
203 if (status)
204 *status = 0;
119 205
120 psiconv_progress(lev+1,off+len-1, 206 psiconv_progress(config,lev+1,off+len-1,
121 "End of X length indicator (total length: %08x)", len); 207 "End of X length indicator (total length: %08x)", len);
122 208
123 return res; 209 return res;
124}
125 210
211ERROR:
212 psiconv_warn(config,lev+1,off,"Reading of X indicator failed");
213 if (status)
214 *status = localstatus;
215 if (length)
216 *length = 0;
217 return 0;
218}
219
126psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev, 220psiconv_length_t psiconv_read_length(const psiconv_config config,const psiconv_buffer buf, int lev,
127 psiconv_u32 off, int *length) 221 psiconv_u32 off, int *length, int *status)
128{ 222{
129 psiconv_length_t res; 223 psiconv_length_t res;
224 int localstatus;
225
130 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off)); 226 res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,
227 &localstatus));
228 if (localstatus) {
229 psiconv_warn(config,lev+1,off,"Reading of length failed");
230 if (length)
231 *length = 0;
232 if (status)
233 *status = localstatus;
234 return 0;
235 }
131 psiconv_debug(lev+1,off,"Length: %f",res); 236 psiconv_debug(config,lev+1,off,"Length: %f",res);
237 if (length)
238 *length = 4;
239 if (status)
240 *status = 0;
241 return res;
242}
243
244psiconv_size_t psiconv_read_size(const psiconv_config config,const psiconv_buffer buf, int lev,
245 psiconv_u32 off, int *length, int *status)
246{
247 psiconv_size_t res;
248 int localstatus;
249 res = ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,&localstatus)) / 20.0;
250 if (localstatus) {
251 psiconv_warn(config,lev+1,off,"Reading of size failed");
252 if (length)
253 *length = 0;
254 if (status)
255 *status = localstatus;
256 return 0;
257 }
258 psiconv_debug(config,lev+1,off,"Size: %f",res);
259 if (status)
260 *status = 0;
132 if (length) 261 if (length)
133 *length = 4; 262 *length = 4;
134 return res; 263 return res;
135} 264}
136 265
137psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev,
138 psiconv_u32 off, int *length)
139{
140 psiconv_size_t res;
141 res = ((psiconv_s32) psiconv_read_u32(buf,lev,off)) / 20.0;
142 psiconv_debug(lev+1,off,"Size: %f",res);
143 if (length)
144 *length = 4;
145 return res;
146}
147
148int psiconv_parse_bool(const psiconv_buffer buf, int lev, psiconv_u32 off, 266int psiconv_parse_bool(const psiconv_config config,const psiconv_buffer buf, int lev, psiconv_u32 off,
149 int *length, psiconv_bool_t *result) 267 int *length, psiconv_bool_t *result)
150{ 268{
151 psiconv_u8 temp; 269 psiconv_u8 temp;
270 int localstatus;
152 temp = psiconv_read_u8(buf,lev,off); 271 temp = psiconv_read_u8(config,buf,lev,off,&localstatus);
272 if (localstatus) {
273 psiconv_warn(config,lev+1,off,"Reading of bool failed");
274 if (length)
275 *length = 0;
276 return localstatus;
277 }
153 if (length) 278 if (length)
154 *length = 1; 279 *length = 1;
155 if (temp == 0) { 280 if (temp == 0) {
156 *result = psiconv_bool_false; 281 *result = psiconv_bool_false;
157 return 0; 282 return 0;
158 } else if (temp == 1) { 283 } else if (temp == 1) {
159 *result = psiconv_bool_true; 284 *result = psiconv_bool_true;
160 return 0; 285 return 0;
161 } 286 }
162 psiconv_warn(lev+1,off,"Unknown value for boolean"); 287 psiconv_warn(config,lev+1,off,"Unknown value for boolean");
163 psiconv_debug(lev+1,off,"Boolean value: %02x",temp); 288 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp);
164 *result = psiconv_bool_true; 289 *result = psiconv_bool_true;
165 return -1; 290 return 0;
166} 291}
167 292
168psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev, 293psiconv_string_t psiconv_read_string(const psiconv_config config,
294 const psiconv_buffer buf,int lev,
169 psiconv_u32 off,int *length) 295 psiconv_u32 off,int *length, int *status)
170{ 296{
297 return psiconv_read_string_aux(config,buf,lev,off,length,status,0);
298}
299
300psiconv_string_t psiconv_read_short_string(const psiconv_config config,
301 const psiconv_buffer buf,int lev,
302 psiconv_u32 off,int *length, int *status)
303{
304 return psiconv_read_string_aux(config,buf,lev,off,length,status,1);
305}
306
307psiconv_string_t psiconv_read_string_aux(const psiconv_config config,
308 const psiconv_buffer buf,int lev,
309 psiconv_u32 off,int *length, int *status,
310 int kind)
311{
171 int stringlen,i,leng,len; 312 int stringlen,i,leng,len,localstatus;
172 psiconv_string_t result; 313 psiconv_string_t result;
173 char *res_copy; 314 char *res_copy;
174 315
175 psiconv_progress(lev+1,off,"Going to read a string"); 316 psiconv_progress(config,lev+1,off,"Going to read a string");
176 317
318 if (kind == 0)
177 stringlen = psiconv_read_S(buf,lev+2,off,&leng); 319 stringlen = psiconv_read_S(config,buf,lev+2,off,&leng,&localstatus);
320 else {
321 stringlen = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
322 leng = 1;
323 }
324
325 if (localstatus)
326 goto ERROR1;
178 psiconv_debug(lev+2,off,"Length: %i",stringlen); 327 psiconv_debug(config,lev+2,off,"Length: %i",stringlen);
179 len = leng; 328 len = leng;
180 329
181 result = malloc(stringlen + 1); 330 result = malloc(stringlen + 1);
331 if (!result)
332 goto ERROR1;
182 for (i = 0; i < stringlen; i++) 333 for (i = 0; (i < stringlen) && !localstatus; i++)
183 result[i] = psiconv_read_u8(buf,lev,off+i+len); 334 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus);
335 if (localstatus)
336 goto ERROR2;
184 result[stringlen] = 0; 337 result[stringlen] = 0;
185 len += stringlen; 338 len += stringlen;
186 339
187 res_copy = psiconv_make_printable(result); 340 res_copy = psiconv_make_printable(result);
341 if (!res_copy)
342 goto ERROR2;
188 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); 343 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
189 free(res_copy); 344 free(res_copy);
190 345
191 if (length) 346 if (length)
192 *length = len; 347 *length = len;
193 348
349 if (status)
350 *status = 0;
351
194 psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len); 352 psiconv_progress(config,lev+1,off+len-1,"End of string (total length: %08x)",len);
195 353
196 return result; 354 return result;
355
356ERROR2:
357 free(result);
358ERROR1:
359 psiconv_warn(config,lev+1,off,"Reading of string failed");
360 if (status)
361 *status = localstatus;
362 if (length)
363 *length = 0;
364 return NULL;
197} 365}
366
367psiconv_float_t psiconv_read_float(const psiconv_config config,const psiconv_buffer buf, int lev,
368 psiconv_u32 off, int *length, int *status)
369{
370 psiconv_float_t result,bitvalue;
371 int res,bit;
372 psiconv_u32 temp=0;
373
374 psiconv_progress(config,lev+1,off,"Going to read a float");
375
376 bitvalue = 0.5;
377 result = 1.0;
378 for (bit = 0x33; bit > 0; bit--) {
379 if ((bit == 0x33) || ((bit & 0x07) == 0x07))
380 temp = psiconv_read_u8(config,buf,lev+2,off+ (bit >> 3),&res);
381 if (res)
382 goto ERROR;
383 if (temp & (0x01 << (bit & 0x07)))
384 result += bitvalue;
385 bitvalue /= 2.0;
386 }
387 temp = psiconv_read_u16(config,buf,lev+2,off+6,&res);
388 if (res)
389 goto ERROR;
390 if (temp & 0x8000)
391 result = -result;
392 temp = (temp & 0x7ff0) >> 4;
393 result *= pow2(((int) temp)-0x3ff);
394 psiconv_debug(config,lev+1,off,"Float value: %f",result);
395 if (length)
396 *length = 8;
397 if (*status)
398 *status = res;
399 return result;
400ERROR:
401 psiconv_warn(config,lev+1,off,"Reading of float failed");
402 if (length)
403 *length = 0;
404 if (*status)
405 *status = res;
406 return 0.0;
407}
408

Legend:
Removed from v.63  
changed lines
  Added in v.182

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