1 |
frodo |
2 |
/* |
2 |
|
|
parse_texted.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 |
|
|
#include <stdlib.h> |
22 |
|
|
|
23 |
|
|
#include "data.h" |
24 |
|
|
#include "parse_routines.h" |
25 |
|
|
|
26 |
|
|
int psiconv_parse_texted_section(const psiconv_buffer buf,int lev, |
27 |
|
|
psiconv_u32 off, int *length, |
28 |
|
|
psiconv_texted_section *result, |
29 |
|
|
psiconv_character_layout base_char, |
30 |
|
|
psiconv_paragraph_layout base_para) |
31 |
|
|
{ |
32 |
|
|
int res = 0; |
33 |
|
|
int len = 0; |
34 |
|
|
psiconv_u32 layout_sec = 0; |
35 |
|
|
psiconv_u32 unknown_sec = 0; |
36 |
|
|
psiconv_u32 replacement_sec = 0; |
37 |
|
|
psiconv_u32 temp; |
38 |
|
|
int leng; |
39 |
|
|
|
40 |
|
|
psiconv_progress(lev+1,off,"Going to read a texted section"); |
41 |
frodo |
64 |
if (!((*result) = malloc(sizeof(**result)))) |
42 |
|
|
goto ERROR1; |
43 |
frodo |
2 |
|
44 |
|
|
psiconv_progress(lev+2,off+len,"Going to read section id"); |
45 |
frodo |
64 |
temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
46 |
|
|
if (res) |
47 |
|
|
goto ERROR2; |
48 |
frodo |
2 |
if (temp != PSICONV_ID_TEXTED_BODY) { |
49 |
|
|
psiconv_warn(lev+2,off+len, |
50 |
|
|
"Page header section body id not found"); |
51 |
|
|
psiconv_debug(lev+2,off+len, |
52 |
|
|
"Page body id: read %08x, expected %08x",temp, |
53 |
|
|
PSICONV_ID_TEXTED); |
54 |
frodo |
64 |
res = -PSICONV_E_PARSE; |
55 |
|
|
goto ERROR2; |
56 |
frodo |
2 |
} |
57 |
|
|
len += 4; |
58 |
|
|
|
59 |
|
|
psiconv_progress(lev+2,off+len,"Going to read the section jumptable"); |
60 |
frodo |
64 |
while (temp = psiconv_read_u32(buf,lev+3,off+len,&res), |
61 |
|
|
!res && temp != PSICONV_ID_TEXTED_TEXT) { |
62 |
frodo |
2 |
len += 4; |
63 |
|
|
if (temp == PSICONV_ID_TEXTED_LAYOUT) { |
64 |
frodo |
64 |
layout_sec = psiconv_read_u32(buf,lev+3,off+len,&res); |
65 |
|
|
if (res) |
66 |
|
|
goto ERROR2; |
67 |
frodo |
2 |
psiconv_debug(lev+3,off+len,"Found Layout section at %08x",layout_sec); |
68 |
|
|
} else if (temp == PSICONV_ID_TEXTED_REPLACEMENT) { |
69 |
frodo |
64 |
replacement_sec = psiconv_read_u32(buf,lev+3,off+len,&res); |
70 |
|
|
if (res) |
71 |
|
|
goto ERROR2; |
72 |
frodo |
2 |
psiconv_debug(lev+3,off+len,"Found Replacement section at %08x", |
73 |
|
|
replacement_sec); |
74 |
|
|
} else if (temp == PSICONV_ID_TEXTED_UNKNOWN) { |
75 |
frodo |
64 |
unknown_sec= psiconv_read_u32(buf,lev+3,off+len,&res); |
76 |
|
|
if (res) |
77 |
|
|
goto ERROR2; |
78 |
frodo |
2 |
if (unknown_sec) { |
79 |
|
|
psiconv_warn(lev+3,off+len, |
80 |
frodo |
64 |
"Unknown section in TextEd jumptable has real offset (ignoring)"); |
81 |
frodo |
2 |
} |
82 |
|
|
psiconv_debug(lev+3,off+len,"Found Unknown section at %08x", |
83 |
|
|
unknown_sec); |
84 |
|
|
} else { |
85 |
frodo |
64 |
psiconv_warn(lev+3,off+len, |
86 |
|
|
"Unknown section in TextEd jumptable (ignoring)"); |
87 |
frodo |
2 |
psiconv_debug(lev+3,off+len,"Section ID %08x at offset %08x",temp, |
88 |
frodo |
64 |
psiconv_read_u32(buf,lev+3,off+len,NULL)); |
89 |
frodo |
2 |
} |
90 |
|
|
len += 4; |
91 |
|
|
} |
92 |
frodo |
64 |
if (res) |
93 |
|
|
goto ERROR2; |
94 |
frodo |
2 |
|
95 |
|
|
len += 4; |
96 |
|
|
psiconv_progress(lev+2,off+len,"Going to read the text"); |
97 |
frodo |
64 |
if ((res = psiconv_parse_text_section(buf,lev+2,off+len,&leng, |
98 |
|
|
&(*result)->paragraphs))) |
99 |
|
|
goto ERROR2; |
100 |
frodo |
2 |
len += leng; |
101 |
|
|
|
102 |
|
|
if (layout_sec) { |
103 |
|
|
psiconv_progress(lev+2,off+len,"Going to read the layout"); |
104 |
frodo |
64 |
if ((res = psiconv_parse_styleless_layout_section(buf,lev+2,layout_sec,NULL, |
105 |
frodo |
2 |
(*result)->paragraphs, |
106 |
frodo |
64 |
base_char,base_para))) |
107 |
|
|
goto ERROR3; |
108 |
frodo |
2 |
} |
109 |
|
|
|
110 |
|
|
#if 0 |
111 |
|
|
if (replacement_sec) { |
112 |
|
|
psiconv_progress(lev+2,off+len,"Going to read the replacements"); |
113 |
|
|
/* WHATEVER */ |
114 |
|
|
} |
115 |
|
|
#endif |
116 |
|
|
|
117 |
|
|
if (length) |
118 |
|
|
*length = len; |
119 |
|
|
|
120 |
|
|
psiconv_progress(lev+1,off+len-1,"End of TextEd section " |
121 |
|
|
"(total length: %08x", len); |
122 |
|
|
|
123 |
frodo |
64 |
return 0; |
124 |
|
|
|
125 |
|
|
ERROR3: |
126 |
|
|
psiconv_free_text_and_layout((*result)->paragraphs); |
127 |
|
|
ERROR2: |
128 |
|
|
free (*result); |
129 |
|
|
ERROR1: |
130 |
|
|
psiconv_warn(lev+1,off,"Reading of TextEd Section failed"); |
131 |
|
|
if (length) |
132 |
|
|
*length = 0; |
133 |
|
|
if (!res) |
134 |
|
|
return -PSICONV_E_NOMEM; |
135 |
|
|
else |
136 |
|
|
return res; |
137 |
frodo |
2 |
} |
138 |
|
|
|
139 |
|
|
|