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