summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/mpdir/utils.c
blob: 16de3f5a8d345475a0489d1182f1eef687f49fb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
/* Copyright (c) 2006 Taco Hoekwater <taco@elvenkind.com>

This file is part of MetaPost.

MetaPost is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

MetaPost is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MetaPost; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

$Id $
*/

#ifdef HAVE_ASPRINTF            /* asprintf is not defined in openbsd-compat.h, but in stdio.h */
#  include <stdio.h>
#endif
#include <sys/types.h>

#include <kpathsea/c-proto.h>
#include <kpathsea/c-stat.h>
#include <kpathsea/c-fopen.h>
#include <string.h>
#include <time.h>
#include <float.h>              /* for DBL_EPSILON */
#include "mplib.h"

/* define char_ptr, char_array & char_limit */
typedef char char_entry;
define_array (char);

#define Z_NULL  0  

typedef unsigned char  Byte;
typedef Byte  Bytef;

#define check_nprintf(size_get, size_want) \
    if ((unsigned)(size_get) >= (unsigned)(size_want)) \
        pdftex_fail ("snprintf failed: file %s, line %d", __FILE__, __LINE__);

/*@unused@*/
static const char perforce_id[] =
    "$Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/utils.c#24 $";

char *cur_file_name = NULL;
strnumber last_tex_string;
static char print_buf[PRINTF_BUF_SIZE];
static char *jobname_cstr = NULL;
static char *job_id_string = NULL;
extern string ptexbanner;       /* from web2c/lib/texmfmp.c */
extern string versionstring;    /* from web2c/lib/version.c */
extern KPSEDLL string kpathsea_version_string;  /* from kpathsea/version.c */

size_t last_ptr_index;          /* for use with alloc_array */



void pdf_puts (const char *s)
{    
  while (*s)
    fputc(*s++,psfile);
}

void pdf_printf (const char *fmt, ...)
{   
    va_list args;
    va_start (args, fmt);
    vsnprintf (print_buf, PRINTF_BUF_SIZE, fmt, args);
    pdf_puts (print_buf);
    va_end (args);
}

strnumber maketexstring (const char *s)
{
    size_t l;
    if (s == NULL || *s == 0)
        return getnullstr ();
    l = strlen (s);
    check_buf (poolptr + l, poolsize);
    while (l-- > 0)
        strpool[poolptr++] = *s++;
    last_tex_string = makestring ();
    return last_tex_string;
}

void mp_printf (const char *fmt, ...)
{
    va_list args;
    int saved_selector;
    saved_selector = selector;
    va_start (args, fmt);
    vsnprintf (print_buf, PRINTF_BUF_SIZE, fmt, args);
    selector = gettermandlogid();
    print (maketexstring (print_buf));
    flushstr (last_tex_string);
    xfflush (stdout);
    va_end (args);
    selector = saved_selector; /* ps_file_only, probably */
}

/* Helper for pdftex_fail. */
static void safe_print (const char *str)
{
    const char *c;
    for (c = str; *c; ++c)
        print (*c);
}

/* pdftex_fail may be called when a buffer overflow has happened/is
   happening, therefore may not call mktexstring.  However, with the
   current implementation it appears that error messages are misleading,
   possibly because pool overflows are detected too late. 
   
   The output format of this fuction must be the same as pdf_error in
   pdftex.web! */

void pdftex_fail (const char *fmt, ...)
{
    va_list args;
    va_start (args, fmt);
    fputs ("\nError: module writet1", stderr);
    if (cur_file_name)
        fprintf (stderr, " (file %s)", cur_file_name);
    fputs (": ", stderr);
    vsprintf (print_buf, fmt, args);
    fputs (print_buf, stderr);
    fputs
        ("\n ==> Fatal error occurred, the output PS file is not finished!\n",
         stderr);
    va_end (args);
    exit (1);
}

/* The output format of this fuction must be the same as pdf_warn in
   pdftex.web! */
void pdftex_warn (const char *fmt, ...)
{
    va_list args;
    va_start (args, fmt);
    fputs ("\nWarning: module writet1", stderr);
    if (cur_file_name)
        fprintf (stderr, " (file %s)", cur_file_name);
    fputs (": ", stderr);
    vsprintf (print_buf, fmt, args);
    fputs (print_buf, stderr);
    fputs ("\n", stderr);
    va_end (args);
}


static char *cstrbuf = NULL;

char *makecstring (integer s)
{
    char *p;
    static int allocsize;
    int allocgrow, i, l ;
    if (s==0)
      return "";
    l = strstart[nextstr[s]] - strstart[s];
	if (l==0)
	  return "";
    check_buf (l + 1, MAX_CSTRING_LEN);
    if (cstrbuf == NULL) {
        allocsize = l + 1;
        cstrbuf = xmallocarray (char, allocsize);
    } else if (l + 1 > allocsize) {
        allocgrow = allocsize * 0.2;
        if (l + 1 - allocgrow > allocsize)
            allocsize = l + 1;
        else if (allocsize < MAX_CSTRING_LEN - allocgrow)
            allocsize += allocgrow;
        else
            allocsize = MAX_CSTRING_LEN;
        cstrbuf = xrealloc (cstrbuf, sizeof (char) * allocsize);
    }
    p = cstrbuf;
    for (i = 0; i < l; i++)
        *p++ = strpool[i + strstart[s]];
    *p = 0;
    return cstrbuf;
}

size_t xfwrite (void *ptr, size_t size, size_t nmemb, FILE * stream)
{
    if (fwrite (ptr, size, nmemb, stream) != nmemb)
        pdftex_fail ("fwrite() failed");
    return nmemb;
}

int xfflush (FILE * stream)
{
    if (fflush (stream) != 0)
        pdftex_fail ("fflush() failed");
    return 0;
}

int xgetc (FILE * stream)
{
    int c = getc (stream);
    if (c < 0 && c != EOF)
        pdftex_fail ("getc() failed");
    return c;
}

int xputc (int c, FILE * stream)
{
    int i = putc (c, stream);
    if (i < 0)
        pdftex_fail ("putc() failed");
    return i;
}


scaled extxnoverd (scaled x, scaled n, scaled d)
{
    double r = (((double) x) * ((double) n)) / ((double) d);
    if (r > DBL_EPSILON)
        r += 0.5;
    else
        r -= 0.5;
    if (r >= (double) maxinteger || r <= -(double) maxinteger)
        pdftex_warn ("arithmetic: number too big");
    return (scaled) r;
}

/* Converts any string given in in in an allowed PDF string which can be
 * handled by printf et.al.: \ is escaped to \\, parenthesis are escaped and
 * control characters are octal encoded.
 * This assumes that the string does not contain any already escaped
 * characters!
 */
char *convertStringToPDFString (const char *in, int len)
{
    static char pstrbuf[MAX_PSTRING_LEN];
    char *out = pstrbuf;
    int i, j = 0, k;
    char buf[5];
    for (i = 0; i < len; i++) {
        check_buf (j + sizeof (buf), MAX_PSTRING_LEN);
        if (((unsigned char) in[i] < '!') || ((unsigned char) in[i] > '~')) {
            /* convert control characters into oct */
            k = snprintf (buf, sizeof (buf),
                          "\\%03o", (unsigned int) (unsigned char) in[i]);
            check_nprintf (k, sizeof (buf));
            out[j++] = buf[0];
            out[j++] = buf[1];
            out[j++] = buf[2];
            out[j++] = buf[3];
        } else if ((in[i] == '(') || (in[i] == ')')) {
            /* escape paranthesis */
            out[j++] = '\\';
            out[j++] = in[i];
        } else if (in[i] == '\\') {
            /* escape backslash */
            out[j++] = '\\';
            out[j++] = '\\';
        } else {
            /* copy char :-) */
            out[j++] = in[i];
        }
    }
    out[j] = '\0';
    return pstrbuf;
}


/* Converts any string given in in in an allowed PDF string which can be
 * handled by printf et.al.: \ is escaped to \\, parenthesis are escaped and
 * control characters are octal encoded.
 * This assumes that the string does not contain any already escaped
 * characters!
 *
 * See escapename for parameter description.
 */
void escapestring (poolpointer in)
{
    const poolpointer out = poolptr;
    unsigned char ch;
    while (in < out) {
        if (poolptr + 4 >= poolsize) {
            poolptr = poolsize;
            /* error by str_toks that calls str_room(1) */
            return;
        }

        ch = (unsigned char) strpool[in++];

        if ((ch < '!') || (ch > '~')) {
            /* convert control characters into oct */
            int i = snprintf ((char *) &strpool[poolptr], 5,
                              "\\%.3o", (unsigned int) ch);
            check_nprintf (i, 5);
            poolptr += i;
            continue;
        }
        if ((ch == '(') || (ch == ')') || (ch == '\\')) {
            /* escape parenthesis and backslash */
            strpool[poolptr++] = '\\';
        }
        /* copy char :-) */
        strpool[poolptr++] = ch;
    }
}

/* Convert any given string in a PDF name using escaping mechanism
   of PDF 1.2. The result does not include the leading slash.
   
   PDF specification 1.6, section 3.2.6 "Name Objects" explains:
   <blockquote>
    Beginning with PDF 1.2, any character except null (character code 0) may
    be included in a name by writing its 2-digit hexadecimal code, preceded
    by the number sign character (#); see implementation notes 3 and 4 in
    Appendix H. This syntax is required to represent any of the delimiter or
    white-space characters or the number sign character itself; it is
    recommended but not required for characters whose codes are outside the
    range 33 (!) to 126 (~).
   </blockquote>
   The following table shows the conversion that are done by this
   function:
     code      result   reason
     -----------------------------------
     0         ignored  not allowed
     1..32     escaped  must for white-space:
                          9 (tab), 10 (lf), 12 (ff), 13 (cr), 32 (space)
                        recommended for the other control characters
     35        escaped  escape char "#"
     37        escaped  delimiter "%"
     40..41    escaped  delimiters "(" and ")"
     47        escaped  delimiter "/"
     60        escaped  delimiter "<"
     62        escaped  delimiter ">"
     91        escaped  delimiter "["
     93        escaped  delimiter "]"
     123       escaped  delimiter "{"
     125       escaped  delimiter "}"
     127..255  escaped  recommended
     else      copy     regular characters
     
   Parameter "in" is a pointer into the string pool where
   the input string is located. The output string is written
   as temporary string right after the input string.
   Thus at the begin of the procedure the global variable
   "poolptr" points to the start of the output string and
   after the end when the procedure returns.
*/
void escapename (poolpointer in)
{
    const poolpointer out = poolptr;
    unsigned char ch;
    int i;

    while (in < out) {
        if (poolptr + 3 >= poolsize) {
            poolptr = poolsize;
            /* error by str_toks that calls str_room(1) */
            return;
        }

        ch = (unsigned char) strpool[in++];

        if ((ch >= 1 && ch <= 32) || ch >= 127) {
            /* escape */
            i = snprintf ((char *) &strpool[poolptr], 4,
                          "#%.2X", (unsigned int) ch);
            check_nprintf (i, 4);
            poolptr += i;
            continue;
        }
        switch (ch) {
        case 0:
            /* ignore */
            break;
        case 35:
        case 37:
        case 40:
        case 41:
        case 47:
        case 60:
        case 62:
        case 91:
        case 93:
        case 123:
        case 125:
            /* escape */
            i = snprintf ((char *) &strpool[poolptr], 4,
                          "#%.2X", (unsigned int) ch);
            check_nprintf (i, 4);
            poolptr += i;
            break;
        default:
            /* copy */
            strpool[poolptr++] = ch;
        }
    }
}

/* Convert any given string in a PDF hexadecimal string. The
   result does not include the angle brackets.
   
   This procedure uses uppercase hexadecimal letters.
   
   See escapename for description of parameters.
*/
void escapehex (poolpointer in)
{
    const poolpointer out = poolptr;
    unsigned char ch;
    int i;

    while (in < out) {
        if (poolptr + 2 >= poolsize) {
            poolptr = poolsize;
            /* error by str_toks that calls str_room(1) */
            return;
        }

        ch = (unsigned char) strpool[in++];

        i = snprintf ((char *) &strpool[poolptr], 3, "%.2X", (unsigned int) ch);
        check_nprintf (i, 3);
        poolptr += 2;
    }
}

/* Unescape any given hexadecimal string.

   Last hex digit can be omitted, it is replaced by zero, see
   PDF specification.
   
   Invalid digits are silently ignored.

   See escapename for description of parameters.
*/
void unescapehex (poolpointer in)
{
    const poolpointer out = poolptr;
    unsigned char ch;
    unsigned char a = 0;        /* to avoid warning about uninitialized use of a */
    boolean first = true;

    while (in < out) {
        if (poolptr + 1 >= poolsize) {
            poolptr = poolsize;
            /* error by str_toks that calls str_room(1) */
            return;
        }

        ch = (unsigned char) strpool[in++];

        if ((ch >= '0') && (ch <= '9')) {
            ch -= '0';
        } else if ((ch >= 'A') && (ch <= 'F')) {
            ch -= 'A' - 10;
        } else if ((ch >= 'a') && (ch <= 'f')) {
            ch -= 'a' - 10;
        } else {
            continue;           /* ignore wrong character */
        }

        if (first) {
            a = ch << 4;
            first = false;
            continue;
        }

        strpool[poolptr++] = a + ch;
        first = true;
    }
    if (!first) {               /* last hex digit is omitted */
        strpool[poolptr++] = ch << 4;
    }
}


/* Converts any string given in in in an allowed PDF string which is 
 * hexadecimal encoded;
 * sizeof(out) should be at least lin*2+1.
 */
static void convertStringToHexString (const char *in, char *out, int lin)
{
    int i, j, k;
    char buf[3];
    j = 0;
    for (i = 0; i < lin; i++) {
        k = snprintf (buf, sizeof (buf),
                      "%02X", (unsigned int) (unsigned char) in[i]);
        check_nprintf (k, sizeof (buf));
        out[j++] = buf[0];
        out[j++] = buf[1];
    }
    out[j] = '\0';
}



/* makecfilename
  input/ouput same as makecstring:
    input: string number
    output: C string with quotes removed.
    That means, file names that are legal on some operation systems
    cannot any more be used since pdfTeX version 1.30.4.
*/
char *makecfilename (strnumber s)
{
    char *name = makecstring (s);
    char *p = name;
    char *q = name;

    while (*p) {
        if (*p != '"')
            *q++ = *p;
        p++;
    }
    *q = '\0';
    return name;
}

/* function strips trailing zeros in string with numbers; */
/* leading zeros are not stripped (as in real life) */
char *stripzeros (char *a)
{
    enum { NONUM, DOTNONUM, INT, DOT, LEADDOT, FRAC } s = NONUM, t = NONUM;
    char *p, *q, *r;
    for (p = q = r = a; *p != '\0';) {
        switch (s) {
        case NONUM:
            if (*p >= '0' && *p <= '9')
                s = INT;
            else if (*p == '.')
                s = LEADDOT;
            break;
        case DOTNONUM:
            if (*p != '.' && (*p < '0' || *p > '9'))
                s = NONUM;
            break;
        case INT:
            if (*p == '.')
                s = DOT;
            else if (*p < '0' || *p > '9')
                s = NONUM;
            break;
        case DOT:
        case LEADDOT:
            if (*p >= '0' && *p <= '9')
                s = FRAC;
            else if (*p == '.')
                s = DOTNONUM;
            else
                s = NONUM;
            break;
        case FRAC:
            if (*p == '.')
                s = DOTNONUM;
            else if (*p < '0' || *p > '9')
                s = NONUM;
            break;
        default:;
        }
        switch (s) {
        case DOT:
            r = q;
            break;
        case LEADDOT:
            r = q + 1;
            break;
        case FRAC:
            if (*p > '0')
                r = q + 1;
            break;
        case NONUM:
            if ((t == FRAC || t == DOT) && r != a) {
                q = r--;
                if (*r == '.')  /* was a LEADDOT */
                    *r = '0';
                r = a;
            }
            break;
        default:;
        }
        *q++ = *p++;
        t = s;
    }
    *q = '\0';
    return a;
}

void stripspaces (char *p)
{
    char *q;
    for (q = p; *p != '\0'; p++) {
        if (*p != ' ')
            *q++ = *p;
    }
    *q = '\0';
}

void initversionstring (char **versions)
{
}

void setjobid (int year, int month, int day, int time)
{
    char *name_string, *format_string, *s;
    size_t slen;
    int i;

    if (job_id_string != NULL)
        return;

    name_string = xstrdup (makecstring (jobname));
	format_string = xstrdup (makecstring (memident));
	
    slen = SMALL_BUF_SIZE +
        strlen (name_string) +
        strlen (format_string) +
      //strlen (banner) +
        strlen (versionstring) + strlen (kpathsea_version_string);
    s = xtalloc (slen, char);
    /* The Web2c version string starts with a space.  */
    i = snprintf (s, slen,
                  "%.4d/%.2d/%.2d %.2d:%.2d %s %s%s %s",
                  (year>>16), (month>>16), (day>>16), (time>>16) / 60, (time>>16) % 60,
                  name_string, format_string, //ptexbanner,
                  versionstring, kpathsea_version_string);
    check_nprintf (i, slen);
    job_id_string = xstrdup (s);
    xfree (s);
    xfree (name_string);
    xfree (format_string);
}



static void fnstr_append (const char *s)
{
    size_t l = strlen (s) + 1;
    alloc_array (char, l, SMALL_ARRAY_SIZE);
    strcat (char_ptr, s);
    char_ptr = strend (char_ptr);
}

/* this is not really a true crc32, but it should be just enough to keep
   subsets prefixes somewhat disjunct */
unsigned long crc32 (int oldcrc, const Byte *buf, int len) {
  unsigned long ret;
  int i;
  if (oldcrc==0)
	ret = (23<<24)+(45<<16)+(67<<8)+89;
  else
	for (i=0;i<len;i++)
	  ret = (ret<<2)+buf[i];
  return ret;
}

void make_subset_tag (fm_entry * fm_cur, char **glyph_names, int tex_font)
{
    char tag[7];
    unsigned long crc;
    int i;
    size_t l ;
    if (job_id_string ==NULL)
      exit;
    l = strlen (job_id_string) + 1;
    
    alloc_array (char, l, SMALL_ARRAY_SIZE);
    strcpy (char_array, job_id_string);
    char_ptr = strend (char_array);
    if (fm_cur->tfm_name != NULL) {
        fnstr_append (" TFM name: ");
        fnstr_append (fm_cur->tfm_name);
    }
    fnstr_append (" PS name: ");
    //if (font_keys[FONTNAME_CODE].valid)
    //    fnstr_append (fontname_buf);
    //else 
    if (fm_cur->ps_name != NULL)
        fnstr_append (fm_cur->ps_name);
    fnstr_append (" Encoding: ");
    if (fm_cur->encoding != NULL && (fm_cur->encoding)->name != NULL)
        fnstr_append ((fm_cur->encoding)->name);
    else
        fnstr_append ("built-in");
    fnstr_append (" CharSet: ");
    assert (glyph_names != NULL);
    for (i = 0; i < 256; i++)
        if (mpcharmarked (tex_font, i) && glyph_names[i] != notdef) {
			if (glyph_names[i]!=NULL) {
			  fnstr_append ("/");
			  fnstr_append (glyph_names[i]);
			}
        }
    if (fm_cur->charset != NULL) {
        fnstr_append (" Extra CharSet: ");
        fnstr_append (fm_cur->charset);
    }
    crc = crc32 (0L, Z_NULL, 0);
    crc = crc32 (crc, (Bytef *) char_array, strlen (char_array));
    /* we need to fit a 32-bit number into a string of 6 uppercase chars long;
     * there are 26 uppercase chars ==> each char represents a number in range
     * 0..25. The maximal number that can be represented by the tag is
     * 26^6 - 1, which is a number between 2^28 and 2^29. Thus the bits 29..31
     * of the CRC must be dropped out.
     */
    for (i = 0; i < 6; i++) {
        tag[i] = 'A' + crc % 26;
        crc /= 26;
    }
    tag[6] = 0;
    fm_cur->subset_tag = xstrdup (tag);
}