/src/libwpd/src/lib/WP5VariableLengthGroup.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) 2003 William Lachance (wrlach@gmail.com) |
11 | | * Copyright (C) 2003-2004 Marc Maurer (uwog@uwog.net) |
12 | | * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch) |
13 | | * |
14 | | * For minor contributions see the git repository. |
15 | | * |
16 | | * Alternatively, the contents of this file may be used under the terms |
17 | | * of the GNU Lesser General Public License Version 2.1 or later |
18 | | * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are |
19 | | * applicable instead of those above. |
20 | | * |
21 | | * For further information visit http://libwpd.sourceforge.net |
22 | | */ |
23 | | |
24 | | /* "This product is not manufactured, approved, or supported by |
25 | | * Corel Corporation or Corel Corporation Limited." |
26 | | */ |
27 | | |
28 | | #include "WP5PageFormatGroup.h" |
29 | | #include "WP5VariableLengthGroup.h" |
30 | | #include "WP5UnsupportedVariableLengthGroup.h" |
31 | | #include "libwpd_internal.h" |
32 | | #include "WP5HeaderFooterGroup.h" |
33 | | #include "WP5FontGroup.h" |
34 | | #include "WP5BoxGroup.h" |
35 | | #include "WP5FootnoteEndnoteGroup.h" |
36 | | #include "WP5DefinitionGroup.h" |
37 | | #include "WP5TableEOLGroup.h" |
38 | | #include "WP5TableEOPGroup.h" |
39 | | |
40 | | WP5VariableLengthGroup::WP5VariableLengthGroup() : |
41 | 62.5k | m_subGroup(0), |
42 | 62.5k | m_size(0) |
43 | 62.5k | { |
44 | 62.5k | } |
45 | | |
46 | | WP5VariableLengthGroup *WP5VariableLengthGroup::constructVariableLengthGroup(librevenge::RVNGInputStream *input, WPXEncryption *encryption, const unsigned char group) |
47 | 62.5k | { |
48 | 62.5k | WPD_DEBUG_MSG(("WordPerfect: handling a variable length group Ox%x\n", group)); |
49 | 62.5k | switch (group) |
50 | 62.5k | { |
51 | 700 | case WP5_TOP_DEFINITION_GROUP: |
52 | 700 | return new WP5DefinitionGroup(input, encryption); |
53 | 18.8k | case WP5_TOP_PAGE_FORMAT_GROUP: |
54 | 18.8k | return new WP5PageFormatGroup(input, encryption); |
55 | 18.9k | case WP5_TOP_HEADER_FOOTER_GROUP: |
56 | 18.9k | return new WP5HeaderFooterGroup(input, encryption); |
57 | 3.98k | case WP5_TOP_FONT_GROUP: |
58 | 3.98k | return new WP5FontGroup(input, encryption); |
59 | 8.28k | case WP5_TOP_FOOTNOTE_ENDNOTE_GROUP: |
60 | 8.28k | return new WP5FootnoteEndnoteGroup(input, encryption); |
61 | 6.22k | case WP5_TOP_BOX_GROUP: |
62 | 6.22k | return new WP5BoxGroup(input, encryption); |
63 | 3.62k | case WP5_TOP_TABLE_EOL_GROUP: |
64 | 3.62k | return new WP5TableEOLGroup(input, encryption); |
65 | 692 | case WP5_TOP_TABLE_EOP_GROUP: |
66 | 692 | return new WP5TableEOPGroup(input, encryption); |
67 | 1.26k | default: |
68 | | // this is an unhandled group, just skip it |
69 | 1.26k | return new WP5UnsupportedVariableLengthGroup(input, encryption); |
70 | 62.5k | } |
71 | 62.5k | } |
72 | | |
73 | | bool WP5VariableLengthGroup::isGroupConsistent(librevenge::RVNGInputStream *input, WPXEncryption *encryption, const unsigned char group) |
74 | 2.98M | { |
75 | 2.98M | long startPosition = input->tell(); |
76 | | |
77 | 2.98M | try |
78 | 2.98M | { |
79 | 2.98M | unsigned char subGroup = readU8(input, encryption); |
80 | 2.98M | unsigned short size = readU16(input, encryption); |
81 | | |
82 | 2.98M | if (input->seek((startPosition + size - 1), librevenge::RVNG_SEEK_SET) || input->isEnd()) |
83 | 2.21M | { |
84 | 2.21M | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
85 | 2.21M | return false; |
86 | 2.21M | } |
87 | 768k | if (size != readU16(input, encryption)) |
88 | 667k | { |
89 | 667k | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
90 | 667k | return false; |
91 | 667k | } |
92 | 100k | if (subGroup != readU8(input, encryption)) |
93 | 16.4k | { |
94 | 16.4k | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
95 | 16.4k | return false; |
96 | 16.4k | } |
97 | 84.2k | if (group != readU8(input, encryption)) |
98 | 5.20k | { |
99 | 5.20k | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
100 | 5.20k | return false; |
101 | 5.20k | } |
102 | | |
103 | 79.0k | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
104 | 79.0k | return true; |
105 | 84.2k | } |
106 | 2.98M | catch (...) |
107 | 2.98M | { |
108 | 16.5k | input->seek(startPosition, librevenge::RVNG_SEEK_SET); |
109 | 16.5k | return false; |
110 | 16.5k | } |
111 | 2.98M | } |
112 | | |
113 | | void WP5VariableLengthGroup::_read(librevenge::RVNGInputStream *input, WPXEncryption *encryption) |
114 | 62.5k | { |
115 | 62.5k | long startPosition = input->tell(); |
116 | | |
117 | 62.5k | m_subGroup = readU8(input, encryption); |
118 | 62.5k | m_size = (unsigned short)(readU16(input, encryption) + 4); // the length is the number of data bytes minus 4 (ie. the function codes) |
119 | | |
120 | 62.5k | WPD_DEBUG_MSG(("WordPerfect: Read variable group header (start_position: %li, sub_group: 0x%2x, size: %u)\n", startPosition, m_subGroup, m_size)); |
121 | | |
122 | 62.5k | _readContents(input, encryption); |
123 | | |
124 | 62.5k | input->seek((startPosition + m_size - 5), librevenge::RVNG_SEEK_SET); |
125 | | |
126 | 62.5k | if (m_size != (readU16(input, encryption) + 4)) |
127 | 0 | { |
128 | 0 | WPD_DEBUG_MSG(("WordPerfect: Possible corruption detected. Bailing out!\n")); |
129 | 0 | throw FileException(); |
130 | 0 | } |
131 | 62.5k | if (m_subGroup != readU8(input, encryption)) |
132 | 0 | { |
133 | 0 | WPD_DEBUG_MSG(("WordPerfect: Possible corruption detected. Bailing out!\n")); |
134 | 0 | throw FileException(); |
135 | 0 | } |
136 | | |
137 | 62.5k | input->seek((startPosition + m_size - 1), librevenge::RVNG_SEEK_SET); |
138 | 62.5k | } |
139 | | /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */ |