File: depot.c

package info (click to toggle)
antiword 0.37-16
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 2,236 kB
  • sloc: ansic: 27,835; perl: 174; sh: 129; php: 83; makefile: 24
file content (114 lines) | stat: -rw-r--r-- 2,724 bytes parent folder | download | duplicates (9)
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
/*
 * depot.c
 * Copyright (C) 1998-2002 A.J. van Os; Released under GPL
 *
 * Description:
 * Functions to compute the depot offset
 */

#include "antiword.h"

#define SIZE_RATIO	(BIG_BLOCK_SIZE/SMALL_BLOCK_SIZE)

static ULONG	*aulSmallBlockList = NULL;
static size_t	tSmallBlockListLen = 0;


/*
 * vDestroySmallBlockList - destroy the small block list
 */
void
vDestroySmallBlockList(void)
{
	DBG_MSG("vDestroySmallBlockList");

	aulSmallBlockList = xfree(aulSmallBlockList);
	tSmallBlockListLen = 0;
} /* end of vDestroySmalBlockList */

/*
 * vCreateSmallBlockList - create the small block list
 *
 * returns: TRUE when successful, otherwise FALSE
 */
BOOL
bCreateSmallBlockList(ULONG ulStartblock, const ULONG *aulBBD, size_t tBBDLen)
{
	ULONG	ulTmp;
	size_t	tSize;
	int	iIndex;

	fail(aulSmallBlockList != NULL);
	fail(tSmallBlockListLen != 0);
	fail(ulStartblock > MAX_BLOCKNUMBER && ulStartblock != END_OF_CHAIN);
	fail(aulBBD == NULL);
	fail(tBBDLen == 0);

	/* Find the length of the small block list */
	for (tSmallBlockListLen = 0, ulTmp = ulStartblock;
	     tSmallBlockListLen < tBBDLen && ulTmp != END_OF_CHAIN;
	     tSmallBlockListLen++, ulTmp = aulBBD[ulTmp]) {
		if (ulTmp >= (ULONG)tBBDLen) {
			DBG_DEC(ulTmp);
			DBG_DEC(tBBDLen);
			werr(1, "The Big Block Depot is damaged");
		}
	}
	DBG_DEC(tSmallBlockListLen);

	if (tSmallBlockListLen == 0) {
		/* There is no small block list */
		fail(ulStartblock != END_OF_CHAIN);
		aulSmallBlockList = NULL;
		return TRUE;
	}

	/* Create the small block list */
	tSize = tSmallBlockListLen * sizeof(ULONG);
	aulSmallBlockList = xmalloc(tSize);
	for (iIndex = 0, ulTmp = ulStartblock;
	     iIndex < (int)tBBDLen && ulTmp != END_OF_CHAIN;
	     iIndex++, ulTmp = aulBBD[ulTmp]) {
		if (ulTmp >= (ULONG)tBBDLen) {
			DBG_DEC(ulTmp);
			DBG_DEC(tBBDLen);
			werr(1, "The Big Block Depot is damaged");
		}
		aulSmallBlockList[iIndex] = ulTmp;
		NO_DBG_DEC(aulSmallBlockList[iIndex]);
	}
	return TRUE;
} /* end of bCreateSmallBlockList */

/*
 * ulDepotOffset - get the depot offset the block list
 */
ULONG
ulDepotOffset(ULONG ulIndex, size_t tBlockSize)
{
	ULONG	ulTmp;
	size_t	tTmp;

	fail(ulIndex >= ULONG_MAX / BIG_BLOCK_SIZE);

	switch (tBlockSize) {
	case BIG_BLOCK_SIZE:
		return (ulIndex + 1) * BIG_BLOCK_SIZE;
	case SMALL_BLOCK_SIZE:
		tTmp = (size_t)(ulIndex / SIZE_RATIO);
		ulTmp = ulIndex % SIZE_RATIO;
		if (aulSmallBlockList == NULL ||
		    tTmp >= tSmallBlockListLen) {
			DBG_HEX(aulSmallBlockList);
			DBG_DEC(tSmallBlockListLen);
			DBG_DEC(tTmp);
			return 0;
		}
		return ((aulSmallBlockList[tTmp] + 1) * SIZE_RATIO +
				ulTmp) * SMALL_BLOCK_SIZE;
	default:
		DBG_DEC(tBlockSize);
		DBG_FIXME();
		return 0;
	}
} /* end of ulDepotOffset */