-
Notifications
You must be signed in to change notification settings - Fork 4
/
prop0.c
489 lines (457 loc) · 12.3 KB
/
prop0.c
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* prop0.c
* Copyright (C) 2002-2004 A.J. van Os; Released under GNU GPL
*
* Description:
* Read the property information from a Word for DOS file
*/
#include <string.h>
#include <time.h>
#include "antiword.h"
/*
* tConvertDosDate - convert DOS date format
*
* returns Unix time_t or -1
*/
static time_t
tConvertDosDate(const char *szDosDate)
{
struct tm tTime;
const char *pcTmp;
time_t tResult;
memset(&tTime, 0, sizeof(tTime));
pcTmp = szDosDate;
/* Get the month */
if (!isdigit(*pcTmp)) {
return (time_t)-1;
}
tTime.tm_mon = (int)(*pcTmp - '0');
pcTmp++;
if (isdigit(*pcTmp)) {
tTime.tm_mon *= 10;
tTime.tm_mon += (int)(*pcTmp - '0');
pcTmp++;
}
/* Get the first separater */
if (isalnum(*pcTmp)) {
return (time_t)-1;
}
pcTmp++;
/* Get the day */
if (!isdigit(*pcTmp)) {
return (time_t)-1;
}
tTime.tm_mday = (int)(*pcTmp - '0');
pcTmp++;
if (isdigit(*pcTmp)) {
tTime.tm_mday *= 10;
tTime.tm_mday += (int)(*pcTmp - '0');
pcTmp++;
}
/* Get the second separater */
if (isalnum(*pcTmp)) {
return (time_t)-1;
}
pcTmp++;
/* Get the year */
if (!isdigit(*pcTmp)) {
return (time_t)-1;
}
tTime.tm_year = (int)(*pcTmp - '0');
pcTmp++;
if (isdigit(*pcTmp)) {
tTime.tm_year *= 10;
tTime.tm_year += (int)(*pcTmp - '0');
pcTmp++;
}
/* Check the values */
if (tTime.tm_mon == 0 || tTime.tm_mday == 0 || tTime.tm_mday > 31) {
return (time_t)-1;
}
/* Correct the values */
tTime.tm_mon--; /* From 01-12 to 00-11 */
if (tTime.tm_year < 80) {
tTime.tm_year += 100; /* 00 means 2000 is 100 */
}
tTime.tm_isdst = -1;
tResult = mktime(&tTime);
NO_DBG_MSG(ctime(&tResult));
return tResult;
} /* end of tConvertDosDate */
/*
* Build the lists with Document Property Information for Word for DOS files
*/
void
vGet0DopInfo(FILE *pFile, const UCHAR *aucHeader)
{
document_block_type tDocument;
UCHAR *aucBuffer;
ULONG ulBeginSumdInfo, ulBeginNextBlock;
size_t tLen;
USHORT usOffset;
tDocument.ucHdrFtrSpecification = 0;
tDocument.usDefaultTabWidth = usGetWord(0x70, aucHeader); /* dxaTab */
tDocument.tCreateDate = (time_t)-1;
tDocument.tRevisedDate = (time_t)-1;
ulBeginSumdInfo = 128 * (ULONG)usGetWord(0x1c, aucHeader);
DBG_HEX(ulBeginSumdInfo);
ulBeginNextBlock = 128 * (ULONG)usGetWord(0x6a, aucHeader);
DBG_HEX(ulBeginNextBlock);
if (ulBeginSumdInfo < ulBeginNextBlock && ulBeginNextBlock != 0) {
/* There is a summary information block */
tLen = (size_t)(ulBeginNextBlock - ulBeginSumdInfo);
aucBuffer = xmalloc(tLen);
/* Read the summary information block */
if (bReadBytes(aucBuffer, tLen, ulBeginSumdInfo, pFile)) {
usOffset = usGetWord(12, aucBuffer);
if (aucBuffer[usOffset] != 0) {
NO_DBG_STRN(aucBuffer + usOffset, 8);
tDocument.tRevisedDate =
tConvertDosDate((char *)aucBuffer + usOffset);
}
usOffset = usGetWord(14, aucBuffer);
if (aucBuffer[usOffset] != 0) {
NO_DBG_STRN(aucBuffer + usOffset, 8);
tDocument.tCreateDate =
tConvertDosDate((char *)aucBuffer + usOffset);
}
}
aucBuffer = xfree(aucBuffer);
}
vCreateDocumentInfoList(&tDocument);
} /* end of vGet0DopInfo */
/*
* Fill the section information block with information
* from a Word for DOS file.
*/
static void
vGet0SectionInfo(const UCHAR *aucGrpprl, size_t tBytes,
section_block_type *pSection)
{
USHORT usCcol;
UCHAR ucTmp;
fail(aucGrpprl == NULL || pSection == NULL);
if (tBytes < 2) {
return;
}
/* bkc */
ucTmp = ucGetByte(1, aucGrpprl);
DBG_HEX(ucTmp);
ucTmp &= 0x07;
DBG_HEX(ucTmp);
pSection->bNewPage = ucTmp != 0 && ucTmp != 1;
if (tBytes < 18) {
return;
}
/* ccolM1 */
usCcol = (USHORT)ucGetByte(17, aucGrpprl);
DBG_DEC(usCcol);
} /* end of vGet0SectionInfo */
/*
* Build the lists with Section Property Information for Word for DOS files
*/
void
vGet0SepInfo(FILE *pFile, const UCHAR *aucHeader)
{
section_block_type tSection;
UCHAR *aucBuffer;
ULONG ulBeginOfText, ulTextOffset, ulBeginSectInfo;
ULONG ulCharPos, ulSectPage, ulBeginNextBlock;
size_t tSectInfoLen, tIndex, tSections, tBytes;
UCHAR aucTmp[2], aucFpage[35];
fail(pFile == NULL || aucHeader == NULL);
ulBeginOfText = 128;
NO_DBG_HEX(ulBeginOfText);
ulBeginSectInfo = 128 * (ULONG)usGetWord(0x18, aucHeader);
DBG_HEX(ulBeginSectInfo);
ulBeginNextBlock = 128 * (ULONG)usGetWord(0x1a, aucHeader);
DBG_HEX(ulBeginNextBlock);
if (ulBeginSectInfo == ulBeginNextBlock) {
/* There is no section information block */
return;
}
/* Get the the number of sections */
if (!bReadBytes(aucTmp, 2, ulBeginSectInfo, pFile)) {
return;
}
tSections = (size_t)usGetWord(0, aucTmp);
NO_DBG_DEC(tSections);
/* Read the Section Descriptors */
tSectInfoLen = 10 * tSections;
NO_DBG_DEC(tSectInfoLen);
aucBuffer = xmalloc(tSectInfoLen);
if (!bReadBytes(aucBuffer, tSectInfoLen, ulBeginSectInfo + 4, pFile)) {
aucBuffer = xfree(aucBuffer);
return;
}
NO_DBG_PRINT_BLOCK(aucBuffer, tSectInfoLen);
/* Read the Section Properties */
for (tIndex = 0; tIndex < tSections; tIndex++) {
ulTextOffset = ulGetLong(10 * tIndex, aucBuffer);
NO_DBG_HEX(ulTextOffset);
ulCharPos = ulBeginOfText + ulTextOffset;
NO_DBG_HEX(ulTextOffset);
ulSectPage = ulGetLong(10 * tIndex + 6, aucBuffer);
NO_DBG_HEX(ulSectPage);
if (ulSectPage == FC_INVALID || /* Must use defaults */
ulSectPage < 128 || /* Should not happen */
ulSectPage >= ulBeginSectInfo) { /* Should not happen */
DBG_HEX_C(ulSectPage != FC_INVALID, ulSectPage);
vDefault2SectionInfoList(ulCharPos);
continue;
}
/* Get the number of bytes to read */
if (!bReadBytes(aucTmp, 1, ulSectPage, pFile)) {
continue;
}
tBytes = 1 + (size_t)ucGetByte(0, aucTmp);
NO_DBG_DEC(tBytes);
if (tBytes > sizeof(aucFpage)) {
DBG_DEC(tBytes);
tBytes = sizeof(aucFpage);
}
/* Read the bytes */
if (!bReadBytes(aucFpage, tBytes, ulSectPage, pFile)) {
continue;
}
NO_DBG_PRINT_BLOCK(aucFpage, tBytes);
/* Process the bytes */
vGetDefaultSection(&tSection);
vGet0SectionInfo(aucFpage + 1, tBytes - 1, &tSection);
vAdd2SectionInfoList(&tSection, ulCharPos);
}
/* Clean up before you leave */
aucBuffer = xfree(aucBuffer);
} /* end of vGet0SepInfo */
/*
* Fill the style information block with information
* from a Word for DOS file.
*/
static void
vGet0StyleInfo(int iFodo, const UCHAR *aucGrpprl, style_block_type *pStyle)
{
int iBytes;
UCHAR ucTmp;
fail(iFodo <= 0 || aucGrpprl == NULL || pStyle == NULL);
pStyle->usIstdNext = ISTD_NORMAL;
iBytes = (int)ucGetByte(iFodo, aucGrpprl);
if (iBytes < 1) {
return;
}
/* stc if styled */
ucTmp = ucGetByte(iFodo + 1, aucGrpprl);
if ((ucTmp & BIT(0)) != 0) {
ucTmp >>= 1;
if (ucTmp >= 88 && ucTmp <= 94) {
/* Header levels 1 through 7 */
pStyle->usIstd = ucTmp - 87;
pStyle->ucNumLevel = 1;
}
}
if (iBytes < 2) {
return;
}
/* jc */
ucTmp = ucGetByte(iFodo + 2, aucGrpprl);
pStyle->ucAlignment = ucTmp & 0x02;
if (iBytes < 3) {
return;
}
/* stc */
ucTmp = ucGetByte(iFodo + 3, aucGrpprl);
ucTmp &= 0x7f;
if (ucTmp >= 88 && ucTmp <= 94) {
/* Header levels 1 through 7 */
pStyle->usIstd = ucTmp - 87;
pStyle->ucNumLevel = 1;
}
if (iBytes < 6) {
return;
}
/* dxaRight */
pStyle->sRightIndent = (short)usGetWord(iFodo + 5, aucGrpprl);
NO_DBG_DEC(pStyle->sRightIndent);
if (iBytes < 8) {
return;
}
/* dxaLeft */
pStyle->sLeftIndent = (short)usGetWord(iFodo + 7, aucGrpprl);
NO_DBG_DEC(pStyle->sLeftIndent);
if (iBytes < 10) {
return;
}
/* dxaLeft1 */
pStyle->sLeftIndent1 = (short)usGetWord(iFodo + 9, aucGrpprl);
NO_DBG_DEC(pStyle->sLeftIndent1);
if (iBytes < 14) {
return;
}
/* dyaBefore */
pStyle->usBeforeIndent = usGetWord(iFodo + 13, aucGrpprl);
NO_DBG_DEC(pStyle->usBeforeIndent);
if (iBytes < 16) {
return;
}
/* dyaAfter */
pStyle->usAfterIndent = usGetWord(iFodo + 15, aucGrpprl);
NO_DBG_DEC(pStyle->usAfterIndent);
} /* end of vGet0StyleInfo */
/*
* Build the lists with Paragraph Information for Word for DOS files
*/
void
vGet0PapInfo(FILE *pFile, const UCHAR *aucHeader)
{
style_block_type tStyle;
ULONG ulBeginParfInfo, ulCharPos, ulCharPosNext;
int iIndex, iRun, iFodo;
UCHAR aucFpage[128];
fail(pFile == NULL || aucHeader == NULL);
ulBeginParfInfo = 128 * (ULONG)usGetWord(0x12, aucHeader);
NO_DBG_HEX(ulBeginParfInfo);
do {
if (!bReadBytes(aucFpage, 128, ulBeginParfInfo, pFile)) {
return;
}
NO_DBG_PRINT_BLOCK(aucFpage, 128);
ulCharPosNext = ulGetLong(0, aucFpage);
iRun = (int)ucGetByte(0x7f, aucFpage);
NO_DBG_DEC(iRun);
for (iIndex = 0; iIndex < iRun; iIndex++) {
iFodo = (int)usGetWord(6 * iIndex + 8, aucFpage);
if (iFodo <= 0 || iFodo > 0x79) {
DBG_DEC_C(iFodo != (int)0xffff, iFodo);
continue;
}
vFillStyleFromStylesheet(0, &tStyle);
vGet0StyleInfo(iFodo, aucFpage + 4, &tStyle);
ulCharPos = ulCharPosNext;
ulCharPosNext = ulGetLong(6 * iIndex + 4, aucFpage);
tStyle.ulFileOffset = ulCharPos;
vAdd2StyleInfoList(&tStyle);
}
ulBeginParfInfo += 128;
} while (ulCharPosNext == ulBeginParfInfo);
} /* end of vGet0PapInfo */
/*
* Fill the font information block with information
* from a Word for DOS file.
*/
static void
vGet0FontInfo(int iFodo, const UCHAR *aucGrpprl, font_block_type *pFont)
{
int iBytes;
UCHAR ucTmp;
fail(iFodo <= 0 || aucGrpprl == NULL || pFont == NULL);
iBytes = (int)ucGetByte(iFodo, aucGrpprl);
if (iBytes < 2) {
return;
}
/* fBold, fItalic, cFtc */
ucTmp = ucGetByte(iFodo + 2, aucGrpprl);
if ((ucTmp & BIT(0)) != 0) {
pFont->usFontStyle |= FONT_BOLD;
}
if ((ucTmp & BIT(1)) != 0) {
pFont->usFontStyle |= FONT_ITALIC;
}
pFont->ucFontNumber = ucTmp >> 2;
NO_DBG_DEC(pFont->ucFontNumber);
if (iBytes < 3) {
return;
}
/* cHps */
pFont->usFontSize = (USHORT)ucGetByte(iFodo + 3, aucGrpprl);
NO_DBG_DEC(pFont->usFontSize);
if (iBytes < 4) {
return;
}
/* cKul, fStrike, fCaps, fSmallCaps, fVanish */
ucTmp = ucGetByte(iFodo + 4, aucGrpprl);
if ((ucTmp & BIT(0)) != 0 || (ucTmp & BIT(2)) != 0) {
pFont->usFontStyle |= FONT_UNDERLINE;
}
if ((ucTmp & BIT(1)) != 0) {
pFont->usFontStyle |= FONT_STRIKE;
}
if ((ucTmp & BIT(4)) != 0) {
pFont->usFontStyle |= FONT_CAPITALS;
}
if ((ucTmp & BIT(5)) != 0) {
pFont->usFontStyle |= FONT_SMALL_CAPITALS;
}
if ((ucTmp & BIT(7)) != 0) {
pFont->usFontStyle |= FONT_HIDDEN;
}
DBG_HEX(pFont->usFontStyle);
if (iBytes < 6) {
return;
}
/* cIss */
ucTmp = ucGetByte(iFodo + 6, aucGrpprl);
if (ucTmp != 0) {
if (ucTmp < 128) {
pFont->usFontStyle |= FONT_SUPERSCRIPT;
DBG_MSG("Superscript");
} else {
pFont->usFontStyle |= FONT_SUBSCRIPT;
DBG_MSG("Subscript");
}
}
if (iBytes < 7) {
return;
}
/* cIco */
ucTmp = ucGetByte(iFodo + 7, aucGrpprl);
switch (ucTmp & 0x07) {
case 0: pFont->ucFontColor = FONT_COLOR_BLACK; break;
case 1: pFont->ucFontColor = FONT_COLOR_RED; break;
case 2: pFont->ucFontColor = FONT_COLOR_GREEN; break;
case 3: pFont->ucFontColor = FONT_COLOR_BLUE; break;
case 4: pFont->ucFontColor = FONT_COLOR_CYAN; break;
case 5: pFont->ucFontColor = FONT_COLOR_MAGENTA; break;
case 6: pFont->ucFontColor = FONT_COLOR_YELLOW; break;
case 7: pFont->ucFontColor = FONT_COLOR_WHITE; break;
default:pFont->ucFontColor = FONT_COLOR_BLACK; break;
}
NO_DBG_DEC(pFont->ucFontColor);
} /* end of vGet0FontInfo */
/*
* Build the lists with Character Information for Word for DOS files
*/
void
vGet0ChrInfo(FILE *pFile, const UCHAR *aucHeader)
{
font_block_type tFont;
ULONG ulBeginCharInfo, ulCharPos, ulCharPosNext;
int iIndex, iRun, iFodo;
UCHAR aucFpage[128];
fail(pFile == NULL || aucHeader == NULL);
ulBeginCharInfo = ulGetLong(0x0e, aucHeader);
NO_DBG_HEX(ulBeginCharInfo);
ulBeginCharInfo = ROUND128(ulBeginCharInfo);
NO_DBG_HEX(ulBeginCharInfo);
do {
if (!bReadBytes(aucFpage, 128, ulBeginCharInfo, pFile)) {
return;
}
NO_DBG_PRINT_BLOCK(aucFpage, 128);
ulCharPosNext = ulGetLong(0, aucFpage);
iRun = (int)ucGetByte(0x7f, aucFpage);
NO_DBG_DEC(iRun);
for (iIndex = 0; iIndex < iRun; iIndex++) {
iFodo = (int)usGetWord(6 * iIndex + 8, aucFpage);
if (iFodo <= 0 || iFodo > 0x79) {
DBG_DEC_C(iFodo != (int)0xffff, iFodo);
continue;
}
vFillFontFromStylesheet(0, &tFont);
vGet0FontInfo(iFodo, aucFpage + 4, &tFont);
ulCharPos = ulCharPosNext;
ulCharPosNext = ulGetLong(6 * iIndex + 4, aucFpage);
tFont.ulFileOffset = ulCharPos;
vAdd2FontInfoList(&tFont);
}
ulBeginCharInfo += 128;
} while (ulCharPosNext == ulBeginCharInfo);
} /* end of vGet0ChrInfo */