Debian Codegraph @

texk/xdvipsk/otflib/dpxutil.c

raw
1 /* This is xdvipsk, an eXtended version of dvips(k) by Tomas Rokicki.
2
3 Copyright (C) 2016 by VTeX Ltd (www.vtex.lt),
4 the xdvipsk project team - Sigitas Tolusis and Arunas Povilaitis.
5
6 Program original code copyright (C) 2007-2014 by Jin-Hwan Cho and
7 Shunsaku Hirata, the dvipdfmx project team.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 */
23
24 #include <config.h>
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31
32 #include "system.h"
33 #include "mem.h"
34 #include "error.h"
35
36 #include "dpxutil.h"
37
38 int
39 xtoi (char c)
40 {
41 if (c >= '0' && c <= '9')
42 return (c - '0');
43 else if (c >= 'a' && c <= 'f')
44 return (c - 'W');
45 else if (c >= 'A' && c <= 'F')
46 return (c - '7');
47 else
48 return -1;
49 }
50
51 int
52 sputx (unsigned char c, char **s, char *end)
53 {
54 char hi = (c >> 4), lo = c & 0x0f;
55
56 if (*s + 2 > end)
57 ERROR("Buffer overflow.");
58 **s = (hi < 10) ? hi + '0' : hi + '7';
59 *(*s+1) = (lo < 10) ? lo + '0' : lo + '7';
60 *s += 2;
61
62 return 2;
63 }
64
65 int
66 getxpair (unsigned char **s)
67 {
68 int hi, lo;
69 hi = xtoi(**s);
70 if (hi < 0)
71 return hi;
72 (*s)++;
73 lo = xtoi(**s);
74 if (lo < 0)
75 return lo;
76 (*s)++;
77 return ((hi << 4)| lo);
78 }
79
80 int
81 putxpair (unsigned char c, char **s)
82 {
83 char hi = (c >> 4), lo = c & 0x0f;
84
85 **s = (hi < 10) ? hi + '0' : hi + '7';
86 *(*s+1) = (lo < 10) ? lo + '0' : lo + '7';
87 *s += 2;
88
89 return 2;
90 }
91
92 /* Overflowed value is set to invalid char. */
93 unsigned char
94 ostrtouc (unsigned char **inbuf, unsigned char *inbufend, unsigned char *valid)
95 {
96 unsigned char *cur = *inbuf;
97 unsigned int val = 0;
98
99 while (cur < inbufend && cur < *inbuf + 3 &&
100 (*cur >= '0' && *cur <= '7')) {
101 val = (val << 3) | (*cur - '0');
102 cur++;
103 }
104 if (val > 255 || cur == *inbuf)
105 *valid = 0;
106 else
107 *valid = 1;
108
109 *inbuf = cur;
110 return (unsigned char) val;
111 }
112
113 unsigned char
114 esctouc (unsigned char **inbuf, unsigned char *inbufend, unsigned char *valid)
115 {
116 unsigned char unescaped, escaped;
117
118 escaped = **inbuf;
119 *valid = 1;
120 switch (escaped) {
121 /* Backslash, unbalanced paranthes */
122 case '\\': case ')': case '(':
123 unescaped = escaped;
124 (*inbuf)++;
125 break;
126 /* Other escaped char */
127 case 'n': unescaped = '\n'; (*inbuf)++; break;
128 case 'r': unescaped = '\r'; (*inbuf)++; break;
129 case 't': unescaped = '\t'; (*inbuf)++; break;
130 case 'b': unescaped = '\b'; (*inbuf)++; break;
131 case 'f': unescaped = '\f'; (*inbuf)++; break;
132 /*
133 * An end-of-line marker preceeded by backslash is not part of a
134 * literal string
135 */
136 case '\r':
137 unescaped = 0;
138 *valid = 0;
139 *inbuf += (*inbuf < inbufend - 1 && *(*inbuf+1) == '\n') ? 2 : 1;
140 break;
141 case '\n':
142 unescaped = 0;
143 *valid = 0;
144 (*inbuf)++;
145 break;
146 /* Possibly octal notion */
147 default:
148 unescaped = ostrtouc(inbuf, inbufend, valid);
149 }
150
151 return unescaped;
152 }
153
154 void
155 skip_white_spaces (unsigned char **s, unsigned char *endptr)
156 {
157 while (*s < endptr)
158 if (!is_space(**s))
159 break;
160 else
161 (*s)++;
162 }
163
164 void
165 ht_init_table (struct ht_table *ht, hval_free_func hval_free_fn)
166 {
167 int i;
168
169 ASSERT(ht);
170
171 for (i = 0; i < HASH_TABLE_SIZE; i++) {
172 ht->table[i] = NULL;
173 }
174 ht->count = 0;
175 ht->hval_free_fn = hval_free_fn;
176 }
177
178 void
179 ht_clear_table (struct ht_table *ht)
180 {
181 int i;
182
183 ASSERT(ht);
184
185 for (i = 0; i < HASH_TABLE_SIZE; i++) {
186 struct ht_entry *hent, *next;
187
188 hent = ht->table[i];
189 while (hent) {
190 if (hent->value && ht->hval_free_fn) {
191 ht->hval_free_fn(hent->value);
192 }
193 hent->value = NULL;
194 if (hent->key) {
195 RELEASE(hent->key);
196 }
197 hent->key = NULL;
198 next = hent->next;
199 RELEASE(hent);
200 hent = next;
201 }
202 ht->table[i] = NULL;
203 }
204 ht->count = 0;
205 ht->hval_free_fn = NULL;
206 }
207
208 long ht_table_size (struct ht_table *ht)
209 {
210 ASSERT(ht);
211
212 return ht->count;
213 }
214
215 static unsigned int
216 get_hash (const void *key, int keylen)
217 {
218 unsigned int hkey = 0;
219 int i;
220
221 for (i = 0; i < keylen; i++) {
222 hkey = (hkey << 5) + hkey + ((const char *)key)[i];
223 }
224
225 return (hkey % HASH_TABLE_SIZE);
226 }
227
228 void *
229 ht_lookup_table (struct ht_table *ht, const void *key, int keylen)
230 {
231 struct ht_entry *hent;
232 unsigned int hkey;
233
234 ASSERT(ht && key);
235
236 hkey = get_hash(key, keylen);
237 hent = ht->table[hkey];
238 while (hent) {
239 if (hent->keylen == keylen &&
240 !memcmp(hent->key, key, keylen)) {
241 return hent->value;
242 }
243 hent = hent->next;
244 }
245
246 return NULL;
247 }
248
249 int
250 ht_remove_table (struct ht_table *ht,
251 const void *key, int keylen)
252 /* returns 1 if the element was found and removed and 0 otherwise */
253 {
254 struct ht_entry *hent, *prev;
255 unsigned int hkey;
256
257 ASSERT(ht && key);
258
259 hkey = get_hash(key, keylen);
260 hent = ht->table[hkey];
261 prev = NULL;
262 while (hent) {
263 if (hent->keylen == keylen &&
264 !memcmp(hent->key, key, keylen)) {
265 break;
266 }
267 prev = hent;
268 hent = hent->next;
269 }
270 if (hent) {
271 if (hent->key)
272 RELEASE(hent->key);
273 hent->key = NULL;
274 hent->keylen = 0;
275 if (hent->value && ht->hval_free_fn) {
276 ht->hval_free_fn(hent->value);
277 }
278 hent->value = NULL;
279 if (prev) {
280 prev->next = hent->next;
281 } else {
282 ht->table[hkey] = hent->next;
283 }
284 RELEASE(hent);
285 ht->count--;
286 return 1;
287 } else
288 return 0;
289 }
290
291 /* replace... */
292 void
293 ht_insert_table (struct ht_table *ht,
294 const void *key, int keylen, void *value)
295 {
296 struct ht_entry *hent, *prev;
297 unsigned int hkey;
298
299 ASSERT(ht && key);
300
301 hkey = get_hash(key, keylen);
302 hent = ht->table[hkey];
303 prev = NULL;
304 while (hent) {
305 if (hent->keylen == keylen &&
306 !memcmp(hent->key, key, keylen)) {
307 break;
308 }
309 prev = hent;
310 hent = hent->next;
311 }
312 if (hent) {
313 if (hent->value && ht->hval_free_fn)
314 ht->hval_free_fn(hent->value);
315 hent->value = value;
316 } else {
317 hent = NEW(1, struct ht_entry);
318 hent->key = NEW(keylen, char);
319 memcpy(hent->key, key, keylen);
320 hent->keylen = keylen;
321 hent->value = value;
322 hent->next = NULL;
323 if (prev) {
324 prev->next = hent;
325 } else {
326 ht->table[hkey] = hent;
327 }
328 ht->count++;
329 }
330 }
331
332 void
333 ht_append_table (struct ht_table *ht,
334 const void *key, int keylen, void *value)
335 {
336 struct ht_entry *hent, *last;
337 unsigned int hkey;
338
339 hkey = get_hash(key, keylen);
340 hent = ht->table[hkey];
341 if (!hent) {
342 hent = NEW(1, struct ht_entry);
343 ht->table[hkey] = hent;
344 } else {
345 while (hent) {
346 last = hent;
347 hent = hent->next;
348 }
349 hent = NEW(1, struct ht_entry);
350 last->next = hent;
351 }
352 hent->key = NEW(keylen, char);
353 memcpy(hent->key, key, keylen);
354 hent->keylen = keylen;
355 hent->value = value;
356 hent->next = NULL;
357
358 ht->count++;
359 }
360
361 int
362 ht_set_iter (struct ht_table *ht, struct ht_iter *iter)
363 {
364 int i;
365
366 ASSERT(ht && ht->table && iter);
367
368 for (i = 0; i < HASH_TABLE_SIZE; i++) {
369 if (ht->table[i]) {
370 iter->index = i;
371 iter->curr = ht->table[i];
372 iter->hash = ht;
373 return 0;
374 }
375 }
376
377 return -1;
378 }
379
380 void
381 ht_clear_iter (struct ht_iter *iter)
382 {
383 if (iter) {
384 iter->index = HASH_TABLE_SIZE;
385 iter->curr = NULL;
386 iter->hash = NULL;
387 }
388 }
389
390 char *
391 ht_iter_getkey (struct ht_iter *iter, int *keylen)
392 {
393 struct ht_entry *hent;
394
395 hent = (struct ht_entry *) iter->curr;
396 if (iter && hent) {
397 *keylen = hent->keylen;
398 return hent->key;
399 } else {
400 *keylen = 0;
401 return NULL;
402 }
403 }
404
405 void *
406 ht_iter_getval (struct ht_iter *iter)
407 {
408 struct ht_entry *hent;
409
410 hent = (struct ht_entry *) iter->curr;
411 if (iter && hent) {
412 return hent->value;
413 } else {
414 return NULL;
415 }
416 }
417
418 int
419 ht_iter_next (struct ht_iter *iter)
420 {
421 struct ht_entry *hent;
422 struct ht_table *ht;
423
424 ASSERT(iter);
425
426 ht = iter->hash;
427 hent = (struct ht_entry *) iter->curr;
428 hent = hent->next;
429 while (!hent &&
430 ++iter->index < HASH_TABLE_SIZE) {
431 hent = ht->table[iter->index];
432 }
433 iter->curr = hent;
434
435 return (hent ? 0 : -1);
436 }
437
438
439 static int
440 read_c_escchar (char *r, const char **pp, const char *endptr)
441 {
442 int c = 0, l = 1;
443 const char *p = *pp;
444
445 switch (p[0]) {
446 case 'a' : c = '\a'; p++; break;
447 case 'b' : c = '\b'; p++; break;
448 case 'f' : c = '\f'; p++; break;
449 case 'n' : c = '\n'; p++; break;
450 case 'r' : c = '\r'; p++; break;
451 case 't' : c = '\t'; p++; break;
452 case 'v' : c = '\v'; p++; break;
453 case '\\': case '?': case '\'': case '\"':
454 c = p[0]; p++;
455 break;
456 case '\n': l = 0; p++; break;
457 case '\r':
458 {
459 p++;
460 if (p < endptr && p[0] == '\n')
461 p++;
462 l = 0;
463 }
464 break;
465 case '0': case '1': case '2': case '3':
466 case '4': case '5': case '6': case '7':
467 {
468 int i;
469 for (c = 0, i = 0;
470 i < 3 && p < endptr &&
471 p[0] >= '0' && p[0] <= '7'; i++, p++)
472 c = (c << 3) + (p[0] - '0');
473 }
474 break;
475 case 'x':
476 {
477 int i;
478 for (c = 0, i = 0, p++;
479 i < 2 && p < endptr && isxdigit((unsigned char)p[0]);
480 i++, p++)
481 c = (c << 4) +
482 (isdigit((unsigned char)p[0]) ?
483 p[0] - '0' :
484 (islower((unsigned char)p[0]) ? p[0] - 'a' + 10: p[0] - 'A' + 10));
485 }
486 break;
487 default:
488 WARN("Unknown escape char sequence: \\%c", p[0]);
489 l = 0; p++;
490 break;
491 }
492
493 if (r)
494 *r = (char) c;
495 *pp = p;
496 return l;
497 }
498
499 #define C_QUOTE '"'
500 #define C_ESCAPE '\\'
501 static int
502 read_c_litstrc (char *q, int len, const char **pp, const char *endptr)
503 {
504 const char *p;
505 int l = 0;
506 #define Q_TERM 0
507 #define Q_CONT -1
508 #define Q_ERROR_UNTERM -1
509 #define Q_ERROR_INVAL -2
510 #define Q_ERROR_BUFF -3
511 int s = Q_CONT;
512
513 for (l = 0, p = *pp;
514 s == Q_CONT && p < endptr; ) {
515 switch (p[0]) {
516 case C_QUOTE:
517 s = Q_TERM; p++;
518 break;
519 case C_ESCAPE:
520 if (q && l == len)
521 s = Q_ERROR_BUFF;
522 else {
523 p++;
524 l += read_c_escchar(q ? &q[l] : NULL, &p, endptr);
525 }
526 break;
527 case '\n': case '\r':
528 s = Q_ERROR_INVAL;
529 break;
530 default:
531 if (q && l == len)
532 s = Q_ERROR_BUFF;
533 else {
534 if (!q)
535 l++;
536 else
537 q[l++] = p[0];
538 p++;
539 }
540 break;
541 }
542 }
543 if (s == Q_TERM) {
544 if (q && l == len)
545 s = Q_ERROR_BUFF;
546 else if (q)
547 q[l++] = '\0';
548 }
549
550 *pp = p;
551 return ((s == Q_TERM) ? l : s);
552 }
553
554 char *
555 parse_c_string (const char **pp, const char *endptr)
556 {
557 char *q = NULL;
558 const char *p = *pp;
559 int l = 0;
560
561 if (p >= endptr || p[0] != C_QUOTE)
562 return NULL;
563
564 p++;
565 l = read_c_litstrc(NULL, 0, &p, endptr);
566 if (l >= 0) {
567 q = NEW(l + 1, char);
568 p = *pp + 1;
569 l = read_c_litstrc(q, l + 1, &p, endptr);
570 }
571
572 *pp = p;
573 return q;
574 }
575
576 #define ISCNONDIGITS(c) ( \
577 (c) == '_' || \
578 ((c) >= 'a' && (c) <= 'z') || \
579 ((c) >= 'A' && (c) <= 'Z') \
580 )
581 #define ISCIDENTCHAR(c) ( \
582 ISCNONDIGITS((c)) || \
583 ((c) >= '0' && (c) <= '9') \
584 )
585
586 char *
587 parse_c_ident (const char **pp, const char *endptr)
588 {
589 char *q = NULL;
590 const char *p = *pp;
591 int n;
592
593 if (p >= endptr || !ISCNONDIGITS(*p))
594 return NULL;
595
596 for (n = 0; p < endptr && ISCIDENTCHAR(*p); p++, n++);
597 q = NEW(n + 1, char);
598 memcpy(q, *pp, n); q[n] = '\0';
599
600 *pp = p;
601 return q;
602 }
603
604 char *
605 parse_float_decimal (const char **pp, const char *endptr)
606 {
607 char *q = NULL;
608 const char *p = *pp;
609 int s = 0, n = 0;
610
611 if (p >= endptr)
612 return NULL;
613
614 if (p[0] == '+' || p[0] == '-')
615 p++;
616
617 /* 1. .01 001 001E-001 */
618 for (s = 0, n = 0; p < endptr && s >= 0; ) {
619 switch (p[0]) {
620 case '+': case '-':
621 if (s != 2)
622 s = -1;
623 else {
624 s = 3; p++;
625 }
626 break;
627 case '.':
628 if (s > 0)
629 s = -1;
630 else {
631 s = 1; p++;
632 }
633 break;
634 case '0': case '1': case '2': case '3': case '4':
635 case '5': case '6': case '7': case '8': case '9':
636 n++; p++;
637 break;
638 case 'E': case 'e':
639 if (n == 0 || s == 2)
640 s = -1;
641 else {
642 s = 2; p++;
643 }
644 break;
645 default:
646 s = -1;
647 break;
648 }
649 }
650
651 if (n != 0) {
652 n = (int) (p - *pp);
653 q = NEW(n + 1, char);
654 memcpy(q, *pp, n); q[n] = '\0';
655 }
656
657 *pp = p;
658 return q;
659 }