Imported from antiword-0.37.tar.gz.
[antiword.git] / depot.c
blob295c39d937aa5bec86aa97f7be5330d467a5c355
1 /*
2 * depot.c
3 * Copyright (C) 1998-2002 A.J. van Os; Released under GPL
5 * Description:
6 * Functions to compute the depot offset
7 */
9 #include "antiword.h"
11 #define SIZE_RATIO (BIG_BLOCK_SIZE/SMALL_BLOCK_SIZE)
13 static ULONG *aulSmallBlockList = NULL;
14 static size_t tSmallBlockListLen = 0;
18 * vDestroySmallBlockList - destroy the small block list
20 void
21 vDestroySmallBlockList(void)
23 DBG_MSG("vDestroySmallBlockList");
25 aulSmallBlockList = xfree(aulSmallBlockList);
26 tSmallBlockListLen = 0;
27 } /* end of vDestroySmalBlockList */
30 * vCreateSmallBlockList - create the small block list
32 * returns: TRUE when successful, otherwise FALSE
34 BOOL
35 bCreateSmallBlockList(ULONG ulStartblock, const ULONG *aulBBD, size_t tBBDLen)
37 ULONG ulTmp;
38 size_t tSize;
39 int iIndex;
41 fail(aulSmallBlockList != NULL);
42 fail(tSmallBlockListLen != 0);
43 fail(ulStartblock > MAX_BLOCKNUMBER && ulStartblock != END_OF_CHAIN);
44 fail(aulBBD == NULL);
45 fail(tBBDLen == 0);
47 /* Find the length of the small block list */
48 for (tSmallBlockListLen = 0, ulTmp = ulStartblock;
49 tSmallBlockListLen < tBBDLen && ulTmp != END_OF_CHAIN;
50 tSmallBlockListLen++, ulTmp = aulBBD[ulTmp]) {
51 if (ulTmp >= (ULONG)tBBDLen) {
52 DBG_DEC(ulTmp);
53 DBG_DEC(tBBDLen);
54 werr(1, "The Big Block Depot is damaged");
57 DBG_DEC(tSmallBlockListLen);
59 if (tSmallBlockListLen == 0) {
60 /* There is no small block list */
61 fail(ulStartblock != END_OF_CHAIN);
62 aulSmallBlockList = NULL;
63 return TRUE;
66 /* Create the small block list */
67 tSize = tSmallBlockListLen * sizeof(ULONG);
68 aulSmallBlockList = xmalloc(tSize);
69 for (iIndex = 0, ulTmp = ulStartblock;
70 iIndex < (int)tBBDLen && ulTmp != END_OF_CHAIN;
71 iIndex++, ulTmp = aulBBD[ulTmp]) {
72 if (ulTmp >= (ULONG)tBBDLen) {
73 DBG_DEC(ulTmp);
74 DBG_DEC(tBBDLen);
75 werr(1, "The Big Block Depot is damaged");
77 aulSmallBlockList[iIndex] = ulTmp;
78 NO_DBG_DEC(aulSmallBlockList[iIndex]);
80 return TRUE;
81 } /* end of bCreateSmallBlockList */
84 * ulDepotOffset - get the depot offset the block list
86 ULONG
87 ulDepotOffset(ULONG ulIndex, size_t tBlockSize)
89 ULONG ulTmp;
90 size_t tTmp;
92 fail(ulIndex >= ULONG_MAX / BIG_BLOCK_SIZE);
94 switch (tBlockSize) {
95 case BIG_BLOCK_SIZE:
96 return (ulIndex + 1) * BIG_BLOCK_SIZE;
97 case SMALL_BLOCK_SIZE:
98 tTmp = (size_t)(ulIndex / SIZE_RATIO);
99 ulTmp = ulIndex % SIZE_RATIO;
100 if (aulSmallBlockList == NULL ||
101 tTmp >= tSmallBlockListLen) {
102 DBG_HEX(aulSmallBlockList);
103 DBG_DEC(tSmallBlockListLen);
104 DBG_DEC(tTmp);
105 return 0;
107 return ((aulSmallBlockList[tTmp] + 1) * SIZE_RATIO +
108 ulTmp) * SMALL_BLOCK_SIZE;
109 default:
110 DBG_DEC(tBlockSize);
111 DBG_FIXME();
112 return 0;
114 } /* end of ulDepotOffset */