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
|
/*
* PostScript Type 1 font file support for Halibut
*/
/*
* Type 1 font file formats are specified by Adobe Technical Note
* #5040: "Supporting Downloadable PostScript Language Fonts".
* Halibut supports hexadecimal format (section 3.1) and IBM PC format
* (section 3.3), commonly called PFA and PFB respectively.
*/
#include <assert.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include "halibut.h"
#include "paper.h"
#define PFB_ASCII 1
#define PFB_BINARY 2
#define PFB_EOF 3
typedef struct t1_font_Tag t1_font;
typedef struct t1_data_Tag t1_data;
font_info *all_fonts;
struct t1_font_Tag {
t1_data *data;
size_t length1;
size_t length2;
filepos pos;
};
struct t1_data_Tag {
char type;
size_t length;
unsigned char *data;
t1_data *next;
};
typedef struct pfstate_Tag {
t1_data *data;
t1_data *curblock;
size_t offset;
} pfstate;
static void pf_identify(t1_font *tf);
static t1_data *load_pfb_file(FILE *fp, filepos *pos) {
t1_data *head = NULL, *tail = NULL;
int c, i;
char type;
pos->line = 0;
for (;;) {
if (fgetc(fp) != 128) abort();
type = fgetc(fp);
if (type == PFB_EOF) return head;
if (tail) {
tail->next = snew(t1_data);
tail = tail->next;
} else {
head = snew(t1_data);
tail = head;
}
tail->type = type;
tail->length = 0;
for (i = 0; i < 4; i++) {
c = fgetc(fp);
if (c == EOF) abort();
tail->length |= c << (8 * i);
}
tail->data = snewn((int)tail->length, unsigned char);
if (fread(tail->data, 1, tail->length, fp) != tail->length) abort();
}
}
static t1_data *load_pfa_file(FILE *fp, filepos *pos) {
t1_data *ret = snew(t1_data);
size_t off = 0, len, got;
pos->line = 0;
ret->type = PFB_ASCII;
len = 32768;
ret->data = snewn((int)len, unsigned char);
for (;;) {
got = fread(ret->data + off, 1, len - off, fp);
off += got;
if (off != len) break;
len *= 2;
ret->data = sresize(ret->data, (int)len, unsigned char);
}
ret->data = sresize(ret->data, (int)off, unsigned char);
ret->length = off;
return ret;
}
void read_pfa_file(input *in) {
t1_font *tf = snew(t1_font);
tf->data = load_pfa_file(in->currfp, &in->pos);
tf->pos = in->pos;
tf->length1 = tf->length2 = 0;
fclose(in->currfp);
pf_identify(tf);
}
void read_pfb_file(input *in) {
t1_font *tf = snew(t1_font);
tf->data = load_pfb_file(in->currfp, &in->pos);
tf->pos = in->pos;
tf->length1 = tf->length2 = 0;
fclose(in->currfp);
pf_identify(tf);
}
static char *pf_read_token(pfstate *);
/*
* Read a character from the initial plaintext part of a Type 1 font
*/
static int pf_getc(pfstate *pf) {
if (pf->offset == pf->curblock->length) {
if (pf->curblock->next == NULL) return EOF;
pf->curblock = pf->curblock->next;
pf->offset = 0;
}
if (pf->curblock->type != PFB_ASCII) return EOF;
return pf->curblock->data[pf->offset++];
}
static void pf_ungetc(int c, pfstate *pf) {
assert(pf->offset > 0);
pf->offset--;
assert(c == pf->curblock->data[pf->offset]);
}
static void pf_rewind(pfstate *pf) {
pf->curblock = pf->data;
pf->offset = 0;
}
static void pf_seek(pfstate *pf, size_t off) {
t1_data *td = pf->data;
while (td->length < off) {
off -= td->length;
td = td->next;
}
pf->curblock = td;
pf->offset = off;
}
static size_t pf_tell(pfstate *pf) {
t1_data *td = pf->data;
size_t o = 0;
while (td != pf->curblock) {
o += td->length;
td = td->next;
}
return o + pf->offset;
}
static void pf_identify(t1_font *tf) {
rdstringc rsc = { 0, 0, NULL };
char *p;
size_t len;
char *fontname;
font_info *fi;
int c;
pfstate pfs, *pf = &pfs;
pf->data = tf->data;
pf_rewind(pf);
do {
c = pf_getc(pf);
if (c == EOF) {
sfree(rsc.text);
error(err_pfeof, &tf->pos);
return;
}
rdaddc(&rsc, c);
} while (c != 012 && c != 015);
p = rsc.text;
if ((p = strchr(p, ':')) == NULL) {
sfree(rsc.text);
error(err_pfhead, &tf->pos);
return;
}
p++;
p += strspn(p, " \t");
len = strcspn(p, " \t");
fontname = snewn((int)len + 1, char);
memcpy(fontname, p, len);
fontname[len] = 0;
sfree(rsc.text);
for (fi = all_fonts; fi; fi = fi->next) {
if (strcmp(fi->name, fontname) == 0) {
fi->fontfile = tf;
sfree(fontname);
return;
}
}
error(err_pfnoafm, &tf->pos, fontname);
sfree(fontname);
}
/*
* PostScript white space characters; PLRM3 table 3.1
*/
static int pf_isspace(int c) {
return c == 000 || c == 011 || c == 012 || c == 014 || c == 015 ||
c == ' ';
}
/*
* PostScript special characters; PLRM3 page 27
*/
static int pf_isspecial(int c) {
return c == '(' || c == ')' || c == '<' || c == '>' || c == '[' ||
c == ']' || c == '{' || c == '}' || c == '/' || c == '%';
}
static size_t pf_findtoken(t1_font *tf, size_t off, char const *needle) {
char *tok;
pfstate pfs, *pf = &pfs;
pf->data = tf->data;
pf_seek(pf, off);
for (;;) {
tok = pf_read_token(pf);
if (tok == NULL) {
if (pf->offset == 0 && pf->curblock->type == PFB_BINARY)
pf->curblock = pf->curblock->next;
else
return (size_t)-1;
} else {
if (strcmp(tok, needle) == 0) {
sfree(tok);
return pf_tell(pf);
}
sfree(tok);
}
}
}
static size_t pf_length1(t1_font *tf) {
size_t ret;
ret = pf_findtoken(tf, 0, "eexec");
if (ret == (size_t)-1) {
error(err_pfeof, &tf->pos);
return 0;
}
return ret;
}
static size_t pf_length2(t1_font *tf) {
size_t ret;
if (tf->length1 == 0)
tf->length1 = pf_length1(tf);
ret = pf_findtoken(tf, tf->length1, "cleartomark");
if (ret == (size_t)-1) {
error(err_pfeof, &tf->pos);
return 0;
}
return ret - 12 - tf->length1; /* backspace over "cleartomark\n" */
}
static void pf_getascii(t1_font *tf, size_t off, size_t len,
char **bufp, size_t *lenp) {
t1_data *td = tf->data;
size_t blk, i;
char *p;
while (td && off >= td->length) {
off -= td->length;
td = td->next;
}
*bufp = NULL;
*lenp = 0;
while (td && len) {
blk = len < td->length ? len : td->length;
if (td->type == PFB_ASCII) {
*bufp = sresize(*bufp, (int)(*lenp + blk), char);
memcpy(*bufp + *lenp, td->data + off, blk);
*lenp += blk;
} else {
*bufp = sresize(*bufp, (int)(*lenp + blk * 2 + blk / 39 + 3), char);
p = *bufp + *lenp;
for (i = 0; i < blk; i++) {
if (i % 39 == 0) p += sprintf(p, "\n");
p += sprintf(p, "%02x", td->data[off + i]);
}
p += sprintf(p, "\n");
*lenp = p - *bufp;
}
len -= blk;
td = td->next;
off = 0;
}
}
void pf_writeps(font_info const *fi, FILE *ofp) {
char *buf;
size_t len;
pf_getascii(fi->fontfile, 0, INT_MAX, &buf, &len);
fwrite(buf, 1, len, ofp);
sfree(buf);
}
static int hexval(char c) {
if (c >= '0' && c <= '9') return c - '0';
if (c >= 'A' && c <= 'F') return c - 'A' + 0xA;
if (c >= 'a' && c <= 'f') return c - 'a' + 0xa;
return 0;
}
static void pf_getbinary(t1_font *tf, size_t off, size_t len,
char **bufp, size_t *lenp) {
t1_data *td = tf->data;
size_t blk, i;
int havenybble = 0;
char *p, nybble;
while (td && off >= td->length) {
off -= td->length;
td = td->next;
}
*bufp = NULL;
*lenp = 0;
while (td && len) {
blk = len < td->length ? len : td->length;
if (td->type == PFB_BINARY) {
*bufp = sresize(*bufp, (int)(*lenp + blk), char);
memcpy(*bufp + *lenp, td->data + off, blk);
*lenp += blk;
} else {
*bufp = sresize(*bufp, (int)(*lenp + blk / 2 + 1), char);
p = *bufp + *lenp;
for (i = 0; i < blk; i++) {
if (pf_isspace(td->data[off + i])) continue;
if (!havenybble)
nybble = hexval(td->data[off+i]);
else
*p++ = (nybble << 4) | hexval(td->data[off+i]);
havenybble = !havenybble;
}
*lenp = p - *bufp;
}
len -= blk;
td = td->next;
off = 0;
}
}
/*
* Return the initial, unencrypted, part of a font.
*/
void pf_part1(font_info *fi, char **bufp, size_t *lenp) {
t1_font *tf = fi->fontfile;
if (tf->length1 == 0)
tf->length1 = pf_length1(tf);
pf_getascii(tf, 0, tf->length1, bufp, lenp);
}
/*
* Return the middle, encrypted, part of a font.
*/
void pf_part2(font_info *fi, char **bufp, size_t *lenp) {
t1_font *tf = fi->fontfile;
if (tf->length2 == 0)
tf->length2 = pf_length2(tf);
pf_getbinary(tf, tf->length1, tf->length2, bufp, lenp);
if (*lenp >= 256)
*lenp -= 256;
}
static char *pf_read_litstring(pfstate *pf) {
rdstringc rsc = { 0, 0, NULL };
int depth = 1;
int c;
rdaddc(&rsc, '(');
do {
c = pf_getc(pf);
switch (c) {
case '(':
depth++; break;
case ')':
depth--; break;
case '\\':
rdaddc(&rsc, '\\');
c = pf_getc(pf);
break;
}
if (c != EOF) rdaddc(&rsc, c);
} while (depth > 0 && c != EOF);
return rsc.text;
}
static char *pf_read_hexstring(pfstate *pf) {
rdstringc rsc = { 0, 0, NULL };
int c;
rdaddc(&rsc, '<');
do {
c = pf_getc(pf);
if (c != EOF) rdaddc(&rsc, c);
} while (c != '>' && c != EOF);
return rsc.text;
}
static char *pf_read_word(pfstate *pf, int c) {
rdstringc rsc = { 0, 0, NULL };
rdaddc(&rsc, c);
if (c == '{' || c == '}' || c == '[' || c == ']')
return rsc.text;
for (;;) {
c = pf_getc(pf);
if (pf_isspecial(c) || pf_isspace(c) || c == EOF) break;
rdaddc(&rsc, c);
}
if (pf_isspecial(c)) pf_ungetc(c, pf);
return rsc.text;
}
static char *pf_read_token(pfstate *pf) {
int c;
do {
c = pf_getc(pf);
} while (pf_isspace(c));
if (c == EOF) return NULL;
if (c == '%') {
do {
c = pf_getc(pf);
} while (c != 012 && c != 015);
return pf_read_token(pf);
}
if (c == '(') return pf_read_litstring(pf);
if (c == '<') return pf_read_hexstring(pf);
return pf_read_word(pf, c);
}
|