/src/libstaroffice/src/lib/StarZone.hxx
Line | Count | Source |
1 | | /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */ |
2 | | |
3 | | /* libstaroffice |
4 | | * Version: MPL 2.0 / LGPLv2+ |
5 | | * |
6 | | * The contents of this file are subject to the Mozilla Public License Version |
7 | | * 2.0 (the "License"); you may not use this file except in compliance with |
8 | | * the License or as specified alternatively below. You may obtain a copy of |
9 | | * the License at http://www.mozilla.org/MPL/ |
10 | | * |
11 | | * Software distributed under the License is distributed on an "AS IS" basis, |
12 | | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
13 | | * for the specific language governing rights and limitations under the |
14 | | * License. |
15 | | * |
16 | | * Major Contributor(s): |
17 | | * Copyright (C) 2002 William Lachance (wrlach@gmail.com) |
18 | | * Copyright (C) 2002,2004 Marc Maurer (uwog@uwog.net) |
19 | | * Copyright (C) 2004-2006 Fridrich Strba (fridrich.strba@bluewin.ch) |
20 | | * Copyright (C) 2006, 2007 Andrew Ziem |
21 | | * Copyright (C) 2011, 2012 Alonso Laurent (alonso@loria.fr) |
22 | | * |
23 | | * |
24 | | * All Rights Reserved. |
25 | | * |
26 | | * For minor contributions see the git repository. |
27 | | * |
28 | | * Alternatively, the contents of this file may be used under the terms of |
29 | | * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"), |
30 | | * in which case the provisions of the LGPLv2+ are applicable |
31 | | * instead of those above. |
32 | | */ |
33 | | |
34 | | /* |
35 | | * StarZone zone of StarOffice document |
36 | | * |
37 | | */ |
38 | | #ifndef STAR_ZONE |
39 | | # define STAR_ZONE |
40 | | |
41 | | #include <vector> |
42 | | #include <stack> |
43 | | |
44 | | #include "libstaroffice_internal.hxx" |
45 | | |
46 | | #include "STOFFDebug.hxx" |
47 | | #include "StarEncoding.hxx" |
48 | | |
49 | | class StarEncryption; |
50 | | |
51 | | /** \brief a zone in a StarOffice file |
52 | | * |
53 | | * |
54 | | * |
55 | | */ |
56 | | class StarZone |
57 | | { |
58 | | public: |
59 | | //! constructor |
60 | | StarZone(STOFFInputStreamPtr const &input, std::string const &ascName, std::string const &zoneName, char const *password); |
61 | | //! destructor |
62 | | virtual ~StarZone(); |
63 | | //! read the zone header present in a SW file |
64 | | bool readSWHeader(); |
65 | | |
66 | | //! check encryption |
67 | | bool checkEncryption(uint32_t date, uint32_t time, std::vector<uint8_t> const &passwd); |
68 | | //! open a zone header present in a SDR file |
69 | | bool openSDRHeader(std::string &magic); |
70 | | //! close a zone header |
71 | | bool closeSDRHeader(std::string const &debugName); |
72 | | |
73 | | //! open a zone header present in a SCH file |
74 | | bool openSCHHeader(); |
75 | | //! close a zone header |
76 | | bool closeSCHHeader(std::string const &debugName); |
77 | | |
78 | | //! open a version compat header (version+size) |
79 | | bool openVersionCompatHeader(); |
80 | | //! close a zone header |
81 | | bool closeVersionCompatHeader(std::string const &debugName); |
82 | | |
83 | | //! returns the StarOffice version: 3-5 |
84 | | int getVersion() const |
85 | 0 | { |
86 | 0 | return m_version; |
87 | 0 | } |
88 | | //! returns the StarOffice document version |
89 | | int getDocumentVersion() const |
90 | 2.32k | { |
91 | 2.32k | return m_documentVersion; |
92 | 2.32k | } |
93 | | //! returns the StarOffice header version (if set) |
94 | | int getHeaderVersion() const |
95 | 668k | { |
96 | 668k | return m_headerVersionStack.empty() ? 0 : m_headerVersionStack.top(); |
97 | 668k | } |
98 | | //! checks if the document is compatible with vers |
99 | | int isCompatibleWith(int vers) const |
100 | 591k | { |
101 | 591k | return m_documentVersion>=vers; |
102 | 591k | } |
103 | | //! checks if the document is compatible with vers1 and not vers2 |
104 | | int isCompatibleWith(int vers1, int vers2) const |
105 | 41.0k | { |
106 | 41.0k | return m_documentVersion>=vers1 && m_documentVersion<vers2; |
107 | 41.0k | } |
108 | | //! checks if the document is compatible with vers1 and not vers2 or vers3 |
109 | | int isCompatibleWith(int vers1, int vers2, int vers3) const |
110 | 138k | { |
111 | 138k | return (m_documentVersion>=vers1 && m_documentVersion<vers2) || |
112 | 137k | m_documentVersion>=vers3; |
113 | 138k | } |
114 | | //! checks if the document is compatible with vers1 and not vers2 or vers3 and not vers4 |
115 | | int isCompatibleWith(int vers1, int vers2, int vers3, int vers4) const |
116 | 20.5k | { |
117 | 20.5k | return (m_documentVersion>=vers1 && m_documentVersion<vers2) || |
118 | 20.4k | (m_documentVersion>=vers3 && m_documentVersion<vers4); |
119 | 20.5k | } |
120 | | //! returns the zone encoding |
121 | | StarEncoding::Encoding getEncoding() const |
122 | 4.10k | { |
123 | 4.10k | return m_encoding; |
124 | 4.10k | } |
125 | | //! sets the zone encoding |
126 | | void setEncoding(StarEncoding::Encoding encod) |
127 | 11.1k | { |
128 | 11.1k | m_encoding=encod; |
129 | 11.1k | } |
130 | | //! returns the zone GUI type |
131 | | int getGuiType() const |
132 | 0 | { |
133 | 0 | return m_guiType; |
134 | 0 | } |
135 | | //! sets the zone GUI type |
136 | | void setGuiType(int type) |
137 | 6.10k | { |
138 | 6.10k | m_guiType=type; |
139 | 6.10k | } |
140 | | // |
141 | | // basic |
142 | | // |
143 | | |
144 | | //! try to open a classic record: size (32 bytes) + size-4 bytes |
145 | | bool openRecord(); |
146 | | //! try to close a record |
147 | | bool closeRecord(std::string const &debugName) |
148 | 715k | { |
149 | 715k | return closeRecord(' ', debugName); |
150 | 715k | } |
151 | | //! open a dummy record |
152 | | bool openDummyRecord(); |
153 | | //! close a dummy record |
154 | | bool closeDummyRecord() |
155 | 1.23M | { |
156 | 1.23M | return closeRecord('@', "Entries(BadDummy)"); |
157 | 1.23M | } |
158 | | // |
159 | | // sc record |
160 | | // |
161 | | |
162 | | //! try to open a SC record: size (32 bytes) + size bytes |
163 | | bool openSCRecord(); |
164 | | //! try to close a record |
165 | | bool closeSCRecord(std::string const &debugName) |
166 | 262k | { |
167 | 262k | return closeRecord('_', debugName); |
168 | 262k | } |
169 | | |
170 | | // |
171 | | // sw record |
172 | | // |
173 | | |
174 | | //! try to open a SW record: type + size (24 bytes) |
175 | | bool openSWRecord(unsigned char &type); |
176 | | //! try to close a record |
177 | | bool closeSWRecord(unsigned char type, std::string const &debugName) |
178 | 851k | { |
179 | 851k | return closeRecord(type, debugName); |
180 | 851k | } |
181 | | |
182 | | // |
183 | | // sfx record |
184 | | // |
185 | | |
186 | | //! try to open a Sfx record: type + size (24 bytes) |
187 | | bool openSfxRecord(unsigned char &type); |
188 | | //! try to close a record |
189 | | bool closeSfxRecord(unsigned char type, std::string const &debugName) |
190 | 649k | { |
191 | 649k | return closeRecord(type, debugName); |
192 | 649k | } |
193 | | |
194 | | //! returns the record level |
195 | | int getRecordLevel() const |
196 | 9.99M | { |
197 | 9.99M | return int(m_positionStack.size()); |
198 | 9.99M | } |
199 | | //! returns the actual record last position |
200 | | long getRecordLastPosition() const |
201 | 3.96M | { |
202 | 3.96M | if (m_positionStack.empty()) { |
203 | 533 | STOFF_DEBUG_MSG(("StarZone::getRecordLastPosition: can not find last position\n")); |
204 | 533 | return 0; |
205 | 533 | } |
206 | 3.96M | return m_positionStack.top(); |
207 | 3.96M | } |
208 | | |
209 | | //! try to open a cflag zone |
210 | | unsigned char openFlagZone(); |
211 | | //! close the cflag zone |
212 | | void closeFlagZone(); |
213 | | //! returns the flag last position |
214 | | long getFlagLastPosition() const |
215 | 1.17k | { |
216 | 1.17k | return m_flagEndZone; |
217 | 1.17k | } |
218 | | |
219 | | //! try to read an unicode string |
220 | | bool readString(std::vector<uint32_t> &string, int encoding=-1) const |
221 | 3.43M | { |
222 | 3.43M | std::vector<size_t> srcPositions; |
223 | 3.43M | return readString(string, srcPositions, encoding); |
224 | 3.43M | } |
225 | | //! try to read an unicode string |
226 | | bool readString(std::vector<uint32_t> &string, std::vector<size_t> &srcPositions, int encoding=-1, bool checkEncryption=false) const; |
227 | | //! try to read a pool of strings |
228 | | bool readStringsPool(); |
229 | | //! return the number of pool name |
230 | | int getNumPoolNames() const |
231 | 26.6k | { |
232 | 26.6k | return int(m_poolList.size()); |
233 | 26.6k | } |
234 | | //! try to return a pool name |
235 | | bool getPoolName(int poolId, librevenge::RVNGString &res) const |
236 | 196k | { |
237 | 196k | res=""; |
238 | 196k | if (poolId>=0 && poolId<int(m_poolList.size())) { |
239 | 91.6k | res=m_poolList[size_t(poolId)]; |
240 | 91.6k | return true; |
241 | 91.6k | } |
242 | 104k | if (poolId==0xFFF0) return true; |
243 | 104k | STOFF_DEBUG_MSG(("StarZone::getPoolName: can not find pool name for %d\n", poolId)); |
244 | 104k | return false; |
245 | 104k | } |
246 | | //! return the zone input |
247 | | STOFFInputStreamPtr input() |
248 | 22.9M | { |
249 | 22.9M | return m_input; |
250 | 22.9M | } |
251 | | //! reset the current input |
252 | | void setInput(STOFFInputStreamPtr input); |
253 | | //! returns the ascii file |
254 | | libstoff::DebugFile &ascii() |
255 | 11.2M | { |
256 | 11.2M | return m_ascii; |
257 | 11.2M | } |
258 | | //! return the zone name |
259 | | std::string const &name() const |
260 | 0 | { |
261 | 0 | return m_zoneName; |
262 | 0 | } |
263 | | protected: |
264 | | // |
265 | | // low level |
266 | | // |
267 | | |
268 | | //! try to read the record sizes |
269 | | bool readRecordSizes(long pos); |
270 | | //! try to close a record |
271 | | bool closeRecord(unsigned char type, std::string const &debugName); |
272 | | |
273 | | // |
274 | | // data |
275 | | // |
276 | | |
277 | | //! the input stream |
278 | | STOFFInputStreamPtr m_input; |
279 | | //! the ascii zone |
280 | | libstoff::DebugFile m_ascii; |
281 | | //! the zone version |
282 | | int m_version; |
283 | | //! the document version |
284 | | int m_documentVersion; |
285 | | //! the header version (for SDR zone) |
286 | | std::stack<int> m_headerVersionStack; |
287 | | //! the zone encoding |
288 | | StarEncoding::Encoding m_encoding; |
289 | | //! the zone GUI type |
290 | | int m_guiType; |
291 | | //! the encryption |
292 | | std::shared_ptr<StarEncryption> m_encryption; |
293 | | //! the file ascii name |
294 | | std::string m_asciiName; |
295 | | //! the zone name |
296 | | std::string m_zoneName; |
297 | | |
298 | | //! the type stack |
299 | | std::stack<unsigned char> m_typeStack; |
300 | | //! the position stack |
301 | | std::stack<long> m_positionStack; |
302 | | //! other position to end position zone |
303 | | std::map<long, long> m_beginToEndMap; |
304 | | //! end of a cflags zone |
305 | | long m_flagEndZone; |
306 | | |
307 | | //! the pool name list |
308 | | std::vector<librevenge::RVNGString> m_poolList; |
309 | | }; |
310 | | #endif |
311 | | // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: |