listlist.c 7.67 KB
Newer Older
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
/*
 * listlist.c
 * Copyright (C) 2002,2003 A.J. van Os; Released under GPL
 *
 * Description:
 * Build, read and destroy a list of Word list information
 *
 * Note:
 * This list only exists when the Word document is saved by Word 8 or later
 */

#include "antiword.h"

/*
 * Private structure to hide the way the information
 * is stored from the rest of the program
 */
typedef struct list_desc_tag {
	list_block_type		tInfo;
	ULONG			ulListID;
	USHORT			usIstd;
	UCHAR			ucListLevel;
	struct list_desc_tag	*pNext;
} list_desc_type;

typedef struct list_value_tag {
	USHORT			usValue;
	USHORT			usListIndex;
	UCHAR			ucListLevel;
	struct list_value_tag	*pNext;
} list_value_type;

/* Variables needed to describe the LFO list (pllfo) */
static ULONG		*aulLfoList = NULL;
static USHORT		usLfoLen = 0;
/* Variables needed to write the List Information List */
static list_desc_type	*pAnchor = NULL;
static list_desc_type	*pBlockLast = NULL;
/* Variable needed for numbering new lists */
static list_value_type	*pValues = NULL;
/* Variables needed for numbering old lists */
static int	iOldListSeqNumber = 0;
static USHORT	usOldListValue = 0;


/*
 * vDestroyListInfoList - destroy the List Information List
 */
void
vDestroyListInfoList(void)
{
	list_desc_type	*pCurr, *pNext;
	list_value_type	*pValueCurr, *pValueNext;

	DBG_MSG("vDestroyListInfoList");

	/* Free the LFO list */
	usLfoLen = 0;
	aulLfoList = xfree(aulLfoList);

	/* Free the List Information List */
	pCurr = pAnchor;
	while (pCurr != NULL) {
		pNext = pCurr->pNext;
		pCurr = xfree(pCurr);
		pCurr = pNext;
	}
	pAnchor = NULL;
	/* Reset all control variables */
	pBlockLast = NULL;

	/* Free the values list */
	pValueCurr = pValues;
	while (pValueCurr != NULL) {
		pValueNext = pValueCurr->pNext;
		pValueCurr = xfree(pValueCurr);
		pValueCurr = pValueNext;
	}
	pValues = NULL;
	/* Reset the values for the old lists */
	iOldListSeqNumber = 0;
	usOldListValue = 0;
} /* end of vDestroyListInfoList */

/*
 * vBuildLfoList - build the LFO list (pllfo)
 */
void
vBuildLfoList(const UCHAR *aucBuffer, size_t tBufLen)
{
	size_t	tRecords;
	int	iIndex;

	fail(aucBuffer == NULL);

	if (tBufLen < 4) {
		return;
	}
	tRecords = (size_t)ulGetLong(0, aucBuffer);
	NO_DBG_DEC(tRecords);
	if (4 + 16 * tRecords > tBufLen || tRecords >= 0x7fff) {
		/* Just a sanity check */
		DBG_DEC(tRecords);
		DBG_DEC(4 + 16 * tRecords);
		DBG_DEC(tBufLen);
		return;
	}
	aulLfoList = xcalloc(tRecords, sizeof(ULONG));
	for (iIndex = 0; iIndex < (int)tRecords; iIndex++) {
		aulLfoList[iIndex] = ulGetLong(4 + 16 * iIndex, aucBuffer);
		NO_DBG_HEX(aulLfoList[iIndex]);
	}
	usLfoLen = (USHORT)tRecords;
} /* end of vBuildLfoList */

/*
 * vAdd2ListInfoList - add an element to the List Information list
 */
void
vAdd2ListInfoList(ULONG ulListID, USHORT usIstd, UCHAR ucListLevel,
	const list_block_type *pListBlock)
{
	list_desc_type	*pListMember;

	fail(pListBlock == NULL);

	NO_DBG_HEX(ulListID);
	NO_DBG_DEC(usIstd);
	NO_DBG_DEC(ucListLevel);
	NO_DBG_DEC(pListBlock->ulStartAt);
	NO_DBG_DEC(pListBlock->bNoRestart);
	NO_DBG_DEC(pListBlock->sLeftIndent);
	NO_DBG_HEX(pListBlock->ucNFC);
	NO_DBG_HEX(pListBlock->usListChar);

	/* Create list member */
	pListMember = xmalloc(sizeof(list_desc_type));
	/* Fill the list member */
	pListMember->tInfo = *pListBlock;
	pListMember->ulListID = ulListID;
	pListMember->usIstd = usIstd;
	pListMember->ucListLevel = ucListLevel;
	pListMember->pNext = NULL;
	/* Correct the values where needed */
	if (pListMember->tInfo.ulStartAt > 0xffff) {
		DBG_DEC(pListMember->tInfo.ulStartAt);
		pListMember->tInfo.ulStartAt = 1;
	}
	/* Add the new member to the list */
	if (pAnchor == NULL) {
		pAnchor = pListMember;
	} else {
		fail(pBlockLast == NULL);
		pBlockLast->pNext = pListMember;
	}
	pBlockLast = pListMember;
} /* end of vAdd2ListInfoList */

/*
 * Get a matching record from the List Information List
 *
 * Returns NULL if no matching records is found
 */
const list_block_type *
pGetListInfo(USHORT usListIndex, UCHAR ucListLevel)
{
	list_desc_type	*pCurr;
	list_block_type	*pNearMatch;
	ULONG	ulListID;

	if (usListIndex == 0) {
		return NULL;
	}
	if (usListIndex - 1 >= usLfoLen || ucListLevel > 8) {
		DBG_DEC(usListIndex);
		DBG_DEC(ucListLevel);
		return NULL;
	}
	fail(aulLfoList == NULL);
	ulListID = aulLfoList[usListIndex - 1];
	NO_DBG_HEX(ulListID);

	pNearMatch = NULL;
	for (pCurr = pAnchor; pCurr != NULL; pCurr = pCurr->pNext) {
		if (pCurr->ulListID != ulListID) {
			/* No match */
			continue;
		}
		if (pCurr->ucListLevel == ucListLevel) {
			/* Exact match */
			return &pCurr->tInfo;
		}
		if (pCurr->ucListLevel == 0) {
			/* Near match */
			pNearMatch = &pCurr->tInfo;
		}
	}
	/* No exact match, use a near match if any */
	return pNearMatch;
} /* end of pGetListInfo */

/*
 * Get a matching record from the List Information List
 *
 * Returns NULL if no matching records is found
 */
const list_block_type *
pGetListInfoByIstd(USHORT usIstd)
{
	list_desc_type	*pCurr;

	if (usIstd == ISTD_INVALID || usIstd == STI_NIL || usIstd == STI_USER) {
		return NULL;
	}

	for (pCurr = pAnchor; pCurr != NULL; pCurr = pCurr->pNext) {
		if (pCurr->usIstd == usIstd) {
			return &pCurr->tInfo;
		}
	}
	return NULL;
} /* end of pGetListInfoByIstd */

/*
 * vRestartListValues - reset the less significant list levels
 */
static void
vRestartListValues(USHORT usListIndex, UCHAR ucListLevel)
{
	list_value_type	*pPrev, *pCurr, *pNext;
	int		iCounter;

	iCounter = 0;
	pPrev = NULL;
	pCurr = pValues;

	while (pCurr != NULL) {
		if (pCurr->usListIndex != usListIndex ||
		    pCurr->ucListLevel <= ucListLevel) {
			pPrev = pCurr;
			pCurr = pCurr->pNext;
			continue;
		}
		/* Reset the level by deleting the record */
		pNext = pCurr->pNext;
		if (pPrev == NULL) {
			pValues = pNext;
		} else {
			pPrev->pNext = pNext;
		}
		DBG_DEC(pCurr->usListIndex);
		DBG_DEC(pCurr->ucListLevel);
		pCurr = xfree(pCurr);
		pCurr = pNext;
		iCounter++;
	}
	DBG_DEC_C(iCounter > 0, iCounter);
} /* end of vRestartListValues */

/*
 * usGetListValue - Get the current value of the given list
 *
 * Returns the value of the given list
 */
USHORT
usGetListValue(int iListSeqNumber, int iWordVersion,
	const style_block_type *pStyle)
{
	list_value_type	*pCurr;
	USHORT		usValue;

	fail(iListSeqNumber < 0);
	fail(iListSeqNumber < iOldListSeqNumber);
	fail(iWordVersion < 0);
	fail(pStyle == NULL);

	if (iListSeqNumber <= 0) {
		return 0;
	}

	if (iWordVersion < 8) {
		/* Old style list */
		if (iListSeqNumber == iOldListSeqNumber ||
		    (iListSeqNumber == iOldListSeqNumber + 1 &&
		     eGetNumType(pStyle->ucNumLevel) == level_type_sequence)) {
			if (!pStyle->bNumPause) {
				usOldListValue++;
			}
		} else {
			usOldListValue = pStyle->usStartAt;
		}
		iOldListSeqNumber = iListSeqNumber;
		return usOldListValue;
	}

	/* New style list */
	if (pStyle->usListIndex == 0 ||
	    pStyle->usListIndex - 1 >= usLfoLen ||
	    pStyle->ucListLevel > 8) {
		/* Out of range; no need to search */
		return 0;
	}

	for (pCurr = pValues; pCurr != NULL; pCurr = pCurr->pNext) {
		if (pCurr->usListIndex == pStyle->usListIndex &&
		    pCurr->ucListLevel == pStyle->ucListLevel) {
			/* Record found; increment and return the value */
			pCurr->usValue++;
			usValue = pCurr->usValue;
			if (!pStyle->bNoRestart) {
				vRestartListValues(pStyle->usListIndex,
						pStyle->ucListLevel);
			}
			return usValue;
		}
	}

	/* Record not found; create it and add it to the front of the list */
	pCurr = xmalloc(sizeof(list_value_type));
	pCurr->usValue = pStyle->usStartAt;
	pCurr->usListIndex = pStyle->usListIndex;
	pCurr->ucListLevel = pStyle->ucListLevel;
	pCurr->pNext = pValues;
	pValues = pCurr;
	usValue = pCurr->usValue;
	if (!pStyle->bNoRestart) {
		vRestartListValues(pStyle->usListIndex, pStyle->ucListLevel);
	}
	return usValue;
} /* end of usGetListValue */