File: wordlib.c

package info (click to toggle)
antiword 0.37-14
  • links: PTS
  • area: main
  • in suites: buster
  • size: 2,292 kB
  • sloc: ansic: 27,835; sh: 196; perl: 174; php: 83; makefile: 37
file content (411 lines) | stat: -rw-r--r-- 8,987 bytes parent folder | download | duplicates (3)
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
/*
 * wordlib.c
 * Copyright (C) 1998-2004 A.J. van Os; Released under GNU GPL
 *
 * Description:
 * Deal with the internals of a MS Word file
 */

#include "antiword.h"

static BOOL	bOldMacFile = FALSE;


/*
 * Common part of the file checking functions
 */
static BOOL
bCheckBytes(FILE *pFile, const UCHAR *aucBytes, size_t tBytes)
{
	int	iIndex, iChar;

	fail(pFile == NULL || aucBytes == NULL || tBytes == 0);

	rewind(pFile);

	for (iIndex = 0; iIndex < (int)tBytes; iIndex++) {
		iChar = getc(pFile);
		if (iChar == EOF || iChar != (int)aucBytes[iIndex]) {
			NO_DBG_HEX(iChar);
			NO_DBG_HEX(aucBytes[iIndex]);
			return FALSE;
		}
	}
	return TRUE;
} /* end of bCheckBytes */

/*
 * This function checks whether the given file is or is not a "Word for DOS"
 * document
 */
BOOL
bIsWordForDosFile(FILE *pFile, long lFilesize)
{
	static const UCHAR	aucBytes[] =
		{ 0x31, 0xbe, 0x00, 0x00, 0x00, 0xab };	/* Word for DOS */

	DBG_MSG("bIsWordForDosFile");

	if (pFile == NULL || lFilesize < 0) {
		DBG_MSG("No proper file given");
		return FALSE;
	}
	if (lFilesize < 128) {
		DBG_MSG("File too small to be a Word document");
		return FALSE;
	}
	return bCheckBytes(pFile, aucBytes, elementsof(aucBytes));
} /* end of bIsWordForDosFile */

/*
 * This function checks whether the given file is or is not a file with an
 * OLE envelope (That is a document made by Word 6 or later)
 */
static BOOL
bIsWordFileWithOLE(FILE *pFile, long lFilesize)
{
	static const UCHAR	aucBytes[] =
		{ 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 };
	int	iTailLen;

	if (pFile == NULL || lFilesize < 0) {
		DBG_MSG("No proper file given");
		return FALSE;
	}
	if (lFilesize < (long)BIG_BLOCK_SIZE * 3) {
		DBG_MSG("This file is too small to be a Word document");
		return FALSE;
	}

	iTailLen = (int)(lFilesize % BIG_BLOCK_SIZE);
	switch (iTailLen) {
	case 0:		/* No tail, as it should be */
		break;
	case 1:
	case 2:		/* Filesize mismatch or a buggy email program */
		if ((int)(lFilesize % 3) == iTailLen) {
			DBG_DEC(lFilesize);
			return FALSE;
		}
		/*
		 * Ignore extra bytes caused by buggy email programs.
		 * They have bugs in their base64 encoding or decoding.
		 * 3 bytes -> 4 ascii chars -> 3 bytes
		 */
		DBG_MSG("Document with extra bytes");
		break;
	default:	/* Wrong filesize for a Word document */
		DBG_DEC(lFilesize);
		DBG_DEC(iTailLen);
		return FALSE;
	}
	return bCheckBytes(pFile, aucBytes, elementsof(aucBytes));
} /* end of bIsWordFileWithOLE */

/*
 * This function checks whether the given file is or is not a RTF document
 */
BOOL
bIsRtfFile(FILE *pFile)
{
	static const UCHAR	aucBytes[] =
		{ '{', '\\', 'r', 't', 'f', '1' };

	DBG_MSG("bIsRtfFile");

	return bCheckBytes(pFile, aucBytes, elementsof(aucBytes));
} /* end of bIsRtfFile */

/*
 * This function checks whether the given file is or is not a WP document
 */
BOOL
bIsWordPerfectFile(FILE *pFile)
{
	static const UCHAR	aucBytes[] =
		{ 0xff, 'W', 'P', 'C' };

	DBG_MSG("bIsWordPerfectFile");

	return bCheckBytes(pFile, aucBytes, elementsof(aucBytes));
} /* end of bIsWordPerfectFile */

/*
 * This function checks whether the given file is or is not a ZIP file
 */
BOOL
bIsZipFile(FILE *pFile)
{
	static const UCHAR	aucBytes[] =
		{ 'P', 'K', 0x03, 0x04 };

	DBG_MSG("bIsZipFile");

	return bCheckBytes(pFile, aucBytes, elementsof(aucBytes));
} /* end of bIsZipFile */

/*
 * This function checks whether the given file is or is not a XML file
 */
BOOL
bIsXMLFile(FILE *pFile)
{
	static const UCHAR	aucBytes[] =
		{ '<', '?', 'x', 'm', 'l' };

	DBG_MSG("bIsXMLFile");

	return bCheckBytes(pFile, aucBytes, elementsof(aucBytes));
} /* end of bIsXMLFile */

/*
 * This function checks whether the given file is or is not a HTML file
 */
BOOL
bIsHTMLFile(FILE *pFile)
{
	static const UCHAR	aucBytes[2][5] = {
		{ '<', 'h', 't', 'm', 'l' },
		{ '<', 'H', 'T', 'M', 'L' },
	};
	int	iIndex;

	DBG_MSG("bIsHTMLFile");

	for (iIndex = 0; iIndex < (int)elementsof(aucBytes); iIndex++) {
		if (bCheckBytes(pFile,
				aucBytes[iIndex],
				elementsof(aucBytes[iIndex]))) {
			return TRUE;
		}
	}
	return FALSE;
} /* end of bIsHTMLFile */

/*
 * This function checks whether the given file is or is not a "Win Word 1 or 2"
 * document
 */
BOOL
bIsWinWord12File(FILE *pFile, long lFilesize)
{
	static const UCHAR	aucBytes[2][4] = {
		{ 0x9b, 0xa5, 0x21, 0x00 },	/* Win Word 1.x */
		{ 0xdb, 0xa5, 0x2d, 0x00 },	/* Win Word 2.0 */
	};
	int	iIndex;

	DBG_MSG("bIsWinWord12File");

	if (pFile == NULL || lFilesize < 0) {
		DBG_MSG("No proper file given");
		return FALSE;
	}
	if (lFilesize < 384) {
		DBG_MSG("This file is too small to be a Word document");
		return FALSE;
	}

	for (iIndex = 0; iIndex < (int)elementsof(aucBytes); iIndex++) {
		if (bCheckBytes(pFile,
				aucBytes[iIndex],
				elementsof(aucBytes[iIndex]))) {
			return TRUE;
		}
	}
	return FALSE;
} /* end of bIsWinWord12File */

/*
 * This function checks whether the given file is or is not a "Mac Word 4 or 5"
 * document
 */
BOOL
bIsMacWord45File(FILE *pFile)
{
	static const UCHAR	aucBytes[2][6] = {
		{ 0xfe, 0x37, 0x00, 0x1c, 0x00, 0x00 },	/* Mac Word 4 */
		{ 0xfe, 0x37, 0x00, 0x23, 0x00, 0x00 },	/* Mac Word 5 */
	};
	int	iIndex;

	DBG_MSG("bIsMacWord45File");

	for (iIndex = 0; iIndex < (int)elementsof(aucBytes); iIndex++) {
		if (bCheckBytes(pFile,
				aucBytes[iIndex],
				elementsof(aucBytes[iIndex]))) {
			return TRUE;
		}
	}
	return FALSE;
} /* end of bIsMacWord45File */

/*
 * iGuessVersionNumber - guess the Word version number from first few bytes
 *
 * Returns the guessed version number or -1 when no guess it possible
 */
int
iGuessVersionNumber(FILE *pFile, long lFilesize)
{
	if(bIsWordForDosFile(pFile, lFilesize)) {
		return 0;
	}
	if (bIsWinWord12File(pFile, lFilesize)) {
		return 2;
	}
	if (bIsMacWord45File(pFile)) {
		return 5;
	}
	if (bIsWordFileWithOLE(pFile, lFilesize)) {
		return 6;
	}
	return -1;
} /* end of iGuessVersionNumber */

/*
 * iGetVersionNumber - get the Word version number from the header
 *
 * Returns the version number or -1 when unknown
 */
int
iGetVersionNumber(const UCHAR *aucHeader)
{
	USHORT	usFib, usChse;

	usFib = usGetWord(0x02, aucHeader);
	if (usFib >= 0x1000) {
		/* To big: must be MacWord using Big Endian */
		DBG_HEX(usFib);
		usFib = usGetWordBE(0x02, aucHeader);
	}
	DBG_DEC(usFib);
	bOldMacFile = FALSE;
	switch (usFib) {
	case   0:
		DBG_MSG("Word for DOS");
		return 0;
	case  28:
		DBG_MSG("Word 4 for Macintosh");
		bOldMacFile = TRUE;
		return 4;
	case  33:
		DBG_MSG("Word 1.x for Windows");
		return 1;
	case  35:
		DBG_MSG("Word 5 for Macintosh");
		bOldMacFile = TRUE;
		return 5;
	case  45:
		DBG_MSG("Word 2 for Windows");
		return 2;
	case 101:
	case 102:
		DBG_MSG("Word 6 for Windows");
		return 6;
	case 103:
	case 104:
		usChse = usGetWord(0x14, aucHeader);
		DBG_DEC(usChse);
		switch (usChse) {
		case 0:
			DBG_MSG("Word 7 for Win95");
			return 7;
		case 256:
			DBG_MSG("Word 6 for Macintosh");
			bOldMacFile = TRUE;
			return 6;
		default:
			DBG_FIXME();
			if ((int)ucGetByte(0x05, aucHeader) == 0xe0) {
				DBG_MSG("Word 7 for Win95");
				return 7;
			}
			DBG_MSG("Word 6 for Macintosh");
			bOldMacFile = TRUE;
			return 6;
		}
	default:
		usChse = usGetWord(0x14, aucHeader);
		DBG_DEC(usChse);
		if (usFib < 192) {
			/* Unknown or unsupported version of Word */
			DBG_DEC(usFib);
			return -1;
		}
		DBG_MSG_C(usChse != 256, "Word97 for Win95/98/NT");
		DBG_MSG_C(usChse == 256, "Word98 for Macintosh");
		return 8;
	}
} /* end of iGetVersionNumber */

/*
 * TRUE if the current file was made by Word version 6 or older on an
 * Apple Macintosh, otherwise FALSE.
 * This function hides the methode of how to find out from the rest of the
 * program.
 */
BOOL
bIsOldMacFile(void)
{
	return bOldMacFile;
} /* end of bIsOldMacFile */

/*
 * iInitDocument - initialize a document
 *
 * Returns the version of Word that made the document or -1
 */
int
iInitDocument(FILE *pFile, long lFilesize)
{
	int	iGuess, iWordVersion;

	iGuess = iGuessVersionNumber(pFile, lFilesize);
	switch (iGuess) {
	case 0:
		iWordVersion = iInitDocumentDOS(pFile, lFilesize);
		break;
	case 2:
		iWordVersion = iInitDocumentWIN(pFile, lFilesize);
		break;
	case 5:
		iWordVersion = iInitDocumentMAC(pFile, lFilesize);
		break;
	case 6:
		iWordVersion = iInitDocumentOLE(pFile, lFilesize);
		break;
	default:
		DBG_DEC(iGuess);
		iWordVersion = -1;
		break;
	}
	return iWordVersion;
} /* end of iInitDocument */

/*
 * vFreeDocument - free a document by free-ing its parts
 */
void
vFreeDocument(void)
{
	DBG_MSG("vFreeDocument");

	/* Free the memory */
	vDestroyTextBlockList();
	vDestroyDataBlockList();
	vDestroyListInfoList();
	vDestroyRowInfoList();
	vDestroyStyleInfoList();
	vDestroyFontInfoList();
	vDestroyStylesheetList();
	vDestroyPictInfoList();
	vDestroyDocumentInfoList();
	vDestroySectionInfoList();
	vDestroyHdrFtrInfoList();
	vDestroyPropModList();
	vDestroyNotesInfoLists();
	vDestroyFontTable();
	vDestroySummaryInfo();
} /* end of vFreeDocument */