Coverage Report

Created: 2025-11-16 07:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/pdf/pdf_font1C.c
Line
Count
Source
1
/* Copyright (C) 2019-2025 Artifex Software, Inc.
2
   All Rights Reserved.
3
4
   This software is provided AS-IS with no warranty, either express or
5
   implied.
6
7
   This software is distributed under license and may not be copied,
8
   modified or distributed except as expressly authorized under the terms
9
   of the license contained in the file LICENSE in this distribution.
10
11
   Refer to licensing information at http://www.artifex.com or contact
12
   Artifex Software, Inc.,  39 Mesa Street, Suite 108A, San Francisco,
13
   CA 94129, USA, for further information.
14
*/
15
16
/* code for CFF (type 1C) font handling */
17
18
#include "pdf_int.h"
19
20
#include "gscedata.h"
21
#include "gscencs.h"
22
#include "gxfont0.h"
23
#include "gxfcid.h"
24
#include "assert_.h"
25
26
#include "pdf_types.h"
27
#include "pdf_font_types.h"
28
#include "pdf_font.h"
29
#include "pdf_font1C.h"
30
#include "pdf_fontps.h"
31
#include "pdf_dict.h"
32
#include "pdf_deref.h"
33
#include "pdf_file.h"
34
#include "pdf_array.h"
35
36
#include "gxtype1.h"        /* for gs_type1_state_s */
37
#include "gsutil.h"        /* For gs_next_ids() */
38
39
static byte *
40
pdfi_find_cff_index(byte *p, byte *e, int idx, byte **pp, byte **ep);
41
42
/* This is a super set of the contents of a pdfi Type 1C font/CIDFont.
43
   Meaning we can store everying as we interpret, and not worry
44
   about the actual font type until the end
45
 */
46
typedef struct pdfi_cff_font_priv_s {
47
    pdf_font_common;
48
    pdf_array *Subrs;
49
    int NumSubrs;
50
    pdf_array *GlobalSubrs;
51
    int NumGlobalSubrs;
52
    pdf_dict *CharStrings;
53
    byte *cffdata;
54
    byte *cffend;
55
    byte *gsubrs;
56
    byte *subrs;
57
    byte *charstrings;
58
    int ncharstrings;
59
    pdf_dict *CIDSystemInfo;
60
    int64_t DW;
61
    pdf_array *W;
62
    pdf_array *DW2;
63
    pdf_array *W2;
64
    pdf_buffer *cidtogidmap;
65
    pdf_array *FDArray;
66
    /* The registry and ordering strings in gs_font_cid0_data are just references to
67
       strings assumed to be managed be managed by the interpreter - so we have to stash
68
       them in the pdfi font, too.
69
     */
70
    pdf_string *registry;
71
    pdf_string *ordering;
72
    int supplement;
73
    int cidcount;
74
    int uidbase;
75
    font_proc_glyph_info((*orig_glyph_info));
76
} pdfi_cff_font_priv;
77
78
/* Same thing for the Ghostscript font
79
 */
80
typedef struct pdfi_gs_cff_font_priv_s {
81
    gs_font_base_common;
82
    gs_type1_data type1data;
83
    gs_font_cid0_data cidata;
84
    bool forcecid;
85
    pdfi_cff_font_priv pdfcffpriv;
86
} pdfi_gs_cff_font_priv;
87
88
typedef struct pdfi_gs_cff_font_common_priv_s {
89
    gs_font_base_common;
90
} pdfi_gs_cff_font_common_priv;
91
92
typedef struct cff_font_offsets_s
93
{
94
    unsigned int fdarray_off;
95
    unsigned int fdselect_off;
96
    unsigned int charset_off;
97
    unsigned int encoding_off;
98
    unsigned int strings_off;
99
    unsigned int strings_size;
100
    unsigned int private_off;
101
    unsigned int private_size;
102
    bool have_ros;
103
    bool have_matrix;
104
} cff_font_offsets;
105
106
static int
107
pdfi_make_string_from_sid(pdf_context *ctx, pdf_obj **str,
108
                          pdfi_cff_font_priv *font, cff_font_offsets *offsets, unsigned int sid);
109
110
static void
111
pdfi_init_cff_font_priv(pdf_context *ctx, pdfi_gs_cff_font_priv *cffpriv,
112
                        byte *buf, int buflen, bool for_fdarray);
113
114
static int
115
pdfi_alloc_cff_font(pdf_context *ctx, pdf_font_cff ** font, uint32_t obj_num, bool for_fdarray);
116
117
/* CALLBACKS */
118
static int
119
pdfi_cff_glyph_data(gs_font_type1 *pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
120
1.58M
{
121
1.58M
    int code = 0;
122
1.58M
    pdf_font_cff *cfffont = (pdf_font_cff *) pfont->client_data;
123
1.58M
    pdf_context *ctx = (pdf_context *) cfffont->ctx;
124
1.58M
    pdf_name *glyphname = NULL;
125
1.58M
    pdf_string *charstring = NULL;
126
127
    /* Getting here with Encoding == NULL means it's a subfont from an FDArray
128
       so we index directly by gid
129
     */
130
1.58M
    if (cfffont->Encoding == NULL) {
131
0
        char indstring[33];
132
0
        int l = gs_snprintf(indstring, sizeof(indstring), "%u", (unsigned int)glyph);
133
134
0
        code = pdfi_name_alloc(ctx, (byte *) indstring, l, (pdf_obj **) &glyphname);
135
0
        if (code >= 0)
136
0
            pdfi_countup(glyphname);
137
0
    }
138
1.58M
    else {
139
1.58M
        gs_const_string gname;
140
1.58M
        code = (*ctx->get_glyph_name)((gs_font *)pfont, glyph, &gname);
141
1.58M
        if (code >= 0) {
142
1.58M
            code = pdfi_name_alloc(ctx, (byte *) gname.data, gname.size, (pdf_obj **) &glyphname);
143
1.58M
            if (code >= 0)
144
1.58M
                pdfi_countup(glyphname);
145
1.58M
        }
146
1.58M
    }
147
1.58M
    if (code >= 0) {
148
1.58M
        code = pdfi_dict_get_by_key(ctx, cfffont->CharStrings, glyphname, (pdf_obj **) &charstring);
149
1.58M
        if (code < 0) {
150
1.70k
            code = pdfi_map_glyph_name_via_agl(cfffont->CharStrings, glyphname, &charstring);
151
1.70k
        }
152
1.58M
        if (code >= 0)
153
1.58M
            gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
154
1.58M
    }
155
156
1.58M
    pdfi_countdown(glyphname);
157
1.58M
    pdfi_countdown(charstring);
158
159
1.58M
    return code;
160
1.58M
}
161
162
static int
163
pdfi_cff_subr_data(gs_font_type1 *pfont, int index, bool global, gs_glyph_data_t *pgd)
164
1.21M
{
165
1.21M
    int code = 0;
166
1.21M
    pdf_font_cff *cfffont = (pdf_font_cff *) pfont->client_data;
167
168
1.21M
    if ((global &&index >= cfffont->NumGlobalSubrs)||(!global &&index >= cfffont->NumSubrs)) {
169
12.4k
        code = gs_note_error(gs_error_rangecheck);
170
12.4k
    }
171
1.20M
    else {
172
1.20M
        pdf_string *subrstring;
173
1.20M
        pdf_array *s = global ? cfffont->GlobalSubrs : cfffont->Subrs;
174
175
1.20M
        code = pdfi_array_get(cfffont->ctx, s, (uint64_t) index, (pdf_obj **) &subrstring);
176
1.20M
        if (code >= 0) {
177
1.20M
            gs_glyph_data_from_bytes(pgd, subrstring->data, 0, subrstring->length, NULL);
178
1.20M
            pdfi_countdown(subrstring);
179
1.20M
        }
180
1.20M
    }
181
1.21M
    return code;
182
1.21M
}
183
184
static int
185
pdfi_cff_seac_data(gs_font_type1 *pfont, int ccode, gs_glyph *pglyph, gs_const_string *gstr, gs_glyph_data_t *pgd)
186
2.75k
{
187
2.75k
    int code = 0;
188
2.75k
    pdf_font_cff *cfffont = (pdf_font_cff *) pfont->client_data;
189
2.75k
    pdf_context *ctx = (pdf_context *) cfffont->ctx;
190
2.75k
    gs_glyph glyph = gs_c_known_encode((gs_char)ccode, ENCODING_INDEX_STANDARD);
191
192
2.75k
    if (glyph == GS_NO_GLYPH)
193
1.19k
        return_error(gs_error_rangecheck);
194
195
1.56k
    code = gs_c_glyph_name(glyph, gstr);
196
197
1.56k
    if (code >= 0) {
198
1.56k
        unsigned int nindex;
199
1.56k
        code = (*ctx->get_glyph_index)((gs_font *)pfont, (byte *)gstr->data, gstr->size, &nindex);
200
1.56k
        if (pglyph != NULL)
201
1.49k
            *pglyph = (gs_glyph)nindex;
202
1.56k
    }
203
204
1.56k
    if (code >= 0) {
205
1.56k
        pdf_name *glyphname = NULL;
206
1.56k
        pdf_string *charstring = NULL;
207
1.56k
        code = pdfi_name_alloc(ctx, (byte *) gstr->data, gstr->size, (pdf_obj **) &glyphname);
208
1.56k
        if (code >= 0) {
209
1.56k
            pdfi_countup(glyphname);
210
1.56k
            code = pdfi_dict_get_by_key(ctx, cfffont->CharStrings, glyphname, (pdf_obj **)&charstring);
211
1.56k
            pdfi_countdown(glyphname);
212
1.56k
            if (code >= 0) {
213
1.46k
                if (pgd != NULL) {
214
39
                    gs_glyph_data_from_bytes(pgd, charstring->data, 0, charstring->length, NULL);
215
39
                }
216
1.46k
                pdfi_countdown(charstring);
217
1.46k
            }
218
1.56k
        }
219
1.56k
    }
220
221
1.56k
    return code;
222
2.75k
}
223
224
/* push/pop are null ops here */
225
static int
226
pdfi_cff_push(void *callback_data, const fixed *pf, int count)
227
0
{
228
0
    (void)callback_data;
229
0
    (void)pf;
230
0
    (void)count;
231
0
    return 0;
232
0
}
233
static int
234
pdfi_cff_pop(void *callback_data, fixed *pf)
235
0
{
236
0
    (void)callback_data;
237
0
    (void)pf;
238
0
    return 0;
239
0
}
240
241
static int
242
pdfi_cff_enumerate_glyph(gs_font *pfont, int *pindex,
243
                         gs_glyph_space_t glyph_space, gs_glyph *pglyph)
244
325k
{
245
325k
    int code, j;
246
325k
    pdf_name *key = NULL;
247
325k
    uint64_t i = (uint64_t) *pindex;
248
325k
    pdf_dict *cstrings;
249
325k
    pdf_font *pdffont = (pdf_font *) pfont->client_data;
250
325k
    pdf_context *ctx = (pdf_context *) pdffont->ctx;
251
252
325k
    (void)glyph_space;
253
254
    /* Slightly naff: if build_char is NULL, this is an FDArray subfont */
255
325k
    if (pfont->procs.build_char == NULL) {
256
1.87k
        *pindex = 0;
257
1.87k
        *pglyph = GS_NO_GLYPH;
258
1.87k
        return 0;
259
1.87k
    }
260
323k
    else if (pdffont->pdfi_font_type == e_pdf_cidfont_type0) {
261
829
        pdf_cidfont_type0 *cffcidfont = (pdf_cidfont_type0 *) pdffont;
262
829
        cstrings = cffcidfont->CharStrings;
263
829
    }
264
322k
    else {
265
322k
        pdf_font_cff *cfffont = (pdf_font_cff *) pdffont;
266
267
322k
        cstrings = cfffont->CharStrings;
268
322k
    }
269
323k
    if (*pindex <= 0)
270
17.6k
        code = pdfi_dict_key_first(pdffont->ctx, cstrings, (pdf_obj **) &key, &i);
271
306k
    else
272
306k
        code = pdfi_dict_key_next(pdffont->ctx, cstrings, (pdf_obj **) &key, &i);
273
323k
    if (code < 0) {
274
5.10k
        i = 0;
275
5.10k
        code = gs_note_error(gs_error_undefined);
276
5.10k
    }
277
    /* If Encoding == NULL, it's an FDArray subfont */
278
318k
    else if (pdffont->pdfi_font_type != e_pdf_cidfont_type0 && pdffont->Encoding != NULL) {
279
317k
        unsigned int nindex;
280
317k
        code = (*ctx->get_glyph_index)(pfont, key->data, key->length, &nindex);
281
317k
        if (code < 0) {
282
8
            code = (*ctx->get_glyph_index)(pfont, (byte *)".notdef", 7, &nindex);
283
8
            if (code < 0)
284
0
                *pglyph = GS_NO_GLYPH;
285
8
            else
286
8
                *pglyph = (gs_glyph)nindex;
287
8
        }
288
317k
        else
289
317k
            *pglyph = (gs_glyph)nindex;
290
317k
    }
291
802
    else {
292
802
        char kbuf[32];
293
802
        int l;
294
802
        unsigned int val;
295
        /* If this font started life as a CFF font that we've force to
296
           act like a CIDFont, we can end up with a ".notdef" glyph name
297
         */
298
802
        if (key->length == 7 && memcmp(key->data, ".notdef", 7) == 0) {
299
1
            val = 0;
300
1
            l = 1;
301
1
        }
302
801
        else {
303
801
            memcpy(kbuf, key->data, key->length);
304
801
            kbuf[key->length] = 0;
305
306
801
            l = sscanf(kbuf, "%ud", &val);
307
801
        }
308
802
        if (l > 0) {
309
802
            pdf_cidfont_type0 *cffcidfont = (pdf_cidfont_type0 *) pdffont;
310
802
            if (cffcidfont->cidtogidmap != NULL && cffcidfont->cidtogidmap->length > 0) {
311
67.2k
                for (j = (cffcidfont->cidtogidmap->length >> 1) - 1; j >= 0; j--) {
312
67.2k
                    if (val == (cffcidfont->cidtogidmap->data[j << 1] << 8 | cffcidfont->cidtogidmap->data[(j << 1) + 1])) {
313
15
                        val = j;
314
15
                        break;
315
15
                    }
316
67.2k
                }
317
17
            }
318
802
            *pglyph = (gs_glyph) (val) + GS_MIN_CID_GLYPH;
319
802
        }
320
802
    }
321
323k
    *pindex = (int)i;
322
323k
    pdfi_countdown(key);
323
323k
    return code;
324
325k
}
325
326
/* This *should* only get called for SEAC lookups, which have to come from StandardEncoding
327
   so just try to lookup the string in the standard encodings
328
 */
329
int
330
pdfi_cff_global_glyph_code(const gs_font *pfont, gs_const_string *gstr, gs_glyph *pglyph)
331
62
{
332
62
    *pglyph = gs_c_name_glyph(gstr->data, gstr->size);
333
62
    return 0;
334
62
}
335
336
static int
337
pdfi_cff_glyph_outline(gs_font *pfont, int WMode, gs_glyph glyph,
338
                       const gs_matrix *pmat, gx_path *ppath, double sbw[4])
339
296k
{
340
296k
    gs_glyph_data_t gd;
341
296k
    gs_glyph_data_t *pgd = &gd;
342
296k
    gs_font_type1 *pfont1;
343
296k
    int code;
344
345
296k
    if (pfont->FontType == ft_CID_encrypted) {
346
35.2k
        gs_font_cid0 *pfcid0 = (gs_font_cid0 *) pfont;
347
35.2k
        int fididx = 0;
348
349
35.2k
        code = (*pfcid0->cidata.glyph_data) ((gs_font_base *) pfont, glyph, pgd, &fididx);
350
35.2k
        if (fididx < pfcid0->cidata.FDArray_size)
351
35.2k
            pfont1 = pfcid0->cidata.FDArray[fididx];
352
0
        else
353
0
            code = gs_note_error(gs_error_invalidaccess);
354
35.2k
    }
355
260k
    else {
356
260k
        pfont1 = (gs_font_type1 *) pfont;
357
260k
        code = (*pfont1->data.procs.glyph_data) ((gs_font_type1 *) pfont, glyph, pgd);
358
260k
    }
359
360
296k
    if (code >= 0) {
361
294k
        gs_type1_state cis = { 0 };
362
294k
        gs_type1_state *pcis = &cis;
363
294k
        gs_gstate gs;
364
294k
        int value;
365
366
294k
        if (pmat)
367
22.8k
            gs_matrix_fixed_from_matrix(&gs.ctm, pmat);
368
271k
        else {
369
271k
            gs_matrix imat;
370
371
271k
            gs_make_identity(&imat);
372
271k
            gs_matrix_fixed_from_matrix(&gs.ctm, &imat);
373
271k
        }
374
294k
        gs.flatness = 0;
375
294k
        code = gs_type1_interp_init(pcis, &gs, ppath, NULL, NULL, true, 0, pfont1);
376
294k
        if (code < 0)
377
0
            return code;
378
379
294k
        pcis->no_grid_fitting = true;
380
294k
        gs_type1_set_callback_data(pcis, NULL);
381
        /* Continue interpreting. */
382
587k
      icont:
383
587k
        code = pfont1->data.interpret(pcis, pgd, &value);
384
587k
        switch (code) {
385
288k
            case 0:            /* all done */
386
                /* falls through */
387
294k
            default:           /* code < 0, error */
388
294k
                return code;
389
0
            case type1_result_callothersubr:   /* unknown OtherSubr */
390
0
                return_error(gs_error_rangecheck);      /* can't handle it */
391
293k
            case type1_result_sbw:     /* [h]sbw, just continue */
392
293k
                type1_cis_get_metrics(pcis, sbw);
393
293k
                pgd = 0;
394
293k
                goto icont;
395
587k
        }
396
587k
    }
397
1.73k
    return code;
398
296k
}
399
static int
400
pdfi_cff_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat, int members, gs_glyph_info_t *info)
401
1.25M
{
402
1.25M
    if ((members & GLYPH_INFO_OUTLINE_WIDTHS) == 0)
403
1.23M
        return gs_type1_glyph_info(font, glyph, pmat, members, info);
404
405
14.1k
    return gs_default_glyph_info(font, glyph, pmat, members, info);
406
1.25M
}
407
408
static int
409
pdfi_cff_fdarray_glyph_data(gs_font_type1 *pfont, gs_glyph glyph, gs_glyph_data_t *pgd)
410
0
{
411
0
    return_error(gs_error_invalidfont);
412
0
}
413
414
static int
415
pdfi_cff_fdarray_seac_data(gs_font_type1 *pfont, int ccode,
416
                           gs_glyph *pglyph, gs_const_string *gstr, gs_glyph_data_t *pgd)
417
0
{
418
0
    return_error(gs_error_invalidfont);
419
0
}
420
421
/* Note that pgd may be NULL - so only retrieve the fidx */
422
static int
423
pdfi_cff_cid_glyph_data(gs_font_base *pbfont, gs_glyph glyph, gs_glyph_data_t *pgd, int *pfidx)
424
926k
{
425
926k
    int code = 0;
426
926k
    pdf_cidfont_type0 *pdffont9 = (pdf_cidfont_type0 *) pbfont->client_data;
427
926k
    gs_font_cid0 *gscidfont = (gs_font_cid0 *) pbfont;
428
926k
    pdf_name *glyphname = NULL;
429
926k
    pdf_string *charstring = NULL;
430
926k
    char nbuf[64];
431
926k
    uint32_t l;
432
926k
    gs_glyph gid;
433
434
926k
    *pfidx = 0;
435
436
926k
    if (glyph < GS_MIN_CID_GLYPH)
437
328k
        gid = glyph;
438
598k
    else
439
598k
        gid = glyph - GS_MIN_CID_GLYPH;
440
441
926k
    if (pdffont9->cidtogidmap != NULL && pdffont9->cidtogidmap->length > (gid << 1) + 1) {
442
1.73k
        gid = pdffont9->cidtogidmap->data[gid << 1] << 8 | pdffont9->cidtogidmap->data[(gid << 1) + 1];
443
1.73k
    }
444
445
926k
    l = gs_snprintf(nbuf, sizeof(nbuf), "%" PRId64, gid);
446
447
926k
    code = pdfi_name_alloc(pdffont9->ctx, (byte *) nbuf, l, (pdf_obj **) &glyphname);
448
926k
    if (code >= 0) {
449
926k
        pdfi_countup(glyphname);
450
926k
        code = pdfi_dict_get_by_key(pdffont9->ctx, pdffont9->CharStrings, glyphname, (pdf_obj **) &charstring);
451
926k
        if (code >= 0 && charstring->length >= gscidfont->cidata.FDBytes) {
452
566k
            if (gscidfont->cidata.FDBytes != 0) {
453
545k
                if ((int)charstring->data[0] >= gscidfont->cidata.FDArray_size)
454
56
                    code = gs_note_error(gs_error_invalidfont);
455
544k
                else
456
544k
                    *pfidx = (int)charstring->data[0];
457
545k
            }
458
459
566k
            if (code >= 0 && pgd && ((int64_t)charstring->length - (int64_t)gscidfont->cidata.FDBytes) >= 0)
460
232k
                gs_glyph_data_from_bytes(pgd, charstring->data + gscidfont->cidata.FDBytes, 0, charstring->length - gscidfont->cidata.FDBytes, NULL);
461
566k
        }
462
926k
    }
463
926k
    pdfi_countdown(charstring);
464
926k
    pdfi_countdown(glyphname);
465
466
926k
    return code;
467
926k
}
468
469
470
static int
471
pdfi_cff_cidfont_glyph_info(gs_font *font, gs_glyph glyph, const gs_matrix *pmat,
472
                     int members, gs_glyph_info_t *info)
473
35.2k
{
474
35.2k
    int code;
475
35.2k
    gs_font_cid0 *pcidfont = (gs_font_cid0 *)font;
476
35.2k
    pdf_cidfont_type0 *pdffont9 = (pdf_cidfont_type0 *)font->client_data;
477
35.2k
    code = (*pdffont9->orig_glyph_info)(font, glyph, pmat, members, info);
478
35.2k
    if (code < 0)
479
1.32k
        return code;
480
481
33.9k
    if ((members & GLYPH_INFO_WIDTHS) != 0
482
28.2k
      && glyph > GS_MIN_CID_GLYPH
483
27.7k
      && glyph < GS_MIN_GLYPH_INDEX) {
484
27.7k
        double widths[6] = {0};
485
27.7k
        int fidx;
486
27.7k
        gs_matrix imat;
487
27.7k
        gs_matrix mat1 = font->FontMatrix;
488
27.7k
        gs_glyph g = glyph - GS_MIN_CID_GLYPH;
489
490
27.7k
        code = (*pcidfont->cidata.glyph_data) ((gs_font_base *)font, g + GS_MIN_CID_GLYPH, NULL, &fidx);
491
27.7k
        if (code < 0)
492
0
            return code;
493
27.7k
        if (fidx < pcidfont->cidata.FDArray_size) {
494
27.7k
            gs_font_type1 *pfdfont = pcidfont->cidata.FDArray[fidx];
495
            /* The following cannot fail - if the matrix multiplication didn't work
496
               we'd have errored out at a higher level
497
             */
498
27.7k
            (void)gs_matrix_multiply(&font->FontMatrix, &pfdfont->FontMatrix, &mat1);
499
27.7k
        }
500
27.7k
        code = gs_matrix_invert(&mat1, &imat);
501
27.7k
        if (code < 0)
502
0
            return code; /* By this stage, this should be impossible */
503
27.7k
        if (pmat) {
504
8.35k
            gs_matrix_multiply(&imat, pmat, &mat1);
505
8.35k
        }
506
19.4k
        else {
507
19.4k
            mat1 = imat;
508
19.4k
        }
509
510
27.7k
        code = pdfi_get_cidfont_glyph_metrics(font, g, widths, true);
511
27.7k
        if (code >= 0) {
512
27.7k
            code = gs_point_transform(widths[GLYPH_W0_WIDTH_INDEX] / 1000.0, widths[GLYPH_W0_HEIGHT_INDEX] / 1000.0, &mat1, &info->width[0]);
513
27.7k
            if (code < 0)
514
0
                return code;
515
27.7k
            info->members |= GLYPH_INFO_WIDTH0;
516
517
27.7k
            if ((members & GLYPH_INFO_WIDTH1) != 0 && (widths[GLYPH_W1_WIDTH_INDEX] != 0 || widths[GLYPH_W1_HEIGHT_INDEX] != 0)) {
518
4.92k
                code = gs_point_transform(widths[GLYPH_W1_WIDTH_INDEX] / 1000.0, widths[GLYPH_W1_HEIGHT_INDEX] / 1000.0, &mat1, &info->width[1]);
519
4.92k
                info->members |= GLYPH_INFO_WIDTH1;
520
4.92k
            }
521
27.7k
            if ((members & GLYPH_INFO_VVECTOR1) != 0) {
522
4.92k
                code = gs_point_transform(widths[GLYPH_W1_V_X_INDEX] / 1000.0, widths[GLYPH_W1_V_Y_INDEX] / 1000.0, &mat1, &info->v);
523
4.92k
                info->members |= GLYPH_INFO_VVECTOR1;
524
4.92k
            }
525
27.7k
        }
526
27.7k
    }
527
33.9k
    return code;
528
33.9k
}
529
530
/* END CALLBACKS */
531
532
#if 0                           /* not currently used */
533
static inline int
534
s16(const byte *p)
535
{
536
    return (signed short)((p[0] << 8) | p[1]);
537
}
538
#endif /* not currently used */
539
540
static inline int
541
u16(const byte *p, const byte *e, int *ret)
542
695M
{
543
695M
    if (p + 1 > e) {
544
0
        *ret = 0;
545
0
        return_error(gs_error_invalidfont);
546
0
    }
547
695M
    *ret = (p[0] << 8) | p[1];
548
695M
    return 0;
549
695M
}
550
551
static inline int
552
u24(const byte *p, const byte *e, int *ret)
553
18.0k
{
554
18.0k
    if (p + 2 > e) {
555
0
        *ret = 0;
556
0
        return_error(gs_error_invalidfont);
557
0
    }
558
18.0k
    *ret = (p[0] << 16) | (p[1] << 8) | p[2];
559
18.0k
    return 0;
560
18.0k
}
561
562
static inline int
563
u32(const byte *p, const byte *e, int *ret)
564
24.5k
{
565
24.5k
    if (p + 3 > e) {
566
1
        *ret = 0;
567
1
        return_error(gs_error_invalidfont);
568
1
    }
569
24.5k
    *ret = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
570
24.5k
    return 0;
571
24.5k
}
572
573
574
static int
575
subrbias(int count)
576
11.7k
{
577
11.7k
    return count < 1240 ? 107 : count < 33900 ? 1131 : 32768;
578
11.7k
}
579
580
static int
581
uofs(const byte *p, const byte *e, int offsize, int *ret)
582
18.3M
{
583
18.3M
    if (p >= e) {
584
21
        *ret = 0;
585
21
        return_error(gs_error_invalidfont);
586
21
    }
587
18.3M
    if (offsize == 1) {
588
1.64M
        *ret = p[0];
589
1.64M
        return 0;
590
1.64M
    }
591
16.7M
    if (offsize == 2)
592
16.6M
        return u16(p, e, ret);
593
41.2k
    if (offsize == 3)
594
18.0k
        return u24(p, e, ret);
595
23.1k
    if (offsize == 4)
596
23.1k
        return u32(p, e, ret);
597
598
23.1k
    return_error(gs_error_invalidfont);
599
23.1k
}
600
601
static int
602
iso_adobe_charset_proc(const byte *p, const byte *pe, unsigned i)
603
7.78k
{
604
7.78k
    if (i < 228)
605
7.78k
        return i + 1;
606
0
    else
607
0
        return_error(gs_error_rangecheck);
608
7.78k
}
609
610
static int
611
expert_charset_proc(const byte *p, const byte *pe, unsigned i)
612
0
{
613
0
    if (i < gs_c_known_encoding_lengths[6])
614
0
        return gs_c_known_encodings[6][i];
615
616
0
    return_error(gs_error_rangecheck);
617
0
}
618
619
static int
620
expert_subset_charset_proc(const byte *p, const byte *pe, unsigned int i)
621
0
{
622
#if 0
623
    if (i < sizeof(expert_subset_charset) / sizeof(*expert_subset_charset))
624
        return expert_subset_charset[i];
625
#endif
626
0
    return_error(gs_error_rangecheck);
627
0
}
628
629
static int
630
format0_charset_proc(const byte *p, const byte *pe, unsigned int i)
631
111k
{
632
111k
    int code, ret;
633
111k
    if (p + 2 * i > pe)
634
0
        return gs_error_rangecheck;
635
636
111k
    if ((code = u16(p + 2 * i, pe, &ret)) < 0) {
637
0
        return code;
638
0
    }
639
111k
    return ret;
640
111k
}
641
642
static int
643
format1_charset_proc(const byte *p, const byte *pe, unsigned int i)
644
360k
{
645
360k
    int code = gs_error_rangecheck;
646
360k
    unsigned int cid = 0;
647
648
7.11M
    while (p < pe - 3) {
649
7.11M
        unsigned int first, count;
650
651
7.11M
        code = (unsigned int)u16(p, pe, (int *)&first);
652
7.11M
        if (code < 0)
653
0
            break;
654
7.11M
        count = (unsigned int)p[2] + 1;
655
656
7.11M
        if (i < cid + count) {
657
360k
            code = first + i - cid;
658
360k
            break;
659
360k
        }
660
6.75M
        p += 3;
661
6.75M
        cid += count;
662
6.75M
    }
663
360k
    return code;
664
360k
}
665
666
static int
667
format2_charset_proc(const byte *p, const byte *pe, unsigned int i)
668
4.62M
{
669
4.62M
    int code = gs_error_rangecheck;
670
4.62M
    unsigned int cid = 0;
671
672
4.62M
    while (p < pe - 4) {
673
4.62M
        unsigned int first, count;
674
675
4.62M
        code = u16(p, pe, (int *)&first);
676
4.62M
        if (code >= 0)
677
4.62M
            code = u16(p + 2, pe, (int *)&count);
678
4.62M
        if (code < 0)
679
0
            break;
680
681
4.62M
        count += 1;
682
683
4.62M
        if (i < cid + count) {
684
4.62M
            code = first + i - cid;
685
4.62M
            break;
686
4.62M
        }
687
0
        p += 4;
688
0
        cid += count;
689
0
    }
690
4.62M
    return code;
691
4.62M
}
692
693
static int
694
format0_fdselect_proc(const byte *p, const byte *pe, unsigned int i)
695
2.09k
{
696
2.09k
    return (int)(*(p + i));
697
2.09k
}
698
699
static int
700
format3_fdselect_proc(const byte *p, const byte *pe, unsigned int i)
701
4.63M
{
702
4.63M
    unsigned int n_ranges;
703
4.63M
    int code;
704
705
4.63M
    if ((code = u16(p, pe, (int *)&n_ranges)) < 0)
706
0
        return code;
707
708
4.63M
    p += 2;
709
710
326M
    while (n_ranges-- && p + 5 <= pe) {
711
326M
        unsigned int first, last;
712
713
326M
        code = u16(p, pe, (int *)&first);
714
326M
        if (code >= 0)
715
326M
            code = u16(p + 3, pe, (int *)&last);
716
717
326M
        if (code < 0)
718
0
            break;
719
720
326M
        if (i >= first && i < last) {
721
4.63M
            return (int)(*(p + 2));
722
4.63M
        }
723
321M
        p += 3;
724
321M
    }
725
4.63M
    return_error(gs_error_rangecheck);
726
4.63M
}
727
728
729
static byte *
730
pdfi_read_cff_real(byte *p, byte *e, float *val)
731
22.2k
{
732
22.2k
    char buf[65];
733
22.2k
    char *txt = buf;
734
735
    /* b0 was 30 */
736
737
86.5k
    while (txt < buf + (sizeof buf) - 5 && p < e) {
738
86.3k
        int b, n;
739
740
86.3k
        b = *p++;
741
742
86.3k
        n = (b >> 4) &0xf;
743
86.3k
        if (n < 0xA) {
744
60.6k
            *txt++ = n + '0';
745
60.6k
        }
746
25.7k
        else if (n == 0xA) {
747
12.6k
            *txt++ = '.';
748
12.6k
        }
749
13.0k
        else if (n == 0xB) {
750
227
            *txt++ = 'E';
751
227
        }
752
12.8k
        else if (n == 0xC) {
753
233
            *txt++ = 'E';
754
233
            *txt++ = '-';
755
233
        }
756
12.5k
        else if (n == 0xE) {
757
1.37k
            *txt++ = '-';
758
1.37k
        }
759
11.2k
        else if (n == 0xF) {
760
11.0k
            break;
761
11.0k
        }
762
763
75.2k
        n = b &0xf;
764
75.2k
        if (n < 0xA) {
765
55.5k
            *txt++ = n + '0';
766
55.5k
        }
767
19.6k
        else if (n == 0xA) {
768
7.45k
            *txt++ = '.';
769
7.45k
        }
770
12.2k
        else if (n == 0xB) {
771
435
            *txt++ = 'E';
772
435
        }
773
11.8k
        else if (n == 0xC) {
774
304
            *txt++ = 'E';
775
304
            *txt++ = '-';
776
304
        }
777
11.4k
        else if (n == 0xE) {
778
276
            *txt++ = '-';
779
276
        }
780
11.2k
        else if (n == 0xF) {
781
10.9k
            break;
782
10.9k
        }
783
75.2k
    }
784
785
22.2k
    *txt = 0;
786
787
22.2k
    *val = atof(buf);
788
789
22.2k
    return p;
790
22.2k
}
791
792
static byte *
793
pdfi_read_cff_integer(byte *p, byte *e, int b0, int *val)
794
482k
{
795
482k
    int b1, b2, b3, b4;
796
797
482k
    if (b0 == 28) {
798
28.7k
        if (p + 2 > e) {
799
23
            gs_throw(-1, "corrupt dictionary (integer)");
800
23
            return 0;
801
23
        }
802
28.7k
        b1 = *p++;
803
28.7k
        b2 = *p++;
804
28.7k
        *val = (b1 << 8) | b2;
805
28.7k
    }
806
807
453k
    else if (b0 == 29) {
808
11.7k
        if (p + 4 > e) {
809
13
            gs_throw(-1, "corrupt dictionary (integer)");
810
13
            return 0;
811
13
        }
812
11.6k
        b1 = *p++;
813
11.6k
        b2 = *p++;
814
11.6k
        b3 = *p++;
815
11.6k
        b4 = *p++;
816
11.6k
        *val = (b1 << 24) | (b2 << 16) | (b3 << 8) | b4;
817
11.6k
    }
818
819
442k
    else if (b0 < 247) {
820
220k
        *val = b0 - 139;
821
220k
    }
822
823
222k
    else if (b0 < 251) {
824
176k
        if (p + 1 > e) {
825
318
            gs_throw(-1, "corrupt dictionary (integer)");
826
318
            return 0;
827
318
        }
828
176k
        b1 = *p++;
829
176k
        *val = (b0 - 247) * 256 + b1 + 108;
830
176k
    }
831
832
45.4k
    else {
833
45.4k
        if (p + 1 > e) {
834
56
            gs_throw(-1, "corrupt dictionary (integer)");
835
56
            return 0;
836
56
        }
837
45.3k
        b1 = *p++;
838
45.3k
        *val = -(b0 - 251) * 256 - b1 - 108;
839
45.3k
    }
840
841
482k
    return p;
842
482k
}
843
844
static inline void
845
pdfi_cff_font_priv_defaults(pdfi_gs_cff_font_priv *ptpriv)
846
15.8k
{
847
15.8k
    ptpriv->type1data.BlueScale = 0.039625f;
848
15.8k
    ptpriv->type1data.BlueShift = 7;
849
15.8k
    ptpriv->type1data.BlueFuzz = 1;
850
15.8k
    ptpriv->type1data.ExpansionFactor = 0.06f;
851
15.8k
}
852
853
750k
#define PDFI_CFF_STACK_SIZE 48
854
855
static int
856
pdfi_read_cff_dict(byte *p, byte *e, pdfi_gs_cff_font_priv *ptpriv, cff_font_offsets *offsets, bool topdict)
857
37.9k
{
858
37.9k
    pdfi_cff_font_priv *font = &ptpriv->pdfcffpriv;
859
37.9k
    struct
860
37.9k
    {
861
37.9k
        int ival;
862
37.9k
        float fval;
863
37.9k
    } args[PDFI_CFF_STACK_SIZE];
864
37.9k
    int offset;
865
37.9k
    int b0, n;
866
37.9k
    double f;
867
37.9k
    int i;
868
37.9k
    int code = 0;
869
37.9k
    bool do_priv = false;
870
871
37.9k
    memset(args, 0x00, sizeof(args));
872
873
37.9k
    offset = p - font->cffdata;
874
875
37.9k
    n = 0;
876
791k
    while (p < e && code >= 0) {
877
753k
        b0 = *p;
878
753k
        p++;
879
880
753k
        switch (b0) {
881
226
            case 22:
882
271
            case 23:
883
442
            case 24:
884
609
            case 25:
885
1.10k
            case 26:
886
1.52k
            case 27:
887
2.03k
            case 31:
888
2.90k
            case 255:
889
2.90k
                continue;
890
750k
            default:
891
750k
                break;
892
753k
        }
893
894
750k
        if (b0 < 22) {
895
245k
            if (b0 == 12) {
896
60.1k
                if (p + 1 > e) {
897
35
                    return gs_throw(-1, "corrupt dictionary (operator)");
898
35
                }
899
60.1k
                b0 = 0x100 | *p++;
900
60.1k
            }
901
245k
            switch (b0) {
902
12.0k
                case 1:
903
12.0k
                {
904
12.0k
                    code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->notice, font, offsets, args[0].ival);
905
12.0k
                    break;
906
0
                }
907
6.54k
                case 2:
908
6.54k
                {
909
6.54k
                    code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->fullname, font, offsets, args[0].ival);
910
6.54k
                    break;
911
0
                }
912
6.58k
                case 3:
913
6.58k
                {
914
6.58k
                    code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->familyname, font, offsets, args[0].ival);
915
6.58k
                    break;
916
0
                }
917
1.93k
                case 13: /* UniqueID */
918
                  /* UID may not be less than 0, that makes it an XUID */
919
1.93k
                  if (args[0].ival >= 0)
920
1.91k
                      uid_set_UniqueID(&ptpriv->UID, args[0].ival);
921
1.93k
                  break;
922
923
4.36k
                case 14: /* XUID */
924
4.36k
                {
925
4.36k
                    long *xvalues = NULL;
926
927
4.36k
                    if (n > 0) {
928
2.99k
                        xvalues = (long *)gs_alloc_byte_array(font->pfont->memory, n, sizeof(long), "pdfi_read_cff_dict");
929
2.99k
                        if (xvalues == NULL) {
930
0
                            uid_set_invalid(&ptpriv->UID);
931
0
                        }
932
2.99k
                        else {
933
18.3k
                            for (i = 1; i <= n; i++) {
934
15.4k
                                xvalues[n - i] = args[i - 1].ival;
935
15.4k
                            }
936
2.99k
                            if (uid_is_XUID(&ptpriv->UID))
937
2.99k
                                uid_free(&ptpriv->UID, font->pfont->memory, "pdfi_read_cff_dict");
938
2.99k
                            uid_set_XUID(&ptpriv->UID, xvalues, n);
939
2.99k
                        }
940
2.99k
                    } else
941
1.37k
                        uid_set_invalid(&ptpriv->UID);
942
4.36k
                    break;
943
0
                }
944
945
                /* some CFF file offsets */
946
15.6k
                case 15:
947
15.6k
                {
948
15.6k
                    if (args[0].ival < 0) {
949
32
                        code = gs_note_error(gs_error_invalidfont);
950
32
                        break;
951
32
                    }
952
15.6k
                    offsets->charset_off = args[0].ival;
953
15.6k
                    break;
954
15.6k
                }
955
8.52k
                case 16:
956
8.52k
                {
957
8.52k
                    if (args[0].ival < 0) {
958
13
                        code = gs_note_error(gs_error_invalidfont);
959
13
                        break;
960
13
                    }
961
8.51k
                    offsets->encoding_off = args[0].ival;
962
8.51k
                    break;
963
8.52k
                }
964
15.7k
                case 17:
965
15.7k
                {
966
15.7k
                    if (args[0].ival < 0) {
967
12
                        code = gs_note_error(gs_error_invalidfont);
968
12
                        break;
969
12
                    }
970
15.7k
                    font->charstrings = font->cffdata + args[0].ival;
971
15.7k
                    break;
972
15.7k
                }
973
974
16.8k
                case 18:
975
16.8k
                {
976
16.8k
                    offsets->private_size = args[0].ival;
977
16.8k
                    if (args[1].ival < 0) {
978
45
                        code = gs_note_error(gs_error_invalidfont);
979
45
                        break;
980
45
                    }
981
16.8k
                    offsets->private_off = args[1].ival;
982
                    /* Catch a broken font with a self referencing Private dict */
983
16.8k
                    if (topdict == true)
984
16.8k
                        do_priv = offsets->private_size > 0 ? true : false;
985
41
                    else {
986
41
                        do_priv = false;
987
41
                        code = gs_error_invalidfont;
988
41
                        break;
989
41
                    }
990
16.8k
                    break;
991
16.8k
                }
992
993
16.8k
                case 19:
994
945
                {
995
945
                    if (args[0].ival < 0) {
996
50
                        code = gs_note_error(gs_error_invalidfont);
997
50
                        break;
998
50
                    }
999
895
                    font->subrs = font->cffdata + offset + args[0].ival;
1000
895
                    break;
1001
945
                }
1002
1003
2.49k
                case 256 | 30:
1004
2.49k
                {
1005
2.49k
                    code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->registry, font, offsets, args[0].ival);
1006
2.49k
                    if (code < 0)
1007
5
                        break;
1008
2.49k
                    code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->ordering, font, offsets, args[1].ival);
1009
2.49k
                    if (code < 0)
1010
0
                        break;
1011
2.49k
                    font->supplement = args[2].ival;
1012
2.49k
                    offsets->have_ros = true;
1013
2.49k
                    ptpriv->FontType = ft_CID_encrypted;
1014
2.49k
                    break;
1015
2.49k
                }
1016
1017
2.48k
                case 256 | 34:
1018
2.48k
                {
1019
2.48k
                    font->cidcount = args[0].ival;
1020
2.48k
                    break;
1021
2.49k
                }
1022
1023
471
                case 256 | 35:
1024
471
                {
1025
471
                    font->uidbase = args[0].ival;
1026
471
                    break;
1027
2.49k
                }
1028
1029
2.48k
                case 256 | 36:
1030
2.48k
                {
1031
2.48k
                    offsets->fdarray_off = args[0].ival;
1032
2.48k
                    break;
1033
2.49k
                }
1034
1035
2.48k
                case 256 | 37:
1036
2.48k
                {
1037
2.48k
                    offsets->fdselect_off = args[0].ival;
1038
2.48k
                    break;
1039
2.49k
                }
1040
1041
3.58k
                case 256 | 38:
1042
3.58k
                {
1043
3.58k
                    pdf_string *fnamestr = NULL;
1044
1045
3.58k
                    code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &fnamestr, font, offsets, args[0].ival);
1046
3.58k
                    if (code >= 0) {
1047
3.57k
                        int nlen = fnamestr->length > gs_font_name_max ? gs_font_name_max : fnamestr->length;
1048
3.57k
                        memcpy(ptpriv->font_name.chars, fnamestr->data, nlen);
1049
3.57k
                        memcpy(ptpriv->key_name.chars, fnamestr->data, nlen);
1050
3.57k
                        ptpriv->font_name.size = ptpriv->key_name.size = nlen;
1051
3.57k
                        pdfi_countdown(fnamestr);
1052
3.57k
                    }
1053
3.58k
                    break;
1054
2.49k
                }
1055
1056
                /* Type1 stuff that need to be set for the ptpriv struct */
1057
1058
35
                case 256 | 6:
1059
35
                {
1060
35
                    if (args[0].ival == 1) {
1061
0
                        ptpriv->type1data.interpret = gs_type1_interpret;
1062
0
                        ptpriv->type1data.lenIV = -1;       /* FIXME */
1063
0
                    }
1064
35
                    break;
1065
2.49k
                }
1066
1067
1.00k
                case 256 | 7:
1068
1.00k
                {
1069
1.00k
                    ptpriv->FontMatrix.xx = args[0].fval;
1070
1.00k
                    ptpriv->FontMatrix.xy = args[1].fval;
1071
1.00k
                    ptpriv->FontMatrix.yx = args[2].fval;
1072
1.00k
                    ptpriv->FontMatrix.yy = args[3].fval;
1073
1.00k
                    ptpriv->FontMatrix.tx = args[4].fval;
1074
1.00k
                    ptpriv->FontMatrix.ty = args[5].fval;
1075
1.00k
                    offsets->have_matrix = true;
1076
1.00k
                    break;
1077
2.49k
                }
1078
16.7k
                case 5:
1079
16.7k
                {
1080
16.7k
                    ptpriv->FontBBox.p.x = args[0].fval;
1081
16.7k
                    ptpriv->FontBBox.p.y = args[1].fval;
1082
16.7k
                    ptpriv->FontBBox.q.x = args[2].fval;
1083
16.7k
                    ptpriv->FontBBox.q.y = args[3].fval;
1084
16.7k
                    break;
1085
2.49k
                }
1086
1087
9.20k
                case 20:
1088
9.20k
                {
1089
9.20k
                    ptpriv->type1data.defaultWidthX = float2fixed(args[0].fval);
1090
9.20k
                    break;
1091
2.49k
                }
1092
1093
7.43k
                case 21:
1094
7.43k
                {
1095
7.43k
                    ptpriv->type1data.nominalWidthX = float2fixed(args[0].fval);
1096
7.43k
                    break;
1097
2.49k
                }
1098
1099
8
                case 256 | 19:
1100
8
                {
1101
8
                    ptpriv->type1data.initialRandomSeed = args[0].ival;
1102
8
                    break;
1103
2.49k
                }
1104
1105
12.7k
                case 6:
1106
12.7k
                {
1107
12.7k
                    if (n > max_BlueValues * 2) n = max_BlueValues * 2;
1108
12.7k
                    ptpriv->type1data.BlueValues.count = n;
1109
12.7k
                    ptpriv->type1data.BlueValues.values[0] = args[0].fval;
1110
83.5k
                    for (i = 1; i < n; i++) {
1111
70.8k
                        ptpriv->type1data.BlueValues.values[i] = ptpriv->type1data.BlueValues.values[i - 1] + args[i].fval;
1112
70.8k
                    }
1113
12.7k
                    break;
1114
2.49k
                }
1115
1116
8.83k
                case 7:
1117
8.83k
                {
1118
8.83k
                    if (n > max_OtherBlues * 2) n = max_OtherBlues * 2;
1119
8.83k
                    ptpriv->type1data.OtherBlues.count = n;
1120
8.83k
                    ptpriv->type1data.OtherBlues.values[0] = args[0].fval;
1121
37.1k
                    for (i = 1; i < n; i++) {
1122
28.2k
                        ptpriv->type1data.OtherBlues.values[i] = ptpriv->type1data.OtherBlues.values[i - 1] + args[i].fval;
1123
28.2k
                    }
1124
8.83k
                    break;
1125
2.49k
                }
1126
1127
3.36k
                case 8:
1128
3.36k
                {
1129
3.36k
                    if (n > max_FamilyBlues * 2) n = max_FamilyBlues * 2;
1130
3.36k
                    ptpriv->type1data.FamilyBlues.count = n;
1131
3.36k
                    ptpriv->type1data.FamilyBlues.values[0] = args[0].fval;
1132
20.0k
                    for (i = 1; i < n; i++) {
1133
16.6k
                        ptpriv->type1data.FamilyBlues.values[i] = ptpriv->type1data.FamilyBlues.values[i - 1] + args[i].fval;
1134
16.6k
                    }
1135
3.36k
                    break;
1136
2.49k
                }
1137
1138
2.73k
                case 9:
1139
2.73k
                {
1140
2.73k
                    if (n > max_FamilyOtherBlues * 2) n = max_FamilyOtherBlues * 2;
1141
2.73k
                    ptpriv->type1data.FamilyOtherBlues.count = n;
1142
2.73k
                    ptpriv->type1data.FamilyOtherBlues.values[0] = args[0].fval;
1143
14.6k
                    for (i = 1; i < n; i++) {
1144
11.9k
                        ptpriv->type1data.FamilyOtherBlues.values[i] = ptpriv->type1data.FamilyOtherBlues.values[i - 1] + args[i].fval;
1145
11.9k
                    }
1146
2.73k
                    break;
1147
2.49k
                }
1148
1149
9.54k
                case 10:
1150
9.54k
                {
1151
9.54k
                    ptpriv->type1data.StdHW.count = 1;
1152
9.54k
                    ptpriv->type1data.StdHW.values[0] = args[0].fval;
1153
9.54k
                    break;
1154
2.49k
                }
1155
1156
9.86k
                case 11:
1157
9.86k
                {
1158
9.86k
                    ptpriv->type1data.StdVW.count = 1;
1159
9.86k
                    ptpriv->type1data.StdVW.values[0] = args[0].fval;
1160
9.86k
                    break;
1161
2.49k
                }
1162
1163
902
                case 256:
1164
902
                {
1165
902
                    code = pdfi_make_string_from_sid(font->ctx, (pdf_obj **) &font->copyright, font, offsets, args[0].ival);
1166
902
                    break;
1167
2.49k
                }
1168
1169
3.55k
                case 256 | 9:
1170
3.55k
                {
1171
3.55k
                    ptpriv->type1data.BlueScale = args[0].fval;
1172
3.55k
                    break;
1173
2.49k
                }
1174
1175
482
                case 256 | 10:
1176
482
                {
1177
482
                    ptpriv->type1data.BlueShift = args[0].fval;
1178
482
                    break;
1179
2.49k
                }
1180
1181
529
                case 256 | 11:
1182
529
                {
1183
529
                    ptpriv->type1data.BlueFuzz = (int)args[0].fval;
1184
529
                    break;
1185
2.49k
                }
1186
1187
6.07k
                case 256 | 12:
1188
6.07k
                {
1189
6.07k
                    if (n > max_StemSnap) n = max_StemSnap;
1190
6.07k
                    ptpriv->type1data.StemSnapH.count = n;
1191
27.0k
                    for (f = 0, i = 0; i < n; f += args[i].fval, i++)
1192
21.0k
                        ptpriv->type1data.StemSnapH.values[i] = f;
1193
6.07k
                    break;
1194
2.49k
                }
1195
1196
6.19k
                case 256 | 13:
1197
6.19k
                {
1198
6.19k
                    if (n > max_StemSnap) n = max_StemSnap;
1199
6.19k
                    ptpriv->type1data.StemSnapV.count = n;
1200
25.3k
                    for (f = 0, i = 0; i < n; f += args[i].fval, i++)
1201
19.1k
                        ptpriv->type1data.StemSnapV.values[i] = f;
1202
6.19k
                    break;
1203
2.49k
                }
1204
1205
3.45k
                case 256 | 14:
1206
3.45k
                {
1207
3.45k
                    ptpriv->type1data.ForceBold = args[0].ival;
1208
3.45k
                    break;
1209
2.49k
                }
1210
1211
1.66k
                case 256 | 17:
1212
1.66k
                {
1213
1.66k
                    ptpriv->type1data.LanguageGroup = args[0].ival;
1214
1.66k
                    break;
1215
2.49k
                }
1216
1217
81
                case 256 | 18:
1218
81
                {
1219
81
                    ptpriv->type1data.ExpansionFactor = args[0].fval;
1220
81
                    break;
1221
2.49k
                }
1222
37.8k
                default:
1223
37.8k
                    break;
1224
245k
            }
1225
245k
            n = 0;
1226
245k
        }
1227
505k
        else {
1228
505k
            if (b0 == 30) {
1229
22.2k
                p = pdfi_read_cff_real(p, e, &args[n].fval);
1230
22.2k
                if (!p) {
1231
0
                    dbgprintf("\nCFF: corrupt dictionary operand\n");
1232
0
                    break;
1233
0
                }
1234
22.2k
                args[n].ival = (int)args[n].fval;
1235
22.2k
                n++;
1236
22.2k
            }
1237
482k
            else if (b0 == 28 || b0 == 29 || (b0 >= 32 && b0 <= 254)) {
1238
                /* If we run out of data reading an integer at the very end of the stream, don't throw an error
1239
                   just return.
1240
                 */
1241
482k
                bool near_end = ((e - p) <= 4);
1242
482k
                p = pdfi_read_cff_integer(p, e, b0, &args[n].ival);
1243
482k
                if (!p) {
1244
410
                    if (!near_end)
1245
0
                        code = gs_note_error(gs_error_invalidfont);
1246
410
                    dbgprintf("\nCFF: corrupt dictionary operand\n");
1247
410
                    break;
1248
410
                }
1249
482k
                args[n].fval = (float)args[n].ival;
1250
482k
                n++;
1251
482k
            }
1252
0
            else {
1253
0
                dbgprintf1("CFF: corrupt dictionary operand (b0 = %d)", b0);
1254
0
            }
1255
505k
        }
1256
750k
        if (n >= PDFI_CFF_STACK_SIZE) {
1257
8
            code = gs_error_invalidfont;
1258
8
            break;
1259
8
        }
1260
750k
    }
1261
1262
    /* recurse for the private dictionary */
1263
37.8k
    if (do_priv && code >= 0) {
1264
16.7k
        byte *dend = font->cffdata + offsets->private_off + offsets->private_size;
1265
1266
16.7k
        if (dend > font->cffend)
1267
1.73k
            dend = font->cffend;
1268
1269
16.7k
        if (p == NULL)
1270
0
            code = gs_error_invalidfont;
1271
16.7k
        else
1272
16.7k
            code = pdfi_read_cff_dict(font->cffdata + offsets->private_off, dend, ptpriv, offsets, false);
1273
1274
16.7k
        if (code < 0)
1275
16.7k
            dbgprintf("CFF: cannot read private dictionary");
1276
16.7k
    }
1277
1278
37.8k
    return code;
1279
37.9k
}
1280
1281
/*
1282
 * Get the number of items in an INDEX, and return
1283
 * a pointer to the end of the INDEX or NULL on
1284
 * failure.
1285
 */
1286
static byte *
1287
pdfi_count_cff_index(byte *p, byte *e, int *countp)
1288
66.9k
{
1289
66.9k
    int count, offsize, last;
1290
66.9k
    int code;
1291
1292
66.9k
    if (p + 3 > e) {
1293
292
        gs_throw(-1, "not enough data for index header");
1294
292
        return 0;
1295
292
    }
1296
1297
66.6k
    if ((code = u16(p, e, &count)) < 0)
1298
0
        return NULL;
1299
1300
66.6k
    p += 2;
1301
66.6k
    *countp = count;
1302
1303
66.6k
    if (count == 0)
1304
15.2k
        return p;
1305
1306
51.3k
    offsize = *p++;
1307
1308
51.3k
    if (offsize < 1 || offsize > 4) {
1309
691
        gs_throw(-1, "corrupt index header");
1310
691
        return 0;
1311
691
    }
1312
1313
50.6k
    if (p + count * offsize > e) {
1314
212
        gs_throw(-1, "not enough data for index offset table");
1315
212
        return 0;
1316
212
    }
1317
1318
50.4k
    p += count * offsize;
1319
50.4k
    code = uofs(p, e, offsize, &last);
1320
50.4k
    p += offsize;
1321
50.4k
    p--;                        /* stupid offsets */
1322
1323
50.4k
    if (last < 0 || code < 0) {
1324
34
        gs_throw(-1, "corrupt index");
1325
34
        return 0;
1326
34
    }
1327
1328
50.4k
    if (p + last > e) {
1329
1.49k
        gs_throw(-1, "not enough data for index data");
1330
1.49k
        return 0;
1331
1.49k
    }
1332
1333
48.9k
    p += last;
1334
1335
48.9k
    return p;
1336
50.4k
}
1337
1338
/*
1339
 * Locate and store pointers to the data of an
1340
 * item in the index that starts at 'p'.
1341
 * Return pointer to the end of the index,
1342
 * or NULL on failure.
1343
 */
1344
static byte *
1345
pdfi_find_cff_index(byte *p, byte *e, int idx, byte ** pp, byte ** ep)
1346
6.10M
{
1347
6.10M
    int code, count, offsize, sofs, eofs, last;
1348
1349
6.10M
    if (p == NULL)
1350
12
        return 0;
1351
1352
6.10M
    if (p + 3 > e) {
1353
33
        gs_throw(-1, "not enough data for index header");
1354
33
        return 0;
1355
33
    }
1356
1357
6.10M
    if (u16(p, e, &count) < 0)
1358
0
        return NULL;
1359
1360
6.10M
    p += 2;
1361
6.10M
    if (count == 0)
1362
348
        return 0;
1363
1364
6.10M
    offsize = *p++;
1365
1366
6.10M
    if (offsize < 1 || offsize > 4) {
1367
103
        gs_throw(-1, "corrupt index header");
1368
103
        return 0;
1369
103
    }
1370
1371
6.10M
    if (p + count * offsize > e) {
1372
81
        gs_throw(-1, "not enough data for index offset table");
1373
81
        return 0;
1374
81
    }
1375
1376
6.10M
    if (idx < 0 || idx >= count) {
1377
1.04k
        gs_throw(-1, "tried to access non-existing index item");
1378
1.04k
        return 0;
1379
1.04k
    }
1380
1381
6.10M
    code = uofs(p + idx * offsize, e,  offsize, &sofs);
1382
6.10M
    if (code >= 0)
1383
6.10M
        code = uofs(p + (idx + 1) * offsize, e, offsize, &eofs);
1384
6.10M
    if (code >= 0)
1385
6.10M
        code = uofs(p + count * offsize, e, offsize, &last);
1386
1387
6.10M
    if (code < 0) {
1388
9
        gs_throw(-1, "not enough data for index data");
1389
9
        return 0;
1390
9
    }
1391
1392
6.10M
    p += count * offsize;
1393
6.10M
    p += offsize;
1394
6.10M
    p--;                        /* stupid offsets */
1395
1396
6.10M
    if (p + last > e) {
1397
176
        gs_throw(-1, "not enough data for index data");
1398
176
        return 0;
1399
176
    }
1400
1401
6.10M
    if (sofs < 0 || eofs < 0 || sofs > eofs || eofs > last) {
1402
75.0k
        gs_throw(-1, "corrupt index offset table");
1403
75.0k
        return 0;
1404
75.0k
    }
1405
1406
6.03M
    *pp = p + sofs;
1407
6.03M
    *ep = p + eofs;
1408
1409
6.03M
    return p + last;
1410
6.10M
}
1411
1412
static int
1413
pdfi_make_name_from_sid(pdf_context *ctx, pdf_obj ** nm, pdfi_cff_font_priv *font, cff_font_offsets *offsets, unsigned int sid)
1414
432k
{
1415
432k
    gs_string str;
1416
432k
    byte *p;
1417
1418
432k
    if (sid < gs_c_known_encoding_lengths[10]) {
1419
316k
        gs_glyph gl = gs_c_known_encode(sid, 10);
1420
1421
316k
        (void)gs_c_glyph_name(gl, (gs_const_string *) &str);
1422
316k
    }
1423
116k
    else {
1424
116k
        byte *strp, *stre;
1425
1426
116k
        p = pdfi_find_cff_index(font->cffdata + offsets->strings_off, font->cffend, sid - gs_c_known_encoding_lengths[10], &strp, &stre);
1427
116k
        if (p == NULL)
1428
1.12k
            return_error(gs_error_rangecheck);
1429
115k
        str.data = strp;
1430
115k
        str.size = stre - strp;
1431
115k
    }
1432
431k
    return pdfi_name_alloc(ctx, str.data, str.size, nm);
1433
432k
}
1434
1435
static int
1436
pdfi_make_string_from_sid(pdf_context *ctx, pdf_obj ** s0, pdfi_cff_font_priv *font, cff_font_offsets *offsets, unsigned int sid)
1437
34.6k
{
1438
34.6k
    byte *p;
1439
34.6k
    int code;
1440
34.6k
    gs_string str;
1441
34.6k
    pdf_string *s = NULL;
1442
1443
34.6k
    if (sid < gs_c_known_encoding_lengths[10]) {
1444
839
        gs_glyph gl = gs_c_known_encode(sid, 10);
1445
1446
839
        (void)gs_c_glyph_name(gl, (gs_const_string *) &str);
1447
839
    }
1448
33.8k
    else {
1449
33.8k
        byte *strp, *stre;
1450
1451
33.8k
        p = pdfi_find_cff_index(font->cffdata + offsets->strings_off, font->cffend,
1452
33.8k
                                sid - gs_c_known_encoding_lengths[10], &strp, &stre);
1453
33.8k
        if (p == NULL)
1454
298
            return_error(gs_error_rangecheck);
1455
33.5k
        str.data = strp;
1456
33.5k
        str.size = stre - strp;
1457
33.5k
    }
1458
34.3k
    code = pdfi_object_alloc(ctx, PDF_STRING, str.size, (pdf_obj **) &s);
1459
34.3k
    if (code < 0)
1460
0
        return code;
1461
34.3k
    pdfi_countup(s);
1462
34.3k
    memcpy(s->data, str.data, str.size);
1463
34.3k
    s->length = str.size;
1464
1465
34.3k
    *s0 = (pdf_obj *) s;
1466
34.3k
    return 0;
1467
34.3k
}
1468
1469
static int
1470
pdfi_cff_build_encoding(pdf_context *ctx, pdfi_gs_cff_font_priv *ptpriv, cff_font_offsets *offsets,
1471
                        int (*charset_proc)(const byte *p, const byte *pe, unsigned int i))
1472
11.2k
{
1473
11.2k
    pdfi_cff_font_priv *font = &ptpriv->pdfcffpriv;
1474
11.2k
    int code = 0;
1475
11.2k
    byte *s, *e, *lp;
1476
11.2k
    pdf_string *pstr;
1477
11.2k
    unsigned int i, gid, enc_format = 0;
1478
11.2k
    int sid;
1479
11.2k
    pdf_name *ndname = NULL;
1480
11.2k
    unsigned char gid2char[256];
1481
11.2k
    unsigned supp_enc_offset = 0;
1482
1483
11.2k
    if (offsets->encoding_off <= 1) {
1484
        /* Either standard or expert encoding */
1485
4.37k
        pdf_name *enm = NULL;
1486
4.37k
        const char *const stdenc = "StandardEncoding";
1487
4.37k
        const char *const expenc = "MacExpertEncoding";
1488
4.37k
        char const *enctouse;
1489
1490
4.37k
        if (offsets->encoding_off < 1) {
1491
4.33k
            enctouse = stdenc;
1492
4.33k
        }
1493
44
        else {
1494
44
            enctouse = expenc;
1495
44
        }
1496
4.37k
        code = pdfi_name_alloc(ctx, (byte *) enctouse, strlen(enctouse), (pdf_obj **) &enm);
1497
4.37k
        if (code >= 0) {
1498
4.37k
            pdfi_countup(enm);
1499
4.37k
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, (pdf_obj *) enm, NULL, (pdf_obj **) &font->Encoding);
1500
4.37k
            pdfi_countdown(enm);
1501
4.37k
        }
1502
4.37k
    }
1503
6.84k
    else {
1504
6.84k
        if (font->cffdata + offsets->encoding_off > font->cffend) {
1505
9
            code = gs_note_error(gs_error_invalidfont);
1506
9
        }
1507
6.83k
        else {
1508
6.83k
            code = pdfi_object_alloc(ctx, PDF_ARRAY, 256, (pdf_obj **) &font->Encoding);
1509
6.83k
            if (code < 0)
1510
0
                return code;
1511
1512
6.83k
            code = pdfi_name_alloc(ctx, (byte *) ".notdef", 7, (pdf_obj **) &ndname);
1513
6.83k
            if (code < 0)
1514
0
                return code;
1515
1516
1517
6.83k
            pdfi_countup(font->Encoding);
1518
6.83k
            pdfi_countup(ndname);
1519
6.83k
            code = 0;
1520
            /* Prepopulate with notdefs */
1521
1.75M
            for (i = 0; i < 256 && code >= 0; i++) {
1522
1.75M
                code = pdfi_array_put(ctx, font->Encoding, (uint64_t) i, (pdf_obj *) ndname);
1523
1.75M
            }
1524
1525
6.83k
            if (code >= 0) {
1526
6.83k
                byte *p = font->cffdata + offsets->encoding_off;
1527
1528
6.83k
                enc_format = p[0];
1529
1530
6.83k
                lp = pdfi_find_cff_index(font->charstrings, font->cffend, 0, &s, &e);
1531
6.83k
                if (lp == NULL) {
1532
29
                    code = gs_note_error(gs_error_rangecheck);
1533
29
                    goto done;
1534
29
                }
1535
6.81k
                code = pdfi_object_alloc(ctx, PDF_STRING, e - s, (pdf_obj **) &pstr);
1536
6.81k
                if (code < 0)
1537
0
                    goto done;
1538
6.81k
                memcpy(pstr->data, s, e - s);
1539
6.81k
                pdfi_countup(pstr);
1540
6.81k
                code =
1541
6.81k
                    pdfi_dict_put_obj(ctx, font->CharStrings, (pdf_obj *) ndname, (pdf_obj *) pstr, true);
1542
6.81k
                pdfi_countdown(pstr);
1543
6.81k
                if (code < 0) {
1544
0
                    goto done;
1545
0
                }
1546
6.81k
                pdfi_countdown(ndname);
1547
6.81k
                ndname = NULL;  /* just to avoid bad things! */
1548
1549
6.81k
                if ((enc_format &0x7f) == 0) {
1550
4.36k
                    unsigned int n_codes = p[1];
1551
1552
4.36k
                    if (p + 2 + n_codes > font->cffend) {
1553
0
                        return_error(gs_error_invalidfont);
1554
0
                    }
1555
4.36k
                    gid2char[0] = 0;
1556
43.6k
                    for (i = 0; i < n_codes; i++) {
1557
39.3k
                        gid2char[i + 1] = p[2 + i];
1558
39.3k
                    }
1559
4.36k
                    memset(gid2char + n_codes + 1, 0, sizeof(gid2char) - n_codes - 1);
1560
4.36k
                    supp_enc_offset = 2 + n_codes;
1561
4.36k
                }
1562
2.44k
                else if ((enc_format &0x7f) == 1) {
1563
2.43k
                    unsigned int n_ranges = p[1];
1564
2.43k
                    unsigned int first, left, j, k = 1;
1565
1566
2.43k
                    if (p + 2 + 2 * n_ranges > font->cffend) {
1567
0
                        return_error(gs_error_invalidfont);
1568
0
                    }
1569
2.43k
                    gid2char[0] = 0;
1570
18.5k
                    for (i = 0; i < n_ranges; i++) {
1571
16.1k
                        first = p[2 + 2 * i];
1572
16.1k
                        left = p[3 + 2 * i];
1573
137k
                        for (j = 0; j <= left && k < 256; j++)
1574
121k
                            gid2char[k++] = first + j;
1575
16.1k
                    }
1576
2.43k
                    memset(gid2char + k, 0, sizeof(gid2char) - k);
1577
2.43k
                    supp_enc_offset = 2 * n_ranges + 2;
1578
2.43k
                }
1579
9
                else {
1580
9
                    return_error(gs_error_rangecheck);
1581
9
                }
1582
6.81k
            }
1583
6.83k
        }
1584
6.84k
    }
1585
11.1k
    if (code >= 0) {
1586
11.1k
        pdf_obj *gname;
1587
1588
11.1k
        code = 0;
1589
1590
11.1k
        lp = pdfi_find_cff_index(font->charstrings, font->cffend, 0, &s, &e);
1591
11.1k
        if (lp == NULL) {
1592
25
            code = gs_note_error(gs_error_rangecheck);
1593
25
            goto done;
1594
25
        }
1595
11.1k
        code = pdfi_object_alloc(ctx, PDF_STRING, e - s, (pdf_obj **) &pstr);
1596
11.1k
        if (code < 0)
1597
0
            goto done;
1598
11.1k
        memcpy(pstr->data, s, e - s);
1599
11.1k
        pdfi_countup(pstr);
1600
11.1k
        if (ptpriv->forcecid) {
1601
536
            char buf[40];
1602
536
            int len = gs_snprintf(buf, sizeof(buf), "%d", 0);
1603
1604
536
            code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1605
536
            if (code < 0) {
1606
0
                pdfi_countdown(pstr);
1607
0
                return code;
1608
0
            }
1609
536
            pdfi_countup(gname);
1610
536
        }
1611
10.6k
        else {
1612
10.6k
            code = pdfi_name_alloc(ctx, (byte *) ".notdef", 7, &gname);
1613
10.6k
            if (code < 0) {
1614
0
                pdfi_countdown(pstr);
1615
0
                goto done;
1616
0
            }
1617
10.6k
            pdfi_countup(gname);
1618
10.6k
        }
1619
11.1k
        code = pdfi_dict_put_obj(ctx, font->CharStrings, gname, (pdf_obj *) pstr, true);
1620
11.1k
        pdfi_countdown(pstr);
1621
11.1k
        pdfi_countdown(gname);
1622
11.1k
        if (code < 0)
1623
0
            goto done;
1624
1625
577k
        for (gid = 1; gid < font->ncharstrings && code >= 0; gid++) {
1626
1627
565k
            lp = pdfi_find_cff_index(font->charstrings, font->cffend, gid, &s, &e);
1628
565k
            if (lp == NULL) {
1629
144
                code = gs_note_error(gs_error_rangecheck);
1630
144
                continue;
1631
144
            }
1632
565k
            code = pdfi_object_alloc(ctx, PDF_STRING, e - s, (pdf_obj **) &pstr);
1633
565k
            if (code < 0)
1634
0
                return code;
1635
565k
            memcpy(pstr->data, s, e - s);
1636
565k
            pdfi_countup(pstr);
1637
1638
565k
            if (ptpriv->forcecid) {
1639
132k
                char buf[40];
1640
132k
                int len = gs_snprintf(buf, sizeof(buf), "%d", gid);
1641
1642
132k
                code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1643
132k
                if (code < 0) {
1644
0
                    pdfi_countdown(pstr);
1645
0
                    return code;
1646
0
                }
1647
132k
            }
1648
432k
            else {
1649
432k
                sid = (*charset_proc) (font->cffdata + offsets->charset_off + 1, font->cffend, gid - 1);
1650
432k
                if (sid < 0) {
1651
0
                    pdfi_countdown(pstr);
1652
0
                    return sid;
1653
0
                }
1654
432k
                if ((code = pdfi_make_name_from_sid(ctx, &gname, font, offsets, sid)) < 0) {
1655
1.12k
                    char buf[40];
1656
1.12k
                    int len = gs_snprintf(buf, sizeof(buf), "sid-%d", sid);
1657
1658
1.12k
                    code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1659
1.12k
                    if (code < 0) {
1660
0
                        pdfi_countdown(pstr);
1661
0
                        return code;
1662
0
                    }
1663
1.12k
                }
1664
432k
            }
1665
565k
            pdfi_countup(gname);
1666
565k
            code = pdfi_dict_put_obj(ctx, font->CharStrings, gname, (pdf_obj *) pstr, true);
1667
565k
            pdfi_countdown(pstr);
1668
565k
            if (code < 0) {
1669
0
                pdfi_countdown(gname);
1670
0
                return code;
1671
0
            }
1672
565k
            if (offsets->encoding_off > 1 && gid < 256) {
1673
248k
                code = pdfi_array_put(ctx, font->Encoding, (int64_t) gid2char[gid], gname);
1674
248k
            }
1675
565k
            pdfi_countdown(gname);
1676
565k
        }
1677
1678
11.1k
        if (offsets->encoding_off > 1 && (enc_format & 0x80)) {
1679
14
            unsigned int n_supp, charcode, sid;
1680
14
            byte *p = font->cffdata + offsets->encoding_off + supp_enc_offset;
1681
14
            pdf_obj *gname;
1682
1683
14
            n_supp = p[0];
1684
1685
84
            for (i = 0; i < n_supp && code >= 0; i++) {
1686
70
                charcode = p[1 + 3 * i];
1687
70
                code = u16(p + 2 + 3 * i, e, (int *)&sid);
1688
70
                if (code < 0) continue;
1689
1690
70
                if ((code = pdfi_make_name_from_sid(ctx, &gname, font, offsets, sid)) < 0) {
1691
0
                    char buf[40];
1692
0
                    int len = gs_snprintf(buf, sizeof(buf), "sid-%d", sid);
1693
1694
0
                    if (len > 0)
1695
0
                        code = pdfi_name_alloc(ctx, (byte *) buf, len, &gname);
1696
0
                    else
1697
0
                        code = 0;
1698
1699
0
                    if (code < 0)
1700
0
                        continue;
1701
0
                }
1702
70
                pdfi_countup(gname);
1703
70
                code = pdfi_array_put(ctx, font->Encoding, (int64_t) charcode, gname);
1704
70
                pdfi_countdown(gname);
1705
70
            }
1706
14
        }
1707
11.1k
    }
1708
11.2k
  done:
1709
11.2k
    if (code < 0) {
1710
207
        pdfi_countdown(ndname);
1711
207
    }
1712
11.2k
    return code;
1713
11.1k
}
1714
1715
1716
/*
1717
 * Scan the CFF file structure and extract important data.
1718
 */
1719
1720
static int
1721
pdfi_read_cff(pdf_context *ctx, pdfi_gs_cff_font_priv *ptpriv)
1722
16.3k
{
1723
16.3k
    pdfi_cff_font_priv *font = &ptpriv->pdfcffpriv;
1724
16.3k
    byte *pstore, *p = font->cffdata;
1725
16.3k
    byte *e = font->cffend;
1726
16.3k
    byte *dictp, *dicte;
1727
16.3k
    byte *strp, *stre;
1728
16.3k
    byte *nms, *nmp, *nme;
1729
16.3k
    int count;
1730
16.3k
    int i, code = 0;
1731
16.3k
    cff_font_offsets offsets = { 0 };
1732
16.3k
    int (*charset_proc)(const byte *p, const byte *pe, unsigned int i);
1733
16.3k
    int major, minor, hdrsize;
1734
1735
    /* CFF header */
1736
16.3k
    if (p + 4 > e)
1737
2
        return gs_throw(gs_error_invalidfont, "not enough data for header");
1738
1739
16.3k
    major = *p;
1740
16.3k
    minor = *(p + 1);
1741
16.3k
    hdrsize = *(p + 2);
1742
1743
16.3k
    if (major != 1 || minor != 0)
1744
0
        return gs_throw(gs_error_invalidfont, "not a CFF 1.0 file");
1745
1746
16.3k
    if (p + hdrsize > e)
1747
2
        return gs_throw(gs_error_invalidfont, "not enough data for extended header");
1748
16.3k
    p += hdrsize;
1749
1750
    /* Name INDEX */
1751
16.3k
    nms = p;
1752
16.3k
    p = pdfi_count_cff_index(p, e, &count);
1753
16.3k
    if (p == NULL)
1754
110
        return gs_throw(gs_error_invalidfont, "cannot read name index");
1755
16.2k
    if (count != 1)
1756
4
        return gs_throw(gs_error_invalidfont, "file did not contain exactly one font");
1757
1758
16.2k
    nms = pdfi_find_cff_index(nms, e, 0, &nmp, &nme);
1759
16.2k
    if (!nms)
1760
2
        return gs_throw(gs_error_invalidfont, "cannot read names index");
1761
16.2k
    else {
1762
16.2k
        int len = nme - nmp < sizeof(ptpriv->key_name.chars) ? nme - nmp : sizeof(ptpriv->key_name.chars);
1763
16.2k
        memcpy(ptpriv->key_name.chars, nmp, len);
1764
16.2k
        memcpy(ptpriv->font_name.chars, nmp, len);
1765
16.2k
        ptpriv->key_name.size = ptpriv->font_name.size = len;
1766
16.2k
    }
1767
1768
    /* Top Dict INDEX */
1769
16.2k
    p = pdfi_find_cff_index(p, e, 0, &dictp, &dicte);
1770
16.2k
    if (p == NULL)
1771
133
        return gs_throw(gs_error_invalidfont, "cannot read top dict index");
1772
1773
    /* String index */
1774
16.1k
    pstore = p;
1775
16.1k
    p = pdfi_find_cff_index(p, e, 0, &strp, &stre);
1776
1777
16.1k
    offsets.strings_off = pstore - font->cffdata;
1778
1779
16.1k
    p = pdfi_count_cff_index(pstore, e, &count);
1780
16.1k
    if (p == NULL)
1781
281
        return_error(gs_error_invalidfont);
1782
1783
15.8k
    offsets.strings_size = (unsigned int)count;
1784
1785
    /* Global Subr INDEX */
1786
15.8k
    font->gsubrs = p;
1787
15.8k
    p = pdfi_count_cff_index(p, e, &font->NumGlobalSubrs);
1788
15.8k
    if (p == NULL) {
1789
199
        font->GlobalSubrs = NULL;
1790
199
        font->NumGlobalSubrs = 0;
1791
199
    }
1792
    /* Read the top and private dictionaries */
1793
15.8k
    pdfi_cff_font_priv_defaults(ptpriv);
1794
15.8k
    code = pdfi_read_cff_dict(dictp, dicte, ptpriv, &offsets, true);
1795
15.8k
    if (code < 0)
1796
498
        return gs_rethrow(code, "cannot read top dictionary");
1797
1798
    /* Check the subrs index */
1799
15.3k
    font->NumSubrs = 0;
1800
15.3k
    if (font->subrs) {
1801
753
        p = pdfi_count_cff_index(font->subrs, e, &font->NumSubrs);
1802
753
        if (p == NULL || font->NumSubrs > 65536) {
1803
269
            font->Subrs = NULL;
1804
269
            font->NumSubrs = 0;
1805
269
        }
1806
484
        else {
1807
484
            ptpriv->type1data.subroutineNumberBias = subrbias(font->NumSubrs);
1808
484
        }
1809
753
    }
1810
1811
1812
15.3k
    font->GlobalSubrs = NULL;
1813
15.3k
    if (font->NumGlobalSubrs > 0 && font->NumGlobalSubrs <= 65536) {
1814
638
        ptpriv->type1data.gsubrNumberBias = subrbias(font->NumGlobalSubrs);
1815
638
        code = pdfi_object_alloc(ctx, PDF_ARRAY, font->NumGlobalSubrs, (pdf_obj **) &font->GlobalSubrs);
1816
638
        if (code >= 0) {
1817
638
            font->GlobalSubrs->refcnt = 1;
1818
543k
            for (i = 0; i < font->NumGlobalSubrs; i++) {
1819
542k
                pdf_string *gsubrstr;
1820
1821
542k
                p = pdfi_find_cff_index(font->gsubrs, font->cffend, i, &strp, &stre);
1822
542k
                if (p) {
1823
482k
                    code = pdfi_object_alloc(ctx, PDF_STRING, stre - strp, (pdf_obj **) &gsubrstr);
1824
482k
                    if (code >= 0) {
1825
482k
                        memcpy(gsubrstr->data, strp, gsubrstr->length);
1826
482k
                        code =
1827
482k
                            pdfi_array_put(ctx, font->GlobalSubrs, (uint64_t) i,
1828
482k
                                           (pdf_obj *) gsubrstr);
1829
482k
                        if (code < 0) {
1830
0
                            gsubrstr->refcnt = 1;
1831
0
                            pdfi_countdown(gsubrstr);
1832
0
                        }
1833
482k
                    }
1834
482k
                }
1835
60.7k
                else {
1836
60.7k
                    code = pdfi_array_put(ctx, font->GlobalSubrs, (uint64_t) i, PDF_NULL_OBJ);
1837
60.7k
                    if (code < 0) {
1838
0
                        pdfi_countdown(font->GlobalSubrs);
1839
0
                        font->GlobalSubrs = NULL;
1840
0
                    }
1841
60.7k
                }
1842
542k
            }
1843
638
        }
1844
638
    }
1845
1846
15.3k
    font->Subrs = NULL;
1847
15.3k
    if (font->NumSubrs > 0) {
1848
483
        code = pdfi_object_alloc(ctx, PDF_ARRAY, font->NumSubrs, (pdf_obj **) &font->Subrs);
1849
483
        if (code >= 0 && font->Subrs != NULL) {
1850
483
            font->Subrs->refcnt = 1;
1851
59.8k
            for (i = 0; i < font->NumSubrs; i++) {
1852
59.3k
                pdf_string *subrstr;
1853
1854
59.3k
                p = pdfi_find_cff_index(font->subrs, font->cffend, i, &strp, &stre);
1855
59.3k
                if (p) {
1856
52.8k
                    code = pdfi_object_alloc(ctx, PDF_STRING, stre - strp, (pdf_obj **) &subrstr);
1857
52.8k
                    if (code >= 0) {
1858
52.8k
                        memcpy(subrstr->data, strp, subrstr->length);
1859
52.8k
                        code = pdfi_array_put(ctx, font->Subrs, (uint64_t) i, (pdf_obj *) subrstr);
1860
52.8k
                        if (code < 0) {
1861
0
                            subrstr->refcnt = 1;
1862
0
                            pdfi_countdown(subrstr);
1863
0
                        }
1864
52.8k
                    }
1865
52.8k
                }
1866
6.56k
                else {
1867
6.56k
                    code = pdfi_array_put(ctx, font->Subrs, (uint64_t) i, PDF_NULL_OBJ);
1868
6.56k
                    if (code < 0) {
1869
0
                        pdfi_countdown(font->Subrs);
1870
0
                        font->Subrs = NULL;
1871
0
                        font->NumSubrs = 0;
1872
0
                        break;
1873
0
                    }
1874
6.56k
                }
1875
59.3k
            }
1876
483
        }
1877
483
    }
1878
1879
    /* Check the charstrings index */
1880
15.3k
    if (font->charstrings) {
1881
15.3k
        p = pdfi_count_cff_index(font->charstrings, e, &font->ncharstrings);
1882
15.3k
        if (!p || font->ncharstrings > 65535)
1883
1.67k
            return gs_rethrow(-1, "cannot read charstrings index");
1884
15.3k
    }
1885
13.6k
    code = pdfi_object_alloc(ctx, PDF_DICT, font->ncharstrings, (pdf_obj **) &font->CharStrings);
1886
13.6k
    if (code < 0)
1887
0
        return code;
1888
13.6k
    pdfi_countup(font->CharStrings);
1889
1890
13.6k
    switch (offsets.charset_off) {
1891
183
        case 0:
1892
183
            charset_proc = iso_adobe_charset_proc;
1893
183
            break;
1894
0
        case 1:
1895
0
            charset_proc = expert_charset_proc;
1896
0
            break;
1897
0
        case 2:
1898
0
            charset_proc = expert_subset_charset_proc;
1899
0
            break;
1900
13.4k
        default:{
1901
13.4k
                if (font->cffdata + offsets.charset_off >= font->cffend)
1902
0
                    return_error(gs_error_rangecheck);
1903
1904
13.4k
                switch ((int)font->cffdata[offsets.charset_off]) {
1905
6.54k
                    case 0:
1906
6.54k
                        charset_proc = format0_charset_proc;
1907
6.54k
                        break;
1908
6.71k
                    case 1:
1909
6.71k
                        charset_proc = format1_charset_proc;
1910
6.71k
                        break;
1911
164
                    case 2:
1912
164
                        charset_proc = format2_charset_proc;
1913
164
                        break;
1914
67
                    default:
1915
67
                        return_error(gs_error_rangecheck);
1916
13.4k
                }
1917
13.4k
            }
1918
13.6k
    }
1919
1920
13.6k
    if (offsets.have_ros) {     /* CIDFont */
1921
2.37k
        int fdarray_size;
1922
2.37k
        bool topdict_matrix = offsets.have_matrix;
1923
2.37k
        int (*fdselect_proc)(const byte *p, const byte *pe, unsigned int i);
1924
1925
2.37k
        p = pdfi_count_cff_index(font->cffdata + offsets.fdarray_off, e, &fdarray_size);
1926
2.37k
        if (!p || fdarray_size < 1 || fdarray_size > 64) /* 64 is arbitrary, but seems a reasonable upper limit */
1927
192
            return gs_rethrow(-1, "cannot read charstrings index");
1928
1929
2.18k
        ptpriv->cidata.FDBytes = 1;     /* Basically, always 1 just now */
1930
1931
2.18k
        ptpriv->cidata.FDArray = (gs_font_type1 **) gs_alloc_bytes(ctx->memory, (size_t)fdarray_size * sizeof(gs_font_type1 *), "pdfi_read_cff(fdarray)");
1932
2.18k
        if (!ptpriv->cidata.FDArray)
1933
0
            return_error(gs_error_VMerror);
1934
2.18k
        ptpriv->cidata.FDArray_size = fdarray_size;
1935
1936
2.18k
        code = pdfi_object_alloc(ctx, PDF_ARRAY, fdarray_size, (pdf_obj **) &font->FDArray);
1937
2.18k
        if (code < 0) {
1938
0
            gs_free_object(ctx->memory, ptpriv->cidata.FDArray, "pdfi_read_cff(fdarray)");
1939
0
            ptpriv->cidata.FDArray = NULL;
1940
0
        }
1941
2.18k
        else {
1942
2.18k
            pdfi_countup(font->FDArray);
1943
2.18k
            code = 0;
1944
7.52k
            for (i = 0; i < fdarray_size && code == 0; i++) {
1945
5.33k
                byte *fddictp, *fddicte;
1946
5.33k
                pdfi_gs_cff_font_priv fdptpriv = { 0 };
1947
5.33k
                pdf_font_cff *pdffont = NULL;
1948
5.33k
                gs_font_type1 *pt1font;
1949
1950
5.33k
                pdfi_init_cff_font_priv(ctx, &fdptpriv, font->cffdata, (font->cffend - font->cffdata), true);
1951
1952
5.33k
                offsets.private_off = 0;
1953
1954
5.33k
                p = pdfi_find_cff_index(font->cffdata + offsets.fdarray_off, e, i, &fddictp, &fddicte);
1955
5.33k
                if (!p) {
1956
2
                    ptpriv->cidata.FDArray[i] = NULL;
1957
2
                    code = gs_note_error(gs_error_invalidfont);
1958
2
                    continue;
1959
2
                }
1960
5.33k
                if (fddicte > font->cffend)
1961
0
                    fddicte = font->cffend;
1962
1963
5.33k
                code = pdfi_read_cff_dict(fddictp, fddicte, &fdptpriv, &offsets, true);
1964
5.33k
                if (code < 0) {
1965
36
                    ptpriv->cidata.FDArray[i] = NULL;
1966
36
                    code = gs_note_error(gs_error_invalidfont);
1967
36
                    continue;
1968
36
                }
1969
5.30k
                code = pdfi_alloc_cff_font(ctx, &pdffont, 0, true);
1970
5.30k
                if (code < 0) {
1971
0
                    ptpriv->cidata.FDArray[i] = NULL;
1972
0
                    code = gs_note_error(gs_error_invalidfont);
1973
0
                    continue;
1974
0
                }
1975
5.30k
                pt1font = (gs_font_type1 *) pdffont->pfont;
1976
5.30k
                memcpy(pt1font, &fdptpriv, sizeof(pdfi_gs_cff_font_common_priv));
1977
5.30k
                memcpy(&pt1font->data, &fdptpriv.type1data, sizeof(fdptpriv.type1data));
1978
5.30k
                pt1font->base = (gs_font *) pdffont->pfont;
1979
1980
5.30k
                if (!topdict_matrix && offsets.have_matrix) {
1981
10
                    gs_matrix newfmat, onekmat = { 1000, 0, 0, 1000, 0, 0 };
1982
10
                    code = gs_matrix_multiply(&onekmat, &pt1font->FontMatrix, &newfmat);
1983
10
                    memcpy(&pt1font->FontMatrix, &newfmat, sizeof(newfmat));
1984
10
                }
1985
1986
5.30k
                pt1font->FAPI = NULL;
1987
5.30k
                pt1font->client_data = pdffont;
1988
1989
                /* Check the subrs index */
1990
5.30k
                pdffont->Subrs = NULL;
1991
5.30k
                if (fdptpriv.pdfcffpriv.subrs) {
1992
91
                    p = pdfi_count_cff_index(fdptpriv.pdfcffpriv.subrs, e, &pdffont->NumSubrs);
1993
91
                    if (!p) {
1994
0
                        pdffont->Subrs = NULL;
1995
0
                        pdffont->NumSubrs = 0;
1996
0
                    }
1997
91
                }
1998
1999
5.30k
                if (pdffont->NumSubrs > 0) {
2000
91
                    code = pdfi_object_alloc(ctx, PDF_ARRAY, pdffont->NumSubrs, (pdf_obj **) &pdffont->Subrs);
2001
91
                    if (code >= 0) {
2002
91
                        int j;
2003
2004
91
                        pdffont->Subrs->refcnt = 1;
2005
40.3k
                        for (j = 0; j < pdffont->NumSubrs; j++) {
2006
40.2k
                            pdf_string *subrstr;
2007
2008
40.2k
                            p = pdfi_find_cff_index(fdptpriv.pdfcffpriv.subrs, e, j, &strp, &stre);
2009
40.2k
                            if (p) {
2010
40.2k
                                code = pdfi_object_alloc(ctx, PDF_STRING, stre - strp, (pdf_obj **) &subrstr);
2011
40.2k
                                if (code >= 0) {
2012
40.2k
                                    memcpy(subrstr->data, strp, subrstr->length);
2013
40.2k
                                    code = pdfi_array_put(ctx, pdffont->Subrs, (uint64_t) j, (pdf_obj *) subrstr);
2014
40.2k
                                    if (code < 0) {
2015
0
                                        subrstr->refcnt = 1;
2016
0
                                        pdfi_countdown(subrstr);
2017
0
                                    }
2018
40.2k
                                }
2019
40.2k
                            }
2020
40.2k
                        }
2021
91
                    }
2022
91
                }
2023
2024
5.30k
                pdffont->GlobalSubrs = font->GlobalSubrs;
2025
5.30k
                pdffont->NumGlobalSubrs = font->NumGlobalSubrs;
2026
5.30k
                pdfi_countup(pdffont->GlobalSubrs);
2027
5.30k
                pdffont->CharStrings = font->CharStrings;
2028
5.30k
                pdfi_countup(pdffont->CharStrings);
2029
5.30k
                pt1font->data.subroutineNumberBias = subrbias(pdffont->NumSubrs);
2030
5.30k
                pt1font->data.gsubrNumberBias = subrbias(pdffont->NumGlobalSubrs);
2031
2032
5.30k
                ptpriv->cidata.FDArray[i] = pt1font;
2033
5.30k
                (void)pdfi_array_put(ctx, font->FDArray, i, (pdf_obj *) pdffont);
2034
5.30k
                pdfi_countdown(pdffont);
2035
5.30k
            }
2036
2.18k
            if (code < 0) {
2037
38
                pdfi_countdown(font->FDArray);
2038
38
                font->FDArray = NULL;
2039
419
                for (i = 0; i < ptpriv->cidata.FDArray_size; i++) {
2040
381
                    ptpriv->cidata.FDArray[i] = NULL;
2041
381
                }
2042
38
            }
2043
2.14k
            else {
2044
2.14k
                if (font->cffdata + offsets.fdselect_off > font->cffend)
2045
10
                    return_error(gs_error_rangecheck);
2046
2047
2.13k
                switch ((int)font->cffdata[offsets.fdselect_off]) {
2048
1.16k
                    case 0:
2049
1.16k
                        fdselect_proc = format0_fdselect_proc;
2050
1.16k
                        break;
2051
960
                    case 3:
2052
960
                        fdselect_proc = format3_fdselect_proc;
2053
960
                        break;
2054
19
                    default:
2055
19
                        return_error(gs_error_rangecheck);
2056
2.13k
                }
2057
2058
2.12k
                if (font->ncharstrings > 0) {
2059
2.11k
                    int maxcid = 0;
2060
4.67M
                    for (i = 0; i < font->ncharstrings; i++) {
2061
4.67M
                        int fd, g;
2062
4.67M
                        char gkey[64];
2063
4.67M
                        pdf_string *charstr;
2064
2065
4.67M
                        fd = fdarray_size <= 1 ? 0 : (*fdselect_proc) (font->cffdata + offsets.fdselect_off + 1, font->cffend, i);
2066
2067
4.67M
                        p = pdfi_find_cff_index(font->charstrings, font->cffend, i, &strp, &stre);
2068
4.67M
                        if (!p)
2069
7.09k
                            continue;
2070
2071
4.67M
                        code = pdfi_object_alloc(ctx, PDF_STRING, (stre - strp) + 1, (pdf_obj **) &charstr);
2072
4.67M
                        if (code < 0)
2073
0
                            continue;
2074
4.67M
                        charstr->data[0] = (byte) fd;
2075
4.67M
                        memcpy(charstr->data + 1, strp, charstr->length - 1);
2076
2077
4.67M
                        if (i == 0) {
2078
2.09k
                            g = 0;
2079
2.09k
                        }
2080
4.66M
                        else {
2081
4.66M
                            g = (*charset_proc) (font->cffdata + offsets.charset_off + 1, font->cffend, i - 1);
2082
4.66M
                        }
2083
2084
4.67M
                        if (g > maxcid) maxcid = g;
2085
4.67M
                        gs_snprintf(gkey, sizeof(gkey), "%d", g);
2086
4.67M
                        code = pdfi_dict_put_unchecked(ctx, font->CharStrings, gkey, (pdf_obj *) charstr);
2087
4.67M
                    }
2088
2.11k
                    if (maxcid > ptpriv->pdfcffpriv.cidcount - 1)
2089
34
                        ptpriv->pdfcffpriv.cidcount = maxcid + 1;
2090
2.11k
                }
2091
2.12k
            }
2092
2.18k
        }
2093
2.18k
    }
2094
11.2k
    else {
2095
11.2k
        code = pdfi_cff_build_encoding(ctx, ptpriv, &offsets, charset_proc);
2096
11.2k
    }
2097
13.3k
    return code;
2098
13.6k
}
2099
2100
static int
2101
pdfi_alloc_cff_cidfont(pdf_context *ctx, pdf_cidfont_type0 ** font, uint32_t obj_num)
2102
2.61k
{
2103
2.61k
    pdf_cidfont_type0 *cffcidfont = NULL;
2104
2.61k
    gs_font_cid0 *pfont = NULL;
2105
2.61k
    gs_matrix defmat = { 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f };
2106
2107
2.61k
    cffcidfont = (pdf_cidfont_type0 *) gs_alloc_bytes(ctx->memory, sizeof(pdf_cidfont_type0), "pdfi (cff pdf_cidfont_type0)");
2108
2.61k
    if (cffcidfont == NULL)
2109
0
        return_error(gs_error_VMerror);
2110
2111
2.61k
    memset(cffcidfont, 0x00, sizeof(pdf_cidfont_type0));
2112
2.61k
    cffcidfont->ctx = ctx;
2113
2.61k
    cffcidfont->type = PDF_FONT;
2114
2.61k
    cffcidfont->pdfi_font_type = e_pdf_cidfont_type0;
2115
2116
#if REFCNT_DEBUG
2117
    cffcidfont->UID = ctx->UID++;
2118
    outprintf(ctx->memory, "Allocated object of type %c with UID %" PRIi64 "\n", cffcidfont->type,
2119
              cffcidfont->UID);
2120
#endif
2121
2122
2.61k
    pdfi_countup(cffcidfont);
2123
2124
2.61k
    pfont = (gs_font_cid0 *) gs_alloc_struct(ctx->memory, gs_font_cid0, &st_gs_font_cid0, "pdfi (cff cid pfont)");
2125
2.61k
    if (pfont == NULL) {
2126
0
        pdfi_countdown(cffcidfont);
2127
0
        return_error(gs_error_VMerror);
2128
0
    }
2129
2.61k
    memset(pfont, 0x00, sizeof(gs_font_cid0));
2130
2131
2.61k
    cffcidfont->pfont = (gs_font_base *) pfont;
2132
2.61k
    memcpy(&pfont->orig_FontMatrix, &defmat, sizeof(defmat));
2133
2.61k
    memcpy(&pfont->FontMatrix, &defmat, sizeof(defmat));
2134
2.61k
    pfont->next = pfont->prev = 0;
2135
2.61k
    pfont->memory = ctx->memory;
2136
2.61k
    pfont->dir = ctx->font_dir;
2137
2.61k
    pfont->is_resource = false;
2138
2.61k
    gs_notify_init(&pfont->notify_list, ctx->memory);
2139
2.61k
    pfont->base = (gs_font *) cffcidfont->pfont;
2140
2.61k
    pfont->client_data = cffcidfont;
2141
2.61k
    pfont->WMode = 0;
2142
2.61k
    pfont->PaintType = 0;
2143
2.61k
    pfont->StrokeWidth = 0;
2144
2.61k
    pfont->is_cached = 0;
2145
2.61k
    pfont->FAPI = NULL;
2146
2.61k
    pfont->FAPI_font_data = NULL;
2147
2.61k
    pfont->procs.init_fstack = gs_type0_init_fstack;
2148
2.61k
    pfont->procs.next_char_glyph = gs_default_next_char_glyph;
2149
2.61k
    pfont->FontType = ft_CID_encrypted;
2150
2.61k
    pfont->ExactSize = fbit_use_outlines;
2151
2.61k
    pfont->InBetweenSize = fbit_use_outlines;
2152
2.61k
    pfont->TransformedChar = fbit_use_outlines;
2153
    /* We may want to do something clever with an XUID here */
2154
2.61k
    pfont->id = gs_next_ids(ctx->memory, 1);
2155
2.61k
    uid_set_invalid(&pfont->UID);
2156
2157
    /* The buildchar proc will be filled in by FAPI -
2158
       we won't worry about working without FAPI */
2159
2.61k
    pfont->procs.encode_char = pdfi_encode_char;
2160
2.61k
    pfont->procs.glyph_name = ctx->get_glyph_name;
2161
2.61k
    pfont->procs.decode_glyph = pdfi_cidfont_decode_glyph;
2162
2.61k
    pfont->procs.define_font = gs_no_define_font;
2163
2.61k
    pfont->procs.make_font = gs_no_make_font;
2164
2165
2.61k
    cffcidfont->default_font_info = gs_default_font_info;
2166
2.61k
    pfont->procs.font_info = pdfi_default_font_info;
2167
2168
2.61k
    pfont->procs.glyph_info = gs_default_glyph_info;
2169
2.61k
    pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2170
2.61k
    pfont->procs.build_char = NULL;
2171
2.61k
    pfont->procs.same_font = gs_default_same_font;
2172
2.61k
    pfont->procs.enumerate_glyph = pdfi_cff_enumerate_glyph;
2173
2174
2.61k
    pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2175
2176
2.61k
    pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
2177
2.61k
    pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
2178
2179
2.61k
    pfont->client_data = (void *)cffcidfont;
2180
2181
2.61k
    *font = cffcidfont;
2182
2.61k
    return 0;
2183
2.61k
}
2184
2185
static int
2186
pdfi_alloc_cff_font(pdf_context *ctx, pdf_font_cff ** font, uint32_t obj_num, bool for_fdarray)
2187
16.4k
{
2188
16.4k
    pdf_font_cff *cfffont = NULL;
2189
16.4k
    gs_font_type1 *pfont = NULL;
2190
16.4k
    gs_matrix defmat_font = { 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f };
2191
16.4k
    gs_matrix defmat_fd = { 1.00f, 0.0f, 0.0f, 1.000f, 0.0f, 0.0f };
2192
16.4k
    gs_matrix *defmat = (for_fdarray ? &defmat_fd : &defmat_font);
2193
2194
16.4k
    cfffont = (pdf_font_cff *) gs_alloc_bytes(ctx->memory, sizeof(pdf_font_cff), "pdfi (cff pdf_font)");
2195
16.4k
    if (cfffont == NULL)
2196
0
        return_error(gs_error_VMerror);
2197
2198
16.4k
    memset(cfffont, 0x00, sizeof(pdf_font_cff));
2199
16.4k
    cfffont->ctx = ctx;
2200
16.4k
    cfffont->type = PDF_FONT;
2201
16.4k
    cfffont->pdfi_font_type = e_pdf_font_cff;
2202
2203
#if REFCNT_DEBUG
2204
    cfffont->UID = ctx->UID++;
2205
    outprintf(ctx->memory, "Allocated object of type %c with UID %" PRIi64 "\n", cfffont->type,
2206
              cfffont->UID);
2207
#endif
2208
2209
16.4k
    pdfi_countup(cfffont);
2210
2211
16.4k
    pfont = (gs_font_type1 *) gs_alloc_struct(ctx->memory, gs_font_type1, &st_gs_font_type1, "pdfi (truetype pfont)");
2212
16.4k
    if (pfont == NULL) {
2213
0
        pdfi_countdown(cfffont);
2214
0
        return_error(gs_error_VMerror);
2215
0
    }
2216
16.4k
    memset(pfont, 0x00, sizeof(gs_font_type1));
2217
2218
16.4k
    cfffont->pfont = (gs_font_base *) pfont;
2219
16.4k
    memcpy(&pfont->orig_FontMatrix, defmat, sizeof(*defmat));
2220
16.4k
    memcpy(&pfont->FontMatrix, defmat, sizeof(*defmat));
2221
16.4k
    pfont->next = pfont->prev = 0;
2222
16.4k
    pfont->memory = ctx->memory;
2223
16.4k
    pfont->dir = ctx->font_dir;
2224
16.4k
    pfont->is_resource = false;
2225
16.4k
    gs_notify_init(&pfont->notify_list, ctx->memory);
2226
16.4k
    pfont->base = (gs_font *) cfffont->pfont;
2227
16.4k
    pfont->client_data = cfffont;
2228
16.4k
    pfont->WMode = 0;
2229
16.4k
    pfont->PaintType = 0;
2230
16.4k
    pfont->StrokeWidth = 0;
2231
16.4k
    pfont->is_cached = 0;
2232
16.4k
    pfont->FAPI = NULL;
2233
16.4k
    pfont->FAPI_font_data = NULL;
2234
16.4k
    pfont->procs.init_fstack = gs_default_init_fstack;
2235
16.4k
    pfont->procs.next_char_glyph = gs_default_next_char_glyph;
2236
16.4k
    pfont->FontType = ft_encrypted2;
2237
16.4k
    pfont->ExactSize = fbit_use_outlines;
2238
16.4k
    pfont->InBetweenSize = fbit_use_outlines;
2239
16.4k
    pfont->TransformedChar = fbit_use_outlines;
2240
    /* We may want to do something clever with an XUID here */
2241
16.4k
    pfont->id = gs_next_ids(ctx->memory, 1);
2242
16.4k
    uid_set_invalid(&pfont->UID);
2243
2244
    /* The buildchar proc will be filled in by FAPI -
2245
       we won't worry about working without FAPI */
2246
16.4k
    pfont->procs.encode_char = pdfi_encode_char;
2247
16.4k
    pfont->procs.glyph_name = ctx->get_glyph_name;
2248
16.4k
    pfont->procs.decode_glyph = pdfi_decode_glyph;
2249
16.4k
    pfont->procs.define_font = gs_no_define_font;
2250
16.4k
    pfont->procs.make_font = gs_no_make_font;
2251
2252
16.4k
    cfffont->default_font_info = gs_default_font_info;
2253
16.4k
    pfont->procs.font_info = pdfi_default_font_info;
2254
2255
16.4k
    pfont->procs.glyph_info = gs_default_glyph_info;
2256
16.4k
    pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2257
16.4k
    pfont->procs.build_char = NULL;
2258
16.4k
    pfont->procs.same_font = gs_default_same_font;
2259
16.4k
    pfont->procs.enumerate_glyph = pdfi_cff_enumerate_glyph;
2260
2261
16.4k
    pfont->data.procs.glyph_data = for_fdarray ? pdfi_cff_fdarray_glyph_data : pdfi_cff_glyph_data;
2262
16.4k
    pfont->data.procs.subr_data = pdfi_cff_subr_data;
2263
16.4k
    pfont->data.procs.seac_data = for_fdarray ? pdfi_cff_fdarray_seac_data : pdfi_cff_seac_data;
2264
16.4k
    pfont->data.procs.push_values = pdfi_cff_push;
2265
16.4k
    pfont->data.procs.pop_value = pdfi_cff_pop;
2266
16.4k
    pfont->data.interpret = gs_type2_interpret;
2267
16.4k
    pfont->data.lenIV = -1;
2268
2269
16.4k
    pfont->encoding_index = ENCODING_INDEX_UNKNOWN;
2270
16.4k
    pfont->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
2271
2272
16.4k
    pfont->client_data = (void *)cfffont;
2273
2274
16.4k
    *font = cfffont;
2275
16.4k
    return 0;
2276
16.4k
}
2277
2278
static void
2279
pdfi_init_cff_font_priv(pdf_context *ctx, pdfi_gs_cff_font_priv *cffpriv,
2280
                        byte *buf, int buflen, bool for_fdarray)
2281
21.7k
{
2282
21.7k
    gs_matrix defmat_font = { 0.001f, 0.0f, 0.0f, 0.001f, 0.0f, 0.0f };
2283
21.7k
    gs_matrix defmat_fd = { 1.00f, 0.0f, 0.0f, 1.000f, 0.0f, 0.0f };
2284
21.7k
    gs_matrix *defmat = (for_fdarray ? &defmat_fd : &defmat_font);
2285
2286
21.7k
    memset(cffpriv, 0x00, sizeof(pdfi_gs_cff_font_priv));
2287
2288
21.7k
    cffpriv->pdfcffpriv.ctx = ctx;
2289
21.7k
    cffpriv->pdfcffpriv.type = PDF_FONT;
2290
21.7k
    cffpriv->pdfcffpriv.pdfi_font_type = e_pdf_font_cff;
2291
21.7k
    cffpriv->client_data = (void *)(&cffpriv->pdfcffpriv);
2292
21.7k
    cffpriv->pdfcffpriv.pfont = (gs_font_base *)cffpriv;
2293
    /* Dummy value for dummy object */
2294
21.7k
    cffpriv->pdfcffpriv.refcnt = 0xf0f0f0f0;
2295
21.7k
    cffpriv->pdfcffpriv.cffdata = buf;
2296
21.7k
    cffpriv->pdfcffpriv.cffend = buf + buflen;
2297
21.7k
    cffpriv->pdfcffpriv.cidcount = 8720;
2298
2299
21.7k
    memcpy(&cffpriv->orig_FontMatrix, defmat, sizeof(*defmat));
2300
21.7k
    memcpy(&cffpriv->FontMatrix, defmat, sizeof(*defmat));
2301
21.7k
    cffpriv->next = cffpriv->prev = 0;
2302
21.7k
    cffpriv->memory = ctx->memory;
2303
21.7k
    cffpriv->dir = ctx->font_dir;
2304
21.7k
    cffpriv->is_resource = false;
2305
21.7k
    gs_notify_init(&cffpriv->notify_list, ctx->memory);
2306
21.7k
    cffpriv->WMode = 0;
2307
21.7k
    cffpriv->PaintType = 0;
2308
21.7k
    cffpriv->StrokeWidth = 0;
2309
21.7k
    cffpriv->is_cached = 0;
2310
21.7k
    cffpriv->FAPI = NULL;
2311
21.7k
    cffpriv->FAPI_font_data = NULL;
2312
21.7k
    cffpriv->procs.init_fstack = gs_default_init_fstack;
2313
21.7k
    cffpriv->procs.next_char_glyph = gs_default_next_char_glyph;
2314
21.7k
    cffpriv->FontType = ft_encrypted2;
2315
21.7k
    cffpriv->ExactSize = fbit_use_outlines;
2316
21.7k
    cffpriv->InBetweenSize = fbit_use_outlines;
2317
21.7k
    cffpriv->TransformedChar = fbit_use_outlines;
2318
    /* We may want to do something clever with an XUID here */
2319
21.7k
    cffpriv->id = gs_next_ids(ctx->memory, 1);
2320
21.7k
    uid_set_invalid(&cffpriv->UID);
2321
2322
2323
    /* The buildchar proc will be filled in by FAPI -
2324
       we won't worry about working without FAPI */
2325
21.7k
    cffpriv->procs.encode_char = pdfi_encode_char;
2326
21.7k
    cffpriv->procs.glyph_name = ctx->get_glyph_name;
2327
21.7k
    cffpriv->procs.decode_glyph = pdfi_decode_glyph;
2328
21.7k
    cffpriv->procs.define_font = gs_no_define_font;
2329
2330
21.7k
    cffpriv->pdfcffpriv.default_font_info = gs_default_font_info;
2331
21.7k
    cffpriv->procs.font_info = pdfi_default_font_info;
2332
2333
21.7k
    cffpriv->procs.glyph_info = gs_default_glyph_info;
2334
21.7k
    cffpriv->procs.glyph_outline = pdfi_cff_glyph_outline;
2335
21.7k
    cffpriv->procs.build_char = NULL;
2336
21.7k
    cffpriv->procs.same_font = gs_default_same_font;
2337
21.7k
    cffpriv->procs.enumerate_glyph = pdfi_cff_enumerate_glyph;
2338
2339
21.7k
    cffpriv->type1data.procs.glyph_data = pdfi_cff_glyph_data;
2340
21.7k
    cffpriv->type1data.procs.subr_data = pdfi_cff_subr_data;
2341
21.7k
    cffpriv->type1data.procs.seac_data = pdfi_cff_seac_data;
2342
21.7k
    cffpriv->type1data.procs.push_values = pdfi_cff_push;
2343
21.7k
    cffpriv->type1data.procs.pop_value = pdfi_cff_pop;
2344
21.7k
    cffpriv->type1data.interpret = gs_type2_interpret;
2345
21.7k
    cffpriv->type1data.lenIV = -1;
2346
2347
21.7k
    cffpriv->encoding_index = ENCODING_INDEX_UNKNOWN;
2348
21.7k
    cffpriv->nearest_encoding_index = ENCODING_INDEX_UNKNOWN;
2349
21.7k
}
2350
2351
int
2352
pdfi_read_cff_font(pdf_context *ctx, pdf_dict *font_dict, pdf_dict *stream_dict, pdf_dict *page_dict, byte *pfbuf, int64_t fbuflen, bool forcecid, pdf_font **ppdffont)
2353
16.6k
{
2354
16.6k
    int code = 0;
2355
2356
16.6k
    pdf_font *ppdfont = NULL;
2357
16.6k
    pdf_obj *basefont = NULL;
2358
16.6k
    pdf_obj *tmp = NULL;
2359
16.6k
    pdf_obj *fontdesc = NULL;
2360
16.6k
    pdf_string *registry = NULL;
2361
16.6k
    pdf_string *ordering = NULL;
2362
16.6k
    byte *fbuf = pfbuf;
2363
2364
16.6k
    if (!memcmp(fbuf, "OTTO", 4)) {
2365
728
        int i, ntables;
2366
728
        byte *p;
2367
728
        uint32_t toffs = 0, tlen = 0;
2368
2369
728
        code = u16(fbuf + 4, fbuf + fbuflen, &ntables);
2370
2371
728
        if (code < 0 || ntables > 64) {
2372
23
            gs_free_object(ctx->memory, pfbuf, "pdfi_read_cff_font(fbuf)");
2373
23
            return_error(gs_error_invalidfont);
2374
23
        }
2375
2376
930
        for (i = 0; i < ntables; i++) {
2377
922
            p = fbuf + 12 + i * 16;
2378
922
            if (p >= fbuf + fbuflen)
2379
3
                break;
2380
2381
919
            if (!memcmp(p, "CFF ", 4)) {
2382
694
                code = u32(p + 8, fbuf + fbuflen, (int *)&toffs);
2383
694
                if (code >= 0)
2384
693
                    code = u32(p + 12, fbuf + fbuflen, (int *)&tlen);
2385
694
                if (code < 0) {
2386
1
                    toffs = tlen = 0;
2387
1
                }
2388
694
                break;
2389
694
            }
2390
919
        }
2391
        /* Sanity check the offset and size of the CFF table and make sure the declared
2392
         * size and position fits inside the data we have. Promote the 32-bit variables to
2393
         * 64-bit to avoid overflow calculating the end of the table.
2394
         */
2395
705
        if (toffs == 0 || tlen == 0 || (uint64_t)toffs + (uint64_t)tlen > fbuflen) {
2396
154
            gs_free_object(ctx->memory, pfbuf, "pdfi_read_cff_font(fbuf)");
2397
154
            return_error(gs_error_invalidfont);
2398
154
        }
2399
551
        fbuf += toffs;
2400
551
        fbuflen = tlen;
2401
551
    }
2402
2403
16.5k
    if (font_dict != NULL) {
2404
16.5k
        code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
2405
16.5k
        if (code < 0) {
2406
0
            fontdesc = NULL;
2407
0
        }
2408
16.5k
    }
2409
0
    else {
2410
0
        fontdesc = NULL;
2411
0
    }
2412
2413
    /* Vestigial magic number check - we can't check the third byte, as we have out of
2414
       spec fonts that have a head size > 4
2415
     */
2416
16.5k
    if (fbuf[0] == 1 && fbuf[1] == 0 && code >= 0) {
2417
16.3k
        pdfi_gs_cff_font_priv cffpriv;
2418
2419
16.3k
        pdfi_init_cff_font_priv(ctx, &cffpriv, fbuf, fbuflen, false);
2420
16.3k
        cffpriv.forcecid = forcecid;
2421
16.3k
        code = pdfi_read_cff(ctx, &cffpriv);
2422
2423
16.3k
        if (code >= 0) {
2424
13.1k
            if (cffpriv.FontType == ft_CID_encrypted) {
2425
2.12k
                pdf_obj *obj = NULL;
2426
2.12k
                pdf_cidfont_type0 *cffcid = NULL;
2427
2.12k
                gs_font_cid0 *pfont = NULL;
2428
2429
2.12k
                code = pdfi_alloc_cff_cidfont(ctx, &cffcid, font_dict->object_num);
2430
2.12k
                if (code < 0)
2431
0
                    goto error;
2432
2433
2.12k
                pfont = (gs_font_cid0 *) cffcid->pfont;
2434
2.12k
                ppdfont = (pdf_font *) cffcid;
2435
2436
2.12k
                memcpy(pfont, &cffpriv, sizeof(pdfi_gs_cff_font_common_priv));
2437
2.12k
                memcpy(&pfont->cidata, &cffpriv.cidata, sizeof(pfont->cidata));
2438
2439
2.12k
                pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2440
2.12k
                pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2441
2442
2.12k
                cffcid->orig_glyph_info = pfont->procs.glyph_info;
2443
2.12k
                pfont->procs.glyph_info = pdfi_cff_cidfont_glyph_info;
2444
2445
2.12k
                pfont->cidata.proc_data = NULL;
2446
2.12k
                pfont->FAPI = NULL;
2447
2.12k
                pfont->base = (gs_font *) cffcid->pfont;
2448
2449
2.12k
                code = pdfi_dict_knownget_type(ctx, font_dict, "CIDSystemInfo", PDF_DICT, (pdf_obj **)&obj);
2450
2.12k
                if (code <= 0) {
2451
18
                    cffcid->registry = cffpriv.pdfcffpriv.registry;
2452
18
                    cffcid->ordering = cffpriv.pdfcffpriv.ordering;
2453
18
                    cffcid->supplement = cffpriv.pdfcffpriv.supplement;
2454
18
                }
2455
2.10k
                else {
2456
2.10k
                    pdf_num *suppl = NULL;
2457
2458
2.10k
                    code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Registry", PDF_STRING, (pdf_obj **)&cffcid->registry);
2459
2.10k
                    if (code <= 0) {
2460
4
                        cffcid->registry = cffpriv.pdfcffpriv.registry;
2461
4
                    }
2462
2.09k
                    else {
2463
2.09k
                        pdfi_countdown(cffpriv.pdfcffpriv.registry);
2464
2.09k
                        cffpriv.pdfcffpriv.registry = NULL;
2465
2.09k
                    }
2466
2467
2.10k
                    code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Ordering", PDF_STRING, (pdf_obj **)&cffcid->ordering);
2468
2.10k
                    if (code <= 0) {
2469
3
                        cffcid->ordering = cffpriv.pdfcffpriv.ordering;
2470
3
                    }
2471
2.09k
                    else {
2472
2.09k
                        pdfi_countdown(cffpriv.pdfcffpriv.ordering);
2473
2.09k
                        cffpriv.pdfcffpriv.ordering = NULL;
2474
2.09k
                    }
2475
2.10k
                    code = pdfi_dict_knownget_type(ctx, (pdf_dict *)obj, "Supplement", PDF_INT, (pdf_obj **)&suppl);
2476
2.10k
                    if (code <= 0 || pdfi_type_of(suppl) != PDF_INT) {
2477
0
                        cffcid->supplement = cffpriv.pdfcffpriv.supplement;
2478
0
                    }
2479
2.10k
                    else {
2480
2.10k
                        cffcid->supplement = suppl->value.i;
2481
2.10k
                    }
2482
2.10k
                    pdfi_countdown(suppl);
2483
2.10k
                }
2484
2.12k
                pdfi_countdown(obj);
2485
2.12k
                obj = NULL;
2486
2487
2.12k
                pfont->cidata.common.CIDSystemInfo.Registry.data = cffcid->registry->data;
2488
2.12k
                pfont->cidata.common.CIDSystemInfo.Registry.size = cffcid->registry->length;
2489
2.12k
                pfont->cidata.common.CIDSystemInfo.Ordering.data = cffcid->ordering->data;
2490
2.12k
                pfont->cidata.common.CIDSystemInfo.Ordering.size = cffcid->ordering->length;
2491
2.12k
                pfont->cidata.common.CIDSystemInfo.Supplement = cffcid->supplement;
2492
2493
                /* We don't need to bounds check these strings because they were checked when parsing
2494
                 * from the CFF stream.
2495
                 */
2496
2.12k
                memcpy(pfont->font_name.chars, cffpriv.font_name.chars, cffpriv.font_name.size);
2497
2.12k
                pfont->font_name.size = cffpriv.font_name.size;
2498
2.12k
                memcpy(pfont->key_name.chars, cffpriv.key_name.chars, cffpriv.key_name.size);
2499
2.12k
                pfont->key_name.size = cffpriv.key_name.size;
2500
2501
2.12k
                cffcid->FontDescriptor = (pdf_dict *) fontdesc;
2502
2.12k
                fontdesc = NULL;
2503
2504
2.12k
                cffcid->PDF_font = font_dict;
2505
2.12k
                pdfi_countup(font_dict);
2506
2507
2.12k
                pfont->client_data = cffcid;
2508
2509
2.12k
                cffcid->object_num = font_dict->object_num;
2510
2.12k
                cffcid->generation_num = font_dict->generation_num;
2511
2.12k
                cffcid->indirect_num = font_dict->indirect_num;
2512
2.12k
                cffcid->indirect_gen = font_dict->indirect_gen;
2513
2514
2.12k
                cffcid->CharStrings = cffpriv.pdfcffpriv.CharStrings;
2515
2.12k
                cffpriv.pdfcffpriv.CharStrings = NULL;
2516
2517
2.12k
                cffcid->Subrs = cffpriv.pdfcffpriv.Subrs;
2518
2.12k
                cffcid->NumSubrs = cffpriv.pdfcffpriv.NumSubrs;
2519
2.12k
                cffpriv.pdfcffpriv.Subrs = NULL;
2520
2521
2.12k
                cffcid->GlobalSubrs = cffpriv.pdfcffpriv.GlobalSubrs;
2522
2.12k
                cffcid->NumGlobalSubrs = cffpriv.pdfcffpriv.NumGlobalSubrs;
2523
2.12k
                cffpriv.pdfcffpriv.GlobalSubrs = NULL;
2524
2525
2.12k
                cffcid->FDArray = cffpriv.pdfcffpriv.FDArray;
2526
2.12k
                cffpriv.pdfcffpriv.FDArray = NULL;
2527
2528
2.12k
                cffcid->copyright = cffpriv.pdfcffpriv.copyright;
2529
2.12k
                cffcid->notice = cffpriv.pdfcffpriv.notice;
2530
2.12k
                cffcid->fullname = cffpriv.pdfcffpriv.fullname;
2531
2.12k
                cffcid->familyname = cffpriv.pdfcffpriv.familyname;
2532
2.12k
                cffpriv.pdfcffpriv.copyright = cffpriv.pdfcffpriv.notice \
2533
2.12k
                    = cffpriv.pdfcffpriv.fullname = cffpriv.pdfcffpriv.familyname = NULL;
2534
2535
2.12k
                pfont->cidata.common.CIDCount = cffpriv.pdfcffpriv.cidcount;
2536
2537
2.12k
                cffcid->cidtogidmap = NULL;
2538
2.12k
                code = pdfi_dict_knownget(ctx, font_dict, "CIDToGIDMap", (pdf_obj **) &obj);
2539
2.12k
                if (code > 0) {
2540
                    /* CIDToGIDMap can only be a stream or a name, and if it's a name
2541
                       it's only permitted to be "/Identity", so ignore it
2542
                     */
2543
0
                    if (pdfi_type_of(obj) == PDF_STREAM) {
2544
0
                        byte *d;
2545
0
                        int64_t sz = 0;
2546
2547
0
                        code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&cffcid->cidtogidmap);
2548
0
                        if (code < 0) {
2549
0
                            pdfi_countdown(obj);
2550
0
                            goto error;
2551
0
                        }
2552
0
                        pdfi_countup(cffcid->cidtogidmap);
2553
0
                        code = pdfi_stream_to_buffer(ctx, (pdf_stream *)obj, &d, &sz);
2554
0
                        if (code < 0) {
2555
0
                            pdfi_countdown(obj);
2556
0
                            goto error;
2557
0
                        }
2558
0
                        code = pdfi_buffer_set_data((pdf_obj *)cffcid->cidtogidmap, d, (int32_t)sz);
2559
0
                        if (code < 0) {
2560
0
                            pdfi_countdown(obj);
2561
0
                            goto error;
2562
0
                        }
2563
0
                    }
2564
0
                    pdfi_countdown(obj);
2565
0
                    obj = NULL;
2566
2567
0
                    if (cffcid->cidtogidmap != NULL && cffcid->cidtogidmap->length > 0) {
2568
0
                        pfont->cidata.common.CIDCount = cffcid->cidtogidmap->length >> 1;
2569
0
                    }
2570
0
                }
2571
2.12k
                pfont->cidata.common.MaxCID = pfont->cidata.common.CIDCount - 1;
2572
2573
2.12k
                code = pdfi_dict_knownget_number(ctx, font_dict, "DW", &cffcid->DW);
2574
2.12k
                if (code <= 0) {
2575
600
                    cffcid->DW = 1000;
2576
600
                }
2577
2578
2.12k
                code = pdfi_dict_knownget_type(ctx, font_dict, "DW2", PDF_ARRAY, (pdf_obj **) &obj);
2579
2.12k
                if (code > 0) {
2580
105
                    cffcid->DW2 = (pdf_array *) obj;
2581
105
                    obj = NULL;
2582
105
                }
2583
2.01k
                else {
2584
2.01k
                    cffcid->DW2 = NULL;
2585
2.01k
                }
2586
2.12k
                code = pdfi_dict_knownget_type(ctx, font_dict, "W", PDF_ARRAY, (pdf_obj **) &obj);
2587
2.12k
                if (code > 0) {
2588
1.49k
                    cffcid->W = (pdf_array *) obj;
2589
1.49k
                    obj = NULL;
2590
1.49k
                }
2591
624
                else {
2592
624
                    cffcid->W = NULL;
2593
624
                }
2594
2.12k
                code = pdfi_dict_knownget_type(ctx, font_dict, "W2", PDF_ARRAY, (pdf_obj **) &obj);
2595
2.12k
                if (code > 0) {
2596
0
                    cffcid->W2 = (pdf_array *) obj;
2597
0
                    obj = NULL;
2598
0
                }
2599
2.12k
                else {
2600
2.12k
                    cffcid->W2 = NULL;
2601
2.12k
                }
2602
2.12k
                cffcid->pfont->id = gs_next_ids(ctx->memory, 1);
2603
2.12k
            }
2604
11.0k
            else if (forcecid) {
2605
492
                pdf_obj *obj;
2606
492
                pdf_cidfont_type0 *cffcid;
2607
492
                gs_font_cid0 *pfont;
2608
492
                pdf_font_cff *fdcfffont;
2609
492
                gs_font_type1 *pfdfont = NULL;
2610
492
                static const char *const reg = "Adobe";
2611
492
                static const char *const ord = "Identity";
2612
2613
492
                code = pdfi_object_alloc(ctx, PDF_STRING, strlen(reg), (pdf_obj **) &registry);
2614
492
                if (code < 0)
2615
0
                    goto error;
2616
492
                pdfi_countup(registry);
2617
2618
492
                code = pdfi_object_alloc(ctx, PDF_STRING, strlen(ord), (pdf_obj **) &ordering);
2619
492
                if (code < 0) {
2620
0
                    goto error;
2621
0
                }
2622
492
                pdfi_countup(ordering);
2623
2624
492
                memcpy(registry->data, reg, strlen(reg));
2625
492
                registry->length = strlen(reg);
2626
492
                memcpy(ordering->data, ord, strlen(ord));
2627
492
                ordering->length = strlen(ord);
2628
2629
492
                code = pdfi_alloc_cff_font(ctx, &fdcfffont, 0, true);
2630
492
                if (code < 0)
2631
0
                    goto error;
2632
2633
492
                pfdfont = (gs_font_type1 *) fdcfffont->pfont;
2634
2635
492
                code = pdfi_alloc_cff_cidfont(ctx, &cffcid, 0);
2636
492
                if (code < 0) {
2637
0
                    gs_free_object(ctx->memory, fdcfffont, "pdfi_read_cff_font");
2638
0
                    gs_free_object(ctx->memory, pfdfont, "pdfi_read_cff_font");
2639
0
                    goto error;
2640
0
                }
2641
492
                ppdfont = (pdf_font *) cffcid;
2642
2643
492
                code = pdfi_object_alloc(ctx, PDF_ARRAY, 1, (pdf_obj **) &cffcid->FDArray);
2644
492
                if (code < 0)
2645
0
                    goto error;
2646
492
                pdfi_countup(cffcid->FDArray);
2647
2648
492
                pfont = (gs_font_cid0 *) cffcid->pfont;
2649
492
                pfont->cidata.FDArray = (gs_font_type1 **) gs_alloc_bytes(ctx->memory, sizeof(gs_font_type1 *), "pdfi_read_cff_font");
2650
492
                pfont->base = (gs_font *)pfont;
2651
492
                if (!pfont->cidata.FDArray) {
2652
0
                    pdfi_countdown(cffcid->FDArray);
2653
0
                    gs_free_object(ctx->memory, fdcfffont, "pdfi_read_cff_font");
2654
0
                    gs_free_object(ctx->memory, pfdfont, "pdfi_read_cff_font");
2655
0
                    gs_free_object(ctx->memory, cffcid, "pdfi_read_cff_font");
2656
0
                    gs_free_object(ctx->memory, pfont, "pdfi_read_cff_font");
2657
0
                    goto error;
2658
0
                }
2659
2660
492
                memcpy(pfdfont, &cffpriv, sizeof(pdfi_gs_cff_font_common_priv));
2661
492
                memcpy(&pfdfont->data, &cffpriv.type1data, sizeof(pfdfont->data));
2662
2663
2664
492
                pfont->procs.glyph_outline = pdfi_cff_glyph_outline;
2665
492
                pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2666
492
                pfont->cidata.common.CIDCount = cffpriv.pdfcffpriv.CharStrings->entries;
2667
492
                pfont->cidata.common.MaxCID = pfont->cidata.common.CIDCount - 1;
2668
2669
492
                cffcid->orig_glyph_info = pfont->procs.glyph_info;
2670
492
                pfont->procs.glyph_info = pdfi_cff_cidfont_glyph_info;
2671
2672
492
                pfdfont->FAPI = NULL;
2673
492
                pfdfont->base = (gs_font *)pfdfont;
2674
492
                pfdfont->client_data = fdcfffont;
2675
492
                pdfi_array_put(ctx, cffcid->FDArray, 0, (pdf_obj *) fdcfffont);
2676
2677
492
                fdcfffont->object_num = 0;
2678
492
                fdcfffont->generation_num = 0;
2679
2680
492
                (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
2681
492
                fdcfffont->BaseFont = basefont;
2682
492
                fdcfffont->Name = basefont;
2683
492
                pdfi_countup(basefont);
2684
2685
492
                pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2686
492
                cffpriv.pdfcffpriv.Encoding = NULL;
2687
2688
492
                fdcfffont->CharStrings = cffpriv.pdfcffpriv.CharStrings;
2689
492
                cffpriv.pdfcffpriv.CharStrings = NULL;
2690
492
                fdcfffont->Subrs = cffpriv.pdfcffpriv.Subrs;
2691
492
                cffpriv.pdfcffpriv.Subrs = NULL;
2692
492
                fdcfffont->NumSubrs = cffpriv.pdfcffpriv.NumSubrs;
2693
492
                fdcfffont->GlobalSubrs = cffpriv.pdfcffpriv.GlobalSubrs;
2694
492
                cffpriv.pdfcffpriv.GlobalSubrs = NULL;
2695
492
                fdcfffont->NumGlobalSubrs = cffpriv.pdfcffpriv.NumGlobalSubrs;
2696
2697
492
                fdcfffont->copyright = cffpriv.pdfcffpriv.copyright;
2698
492
                fdcfffont->notice = cffpriv.pdfcffpriv.notice;
2699
492
                fdcfffont->fullname = cffpriv.pdfcffpriv.fullname;
2700
492
                fdcfffont->familyname = cffpriv.pdfcffpriv.familyname;
2701
492
                cffpriv.pdfcffpriv.copyright = cffpriv.pdfcffpriv.notice \
2702
492
                    = cffpriv.pdfcffpriv.fullname = cffpriv.pdfcffpriv.familyname = NULL;
2703
2704
492
                cffcid->CharStrings = fdcfffont->CharStrings;
2705
492
                pdfi_countup(cffcid->CharStrings);
2706
492
                cffcid->Subrs = fdcfffont->Subrs;
2707
492
                pdfi_countup(cffcid->Subrs);
2708
492
                cffcid->GlobalSubrs = fdcfffont->GlobalSubrs;
2709
492
                pdfi_countup(cffcid->GlobalSubrs);
2710
492
                pdfi_countdown(fdcfffont);
2711
2712
492
                cffcid->FontDescriptor = (pdf_dict *) fontdesc;
2713
492
                fontdesc = NULL;
2714
2715
492
                cffcid->registry = registry;
2716
492
                cffcid->ordering = ordering;
2717
492
                registry = ordering = NULL;
2718
492
                cffcid->supplement = 0;
2719
2720
                /* Because we're faking a CIDFont, we want to move the scaling to the "parent" fake
2721
                   CIDFont, and make the FDArrray use identity scaling
2722
                 */
2723
492
                memcpy(&pfont->FontMatrix, &pfdfont->FontMatrix, sizeof(pfdfont->FontMatrix));
2724
492
                memcpy(&pfont->orig_FontMatrix, &pfdfont->orig_FontMatrix, sizeof(pfdfont->orig_FontMatrix));
2725
2726
492
                gs_make_identity(&pfdfont->FontMatrix);
2727
492
                gs_make_identity(&pfdfont->orig_FontMatrix);
2728
2729
492
                pfont->cidata.CIDMapOffset = 0;
2730
492
                pfont->cidata.FDArray_size = 1;
2731
492
                pfont->cidata.FDBytes = 0;
2732
492
                pfont->cidata.glyph_data = pdfi_cff_cid_glyph_data;
2733
492
                pfont->cidata.FDArray[0] = pfdfont;
2734
492
                pfont->cidata.common.CIDSystemInfo.Registry.data = cffcid->registry->data;
2735
492
                pfont->cidata.common.CIDSystemInfo.Registry.size = cffcid->registry->length;
2736
492
                pfont->cidata.common.CIDSystemInfo.Ordering.data = cffcid->ordering->data;
2737
492
                pfont->cidata.common.CIDSystemInfo.Ordering.size = cffcid->ordering->length;
2738
492
                pfont->cidata.common.CIDSystemInfo.Supplement = cffcid->supplement;
2739
492
                pfont->client_data = cffcid;
2740
2741
                /* We don't need to bounds check these strings because they were checked when parsing
2742
                 * from the CFF stream.
2743
                 */
2744
492
                memcpy(pfont->font_name.chars, cffpriv.font_name.chars, cffpriv.font_name.size);
2745
492
                pfont->font_name.size = cffpriv.font_name.size;
2746
492
                memcpy(pfont->key_name.chars, cffpriv.key_name.chars, cffpriv.key_name.size);
2747
492
                pfont->key_name.size = cffpriv.key_name.size;
2748
2749
492
                cffcid->object_num = font_dict->object_num;
2750
492
                cffcid->generation_num = font_dict->generation_num;
2751
492
                cffcid->indirect_num = font_dict->indirect_num;
2752
492
                cffcid->indirect_gen = font_dict->indirect_gen;
2753
2754
492
                cffcid->PDF_font = font_dict;
2755
492
                pdfi_countup(font_dict);
2756
2757
492
                cffcid->cidtogidmap = NULL;
2758
492
                code = pdfi_dict_knownget(ctx, font_dict, "CIDToGIDMap", (pdf_obj **) &obj);
2759
492
                if (code > 0) {
2760
292
                    byte *d;
2761
292
                    int64_t sz = 0;
2762
                    /* CIDToGIDMap can only be a stream or a name, and if it's a name
2763
                       it's only permitted to be "/Identity", so ignore it
2764
                     */
2765
292
                    if (pdfi_type_of(obj) == PDF_STREAM) {
2766
23
                        code = pdfi_object_alloc(ctx, PDF_BUFFER, 0, (pdf_obj **)&cffcid->cidtogidmap);
2767
23
                        if (code < 0) {
2768
0
                            goto error;
2769
0
                        }
2770
23
                        pdfi_countup(cffcid->cidtogidmap);
2771
23
                        code = pdfi_stream_to_buffer(ctx, (pdf_stream *)obj, &d, &sz);
2772
23
                        if (code < 0) {
2773
0
                            goto error;
2774
0
                        }
2775
23
                        code = pdfi_buffer_set_data((pdf_obj *)cffcid->cidtogidmap, d, (int32_t)sz);
2776
23
                        if (code < 0) {
2777
0
                            goto error;
2778
0
                        }
2779
23
                    }
2780
292
                    pdfi_countdown(obj);
2781
292
                    obj = NULL;
2782
2783
292
                    if (cffcid->cidtogidmap != NULL && cffcid->cidtogidmap->length > 0) {
2784
23
                        pfont->cidata.common.CIDCount = cffcid->cidtogidmap->length >> 1;
2785
23
                    }
2786
292
                }
2787
492
                pfont->cidata.common.MaxCID = pfont->cidata.common.CIDCount - 1;
2788
2789
492
                code = pdfi_dict_knownget_number(ctx, font_dict, "DW", &cffcid->DW);
2790
492
                if (code <= 0) {
2791
151
                    cffcid->DW = 1000;
2792
151
                }
2793
2794
492
                code = pdfi_dict_knownget_type(ctx, font_dict, "DW2", PDF_ARRAY, (pdf_obj **) &obj);
2795
492
                if (code > 0) {
2796
0
                    cffcid->DW2 = (pdf_array *) obj;
2797
0
                    obj = NULL;
2798
0
                }
2799
492
                else {
2800
492
                    cffcid->DW2 = NULL;
2801
492
                }
2802
492
                code = pdfi_dict_knownget_type(ctx, font_dict, "W", PDF_ARRAY, (pdf_obj **) &obj);
2803
492
                if (code > 0) {
2804
480
                    cffcid->W = (pdf_array *) obj;
2805
480
                    obj = NULL;
2806
480
                }
2807
12
                else {
2808
12
                    cffcid->W = NULL;
2809
12
                }
2810
492
                code = pdfi_dict_knownget_type(ctx, font_dict, "W2", PDF_ARRAY, (pdf_obj **) &obj);
2811
492
                if (code > 0) {
2812
0
                    cffcid->W2 = (pdf_array *) obj;
2813
0
                    obj = NULL;
2814
0
                }
2815
492
                else {
2816
492
                    cffcid->W2 = NULL;
2817
492
                }
2818
2819
492
                cffcid->pfont->id = gs_next_ids(ctx->memory, 1);
2820
492
            }
2821
10.5k
            else {
2822
10.5k
                pdf_font_cff *cfffont = NULL;
2823
10.5k
                gs_font_type1 *pfont = NULL;
2824
10.5k
                pdf_obj *tounicode = NULL;
2825
2826
10.5k
                code = pdfi_alloc_cff_font(ctx, &cfffont, font_dict != NULL ? font_dict->object_num : 0, false);
2827
10.5k
                if (code < 0)
2828
0
                    goto error;
2829
2830
10.5k
                pfont = (gs_font_type1 *) cfffont->pfont;
2831
10.5k
                ppdfont = (pdf_font *) cfffont;
2832
2833
10.5k
                memcpy(pfont, &cffpriv, sizeof(pdfi_gs_cff_font_common_priv));
2834
10.5k
                memcpy(&pfont->data, &cffpriv.type1data, sizeof(pfont->data));
2835
10.5k
                pfont->FAPI = NULL;
2836
10.5k
                pfont->client_data = cfffont;
2837
10.5k
                pfont->base = (gs_font *) cfffont->pfont;
2838
2839
10.5k
                pfont->procs.glyph_info = pdfi_cff_glyph_info;
2840
2841
10.5k
                if (font_dict) {
2842
10.5k
                    cfffont->object_num = font_dict->object_num;
2843
10.5k
                    cfffont->generation_num = font_dict->generation_num;
2844
10.5k
                    cfffont->indirect_num = font_dict->indirect_num;
2845
10.5k
                    cfffont->indirect_gen = font_dict->indirect_gen;
2846
10.5k
                    (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &basefont);
2847
10.5k
                }
2848
2849
10.5k
                cfffont->BaseFont = basefont;
2850
10.5k
                cfffont->Name = basefont;
2851
10.5k
                pdfi_countup(basefont);
2852
2853
10.5k
                cfffont->CharStrings = cffpriv.pdfcffpriv.CharStrings;
2854
10.5k
                cffpriv.pdfcffpriv.CharStrings = NULL;
2855
2856
10.5k
                cfffont->Subrs = cffpriv.pdfcffpriv.Subrs;
2857
10.5k
                cfffont->NumSubrs = cffpriv.pdfcffpriv.NumSubrs;
2858
10.5k
                cffpriv.pdfcffpriv.Subrs = NULL;
2859
2860
10.5k
                cfffont->GlobalSubrs = cffpriv.pdfcffpriv.GlobalSubrs;
2861
10.5k
                cfffont->NumGlobalSubrs = cffpriv.pdfcffpriv.NumGlobalSubrs;
2862
10.5k
                cffpriv.pdfcffpriv.GlobalSubrs = NULL;
2863
2864
10.5k
                cfffont->FontDescriptor = (pdf_dict *) fontdesc;
2865
10.5k
                fontdesc = NULL;
2866
2867
10.5k
                cfffont->copyright = cffpriv.pdfcffpriv.copyright;
2868
10.5k
                cfffont->notice = cffpriv.pdfcffpriv.notice;
2869
10.5k
                cfffont->fullname = cffpriv.pdfcffpriv.fullname;
2870
10.5k
                cfffont->familyname = cffpriv.pdfcffpriv.familyname;
2871
10.5k
                cffpriv.pdfcffpriv.copyright = cffpriv.pdfcffpriv.notice \
2872
10.5k
                    = cffpriv.pdfcffpriv.fullname = cffpriv.pdfcffpriv.familyname = NULL;
2873
2874
10.5k
                cfffont->PDF_font = font_dict;
2875
10.5k
                pdfi_countup(font_dict);
2876
2877
10.5k
                cfffont->descflags = 0;
2878
10.5k
                if (cfffont->FontDescriptor != NULL) {
2879
10.5k
                    code = pdfi_dict_get_int(ctx, cfffont->FontDescriptor, "Flags", &cfffont->descflags);
2880
10.5k
                    if (code >= 0) {
2881
                        /* If both the symbolic and non-symbolic flag are set,
2882
                           believe that latter.
2883
                         */
2884
10.5k
                        if ((cfffont->descflags & 32) != 0)
2885
4.65k
                            cfffont->descflags = (cfffont->descflags & ~4);
2886
10.5k
                    }
2887
10.5k
                }
2888
                /* ZapfDingbats and Symbol we just have to know are symbolic */
2889
10.5k
                if (pdfi_font_known_symbolic(basefont)) {
2890
0
                    cfffont->descflags |= 4;
2891
0
                }
2892
2893
10.5k
                pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)cfffont);
2894
2895
                /* Widths are defined assuming a 1000x1000 design grid, but we apply
2896
                 * them in font space - so undo the 1000x1000 scaling, and apply
2897
                 * the inverse of the font's x scaling
2898
                 */
2899
10.5k
                if (font_dict != NULL) {
2900
                    /* ignore errors with widths... for now */
2901
10.5k
                    (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font*)cfffont, (double)(0.001 / hypot(pfont->FontMatrix.xx, pfont->FontMatrix.xy)));
2902
10.5k
                }
2903
2904
10.5k
                if (font_dict != NULL)
2905
10.5k
                    code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
2906
0
                else
2907
0
                    code = gs_error_undefined;
2908
10.5k
                if (code == 1) {
2909
9.93k
                    if ((cfffont->descflags & 4) != 0 && pdfi_type_of(tmp) == PDF_DICT) {
2910
3.56k
                        code = pdfi_create_Encoding(ctx, (pdf_font *)cfffont, tmp, (pdf_obj *)cffpriv.pdfcffpriv.Encoding, (pdf_obj **) &cfffont->Encoding);
2911
3.56k
                        if (code >= 0) {
2912
3.56k
                            pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2913
3.56k
                            cffpriv.pdfcffpriv.Encoding = NULL;
2914
3.56k
                            code = 1;
2915
3.56k
                        }
2916
3.56k
                    }
2917
6.37k
                    else if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT)) {
2918
6.33k
                        code = pdfi_create_Encoding(ctx, (pdf_font *)cfffont, tmp, NULL, (pdf_obj **) &cfffont->Encoding);
2919
6.33k
                        if (code >= 0) {
2920
6.33k
                            pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2921
6.33k
                            cffpriv.pdfcffpriv.Encoding = NULL;
2922
6.33k
                            code = 1;
2923
6.33k
                        }
2924
6.33k
                    }
2925
34
                    else
2926
34
                        code = gs_error_undefined;
2927
2928
9.93k
                    if (code == 1) {
2929
9.89k
                    }
2930
9.93k
                    pdfi_countdown(tmp);
2931
9.93k
                    tmp = NULL;
2932
9.93k
                }
2933
584
                else {
2934
584
                    pdfi_countdown(tmp);
2935
584
                    tmp = NULL;
2936
584
                    code = 0;
2937
584
                }
2938
10.5k
                if (code <= 0) {
2939
626
                    cfffont->Encoding = cffpriv.pdfcffpriv.Encoding;
2940
626
                    cffpriv.pdfcffpriv.Encoding = NULL;
2941
626
                    cfffont->pfont->encoding_index = cffpriv.encoding_index;
2942
626
                    cfffont->pfont->nearest_encoding_index = cffpriv.nearest_encoding_index;
2943
626
                }
2944
2945
10.5k
                cfffont->pfont->id = gs_next_ids(ctx->memory, 1);
2946
2947
10.5k
                if (ctx->args.ignoretounicode != true && font_dict != NULL) {
2948
10.5k
                    code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tounicode);
2949
10.5k
                    if (code >= 0 && pdfi_type_of(tounicode) == PDF_STREAM) {
2950
2.66k
                        pdf_cmap *tu = NULL;
2951
2.66k
                        code = pdfi_read_cmap(ctx, tounicode, &tu);
2952
2.66k
                        pdfi_countdown(tounicode);
2953
2.66k
                        tounicode = (pdf_obj *)tu;
2954
2.66k
                    }
2955
10.5k
                    if (code < 0 || (tounicode != NULL && pdfi_type_of(tounicode) != PDF_CMAP)) {
2956
7.86k
                        pdfi_countdown(tounicode);
2957
7.86k
                        tounicode = NULL;
2958
7.86k
                        code = 0;
2959
7.86k
                    }
2960
10.5k
                }
2961
0
                else {
2962
0
                    tounicode = NULL;
2963
0
                }
2964
10.5k
                cfffont->ToUnicode = tounicode;
2965
10.5k
                tounicode = NULL;
2966
10.5k
            }
2967
13.1k
        }
2968
16.3k
error:
2969
16.3k
        if (code < 0) {
2970
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.Subrs);
2971
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.GlobalSubrs);
2972
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.CharStrings);
2973
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.CIDSystemInfo);
2974
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.W);
2975
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.DW2);
2976
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.W2);
2977
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.FDArray);
2978
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.registry);
2979
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.ordering);
2980
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.Encoding);
2981
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.copyright);
2982
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.notice);
2983
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.fullname);
2984
3.25k
            pdfi_countdown(cffpriv.pdfcffpriv.familyname);
2985
3.25k
            if (cffpriv.FontType == ft_CID_encrypted) {
2986
373
                if (ppdfont != NULL && ppdfont->pfont != NULL) {
2987
0
                    ((gs_font_cid0 *)ppdfont->pfont)->cidata.FDArray = NULL;
2988
0
                }
2989
373
                gs_free_object(ctx->memory, cffpriv.cidata.FDArray, "pdfi_read_cff_font(gs_font FDArray, error)");
2990
373
            }
2991
3.25k
        }
2992
13.1k
        else {
2993
13.1k
            code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, ppdfont->pfont);
2994
13.1k
            if (code < 0) {
2995
0
                goto error;
2996
0
            }
2997
2998
13.1k
            pdfi_font_set_orig_fonttype(ctx, (pdf_font *)ppdfont);
2999
13.1k
            code = gs_definefont(ctx->font_dir, (gs_font *) ppdfont->pfont);
3000
3001
13.1k
            if (code >= 0)
3002
13.1k
                code = pdfi_fapi_passfont((pdf_font *) ppdfont, 0, NULL, NULL, NULL, 0);
3003
3004
            /* object_num can be zero if the dictionary was defined inline */
3005
13.1k
            if (code >= 0 && ppdfont->object_num != 0) {
3006
11.9k
                (void)replace_cache_entry(ctx, (pdf_obj *) ppdfont);
3007
11.9k
            }
3008
3009
13.1k
            if (code >= 0) {
3010
13.0k
                *ppdffont = (pdf_font *) ppdfont;
3011
13.0k
                ppdfont = NULL;
3012
13.0k
            }
3013
13.1k
        }
3014
16.3k
    }
3015
16.5k
    gs_free_object(ctx->memory, pfbuf, "pdfi_read_cff_font(fbuf)");
3016
16.5k
    pdfi_countdown(ppdfont);
3017
16.5k
    pdfi_countdown(fontdesc);
3018
16.5k
    pdfi_countdown(ordering);
3019
16.5k
    pdfi_countdown(registry);
3020
3021
16.5k
    if (code < 0) {
3022
3.32k
        tmp = NULL;
3023
3.32k
        if (font_dict != NULL) {
3024
3.32k
            if (pdfi_dict_get(ctx, font_dict, ".Path", &tmp) >= 0)
3025
0
            {
3026
0
                char fname[gp_file_name_sizeof + 1];
3027
0
                pdf_string *fobj = (pdf_string *)tmp;
3028
3029
0
                memcpy(fname, fobj->data, fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length);
3030
0
                fname[fobj->length > gp_file_name_sizeof ? gp_file_name_sizeof : fobj->length] = '\0';
3031
3032
0
                (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_cff_font", "Error reading CFF font file %s\n", fname);
3033
0
            }
3034
3.32k
            else {
3035
3.32k
                (void)pdfi_set_error_var(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_cff_font", "Error reading embedded Type1C font object %u\n", font_dict->object_num);
3036
3.32k
            }
3037
3.32k
        }
3038
0
        else {
3039
0
            pdfi_set_error(ctx, code, NULL, E_PDF_BADSTREAM, "pdfi_read_truetype_font", "Error reading font\n");
3040
0
        }
3041
3.32k
        pdfi_countdown(tmp);
3042
3.32k
        *ppdffont = NULL;
3043
3.32k
        return_error(gs_error_invalidfont);
3044
3.32k
    }
3045
3046
13.1k
    return code;
3047
16.5k
}
3048
3049
int
3050
pdfi_read_type1C_font(pdf_context *ctx, pdf_dict *font_dict,
3051
                      pdf_dict *stream_dict, pdf_dict *page_dict, pdf_font **ppdffont)
3052
0
{
3053
0
    int code;
3054
0
    pdf_obj *fontdesc = NULL;
3055
0
    pdf_obj *fontfile = NULL;
3056
0
    byte *fbuf;
3057
0
    int64_t fbuflen = 0;
3058
3059
0
    code = pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, &fontdesc);
3060
3061
0
    if (code >=0 && fontdesc != NULL) {
3062
0
        code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile", PDF_STREAM, &fontfile);
3063
3064
0
        if (code < 0)
3065
0
            code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile2", PDF_STREAM, &fontfile);
3066
3067
0
        if (code < 0)
3068
0
            code = pdfi_dict_get_type(ctx, (pdf_dict *) fontdesc, "FontFile3", PDF_STREAM, &fontfile);
3069
0
    }
3070
0
    pdfi_countdown(fontdesc);
3071
3072
0
    if (code >= 0 && fontfile != NULL) {
3073
0
        code = pdfi_stream_to_buffer(ctx, (pdf_stream *) fontfile, &fbuf, &fbuflen);
3074
0
        pdfi_countdown(fontfile);
3075
0
    }
3076
0
    else {
3077
        /* TODO - handle non-emebedded case */
3078
0
        return_error(gs_error_invalidfont);
3079
0
    }
3080
3081
0
    code = pdfi_read_cff_font(ctx, stream_dict, page_dict, font_dict, fbuf, fbuflen, false, ppdffont);
3082
3083
0
    return code;
3084
0
}
3085
3086
int
3087
pdfi_copy_cff_font(pdf_context *ctx, pdf_font *spdffont, pdf_dict *font_dict, pdf_font **tpdffont)
3088
120
{
3089
120
    int code = 0;
3090
120
    pdf_font_cff *font = NULL;
3091
120
    gs_font_type1 *spfont1 = (gs_font_type1 *) spdffont->pfont;
3092
120
    gs_font_type1 *dpfont1;
3093
120
    gs_id t_id;
3094
120
    pdf_obj *tmp;
3095
3096
120
    if (font_dict == NULL)
3097
0
        return_error(gs_error_invalidfont);
3098
3099
120
    code = pdfi_alloc_cff_font(ctx, &font, font_dict->object_num, false);
3100
120
    if (code < 0)
3101
0
        return code;
3102
120
    dpfont1 = (gs_font_type1 *) font->pfont;
3103
3104
120
    t_id = dpfont1->id;
3105
120
    memcpy(dpfont1, spfont1, sizeof(gs_font_type1));
3106
120
    dpfont1->id = t_id;
3107
120
    dpfont1->FAPI = NULL;
3108
120
    dpfont1->FAPI_font_data = NULL;
3109
120
    dpfont1->notify_list.memory = NULL;
3110
120
    dpfont1->notify_list.first = NULL;
3111
120
    gs_notify_init(&dpfont1->notify_list, dpfont1->memory);
3112
3113
120
    memcpy(font, spdffont, sizeof(pdf_font_type1));
3114
120
    font->refcnt = 1;
3115
120
    font->pfont = (gs_font_base *)dpfont1;
3116
120
    dpfont1->client_data = (void *)font;
3117
120
    font->filename = NULL;
3118
3119
120
    font->PDF_font = font_dict;
3120
120
    font->object_num = font_dict->object_num;
3121
120
    font->generation_num = font_dict->generation_num;
3122
120
    pdfi_countup(font->PDF_font);
3123
3124
    /* We want basefont and descriptor, but we can live without them */
3125
120
    font->BaseFont = NULL;
3126
120
    (void)pdfi_dict_knownget_type(ctx, font_dict, "BaseFont", PDF_NAME, &font->BaseFont);
3127
120
    font->FontDescriptor = NULL;
3128
120
    (void)pdfi_dict_knownget_type(ctx, font_dict, "FontDescriptor", PDF_DICT, (pdf_obj **)&font->FontDescriptor);
3129
3130
120
    pdfi_countup(font->Name);
3131
120
    pdfi_countup(font->CharStrings);
3132
120
    pdfi_countup(font->Subrs);
3133
120
    pdfi_countup(font->GlobalSubrs);
3134
120
    pdfi_countup(font->copyright);
3135
120
    pdfi_countup(font->notice);
3136
120
    pdfi_countup(font->fullname);
3137
120
    pdfi_countup(font->familyname);
3138
3139
120
    if (font->BaseFont != NULL && ((pdf_name *)font->BaseFont)->length <= gs_font_name_max) {
3140
118
        memcpy(dpfont1->key_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
3141
118
        dpfont1->key_name.size = ((pdf_name *)font->BaseFont)->length;
3142
118
        memcpy(dpfont1->font_name.chars, ((pdf_name *)font->BaseFont)->data, ((pdf_name *)font->BaseFont)->length);
3143
118
        dpfont1->font_name.size = ((pdf_name *)font->BaseFont)->length;
3144
118
    }
3145
3146
120
    font->Encoding = NULL;
3147
120
    font->ToUnicode = NULL;
3148
120
    font->Widths = NULL;
3149
3150
120
    pdfi_font_set_first_last_char(ctx, font_dict, (pdf_font *)font);
3151
120
    (void)pdfi_font_create_widths(ctx, font_dict, (pdf_font *)font, (double)(0.001 / hypot(dpfont1->FontMatrix.xx, dpfont1->FontMatrix.xy)));
3152
3153
120
    font->descflags = 0;
3154
120
    if (font->FontDescriptor != NULL) {
3155
120
        code = pdfi_dict_get_int(ctx, font->FontDescriptor, "Flags", &font->descflags);
3156
120
        if (code >= 0) {
3157
            /* If both the symbolic and non-symbolic flag are set,
3158
               believe that latter.
3159
             */
3160
120
            if ((font->descflags & 32) != 0)
3161
120
                font->descflags = (font->descflags & ~4);
3162
120
        }
3163
120
    }
3164
3165
120
    if (pdfi_font_known_symbolic(font->BaseFont)) {
3166
0
        font->descflags |= 4;
3167
0
    }
3168
3169
3170
120
    tmp = NULL;
3171
120
    code = pdfi_dict_knownget(ctx, font_dict, "Encoding", &tmp);
3172
120
    if (code == 1) {
3173
113
        if ((font->descflags & 4) != 0 && pdfi_type_of(tmp) == PDF_DICT) {
3174
0
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, NULL, (pdf_obj **) & font->Encoding);
3175
0
            if (code >= 0)
3176
0
                code = 1;
3177
0
        }
3178
113
        else if ((pdfi_type_of(tmp) == PDF_NAME || pdfi_type_of(tmp) == PDF_DICT)) {
3179
113
            code = pdfi_create_Encoding(ctx, (pdf_font *)font, tmp, (pdf_obj *)spdffont->Encoding, (pdf_obj **) &font->Encoding);
3180
113
            if (code >= 0)
3181
113
                code = 1;
3182
113
        }
3183
0
        else
3184
0
            code = gs_error_undefined;
3185
113
        pdfi_countdown(tmp);
3186
113
        tmp = NULL;
3187
113
    }
3188
7
    else {
3189
7
        pdfi_countdown(tmp);
3190
7
        tmp = NULL;
3191
7
        code = 0;
3192
7
    }
3193
3194
120
    if (code <= 0) {
3195
7
        font->Encoding = spdffont->Encoding;
3196
7
        pdfi_countup(font->Encoding);
3197
7
    }
3198
3199
120
    code = uid_copy(&font->pfont->UID, font->pfont->memory, "pdfi_copy_cff_font");
3200
120
    if (code < 0) {
3201
0
        uid_set_invalid(&font->pfont->UID);
3202
0
    }
3203
3204
120
    if (spdffont->filename == NULL) {
3205
120
        code = pdfi_font_generate_pseudo_XUID(ctx, font_dict, font->pfont);
3206
120
        if (code < 0) {
3207
0
            goto error;
3208
0
        }
3209
120
    }
3210
3211
120
    if (ctx->args.ignoretounicode != true) {
3212
120
        code = pdfi_dict_get(ctx, font_dict, "ToUnicode", (pdf_obj **)&tmp);
3213
120
        if (code >= 0 && pdfi_type_of(tmp) == PDF_STREAM) {
3214
0
            pdf_cmap *tu = NULL;
3215
0
            code = pdfi_read_cmap(ctx, tmp, &tu);
3216
0
            pdfi_countdown(tmp);
3217
0
            tmp = (pdf_obj *)tu;
3218
0
        }
3219
120
        if (code < 0 || (tmp != NULL && pdfi_type_of(tmp) != PDF_CMAP)) {
3220
120
            pdfi_countdown(tmp);
3221
120
            tmp = NULL;
3222
120
            code = 0;
3223
120
        }
3224
120
    }
3225
0
    else {
3226
0
        tmp = NULL;
3227
0
    }
3228
120
    font->ToUnicode = tmp;
3229
3230
120
    pdfi_font_set_orig_fonttype(ctx, (pdf_font *)font);
3231
120
    code = gs_definefont(ctx->font_dir, (gs_font *) font->pfont);
3232
120
    if (code < 0) {
3233
0
        goto error;
3234
0
    }
3235
3236
120
    code = pdfi_fapi_passfont((pdf_font *) font, 0, NULL, NULL, NULL, 0);
3237
120
    if (code < 0) {
3238
0
        goto error;
3239
0
    }
3240
    /* object_num can be zero if the dictionary was defined inline */
3241
120
    if (font->object_num != 0) {
3242
120
        (void)replace_cache_entry(ctx, (pdf_obj *) font);
3243
120
    }
3244
3245
120
    *tpdffont = (pdf_font *)font;
3246
3247
120
error:
3248
120
    if (code < 0)
3249
0
        pdfi_countdown(font);
3250
120
    return code;
3251
120
}
3252
3253
int
3254
pdfi_free_font_cff(pdf_obj *font)
3255
16.4k
{
3256
16.4k
    pdf_font_cff *pdfontcff = (pdf_font_cff *) font;
3257
3258
16.4k
    gs_free_object(OBJ_MEMORY(font), pdfontcff->pfont, "pdfi_free_font_cff(pfont)");
3259
3260
16.4k
    pdfi_countdown(pdfontcff->PDF_font);
3261
16.4k
    pdfi_countdown(pdfontcff->BaseFont);
3262
16.4k
    pdfi_countdown(pdfontcff->Name);
3263
16.4k
    pdfi_countdown(pdfontcff->FontDescriptor);
3264
16.4k
    pdfi_countdown(pdfontcff->CharStrings);
3265
16.4k
    pdfi_countdown(pdfontcff->Subrs);
3266
16.4k
    pdfi_countdown(pdfontcff->GlobalSubrs);
3267
16.4k
    pdfi_countdown(pdfontcff->Encoding);
3268
16.4k
    pdfi_countdown(pdfontcff->ToUnicode);
3269
16.4k
    pdfi_countdown(pdfontcff->filename);
3270
16.4k
    pdfi_countdown(pdfontcff->copyright);
3271
16.4k
    pdfi_countdown(pdfontcff->notice);
3272
16.4k
    pdfi_countdown(pdfontcff->fullname);
3273
16.4k
    pdfi_countdown(pdfontcff->familyname);
3274
3275
16.4k
    gs_free_object(OBJ_MEMORY(font), pdfontcff->Widths, "Type 2 fontWidths");
3276
16.4k
    gs_free_object(OBJ_MEMORY(font), pdfontcff, "pdfi_free_font_cff(pbfont)");
3277
3278
16.4k
    return 0;
3279
16.4k
}
3280
3281
int
3282
pdfi_free_font_cidtype0(pdf_obj *font)
3283
2.61k
{
3284
2.61k
    pdf_cidfont_type0 *pdfont0 = (pdf_cidfont_type0 *) font;
3285
2.61k
    gs_font_cid0 *pfont = (gs_font_cid0 *) pdfont0->pfont;
3286
3287
    /* Only have to free the FDArray memory here. Each gs_font in the array is
3288
       referenced by a pdfi font, reference by pdfont0->FDArray. Freeing that
3289
       array will free each pdfi font, freeing the pdfi font will free the gs_font.
3290
       gs_fonts are not reference counted
3291
     */
3292
2.61k
    gs_free_object(OBJ_MEMORY(font), pfont->cidata.FDArray, "pdfi_free_font_cidtype0(pfont->fdarray)");
3293
2.61k
    gs_free_object(OBJ_MEMORY(font), pdfont0->pfont, "pdfi_free_font_cff(pfont)");
3294
3295
2.61k
    pdfi_countdown(pdfont0->PDF_font);
3296
2.61k
    pdfi_countdown(pdfont0->BaseFont);
3297
2.61k
    pdfi_countdown(pdfont0->FontDescriptor);
3298
2.61k
    pdfi_countdown(pdfont0->CharStrings);
3299
2.61k
    pdfi_countdown(pdfont0->Subrs);
3300
2.61k
    pdfi_countdown(pdfont0->GlobalSubrs);
3301
2.61k
    pdfi_countdown(pdfont0->CIDSystemInfo);
3302
2.61k
    pdfi_countdown(pdfont0->W);
3303
2.61k
    pdfi_countdown(pdfont0->DW2);
3304
2.61k
    pdfi_countdown(pdfont0->W2);
3305
2.61k
    pdfi_countdown(pdfont0->FDArray);
3306
2.61k
    pdfi_countdown(pdfont0->registry);
3307
2.61k
    pdfi_countdown(pdfont0->ordering);
3308
2.61k
    pdfi_countdown(pdfont0->cidtogidmap);
3309
2.61k
    pdfi_countdown(pdfont0->filename);
3310
2.61k
    pdfi_countdown(pdfont0->copyright);
3311
2.61k
    pdfi_countdown(pdfont0->notice);
3312
2.61k
    pdfi_countdown(pdfont0->fullname);
3313
2.61k
    pdfi_countdown(pdfont0->familyname);
3314
3315
2.61k
    gs_free_object(OBJ_MEMORY(font), pdfont0, "pdfi_free_font_cff(pbfont)");
3316
3317
2.61k
    return 0;
3318
2.61k
}