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

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

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