Debian Codegraph @

src/lib/WPS8Struct.cpp

raw symbols SCIP
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* libwps
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) 2009, 2011 Alonso Laurent ([email protected])
11 * Copyright (C) 2006, 2007 Andrew Ziem
12 * Copyright (C) 2004-2006 Fridrich Strba ([email protected])
13 * Copyright (C) 2004 Marc Maurer ([email protected])
14 * Copyright (C) 2003-2005 William Lachance ([email protected])
15 *
16 * For minor contributions see the git repository.
17 *
18 * Alternatively, the contents of this file may be used under the terms
19 * of the GNU Lesser General Public License Version 2.1 or later
20 * (LGPLv2.1+), in which case the provisions of the LGPLv2.1+ are
21 * applicable instead of those above.
22 *
23 * For further information visit http://libwps.sourceforge.net
24 */
25
26 #include "WPS8Struct.h"
27
28 #include "WPSDebug.h"
29
30 namespace WPS8Struct
31 {
32 // try to read a block, which can be or not a list of data
33 bool FileData::readArrayBlock() const
34 {
35 if (isRead()) return isArray();
36 long actPos = m_input->tell();
37 m_input->seek(m_beginOffset, librevenge::RVNG_SEEK_SET);
38 std::string error;
39 bool ok = readBlockData(m_input, m_endOffset, const_cast<FileData &>(*this), error);
40 m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
41 return ok;
42 }
43
44 // create a message to store unparsed data
45 std::string FileData::createErrorString(RVNGInputStreamPtr input, long endPos)
46 {
47 libwps::DebugStream f;
48 f << ",###unread=(" << std::hex;
49 while (input->tell() < endPos-1) f << libwps::readU16(input) << ", ";
50 if (input->tell() < endPos) f << libwps::readU8(input) << ", ";
51 f << ")";
52
53 return f.str();
54 }
55
56 bool FileData::getBorderStyles(WPSBorder::Style &style, WPSBorder::Type &borderType, std::string &mess) const
57 {
58 style = WPSBorder::Simple;
59 borderType = WPSBorder::Single;
60 libwps::DebugStream f;
61 switch (m_value)
62 {
63 case 0:
64 style = WPSBorder::None;
65 break;
66 case 1: // normal
67 break;
68 case 2: // double normal
69 borderType = WPSBorder::Double;
70 break;
71 case 3:
72 f << "ext=2,int=1,";
73 borderType = WPSBorder::Double;
74 break;
75 case 4:
76 f << "ext=1,int=2,";
77 borderType = WPSBorder::Double;
78 break;
79 case 5:
80 style = WPSBorder::Dash;
81 break;
82 case 6:
83 style = WPSBorder::LargeDot;
84 break;
85 case 7:
86 style = WPSBorder::Dot;
87 break;
88 case 8:
89 f << "dash+rot-5,";
90 style = WPSBorder::Dash;
91 break;
92 case 9:
93 f << "dash+rot5,";
94 style = WPSBorder::Dash;
95 break;
96 case 0xa:
97 f << "triple,";
98 borderType = WPSBorder::Triple;
99 break;
100 default:
101 f << "#style=" << std::hex << m_value << std::dec << ",";
102 break;
103 }
104
105 mess = f.str();
106 return true;
107 }
108
109 // operator <<
110 std::ostream &operator<< (std::ostream &o, FileData const &dt)
111 {
112 if (dt.id() != -1)
113 o << "unkn" << std::hex << dt.id() << "[typ=" << dt.m_type << "]:" << std::dec;
114 auto &DT = const_cast<FileData &>(dt);
115 // If the data are unread, try to read them as a block list
116 if (!dt.isRead() && !dt.readArrayBlock())
117 {
118 // if this fails...
119 long size = dt.m_endOffset-dt.m_beginOffset-2;
120 int sz = (size%4) == 0 ? 4 : (size%2) == 0 ? 2 : 1;
121 auto numElt = int(size/sz);
122
123 long actPos = DT.m_input->tell();
124 DT.m_input->seek(dt.m_beginOffset, librevenge::RVNG_SEEK_SET);
125 o << "###FAILS[sz="<< sz << "]=(" << std::hex;
126 auto val = long(libwps::read16(DT.m_input));
127 if (val) o << "unkn=" << val <<",";
128 for (int i = 0; i < numElt; i++)
129 {
130 switch (sz)
131 {
132 case 1:
133 o << libwps::readU8(DT.m_input) << ",";
134 break;
135 case 2:
136 o << libwps::readU16(DT.m_input) << ",";
137 break;
138 case 4:
139 o << libwps::readU32(DT.m_input) << ",";
140 break;
141 default:
142 break;
143 }
144 }
145 o << ")" << std::dec;
146
147 DT.m_input->seek(actPos, librevenge::RVNG_SEEK_SET);
148
149 return o;
150 }
151 if (dt.hasStr()) o << "('" << dt.m_text << "')";
152 if (dt.isFalse()) o << "=false,";
153 if ((dt.m_type & 0x30) || dt.m_value)
154 o << "=" << dt.m_value << ":" << std::hex << dt.m_value << std::dec;
155 if (dt.m_recursData.empty()) return o;
156
157 o << ",ch=(";
158 for (auto const &data : dt.m_recursData)
159 {
160 if (data.isBad()) continue;
161 o << data << ",";
162 }
163 o << ")";
164 return o;
165 }
166
167 // try to read a data : which can be an item, a list or unknown zone
168 bool readBlockData(RVNGInputStreamPtr input, long endPos, FileData &dt, std::string &error)
169 {
170 std::string saveError = error;
171 long actPos = input->tell();
172 dt.m_recursData.resize(0);
173
174 if (actPos+2 > endPos) // to short
175 {
176 error += FileData::createErrorString(input, endPos);
177 return false;
178 }
179
180 dt.m_value = libwps::readU16(input); // normally 0, but who know ...
181 dt.m_beginOffset = dt.m_endOffset = -1;
182
183 int prevId = -1;
184 bool ok = true;
185 while (input->tell() != endPos)
186 {
187 FileData child;
188 if (!readData(input, endPos, child, error))
189 {
190 ok = false;
191 break;
192 }
193 if (child.isBad()) continue;
194 if (prevId > child.id())
195 {
196 ok = false;
197 break;
198 }
199 prevId = child.id();
200 dt.m_recursData.push_back(child);
201 }
202 if (ok) return true;
203
204 if (dt.m_type == -1) dt.m_type = 0x80;
205 dt.m_beginOffset = actPos;
206 dt.m_endOffset = endPos;
207 dt.m_input = input;
208
209 error = saveError;
210 input->seek(endPos, librevenge::RVNG_SEEK_SET);
211
212 return false;
213 }
214
215 // try to read an item
216 bool readData(RVNGInputStreamPtr input, long endPos,
217 FileData &dt, std::string &/*error*/)
218 {
219 long actPos = input->tell();
220 dt = FileData();
221
222 if (actPos >= endPos) return false;
223
224 auto val = long(libwps::readU16(input));
225 dt.m_type = int((val & 0xFF00)>>8);
226 dt.m_id = (val & 0xFF);
227
228 if (dt.m_type & 5)
229 {
230 dt.m_type = -1;
231 return false;
232 }
233
234 dt.m_value = 0;
235 // what is the meaning of dt.m_type & 0xF
236 // maybe :
237 // 0x1/0x4 -> never seem
238 // 0x2 -> set for the main child ?
239 // 0x8 -> signed/unsigned ? set/unset for bool ?
240 switch (dt.m_type>>4)
241 {
242 case 0:
243 return true;
244 case 1:
245 if (actPos+4 > endPos) break;
246 if (dt.m_type == 0x12)
247 {
248 dt.m_value = libwps::readU8(input);
249 input->seek(1, librevenge::RVNG_SEEK_CUR);
250 }
251 else
252 dt.m_value = libwps::readU16(input);
253 return true;
254 case 2:
255 {
256 if (dt.m_type == 0x2a) // special case : STR4 + long
257 {
258 if (actPos+10 > endPos) break;
259 for (int i = 0; i < 4; i++) dt.m_text += char(libwps::readU8(input));
260 dt.m_value = libwps::read32(input);
261 return true;
262 }
263 if (actPos+6 > endPos) break;
264 dt.m_value = libwps::read32(input);
265 return true;
266 }
267 case 8:
268 {
269 if (actPos+4 > endPos) break;
270
271 auto extraSize = long(libwps::readU16(input));
272 long newEndPos = actPos+2+extraSize;
273
274 if ((extraSize%2) || newEndPos > endPos) break;
275
276 // can either be a list of data or a structured list, so we stored the information
277 dt.m_beginOffset = actPos+4;
278 dt.m_endOffset = newEndPos;
279 dt.m_input = input;
280 input->seek(newEndPos, librevenge::RVNG_SEEK_SET);
281 return true;
282 }
283 default:
284 break;
285 }
286
287 dt.m_type = -1;
288 return false;
289 }
290
291 }
292
293 /* vim:set shiftwidth=4 softtabstop=4 noexpandtab: */