Debian Codegraph @

texk/dvipdfm-x/dpxutil.c

raw
1 /* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.
2
3 Copyright (C) 2002-2023 by Jin-Hwan Cho and Shunsaku Hirata,
4 the dvipdfmx project team.
5
6 Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
32
33 #include "system.h"
34 #include "mem.h"
35 #include "error.h"
36
37 #include "dpxutil.h"
38
39 int
40 xtoi (char c)
41 {
42 if (c >= '0' && c <= '9')
43 return (c - '0');
44 else if (c >= 'a' && c <= 'f')
45 return (c - 'W');
46 else if (c >= 'A' && c <= 'F')
47 return (c - '7');
48 else
49 return -1;
50 }
51
52 double
53 min4 (double x1, double x2, double x3, double x4)
54 {
55 double v = x1;
56 if (x2 < v) v = x2;
57 if (x3 < v) v = x3;
58 if (x4 < v) v = x4;
59 return v;
60 }
61
62 double
63 max4 (double x1, double x2, double x3, double x4)
64 {
65 double v = x1;
66 if (x2 > v) v = x2;
67 if (x3 > v) v = x3;
68 if (x4 > v) v = x4;
69 return v;
70 }
71
72 /* Duplicate from pdfparse.c */
73 static void
74 skip_white (const char **pp, const char *endptr)
75 {
76 while (*pp < endptr &&
77 (**pp == ' ' || **pp == '\t' || **pp == '\f' ||
78 **pp == '\r' || **pp == '\n' || **pp == '\0')) {
79 (*pp)++;
80 }
81 }
82
83 /* This need to allow 'true' prefix for unit and length value must be divided
84 * by current magnification.
85 */
86 int
87 dpx_util_read_length (double *vp, double mag, const char **pp, const char *endptr)
88 {
89 char *q;
90 const char *p = *pp;
91 double v, u = 1.0;
92 const char *_ukeys[] = {
93 #define K_UNIT__PT 0
94 #define K_UNIT__IN 1
95 #define K_UNIT__CM 2
96 #define K_UNIT__MM 3
97 #define K_UNIT__BP 4
98 #define K_UNIT__PC 5
99 #define K_UNIT__DD 6
100 #define K_UNIT__CC 7
101 #define K_UNIT__SP 8
102 "pt", "in", "cm", "mm", "bp", "pc", "dd", "cc", "sp",
103 NULL
104 };
105 int k, error = 0;
106
107 q = parse_float_decimal(&p, endptr);
108 if (!q) {
109 *vp = 0.0; *pp = p;
110 return -1;
111 }
112
113 v = atof(q);
114 RELEASE(q);
115
116 skip_white(&p, endptr);
117 q = parse_c_ident(&p, endptr);
118 if (q) {
119 char *qq = q; /* remember this for RELEASE, because q may be advanced */
120 if (strlen(q) >= strlen("true") &&
121 !memcmp(q, "true", strlen("true"))) {
122 u /= mag != 0.0 ? mag : 1.0; /* inverse magnify */
123 q += strlen("true");
124 }
125 if (strlen(q) == 0) { /* "true" was a separate word from the units */
126 RELEASE(qq);
127 skip_white(&p, endptr);
128 qq = q = parse_c_ident(&p, endptr);
129 }
130 if (q) {
131 for (k = 0; _ukeys[k] && strcmp(_ukeys[k], q); k++);
132 switch (k) {
133 case K_UNIT__PT: u *= 72.0 / 72.27; break;
134 case K_UNIT__IN: u *= 72.0; break;
135 case K_UNIT__CM: u *= 72.0 / 2.54 ; break;
136 case K_UNIT__MM: u *= 72.0 / 25.4 ; break;
137 case K_UNIT__BP: u *= 1.0 ; break;
138 case K_UNIT__PC: u *= 12.0 * 72.0 / 72.27 ; break;
139 case K_UNIT__DD: u *= 1238.0 / 1157.0 * 72.0 / 72.27 ; break;
140 case K_UNIT__CC: u *= 12.0 * 1238.0 / 1157.0 * 72.0 / 72.27 ; break;
141 case K_UNIT__SP: u *= 72.0 / (72.27 * 65536) ; break;
142 default:
143 WARN("Unknown unit of measure: %s", q);
144 error = -1;
145 break;
146 }
147 RELEASE(qq);
148 }
149 else {
150 WARN("Missing unit of measure after \"true\"");
151 error = -1;
152 }
153 }
154
155 *vp = v * u; *pp = p;
156 return error;
157 }
158
159 #if defined(_MSC_VER)
160 #define strtoll _strtoi64
161 #endif
162
163 /* If an environment variable SOURCE_DATE_EPOCH is correctly defined like
164 * SOURCE_DATE_EPOCH=1456304492, then returns this value, to be used as the
165 * 'current time', otherwise returns INVALID_EPOCH_VALUE (= (time_t)-1).
166 * In the case of Microsoft Visual Studio 2010, the value should be less
167 * than 32535291600.
168 */
169
170 time_t
171 dpx_util_get_unique_time_if_given(void)
172 {
173 const char *source_date_epoch;
174 int64_t epoch;
175 char *endptr;
176 time_t ret = INVALID_EPOCH_VALUE;
177
178 source_date_epoch = getenv("SOURCE_DATE_EPOCH");
179 if (source_date_epoch) {
180 errno = 0;
181 epoch = strtoll(source_date_epoch, &endptr, 10);
182 if (!(epoch < 0 || *endptr != '\0' || errno != 0)) {
183 ret = (time_t) epoch;
184 #if defined(_MSC_VER)
185 if (ret > 32535291599ULL)
186 ret = 32535291599ULL;
187 #endif
188 }
189 }
190 return ret;
191 }
192
193 /*
194 * Docinfo
195 */
196 #define TIME_STR_SIZE 30
197 int
198 dpx_util_format_asn_date (char *date_string, int need_timezone)
199 {
200 int32_t off, off_hours, off_mins;
201 time_t current_time;
202 struct tm bd_time, gmt;
203 size_t size;
204
205 current_time = dpx_util_get_unique_time_if_given();
206 if (current_time == INVALID_EPOCH_VALUE) {
207 time(&current_time);
208 bd_time = *localtime(&current_time);
209 } else {
210 bd_time = *gmtime(&current_time);
211 }
212
213 size = strftime(date_string, TIME_STR_SIZE, "D:%Y%m%d%H%M%S", &bd_time);
214 /* expected format: "YYYYmmddHHMMSS" */
215 if (size == 0) {
216 /* unexpected, contents of date_string is undefined */
217 date_string[0] = '\0';
218 return 0;
219 }
220
221 /* correction for seconds: %S can be in range 00..61,
222 the PDF reference expects 00..59,
223 therefore we map "60" and "61" to "59" */
224 if (date_string[14] == '6') {
225 date_string[14] = '5';
226 date_string[15] = '9';
227 date_string[16] = '\0'; /* for safety */
228 }
229
230 /* calculate the time zone offset in the same way as in texmfmp.c */
231 gmt = *gmtime(&current_time);
232 off = 60 * (bd_time.tm_hour - gmt.tm_hour) + bd_time.tm_min - gmt.tm_min;
233 if (bd_time.tm_year != gmt.tm_year) {
234 off += (bd_time.tm_year > gmt.tm_year) ? 1440 : -1440;
235 } else if (bd_time.tm_yday != gmt.tm_yday) {
236 off += (bd_time.tm_yday > gmt.tm_yday) ? 1440 : -1440;
237 }
238
239 if (need_timezone) {
240 if (off == 0) {
241 date_string[size++] = 'Z';
242 date_string[size] = 0;
243 } else {
244 off_hours = off / 60;
245 off_mins = abs(off - off_hours * 60);
246 sprintf(&date_string[size], "%+03d'%02d'", off_hours, off_mins);
247 }
248 } else {
249 date_string[size] = 0;
250 }
251
252 return (int)strlen(date_string);
253 }
254
255 void
256 skip_white_spaces (unsigned char **s, unsigned char *endptr)
257 {
258 while (*s < endptr)
259 if (!is_space(**s))
260 break;
261 else
262 (*s)++;
263 }
264
265 void
266 dpx_stack_init (dpx_stack *stack)
267 {
268 stack->size = 0;
269 stack->top = NULL;
270 stack->bottom = NULL;
271 }
272
273 void
274 dpx_stack_push (dpx_stack *stack, void *data)
275 {
276 stack_elem *elem;
277
278 ASSERT(stack);
279
280 elem = NEW(1, stack_elem);
281 elem->prev = stack->top;
282 elem->data = data;
283
284 stack->top = elem;
285 if (stack->size == 0)
286 stack->bottom = elem;
287
288 stack->size++;
289
290 return;
291 }
292
293 void *
294 dpx_stack_pop (dpx_stack *stack)
295 {
296 stack_elem *elem;
297 void *data;
298
299 ASSERT(stack);
300
301 if (stack->size == 0)
302 return NULL;
303
304 data = stack->top->data;
305 elem = stack->top;
306 stack->top = elem->prev;
307 if (stack->size == 1)
308 stack->bottom = NULL;
309 RELEASE(elem);
310
311 stack->size--;
312
313 return data;
314 }
315
316 void *
317 dpx_stack_top (dpx_stack *stack)
318 {
319 void *data;
320
321 ASSERT(stack);
322
323 if (stack->size == 0)
324 return NULL;
325
326 data = stack->top->data;
327
328 return data;
329 }
330
331 void *
332 dpx_stack_at (dpx_stack *stack, int pos)
333 {
334 void *data = NULL;
335 stack_elem *elem;
336
337 if (stack->size == 0)
338 return NULL;
339
340 elem = stack->top;
341 while (pos > 0) {
342 elem = elem->prev;
343 pos--;
344 }
345 if (elem)
346 data = elem->data;
347
348 return data;
349 }
350
351 void
352 dpx_stack_roll (dpx_stack *stack, int n, int j)
353 {
354 if (n > stack->size)
355 return;
356 if (n == 1)
357 return;
358 j = j % n;
359 if (j < 0)
360 j = n + j;
361 while (j-- > 0) {
362 int m = n;
363 stack_elem *elem, *prev, *top;
364
365 elem = top = stack->top;
366 while (--m > 0) {
367 elem = elem->prev;
368 }
369 prev = elem->prev;
370 stack->top = top->prev;
371 elem->prev = top;
372 top->prev = prev;
373 }
374 }
375
376 int
377 dpx_stack_depth (dpx_stack *stack)
378 {
379 ASSERT(stack);
380
381 return stack->size;
382 }
383
384 void
385 ht_init_table (struct ht_table *ht, hval_free_func hval_free_fn)
386 {
387 int i;
388
389 ASSERT(ht);
390
391 for (i = 0; i < HASH_TABLE_SIZE; i++) {
392 ht->table[i] = NULL;
393 }
394 ht->count = 0;
395 ht->hval_free_fn = hval_free_fn;
396 }
397
398 void
399 ht_clear_table (struct ht_table *ht)
400 {
401 int i;
402
403 ASSERT(ht);
404
405 for (i = 0; i < HASH_TABLE_SIZE; i++) {
406 struct ht_entry *hent, *next;
407
408 hent = ht->table[i];
409 while (hent) {
410 if (hent->value && ht->hval_free_fn) {
411 ht->hval_free_fn(hent->value);
412 }
413 hent->value = NULL;
414 if (hent->key) {
415 RELEASE(hent->key);
416 }
417 hent->key = NULL;
418 next = hent->next;
419 RELEASE(hent);
420 hent = next;
421 }
422 ht->table[i] = NULL;
423 }
424 ht->count = 0;
425 ht->hval_free_fn = NULL;
426 }
427
428 int ht_table_size (struct ht_table *ht)
429 {
430 ASSERT(ht);
431
432 return ht->count;
433 }
434
435 static unsigned int
436 get_hash (const void *key, int keylen)
437 {
438 unsigned int hkey = 0;
439 int i;
440
441 for (i = 0; i < keylen; i++) {
442 hkey = (hkey << 5) + hkey + ((const char *)key)[i];
443 }
444
445 return (hkey % HASH_TABLE_SIZE);
446 }
447
448 void *
449 ht_lookup_table (struct ht_table *ht, const void *key, int keylen)
450 {
451 struct ht_entry *hent;
452 unsigned int hkey;
453
454 ASSERT(ht && key);
455
456 hkey = get_hash(key, keylen);
457 hent = ht->table[hkey];
458 while (hent) {
459 if (hent->keylen == keylen &&
460 !memcmp(hent->key, key, keylen)) {
461 return hent->value;
462 }
463 hent = hent->next;
464 }
465
466 return NULL;
467 }
468
469 int
470 ht_remove_table (struct ht_table *ht,
471 const void *key, int keylen)
472 /* returns 1 if the element was found and removed and 0 otherwise */
473 {
474 struct ht_entry *hent, *prev;
475 unsigned int hkey;
476
477 ASSERT(ht && key);
478
479 hkey = get_hash(key, keylen);
480 hent = ht->table[hkey];
481 prev = NULL;
482 while (hent) {
483 if (hent->keylen == keylen &&
484 !memcmp(hent->key, key, keylen)) {
485 break;
486 }
487 prev = hent;
488 hent = hent->next;
489 }
490 if (hent) {
491 if (hent->key)
492 RELEASE(hent->key);
493 hent->key = NULL;
494 hent->keylen = 0;
495 if (hent->value && ht->hval_free_fn) {
496 ht->hval_free_fn(hent->value);
497 }
498 hent->value = NULL;
499 if (prev) {
500 prev->next = hent->next;
501 } else {
502 ht->table[hkey] = hent->next;
503 }
504 RELEASE(hent);
505 ht->count--;
506 return 1;
507 } else
508 return 0;
509 }
510
511 /* replace... */
512 void
513 ht_insert_table (struct ht_table *ht,
514 const void *key, int keylen, void *value)
515 {
516 struct ht_entry *hent, *prev;
517 unsigned int hkey;
518
519 ASSERT(ht && key);
520
521 hkey = get_hash(key, keylen);
522 hent = ht->table[hkey];
523 prev = NULL;
524 while (hent) {
525 if (hent->keylen == keylen &&
526 !memcmp(hent->key, key, keylen)) {
527 break;
528 }
529 prev = hent;
530 hent = hent->next;
531 }
532 if (hent) {
533 if (hent->value && ht->hval_free_fn)
534 ht->hval_free_fn(hent->value);
535 hent->value = value;
536 } else {
537 hent = NEW(1, struct ht_entry);
538 hent->key = NEW(keylen, char);
539 memcpy(hent->key, key, keylen);
540 hent->keylen = keylen;
541 hent->value = value;
542 hent->next = NULL;
543 if (prev) {
544 prev->next = hent;
545 } else {
546 ht->table[hkey] = hent;
547 }
548 ht->count++;
549 }
550 }
551
552 void
553 ht_append_table (struct ht_table *ht,
554 const void *key, int keylen, void *value)
555 {
556 struct ht_entry *hent, *last;
557 unsigned int hkey;
558
559 hkey = get_hash(key, keylen);
560 hent = ht->table[hkey];
561 if (!hent) {
562 hent = NEW(1, struct ht_entry);
563 ht->table[hkey] = hent;
564 } else {
565 while (hent) {
566 last = hent;
567 hent = hent->next;
568 }
569 hent = NEW(1, struct ht_entry);
570 last->next = hent;
571 }
572 hent->key = NEW(keylen, char);
573 memcpy(hent->key, key, keylen);
574 hent->keylen = keylen;
575 hent->value = value;
576 hent->next = NULL;
577
578 ht->count++;
579 }
580
581 int
582 ht_set_iter (struct ht_table *ht, struct ht_iter *iter)
583 {
584 int i;
585
586 ASSERT(ht && iter);
587
588 for (i = 0; i < HASH_TABLE_SIZE; i++) {
589 if (ht->table[i]) {
590 iter->index = i;
591 iter->curr = ht->table[i];
592 iter->hash = ht;
593 return 0;
594 }
595 }
596
597 return -1;
598 }
599
600 void
601 ht_clear_iter (struct ht_iter *iter)
602 {
603 if (iter) {
604 iter->index = HASH_TABLE_SIZE;
605 iter->curr = NULL;
606 iter->hash = NULL;
607 }
608 }
609
610 char *
611 ht_iter_getkey (struct ht_iter *iter, int *keylen)
612 {
613 struct ht_entry *hent;
614
615 hent = (struct ht_entry *) iter->curr;
616 if (iter && hent) {
617 *keylen = hent->keylen;
618 return hent->key;
619 } else {
620 *keylen = 0;
621 return NULL;
622 }
623 }
624
625 void *
626 ht_iter_getval (struct ht_iter *iter)
627 {
628 struct ht_entry *hent;
629
630 hent = (struct ht_entry *) iter->curr;
631 if (iter && hent) {
632 return hent->value;
633 } else {
634 return NULL;
635 }
636 }
637
638 int
639 ht_iter_next (struct ht_iter *iter)
640 {
641 struct ht_entry *hent;
642 struct ht_table *ht;
643
644 ASSERT(iter);
645
646 ht = iter->hash;
647 hent = (struct ht_entry *) iter->curr;
648 hent = hent->next;
649 while (!hent &&
650 ++iter->index < HASH_TABLE_SIZE) {
651 hent = ht->table[iter->index];
652 }
653 iter->curr = hent;
654
655 return (hent ? 0 : -1);
656 }
657
658
659 static int
660 read_c_escchar (char *r, const char **pp, const char *endptr)
661 {
662 int c = 0, l = 1;
663 const char *p = *pp;
664
665 switch (p[0]) {
666 case 'a' : c = '\a'; p++; break;
667 case 'b' : c = '\b'; p++; break;
668 case 'f' : c = '\f'; p++; break;
669 case 'n' : c = '\n'; p++; break;
670 case 'r' : c = '\r'; p++; break;
671 case 't' : c = '\t'; p++; break;
672 case 'v' : c = '\v'; p++; break;
673 case '\\': case '?': case '\'': case '\"':
674 c = p[0]; p++;
675 break;
676 case '\n': l = 0; p++; break;
677 case '\r':
678 {
679 p++;
680 if (p < endptr && p[0] == '\n')
681 p++;
682 l = 0;
683 }
684 break;
685 case '0': case '1': case '2': case '3':
686 case '4': case '5': case '6': case '7':
687 {
688 int i;
689 for (c = 0, i = 0;
690 i < 3 && p < endptr &&
691 p[0] >= '0' && p[0] <= '7'; i++, p++)
692 c = (c << 3) + (p[0] - '0');
693 }
694 break;
695 case 'x':
696 {
697 int i;
698 for (c = 0, i = 0, p++;
699 i < 2 && p < endptr && isxdigit((unsigned char)p[0]);
700 i++, p++)
701 c = (c << 4) +
702 (isdigit((unsigned char)p[0]) ?
703 p[0] - '0' :
704 (islower((unsigned char)p[0]) ? p[0] - 'a' + 10: p[0] - 'A' + 10));
705 }
706 break;
707 default:
708 WARN("Unknown escape char sequence: \\%c", p[0]);
709 l = 0; p++;
710 break;
711 }
712
713 if (r)
714 *r = (char) c;
715 *pp = p;
716 return l;
717 }
718
719 #define C_QUOTE '"'
720 #define C_ESCAPE '\\'
721 static int
722 read_c_litstrc (char *q, int len, const char **pp, const char *endptr)
723 {
724 const char *p;
725 int l = 0;
726 #define Q_TERM 0
727 #define Q_CONT -1
728 #define Q_ERROR_UNTERM -1
729 #define Q_ERROR_INVAL -2
730 #define Q_ERROR_BUFF -3
731 int s = Q_CONT;
732
733 for (l = 0, p = *pp;
734 s == Q_CONT && p < endptr; ) {
735 switch (p[0]) {
736 case C_QUOTE:
737 s = Q_TERM; p++;
738 break;
739 case C_ESCAPE:
740 if (q && l == len)
741 s = Q_ERROR_BUFF;
742 else {
743 p++;
744 l += read_c_escchar(q ? &q[l] : NULL, &p, endptr);
745 }
746 break;
747 case '\n': case '\r':
748 s = Q_ERROR_INVAL;
749 break;
750 default:
751 if (q && l == len)
752 s = Q_ERROR_BUFF;
753 else {
754 if (!q)
755 l++;
756 else
757 q[l++] = p[0];
758 p++;
759 }
760 break;
761 }
762 }
763 if (s == Q_TERM) {
764 if (q && l == len)
765 s = Q_ERROR_BUFF;
766 else if (q)
767 q[l++] = '\0';
768 }
769
770 *pp = p;
771 return ((s == Q_TERM) ? l : s);
772 }
773
774 char *
775 parse_c_string (const char **pp, const char *endptr)
776 {
777 char *q = NULL;
778 const char *p = *pp;
779 int l = 0;
780
781 if (p >= endptr || p[0] != C_QUOTE)
782 return NULL;
783
784 p++;
785 l = read_c_litstrc(NULL, 0, &p, endptr);
786 if (l >= 0) {
787 q = NEW(l + 1, char);
788 p = *pp + 1;
789 l = read_c_litstrc(q, l + 1, &p, endptr);
790 }
791
792 *pp = p;
793 return q;
794 }
795
796 #define ISCNONDIGITS(c) ( \
797 (c) == '_' || \
798 ((c) >= 'a' && (c) <= 'z') || \
799 ((c) >= 'A' && (c) <= 'Z') \
800 )
801 #define ISCIDENTCHAR(c) ( \
802 ISCNONDIGITS((c)) || \
803 ((c) >= '0' && (c) <= '9') \
804 )
805
806 char *
807 parse_c_ident (const char **pp, const char *endptr)
808 {
809 char *q = NULL;
810 const char *p = *pp;
811 int n;
812
813 if (p >= endptr || !ISCNONDIGITS(*p))
814 return NULL;
815
816 for (n = 0; p < endptr && ISCIDENTCHAR(*p); p++, n++);
817 q = NEW(n + 1, char);
818 memcpy(q, *pp, n); q[n] = '\0';
819
820 *pp = p;
821 return q;
822 }
823
824 char *
825 parse_float_decimal (const char **pp, const char *endptr)
826 {
827 char *q = NULL;
828 const char *p = *pp;
829 int s = 0, n = 0;
830
831 if (p >= endptr)
832 return NULL;
833
834 if (p[0] == '+' || p[0] == '-')
835 p++;
836
837 /* 1. .01 001 001E-001 */
838 for (s = 0, n = 0; p < endptr && s >= 0; ) {
839 switch (p[0]) {
840 case '+': case '-':
841 if (s != 2)
842 s = -1;
843 else {
844 s = 3; p++;
845 }
846 break;
847 case '.':
848 if (s > 0)
849 s = -1;
850 else {
851 s = 1; p++;
852 }
853 break;
854 case '0': case '1': case '2': case '3': case '4':
855 case '5': case '6': case '7': case '8': case '9':
856 n++; p++;
857 break;
858 case 'E': case 'e':
859 if (n == 0 || s == 2)
860 s = -1;
861 else {
862 s = 2; p++;
863 }
864 break;
865 default:
866 s = -1;
867 break;
868 }
869 }
870
871 if (n != 0) {
872 n = (int) (p - *pp);
873 q = NEW(n + 1, char);
874 memcpy(q, *pp, n); q[n] = '\0';
875 }
876
877 *pp = p;
878 return q;
879 }