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