… | |
… | |
20 | #include "config.h" |
20 | #include "config.h" |
21 | #include <stdlib.h> |
21 | #include <stdlib.h> |
22 | |
22 | |
23 | #include "parse_routines.h" |
23 | #include "parse_routines.h" |
24 | |
24 | |
25 | psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off) |
25 | psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off, |
|
|
26 | int *status) |
26 | { |
27 | { |
27 | psiconv_u8 *ptr; |
28 | psiconv_u8 *ptr; |
28 | ptr = psiconv_list_get(buf,off); |
29 | ptr = psiconv_list_get(buf,off); |
29 | if (!ptr) |
30 | if (!ptr) { |
30 | psiconv_fatal(lev,off,"Trying byte read past the end of the file"); |
31 | psiconv_warn(lev,off,"Trying byte read past the end of the file"); |
|
|
32 | if (status) |
|
|
33 | *status = -PSICONV_E_PARSE; |
|
|
34 | return 0; |
|
|
35 | } |
|
|
36 | if (status) |
|
|
37 | *status = 0; |
31 | return *ptr; |
38 | return *ptr; |
32 | } |
39 | } |
33 | |
40 | |
34 | psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off) |
41 | psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off, |
|
|
42 | int *status) |
35 | { |
43 | { |
36 | psiconv_u8 *ptr0,*ptr1; |
44 | psiconv_u8 *ptr0,*ptr1; |
37 | ptr0 = psiconv_list_get(buf,off); |
45 | ptr0 = psiconv_list_get(buf,off); |
38 | ptr1 = psiconv_list_get(buf,off+1); |
46 | ptr1 = psiconv_list_get(buf,off+1); |
39 | if (!ptr0 || !ptr1) |
47 | if (!ptr0 || !ptr1) { |
40 | psiconv_fatal(lev,off,"Trying word read past the end of the file"); |
48 | psiconv_warn(lev,off,"Trying word read past the end of the file"); |
|
|
49 | if (status) |
|
|
50 | *status = -PSICONV_E_PARSE; |
|
|
51 | return 0; |
|
|
52 | } |
|
|
53 | if (status) |
|
|
54 | *status = 0; |
41 | return *ptr0 + (*ptr1 << 8); |
55 | return *ptr0 + (*ptr1 << 8); |
42 | } |
56 | } |
43 | |
57 | |
44 | psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off) |
58 | psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off, |
|
|
59 | int *status) |
45 | { |
60 | { |
46 | psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; |
61 | psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3; |
47 | ptr0 = psiconv_list_get(buf,off); |
62 | ptr0 = psiconv_list_get(buf,off); |
48 | ptr1 = psiconv_list_get(buf,off+1); |
63 | ptr1 = psiconv_list_get(buf,off+1); |
49 | ptr2 = psiconv_list_get(buf,off+2); |
64 | ptr2 = psiconv_list_get(buf,off+2); |
50 | ptr3 = psiconv_list_get(buf,off+3); |
65 | ptr3 = psiconv_list_get(buf,off+3); |
51 | if (!ptr0 || !ptr1 || !ptr2 || !ptr3) |
66 | if (!ptr0 || !ptr1 || !ptr2 || !ptr3) { |
52 | psiconv_fatal(lev,off,"Trying long read past the end of the file"); |
67 | psiconv_warn(lev,off,"Trying long read past the end of the file"); |
|
|
68 | if (status) |
|
|
69 | *status = -PSICONV_E_PARSE; |
|
|
70 | return 0; |
|
|
71 | } |
|
|
72 | if (status) |
|
|
73 | *status = 0; |
53 | return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); |
74 | return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24); |
54 | } |
75 | } |
55 | |
76 | |
56 | psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, |
77 | psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off, |
57 | int *length) |
78 | int *length,int *status) |
58 | { |
79 | { |
59 | psiconv_u8 temp; |
80 | psiconv_u8 temp; |
60 | psiconv_S_t res; |
81 | psiconv_S_t res; |
61 | int len; |
82 | int len,localstatus; |
62 | |
83 | |
63 | psiconv_progress(lev+1,off,"Going to read a S length indicator"); |
84 | psiconv_progress(lev+1,off,"Going to read a S length indicator"); |
64 | temp = psiconv_read_u8(buf,lev+2,off); |
85 | temp = psiconv_read_u8(buf,lev+2,off,&localstatus); |
|
|
86 | if (localstatus) |
|
|
87 | goto ERROR; |
65 | if ((temp & 0x03) == 0x02) { |
88 | if ((temp & 0x03) == 0x02) { |
66 | res = psiconv_read_u8(buf,lev+2,off) >> 2; |
89 | res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 2; |
|
|
90 | if (localstatus) |
|
|
91 | goto ERROR; |
67 | len = 1; |
92 | len = 1; |
68 | psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
93 | psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
69 | } else if ((temp & 0x07) == 0x03) { |
94 | } else if ((temp & 0x07) == 0x03) { |
70 | res = psiconv_read_u16(buf,lev+2,off) >> 3; |
95 | res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 3; |
|
|
96 | if (localstatus) |
|
|
97 | goto ERROR; |
71 | len = 2; |
98 | len = 2; |
72 | psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
99 | psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
73 | } else { |
100 | } else { |
74 | psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); |
101 | psiconv_warn(lev+2,off,"S indicator: unknown encoding!"); |
75 | psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); |
102 | psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); |
76 | res = 0; |
103 | goto ERROR; |
77 | len = 1; |
|
|
78 | } |
104 | } |
79 | |
105 | |
80 | if (length) |
106 | if (length) |
81 | *length = len; |
107 | *length = len; |
|
|
108 | if (status) |
|
|
109 | *status = 0; |
82 | |
110 | |
83 | psiconv_progress(lev+1,off+len-1, |
111 | psiconv_progress(lev+1,off+len-1, |
84 | "End of S length indicator (total length: %08x)", len); |
112 | "End of S length indicator (total length: %08x)", len); |
85 | |
113 | |
86 | return res; |
114 | return res; |
|
|
115 | |
|
|
116 | ERROR: |
|
|
117 | psiconv_warn(lev+1,off,"Reading of S indicator failed"); |
|
|
118 | if (status) |
|
|
119 | *status = localstatus; |
|
|
120 | if (length) |
|
|
121 | *length = 0; |
|
|
122 | return 0; |
87 | } |
123 | } |
88 | |
124 | |
89 | psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off, |
125 | psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off, |
90 | int *length) |
126 | int *length, int *status) |
91 | { |
127 | { |
92 | psiconv_u8 temp; |
128 | psiconv_u8 temp; |
93 | psiconv_X_t res; |
129 | psiconv_X_t res; |
94 | int len; |
130 | int len,localstatus; |
95 | |
131 | |
96 | psiconv_progress(lev+1,off,"Going to read a X length indicator"); |
132 | psiconv_progress(lev+1,off,"Going to read a X length indicator"); |
97 | temp = psiconv_read_u8(buf,lev+2,off); |
133 | temp = psiconv_read_u8(buf,lev+2,off,&localstatus); |
|
|
134 | if (localstatus) |
|
|
135 | goto ERROR; |
98 | if ((temp & 0x01) == 0x00) { |
136 | if ((temp & 0x01) == 0x00) { |
99 | res = psiconv_read_u8(buf,lev+2,off) >> 1; |
137 | res = psiconv_read_u8(buf,lev+2,off,&localstatus) >> 1; |
|
|
138 | if (localstatus) |
|
|
139 | goto ERROR; |
100 | len = 1; |
140 | len = 1; |
101 | psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
141 | psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res); |
102 | } else if ((temp & 0x03) == 0x01) { |
142 | } else if ((temp & 0x03) == 0x01) { |
103 | res = psiconv_read_u16(buf,lev+2,off) >> 2; |
143 | res = psiconv_read_u16(buf,lev+2,off,&localstatus) >> 2; |
|
|
144 | if (localstatus) |
|
|
145 | goto ERROR; |
104 | len = 2; |
146 | len = 2; |
105 | psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
147 | psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res); |
106 | } else if ((temp & 0x07) == 0x03) { |
148 | } else if ((temp & 0x07) == 0x03) { |
107 | res = psiconv_read_u32(buf,lev+2,off) >> 3; |
149 | res = psiconv_read_u32(buf,lev+2,off,&localstatus) >> 3; |
|
|
150 | if (localstatus) |
|
|
151 | goto ERROR; |
108 | len = 4; |
152 | len = 4; |
109 | psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); |
153 | psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res); |
110 | } else { |
154 | } else { |
111 | psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); |
155 | psiconv_warn(lev+2,off,"X indicator: unknown encoding!"); |
112 | psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); |
156 | psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp); |
113 | res = 0; |
157 | goto ERROR; |
114 | len = 1; |
|
|
115 | } |
158 | } |
116 | |
159 | |
117 | if (length) |
160 | if (length) |
118 | *length = len; |
161 | *length = len; |
|
|
162 | if (status) |
|
|
163 | *status = 0; |
119 | |
164 | |
120 | psiconv_progress(lev+1,off+len-1, |
165 | psiconv_progress(lev+1,off+len-1, |
121 | "End of X length indicator (total length: %08x)", len); |
166 | "End of X length indicator (total length: %08x)", len); |
122 | |
167 | |
123 | return res; |
168 | return res; |
|
|
169 | |
|
|
170 | ERROR: |
|
|
171 | psiconv_warn(lev+1,off,"Reading of X indicator failed"); |
|
|
172 | if (status) |
|
|
173 | *status = localstatus; |
|
|
174 | if (length) |
|
|
175 | *length = 0; |
|
|
176 | return 0; |
124 | } |
177 | } |
125 | |
178 | |
126 | psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev, |
179 | psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev, |
127 | psiconv_u32 off, int *length) |
180 | psiconv_u32 off, int *length, int *status) |
128 | { |
181 | { |
129 | psiconv_length_t res; |
182 | psiconv_length_t res; |
|
|
183 | int localstatus; |
|
|
184 | |
130 | res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off)); |
185 | res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off, |
|
|
186 | &localstatus)); |
|
|
187 | if (localstatus) { |
|
|
188 | psiconv_warn(lev+1,off,"Reading of length failed"); |
|
|
189 | if (length) |
|
|
190 | *length = 0; |
|
|
191 | if (status) |
|
|
192 | *status = localstatus; |
|
|
193 | return 0; |
|
|
194 | } |
131 | psiconv_debug(lev+1,off,"Length: %f",res); |
195 | psiconv_debug(lev+1,off,"Length: %f",res); |
|
|
196 | if (length) |
|
|
197 | *length = 4; |
|
|
198 | if (status) |
|
|
199 | *status = 0; |
|
|
200 | return res; |
|
|
201 | } |
|
|
202 | |
|
|
203 | psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev, |
|
|
204 | psiconv_u32 off, int *length, int *status) |
|
|
205 | { |
|
|
206 | psiconv_size_t res; |
|
|
207 | int localstatus; |
|
|
208 | res = ((psiconv_s32) psiconv_read_u32(buf,lev,off,&localstatus)) / 20.0; |
|
|
209 | if (localstatus) { |
|
|
210 | psiconv_warn(lev+1,off,"Reading of size failed"); |
|
|
211 | if (length) |
|
|
212 | *length = 0; |
|
|
213 | if (status) |
|
|
214 | *status = localstatus; |
|
|
215 | return 0; |
|
|
216 | } |
|
|
217 | psiconv_debug(lev+1,off,"Size: %f",res); |
|
|
218 | if (status) |
|
|
219 | *status = 0; |
132 | if (length) |
220 | if (length) |
133 | *length = 4; |
221 | *length = 4; |
134 | return res; |
222 | return res; |
135 | } |
223 | } |
136 | |
224 | |
137 | psiconv_size_t psiconv_read_size(const psiconv_buffer buf, int lev, |
|
|
138 | psiconv_u32 off, int *length) |
|
|
139 | { |
|
|
140 | psiconv_size_t res; |
|
|
141 | res = ((psiconv_s32) psiconv_read_u32(buf,lev,off)) / 20.0; |
|
|
142 | psiconv_debug(lev+1,off,"Size: %f",res); |
|
|
143 | if (length) |
|
|
144 | *length = 4; |
|
|
145 | return res; |
|
|
146 | } |
|
|
147 | |
|
|
148 | int psiconv_parse_bool(const psiconv_buffer buf, int lev, psiconv_u32 off, |
225 | int psiconv_parse_bool(const psiconv_buffer buf, int lev, psiconv_u32 off, |
149 | int *length, psiconv_bool_t *result) |
226 | int *length, psiconv_bool_t *result) |
150 | { |
227 | { |
151 | psiconv_u8 temp; |
228 | psiconv_u8 temp; |
|
|
229 | int localstatus; |
152 | temp = psiconv_read_u8(buf,lev,off); |
230 | temp = psiconv_read_u8(buf,lev,off,&localstatus); |
|
|
231 | if (localstatus) { |
|
|
232 | psiconv_warn(lev+1,off,"Reading of bool failed"); |
|
|
233 | if (length) |
|
|
234 | *length = 0; |
|
|
235 | return localstatus; |
|
|
236 | } |
153 | if (length) |
237 | if (length) |
154 | *length = 1; |
238 | *length = 1; |
155 | if (temp == 0) { |
239 | if (temp == 0) { |
156 | *result = psiconv_bool_false; |
240 | *result = psiconv_bool_false; |
157 | return 0; |
241 | return 0; |
… | |
… | |
160 | return 0; |
244 | return 0; |
161 | } |
245 | } |
162 | psiconv_warn(lev+1,off,"Unknown value for boolean"); |
246 | psiconv_warn(lev+1,off,"Unknown value for boolean"); |
163 | psiconv_debug(lev+1,off,"Boolean value: %02x",temp); |
247 | psiconv_debug(lev+1,off,"Boolean value: %02x",temp); |
164 | *result = psiconv_bool_true; |
248 | *result = psiconv_bool_true; |
165 | return -1; |
249 | return 0; |
166 | } |
250 | } |
167 | |
251 | |
168 | psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev, |
252 | psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev, |
169 | psiconv_u32 off,int *length) |
253 | psiconv_u32 off,int *length, int *status) |
170 | { |
254 | { |
171 | int stringlen,i,leng,len; |
255 | int stringlen,i,leng,len,localstatus; |
172 | psiconv_string_t result; |
256 | psiconv_string_t result; |
173 | char *res_copy; |
257 | char *res_copy; |
174 | |
258 | |
175 | psiconv_progress(lev+1,off,"Going to read a string"); |
259 | psiconv_progress(lev+1,off,"Going to read a string"); |
176 | |
260 | |
177 | stringlen = psiconv_read_S(buf,lev+2,off,&leng); |
261 | stringlen = psiconv_read_S(buf,lev+2,off,&leng,&localstatus); |
|
|
262 | if (localstatus) |
|
|
263 | goto ERROR1; |
178 | psiconv_debug(lev+2,off,"Length: %i",stringlen); |
264 | psiconv_debug(lev+2,off,"Length: %i",stringlen); |
179 | len = leng; |
265 | len = leng; |
180 | |
266 | |
181 | result = malloc(stringlen + 1); |
267 | result = malloc(stringlen + 1); |
|
|
268 | if (!result) |
|
|
269 | goto ERROR1; |
182 | for (i = 0; i < stringlen; i++) |
270 | for (i = 0; (i < stringlen) && !localstatus; i++) |
183 | result[i] = psiconv_read_u8(buf,lev,off+i+len); |
271 | result[i] = psiconv_read_u8(buf,lev,off+i+len,&localstatus); |
|
|
272 | if (localstatus) |
|
|
273 | goto ERROR2; |
184 | result[stringlen] = 0; |
274 | result[stringlen] = 0; |
185 | len += stringlen; |
275 | len += stringlen; |
186 | |
276 | |
187 | res_copy = psiconv_make_printable(result); |
277 | res_copy = psiconv_make_printable(result); |
|
|
278 | if (!res_copy) |
|
|
279 | goto ERROR2; |
188 | psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); |
280 | psiconv_debug(lev+2,off,"Contents: `%s'",res_copy); |
189 | free(res_copy); |
281 | free(res_copy); |
190 | |
282 | |
191 | if (length) |
283 | if (length) |
192 | *length = len; |
284 | *length = len; |
193 | |
285 | |
|
|
286 | if (status) |
|
|
287 | *status = 0; |
|
|
288 | |
194 | psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len); |
289 | psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len); |
195 | |
290 | |
196 | return result; |
291 | return result; |
|
|
292 | |
|
|
293 | ERROR2: |
|
|
294 | free(result); |
|
|
295 | ERROR1: |
|
|
296 | psiconv_warn(lev+1,off,"Reading of string failed"); |
|
|
297 | if (status) |
|
|
298 | *status = localstatus; |
|
|
299 | if (length) |
|
|
300 | *length = 0; |
|
|
301 | return NULL; |
197 | } |
302 | } |