summaryrefslogtreecommitdiff
path: root/ident.c
blob: 6d3cff31e66da8c3faba0eeb360723f703ebfbab (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
/*
  Copyright (c) 2003 by Juliusz Chroboczek

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
/*
 * Copyright (c) 2008, Oracle and/or its affiliates.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

/* The function identifyBitmap returns -1 if filename is definitively not
   a font file, 1 if it is a single-face bitmap font with a XLFD name,
   and 0 if it should be processed normally.  identifyBitmap is
   much faster than parsing the whole font. */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdlib.h>
#include <string.h>
#include "zlib.h"
#include "ident.h"

#ifdef X_BZIP2_FONT_COMPRESSION
#include <bzlib.h>
#endif

#define PCF_VERSION (('p'<<24)|('c'<<16)|('f'<<8)|1)
#define PCF_PROPERTIES (1 << 0)

typedef struct _Prop {
    unsigned name;
    int isString;
    unsigned value;
} PropRec, *PropPtr;

#ifdef X_BZIP2_FONT_COMPRESSION
typedef struct {
    enum { gzFontFile, bz2FontFile } type;
    union {
        gzFile gz;
        BZFILE *bz2;
    } f;
    unsigned long pos;
} fontFile;

static inline void *
fontFileOpen(fontFile *ff, const char *filename)
{
    size_t n = strlen(filename);

    if (n > 4 && strcmp(filename + n - 4, ".bz2") == 0) {
        ff->type = bz2FontFile;
        ff->f.bz2 = BZ2_bzopen(filename, "rb");
        ff->pos = 0;
        return ff->f.bz2;
    }
    else {
        ff->type = gzFontFile;
        ff->f.gz = gzopen(filename, "rb");
        return ff->f.gz;
    }
}

static inline int
fontFileRead(fontFile *ff, void *buf, unsigned len)
{
    if (ff->type == gzFontFile) {
        return gzread(ff->f.gz, buf, len);
    }
    else {
        int r = BZ2_bzread(ff->f.bz2, buf, len);

        ff->pos += r;
        return r;
    }
}

static inline int
fontFileGetc(fontFile *ff)
{
    if (ff->type == gzFontFile) {
        return gzgetc(ff->f.gz);
    }
    else {
        char buf;

        if (BZ2_bzread(ff->f.bz2, &buf, 1) != 1) {
            return -1;
        }
        else {
            ff->pos += 1;
            return (int) buf;
        }
    }
}

static long
fontFileSeek(fontFile *ff, z_off_t offset, int whence)
{
    if (ff->type == gzFontFile) {
        return gzseek(ff->f.gz, offset, whence);
    }
    else {
        /* bzlib has no easy equivalent so we have to fake it,
         * fortunately, we only have to handle a couple of cases
         */
        z_off_t n;
        char buf[BUFSIZ];

        switch (whence) {
        case SEEK_SET:
            n = offset - ff->pos;
            break;
        case SEEK_CUR:
            n = offset;
            break;
        default:
            return -1;
        }

        while (n > BUFSIZ) {
            if (BZ2_bzread(ff->f.bz2, buf, BUFSIZ) != BUFSIZ)
                return -1;
            n -= BUFSIZ;
        }
        if (BZ2_bzread(ff->f.bz2, buf, (int) n) != n)
            return -1;
        ff->pos = offset;
        return offset;
    }
}

static inline int
fontFileClose(fontFile *ff)
{
    if (ff->type == gzFontFile) {
        return gzclose(ff->f.gz);
    }
    else {
        BZ2_bzclose(ff->f.bz2);
        return 0;
    }
}

#else                           /* no bzip2, only gzip */
typedef gzFile fontFile;

#define fontFileOpen(ff, filename)	(*(ff) = gzopen(filename, "rb"))
#define fontFileRead(ff, buf, len)	gzread(*(ff), buf, len)
#define fontFileGetc(ff)		gzgetc(*(ff))
#define fontFileSeek(ff, off, whence)	gzseek(*(ff), off, whence)
#define fontFileClose(ff)		gzclose(*(ff))
#endif

static int pcfIdentify(fontFile *f, char **name);
static int bdfIdentify(fontFile *f, char **name);

static int
getLSB32(fontFile *f)
{
    int rc;
    unsigned char c[4];

    rc = fontFileRead(f, c, 4);
    if (rc != 4)
        return -1;
    return (c[0]) | (c[1] << 8) | (c[2] << 16) | (c[3] << 24);
}

static int
getInt8(fontFile *f, int format)
{
    unsigned char c;
    int rc;

    rc = fontFileRead(f, &c, 1);
    if (rc != 1)
        return -1;
    return c;
}

static int
getInt32(fontFile *f, int format)
{
    int rc;
    unsigned char c[4];

    rc = fontFileRead(f, c, 4);
    if (rc != 4)
        return -1;
    else {
        unsigned int u[4] = { c[0], c[1], c[2], c[3] };

        if (format & (1 << 2)) {
            return (int) ((u[0] << 24) | (u[1] << 16) | (u[2] << 8) | (u[3]));
        }
        else {
            return (int) ((u[0]) | (u[1] << 8) | (u[2] << 16) | (u[3] << 24));
        }
    }
}

int
bitmapIdentify(const char *filename, char **name)
{
    fontFile ff;
    int magic;

    if (fontFileOpen(&ff, filename) == NULL)
        return -1;

    magic = getLSB32(&ff);
    if (magic == PCF_VERSION)
        return pcfIdentify(&ff, name);
    else if (magic == ('S' | ('T' << 8) | ('A' << 16) | ('R') << 24))
        return bdfIdentify(&ff, name);

    fontFileClose(&ff);
    return 0;
}

static int
pcfIdentify(fontFile *f, char **name)
{
    int prop_position;
    PropPtr props = NULL;
    int format, count, nprops, i, string_size;
    long rc;
    char *strings = NULL, *s;

    count = getLSB32(f);
    if (count <= 0)
        goto fail;

    prop_position = -1;
    for (i = 0; i < count; i++) {
        int type, offset;

        type = getLSB32(f);
        (void) getLSB32(f);
        (void) getLSB32(f);
        offset = getLSB32(f);
        if (type == PCF_PROPERTIES) {
            prop_position = offset;
            break;
        }
    }
    if (prop_position < 0)
        goto fail;

    rc = fontFileSeek(f, prop_position, SEEK_SET);
    if (rc < 0)
        goto fail;

    format = getLSB32(f);
    if ((format & 0xFFFFFF00) != 0)
        goto fail;
    nprops = getInt32(f, format);
    if (nprops <= 0 || nprops > 1000)
        goto fail;
    props = malloc(nprops * sizeof(PropRec));
    if (props == NULL)
        goto fail;

    for (i = 0; i < nprops; i++) {
        props[i].name = getInt32(f, format);
        props[i].isString = getInt8(f, format);
        props[i].value = getInt32(f, format);
    }
    if (nprops & 3) {
        rc = fontFileSeek(f, 4 - (nprops & 3), SEEK_CUR);
        if (rc < 0)
            goto fail;
    }

    string_size = getInt32(f, format);
    if (string_size < 0 || string_size > 100000)
        goto fail;
    strings = malloc(string_size);
    if (!strings)
        goto fail;

    rc = fontFileRead(f, strings, string_size);
    if (rc != string_size)
        goto fail;

    for (i = 0; i < nprops; i++) {
        if (!props[i].isString ||
            props[i].name >= string_size - 4 || props[i].value >= string_size)
            continue;
        if (strcmp(strings + props[i].name, "FONT") == 0)
            break;
    }

    if (i >= nprops)
        goto fail;

    s = strdup(strings + props[i].value);
    if (s == NULL)
        goto fail;
    *name = s;
    free(strings);
    free(props);
    fontFileClose(f);
    return 1;

 fail:
    if (strings)
        free(strings);
    if (props)
        free(props);
    fontFileClose(f);
    return 0;
}

#define NKEY 20

static char *
getKeyword(fontFile *f, int *eol)
{
    static char keyword[NKEY + 1];
    int c, i;

    i = 0;
    while (i < NKEY) {
        c = fontFileGetc(f);
        if (c == ' ' || c == '\n') {
            if (i <= 0)
                return NULL;
            if (eol)
                *eol = (c == '\n');
            keyword[i] = '\0';
            return keyword;
        }
        if (c < 'A' || c > 'Z')
            return NULL;
        keyword[i++] = c;
    }
    return NULL;
}

static int
bdfskip(fontFile *f)
{
    int c;

    do {
        c = fontFileGetc(f);
    } while (c >= 0 && c != '\n');
    if (c < 0)
        return -1;
    return 1;
}

static char *
bdfend(fontFile *f)
{
    int c;
    char *buf = NULL;
    int bufsize = 0;
    int i = 0;

    do {
        c = fontFileGetc(f);
    } while (c == ' ');

    while (i < 1000) {
        if (c < 0 || (c == '\n' && i == 0)) {
            goto fail;
        }
        if (bufsize < i + 1) {
            char *newbuf;

            if (bufsize == 0) {
                bufsize = 20;
                newbuf = malloc(bufsize);
            }
            else {
                bufsize = 2 * bufsize;
                newbuf = realloc(buf, bufsize);
            }
            if (newbuf == NULL)
                goto fail;
            buf = newbuf;
        }
        if (c == '\n') {
            buf[i] = '\0';
            return buf;
        }
        buf[i++] = c;
        c = fontFileGetc(f);
    }

 fail:
    if (buf)
        free(buf);
    return NULL;
}

static int
bdfIdentify(fontFile *f, char **name)
{
    char *k;
    int rc;
    int eol;

    /* bitmapIdentify already read "STAR", so we need to check for
       "TFONT" */
    k = getKeyword(f, &eol);
    if (k == NULL || eol)
        goto fail;
    if (strcmp(k, "TFONT") != 0)
        goto fail;
    while (1) {
        if (!eol) {
            rc = bdfskip(f);
            if (rc < 0)
                goto fail;
        }
        k = getKeyword(f, &eol);
        if (k == NULL)
            goto fail;
        else if (strcmp(k, "FONT") == 0) {
            if (eol)
                goto fail;
            k = bdfend(f);
            if (k == NULL)
                goto fail;
            *name = k;
            fontFileClose(f);
            return 1;
        }
        else if (strcmp(k, "CHARS") == 0)
            goto fail;
    }
 fail:
    fontFileClose(f);
    return 0;
}