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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.54  
changed lines
  Added in v.79

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