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