| 1 |
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ |
| 2 |
|
| 3 |
/* Abiword |
| 4 |
* Copyright (C) 2001 Christian Biesinger <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef IE_IMP_STAROFFICE_H |
| 23 |
#define IE_IMP_STAROFFICE_H |
| 24 |
|
| 25 |
#include <stdio.h> |
| 26 |
#include <map> |
| 27 |
#include <string> |
| 28 |
|
| 29 |
#include "ut_string_class.h" |
| 30 |
#include "ut_types.h" |
| 31 |
#include "ut_iconv.h" |
| 32 |
#include "ie_imp.h" |
| 33 |
|
| 34 |
#include "sdw_cryptor.h" |
| 35 |
|
| 36 |
class PD_Document; |
| 37 |
|
| 38 |
// The following struct definition was taken from the OpenOffice file |
| 39 |
// sot/inc/stg.hxx line 85ff, with changes because of the different |
| 40 |
// cross-platform toolkits. That file is available under the LGPL. |
| 41 |
struct ClsId { |
| 42 |
UT_sint32 n1; |
| 43 |
UT_sint16 n2, n3; |
| 44 |
UT_uint8 n4, n5, n6, n7, n8, n9, n10, n11; |
| 45 |
}; |
| 46 |
|
| 47 |
/*! Reads a string from the file where the first uint16 contains the length. If it |
| 48 |
* is zero-terminated, length must include the byte for termination. |
| 49 |
* str must be delete[]'d, not free'd! |
| 50 |
* aLength, if non-null, contains the length of the string in bytes. */ |
| 51 |
void readByteString(GsfInput* stream, char*& str, UT_uint16* aLength = NULL) |
| 52 |
throw(UT_Error); |
| 53 |
|
| 54 |
/*! Reads a bytestring from a stream and converts it to UCS-4. Optionally, |
| 55 |
* it can also decrypt it. |
| 56 |
* @param stream The stream to read from |
| 57 |
* @param str The string where the bytestring should be stored |
| 58 |
* @param converter Iconv handle for charset conversion |
| 59 |
* @param cryptor (Optional) The cryptor used for decrypting the string */ |
| 60 |
void readByteString(GsfInput* stream, UT_UCS4Char*& str, UT_iconv_t converter, SDWCryptor* cryptor = NULL) throw(UT_Error); |
| 61 |
|
| 62 |
class DocHdr { |
| 63 |
public: |
| 64 |
DocHdr() : sBlockName(NULL), converter(reinterpret_cast<UT_iconv_t>(-1)) {} |
| 65 |
~DocHdr() { if (sBlockName) free(sBlockName); if (UT_iconv_isValid(converter)) UT_iconv_close(converter); } |
| 66 |
/*! Reads the document header |
| 67 |
* \param stream The OLE Stream to load from - should be the one |
| 68 |
* with the name "StarWriterDocument" |
| 69 |
* \throw UT_Error on failure */ |
| 70 |
void load(GsfInput* stream) throw(UT_Error); |
| 71 |
|
| 72 |
UT_uint8 cLen; // ??? |
| 73 |
UT_uint16 nVersion; |
| 74 |
UT_uint16 nFileFlags; |
| 75 |
UT_sint32 nDocFlags; |
| 76 |
UT_uint32 nRecSzPos; |
| 77 |
UT_sint32 nDummy; |
| 78 |
UT_uint16 nDummy16; // actually 2x dummy8 |
| 79 |
UT_uint8 cRedlineMode; // should actually be an enum, see sw/inc/redlenum.hxx#L83 |
| 80 |
UT_uint8 nCompatVer; |
| 81 |
|
| 82 |
UT_uint8 cPasswd[16]; // password verification data |
| 83 |
|
| 84 |
UT_uint8 cSet; // the encoding to use |
| 85 |
UT_uint8 cGui; |
| 86 |
|
| 87 |
UT_uint32 nDate; |
| 88 |
UT_uint32 nTime; |
| 89 |
|
| 90 |
UT_UCS4Char* sBlockName; // name of a text module |
| 91 |
|
| 92 |
UT_iconv_t converter; // Iconv handle for converting from the doc charset to UCS_2_INTERNAL |
| 93 |
|
| 94 |
SDWCryptor* cryptor; // used for decrypting the document or null if not encrypted |
| 95 |
}; |
| 96 |
|
| 97 |
// A text attribute record |
| 98 |
struct TextAttr { |
| 99 |
TextAttr() : data(NULL), isOff(false), isPara(false) {} |
| 100 |
~TextAttr() { if (data) delete[] data; } |
| 101 |
bool startSet, endSet; // true if the start/end attribute is valid |
| 102 |
UT_uint16 which; |
| 103 |
UT_uint16 ver; |
| 104 |
UT_uint16 start; |
| 105 |
UT_uint16 end; |
| 106 |
|
| 107 |
UT_uint8* data; // possible additional data. NULL if no data existant. |
| 108 |
gsf_off_t dataLen; |
| 109 |
|
| 110 |
UT_String attrName; |
| 111 |
UT_String attrVal; |
| 112 |
bool isOff; // if true, attrVal is undefined |
| 113 |
bool isPara; // should be applied to paragraph, not span |
| 114 |
}; |
| 115 |
|
| 116 |
// File Flags: (from sw/source/core/sw3io/sw3ids.hxx lines 65ff) |
| 117 |
#define SWGF_BLOCKNAME 0x0002 // Header has textmodule |
| 118 |
#define SWGF_HAS_PASSWD 0x0008 // Stream is password protected |
| 119 |
#define SWGF_HAS_PGNUMS 0x0100 // Stream has pagenumbers |
| 120 |
#define SWGF_BAD_FILE 0x8000 // There was an error writing the file |
| 121 |
|
| 122 |
// Document Flags: (from sw/source/core/sw3io/sw3doc.cxx 733ff) |
| 123 |
#define SWDF_BROWSEMODE1 0x1 // show document in browse mode? |
| 124 |
#define SWDF_BROWSEMODE2 0x2 // same as above, one of them need to be set |
| 125 |
#define SWDF_HTMLMODE 0x4 // document is in HTML Mode |
| 126 |
#define SWDF_HEADINBROWSE 0x8 // Show headers in Browse Mode |
| 127 |
#define SWDF_FOOTINBROWSE 0x10 // Show footers in browse mode |
| 128 |
#define SWDF_GLOBALDOC 0x20 // Is a global document (a global document can contain chapter documents... I think) |
| 129 |
#define SWDF_GLOBALDOCSAVELINK 0x40 // Include sections that are linked to the global document when saving |
| 130 |
#define SWDF_LABELDOC 0x80 // is a label ("etiketten") document |
| 131 |
|
| 132 |
// File versions (sw/source/core/sw3io/sw3ids.hxx 77ff) |
| 133 |
#define SWG_POOLIDS 0x3 // IDs for Stringpool-Strings |
| 134 |
#define SWG_LAYFRAMES 0x5 // Layout Frames |
| 135 |
#define SWG_RECSIZES '%' // Record Sizes |
| 136 |
#define SWG_LONGIDX 0x201 |
| 137 |
#define SWG_LONGRECS 0x209 // Record-Length > 8/16MB |
| 138 |
#define SWG_MAJORVERSION SWG_LONGIDX |
| 139 |
|
| 140 |
|
| 141 |
// Document Sections |
| 142 |
#define SWG_ATTRIBUTE 'A' // attribute of a textnode |
| 143 |
#define SWG_COMMENT 'C' // comments |
| 144 |
#define SWG_JOBSETUP 'J' // Printer Job Setup |
| 145 |
#define SWG_CONTENTS 'N' // Textpart |
| 146 |
#define SWG_ATTRSET 'S' // set of attributes |
| 147 |
#define SWG_STRINGPOOL '!' |
| 148 |
#define SWG_TEXTNODE 'T' |
| 149 |
#define SWG_EOF 'Z' |
| 150 |
|
| 151 |
// File format constants, from OpenOffice's tools/inc/solar.h line 471ff |
| 152 |
#define SOFFICE_FILEFORMAT_31 3450 |
| 153 |
#define SOFFICE_FILEFORMAT_40 3580 |
| 154 |
#define SOFFICE_FILEFORMAT_50 5050 |
| 155 |
#define SOFFICE_FILEFORMAT_60 6200 |
| 156 |
|
| 157 |
// Print Job constants |
| 158 |
#define JOBSET_FILE364_SYSTEM 0xFFFF |
| 159 |
#define JOBSET_FILE605_SYSTEM 0xFFFE |
| 160 |
|
| 161 |
#define IDX_NOCONV_FF 0xFFFC |
| 162 |
|
| 163 |
// Staroffice document sniffer. |
| 164 |
class IE_Imp_StarOffice_Sniffer : public IE_ImpSniffer |
| 165 |
{ |
| 166 |
public: |
| 167 |
IE_Imp_StarOffice_Sniffer(); |
| 168 |
virtual ~IE_Imp_StarOffice_Sniffer() {} |
| 169 |
|
| 170 |
virtual const IE_SuffixConfidence * getSuffixConfidence (); |
| 171 |
virtual const IE_MimeConfidence * getMimeConfidence (); |
| 172 |
virtual UT_Confidence_t recognizeContents(GsfInput * input); |
| 173 |
virtual bool getDlgLabels(const char** szDesc, const char** szSuffixList, IEFileType *ft); |
| 174 |
virtual UT_Error constructImporter(PD_Document* pDocument, IE_Imp **ppie); |
| 175 |
}; |
| 176 |
|
| 177 |
// Actual Importer |
| 178 |
class IE_Imp_StarOffice : public IE_Imp |
| 179 |
{ |
| 180 |
public: |
| 181 |
IE_Imp_StarOffice(PD_Document *pDocument); |
| 182 |
~IE_Imp_StarOffice(); |
| 183 |
|
| 184 |
protected: |
| 185 |
virtual UT_Error _loadFile(GsfInput * input); |
| 186 |
|
| 187 |
private: |
| 188 |
FILE* mFile; |
| 189 |
GsfInfile* mOle; |
| 190 |
GsfInput *mDocStream; |
| 191 |
DocHdr mDocHdr; |
| 192 |
|
| 193 |
/// Map for the SWG_STRINGPOOL |
| 194 |
typedef std::map<UT_uint16, std::basic_string<UT_UCS4Char> > stringpool_map; |
| 195 |
stringpool_map mStringPool; |
| 196 |
|
| 197 |
/*! Reads the record size from the stream. That is, usually |
| 198 |
* three bytes starting from the current position. |
| 199 |
* \param aStream the stream to read from |
| 200 |
* \param aSize Reference to the size of the record |
| 201 |
* \param aEOR End of Record - file position where the record is finished*/ |
| 202 |
void readRecSize(GsfInput* stream, UT_uint32& aSize, gsf_off_t* aEOR = NULL) throw(UT_Error); |
| 203 |
/*! Reads a string from the file where the first sint32 contains the length. If it |
| 204 |
* is zero-terminated, length must include the byte for termination. The string will |
| 205 |
* be converted to the charset given in mDocHdr. If the document is encrypted, the |
| 206 |
* string will be decrypted before character set conversion. |
| 207 |
* \param stream The stream to read from |
| 208 |
* \param str Reference to pointer to UT_UCS4Char, where the string is stored. |
| 209 |
* Must be free'd. Is NULL if the function fails. */ |
| 210 |
void readByteString(GsfInput* stream, UT_UCS4Char*& str) throw(UT_Error) { |
| 211 |
::readByteString(stream, str, mDocHdr.converter, mDocHdr.cryptor); |
| 212 |
} |
| 213 |
|
| 214 |
/*! Finds the version number, given a version string. |
| 215 |
* \return One of SOFFICE_FILEFORMAT_31, SOFFICE_FILEFORMAT_40 and |
| 216 |
* SOFFICE_FILEFORMAT_50 */ |
| 217 |
static UT_uint32 getVersion(const char* szVerString); |
| 218 |
}; |
| 219 |
|
| 220 |
/* Helper functions; all throw an UT_IE_BOGUSDOCUMENT on error */ |
| 221 |
/*! Reads the header of a flag record from the stream |
| 222 |
* \param flags Flags (also contain the length in the 4 least significant bytes) |
| 223 |
* \param newPos (optional) Pointer to a variable where the position after the |
| 224 |
* flags record is stored. */ |
| 225 |
void readFlagRec(GsfInput* stream, UT_uint8& flags, gsf_off_t* newPos = NULL) throw(UT_Error); |
| 226 |
|
| 227 |
/*! Reads one character from the given GsfInput. |
| 228 |
* \param aStream The OLE Stream |
| 229 |
* \param aChar Reference to the character |
| 230 |
* \throw UT_Error on failure */ |
| 231 |
inline void readChar(GsfInput* aStream, char& aChar) throw(UT_Error) { |
| 232 |
if (!gsf_input_read(aStream, 1, reinterpret_cast<guint8*>(&aChar))) |
| 233 |
throw UT_IE_BOGUSDOCUMENT; |
| 234 |
} |
| 235 |
|
| 236 |
inline void streamRead(GsfInput* aStream, UT_uint8& aDest) throw(UT_Error) { |
| 237 |
if (!gsf_input_read(aStream, 1, static_cast<guint8*>(&aDest))) |
| 238 |
throw UT_IE_BOGUSDOCUMENT; |
| 239 |
} |
| 240 |
|
| 241 |
inline void streamRead(GsfInput* aStream, UT_sint8& aDest) throw(UT_Error) { |
| 242 |
streamRead(aStream, reinterpret_cast<UT_uint8 &>(aDest)); |
| 243 |
} |
| 244 |
|
| 245 |
inline void streamRead(GsfInput* aStream, char& aDest) throw(UT_Error) { |
| 246 |
streamRead(aStream, reinterpret_cast<UT_uint8 &>(aDest)); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
inline void streamRead(GsfInput* aStream, UT_uint16& aDest, bool isLittleEndian = true) throw(UT_Error) { |
| 251 |
guint8 buf [2]; |
| 252 |
if (!gsf_input_read(aStream, 2, buf)) |
| 253 |
throw UT_IE_BOGUSDOCUMENT; |
| 254 |
if (isLittleEndian) { |
| 255 |
aDest = buf [0] | (buf [1] << 8); |
| 256 |
} |
| 257 |
else { |
| 258 |
aDest = buf [1] | (buf [0] << 8); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
inline void streamRead(GsfInput* aStream, UT_sint16& aDest, bool isLittleEndian = true) throw(UT_Error) { |
| 263 |
streamRead(aStream, reinterpret_cast<UT_uint16 &>(aDest), isLittleEndian); |
| 264 |
} |
| 265 |
|
| 266 |
inline void streamRead(GsfInput* aStream, UT_uint32& aDest, bool isLittleEndian = true) throw(UT_Error) { |
| 267 |
guint8 buf [4]; |
| 268 |
if (!gsf_input_read(aStream, 4, buf)) |
| 269 |
throw UT_IE_BOGUSDOCUMENT; |
| 270 |
if (isLittleEndian) { |
| 271 |
aDest = buf [0] | (buf [1] << 8) | (buf [2] << 16) | (buf [3] << 24); |
| 272 |
} |
| 273 |
else { |
| 274 |
aDest = buf [3] | (buf [2] << 8) | (buf [1] << 16) | (buf [0] << 24); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
inline void streamRead(GsfInput* aStream, UT_sint32& aDest, bool isLittleEndian = true) throw(UT_Error) { |
| 279 |
streamRead(aStream, reinterpret_cast<UT_uint32 &>(aDest), isLittleEndian); |
| 280 |
} |
| 281 |
|
| 282 |
// reads the value as uint8 |
| 283 |
inline void streamRead(GsfInput* aStream, bool& aDest) throw(UT_Error) { |
| 284 |
streamRead(aStream, reinterpret_cast<UT_uint8&>(aDest)); |
| 285 |
} |
| 286 |
|
| 287 |
// Class ID |
| 288 |
inline void streamRead(GsfInput* aStream, ClsId& aClsId) throw(UT_Error) { |
| 289 |
streamRead(aStream, aClsId.n1); |
| 290 |
streamRead(aStream, aClsId.n2); |
| 291 |
streamRead(aStream, aClsId.n3); |
| 292 |
streamRead(aStream, aClsId.n4); |
| 293 |
streamRead(aStream, aClsId.n5); |
| 294 |
streamRead(aStream, aClsId.n6); |
| 295 |
streamRead(aStream, aClsId.n7); |
| 296 |
streamRead(aStream, aClsId.n8); |
| 297 |
streamRead(aStream, aClsId.n9); |
| 298 |
streamRead(aStream, aClsId.n10); |
| 299 |
streamRead(aStream, aClsId.n11); |
| 300 |
} |
| 301 |
#include "ut_debugmsg.h" |
| 302 |
|
| 303 |
// for completeness... |
| 304 |
inline void streamRead(GsfInput* aStream, char* aBuffer, UT_uint32 length) throw(UT_Error) { |
| 305 |
if (!gsf_input_read(aStream, length, reinterpret_cast<guint8 *>(aBuffer))) |
| 306 |
throw UT_IE_BOGUSDOCUMENT; |
| 307 |
} |
| 308 |
|
| 309 |
inline void streamRead(GsfInput* aStream, UT_uint8* aBuffer, UT_uint32 length) throw(UT_Error) { |
| 310 |
if (!gsf_input_read(aStream, length, static_cast<guint8*>(aBuffer))) |
| 311 |
throw UT_IE_BOGUSDOCUMENT; |
| 312 |
} |
| 313 |
|
| 314 |
// readRecSize must have been called already. readFlagRec must not. |
| 315 |
// aEoa = position of the end of the attr. |
| 316 |
void streamRead(GsfInput* aStream, TextAttr& aAttr, gsf_off_t aEoa) throw(UT_Error); |
| 317 |
|
| 318 |
#endif /* IE_IMP_STAROFFICE_H */ |