Imported from antiword-0.37.tar.gz.
[antiword.git] / propmod.c
blob7a3cdbb11c1fb3aa98090867f1a9af4b05c918cf
1 /*
2 * propmod.c
3 * Copyright (C) 2001-2003 A.J. van Os; Released under GPL
5 * Description:
6 * Build, read and destroy a list (array) of Word property modifiers
7 */
9 #include <stdio.h>
10 #include <string.h>
11 #include "antiword.h"
13 #if defined(DEBUG)
14 #define ELEMENTS_TO_ADD 3
15 #else
16 #define ELEMENTS_TO_ADD 30
17 #endif /* DEBUG */
19 /* Variables needed to write the property modifier list */
20 static UCHAR **ppAnchor = NULL;
21 static size_t tNextFree = 0;
22 static size_t tMaxElements = 0;
26 * vDestroyPropModList - destroy the property modifier list
28 void
29 vDestroyPropModList(void)
31 size_t tIndex;
33 DBG_MSG("vDestroyPropModList");
35 /* Free all the elements of the list */
36 for (tIndex = 0; tIndex < tNextFree; tIndex++) {
37 ppAnchor[tIndex] = xfree(ppAnchor[tIndex]);
39 /* Free the list itself */
40 ppAnchor = xfree(ppAnchor);
41 /* Reset all control variables */
42 tNextFree = 0;
43 tMaxElements = 0;
44 } /* end of vDestroyPropModList */
47 * vAdd2PropModList - add an element to the property modifier list
49 void
50 vAdd2PropModList(const UCHAR *aucPropMod)
52 size_t tSize, tLen;
54 fail(aucPropMod == NULL);
56 NO_DBG_MSG("vAdd2PropModList");
58 if (tNextFree >= tMaxElements) {
59 tMaxElements += ELEMENTS_TO_ADD;
60 tSize = tMaxElements * sizeof(UCHAR **);
61 ppAnchor = xrealloc(ppAnchor, tSize);
63 NO_DBG_DEC(tNextFree);
65 tLen = 2 + (size_t)usGetWord(0, aucPropMod);
66 NO_DBG_HEX(tLen);
67 NO_DBG_PRINT_BLOCK(pucPropMod, tLen);
68 ppAnchor[tNextFree] = xmalloc(tLen);
69 memcpy(ppAnchor[tNextFree], aucPropMod, tLen);
70 tNextFree++;
71 } /* end of vAdd2PropModList */
74 * aucReadPropModListItem - get an item of the property modifier list
76 const UCHAR *
77 aucReadPropModListItem(USHORT usPropMod)
79 static UCHAR aucBuffer[4];
80 size_t tIndex;
82 if (usPropMod == IGNORE_PROPMOD) {
83 /* This Properties Modifier must be ignored */
84 return NULL;
87 if (!odd(usPropMod)) {
88 /* Variant 1: The information is in the input ifself */
89 aucBuffer[0] = 2;
90 aucBuffer[1] = 0;
91 aucBuffer[2] = (UCHAR)((usPropMod & 0x00fe) >> 1);
92 aucBuffer[3] = (UCHAR)((usPropMod & 0xff00) >> 8);
93 return aucBuffer;
96 if (ppAnchor == NULL) {
97 /* No information available */
98 return NULL;
101 /* Variant 2: The input contains an index */
102 tIndex = (size_t)(usPropMod >> 1);
103 if (tIndex >= tNextFree) {
104 DBG_HEX(usPropMod);
105 DBG_DEC(tIndex);
106 DBG_DEC(tNextFree);
107 return NULL;
109 return ppAnchor[tIndex];
110 } /* end of aucGetPropModListItem */