/[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 54 - (hide annotations)
Thu Nov 30 20:09:13 2000 UTC (23 years, 4 months ago) by frodo
File MIME type: text/plain
File size: 5867 byte(s)
(Frodo) Important X-encoding-related bug that made psiconv fail on large
        documents

1 frodo 2 /*
2     parse_simple.c - Part of psiconv, a PSION 5 file formats converter
3     Copyright (c) 1999 Frodo Looijaard <frodol@dds.nl>
4    
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     #include <stdlib.h>
22    
23     #include "parse_routines.h"
24    
25     psiconv_u8 psiconv_read_u8(const psiconv_buffer buf,int lev,psiconv_u32 off)
26     {
27     psiconv_u8 *ptr;
28     ptr = psiconv_list_get(buf,off);
29     if (!ptr)
30     psiconv_fatal(lev,off,"Trying byte read past the end of the file");
31     return *ptr;
32     }
33    
34     psiconv_u16 psiconv_read_u16(const psiconv_buffer buf,int lev,psiconv_u32 off)
35     {
36     psiconv_u8 *ptr0,*ptr1;
37     ptr0 = psiconv_list_get(buf,off);
38     ptr1 = psiconv_list_get(buf,off+1);
39     if (!ptr0 || !ptr1)
40     psiconv_fatal(lev,off,"Trying word read past the end of the file");
41     return *ptr0 + (*ptr1 << 8);
42     }
43    
44     psiconv_u32 psiconv_read_u32(const psiconv_buffer buf,int lev,psiconv_u32 off)
45     {
46     psiconv_u8 *ptr0,*ptr1,*ptr2,*ptr3;
47     ptr0 = psiconv_list_get(buf,off);
48     ptr1 = psiconv_list_get(buf,off+1);
49     ptr2 = psiconv_list_get(buf,off+2);
50     ptr3 = psiconv_list_get(buf,off+3);
51     if (!ptr0 || !ptr1 || !ptr2 || !ptr3)
52     psiconv_fatal(lev,off,"Trying long read past the end of the file");
53     return *ptr0 + (*ptr1 << 8) + (*ptr2 << 16) + (*ptr3 << 24);
54     }
55    
56     psiconv_S_t psiconv_read_S(const psiconv_buffer buf, int lev, psiconv_u32 off,
57     int *length)
58     {
59     psiconv_u8 temp;
60     psiconv_S_t res;
61     int len;
62    
63     psiconv_progress(lev+1,off,"Going to read a S length indicator");
64     temp = psiconv_read_u8(buf,lev+2,off);
65     if ((temp & 0x03) == 0x02) {
66     res = psiconv_read_u8(buf,lev+2,off) >> 2;
67     len = 1;
68     psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res);
69     } else if ((temp & 0x07) == 0x03) {
70     res = psiconv_read_u16(buf,lev+2,off) >> 3;
71     len = 2;
72     psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res);
73     } else {
74     psiconv_warn(lev+2,off,"S indicator: unknown encoding!");
75     psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp);
76     res = 0;
77     len = 1;
78     }
79    
80     if (length)
81     *length = len;
82    
83     psiconv_progress(lev+1,off+len-1,
84     "End of S length indicator (total length: %08x)", len);
85    
86     return res;
87     }
88    
89     psiconv_X_t psiconv_read_X(const psiconv_buffer buf, int lev, psiconv_u32 off,
90     int *length)
91     {
92     psiconv_u8 temp;
93     psiconv_X_t res;
94     int len;
95    
96     psiconv_progress(lev+1,off,"Going to read a X length indicator");
97     temp = psiconv_read_u8(buf,lev+2,off);
98     if ((temp & 0x01) == 0x00) {
99     res = psiconv_read_u8(buf,lev+2,off) >> 1;
100     len = 1;
101     psiconv_debug(lev+2,off,"Indicator (1 byte): %02x",res);
102     } else if ((temp & 0x03) == 0x01) {
103     res = psiconv_read_u16(buf,lev+2,off) >> 2;
104     len = 2;
105     psiconv_debug(lev+2,off,"Indicator (2 bytes): %04x",res);
106     } else if ((temp & 0x07) == 0x03) {
107 frodo 54 res = psiconv_read_u32(buf,lev+2,off) >> 3;
108 frodo 2 len = 4;
109     psiconv_debug(lev+2,off,"Indicator (4 bytes): %08x",res);
110     } else {
111     psiconv_warn(lev+2,off,"X indicator: unknown encoding!");
112     psiconv_debug(lev+2,off,"Raw data first byte: %02x",temp);
113     res = 0;
114     len = 1;
115     }
116    
117     if (length)
118     *length = len;
119    
120     psiconv_progress(lev+1,off+len-1,
121     "End of X length indicator (total length: %08x)", len);
122    
123     return res;
124     }
125    
126     psiconv_length_t psiconv_read_length(const psiconv_buffer buf, int lev,
127     psiconv_u32 off, int *length)
128     {
129     psiconv_length_t res;
130     res = (2.54/1440.0) * ((psiconv_s32) psiconv_read_u32(buf,lev,off));
131     psiconv_debug(lev+1,off,"Length: %f",res);
132     if (length)
133     *length = 4;
134     return res;
135     }
136    
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,
149     int *length, psiconv_bool_t *result)
150     {
151     psiconv_u8 temp;
152     temp = psiconv_read_u8(buf,lev,off);
153     if (length)
154     *length = 1;
155     if (temp == 0) {
156     *result = psiconv_bool_false;
157     return 0;
158     } else if (temp == 1) {
159     *result = psiconv_bool_true;
160     return 0;
161     }
162     psiconv_warn(lev+1,off,"Unknown value for boolean");
163     psiconv_debug(lev+1,off,"Boolean value: %02x",temp);
164     *result = psiconv_bool_true;
165     return -1;
166     }
167    
168     psiconv_string_t psiconv_read_string(const psiconv_buffer buf,int lev,
169     psiconv_u32 off,int *length)
170     {
171     int stringlen,i,leng,len;
172     psiconv_string_t result;
173     char *res_copy;
174    
175     psiconv_progress(lev+1,off,"Going to read a string");
176    
177     stringlen = psiconv_read_S(buf,lev+2,off,&leng);
178     psiconv_debug(lev+2,off,"Length: %i",stringlen);
179     len = leng;
180    
181     result = malloc(stringlen + 1);
182     for (i = 0; i < stringlen; i++)
183     result[i] = psiconv_read_u8(buf,lev,off+i+len);
184     result[stringlen] = 0;
185     len += stringlen;
186    
187     res_copy = psiconv_make_printable(result);
188     psiconv_debug(lev+2,off,"Contents: `%s'",res_copy);
189     free(res_copy);
190    
191     if (length)
192     *length = len;
193    
194     psiconv_progress(lev+1,off+len-1,"End of string (total length: %08x)",len);
195    
196     return result;
197     }

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