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