1 /*
2 * "streamable kanji code filter and converter"
3 * Copyright (c) 1998-2002 HappySize, Inc. All rights reserved.
4 *
5 * LICENSE NOTICES
6 *
7 * This file is part of "streamable kanji code filter and converter",
8 * which is distributed under the terms of GNU Lesser General Public
9 * License (version 2) as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with "streamable kanji code filter and converter";
18 * if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 * Suite 330, Boston, MA 02111-1307 USA
20 *
21 * The author of this file:
22 *
23 */
24 /*
25 * The source code included in this files was separated from mbfilter.c
26 * by Moriyoshi Koizumi <moriyoshi@php.net> on 20 Dec 2002. The file
27 * mbfilter.c is included in this package .
28 *
29 */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include <stddef.h>
36
37 #include <string.h>
38 #include "mbfl_allocators.h"
39 #include "mbfl_string.h"
40 #include "mbfl_memory_device.h"
41
42 /*
43 * memory device output functions
44 */
45 void
mbfl_memory_device_init(mbfl_memory_device * device,size_t initsz,size_t allocsz)46 mbfl_memory_device_init(mbfl_memory_device *device, size_t initsz, size_t allocsz)
47 {
48 if (device) {
49 device->length = 0;
50 device->buffer = NULL;
51 if (initsz > 0) {
52 device->buffer = (unsigned char *)mbfl_malloc(initsz);
53 if (device->buffer != NULL) {
54 device->length = initsz;
55 }
56 }
57 device->pos = 0;
58 if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
59 device->allocsz = allocsz;
60 } else {
61 device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
62 }
63 }
64 }
65
66 void
mbfl_memory_device_realloc(mbfl_memory_device * device,size_t initsz,size_t allocsz)67 mbfl_memory_device_realloc(mbfl_memory_device *device, size_t initsz, size_t allocsz)
68 {
69 unsigned char *tmp;
70
71 if (device) {
72 if (initsz > device->length) {
73 tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, initsz);
74 if (tmp != NULL) {
75 device->buffer = tmp;
76 device->length = initsz;
77 }
78 }
79 if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
80 device->allocsz = allocsz;
81 } else {
82 device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
83 }
84 }
85 }
86
87 void
mbfl_memory_device_clear(mbfl_memory_device * device)88 mbfl_memory_device_clear(mbfl_memory_device *device)
89 {
90 if (device) {
91 if (device->buffer) {
92 mbfl_free(device->buffer);
93 }
94 device->buffer = NULL;
95 device->length = 0;
96 device->pos = 0;
97 }
98 }
99
100 void
mbfl_memory_device_reset(mbfl_memory_device * device)101 mbfl_memory_device_reset(mbfl_memory_device *device)
102 {
103 if (device) {
104 device->pos = 0;
105 }
106 }
107
108 void
mbfl_memory_device_unput(mbfl_memory_device * device)109 mbfl_memory_device_unput(mbfl_memory_device *device)
110 {
111 if (device->pos > 0) {
112 device->pos--;
113 }
114 }
115
116 mbfl_string *
mbfl_memory_device_result(mbfl_memory_device * device,mbfl_string * result)117 mbfl_memory_device_result(mbfl_memory_device *device, mbfl_string *result)
118 {
119 if (device && result) {
120 result->len = device->pos;
121 mbfl_memory_device_output('\0', device);
122 result->val = device->buffer;
123 device->buffer = NULL;
124 device->length = 0;
125 device->pos= 0;
126 if (result->val == NULL) {
127 result->len = 0;
128 result = NULL;
129 }
130 } else {
131 result = NULL;
132 }
133
134 return result;
135 }
136
137 int
mbfl_memory_device_output(int c,void * data)138 mbfl_memory_device_output(int c, void *data)
139 {
140 mbfl_memory_device *device = (mbfl_memory_device *)data;
141
142 if (device->pos >= device->length) {
143 /* reallocate buffer */
144 size_t newlen;
145 unsigned char *tmp;
146
147 if (device->length > SIZE_MAX - device->allocsz) {
148 /* overflow */
149 return -1;
150 }
151
152 newlen = device->length + device->allocsz;
153 tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
154 if (tmp == NULL) {
155 return -1;
156 }
157 device->length = newlen;
158 device->buffer = tmp;
159 }
160
161 device->buffer[device->pos++] = (unsigned char)c;
162 return c;
163 }
164
165 int
mbfl_memory_device_output2(int c,void * data)166 mbfl_memory_device_output2(int c, void *data)
167 {
168 mbfl_memory_device *device = (mbfl_memory_device *)data;
169
170 if (2 > device->length - device->pos) {
171 /* reallocate buffer */
172 size_t newlen;
173 unsigned char *tmp;
174
175 if (device->length > SIZE_MAX - device->allocsz) {
176 /* overflow */
177 return -1;
178 }
179
180 newlen = device->length + device->allocsz;
181 tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
182 if (tmp == NULL) {
183 return -1;
184 }
185 device->length = newlen;
186 device->buffer = tmp;
187 }
188
189 device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
190 device->buffer[device->pos++] = (unsigned char)(c & 0xff);
191
192 return c;
193 }
194
195 int
mbfl_memory_device_output4(int c,void * data)196 mbfl_memory_device_output4(int c, void* data)
197 {
198 mbfl_memory_device *device = (mbfl_memory_device *)data;
199
200 if (4 > device->length - device->pos) {
201 /* reallocate buffer */
202 size_t newlen;
203 unsigned char *tmp;
204
205 if (device->length > SIZE_MAX - device->allocsz) {
206 /* overflow */
207 return -1;
208 }
209
210 newlen = device->length + device->allocsz;
211 tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
212 if (tmp == NULL) {
213 return -1;
214 }
215 device->length = newlen;
216 device->buffer = tmp;
217 }
218
219 device->buffer[device->pos++] = (unsigned char)((c >> 24) & 0xff);
220 device->buffer[device->pos++] = (unsigned char)((c >> 16) & 0xff);
221 device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
222 device->buffer[device->pos++] = (unsigned char)(c & 0xff);
223
224 return c;
225 }
226
227 int
mbfl_memory_device_strcat(mbfl_memory_device * device,const char * psrc)228 mbfl_memory_device_strcat(mbfl_memory_device *device, const char *psrc)
229 {
230 return mbfl_memory_device_strncat(device, psrc, strlen(psrc));
231 }
232
233 int
mbfl_memory_device_strncat(mbfl_memory_device * device,const char * psrc,size_t len)234 mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, size_t len)
235 {
236 unsigned char *w;
237
238 if (len > device->length - device->pos) {
239 /* reallocate buffer */
240 size_t newlen;
241 unsigned char *tmp;
242
243 if (len > SIZE_MAX - MBFL_MEMORY_DEVICE_ALLOC_SIZE
244 || device->length > SIZE_MAX - (len + MBFL_MEMORY_DEVICE_ALLOC_SIZE)) {
245 /* overflow */
246 return -1;
247 }
248
249 newlen = device->length + len + MBFL_MEMORY_DEVICE_ALLOC_SIZE;
250 tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen);
251 if (tmp == NULL) {
252 return -1;
253 }
254
255 device->length = newlen;
256 device->buffer = tmp;
257 }
258
259 w = &device->buffer[device->pos];
260 memcpy(w, psrc, len);
261 device->pos += len;
262
263 return 0;
264 }
265
266 int
mbfl_memory_device_devcat(mbfl_memory_device * dest,mbfl_memory_device * src)267 mbfl_memory_device_devcat(mbfl_memory_device *dest, mbfl_memory_device *src)
268 {
269 return mbfl_memory_device_strncat(dest, (const char *) src->buffer, src->pos);
270 }
271
272 void
mbfl_wchar_device_init(mbfl_wchar_device * device)273 mbfl_wchar_device_init(mbfl_wchar_device *device)
274 {
275 if (device) {
276 device->buffer = NULL;
277 device->length = 0;
278 device->pos= 0;
279 device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
280 }
281 }
282
283 void
mbfl_wchar_device_clear(mbfl_wchar_device * device)284 mbfl_wchar_device_clear(mbfl_wchar_device *device)
285 {
286 if (device) {
287 if (device->buffer) {
288 mbfl_free(device->buffer);
289 }
290 device->buffer = NULL;
291 device->length = 0;
292 device->pos = 0;
293 }
294 }
295
296 int
mbfl_wchar_device_output(int c,void * data)297 mbfl_wchar_device_output(int c, void *data)
298 {
299 mbfl_wchar_device *device = (mbfl_wchar_device *)data;
300
301 if (device->pos >= device->length) {
302 /* reallocate buffer */
303 size_t newlen;
304 unsigned int *tmp;
305
306 if (device->length > SIZE_MAX - device->allocsz) {
307 /* overflow */
308 return -1;
309 }
310
311 newlen = device->length + device->allocsz;
312 if (newlen > SIZE_MAX / sizeof(int)) {
313 /* overflow */
314 return -1;
315 }
316
317 tmp = (unsigned int *)mbfl_realloc((void *)device->buffer, newlen*sizeof(int));
318 if (tmp == NULL) {
319 return -1;
320 }
321 device->length = newlen;
322 device->buffer = tmp;
323 }
324
325 device->buffer[device->pos++] = c;
326
327 return c;
328 }
329