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
|
/* Chunk handling: Flexible arrays
* ===============================
*
*/
#include <HTUtils.h>
#include <HTChunk.h>
#include <LYLeaks.h>
/*
* Initialize a chunk with a certain allocation unit
*/
void HTChunkInit(HTChunk *ch, int grow)
{
ch->data = 0;
ch->growby = grow;
ch->size = 0;
ch->allocated = 0;
}
/* Create a chunk with a certain allocation unit
* --------------
*/
HTChunk *HTChunkCreate(int grow)
{
HTChunk *ch = typecalloc(HTChunk);
if (ch == NULL)
outofmem(__FILE__, "creation of chunk");
HTChunkInit(ch, grow);
return ch;
}
HTChunk *HTChunkCreateMayFail(int grow, int failok)
{
HTChunk *ch = typecalloc(HTChunk);
if (ch == NULL) {
if (!failok) {
outofmem(__FILE__, "creation of chunk");
} else {
return ch;
}
}
HTChunkInit(ch, grow);
ch->failok = failok;
return ch;
}
/* Create a chunk with a certain allocation unit and ensured size
* --------------
*/
HTChunk *HTChunkCreate2(int grow, size_t needed)
{
HTChunk *ch = typecalloc(HTChunk);
if (ch == NULL)
outofmem(__FILE__, "HTChunkCreate2");
HTChunkInit(ch, grow);
if (needed > 0) {
ch->allocated = needed - 1 - ((needed - 1) % ch->growby)
+ ch->growby; /* Round up */
CTRACE((tfp, "HTChunkCreate2: requested %d, allocate %d\n",
(int) needed, ch->allocated));
ch->data = typecallocn(char, ch->allocated);
if (!ch->data)
outofmem(__FILE__, "HTChunkCreate2 data");
}
return ch;
}
/* Clear a chunk of all data
* --------------------------
*/
void HTChunkClear(HTChunk *ch)
{
FREE(ch->data);
ch->size = 0;
ch->allocated = 0;
}
/* Free a chunk
* ------------
*/
void HTChunkFree(HTChunk *ch)
{
FREE(ch->data);
FREE(ch);
}
/* Realloc the chunk
* -----------------
*/
BOOL HTChunkRealloc(HTChunk *ch, int growby)
{
char *data;
ch->allocated = ch->allocated + growby;
data = ch->data ? (char *) realloc(ch->data, ch->allocated)
: typecallocn(char, ch->allocated);
if (data) {
ch->data = data;
} else if (ch->failok) {
HTChunkClear(ch); /* allocation failed, clear all data - kw */
return FALSE; /* caller should check ch->allocated - kw */
} else {
outofmem(__FILE__, "HTChunkRealloc");
}
return TRUE;
}
/* Append a character
* ------------------
*/
/* Warning: the code of this function is defined as macro in SGML.c. Change
the macro or undefine it in SGML.c when changing this function. -VH */
void HTChunkPutc(HTChunk *ch, char c)
{
if (ch->size >= ch->allocated) {
if (!HTChunkRealloc(ch, ch->growby))
return;
}
ch->data[ch->size++] = c;
}
/* Ensure a certain size
* ---------------------
*/
void HTChunkEnsure(HTChunk *ch, int needed)
{
if (needed <= ch->allocated)
return;
ch->allocated = needed - 1 - ((needed - 1) % ch->growby)
+ ch->growby; /* Round up */
ch->data = ch->data ? (char *) realloc(ch->data, ch->allocated)
: typecallocn(char, ch->allocated);
if (ch->data == NULL)
outofmem(__FILE__, "HTChunkEnsure");
}
void HTChunkPutb(HTChunk *ch, const char *b, int l)
{
if (l <= 0)
return;
if (ch->size + l > ch->allocated) {
int growby = l - (l % ch->growby) + ch->growby; /* Round up */
if (!HTChunkRealloc(ch, growby))
return;
}
memcpy(ch->data + ch->size, b, l);
ch->size += l;
}
#define PUTC(code) ch->data[ch->size++] = (char)(code)
#define PUTC2(code) ch->data[ch->size++] = (char)(0x80|(0x3f &(code)))
void HTChunkPutUtf8Char(HTChunk *ch, UCode_t code)
{
int utflen;
if (TOASCII(code) < 128)
utflen = 1;
else if (code < 0x800L) {
utflen = 2;
} else if (code < 0x10000L) {
utflen = 3;
} else if (code < 0x200000L) {
utflen = 4;
} else if (code < 0x4000000L) {
utflen = 5;
} else if (code <= 0x7fffffffL) {
utflen = 6;
} else
utflen = 0;
if (ch->size + utflen > ch->allocated) {
int growby = (ch->growby >= utflen) ? ch->growby : utflen;
if (!HTChunkRealloc(ch, growby))
return;
}
switch (utflen) {
case 0:
return;
case 1:
ch->data[ch->size++] = (char) code;
return;
case 2:
PUTC(0xc0 | (code >> 6));
break;
case 3:
PUTC(0xe0 | (code >> 12));
break;
case 4:
PUTC(0xf0 | (code >> 18));
break;
case 5:
PUTC(0xf8 | (code >> 24));
break;
case 6:
PUTC(0xfc | (code >> 30));
break;
}
switch (utflen) {
case 6:
PUTC2(code >> 24);
/* FALLTHRU */
case 5:
PUTC2(code >> 18);
/* FALLTHRU */
case 4:
PUTC2(code >> 12);
/* FALLTHRU */
case 3:
PUTC2(code >> 6);
/* FALLTHRU */
case 2:
PUTC2(code);
break;
}
}
/* Terminate a chunk
* -----------------
*/
void HTChunkTerminate(HTChunk *ch)
{
HTChunkPutc(ch, (char) 0);
}
/* Append a string
* ---------------
*/
void HTChunkPuts(HTChunk *ch, const char *s)
{
const char *p;
for (p = s; *p; p++) {
if (ch->size >= ch->allocated) {
if (!HTChunkRealloc(ch, ch->growby))
return;
}
ch->data[ch->size++] = *p;
}
}
|