-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhdrftrlist.c
371 lines (337 loc) · 9.1 KB
/
hdrftrlist.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
/*
* hdrftrlist.c
* Copyright (C) 2004,2005 A.J. van Os; Released under GNU GPL
*
* Description:
* Build, read and destroy list(s) of Word Header/footer information
*/
#include <string.h>
#include "antiword.h"
#define HDR_EVEN_PAGES 0
#define HDR_ODD_PAGES 1
#define FTR_EVEN_PAGES 2
#define FTR_ODD_PAGES 3
#define HDR_FIRST_PAGE 4
#define FTR_FIRST_PAGE 5
/*
* Private structures to hide the way the information
* is stored from the rest of the program
*/
typedef struct hdrftr_local_tag {
hdrftr_block_type tInfo;
ULONG ulCharPosStart;
ULONG ulCharPosNext;
BOOL bUseful;
BOOL bTextOriginal;
} hdrftr_local_type;
typedef struct hdrftr_mem_tag {
hdrftr_local_type atElement[6];
} hdrftr_mem_type;
/* Variables needed to write the Header/footer Information List */
static hdrftr_mem_type *pHdrFtrList = NULL;
static size_t tHdrFtrLen = 0;
/*
* vDestroyHdrFtrInfoList - destroy the Header/footer Information List
*/
void
vDestroyHdrFtrInfoList(void)
{
hdrftr_mem_type *pRecord;
output_type *pCurr, *pNext;
size_t tHdrFtr, tIndex;
DBG_MSG("vDestroyHdrFtrInfoList");
/* Free the Header/footer Information List */
for (tHdrFtr = 0; tHdrFtr < tHdrFtrLen; tHdrFtr++) {
pRecord = pHdrFtrList + tHdrFtr;
for (tIndex = 0;
tIndex < elementsof(pRecord->atElement);
tIndex++) {
if (!pRecord->atElement[tIndex].bTextOriginal) {
continue;
}
pCurr = pRecord->atElement[tIndex].tInfo.pText;
while (pCurr != NULL) {
pCurr->szStorage = xfree(pCurr->szStorage);
pNext = pCurr->pNext;
pCurr = xfree(pCurr);
pCurr = pNext;
}
}
}
pHdrFtrList = xfree(pHdrFtrList);
/* Reset all control variables */
tHdrFtrLen = 0;
} /* end of vDestroyHdrFtrInfoList */
/*
* vCreat8HdrFtrInfoList - Create the Header/footer Information List
*/
void
vCreat8HdrFtrInfoList(const ULONG *aulCharPos, size_t tLength)
{
hdrftr_mem_type *pListMember;
size_t tHdrFtr, tIndex, tMainIndex;
fail(aulCharPos == NULL);
DBG_DEC(tLength);
if (tLength <= 1) {
return;
}
tHdrFtrLen = tLength / 12;
if (tLength % 12 != 0 && tLength % 12 != 1) {
tHdrFtrLen++;
}
DBG_DEC(tHdrFtrLen);
pHdrFtrList = xcalloc(tHdrFtrLen, sizeof(hdrftr_mem_type));
for (tHdrFtr = 0; tHdrFtr < tHdrFtrLen; tHdrFtr++) {
pListMember = pHdrFtrList + tHdrFtr;
for (tIndex = 0, tMainIndex = tHdrFtr * 12;
tIndex < 6 && tMainIndex < tLength;
tIndex++, tMainIndex++) {
pListMember->atElement[tIndex].tInfo.pText = NULL;
pListMember->atElement[tIndex].ulCharPosStart =
aulCharPos[tMainIndex];
if (tMainIndex + 1 < tLength) {
pListMember->atElement[tIndex].ulCharPosNext =
aulCharPos[tMainIndex + 1];
} else {
pListMember->atElement[tIndex].ulCharPosNext =
aulCharPos[tMainIndex];
}
}
}
} /* end of vCreat8HdrFtrInfoList */
/*
* vCreat6HdrFtrInfoList - Create the Header/footer Information List
*/
void
vCreat6HdrFtrInfoList(const ULONG *aulCharPos, size_t tLength)
{
static const size_t atIndex[] =
{ SIZE_T_MAX, SIZE_T_MAX, FTR_FIRST_PAGE, HDR_FIRST_PAGE,
FTR_ODD_PAGES, FTR_EVEN_PAGES, HDR_ODD_PAGES, HDR_EVEN_PAGES,
};
hdrftr_mem_type *pListMember;
size_t tHdrFtr, tTmp, tIndex, tMainIndex, tBit;
UCHAR ucDopSpecification, ucSepSpecification;
fail(aulCharPos == NULL);
DBG_DEC(tLength);
if (tLength <= 1) {
return;
}
tHdrFtrLen = tGetNumberOfSections();
if (tHdrFtrLen == 0) {
tHdrFtrLen = 1;
}
DBG_DEC(tHdrFtrLen);
pHdrFtrList = xcalloc(tHdrFtrLen, sizeof(hdrftr_mem_type));
/* Get the start index in aulCharPos */
ucDopSpecification = ucGetDopHdrFtrSpecification();
DBG_HEX(ucDopSpecification & 0xe0);
tMainIndex = 0;
for (tBit = 7; tBit >= 5; tBit--) {
if ((ucDopSpecification & BIT(tBit)) != 0) {
tMainIndex++;
}
}
DBG_DEC(tMainIndex);
for (tHdrFtr = 0; tHdrFtr < tHdrFtrLen; tHdrFtr++) {
ucSepSpecification = ucGetSepHdrFtrSpecification(tHdrFtr);
DBG_HEX(ucSepSpecification & 0xfc);
pListMember = pHdrFtrList + tHdrFtr;
for (tTmp = 0;
tTmp < elementsof(pListMember->atElement);
tTmp++) {
pListMember->atElement[tTmp].tInfo.pText = NULL;
}
for (tBit = 7; tBit >= 2; tBit--) {
if (tMainIndex >= tLength) {
break;
}
if ((ucSepSpecification & BIT(tBit)) == 0) {
continue;
}
tIndex = atIndex[tBit];
fail(tIndex >= 6);
pListMember->atElement[tIndex].ulCharPosStart =
aulCharPos[tMainIndex];
if (tMainIndex + 1 < tLength) {
pListMember->atElement[tIndex].ulCharPosNext =
aulCharPos[tMainIndex + 1];
} else {
pListMember->atElement[tIndex].ulCharPosNext =
aulCharPos[tMainIndex];
}
tMainIndex++;
}
}
} /* end of vCreat6HdrFtrInfoList */
/*
* vCreat2HdrFtrInfoList - Create the Header/footer Information List
*/
void
vCreat2HdrFtrInfoList(const ULONG *aulCharPos, size_t tLength)
{
vCreat6HdrFtrInfoList(aulCharPos, tLength);
} /* end of vCreat2HdrFtrInfoList */
/*
* pGetHdrFtrInfo - get the Header/footer information
*/
const hdrftr_block_type *
pGetHdrFtrInfo(int iSectionIndex,
BOOL bWantHeader, BOOL bOddPage, BOOL bFirstInSection)
{
hdrftr_mem_type *pCurr;
fail(iSectionIndex < 0);
fail(pHdrFtrList == NULL && tHdrFtrLen != 0);
if (pHdrFtrList == NULL || tHdrFtrLen == 0) {
/* No information */
return NULL;
}
if (iSectionIndex < 0) {
iSectionIndex = 0;
} else if (iSectionIndex >= (int)tHdrFtrLen) {
iSectionIndex = (int)(tHdrFtrLen - 1);
}
pCurr = pHdrFtrList + iSectionIndex;
if (bFirstInSection) {
if (bWantHeader) {
return &pCurr->atElement[HDR_FIRST_PAGE].tInfo;
} else {
return &pCurr->atElement[FTR_FIRST_PAGE].tInfo;
}
} else {
if (bWantHeader) {
if (bOddPage) {
return &pCurr->atElement[HDR_ODD_PAGES].tInfo;
} else {
return &pCurr->atElement[HDR_EVEN_PAGES].tInfo;
}
} else {
if (bOddPage) {
return &pCurr->atElement[FTR_ODD_PAGES].tInfo;
} else {
return &pCurr->atElement[FTR_EVEN_PAGES].tInfo;
}
}
}
} /* end of pGetHdrFtrInfo */
/*
* lComputeHdrFtrHeight - compute the height of a header or footer
*
* Returns the height in DrawUnits
*/
static long
lComputeHdrFtrHeight(const output_type *pAnchor)
{
const output_type *pCurr;
long lTotal;
USHORT usFontSizeMax;
lTotal = 0;
usFontSizeMax = 0;
for (pCurr = pAnchor; pCurr != NULL; pCurr = pCurr->pNext) {
if (pCurr->tNextFree == 1) {
if (pCurr->szStorage[0] == PAR_END) {
/* End of a paragraph */
lTotal += lComputeLeading(usFontSizeMax);
lTotal += lMilliPoints2DrawUnits(
(long)pCurr->usFontSize * 200);
usFontSizeMax = 0;
continue;
}
if (pCurr->szStorage[0] == HARD_RETURN) {
/* End of a line */
lTotal += lComputeLeading(usFontSizeMax);
usFontSizeMax = 0;
continue;
}
}
if (pCurr->usFontSize > usFontSizeMax) {
usFontSizeMax = pCurr->usFontSize;
}
}
if (usFontSizeMax != 0) {
/* Height of the last paragraph */
lTotal += lComputeLeading(usFontSizeMax);
}
return lTotal;
} /* end of lComputeHdrFtrHeight */
/*
* vPrepareHdrFtrText - prepare the header/footer text
*/
void
vPrepareHdrFtrText(FILE *pFile)
{
hdrftr_mem_type *pCurr, *pPrev;
hdrftr_local_type *pTmp;
output_type *pText;
size_t tHdrFtr, tIndex;
fail(pFile == NULL);
fail(pHdrFtrList == NULL && tHdrFtrLen != 0);
if (pHdrFtrList == NULL || tHdrFtrLen == 0) {
/* No information */
return;
}
/* Fill text, text height and useful-ness */
for (tHdrFtr = 0; tHdrFtr < tHdrFtrLen; tHdrFtr++) {
pCurr = pHdrFtrList + tHdrFtr;
for (tIndex = 0;
tIndex < elementsof(pHdrFtrList->atElement);
tIndex++) {
pTmp = &pCurr->atElement[tIndex];
pTmp->bUseful =
pTmp->ulCharPosStart != pTmp->ulCharPosNext;
if (pTmp->bUseful) {
pText = pHdrFtrDecryptor(pFile,
pTmp->ulCharPosStart,
pTmp->ulCharPosNext);
pTmp->tInfo.pText = pText;
pTmp->tInfo.lHeight =
lComputeHdrFtrHeight(pText);
pTmp->bTextOriginal = pText != NULL;
} else {
pTmp->tInfo.pText = NULL;
pTmp->tInfo.lHeight = 0;
pTmp->bTextOriginal = FALSE;
}
}
}
/* Replace not-useful records by using inheritance */
if (pHdrFtrList->atElement[HDR_FIRST_PAGE].bUseful) {
pTmp = &pHdrFtrList->atElement[HDR_ODD_PAGES];
if (!pTmp->bUseful) {
*pTmp = pHdrFtrList->atElement[HDR_FIRST_PAGE];
pTmp->bTextOriginal = FALSE;
}
pTmp = &pHdrFtrList->atElement[HDR_EVEN_PAGES];
if (!pTmp->bUseful) {
*pTmp = pHdrFtrList->atElement[HDR_FIRST_PAGE];
pTmp->bTextOriginal = FALSE;
}
}
if (pHdrFtrList->atElement[FTR_FIRST_PAGE].bUseful) {
pTmp = &pHdrFtrList->atElement[FTR_ODD_PAGES];
if (!pTmp->bUseful) {
*pTmp = pHdrFtrList->atElement[FTR_FIRST_PAGE];
pTmp->bTextOriginal = FALSE;
}
pTmp = &pHdrFtrList->atElement[FTR_EVEN_PAGES];
if (!pTmp->bUseful) {
*pTmp = pHdrFtrList->atElement[FTR_FIRST_PAGE];
pTmp->bTextOriginal = FALSE;
}
}
for (tHdrFtr = 1, pCurr = &pHdrFtrList[1];
tHdrFtr < tHdrFtrLen;
tHdrFtr++, pCurr++) {
pPrev = pCurr - 1;
for (tIndex = 0;
tIndex < elementsof(pHdrFtrList->atElement);
tIndex++) {
if (!pCurr->atElement[tIndex].bUseful &&
pPrev->atElement[tIndex].bUseful) {
pCurr->atElement[tIndex] =
pPrev->atElement[tIndex];
pCurr->atElement[tIndex].bTextOriginal = FALSE;
}
}
}
} /* end of vPrepareHdrFtrText */