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