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