--- psiconv/trunk/lib/psiconv/parse_common.c 2003/11/20 23:45:47 161 +++ psiconv/trunk/lib/psiconv/parse_common.c 2003/11/21 15:39:14 162 @@ -36,6 +36,9 @@ psiconv_text_and_layout result, psiconv_word_styles_section styles, int with_styles); +static psiconv_file_type_t psiconv_determine_embedded_object_type + (const psiconv_buffer buf,int lev, + int *status); int psiconv_parse_header_section(const psiconv_buffer buf,int lev, psiconv_u32 off, int *length, @@ -941,6 +944,7 @@ psiconv_section_table_section table; psiconv_section_table_entry entry; psiconv_u32 icon_sec=0,display_sec=0,table_sec=0; + psiconv_buffer subbuf; psiconv_progress(lev+1,off+len,"Going to read an Embedded Object"); if (!(*result = malloc(sizeof(**result)))) @@ -995,7 +999,57 @@ psiconv_debug(lev+2,off+len,"Object Icon Section at offset %08x",icon_sec); if ((res = psiconv_parse_object_icon_section(buf,lev+2,icon_sec,NULL, &(*result)->icon))) - goto ERROR2; + goto ERROR3; + } + + psiconv_progress(lev+2,off+len, + "Looking for the Section Table Offset Section"); + if (!table_sec) { + psiconv_warn(lev+2,off+len, + "Embedded Section Table Offset Section not found"); + (*result)->object = NULL; + } else { + psiconv_progress(lev+2,off+len, + "Extracting object: add %08x to all following offsets", + table_sec); + /* We can't determine the length of the object, so we just take it all */ + if ((res = psiconv_buffer_subbuffer(&subbuf,buf,table_sec, + psiconv_buffer_length(buf)-table_sec))) + goto ERROR4; + + if (!((*result)->object = malloc(sizeof(*(*result)->object)))) + goto ERROR5; + + /* We need to find the file type, but we don't have a normal header */ + /* So we try to find the Application ID Section and hope for the best */ + psiconv_progress(lev+3,0,"Trying to determine the file type"); + (*result)->object->type = psiconv_determine_embedded_object_type + (subbuf,lev+3,&res); + switch ((*result)->object->type) { + case psiconv_word_file: + if ((res = psiconv_parse_word_file(subbuf,lev+3,0, + ((psiconv_word_f *) &(*result)->object->file)))) + goto ERROR6; + break; + case psiconv_texted_file: + if ((res = psiconv_parse_texted_file(subbuf,lev+3,0, + ((psiconv_texted_f *) &(*result)->object->file)))) + goto ERROR6; + break; + case psiconv_sheet_file: + if ((res = psiconv_parse_sheet_file(subbuf,lev+3,0, + ((psiconv_sheet_f *) &(*result)->object->file)))) + goto ERROR6; + break; + case psiconv_sketch_file: + if ((res = psiconv_parse_sketch_file(subbuf,lev+3,0, + ((psiconv_sketch_f *) &(*result)->object->file)))) + goto ERROR6; + break; + default: + psiconv_warn(lev+3,0,"Can't parse embedded object (still continuing)"); + (*result)->object->file = NULL; + } } if (length) @@ -1006,7 +1060,15 @@ return res; - + +ERROR6: + free((*result)->object); +ERROR5: + psiconv_buffer_free(subbuf); +ERROR4: + psiconv_free_object_icon_section((*result)->icon); +ERROR3: + psiconv_free_object_display_section((*result)->display); ERROR2: psiconv_free_section_table_section(table); ERROR1: @@ -1021,6 +1083,79 @@ return res; } +psiconv_file_type_t psiconv_determine_embedded_object_type + (const psiconv_buffer buf,int lev, + int *status) +{ + psiconv_u32 off; + psiconv_section_table_section table; + int res,i; + psiconv_file_type_t file_type = psiconv_unknown_file; + psiconv_section_table_entry entry; + psiconv_application_id_section applid; + + psiconv_progress(lev+1,0,"Going to determine embedded object file type"); + psiconv_progress(lev+2,0,"Going to read the Section Table Offset Section"); + off = psiconv_read_u32(buf,lev,0,&res); + if (res) + goto ERROR1; + psiconv_debug(lev+2,0,"Offset: %08x",off); + + psiconv_progress(lev+2,off,"Going to read the Section Table Section"); + if ((res = psiconv_parse_section_table_section(buf,lev+2,off,NULL,&table))) + goto ERROR1; + + psiconv_progress(lev+2,off,"Going to search the Section Table Section " + "for the Application ID Section"); + for (i=0; i < psiconv_list_length(table); i++) { + psiconv_progress(lev+3,off,"Going to read entry %d",i); + if (!(entry = psiconv_list_get(table,i))) + goto ERROR2; + if (entry->id == PSICONV_ID_APPL_ID_SECTION) { + psiconv_progress(lev+3,off, + "Found the Application ID Section at offset %08x", + entry->offset); + off = entry->offset; + break; + } + } + if (i == psiconv_list_length(table)) { + psiconv_warn(lev+2,off,"No Application ID Section found"); + res = PSICONV_E_PARSE; + goto ERROR2; + } + + psiconv_progress(lev+2,off,"Going to read the Application ID Section"); + if ((res = psiconv_parse_application_id_section(buf,lev+2,off,NULL,&applid))) + goto ERROR2; + + switch (applid->id) { + case PSICONV_ID_WORD: file_type = psiconv_word_file; + psiconv_debug(lev+2,off,"Found a Word file"); + break; + case PSICONV_ID_TEXTED: file_type = psiconv_texted_file; + psiconv_debug(lev+2,off,"Found a TextEd file"); + break; + case PSICONV_ID_SKETCH: file_type = psiconv_sketch_file; + psiconv_debug(lev+2,off,"Found a Sketch file"); + break; + case PSICONV_ID_SHEET: file_type = psiconv_sheet_file; + psiconv_debug(lev+2,off,"Found a Sheet file"); + break; + default: psiconv_warn(lev+2,off,"Found an unknown file type"); + psiconv_debug(lev+2,off,"Found ID %08x",applid->id); + } + +ERROR2: + psiconv_free_application_id_section(applid); +ERROR1: + psiconv_free_section_table_section(table); + if (status) + *status = res; + return file_type; + +} + int psiconv_parse_object_display_section(const psiconv_buffer buf,int lev, psiconv_u32 off, int *length, psiconv_object_display_section *result) @@ -1041,10 +1176,10 @@ goto ERROR2; if (temp == 0x00) { (*result)->show_icon = psiconv_bool_true; - psiconv_debug(lev+2,off+len,"Displayed as full document"); + psiconv_debug(lev+2,off+len,"Displayed as icon"); } else if (temp == 0x01) { (*result)->show_icon = psiconv_bool_false; - psiconv_debug(lev+2,off+len,"Displayed as icon"); + psiconv_debug(lev+2,off+len,"Displayed as full document"); } else { psiconv_warn(lev+2,off+len,"Unknown Object Display Section Icon Flag"); psiconv_debug(lev+2,off+len,"Icon flag found: %02x",temp); @@ -1053,18 +1188,18 @@ } len ++; - psiconv_progress(lev+2,off+len,"Going to read the icon width"); + psiconv_progress(lev+2,off+len,"Going to read the display width"); (*result)->width = psiconv_read_length(buf,lev+2,off+len,&leng,&res); if (res) goto ERROR2; - psiconv_debug(lev+2,off+len,"Icon width: %f cm",(*result)->width); + psiconv_debug(lev+2,off+len,"Display width: %f cm",(*result)->width); len += leng; - psiconv_progress(lev+2,off+len,"Going to read the icon height"); + psiconv_progress(lev+2,off+len,"Going to read the display height"); (*result)->height = psiconv_read_length(buf,lev+2,off+len,&leng,&res); if (res) goto ERROR2; - psiconv_debug(lev+2,off+len,"Icon length: %f cm",(*result)->height); + psiconv_debug(lev+2,off+len,"Display length: %f cm",(*result)->height); len += leng; psiconv_progress(lev+2,off+len,"Going to read unknown long (%08x expected)", @@ -1079,7 +1214,7 @@ if (length) *length = len; - psiconv_progress(lev+1,off+len-1,"End of Object Display Section" + psiconv_progress(lev+1,off+len-1,"End of Object Display Section " "(total length: %08x",len); return res;