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) 2004 Marc Maurer (uwog@uwog.net)
11  * Copyright (C) 2006 Fridrich Strba (fridrich.strba@bluewin.ch)
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 "WP3FixedLengthGroup.h"
28 #include "WP3FileStructure.h"
29 #include "WP3AttributeGroup.h"
30 #include "WP3UnsupportedFixedLengthGroup.h"
31 #include "WP3ExtendedCharacterGroup.h"
32 #include "WP3DoubleByteScriptCharacterGroup.h"
33 #include "WP3TabGroup.h"
34 #include "WP3IndentGroup.h"
35 #include "WP3UndoGroup.h"
36 #include "libwpd_internal.h"
37 
WP3FixedLengthGroup(const unsigned char groupID)38 WP3FixedLengthGroup::WP3FixedLengthGroup(const unsigned char groupID)
39 	: m_group(groupID)
40 {
41 }
42 
constructFixedLengthGroup(librevenge::RVNGInputStream * input,WPXEncryption * encryption,const unsigned char groupID)43 WP3FixedLengthGroup *WP3FixedLengthGroup::constructFixedLengthGroup(librevenge::RVNGInputStream *input, WPXEncryption *encryption, const unsigned char groupID)
44 {
45 	switch (groupID)
46 	{
47 	case WP3_EXTENDED_CHARACTER_GROUP:
48 		return new WP3ExtendedCharacterGroup(input, encryption, groupID);
49 
50 	case WP3_TAB_GROUP:
51 		return new WP3TabGroup(input, encryption, groupID);
52 
53 	case WP3_INDENT_GROUP:
54 		return new WP3IndentGroup(input, encryption, groupID);
55 
56 	case WP3_UNDO_GROUP:
57 		return new WP3UndoGroup(input, encryption, groupID);
58 
59 	case WP3_ATTRIBUTE_GROUP:
60 		return new WP3AttributeGroup(input, encryption, groupID);
61 
62 	case WP3_DOUBLE_BYTE_SCRIPT_CHARACTER_GROUP:
63 		return new WP3DoubleByteScriptCharacterGroup(input, encryption, groupID);
64 
65 	// Add the remaining cases here
66 	default:
67 		return new WP3UnsupportedFixedLengthGroup(input, encryption, groupID);
68 	}
69 }
70 
isGroupConsistent(librevenge::RVNGInputStream * input,WPXEncryption * encryption,const unsigned char groupID)71 bool WP3FixedLengthGroup::isGroupConsistent(librevenge::RVNGInputStream *input, WPXEncryption *encryption, const unsigned char groupID)
72 {
73 	long startPosition = input->tell();
74 
75 	try
76 	{
77 		int size = WP3_FIXED_LENGTH_FUNCTION_GROUP_SIZE[groupID-0xC0];
78 		if (input->seek((startPosition + size - 2), librevenge::RVNG_SEEK_SET) || input->isEnd())
79 		{
80 			input->seek(startPosition, librevenge::RVNG_SEEK_SET);
81 			return false;
82 		}
83 		if (groupID != readU8(input, encryption))
84 		{
85 			input->seek(startPosition, librevenge::RVNG_SEEK_SET);
86 			return false;
87 		}
88 
89 		input->seek(startPosition, librevenge::RVNG_SEEK_SET);
90 		return true;
91 	}
92 	catch (...)
93 	{
94 		input->seek(startPosition, librevenge::RVNG_SEEK_SET);
95 		return false;
96 	}
97 }
98 
_read(librevenge::RVNGInputStream * input,WPXEncryption * encryption)99 void WP3FixedLengthGroup::_read(librevenge::RVNGInputStream *input, WPXEncryption *encryption)
100 {
101 	long startPosition = input->tell();
102 	_readContents(input, encryption);
103 
104 	if (m_group >= 0xC0 && m_group <= 0xCF) // just an extra safety check
105 	{
106 		int size = WP3_FIXED_LENGTH_FUNCTION_GROUP_SIZE[m_group-0xC0];
107 		input->seek((startPosition + size - 2), librevenge::RVNG_SEEK_SET);
108 		if (m_group != readU8(input, encryption))
109 		{
110 			WPD_DEBUG_MSG(("WordPerfect: Possible corruption detected. Bailing out!\n"));
111 			throw FileException();
112 		}
113 	}
114 	else
115 		throw FileException();
116 }
117 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */
118