/[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 64 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,
26 int *status) 52 int *status)
27{ 53{
28 psiconv_u8 *ptr; 54 psiconv_u8 *ptr;
29 ptr = psiconv_list_get(buf,off); 55 ptr = psiconv_buffer_get(buf,off);
30 if (!ptr) { 56 if (!ptr) {
31 psiconv_warn(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");
32 if (status) 58 if (status)
33 *status = -PSICONV_E_PARSE; 59 *status = -PSICONV_E_PARSE;
34 return 0; 60 return 0;
35 } 61 }
36 if (status) 62 if (status)
37 *status = 0; 63 *status = 0;
38 return *ptr; 64 return *ptr;
39} 65}
40 66
41psiconv_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,
42 int *status) 68 int *status)
43{ 69{
44 psiconv_u8 *ptr0,*ptr1; 70 psiconv_u8 *ptr0,*ptr1;
45 ptr0 = psiconv_list_get(buf,off); 71 ptr0 = psiconv_buffer_get(buf,off);
46 ptr1 = psiconv_list_get(buf,off+1); 72 ptr1 = psiconv_buffer_get(buf,off+1);
47 if (!ptr0 || !ptr1) { 73 if (!ptr0 || !ptr1) {
48 psiconv_warn(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");
49 if (status) 75 if (status)
50 *status = -PSICONV_E_PARSE; 76 *status = -PSICONV_E_PARSE;
51 return 0; 77 return 0;
52 } 78 }
53 if (status) 79 if (status)
54 *status = 0; 80 *status = 0;
55 return *ptr0 + (*ptr1 << 8); 81 return *ptr0 + (*ptr1 << 8);
56} 82}
57 83
58psiconv_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,
59 int *status) 85 int *status)
60{ 86{
61 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; 87 psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
62 ptr0 = psiconv_list_get(buf,off); 88 ptr0 = psiconv_buffer_get(buf,off);
63 ptr1 = psiconv_list_get(buf,off+1); 89 ptr1 = psiconv_buffer_get(buf,off+1);
64 ptr2 = psiconv_list_get(buf,off+2); 90 ptr2 = psiconv_buffer_get(buf,off+2);
65 ptr3 = psiconv_list_get(buf,off+3); 91 ptr3 = psiconv_buffer_get(buf,off+3);
66 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { 92 if (!ptr0 || !ptr1 || !ptr2 || !ptr3) {
67 psiconv_warn(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");
68 if (status) 94 if (status)
69 *status = -PSICONV_E_PARSE; 95 *status = -PSICONV_E_PARSE;
70 return 0; 96 return 0;
71 } 97 }
72 if (status) 98 if (status)
73 *status = 0; 99 *status = 0;
74 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); 100 return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
75} 101}
76 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
77psiconv_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,
78 int *length,int *status) 119 int *length,int *status)
79{ 120{
80 psiconv_u8 temp; 121 psiconv_u8 temp;
81 psiconv_S_t res; 122 psiconv_S_t res;
82 int len,localstatus; 123 int len,localstatus;
83 124
84 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");
85 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 126 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
86 if (localstatus) 127 if (localstatus)
87 goto ERROR; 128 goto ERROR;
88 if ((temp & 0x03) == 0x02) { 129 if ((temp & 0x03) == 0x02) {
89 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; 130 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 2;
90 if (localstatus) 131 if (localstatus)
91 goto ERROR; 132 goto ERROR;
92 len = 1; 133 len = 1;
93 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 134 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
94 } else if ((temp & 0x07) == 0x03) { 135 } else if ((temp & 0x07) == 0x05) {
95 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; 136 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 3;
96 if (localstatus) 137 if (localstatus)
97 goto ERROR; 138 goto ERROR;
98 len = 2; 139 len = 2;
99 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 140 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
100 } else { 141 } else {
101 psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); 142 psiconv_warn(config,lev+2,off,"S indicator: unknown encoding!");
102 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 143 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
103 goto ERROR; 144 goto ERROR;
104 } 145 }
105 146
106 if (length) 147 if (length)
107 *length = len; 148 *length = len;
108 if (status) 149 if (status)
109 *status = 0; 150 *status = 0;
110 151
111 psiconv_progress(lev+1,off+len-1, 152 psiconv_progress(config,lev+1,off+len-1,
112 "End of S length indicator (total length: %08x)", len); 153 "End of S length indicator (total length: %08x)", len);
113 154
114 return res; 155 return res;
115 156
116ERROR: 157ERROR:
117 psiconv_warn(lev+1,off,"Reading of S indicator failed"); 158 psiconv_warn(config,lev+1,off,"Reading of S indicator failed");
118 if (status) 159 if (status)
119 *status = localstatus; 160 *status = localstatus;
120 if (length) 161 if (length)
121 *length = 0; 162 *length = 0;
122 return 0; 163 return 0;
123} 164}
124 165
125psiconv_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,
126 int *length, int *status) 167 int *length, int *status)
127{ 168{
128 psiconv_u8 temp; 169 psiconv_u8 temp;
129 psiconv_X_t res; 170 psiconv_X_t res;
130 int len,localstatus; 171 int len,localstatus;
131 172
132 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");
133 temp = psiconv_read_u8(buf,lev+2,off,&localstatus); 174 temp = psiconv_read_u8(config,buf,lev+2,off,&localstatus);
134 if (localstatus) 175 if (localstatus)
135 goto ERROR; 176 goto ERROR;
136 if ((temp & 0x01) == 0x00) { 177 if ((temp & 0x01) == 0x00) {
137 res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 1; 178 res = psiconv_read_u8(config,buf,lev+2,off,&localstatus) >> 1;
138 if (localstatus) 179 if (localstatus)
139 goto ERROR; 180 goto ERROR;
140 len = 1; 181 len = 1;
141 psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); 182 psiconv_debug(config,lev+2,off,"Indicator (1 byte): %02x",res);
142 } else if ((temp & 0x03) == 0x01) { 183 } else if ((temp & 0x03) == 0x01) {
143 res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 2; 184 res = psiconv_read_u16(config,buf,lev+2,off,&localstatus) >> 2;
144 if (localstatus) 185 if (localstatus)
145 goto ERROR; 186 goto ERROR;
146 len = 2; 187 len = 2;
147 psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); 188 psiconv_debug(config,lev+2,off,"Indicator (2 bytes): %04x",res);
148 } else if ((temp & 0x07) == 0x03) { 189 } else if ((temp & 0x07) == 0x03) {
149 res = psiconv_read_u32(buf,lev+2,off,&localstatus) >> 3; 190 res = psiconv_read_u32(config,buf,lev+2,off,&localstatus) >> 3;
150 if (localstatus) 191 if (localstatus)
151 goto ERROR; 192 goto ERROR;
152 len = 4; 193 len = 4;
153 psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); 194 psiconv_debug(config,lev+2,off,"Indicator (4 bytes): %08x",res);
154 } else { 195 } else {
155 psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); 196 psiconv_warn(config,lev+2,off,"X indicator: unknown encoding!");
156 psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); 197 psiconv_debug(config,lev+2,off,"Raw data first byte: %02x",temp);
157 goto ERROR; 198 goto ERROR;
158 } 199 }
159 200
160 if (length) 201 if (length)
161 *length = len; 202 *length = len;
162 if (status) 203 if (status)
163 *status = 0; 204 *status = 0;
164 205
165 psiconv_progress(lev+1,off+len-1, 206 psiconv_progress(config,lev+1,off+len-1,
166 "End of X length indicator (total length: %08x)", len); 207 "End of X length indicator (total length: %08x)", len);
167 208
168 return res; 209 return res;
169 210
170ERROR: 211ERROR:
171 psiconv_warn(lev+1,off,"Reading of X indicator failed"); 212 psiconv_warn(config,lev+1,off,"Reading of X indicator failed");
172 if (status) 213 if (status)
173 *status = localstatus; 214 *status = localstatus;
174 if (length) 215 if (length)
175 *length = 0; 216 *length = 0;
176 return 0; 217 return 0;
177} 218}
178 219
179psiconv_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,
180 psiconv_u32 off, int *length, int *status) 221 psiconv_u32 off, int *length, int *status)
181{ 222{
182 psiconv_length_t res; 223 psiconv_length_t res;
183 int localstatus; 224 int localstatus;
184 225
185 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,
186 &localstatus)); 227 &localstatus));
187 if (localstatus) { 228 if (localstatus) {
188 psiconv_warn(lev+1,off,"Reading of length failed"); 229 psiconv_warn(config,lev+1,off,"Reading of length failed");
189 if (length) 230 if (length)
190 *length = 0; 231 *length = 0;
191 if (status) 232 if (status)
192 *status = localstatus; 233 *status = localstatus;
193 return 0; 234 return 0;
194 } 235 }
195 psiconv_debug(lev+1,off,"Length: %f",res); 236 psiconv_debug(config,lev+1,off,"Length: %f",res);
196 if (length) 237 if (length)
197 *length = 4; 238 *length = 4;
198 if (status) 239 if (status)
199 *status = 0; 240 *status = 0;
200 return res; 241 return res;
201} 242}
202 243
203psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev, 244psiconv_size_t psiconv_read_size(const psiconv_config config,const psiconv_buffer buf, int lev,
204 psiconv_u32 off, int *length, int *status) 245 psiconv_u32 off, int *length, int *status)
205{ 246{
206 psiconv_size_t res; 247 psiconv_size_t res;
207 int localstatus; 248 int localstatus;
208 res = ((psiconv_s32) psiconv_read_u32(buf,lev,off,&localstatus)) / 20.0; 249 res = ((psiconv_s32) psiconv_read_u32(config,buf,lev,off,&localstatus)) / 20.0;
209 if (localstatus) { 250 if (localstatus) {
210 psiconv_warn(lev+1,off,"Reading of size failed"); 251 psiconv_warn(config,lev+1,off,"Reading of size failed");
211 if (length) 252 if (length)
212 *length = 0; 253 *length = 0;
213 if (status) 254 if (status)
214 *status = localstatus; 255 *status = localstatus;
215 return 0; 256 return 0;
216 } 257 }
217 psiconv_debug(lev+1,off,"Size: %f",res); 258 psiconv_debug(config,lev+1,off,"Size: %f",res);
218 if (status) 259 if (status)
219 *status = 0; 260 *status = 0;
220 if (length) 261 if (length)
221 *length = 4; 262 *length = 4;
222 return res; 263 return res;
223} 264}
224 265
225int 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,
226 int *length, psiconv_bool_t *result) 267 int *length, psiconv_bool_t *result)
227{ 268{
228 psiconv_u8 temp; 269 psiconv_u8 temp;
229 int localstatus; 270 int localstatus;
230 temp = psiconv_read_u8(buf,lev,off,&localstatus); 271 temp = psiconv_read_u8(config,buf,lev,off,&localstatus);
231 if (localstatus) { 272 if (localstatus) {
232 psiconv_warn(lev+1,off,"Reading of bool failed"); 273 psiconv_warn(config,lev+1,off,"Reading of bool failed");
233 if (length) 274 if (length)
234 *length = 0; 275 *length = 0;
235 return localstatus; 276 return localstatus;
236 } 277 }
237 if (length) 278 if (length)
241 return 0; 282 return 0;
242 } else if (temp == 1) { 283 } else if (temp == 1) {
243 *result = psiconv_bool_true; 284 *result = psiconv_bool_true;
244 return 0; 285 return 0;
245 } 286 }
246 psiconv_warn(lev+1,off,"Unknown value for boolean"); 287 psiconv_warn(config,lev+1,off,"Unknown value for boolean");
247 psiconv_debug(lev+1,off,"Boolean value: %02x",temp); 288 psiconv_debug(config,lev+1,off,"Boolean value: %02x",temp);
248 *result = psiconv_bool_true; 289 *result = psiconv_bool_true;
249 return 0; 290 return 0;
250} 291}
251 292
252psiconv_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,
253 psiconv_u32 off,int *length, int *status) 295 psiconv_u32 off,int *length, int *status)
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)
254{ 311{
255 int stringlen,i,leng,len,localstatus; 312 int stringlen,i,leng,len,localstatus;
256 psiconv_string_t result; 313 psiconv_string_t result;
257 char *res_copy; 314 char *res_copy;
258 315
259 psiconv_progress(lev+1,off,"Going to read a string"); 316 psiconv_progress(config,lev+1,off,"Going to read a string");
260 317
318 if (kind == 0)
261 stringlen = psiconv_read_S(buf,lev+2,off,&leng,&localstatus); 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
262 if (localstatus) 325 if (localstatus)
263 goto ERROR1; 326 goto ERROR1;
264 psiconv_debug(lev+2,off,"Length: %i",stringlen); 327 psiconv_debug(config,lev+2,off,"Length: %i",stringlen);
265 len = leng; 328 len = leng;
266 329
267 result = malloc(stringlen + 1); 330 result = malloc(stringlen + 1);
268 if (!result) 331 if (!result)
269 goto ERROR1; 332 goto ERROR1;
270 for (i = 0; (i < stringlen) && !localstatus; i++) 333 for (i = 0; (i < stringlen) && !localstatus; i++)
271 result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); 334 result[i] = psiconv_read_u8(config,buf,lev,off+i+len,&localstatus);
272 if (localstatus) 335 if (localstatus)
273 goto ERROR2; 336 goto ERROR2;
274 result[stringlen] = 0; 337 result[stringlen] = 0;
275 len += stringlen; 338 len += stringlen;
276 339
277 res_copy = psiconv_make_printable(result); 340 res_copy = psiconv_make_printable(result);
278 if (!res_copy) 341 if (!res_copy)
279 goto ERROR2; 342 goto ERROR2;
280 psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); 343 psiconv_debug(config,lev+2,off,"Contents: `%s'",res_copy);
281 free(res_copy); 344 free(res_copy);
282 345
283 if (length) 346 if (length)
284 *length = len; 347 *length = len;
285 348
286 if (status) 349 if (status)
287 *status = 0; 350 *status = 0;
288 351
289 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);
290 353
291 return result; 354 return result;
292 355
293ERROR2: 356ERROR2:
294 free(result); 357 free(result);
295ERROR1: 358ERROR1:
296 psiconv_warn(lev+1,off,"Reading of string failed"); 359 psiconv_warn(config,lev+1,off,"Reading of string failed");
297 if (status) 360 if (status)
298 *status = localstatus; 361 *status = localstatus;
299 if (length) 362 if (length)
300 *length = 0; 363 *length = 0;
301 return NULL; 364 return NULL;
302} 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.64  
changed lines
  Added in v.182

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