| 1 |
frodo |
2 |
/* |
| 2 |
|
|
parse_driver.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.h" |
| 24 |
|
|
#include "parse_routines.h" |
| 25 |
|
|
#include "data.h" |
| 26 |
|
|
|
| 27 |
|
|
psiconv_file_type_t psiconv_file_type(psiconv_buffer buf) |
| 28 |
|
|
{ |
| 29 |
|
|
psiconv_header_section header; |
| 30 |
|
|
psiconv_file_type_t res; |
| 31 |
|
|
|
| 32 |
|
|
psiconv_parse_header_section(buf,0,0,NULL,&header); |
| 33 |
|
|
res = header->file; |
| 34 |
|
|
psiconv_free_header_section(header); |
| 35 |
|
|
return res; |
| 36 |
|
|
} |
| 37 |
|
|
|
| 38 |
|
|
int psiconv_parse(const psiconv_buffer buf,psiconv_file *result) |
| 39 |
|
|
{ |
| 40 |
|
|
int res=0; |
| 41 |
|
|
int lev=0; |
| 42 |
|
|
int off=0; |
| 43 |
|
|
|
| 44 |
|
|
(*result) = malloc(sizeof(**result)); |
| 45 |
|
|
|
| 46 |
|
|
(*result)->type = psiconv_file_type(buf); |
| 47 |
|
|
if ((*result)->type == psiconv_unknown_file) { |
| 48 |
|
|
psiconv_warn(lev+1,off,"Unknown file type: can't parse!"); |
| 49 |
|
|
(*result)->file = NULL; |
| 50 |
|
|
res = -1; |
| 51 |
|
|
} else if ((*result)->type == psiconv_word_file) |
| 52 |
|
|
res = psiconv_parse_word_file(buf,lev+2,off, |
| 53 |
|
|
(psiconv_word_f *)(&((*result)->file))); |
| 54 |
|
|
else if ((*result)->type == psiconv_texted_file) |
| 55 |
|
|
res = psiconv_parse_texted_file(buf,lev+2,off, |
| 56 |
|
|
(psiconv_texted_f *)(&((*result)->file))); |
| 57 |
|
|
else { |
| 58 |
|
|
psiconv_warn(lev+1,off,"Can't parse this file yet!"); |
| 59 |
|
|
(*result)->file = NULL; |
| 60 |
|
|
} |
| 61 |
|
|
res = -1; |
| 62 |
|
|
|
| 63 |
|
|
return res; |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
|
|
int psiconv_parse_texted_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
| 67 |
|
|
psiconv_texted_f *result) |
| 68 |
|
|
{ |
| 69 |
|
|
int res=0; |
| 70 |
|
|
psiconv_header_section header; |
| 71 |
|
|
psiconv_section_table_section table; |
| 72 |
|
|
psiconv_application_id_section appl_id; |
| 73 |
|
|
char *temp_str; |
| 74 |
|
|
psiconv_character_layout base_char; |
| 75 |
|
|
psiconv_paragraph_layout base_para; |
| 76 |
|
|
psiconv_u32 page_sec = 0; |
| 77 |
|
|
psiconv_u32 texted_sec = 0; |
| 78 |
|
|
psiconv_u32 applid_sec = 0; |
| 79 |
|
|
psiconv_section_table_entry entry; |
| 80 |
|
|
int i; |
| 81 |
|
|
|
| 82 |
|
|
psiconv_progress(lev+1,off,"Going to read a texted file"); |
| 83 |
|
|
*result = malloc(sizeof(**result)); |
| 84 |
|
|
|
| 85 |
|
|
psiconv_progress(lev+2,off, "Going to read the header section"); |
| 86 |
|
|
res |= psiconv_parse_header_section(buf,lev+2,off,NULL,&header); |
| 87 |
|
|
|
| 88 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 89 |
|
|
"Going to read the section table section"); |
| 90 |
|
|
res |= psiconv_parse_section_table_section(buf,lev+2, |
| 91 |
|
|
header->section_table_offset, |
| 92 |
|
|
NULL,&table); |
| 93 |
|
|
|
| 94 |
|
|
for (i = 0; i < psiconv_list_length(table); i ++) { |
| 95 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 96 |
|
|
"Going to read entry %d",i); |
| 97 |
|
|
entry = psiconv_list_get(table,i); |
| 98 |
|
|
if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
| 99 |
|
|
applid_sec = entry->offset; |
| 100 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 101 |
|
|
"Found the Application ID section at %08x",applid_sec); |
| 102 |
|
|
} else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) { |
| 103 |
|
|
page_sec = entry->offset; |
| 104 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 105 |
|
|
"Found the Page Layout section at %08x",page_sec); |
| 106 |
|
|
} else if (entry->id == PSICONV_ID_TEXTED) { |
| 107 |
|
|
texted_sec = entry->offset; |
| 108 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 109 |
|
|
"Found the TextEd section at %08x",texted_sec); |
| 110 |
|
|
} else { |
| 111 |
|
|
psiconv_warn(lev+3,header->section_table_offset, |
| 112 |
|
|
"Found unknown section in the Section Table"); |
| 113 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 114 |
|
|
"Section ID %08x, offset %08x",entry->id,entry->offset); |
| 115 |
|
|
res = -1; |
| 116 |
|
|
} |
| 117 |
|
|
} |
| 118 |
|
|
|
| 119 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 120 |
|
|
"Looking for the Application ID section"); |
| 121 |
|
|
if (! applid_sec) { |
| 122 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 123 |
|
|
"Application ID section not found in the section table"); |
| 124 |
|
|
res = -1; |
| 125 |
|
|
} else { |
| 126 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 127 |
|
|
"Application ID section at offset %08x",applid_sec); |
| 128 |
|
|
res |= psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
| 129 |
|
|
&appl_id); |
| 130 |
|
|
} |
| 131 |
|
|
if ((appl_id->id != PSICONV_ID_TEXTED) || |
| 132 |
|
|
strcmp(appl_id->name,"TextEd.app")) { |
| 133 |
|
|
psiconv_warn(lev+2,applid_sec, |
| 134 |
|
|
"Application ID section contains unexpected data"); |
| 135 |
|
|
psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
| 136 |
|
|
PSICONV_ID_TEXTED,appl_id->id); |
| 137 |
|
|
temp_str = psiconv_make_printable(appl_id->name); |
| 138 |
|
|
psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
| 139 |
|
|
PSICONV_ID_TEXTED,temp_str); |
| 140 |
|
|
free(temp_str); |
| 141 |
|
|
} |
| 142 |
|
|
|
| 143 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 144 |
|
|
"Looking for the Page layout section"); |
| 145 |
|
|
if (! page_sec) { |
| 146 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 147 |
|
|
"Page layout section not found in the section table"); |
| 148 |
|
|
(*result)->page_sec = NULL; |
| 149 |
|
|
res = -1; |
| 150 |
|
|
} else { |
| 151 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 152 |
|
|
"Page layout section at offset %08x",page_sec); |
| 153 |
|
|
res |= psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL, |
| 154 |
|
|
&(*result)->page_sec); |
| 155 |
|
|
} |
| 156 |
|
|
|
| 157 |
|
|
base_char = psiconv_basic_character_layout(); |
| 158 |
|
|
base_para = psiconv_basic_paragraph_layout(); |
| 159 |
|
|
|
| 160 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 161 |
|
|
"Looking for the TextEd section"); |
| 162 |
|
|
if (! texted_sec) { |
| 163 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 164 |
|
|
"TextEd section not found in the section table"); |
| 165 |
|
|
(*result)->texted_sec = NULL; |
| 166 |
|
|
res = -1; |
| 167 |
|
|
} else { |
| 168 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 169 |
|
|
"TextEd section at offset %08x",texted_sec); |
| 170 |
|
|
res |= psiconv_parse_texted_section(buf,lev+2,texted_sec,NULL, |
| 171 |
|
|
&(*result)->texted_sec, |
| 172 |
|
|
base_char,base_para); |
| 173 |
|
|
} |
| 174 |
|
|
psiconv_free_character_layout(base_char); |
| 175 |
|
|
psiconv_free_paragraph_layout(base_para); |
| 176 |
|
|
|
| 177 |
|
|
psiconv_free_application_id_section(appl_id); |
| 178 |
|
|
psiconv_free_header_section(header); |
| 179 |
|
|
psiconv_free_section_table_section(table); |
| 180 |
|
|
|
| 181 |
|
|
psiconv_progress(lev+1,off,"End of word file"); |
| 182 |
|
|
return res; |
| 183 |
|
|
} |
| 184 |
|
|
|
| 185 |
|
|
int psiconv_parse_word_file(const psiconv_buffer buf,int lev, psiconv_u32 off, |
| 186 |
|
|
psiconv_word_f *result) |
| 187 |
|
|
{ |
| 188 |
|
|
int res=0; |
| 189 |
|
|
psiconv_header_section header; |
| 190 |
|
|
psiconv_section_table_section table; |
| 191 |
|
|
psiconv_application_id_section appl_id; |
| 192 |
|
|
char *temp_str; |
| 193 |
|
|
psiconv_u32 pwd_sec = 0; |
| 194 |
|
|
psiconv_u32 status_sec = 0; |
| 195 |
|
|
psiconv_u32 styles_sec = 0; |
| 196 |
|
|
psiconv_u32 page_sec = 0; |
| 197 |
|
|
psiconv_u32 text_sec = 0; |
| 198 |
|
|
psiconv_u32 layout_sec = 0; |
| 199 |
|
|
psiconv_u32 applid_sec = 0; |
| 200 |
|
|
psiconv_section_table_entry entry; |
| 201 |
|
|
int i; |
| 202 |
|
|
|
| 203 |
|
|
psiconv_progress(lev+1,off,"Going to read a word file"); |
| 204 |
|
|
*result = malloc(sizeof(**result)); |
| 205 |
|
|
|
| 206 |
|
|
psiconv_progress(lev+2,off, "Going to read the header section"); |
| 207 |
|
|
res |= psiconv_parse_header_section(buf,lev+2,off,NULL,&header); |
| 208 |
|
|
|
| 209 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 210 |
|
|
"Going to read the section table section"); |
| 211 |
|
|
res |= psiconv_parse_section_table_section(buf,lev+2, |
| 212 |
|
|
header->section_table_offset, |
| 213 |
|
|
NULL,&table); |
| 214 |
|
|
|
| 215 |
|
|
for (i = 0; i < psiconv_list_length(table); i ++) { |
| 216 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 217 |
|
|
"Going to read entry %d",i); |
| 218 |
|
|
entry = psiconv_list_get(table,i); |
| 219 |
|
|
if (entry->id == PSICONV_ID_APPL_ID_SECTION) { |
| 220 |
|
|
applid_sec = entry->offset; |
| 221 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 222 |
|
|
"Found the Application ID section at %08x",applid_sec); |
| 223 |
|
|
} else if (entry->id == PSICONV_ID_PAGE_LAYOUT_SECTION) { |
| 224 |
|
|
page_sec = entry->offset; |
| 225 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 226 |
|
|
"Found the Page Layout section at %08x",page_sec); |
| 227 |
|
|
} else if (entry->id == PSICONV_ID_TEXT_SECTION) { |
| 228 |
|
|
text_sec = entry->offset; |
| 229 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 230 |
|
|
"Found the Text section at %08x",text_sec); |
| 231 |
|
|
} else if (entry->id == PSICONV_ID_PASSWORD_SECTION) { |
| 232 |
|
|
pwd_sec = entry->offset; |
| 233 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 234 |
|
|
"Found the Password section at %08x",pwd_sec); |
| 235 |
|
|
psiconv_warn(lev+3,header->section_table_offset, |
| 236 |
|
|
"Password section found - can't read encrypted data"); |
| 237 |
|
|
res = -1; |
| 238 |
|
|
} else if (entry->id == PSICONV_ID_WORD_STATUS_SECTION) { |
| 239 |
|
|
status_sec = entry->offset; |
| 240 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 241 |
|
|
"Found the Word Status section at %08x",status_sec); |
| 242 |
|
|
} else if (entry->id == PSICONV_ID_WORD_STYLES_SECTION) { |
| 243 |
|
|
styles_sec = entry->offset; |
| 244 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 245 |
|
|
"Found the Word Styles section at %08x",styles_sec); |
| 246 |
|
|
} else if (entry->id == PSICONV_ID_LAYOUT_SECTION) { |
| 247 |
|
|
layout_sec = entry->offset; |
| 248 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 249 |
|
|
"Found the Layout section at %08x",layout_sec); |
| 250 |
|
|
} else { |
| 251 |
|
|
psiconv_warn(lev+3,header->section_table_offset, |
| 252 |
|
|
"Found unknown section in the Section Table"); |
| 253 |
|
|
psiconv_debug(lev+3,header->section_table_offset, |
| 254 |
|
|
"Section ID %08x, offset %08x",entry->id,entry->offset); |
| 255 |
|
|
res = -1; |
| 256 |
|
|
} |
| 257 |
|
|
} |
| 258 |
|
|
|
| 259 |
|
|
|
| 260 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 261 |
|
|
"Looking for the Status section"); |
| 262 |
|
|
if (!status_sec) { |
| 263 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 264 |
|
|
"Status section not found in the section table"); |
| 265 |
|
|
res = -1; |
| 266 |
|
|
} else { |
| 267 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 268 |
|
|
"Status section at offset %08x",status_sec); |
| 269 |
|
|
res |= psiconv_parse_word_status_section(buf,lev+2,status_sec,NULL, |
| 270 |
|
|
&((*result)->status_sec)); |
| 271 |
|
|
} |
| 272 |
|
|
|
| 273 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 274 |
|
|
"Looking for the Application ID section"); |
| 275 |
|
|
if (! applid_sec) { |
| 276 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 277 |
|
|
"Application ID section not found in the section table"); |
| 278 |
|
|
res = -1; |
| 279 |
|
|
} else { |
| 280 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 281 |
|
|
"Application ID section at offset %08x",applid_sec); |
| 282 |
|
|
res |= psiconv_parse_application_id_section(buf,lev+2,applid_sec,NULL, |
| 283 |
|
|
&appl_id); |
| 284 |
|
|
} |
| 285 |
|
|
if ((appl_id->id != PSICONV_ID_WORD) || |
| 286 |
|
|
strcmp(appl_id->name,"Word.app")) { |
| 287 |
|
|
psiconv_warn(lev+2,applid_sec, |
| 288 |
|
|
"Application ID section contains unexpected data"); |
| 289 |
|
|
psiconv_debug(lev+2,applid_sec,"ID: %08x expected, %08x found", |
| 290 |
|
|
PSICONV_ID_WORD,appl_id->id); |
| 291 |
|
|
temp_str = psiconv_make_printable(appl_id->name); |
| 292 |
|
|
psiconv_debug(lev+2,applid_sec,"Name: `%s' expected, `%s' found", |
| 293 |
|
|
PSICONV_ID_WORD,temp_str); |
| 294 |
|
|
free(temp_str); |
| 295 |
|
|
} |
| 296 |
|
|
|
| 297 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 298 |
|
|
"Looking for the Page layout section"); |
| 299 |
|
|
if (! page_sec) { |
| 300 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 301 |
|
|
"Page layout section not found in the section table"); |
| 302 |
|
|
(*result)->page_sec = NULL; |
| 303 |
|
|
res = -1; |
| 304 |
|
|
} else { |
| 305 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 306 |
|
|
"Page layout section at offset %08x",page_sec); |
| 307 |
|
|
res |= psiconv_parse_page_layout_section(buf,lev+2,page_sec,NULL, |
| 308 |
|
|
&(*result)->page_sec); |
| 309 |
|
|
} |
| 310 |
|
|
|
| 311 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 312 |
|
|
"Looking for the Word Style section"); |
| 313 |
|
|
if (!styles_sec) { |
| 314 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 315 |
|
|
"Word styles section not found in the section table"); |
| 316 |
|
|
(*result)->styles_sec = NULL; |
| 317 |
|
|
res = -1; |
| 318 |
|
|
} else { |
| 319 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 320 |
|
|
"Word styles section at offset %08x",styles_sec); |
| 321 |
|
|
res |= psiconv_parse_word_styles_section(buf,lev+2,styles_sec,NULL, |
| 322 |
|
|
&(*result)->styles_sec); |
| 323 |
|
|
} |
| 324 |
|
|
|
| 325 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 326 |
|
|
"Looking for the Text section"); |
| 327 |
|
|
if (!text_sec) { |
| 328 |
|
|
psiconv_warn(lev+2,header->section_table_offset, |
| 329 |
|
|
"Text section not found in the section table"); |
| 330 |
|
|
(*result)->paragraphs = NULL; |
| 331 |
|
|
res = -1; |
| 332 |
|
|
} else { |
| 333 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 334 |
|
|
"Text section at offset %08x",text_sec); |
| 335 |
|
|
res |= psiconv_parse_text_section(buf,lev+2,text_sec,NULL, |
| 336 |
|
|
&(*result)->paragraphs); |
| 337 |
|
|
} |
| 338 |
|
|
|
| 339 |
|
|
if (((*result)->paragraphs) && ((*result)->styles_sec)) { |
| 340 |
|
|
psiconv_progress(lev+2,header->section_table_offset, |
| 341 |
|
|
"Looking for the Layout section"); |
| 342 |
|
|
if (!layout_sec) { |
| 343 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 344 |
|
|
"Layout section not found in the section table"); |
| 345 |
|
|
res = -1; |
| 346 |
|
|
} else { |
| 347 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 348 |
|
|
"Layout section at offset %08x",layout_sec); |
| 349 |
|
|
res |= psiconv_parse_styled_layout_section(buf,lev+2,layout_sec,NULL, |
| 350 |
|
|
(*result)->paragraphs, |
| 351 |
|
|
(*result)->styles_sec); |
| 352 |
|
|
} |
| 353 |
|
|
} else |
| 354 |
|
|
psiconv_debug(lev+2,header->section_table_offset, |
| 355 |
|
|
"Skipping search for Layout section, as either the " |
| 356 |
|
|
"text or the word styles section was not found"); |
| 357 |
|
|
|
| 358 |
|
|
psiconv_free_application_id_section(appl_id); |
| 359 |
|
|
psiconv_free_header_section(header); |
| 360 |
|
|
psiconv_free_section_table_section(table); |
| 361 |
|
|
|
| 362 |
|
|
psiconv_progress(lev+1,off,"End of word file"); |
| 363 |
|
|
return res; |
| 364 |
|
|
} |