-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathOLETemplate.bt
212 lines (191 loc) · 6 KB
/
OLETemplate.bt
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
//---------------------------------------------------------------------------
/*
010 Editor Template for [MS-CFB] Compound File Binary Format file format
2014/10/18 v0.0.1
Source code put in public domain by Didier Stevens, no Copyright
https://DidierStevens.com
Use at your own risk
History:
2013/10/03: start development with 010 Editor v5.0
2014/01/07: continued
2014/01/10: continued
2014/10/18: added mini streams
Todo:
*/
//---------------------------------------------------------------------------
typedef union
{
wstring Wstring;
WORD Characters[32];
} USTRING32;
wstring USTRING32ToWstring(USTRING32 &arg)
{
return arg.Wstring;
}
typedef struct
{
BYTE _abSig[8];
BYTE _clsid[16];
USHORT _uMinorVersion; // [18H,02] minor version of the format: 33 is
// written by reference implementation
USHORT _uDllVersion; // [1AH,02] major version of the dll/format: 3 for
// 512-byte sectors, 4 for 4 KB sectors
USHORT _uByteOrder; // [1CH,02] 0xFFFE: indicates Intel byte-ordering
USHORT _uSectorShift; // [1EH,02] size of sectors in power-of-two;
// typically 9 indicating 512-byte sectors
USHORT _uMiniSectorShift; // [20H,02] size of mini-sectors in power-of-two;
// typically 6 indicating 64-byte mini-sectors
USHORT _usReserved; // [22H,02] reserved, must be zero
ULONG _ulReserved1; // [24H,04] reserved, must be zero
ULONG _csectDir; // [28H,04] must be zero for 512-byte sectors,
// number of SECTs in directory chain for 4 KB
// sectors
ULONG _csectFat; // [2CH,04] number of SECTs in the FAT chain
ULONG _sectDirStart; // [30H,04] first SECT in the directory chain
ULONG _signature; // [34H,04] signature used for transactions; must
// be zero. The reference implementation
// does not support transactions
ULONG _ulMiniSectorCutoff; // [38H,04] maximum size for a mini stream;
// typically 4096 bytes
ULONG _sectMiniFatStart; // [3CH,04] first SECT in the MiniFAT chain
ULONG _csectMiniFat; // [40H,04] number of SECTs in the MiniFAT chain
ULONG _sectDifStart; // [44H,04] first SECT in the DIFAT chain
ULONG _csectDif; // [48H,04] number of SECTs in the DIFAT chain
ULONG _sectFat[109]; // [4CH,436] the SECTs of first 109 FAT sectors
} CFHEAD;
typedef struct
{
USTRING32 entryName <read=USTRING32ToWstring>;
USHORT entryNameLength;
UBYTE objectType;
UBYTE colorFlag;
ULONG leftSiblingID;
ULONG rightSiblingID;
ULONG childID;
UBYTE clsid[16];
ULONG stateBits;
FILETIME creationTime;
FILETIME modifiedTime;
ULONG startingSectorLocation;
UINT64 streamSize;
} DIRENTRY;
wstring DIRENTRYToString(DIRENTRY &direntry)
{
if (direntry.objectType == 0)
return "<UNALLOCATED>";
else
return direntry.entryName.Wstring;
}
void FSeekSector(ULONG sectorID)
{
if (sectorID >= 0)
FSeek((sectorID + 1) * sectorsize);
}
typedef struct(int size)
{
UBYTE data[size];
} STREAMDATA;
void GenerateStream(DIRENTRY &direntry, int sectorsize)
{
local int64 i64SavePosition;
local int iIter;
local UINT64 streamSize;
local UINT64 blockSize;
i64SavePosition = FTell();
iIter = direntry.startingSectorLocation;
FSeekSector(iIter);
streamSize = 0;
blockSize = Min(sectorsize, direntry.streamSize);
Printf("blockSize = %d\n", blockSize);
while (iIter >= 0 && streamSize <= direntry.streamSize)
{
Printf("iIter = %d\n", iIter);
Printf("FAT[iIter] = %d\n", FAT[iIter]);
if (iIter + 1 == FAT[iIter])
{
blockSize += Min(sectorsize, direntry.streamSize - streamSize);
iIter = FAT[iIter];
}
else
{
// STREAMDATA streamdata(Min(blockSize, direntry.streamSize - streamSize));
STREAMDATA streamdata(blockSize);
iIter = FAT[iIter];
FSeekSector(iIter);
blockSize = Min(sectorsize, direntry.streamSize - streamSize);
}
streamSize += sectorsize;
}
FSeek(i64SavePosition);
}
void GenerateMiniStream(LONG lSectorMinistream, DIRENTRY &direntry, int minisectorsize)
{
local int64 i64SavePosition;
local int iIter;
local UINT64 streamSize;
local UINT64 blockSize;
i64SavePosition = FTell();
iIter = direntry.startingSectorLocation;
FSeekSector(lSectorMinistream + iIter);
streamSize = 0;
blockSize = Min(minisectorsize, direntry.streamSize);
Printf("blockSize = %d\n", blockSize);
while (iIter >= 0 && streamSize <= direntry.streamSize)
{
Printf("iIter = %d\n", iIter);
Printf("FAT[iIter] = %d\n", FAT[iIter]);
if (iIter + 1 == FAT[iIter])
{
blockSize += Min(minisectorsize, direntry.streamSize - streamSize);
iIter = FAT[iIter];
}
else
{
// STREAMDATA streamdata(Min(blockSize, direntry.streamSize - streamSize));
STREAMDATA ministreamdata(blockSize);
iIter = FAT[iIter];
FSeekSector(iIter);
blockSize = Min(minisectorsize, direntry.streamSize - streamSize);
}
streamSize += minisectorsize;
}
FSeek(i64SavePosition);
}
// Local variables
local int sectorsize;
local int minisectorsize;
local LONG dirIter;
local int iIter;
local int iCountDirEntries;
local LONG lSectorMinistream;
// Main
LittleEndian();
CFHEAD header;
sectorsize = Pow(2, header._uSectorShift);
minisectorsize = Pow(2, header._uMiniSectorShift);
FSeekSector(header._sectFat[0]);
LONG FAT[sectorsize / 4];
dirIter = header._sectDirStart;
iCountDirEntries = 0;
lSectorMinistream = -1;
while (dirIter >= 0)
{
FSeekSector(dirIter);
for (iIter = 0; iIter < sectorsize / 128; iIter++)
{
DIRENTRY direntry <read=DIRENTRYToString>;
if (direntry[iCountDirEntries].objectType == 5)
{
lSectorMinistream = direntry[iCountDirEntries].startingSectorLocation;
}
if (direntry[iCountDirEntries].objectType == 2)
{
if (lSectorMinistream < 0)
GenerateStream(direntry[iCountDirEntries], sectorsize);
else
GenerateMiniStream(lSectorMinistream, direntry[iCountDirEntries], sectorsize);
}
iCountDirEntries++;
}
dirIter = FAT[dirIter];
}