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

Legend:
Removed from v.110  
changed lines
  Added in v.233

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