/src/libwpd/src/lib/WPXHeader.cpp
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */ |
2 | | /* libwpd |
3 | | * Version: MPL 2.0 / LGPLv2.1+ |
4 | | * |
5 | | * This Source Code Form is subject to the terms of the Mozilla Public |
6 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
7 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
8 | | * |
9 | | * Major Contributor(s): |
10 | | * Copyright (C) 2002 William Lachance (wrlach@gmail.com) |
11 | | * Copyright (C) 2002-2004 Marc Maurer (uwog@uwog.net) |
12 | | * |
13 | | * For minor contributions see the git repository. |
14 | | * |
15 | | * Alternatively, the contents of this file may be used under the terms |
16 | | * of the GNU Lesser General Public License Version 2.1 or later |
17 | | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
18 | | * applicable instead of those above. |
19 | | * |
20 | | * For further information visit http://libwpd.sourceforge.net |
21 | | */ |
22 | | |
23 | | /* "This product is not manufactured, approved, or supported by |
24 | | * Corel Corporation or Corel Corporation Limited." |
25 | | */ |
26 | | |
27 | | #include "WPXHeader.h" |
28 | | #include "WP3Header.h" |
29 | | #include "WP5Header.h" |
30 | | #include "WP6Header.h" |
31 | | #include "WP60Header.h" |
32 | | #include "WP61Header.h" |
33 | | #include "WPXFileStructure.h" |
34 | | #include "libwpd_internal.h" |
35 | | |
36 | | #include <string.h> |
37 | | |
38 | | WPXHeader::WPXHeader(librevenge::RVNGInputStream * /* input */, WPXEncryption * /* encryption */, unsigned documentOffset, unsigned char productType, |
39 | | unsigned char fileType, unsigned char majorVersion, unsigned char minorVersion, unsigned short documentEncryption) : |
40 | 11.0k | m_documentOffset(documentOffset), |
41 | 11.0k | m_productType(productType), |
42 | 11.0k | m_fileType(fileType), |
43 | 11.0k | m_majorVersion(majorVersion), |
44 | 11.0k | m_minorVersion(minorVersion), |
45 | 11.0k | m_documentEncryption(documentEncryption) |
46 | 11.0k | { |
47 | 11.0k | } |
48 | | |
49 | | WPXHeader::~WPXHeader() |
50 | 11.0k | { |
51 | 11.0k | } |
52 | | |
53 | | WPXHeader *WPXHeader::constructHeader(librevenge::RVNGInputStream *input, WPXEncryption *encryption) |
54 | 20.4k | { |
55 | 20.4k | WPD_DEBUG_MSG(("WPXHeader::constructHeader()\n")); |
56 | | |
57 | 20.4k | char fileMagic[4] = { 0, 0, 0, 0 }; |
58 | | /* check the magic */ |
59 | 20.4k | input->seek(WPX_HEADER_MAGIC_OFFSET, librevenge::RVNG_SEEK_SET); |
60 | 81.5k | for (int i=0; i<3 && !input->isEnd(); i++) |
61 | 61.1k | fileMagic[i] = (char)readU8(input, encryption); |
62 | | |
63 | 20.4k | if (strcmp(fileMagic, "WPC")) |
64 | 9.31k | { |
65 | 9.31k | WPD_DEBUG_MSG(("WordPerfect: File magic is not equal to \"WPC\"!\n")); |
66 | 9.31k | return nullptr; |
67 | 9.31k | } |
68 | | |
69 | | /* get the document pointer */ |
70 | 11.1k | input->seek(WPX_HEADER_DOCUMENT_POINTER_OFFSET, librevenge::RVNG_SEEK_SET); |
71 | 11.1k | unsigned documentOffset = readU32(input, encryption); |
72 | | |
73 | | /* get information on product types, file types, versions */ |
74 | 11.1k | input->seek(WPX_HEADER_PRODUCT_TYPE_OFFSET, librevenge::RVNG_SEEK_SET); |
75 | 11.1k | unsigned char productType = readU8(input, encryption); |
76 | 11.1k | unsigned char fileType = readU8(input, encryption); |
77 | 11.1k | unsigned char majorVersion = readU8(input, encryption); |
78 | 11.1k | unsigned char minorVersion = readU8(input, encryption); |
79 | | |
80 | 11.1k | input->seek(WPX_HEADER_ENCRYPTION_OFFSET, librevenge::RVNG_SEEK_SET); |
81 | 11.1k | unsigned short documentEncryption = readU16(input, encryption); |
82 | | |
83 | 11.1k | WPD_DEBUG_MSG(("WordPerfect: Document Offset = 0x%x \n",documentOffset)); |
84 | 11.1k | WPD_DEBUG_MSG(("WordPerfect: Product Type: %i File Type: %i Major Version: %i Minor Version: %i\n", |
85 | 11.1k | productType, fileType, |
86 | 11.1k | majorVersion, minorVersion)); |
87 | 11.1k | WPD_DEBUG_MSG(("WordPerfect: Document Encryption = 0x%x \n",(int)documentEncryption)); |
88 | | |
89 | | |
90 | 11.1k | switch (fileType) |
91 | 11.1k | { |
92 | 1.19k | case 0x0a: |
93 | 1.19k | WPD_DEBUG_MSG(("WordPerfect: Supported file type: \"WordPerfect Document\"\n")); |
94 | 1.19k | switch (majorVersion) |
95 | 1.19k | { |
96 | 1.15k | case 0x00: // WP5 |
97 | 1.15k | documentEncryption = (unsigned short)(((documentEncryption & 0xff00) >> 8) | ((documentEncryption & 0x00ff) << 8)); |
98 | 1.15k | return new WP5Header(input, encryption, documentOffset, productType, fileType, |
99 | 1.15k | majorVersion, minorVersion, documentEncryption); |
100 | 33 | case 0x02: // WP6 |
101 | 33 | switch (minorVersion) |
102 | 33 | { |
103 | 11 | case 0x00: |
104 | 11 | return new WP60Header(input, encryption, documentOffset, productType, fileType, |
105 | 11 | majorVersion, minorVersion, documentEncryption); |
106 | 22 | default: // assume this header can be parsed by a WP61 header parser |
107 | 22 | return new WP61Header(input, encryption, documentOffset, productType, fileType, |
108 | 22 | majorVersion, minorVersion, documentEncryption); |
109 | 33 | } |
110 | 3 | default: |
111 | | // unhandled file format |
112 | 3 | WPD_DEBUG_MSG(("WordPerfect: Unsupported major number: %d\n", majorVersion)); |
113 | 3 | return nullptr; |
114 | 1.19k | } |
115 | 9.91k | case 0x2c: |
116 | 9.91k | WPD_DEBUG_MSG(("WordPerfect: Supported file type: \"MAC WP 2.0 document\"\n")); |
117 | 9.91k | switch (majorVersion) |
118 | 9.91k | { |
119 | 7 | case 0x02: // WP Mac 2.x |
120 | 56 | case 0x03: // WP Mac 3.0-3.5 |
121 | 9.90k | case 0x04: // WP Mac 3.5e |
122 | 9.90k | return new WP3Header(input, encryption, documentOffset, productType, fileType, |
123 | 9.90k | majorVersion, minorVersion, documentEncryption); |
124 | 8 | default: |
125 | | // unhandled file format |
126 | 8 | WPD_DEBUG_MSG(("WordPerfect: Unsupported major number: %d\n", majorVersion)); |
127 | 8 | return nullptr; |
128 | 9.91k | } |
129 | 4 | default: |
130 | 4 | WPD_DEBUG_MSG(("WordPerfect: Unsupported file type: %d\n", fileType)); |
131 | 4 | return nullptr; |
132 | 11.1k | } |
133 | 11.1k | } |
134 | | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |