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