Debian Codegraph @

src/tools/file/ole.h

raw
1 /* -*- Mode: C++; c-default-style: "k&r"; indent-tabs-mode: nil; tab-width: 2; c-basic-offset: 2 -*- */
2
3 /* libmwaw: tools
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) 2011, 2012 Alonso Laurent ([email protected])
18 *
19 *
20 * All Rights Reserved.
21 *
22 * For minor contributions see the git repository.
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * the GNU Lesser General Public License Version 2 or later (the "LGPLv2+"),
26 * in which case the provisions of the LGPLv2+ are applicable
27 * instead of those above.
28 */
29
30 #ifndef MWAW_OLE_H
31 # define MWAW_OLE_H
32 #include <ostream>
33 #include <map>
34 #include <set>
35 #include <string>
36 #include <vector>
37
38 namespace libmwaw_tools
39 {
40 class InputStream;
41
42 /** \brief the main class to read a OLE file ( only the main dir entry to retrieve the clsid type )
43 */
44 class OLE
45 {
46 public:
47 //! the constructor
48 explicit OLE(InputStream &input)
49 : m_input(input)
50 , m_header()
51 , m_dirTree()
52 , m_smallBlockPos()
53 , m_status(S_Unchecked)
54 {
55 }
56 //! the destructor
57 ~OLE() {}
58 //! returns true if the file is an ole
59 bool isOLE()
60 {
61 init();
62 return m_status==S_Ok;
63 }
64 //! returns a string corresponding to the main clsid or ""
65 std::string getCLSIDType();
66 //! returns a string corresponding to the comp obj clsid or ""
67 std::string getCompObjType();
68 //! returns the summary clipname string or ""
69 std::string getClipName();
70
71 protected:
72 enum { Eof = 0xfffffffe };
73 //! an enum to define the different bat type
74 enum Type { BigBat=0, SmallBat, Dirent, MetaBat, BigBatAlloc };
75 //! try to initialised the header, ...
76 bool init();
77 //! try to initialized the bigbat/smallbat table
78 bool initAllocTables();
79
80 //! returns a string correspond to the clsid or ""
81 static std::string getCLSIDType(unsigned const(&clsid)[4]);
82 //! try to load a ole sub file
83 bool load(std::string const &name, std::vector<unsigned char> &buffer);
84
85 // low level:
86 //! return true if we need to use big block
87 bool useBigBlockFor(unsigned long size) const
88 {
89 return size >= m_header.m_threshold;
90 }
91 //! returns the address of a big/small block
92 long getDataAddress(unsigned block, bool isBig) const
93 {
94 unsigned const bigSize=m_header.m_sizeBats[BigBat];
95 if (isBig) return long((block+1)*bigSize);
96 unsigned const smallSize=m_header.m_sizeBats[SmallBat];
97 auto const numBigSmall=unsigned(bigSize/smallSize);
98 auto bId=size_t(block/numBigSmall);
99 if (bId >= m_smallBlockPos.size()) return 0;
100 return long((m_smallBlockPos[bId]+1)*bigSize+smallSize*(block%numBigSmall));
101 }
102
103 //! a struct to define the header data
104 struct Header {
105 //! constructor
106 Header()
107 : m_threshold(4096)
108 , m_bigBatAlloc()
109 {
110 for (auto &num : m_numBats) num=0;
111 for (auto &start : m_startBats) start = Eof;
112 m_sizeBats[BigBat] = 0x200;
113 m_sizeBats[SmallBat] = 0x40;
114 }
115 //! try to read the ole header
116 bool read(InputStream &input);
117 //! returns the start of a big block
118 unsigned long getBigBlockPos(unsigned block) const
119 {
120 return static_cast<unsigned long>(block+1)*static_cast<unsigned long>(m_sizeBats[BigBat]);
121 }
122 //! the number of big bat, small bat, meta bat
123 unsigned m_numBats[5];
124 //! the start of the different zone
125 unsigned m_startBats[5];
126 //! the big and small block size ( default 0x40, 0x200)
127 unsigned m_sizeBats[2];
128 //! the thresold between small and bug file (usually 4K)
129 unsigned m_threshold;
130 //! the first 109 big bat alloc blocks
131 std::vector<unsigned long> m_bigBatAlloc;
132 };
133
134 //! a struct used to store the big and small allocation table
135 class AllocTable
136 {
137 public:
138 //! constructor
139 AllocTable()
140 : m_data()
141 {
142 }
143 //! return the number of elements
144 unsigned long count() const
145 {
146 return static_cast<unsigned long>(m_data.size());
147 }
148 //! accessor to the index value ( no bound check)
149 unsigned long operator[](unsigned long index) const
150 {
151 return m_data[size_t(index)];
152 }
153 //! accessor to the index value ( no bound check)
154 unsigned long &operator[](unsigned long index)
155 {
156 return m_data[size_t(index)];
157 }
158 //! resize the data
159 void resize(unsigned long newsize)
160 {
161 m_data.resize(size_t(newsize), Eof);
162 }
163 //! return the list of blocks which follow to start(included)
164 std::vector<unsigned long> follow(unsigned long start) const;
165 private:
166 //! the data
167 std::vector<unsigned long> m_data;
168 AllocTable(const AllocTable &) = delete;
169 AllocTable &operator=(const AllocTable &) = delete;
170 };
171
172 //! a struct used to store a DirEntry
173 struct DirEntry {
174 public:
175 enum { End= 0xffffffff };
176 //! constructor
177 DirEntry()
178 : m_valid(false)
179 , m_macRootEntry(false)
180 , m_type(0)
181 , m_size(0)
182 , m_start(0)
183 , m_right(End)
184 , m_left(End)
185 , m_child(End)
186 , m_name("")
187 {
188 for (auto &id : m_clsid) id = 0;
189 }
190 //! returns true for a directory
191 bool is_dir() const
192 {
193 return m_type==1 || m_type==5;
194 }
195 //! returns the simplified file name
196 std::string name() const
197 {
198 if (m_name.length() && m_name[0]<32)
199 return m_name.substr(1);
200 return m_name;
201 }
202 /** sets the file name */
203 void setName(std::string const &nm)
204 {
205 m_name=nm;
206 }
207 //! try to read the ole header
208 bool read(InputStream &input);
209
210 bool m_valid; /** false if invalid (should be skipped) */
211 bool m_macRootEntry; /** true if this is a classi mac directory entry */
212 unsigned m_type; /** the type */
213 unsigned long m_size; /** size (not valid if directory) */
214 unsigned long m_start; /** starting block */
215 unsigned m_right; /** previous sibling */
216 unsigned m_left; /** next sibling */
217 unsigned m_child; /** first child */
218
219 /** four uint32_t: the clsid data */
220 unsigned m_clsid[4];
221
222 protected:
223 std::string m_name; /** the name, not in unicode anymore */
224 };
225
226 //! a struct used to store a DirTree
227 class DirTree
228 {
229 public:
230 /** constructor */
231 DirTree()
232 : m_entries()
233 {
234 clear();
235 }
236 /** clear all entries, leaving only a root entries */
237 void clear();
238 /** returns the number of entries */
239 unsigned size() const
240 {
241 return unsigned(m_entries.size());
242 }
243 /** resize the number of entries */
244 void resize(unsigned sz)
245 {
246 m_entries.resize(size_t(sz));
247 }
248 /** returns the entry with a given index */
249 DirEntry const *entry(unsigned ind) const
250 {
251 if (ind >= size()) return 0;
252 return &m_entries[ size_t(ind) ];
253 }
254 /** returns the entry with a given index */
255 DirEntry *entry(unsigned ind)
256 {
257 if (ind >= size()) return 0;
258 return &m_entries[ size_t(ind) ];
259 }
260 /** returns the entry with a given name */
261 DirEntry *entry(const std::string &name)
262 {
263 return entry(index(name));
264 }
265 /** given a fullname (e.g "/ObjectPool/_1020961869"), find the entry */
266 unsigned index(const std::string &name) const;
267 /** tries to find a child of ind with a given name */
268 unsigned find_child(unsigned ind, const std::string &name) const;
269 protected:
270 //! returns a list of siblings corresponding to a node
271 std::vector<unsigned> get_siblings(unsigned ind) const
272 {
273 std::set<unsigned> seens;
274 get_siblings(ind, seens);
275 return std::vector<unsigned>(seens.begin(), seens.end());
276 }
277 //! constructs the list of siblings ( by filling the seens set )
278 void get_siblings(unsigned ind, std::set<unsigned> &seens) const;
279
280 private:
281 //! the list of entry
282 std::vector<DirEntry> m_entries;
283 DirTree(const DirTree &);
284 DirTree &operator=(const DirTree &);
285 };
286
287 //! read a short in the input file
288 static unsigned short readU16(InputStream &input, bool inverted=true);
289 //! read a int in the input file
290 static unsigned int readU32(InputStream &input, bool inverted=true);
291 //! the input stream
292 InputStream &m_input;
293 //! the header
294 Header m_header;
295 //! the dir tree
296 DirTree m_dirTree;
297 //! the big and small allocation table
298 AllocTable m_allocTable[2];
299 //! the small block file position
300 std::vector<unsigned long> m_smallBlockPos;
301
302 //! a enum to know if the ole struct are initialised
303 enum Status { S_Ok, S_Bad, S_Unchecked };
304 //! the actual status
305 Status m_status;
306 private:
307 OLE(OLE const &orig) = delete;
308 OLE &operator=(OLE const &orig) = delete;
309 };
310
311 }
312 #endif
313 // vim: set filetype=cpp tabstop=2 shiftwidth=2 cindent autoindent smartindent noexpandtab: