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 #ifdef HAVE_STDDEF_H
36 #include <stddef.h>
37 #endif
38 
39 #include "mbfl_allocators.h"
40 #include "mbfl_string.h"
41 #include "mbfl_memory_device.h"
42 
43 /*
44  * memory device output functions
45  */
46 void
mbfl_memory_device_init(mbfl_memory_device * device,int initsz,int allocsz)47 mbfl_memory_device_init(mbfl_memory_device *device, int initsz, int allocsz)
48 {
49 	if (device) {
50 		device->length = 0;
51 		device->buffer = (unsigned char *)0;
52 		if (initsz > 0) {
53 			device->buffer = (unsigned char *)mbfl_malloc(initsz*sizeof(unsigned char));
54 			if (device->buffer != NULL) {
55 				device->length = initsz;
56 			}
57 		}
58 		device->pos= 0;
59 		if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
60 			device->allocsz = allocsz;
61 		} else {
62 			device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
63 		}
64 	}
65 }
66 
67 void
mbfl_memory_device_realloc(mbfl_memory_device * device,int initsz,int allocsz)68 mbfl_memory_device_realloc(mbfl_memory_device *device, int initsz, int allocsz)
69 {
70 	unsigned char *tmp;
71 
72 	if (device) {
73 		if (initsz > device->length) {
74 			tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, initsz*sizeof(unsigned char));
75 			if (tmp != NULL) {
76 				device->buffer = tmp;
77 				device->length = initsz;
78 			}
79 		}
80 		if (allocsz > MBFL_MEMORY_DEVICE_ALLOC_SIZE) {
81 			device->allocsz = allocsz;
82 		} else {
83 			device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
84 		}
85 	}
86 }
87 
88 void
mbfl_memory_device_clear(mbfl_memory_device * device)89 mbfl_memory_device_clear(mbfl_memory_device *device)
90 {
91 	if (device) {
92 		if (device->buffer) {
93 			mbfl_free(device->buffer);
94 		}
95 		device->buffer = (unsigned char *)0;
96 		device->length = 0;
97 		device->pos = 0;
98 	}
99 }
100 
101 void
mbfl_memory_device_reset(mbfl_memory_device * device)102 mbfl_memory_device_reset(mbfl_memory_device *device)
103 {
104 	if (device) {
105 		device->pos = 0;
106 	}
107 }
108 
109 void
mbfl_memory_device_unput(mbfl_memory_device * device)110 mbfl_memory_device_unput(mbfl_memory_device *device)
111 {
112 	if (device->pos > 0) {
113 		device->pos--;
114 	}
115 }
116 
117 mbfl_string *
mbfl_memory_device_result(mbfl_memory_device * device,mbfl_string * result)118 mbfl_memory_device_result(mbfl_memory_device *device, mbfl_string *result)
119 {
120 	if (device && result) {
121 		result->len = device->pos;
122 		mbfl_memory_device_output4('\0', device);
123 		result->val = device->buffer;
124 		device->buffer = (unsigned char *)0;
125 		device->length = 0;
126 		device->pos= 0;
127 		if (result->val == NULL) {
128 			result->len = 0;
129 			result = NULL;
130 		}
131 	} else {
132 		result = NULL;
133 	}
134 
135 	return result;
136 }
137 
138 int
mbfl_memory_device_output(int c,void * data)139 mbfl_memory_device_output(int c, void *data)
140 {
141 	mbfl_memory_device *device = (mbfl_memory_device *)data;
142 
143 	if (device->pos >= device->length) {
144 		/* reallocate buffer */
145 		int newlen;
146 		unsigned char *tmp;
147 
148 		newlen = device->length + device->allocsz;
149 		tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
150 		if (tmp == NULL) {
151 			return -1;
152 		}
153 		device->length = newlen;
154 		device->buffer = tmp;
155 	}
156 
157 	device->buffer[device->pos++] = (unsigned char)c;
158 	return c;
159 }
160 
161 int
mbfl_memory_device_output2(int c,void * data)162 mbfl_memory_device_output2(int c, void *data)
163 {
164 	mbfl_memory_device *device = (mbfl_memory_device *)data;
165 
166 	if ((device->pos + 2) >= device->length) {
167 		/* reallocate buffer */
168 		int newlen;
169 		unsigned char *tmp;
170 
171 		newlen = device->length + device->allocsz;
172 		tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
173 		if (tmp == NULL) {
174 			return -1;
175 		}
176 		device->length = newlen;
177 		device->buffer = tmp;
178 	}
179 
180 	device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
181 	device->buffer[device->pos++] = (unsigned char)(c & 0xff);
182 
183 	return c;
184 }
185 
186 int
mbfl_memory_device_output4(int c,void * data)187 mbfl_memory_device_output4(int c, void* data)
188 {
189 	mbfl_memory_device *device = (mbfl_memory_device *)data;
190 
191 	if ((device->pos + 4) >= device->length) {
192 		/* reallocate buffer */
193 		int newlen;
194 		unsigned char *tmp;
195 
196 		newlen = device->length + device->allocsz;
197 		tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
198 		if (tmp == NULL) {
199 			return -1;
200 		}
201 		device->length = newlen;
202 		device->buffer = tmp;
203 	}
204 
205 	device->buffer[device->pos++] = (unsigned char)((c >> 24) & 0xff);
206 	device->buffer[device->pos++] = (unsigned char)((c >> 16) & 0xff);
207 	device->buffer[device->pos++] = (unsigned char)((c >> 8) & 0xff);
208 	device->buffer[device->pos++] = (unsigned char)(c & 0xff);
209 
210 	return c;
211 }
212 
213 int
mbfl_memory_device_strcat(mbfl_memory_device * device,const char * psrc)214 mbfl_memory_device_strcat(mbfl_memory_device *device, const char *psrc)
215 {
216 	int len;
217 	unsigned char *w;
218 	const unsigned char *p;
219 
220 	len = 0;
221 	p = (const unsigned char*)psrc;
222 	while (*p) {
223 		p++;
224 		len++;
225 	}
226 
227 	if ((device->pos + len) >= device->length) {
228 		/* reallocate buffer */
229 		int newlen = device->length + (len + MBFL_MEMORY_DEVICE_ALLOC_SIZE)*sizeof(unsigned char);
230 		unsigned char *tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
231 		if (tmp == NULL) {
232 			return -1;
233 		}
234 		device->length = newlen;
235 		device->buffer = tmp;
236 	}
237 
238 	p = (const unsigned char*)psrc;
239 	w = &device->buffer[device->pos];
240 	device->pos += len;
241 	while (len > 0) {
242 		*w++ = *p++;
243 		len--;
244 	}
245 
246 	return len;
247 }
248 
249 int
mbfl_memory_device_strncat(mbfl_memory_device * device,const char * psrc,int len)250 mbfl_memory_device_strncat(mbfl_memory_device *device, const char *psrc, int len)
251 {
252 	unsigned char *w;
253 
254 	if ((device->pos + len) >= device->length) {
255 		/* reallocate buffer */
256 		int newlen = device->length + len + MBFL_MEMORY_DEVICE_ALLOC_SIZE;
257 		unsigned char *tmp = (unsigned char *)mbfl_realloc((void *)device->buffer, newlen*sizeof(unsigned char));
258 		if (tmp == NULL) {
259 			return -1;
260 		}
261 		device->length = newlen;
262 		device->buffer = tmp;
263 	}
264 
265 	w = &device->buffer[device->pos];
266 	device->pos += len;
267 	while (len > 0) {
268 		*w++ = *psrc++;
269 		len--;
270 	}
271 
272 	return len;
273 }
274 
275 int
mbfl_memory_device_devcat(mbfl_memory_device * dest,mbfl_memory_device * src)276 mbfl_memory_device_devcat(mbfl_memory_device *dest, mbfl_memory_device *src)
277 {
278 	int n;
279 	unsigned char *p, *w;
280 
281 	if ((dest->pos + src->pos) >= dest->length) {
282 		/* reallocate buffer */
283 		int newlen = dest->length + src->pos + MBFL_MEMORY_DEVICE_ALLOC_SIZE;
284 		unsigned char *tmp = (unsigned char *)mbfl_realloc((void *)dest->buffer, newlen*sizeof(unsigned char));
285 		if (tmp == NULL) {
286 			return -1;
287 		}
288 		dest->length = newlen;
289 		dest->buffer = tmp;
290 	}
291 
292 	p = src->buffer;
293 	w = &dest->buffer[dest->pos];
294 	n = src->pos;
295 	dest->pos += n;
296 	while (n > 0) {
297 		*w++ = *p++;
298 		n--;
299 	}
300 
301 	return n;
302 }
303 
304 void
mbfl_wchar_device_init(mbfl_wchar_device * device)305 mbfl_wchar_device_init(mbfl_wchar_device *device)
306 {
307 	if (device) {
308 		device->buffer = (unsigned int *)0;
309 		device->length = 0;
310 		device->pos= 0;
311 		device->allocsz = MBFL_MEMORY_DEVICE_ALLOC_SIZE;
312 	}
313 }
314 
315 void
mbfl_wchar_device_clear(mbfl_wchar_device * device)316 mbfl_wchar_device_clear(mbfl_wchar_device *device)
317 {
318 	if (device) {
319 		if (device->buffer) {
320 			mbfl_free(device->buffer);
321 		}
322 		device->buffer = (unsigned int*)0;
323 		device->length = 0;
324 		device->pos = 0;
325 	}
326 }
327 
328 int
mbfl_wchar_device_output(int c,void * data)329 mbfl_wchar_device_output(int c, void *data)
330 {
331 	mbfl_wchar_device *device = (mbfl_wchar_device *)data;
332 
333 	if (device->pos >= device->length) {
334 		/* reallocate buffer */
335 		int newlen;
336 		unsigned int *tmp;
337 
338 		newlen = device->length + device->allocsz;
339 		tmp = (unsigned int *)mbfl_realloc((void *)device->buffer, newlen*sizeof(int));
340 		if (tmp == NULL) {
341 			return -1;
342 		}
343 		device->length = newlen;
344 		device->buffer = tmp;
345 	}
346 
347 	device->buffer[device->pos++] = c;
348 
349 	return c;
350 }
351