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

Legend:
Removed from v.64  
changed lines
  Added in v.188

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