Back to index

tetex-bin  3.0
Typedefs | Functions | Variables
utils.c File Reference
#include "ptexlib.h"
#include "zlib.h"
#include "md5.h"
#include <kpathsea/c-vararg.h>
#include <kpathsea/c-proto.h>
#include <time.h>

Go to the source code of this file.

Typedefs

typedef char fb_entry
typedef char char_entry

Functions

 define_array (fb)
 define_array (char)
integer fb_offset (void)
void fb_seek (integer offset)
void fb_putchar (eightbits b)
void fb_flush (void)
static void fnstr_append (const char *s)
void make_subset_tag (fm_entry *fm_cur, char **glyph_names)
void pdf_puts (const char *s)
void pdf_printf (const char *fmt,...)
strnumber maketexstring (const char *s)
void tex_printf (const char *fmt,...)
static void safe_print (const char *str)
void pdftex_fail (const char *fmt,...)
void pdftex_warn (const char *fmt,...)
char * makecstring (integer s)
void setjobid (int year, int month, int day, int time, int pdftexversion, int pdftexrevision)
void makepdftexbanner (void)
strnumber getresnameprefix (void)
size_t xfwrite (void *ptr, size_t size, size_t nmemb, FILE *stream)
int xfflush (FILE *stream)
int xgetc (FILE *stream)
int xputc (int c, FILE *stream)
void writestreamlength (integer length, integer offset)
scaled extxnoverd (scaled x, scaled n, scaled d)
void libpdffinish ()
char * convertStringToPDFString (char *in)
void convertStringToHexString (char *in, char *out, int lin)
void printID (strnumber filename)
void printcreationdate ()
void escapestr (strnumber s)
integer escapedstrlen ()
ASCIIcode getescapedchar (integer index)

Variables

static const char perforce_id [] = "$Id: //depot/Build/source.development/TeX/texk/web2c/pdftexdir/utils.c#21 $"
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
static char * escaped_string = NULL
string ptexbanner
string versionstring
KPSEDLL string kpathsea_version_string
size_t last_ptr_index

Typedef Documentation

typedef char char_entry

Definition at line 50 of file utils.c.

typedef char fb_entry

Definition at line 46 of file utils.c.


Function Documentation

void convertStringToHexString ( char *  in,
char *  out,
int  lin 
)

Definition at line 433 of file utils.c.

{
    int i, j;
    char buf[3];
    out[0] = '<';
    j = 1;
    for (i = 0; i < lin; i++) {
        sprintf (buf, "%02X", (unsigned int)(unsigned char)in[i]);
        out[j++] = buf[0];
        out[j++] = buf[1];
        }
    out[j++] = '>';
    out[j] = '\0';
}

Here is the call graph for this function:

Here is the caller graph for this function:

char* convertStringToPDFString ( char *  in)

Definition at line 393 of file utils.c.

{
    static char pstrbuf[MAX_PSTRING_LEN];
    char *out = pstrbuf;
    int lin = strlen (in);
    int i, j;
    char buf[4];
    j = 0;
    for (i = 0; i < lin; i++) {
        check_buf(j + sizeof(buf), MAX_PSTRING_LEN);
        if ((unsigned char)in[i] < ' ') {
            /* convert control characters into hex */
            sprintf (buf, "#%02x", (unsigned int)(unsigned char)in[i]);
            out[j++] = buf[0];
            out[j++] = buf[1];
            out[j++] = buf[2];
            }
        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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

define_array ( fb  )
define_array ( char  )

Definition at line 619 of file utils.c.

Here is the call graph for this function:

void escapestr ( strnumber  s)

Definition at line 614 of file utils.c.

Here is the call graph for this function:

scaled extxnoverd ( scaled  x,
scaled  n,
scaled  d 
)

Definition at line 364 of file utils.c.

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

Here is the call graph for this function:

Definition at line 69 of file utils.c.

{
    fb_entry *p;
    integer n;
    for (p = fb_array; p < fb_ptr;) {
        n = pdfbufsize - pdfptr;
        if (fb_ptr - p < n)
            n = fb_ptr - p;
        memcpy(pdfbuf + pdfptr, p, (unsigned)n);
        pdfptr += n;
        if (pdfptr == pdfbufsize)
            pdfflush();
        p += n;
    }
    fb_ptr = fb_array;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 53 of file utils.c.

{
    return fb_ptr - fb_array;
}

Here is the caller graph for this function:

Definition at line 63 of file utils.c.

{
    alloc_array(fb, 1, SMALL_ARRAY_SIZE);
    *fb_ptr++ = b;
}
void fb_seek ( integer  offset)

Definition at line 58 of file utils.c.

{
     fb_ptr = fb_array + offset;
}
static void fnstr_append ( const char *  s) [static]

Definition at line 86 of file utils.c.

{
    int l = strlen(s) + 1;
    alloc_array(char, l, SMALL_ARRAY_SIZE);
    strcat(char_ptr, s);
    char_ptr = strend(char_ptr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

ASCIIcode getescapedchar ( integer  index)

Definition at line 625 of file utils.c.

{
    if (index < 0 || index >= strlen(escaped_string))
        pdftex_fail("getescapedchar(): index out of range");
    return escaped_string[index];
}

Here is the call graph for this function:

strnumber getresnameprefix ( void  )

Definition at line 303 of file utils.c.

{
/*     static char name_str[] = */
/* "!\"$&'*+,-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\" */
/* "^_`abcdefghijklmnopqrstuvwxyz|~"; */
    static char name_str[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    char prefix[7]; /* make a tag of 6 chars long */
    unsigned long crc;
    int i, base = strlen(name_str);
    crc = crc32(0L, Z_NULL, 0);
    crc = crc32(crc, (Bytef*)job_id_string, strlen(job_id_string));
    for (i = 0; i < 6; i++) {
        prefix[i] = name_str[crc % base];
        crc /= base;
    }
    prefix[6] = 0;
    return maketexstring(prefix);
}

Here is the call graph for this function:

Definition at line 376 of file utils.c.

{
    xfree(fb_array);
    xfree(char_array);
    xfree(job_id_string);
    fm_free();
    t1_free();
    enc_free();
    img_free();
    vf_free();
    epdf_free();
}

Here is the call graph for this function:

void make_subset_tag ( fm_entry fm_cur,
char **  glyph_names 
)

Definition at line 94 of file utils.c.

{
    char tag[7];
    unsigned long crc;
    int i, 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 <= MAX_CHAR_CODE; i++)
        if (pdfcharmarked(tex_font, i) && glyph_names[i] != notdef) {
            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);
}

Here is the call graph for this function:

char* makecstring ( integer  s)

Definition at line 233 of file utils.c.

{
    static char cstrbuf[MAX_CSTRING_LEN];
    char *p = cstrbuf;
    int i, l = strstart[s + 1] - strstart[s];
    check_buf(l + 1, MAX_CSTRING_LEN);
    for (i = 0; i < l; i++)
        *p++ = strpool[i + strstart[s]];
    *p = 0;
    return cstrbuf;
}

Here is the caller graph for this function:

Definition at line 284 of file utils.c.

{
    static boolean pdftexbanner_init = false;
    char *s;

    if (pdftexbanner_init)
        return;

    s = xtalloc(SMALL_BUF_SIZE + 
                strlen(ptexbanner) + 
                strlen(versionstring) + 
                strlen(kpathsea_version_string), char);
    /* The Web2c version string starts with a space.  */
    sprintf(s, "%s%s %s", ptexbanner, versionstring, kpathsea_version_string);
    pdftexbanner = maketexstring(s);
    xfree(s);
    pdftexbanner_init = true;
}

Here is the call graph for this function:

strnumber maketexstring ( const char *  s)

Definition at line 159 of file utils.c.

{
    int 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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pdf_printf ( const char *  fmt,
  ... 
)

Definition at line 150 of file utils.c.

{
    va_list args;
    va_start(args, fmt);
    vsprintf(print_buf, fmt, args);
    pdf_puts(print_buf);                                    
    va_end(args);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pdf_puts ( const char *  s)

Definition at line 143 of file utils.c.

{
    pdfroom(strlen(s) + 1);
    while (*s)
        pdfbuf[pdfptr++] = *s++;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pdftex_fail ( const char *  fmt,
  ... 
)

Definition at line 195 of file utils.c.

{
    va_list args;
    va_start(args, fmt);
    println();
    safe_print("Error: ");
    safe_print(program_invocation_name);
    if (cur_file_name) {
        safe_print(" (file ");
        safe_print(cur_file_name);
        safe_print(")");
    }
    safe_print(": ");
    vsprintf(print_buf, fmt, args);
    safe_print(print_buf);
    va_end(args);
    println();
    safe_print(" ==> Fatal error occurred, the output PDF file is not finished!");
    println();
    exit(-1);
}

Here is the call graph for this function:

void pdftex_warn ( const char *  fmt,
  ... 
)

Definition at line 217 of file utils.c.

{
    va_list args;
    va_start(args, fmt);
    println();
    tex_printf("Warning: %s", program_invocation_name);
    if (cur_file_name)
        tex_printf(" (file %s)", cur_file_name);
    tex_printf(": ");
    vsprintf(print_buf, fmt, args);
    print(maketexstring(print_buf));
    flushstr(last_tex_string);
    va_end(args);
    println();
}

Here is the call graph for this function:

Definition at line 558 of file utils.c.

{
            
    time_t t;
    struct tm lt, gmt;
    size_t size;
    /* minimum size for time_str is 22: "YYYYmmddHHMMSS+HH'MM'" */
    char time_str[40]; /* larger value for safety */
    int off, off_hours, off_mins;
 
    /* get the time */
    t = time(NULL);
    lt = *localtime(&t);
    size = strftime(time_str, sizeof(time_str), "%Y%m%d%H%M%S", &lt);
    /* expected format: "YYYYmmddHHMMSS" */
    if (size != 14) {
        /* An unexpected result of strftime */
        pdftex_warn("/CreationDate dropped because of "
                    "unexpected result of strftime()");
        return;
    }

    /* correction for seconds: %S can be in range 00..61,
       the PDF reference expects 00..59,   
       therefore we map "60" and "61" to "59" */
    if (time_str[12] == '6') {
        time_str[12] = '5';
        time_str[13] = '9'; /* we have checked for size above */
    }

    /* get the time zone offset */
    gmt = *gmtime(&t);

    /* this calculation method was found in exim's tod.c */
    off = 60*(lt.tm_hour - gmt.tm_hour) + lt.tm_min - gmt.tm_min;
    if (lt.tm_year != gmt.tm_year) {
        off += (lt.tm_year > gmt.tm_year) ? 1440 : -1440;
    }
    else if (lt.tm_yday != gmt.tm_yday) {
        off += (lt.tm_yday > gmt.tm_yday) ? 1440 : -1440;
    }

    if (off == 0) {
        time_str[size++] = 'Z';
        time_str[size] = 0;
    }
    else {
        off_hours = off/60;
        off_mins = abs(off - off_hours*60);
        sprintf(&time_str[size], "%+03i'%02d'", off_hours, off_mins);
    }

    /* print result */
    pdf_printf("/CreationDate (D:%s)\n", time_str);
}

Here is the call graph for this function:

void printID ( strnumber  filename)

Definition at line 479 of file utils.c.

{
    time_t t;
    size_t size;
    char time_str[32];
    md5_state_t state;
    md5_byte_t digest[16];
    char id[64];
    char *file_name;
    char pwd[4096];
    /* start md5 */
    md5_init(&state);
    /* get the time */
    t = time(NULL);
    size = strftime (time_str, sizeof(time_str), "%Y%m%dT%H%M%SZ", gmtime(&t));
    md5_append(&state, (const md5_byte_t *)time_str, size);
    /* get the file name */
    if (getcwd(pwd, sizeof(pwd)) == NULL)
        pdftex_fail("getcwd() failed (path too long?)");
    file_name = makecstring(filename);
    md5_append(&state, (const md5_byte_t *)pwd, strlen(pwd));
    md5_append(&state, (const md5_byte_t *)"/", 1);
    md5_append(&state, (const md5_byte_t *)file_name, strlen(file_name));
    /* finish md5 */
    md5_finish(&state, digest);
    /* write the IDs */
    convertStringToHexString ((char*)digest, id, 16);
    pdf_printf("/ID [%s %s]\n", id, id);
}

Here is the call graph for this function:

static void safe_print ( const char *  str) [static]

Definition at line 184 of file utils.c.

{
    const char *c;
    for (c = str; *c; ++c)
        print(*c);
}

Here is the caller graph for this function:

void setjobid ( int  year,
int  month,
int  day,
int  time,
int  pdftexversion,
int  pdftexrevision 
)

Definition at line 258 of file utils.c.

{
    char *name_string, *format_string, *s;

    if (job_id_string != NULL)
        return;

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

Here is the call graph for this function:

void tex_printf ( const char *  fmt,
  ... 
)

Definition at line 172 of file utils.c.

{
    va_list args;
    va_start(args, fmt);
    vsprintf(print_buf, fmt, args);
    print(maketexstring(print_buf));
    flushstr(last_tex_string);
    xfflush(stdout);
    va_end(args);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void writestreamlength ( integer  length,
integer  offset 
)

Definition at line 353 of file utils.c.

{
    integer save_offset;
    if (jobname_cstr == NULL)
        jobname_cstr = xstrdup(makecstring(jobname));
    save_offset = xftell(pdffile, jobname_cstr);
    xfseek(pdffile, offset, SEEK_SET, jobname_cstr);
    fprintf(pdffile, "%li", (long int)length);
    xfseek(pdffile, pdfoffset(), SEEK_SET, jobname_cstr);
}

Here is the call graph for this function:

int xfflush ( FILE stream)

Definition at line 330 of file utils.c.

{
    if (fflush(stream) != 0)
        pdftex_fail("fflush() failed");
    return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

size_t xfwrite ( void ptr,
size_t  size,
size_t  nmemb,
FILE stream 
)

Definition at line 323 of file utils.c.

{
    if (fwrite(ptr, size, nmemb, stream) != nmemb)
        pdftex_fail("fwrite() failed");
    return nmemb;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int xgetc ( FILE stream)

Definition at line 337 of file utils.c.

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

Here is the caller graph for this function:

int xputc ( int  c,
FILE stream 
)

Definition at line 345 of file utils.c.

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

Variable Documentation

Definition at line 33 of file utils.c.

char* escaped_string = NULL [static]

Definition at line 38 of file utils.c.

char* job_id_string = NULL [static]

Definition at line 37 of file utils.c.

char* jobname_cstr = NULL [static]

Definition at line 36 of file utils.c.

Definition at line 4 of file version.c.

Definition at line 43 of file utils.c.

strnumber last_tex_string

Definition at line 34 of file utils.c.

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

Definition at line 30 of file utils.c.

char print_buf[PRINTF_BUF_SIZE] [static]

Definition at line 35 of file utils.c.

Definition at line 6 of file version.c.