1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/*
* Copyright (C) 2004-2012 Geometer Plus <contact@geometerplus.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include <ZLLogger.h>
#include "OleStorage.h"
#include "OleUtil.h"
#include <cstring>
const std::size_t OleStorage::BBD_BLOCK_SIZE = 512;
OleStorage::OleStorage() {
clear();
}
void OleStorage::clear() {
myInputStream = 0;
mySectorSize = 0;
myShortSectorSize = 0;
myStreamSize = 0;
myRootEntryIndex = -1;
myDIFAT.clear();
myBBD.clear();
mySBD.clear();
myProperties.clear();
myEntries.clear();
}
bool OleStorage::init(shared_ptr<ZLInputStream> stream, std::size_t streamSize) {
clear();
myInputStream = stream;
myStreamSize = streamSize;
myInputStream->seek(0, true);
char oleBuf[BBD_BLOCK_SIZE];
std::size_t ret = myInputStream->read(oleBuf, BBD_BLOCK_SIZE);
if (ret != BBD_BLOCK_SIZE) {
clear();
return false;
}
static const char OLE_SIGN[] = {(char)0xD0, (char)0xCF, (char)0x11, (char)0xE0, (char)0xA1, (char)0xB1, (char)0x1A, (char)0xE1, 0};
if (std::strncmp(oleBuf, OLE_SIGN, 8) != 0) {
clear();
return false;
}
mySectorSize = 1 << OleUtil::getU2Bytes(oleBuf, 0x1e); //offset for value of big sector size
myShortSectorSize = 1 << OleUtil::getU2Bytes(oleBuf, 0x20); //offset for value of small sector size
if (readDIFAT(oleBuf) && readBBD(oleBuf) && readSBD(oleBuf) && readProperties(oleBuf) && readAllEntries()) {
return true;
}
clear();
return false;
}
bool OleStorage::readDIFAT(char *oleBuf) {
int difatBlock = OleUtil::get4Bytes(oleBuf, 0x44); //address for first difat sector
int difatSectorNumbers = OleUtil::get4Bytes(oleBuf, 0x48); //numbers of additional difat records
//436 of difat records are stored in header, by offset 0x4c
for (unsigned int i = 0; i < 436; i += 4) {
myDIFAT.push_back(OleUtil::get4Bytes(oleBuf + 0x4c, i));
}
//for files > 6.78 mb we need read additional DIFAT fields
for (int i = 0; difatBlock > 0 && i < difatSectorNumbers; ++i) {
ZLLogger::Instance().println("DocPlugin", "Read additional data for DIFAT");
char buffer[mySectorSize];
myInputStream->seek(BBD_BLOCK_SIZE + difatBlock * mySectorSize, true);
if (myInputStream->read(buffer, mySectorSize) != mySectorSize) {
ZLLogger::Instance().println("DocPlugin", "Error read DIFAT!");
return false;
}
for (unsigned int j = 0; j < (mySectorSize - 4); j += 4) {
myDIFAT.push_back(OleUtil::get4Bytes(buffer, j));
}
difatBlock = OleUtil::get4Bytes(buffer, mySectorSize - 4); //next DIFAT block is pointed at the end of the sector
}
//removing unusable DIFAT links
//0xFFFFFFFF means "free section"
while (!myDIFAT.empty() && myDIFAT.back() == (int)0xFFFFFFFF) {
myDIFAT.pop_back();
}
return true;
}
bool OleStorage::readBBD(char *oleBuf) {
char buffer[mySectorSize];
unsigned int bbdNumberBlocks = OleUtil::getU4Bytes(oleBuf, 0x2c); //number of big blocks
if (myDIFAT.size() < bbdNumberBlocks) {
//TODO maybe add check on myDIFAT == bbdNumberBlocks
ZLLogger::Instance().println("DocPlugin", "Wrong number of FAT blocks value");
return false;
}
for (unsigned int i = 0; i < bbdNumberBlocks; ++i) {
int bbdSector = myDIFAT.at(i);
if (bbdSector >= (int)(myStreamSize / mySectorSize) || bbdSector < 0) {
ZLLogger::Instance().println("DocPlugin", "Bad BBD entry!");
return false;
}
myInputStream->seek(BBD_BLOCK_SIZE + bbdSector * mySectorSize, true);
if (myInputStream->read(buffer, mySectorSize) != mySectorSize) {
ZLLogger::Instance().println("DocPlugin", "Error during reading BBD!");
return false;
}
for (unsigned int j = 0; j < mySectorSize; j += 4) {
myBBD.push_back(OleUtil::get4Bytes(buffer, j));
}
}
return true;
}
bool OleStorage::readSBD(char *oleBuf) {
int sbdCur = OleUtil::get4Bytes(oleBuf, 0x3c); //address of first small sector
int sbdCount = OleUtil::get4Bytes(oleBuf, 0x40); //count of small sectors
if (sbdCur <= 0) {
ZLLogger::Instance().println("DocPlugin", "There's no SBD, don't read it");
return true;
}
char buffer[mySectorSize];
for (int i = 0; i < sbdCount; ++i) {
if (i != 0) {
if (sbdCur < 0 || (unsigned int)sbdCur >= myBBD.size()) {
ZLLogger::Instance().println("DocPlugin", "error during parsing SBD");
return false;
}
sbdCur = myBBD.at(sbdCur);
}
if (sbdCur <= 0) {
break;
}
myInputStream->seek(BBD_BLOCK_SIZE + sbdCur * mySectorSize, true);
if (myInputStream->read(buffer, mySectorSize) != mySectorSize) {
ZLLogger::Instance().println("DocPlugin", "reading error during parsing SBD");
return false;
}
for (unsigned int j = 0; j < mySectorSize; j += 4) {
mySBD.push_back(OleUtil::get4Bytes(buffer, j));
}
}
return true;
}
bool OleStorage::readProperties(char *oleBuf) {
int propCur = OleUtil::get4Bytes(oleBuf, 0x30); //offset for address of sector with first property
if (propCur < 0) {
ZLLogger::Instance().println("DocPlugin", "Wrong first directory sector location");
return false;
}
char buffer[mySectorSize];
do {
myInputStream->seek(BBD_BLOCK_SIZE + propCur * mySectorSize, true);
if (myInputStream->read(buffer, mySectorSize) != mySectorSize) {
ZLLogger::Instance().println("DocPlugin", "Error during reading properties");
return false;
}
for (unsigned int j = 0; j < mySectorSize; j += 128) {
myProperties.push_back(std::string(buffer + j, 128));
}
if (propCur < 0 || (std::size_t)propCur >= myBBD.size()) {
break;
}
propCur = myBBD.at(propCur);
} while (propCur >= 0 && propCur < (int)(myStreamSize / mySectorSize));
return true;
}
bool OleStorage::readAllEntries() {
int propCount = myProperties.size();
for (int i = 0; i < propCount; ++i) {
OleEntry entry;
bool result = readOleEntry(i, entry);
if (!result) {
break;
}
if (entry.type == OleEntry::ROOT_DIR) {
myRootEntryIndex = i;
}
myEntries.push_back(entry);
}
if (myRootEntryIndex < 0) {
return false;
}
return true;
}
bool OleStorage::readOleEntry(int propNumber, OleEntry &e) {
static const std::string ROOT_ENTRY = "Root Entry";
std::string property = myProperties.at(propNumber);
char oleType = property.at(0x42); //offset for Ole Type
if (oleType != 1 && oleType != 2 && oleType != 3 && oleType != 5) {
ZLLogger::Instance().println("DocPlugin", "entry -- not right ole type");
return false;
}
e.type = (OleEntry::Type)oleType;
int nameLength = OleUtil::getU2Bytes(property.c_str(), 0x40); //offset for value entry's name length
e.name.clear();
e.name.reserve(33); //max size of entry name
if ((unsigned int)nameLength >= property.size()) {
return false;
}
for (int i = 0; i < nameLength; i+=2) {
char c = property.at(i);
if (c != 0) {
e.name += c;
}
}
e.length = OleUtil::getU4Bytes(property.c_str(), 0x78); //offset for entry's length value
e.isBigBlock = e.length >= 0x1000 || e.name == ROOT_ENTRY;
// Read sector chain
if (property.size() < 0x74 + 4) {
ZLLogger::Instance().println("DocPlugin", "problems with reading ole entry");
return false;
}
int chainCur = OleUtil::get4Bytes(property.c_str(), 0x74); //offset for start block of entry
if (chainCur >= 0 && (chainCur <= (int)(myStreamSize / (e.isBigBlock ? mySectorSize : myShortSectorSize)))) {
//filling blocks with chains
do {
e.blocks.push_back((unsigned int)chainCur);
if (e.isBigBlock && (std::size_t)chainCur < myBBD.size()) {
chainCur = myBBD.at(chainCur);
} else if (!mySBD.empty() && (std::size_t)chainCur < mySBD.size()) {
chainCur = mySBD.at(chainCur);
} else {
chainCur = -1;
}
} while (chainCur > 0 &&
chainCur < (int)(e.isBigBlock ? myBBD.size() : mySBD.size()) &&
e.blocks.size() <= e.length / (e.isBigBlock ? mySectorSize : myShortSectorSize));
}
e.length = std::min(e.length, (unsigned int)((e.isBigBlock ? mySectorSize : myShortSectorSize) * e.blocks.size()));
return true;
}
bool OleStorage::countFileOffsetOfBlock(const OleEntry &e, unsigned int blockNumber, unsigned int &result) const {
//TODO maybe better syntax can be used?
if (e.blocks.size() <= (std::size_t)blockNumber) {
ZLLogger::Instance().println("DocPlugin", "countFileOffsetOfBlock can't be done, blockNumber is invalid");
return false;
}
if (e.isBigBlock) {
result = BBD_BLOCK_SIZE + e.blocks.at(blockNumber) * mySectorSize;
} else {
unsigned int sbdPerSector = mySectorSize / myShortSectorSize;
unsigned int sbdSectorNumber = e.blocks.at(blockNumber) / sbdPerSector;
unsigned int sbdSectorMod = e.blocks.at(blockNumber) % sbdPerSector;
if (myEntries.at(myRootEntryIndex).blocks.size() <= (std::size_t)sbdSectorNumber) {
ZLLogger::Instance().println("DocPlugin", "countFileOffsetOfBlock can't be done, invalid sbd data");
return false;
}
result = BBD_BLOCK_SIZE + myEntries.at(myRootEntryIndex).blocks.at(sbdSectorNumber) * mySectorSize + sbdSectorMod * myShortSectorSize;
}
return true;
}
bool OleStorage::getEntryByName(std::string name, OleEntry &returnEntry) const {
//TODO fix the workaround for duplicates streams: now it takes a stream with max length
unsigned int maxLength = 0;
for (std::size_t i = 0; i < myEntries.size(); ++i) {
const OleEntry &entry = myEntries.at(i);
if (entry.name == name && entry.length >= maxLength) {
returnEntry = entry;
maxLength = entry.length;
}
}
return maxLength > 0;
}
|