/[public]/psiconv/trunk/lib/psiconv/parse_simple.c
ViewVC logotype

Annotation of /psiconv/trunk/lib/psiconv/parse_simple.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 71 - (hide annotations)
Fri Dec 22 22:31:50 2000 UTC (23 years, 3 months ago) by frodo
File MIME type: text/plain
File size: 8243 byte(s)
(Frodo) First generate routines! Reshuffled a few things to make it all work out

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

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26