xref: /plan9/sys/src/cmd/aux/antiword/propmod.c (revision f5736e95f14e1485b3a0291fa82d86cca323ab61)
1 /*
2  * propmod.c
3  * Copyright (C) 2001-2003 A.J. van Os; Released under GPL
4  *
5  * Description:
6  * Build, read and destroy a list (array) of Word property modifiers
7  */
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include "antiword.h"
12 
13 #if defined(DEBUG)
14 #define ELEMENTS_TO_ADD	 3
15 #else
16 #define ELEMENTS_TO_ADD	30
17 #endif /* DEBUG */
18 
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;
23 
24 
25 /*
26  * vDestroyPropModList - destroy the property modifier list
27  */
28 void
vDestroyPropModList(void)29 vDestroyPropModList(void)
30 {
31 	size_t	tIndex;
32 
33 	DBG_MSG("vDestroyPropModList");
34 
35 	/* Free all the elements of the list */
36 	for (tIndex = 0; tIndex < tNextFree; tIndex++) {
37 		ppAnchor[tIndex] = xfree(ppAnchor[tIndex]);
38 	}
39 	/* Free the list itself */
40 	ppAnchor = xfree(ppAnchor);
41 	/* Reset all control variables */
42 	tNextFree = 0;
43 	tMaxElements = 0;
44 } /* end of vDestroyPropModList */
45 
46 /*
47  * vAdd2PropModList - add an element to the property modifier list
48  */
49 void
vAdd2PropModList(const UCHAR * aucPropMod)50 vAdd2PropModList(const UCHAR *aucPropMod)
51 {
52 	size_t	tSize, tLen;
53 
54 	fail(aucPropMod == NULL);
55 
56 	NO_DBG_MSG("vAdd2PropModList");
57 
58 	if (tNextFree >= tMaxElements) {
59 		tMaxElements += ELEMENTS_TO_ADD;
60 		tSize = tMaxElements * sizeof(UCHAR **);
61 		ppAnchor = xrealloc(ppAnchor, tSize);
62 	}
63 	NO_DBG_DEC(tNextFree);
64 
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 */
72 
73 /*
74  * aucReadPropModListItem - get an item of the property modifier list
75  */
76 const UCHAR *
aucReadPropModListItem(USHORT usPropMod)77 aucReadPropModListItem(USHORT usPropMod)
78 {
79 	static UCHAR	aucBuffer[4];
80 	size_t	tIndex;
81 
82 	if (usPropMod == IGNORE_PROPMOD) {
83 		/* This Properties Modifier must be ignored */
84 		return NULL;
85 	}
86 
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;
94 	}
95 
96 	if (ppAnchor == NULL) {
97 		/* No information available */
98 		return NULL;
99 	}
100 
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;
108 	}
109 	return ppAnchor[tIndex];
110 } /* end of aucGetPropModListItem */
111