Skip to content
Newer
Older
100644 425 lines (331 sloc) 8.8 KB
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
1 #include "imager.h"
50c7538 Move freetype 2 support into its own module
Tony Cook authored Sep 25, 2010
2 #include "imageri.h"
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
3 #include <stdlib.h>
4 #ifndef _MSC_VER
5 #include <unistd.h>
6 #endif
7
8
9 /* FIXME: make allocation dynamic */
10
11
12 #ifdef IMAGER_DEBUG_MALLOC
13
2ff8ed3 Added more logging to quant.c. Removed global variables from gif.c.
Arnar Mar Hrafnkelsson authored May 14, 2001
14 #define MAXMAL 102400
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
15 #define MAXDESC 65
16
a743c0a Removed a bunch of unused variables and fixed an attempt to print out a
Arnar Mar Hrafnkelsson authored May 14, 2001
17 #define UNDRRNVAL 10
18 #define OVERRNVAL 10
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
19
20 #define PADBYTE 0xaa
21
22
23 static int malloc_need_init = 1;
24
25 typedef struct {
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
26 void* ptr;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
27 size_t size;
335078f @tonycoz [rt #70388] debug mymalloc() no longer builds a string, just stores t…
authored Mar 10, 2012
28 const char *file;
29 int line;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
30 } malloc_entry;
31
32 malloc_entry malloc_pointers[MAXMAL];
33
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
34
35
36
37 /* Utility functions */
38
39
40 static
41 void
aea697a fix errors and some warnings on Win32
Tony Cook authored Nov 1, 2001
42 malloc_init(void) {
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
43 int i;
44 for(i=0; i<MAXMAL; i++) malloc_pointers[i].ptr = NULL;
45 malloc_need_init = 0;
46 atexit(malloc_state);
47 }
48
49
50 static
51 int
52 find_ptr(void *p) {
53 int i;
54 for(i=0;i<MAXMAL;i++)
55 if (malloc_pointers[i].ptr == p)
56 return i;
57 return -1;
58 }
59
60
61 /* Takes a pointer to real start of array,
62 * sets the entries in the table, returns
63 * the offset corrected pointer */
64
65 static
66 void *
67 set_entry(int i, char *buf, size_t size, char *file, int line) {
68 memset( buf, PADBYTE, UNDRRNVAL );
69 memset( &buf[UNDRRNVAL+size], PADBYTE, OVERRNVAL );
70 buf += UNDRRNVAL;
71 malloc_pointers[i].ptr = buf;
72 malloc_pointers[i].size = size;
335078f @tonycoz [rt #70388] debug mymalloc() no longer builds a string, just stores t…
authored Mar 10, 2012
73 malloc_pointers[i].file = file;
74 malloc_pointers[i].line = line;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
75 return buf;
76 }
77
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
78 void
aea697a fix errors and some warnings on Win32
Tony Cook authored Nov 1, 2001
79 malloc_state(void) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
80 int i;
81 size_t total = 0;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
82
cd4b0b2 have error.c free a non-leak when using the debug malloc
Tony Cook authored Nov 1, 2001
83 i_clear_error();
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
84 mm_log((0,"malloc_state()\n"));
85 bndcheck_all();
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
86 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr != NULL) {
335078f @tonycoz [rt #70388] debug mymalloc() no longer builds a string, just stores t…
authored Mar 10, 2012
87 mm_log((0,"%d: %lu (%p) : %s (%d)\n", i, (unsigned long)malloc_pointers[i].size, malloc_pointers[i].ptr, malloc_pointers[i].file, malloc_pointers[i].line));
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
88 total += malloc_pointers[i].size;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
89 }
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
90 if (total == 0) mm_log((0,"No memory currently used!\n"))
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
91 else mm_log((0,"total: %lu\n", (unsigned long)total));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
92 }
93
94
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
95
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
96 void*
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
97 mymalloc_file_line(size_t size, char* file, int line) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
98 char *buf;
99 int i;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
100 if (malloc_need_init) malloc_init();
101
102 /* bndcheck_all(); Uncomment for LOTS OF THRASHING */
103
104 if ( (i = find_ptr(NULL)) < 0 ) {
105 mm_log((0,"more than %d segments allocated at %s (%d)\n", MAXMAL, file, line));
106 exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
107 }
108
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
109 if ( (buf = malloc(size+UNDRRNVAL+OVERRNVAL)) == NULL ) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
110 mm_log((1,"Unable to allocate %ld for %s (%i)\n", (long)size, file, line));
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
111 exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
112 }
113
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
114 buf = set_entry(i, buf, size, file, line);
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
115 mm_log((1,"mymalloc_file_line: slot <%d> %ld bytes allocated at %p for %s (%d)\n", i, (long)size, buf, file, line));
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
116 return buf;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
117 }
118
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
119 void *
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
120 (mymalloc)(size_t size) {
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
121 return mymalloc_file_line(size, "unknown", 0);
122 }
123
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
124 void*
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
125 myrealloc_file_line(void *ptr, size_t newsize, char* file, int line) {
126 char *buf;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
127 int i;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
128
129 if (malloc_need_init) malloc_init();
130 /* bndcheck_all(); ACTIVATE FOR LOTS OF THRASHING */
131
132 if (!ptr) {
133 mm_log((1, "realloc called with ptr = NULL, sending request to malloc\n"));
134 return mymalloc_file_line(newsize, file, line);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
135 }
136
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
137 if (!newsize) {
138 mm_log((1, "newsize = 0, sending request to free\n"));
139 myfree_file_line(ptr, file, line);
140 return NULL;
141 }
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
142
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
143 if ( (i = find_ptr(ptr)) == -1) {
144 mm_log((0, "Unable to find %p in realloc for %s (%i)\n", ptr, file, line));
145 exit(3);
146 }
147
aea697a fix errors and some warnings on Win32
Tony Cook authored Nov 1, 2001
148 if ( (buf = realloc(((char *)ptr)-UNDRRNVAL, UNDRRNVAL+OVERRNVAL+newsize)) == NULL ) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
149 mm_log((1,"Unable to reallocate %ld bytes at %p for %s (%i)\n", (long)
150 newsize, ptr, file, line));
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
151 exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
152 }
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
153
154 buf = set_entry(i, buf, newsize, file, line);
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
155 mm_log((1,"realloc_file_line: slot <%d> %ld bytes allocated at %p for %s (%d)\n", i, (long)newsize, buf, file, line));
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
156 return buf;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
157 }
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
158
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
159 void *
160 (myrealloc)(void *ptr, size_t newsize) {
161 return myrealloc_file_line(ptr, newsize, "unknown", 0);
162 }
163
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
164 static
165 void
166 bndcheck(int idx) {
167 int i;
168 size_t s = malloc_pointers[idx].size;
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
169 unsigned char *pp = malloc_pointers[idx].ptr;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
170 if (!pp) {
171 mm_log((1, "bndcheck: No pointer in slot %d\n", idx));
172 return;
173 }
174
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
175 for(i=0;i<UNDRRNVAL;i++) {
176 if (pp[-(1+i)] != PADBYTE)
177 mm_log((1,"bndcheck: UNDERRUN OF %d bytes detected: slot = %d, point = %p, size = %ld\n", i+1, idx, pp, (long)s ));
178 }
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
179
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
180 for(i=0;i<OVERRNVAL;i++) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
181 if (pp[s+i] != PADBYTE)
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
182 mm_log((1,"bndcheck: OVERRUN OF %d bytes detected: slot = %d, point = %p, size = %ld\n", i+1, idx, pp, (long)s ));
183 }
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
184 }
185
186 void
187 bndcheck_all() {
188 int idx;
189 mm_log((1, "bndcheck_all()\n"));
76ff75b tga.c should now support reading/writing all variants of targa files.…
Arnar Mar Hrafnkelsson authored Oct 22, 2001
190 for(idx=0; idx<MAXMAL; idx++)
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
191 if (malloc_pointers[idx].ptr)
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
192 bndcheck(idx);
193 }
194
195 void
196 myfree_file_line(void *p, char *file, int line) {
197 char *pp = p;
198 int match = 0;
199 int i;
e2f09cf myfree_file_line no longer treats freeing a NULL pointer as an error
Tony Cook authored May 5, 2008
200
201 if (p == NULL)
202 return;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
203
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
204 for(i=0; i<MAXMAL; i++) if (malloc_pointers[i].ptr == p) {
335078f @tonycoz [rt #70388] debug mymalloc() no longer builds a string, just stores t…
authored Mar 10, 2012
205 mm_log((1,"myfree_file_line: pointer %i (%s (%d)) freed at %s (%i)\n", i, malloc_pointers[i].file, malloc_pointers[i].line, file, line));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
206 bndcheck(i);
fe6163b Various Changes to the memory accounting code, myrealloc() added.
Arnar Mar Hrafnkelsson authored Oct 22, 2001
207 malloc_pointers[i].ptr = NULL;
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
208 match++;
209 }
97c4eff merge write to gif tags updates
Tony Cook authored Mar 6, 2002
210
211 mm_log((1, "myfree_file_line: freeing address %p (real %p)\n", pp, pp-UNDRRNVAL));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
212
213 if (match != 1) {
f1ac502 support for generic fills for box and arc, with solid, hatched
Tony Cook authored Sep 1, 2001
214 mm_log((1, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line));
aea697a fix errors and some warnings on Win32
Tony Cook authored Nov 1, 2001
215 fprintf(stderr, "myfree_file_line: INCONSISTENT REFCOUNT %d at %s (%i)\n", match, file, line);
a73aeb5 Fixed most outstanding memory leaks that are revealed in the test cases.
Arnar Mar Hrafnkelsson authored Nov 1, 2001
216 exit(255);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
217 }
218
219
220 free(pp-UNDRRNVAL);
221 }
222
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
223 void
224 (myfree)(void *block) {
225 myfree_file_line(block, "unknown", 0);
226 }
227
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
228 #else
229
230 void
231 malloc_state() {
232 }
233
234 void*
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
235 mymalloc(size_t size) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
236 void *buf;
237
a659442 - eliminate unused variables and static functions
Tony Cook authored Jan 24, 2005
238 if (size < 0) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
239 fprintf(stderr, "Attempt to allocate size %ld\n", (long)size);
a659442 - eliminate unused variables and static functions
Tony Cook authored Jan 24, 2005
240 exit(3);
241 }
242
a743c0a Removed a bunch of unused variables and fixed an attempt to print out a
Arnar Mar Hrafnkelsson authored May 14, 2001
243 if ( (buf = malloc(size)) == NULL ) {
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
244 mm_log((1, "mymalloc: unable to malloc %ld\n", (long)size));
245 fprintf(stderr,"Unable to malloc %ld.\n", (long)size); exit(3);
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
246 }
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
247 mm_log((1, "mymalloc(size %ld) -> %p\n", (long)size, buf));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
248 return buf;
249 }
250
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
251 void *
252 mymalloc_file_line(size_t size, char *file, int line) {
253 return mymalloc(size);
254 }
255
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
256 void
257 myfree(void *p) {
dd55acc Switched all of jpeg to iolayer functions, removed older functions an…
Arnar Mar Hrafnkelsson authored Jun 8, 2001
258 mm_log((1, "myfree(p %p)\n", p));
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
259 free(p);
260 }
261
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
262 void
263 myfree_file_line(void *p, char *file, int line) {
264 myfree(p);
265 }
266
faa9b3e Egads
Tony Cook authored Aug 13, 2001
267 void *
268 myrealloc(void *block, size_t size) {
269 void *result;
270
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
271 mm_log((1, "myrealloc(block %p, size %ld)\n", block, (long)size));
faa9b3e Egads
Tony Cook authored Aug 13, 2001
272 if ((result = realloc(block, size)) == NULL) {
273 mm_log((1, "myrealloc: out of memory\n"));
274 fprintf(stderr, "Out of memory.\n");
275 exit(3);
276 }
277 return result;
278 }
279
e310e5f - more memory allocation integer overflow auditing
Tony Cook authored Feb 17, 2006
280 void *
281 myrealloc_file_line(void *block, size_t newsize, char *file, int size) {
282 return myrealloc(block, newsize);
283 }
284
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
285 #endif /* IMAGER_MALLOC_DEBUG */
286
287
288
289
8047cbb Added memory pools to make clean up of temporary buffers simpler.
Arnar Mar Hrafnkelsson authored Dec 28, 2001
290 /* memory pool implementation */
291
292 void
293 i_mempool_init(i_mempool *mp) {
294 mp->alloc = 10;
295 mp->used = 0;
296 mp->p = mymalloc(sizeof(void*)*mp->alloc);
297 }
298
299 void
300 i_mempool_extend(i_mempool *mp) {
301 mp->p = myrealloc(mp->p, mp->alloc * 2);
302 mp->alloc *=2;
303 }
304
305 void *
306 i_mempool_alloc(i_mempool *mp, size_t size) {
307 if (mp->used == mp->alloc) i_mempool_extend(mp);
308 mp->p[mp->used] = mymalloc(size);
309 mp->used++;
310 return mp->p[mp->used-1];
311 }
312
313
314 void
315 i_mempool_destroy(i_mempool *mp) {
316 unsigned int i;
317 for(i=0; i<mp->used; i++) myfree(mp->p[i]);
318 myfree(mp->p);
319 }
320
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
321
322
5473b91 Cleaned up io.h, io.c which had functions not used any more, removed …
Arnar Mar Hrafnkelsson authored Oct 23, 2001
323 /* Should these really be here? */
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
324
325 #undef min
326 #undef max
327
50c7538 Move freetype 2 support into its own module
Tony Cook authored Sep 25, 2010
328 i_img_dim
329 i_minx(i_img_dim a, i_img_dim b) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
330 if (a<b) return a; else return b;
331 }
332
50c7538 Move freetype 2 support into its own module
Tony Cook authored Sep 25, 2010
333 i_img_dim
334 i_maxx(i_img_dim a, i_img_dim b) {
02d1d62 Initial revision
Arnar Mar Hrafnkelsson authored Mar 25, 2001
335 if (a>b) return a; else return b;
336 }
337
4f68b48 support UTF with Freetype 1.x
Tony Cook authored Apr 18, 2002
338
339 struct utf8_size {
340 int mask, expect;
341 int size;
342 };
343
344 struct utf8_size utf8_sizes[] =
345 {
346 { 0x80, 0x00, 1 },
347 { 0xE0, 0xC0, 2 },
348 { 0xF0, 0xE0, 3 },
349 { 0xF8, 0xF0, 4 },
350 };
351
352 /*
50c7538 Move freetype 2 support into its own module
Tony Cook authored Sep 25, 2010
353 =item i_utf8_advance(char **p, size_t *len)
4f68b48 support UTF with Freetype 1.x
Tony Cook authored Apr 18, 2002
354
50c7538 Move freetype 2 support into its own module
Tony Cook authored Sep 25, 2010
355 Retrieve a C<UTF-8> character from the stream.
4f68b48 support UTF with Freetype 1.x
Tony Cook authored Apr 18, 2002
356
357 Modifies *p and *len to indicate the consumed characters.
358
50c7538 Move freetype 2 support into its own module
Tony Cook authored Sep 25, 2010
359 This doesn't support the extended C<UTF-8> encoding used by later
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
360 versions of Perl. Since this is typically used to implement text
361 output by font drivers, the strings supplied shouldn't have such out
362 of range characters.
4f68b48 support UTF with Freetype 1.x
Tony Cook authored Apr 18, 2002
363
50c7538 Move freetype 2 support into its own module
Tony Cook authored Sep 25, 2010
364 This doesn't check that the C<UTF-8> character is using the shortest
855fe4d break out of the search loop early when we find the right utf8 code
Tony Cook authored Oct 25, 2006
365 possible representation.
366
8d14daa @tonycoz switch to using size_t and i_img_dim strictly
authored Jun 14, 2011
367 Returns ~0UL on failure.
368
4f68b48 support UTF with Freetype 1.x
Tony Cook authored Apr 18, 2002
369 =cut
370 */
371
855fe4d break out of the search loop early when we find the right utf8 code
Tony Cook authored Oct 25, 2006
372 unsigned long
718b8c9 move win32 font support in a new directory, and use Imager::Probe
Tony Cook authored Sep 16, 2010
373 i_utf8_advance(char const **p, size_t *len) {
4f68b48 support UTF with Freetype 1.x
Tony Cook authored Apr 18, 2002
374 unsigned char c;
375 int i, ci, clen = 0;
376 unsigned char codes[3];
377 if (*len == 0)
378 return ~0UL;
379 c = *(*p)++; --*len;
380
381 for (i = 0; i < sizeof(utf8_sizes)/sizeof(*utf8_sizes); ++i) {
382 if ((c & utf8_sizes[i].mask) == utf8_sizes[i].expect) {
383 clen = utf8_sizes[i].size;
855fe4d break out of the search loop early when we find the right utf8 code
Tony Cook authored Oct 25, 2006
384 break;
4f68b48 support UTF with Freetype 1.x
Tony Cook authored Apr 18, 2002
385 }
386 }
387 if (clen == 0 || *len < clen-1) {
388 --*p; ++*len;
389 return ~0UL;
390 }
391
392 /* check that each character is well formed */
393 i = 1;
394 ci = 0;
395 while (i < clen) {
396 if (((*p)[ci] & 0xC0) != 0x80) {
397 --*p; ++*len;
398 return ~0UL;
399 }
400 codes[ci] = (*p)[ci];
401 ++ci; ++i;
402 }
403 *p += clen-1; *len -= clen-1;
404 if (c & 0x80) {
405 if ((c & 0xE0) == 0xC0) {
406 return ((c & 0x1F) << 6) + (codes[0] & 0x3F);
407 }
408 else if ((c & 0xF0) == 0xE0) {
409 return ((c & 0x0F) << 12) | ((codes[0] & 0x3F) << 6) | (codes[1] & 0x3f);
410 }
411 else if ((c & 0xF8) == 0xF0) {
412 return ((c & 0x07) << 18) | ((codes[0] & 0x3F) << 12)
413 | ((codes[1] & 0x3F) << 6) | (codes[2] & 0x3F);
414 }
415 else {
416 *p -= clen; *len += clen;
417 return ~0UL;
418 }
419 }
420 else {
421 return c;
422 }
423 }
424
Something went wrong with that request. Please try again.