Coverage Report

Created: 2025-06-10 07:26

/src/ghostpdl/psi/zfcmap.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2023 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
17
/* CMap creation operator */
18
#include "memory_.h"
19
#include "ghost.h"
20
#include "oper.h"
21
#include "gsmatrix.h"   /* for gxfont.h */
22
#include "gsstruct.h"
23
#include "gsutil.h"   /* for bytes_compare */
24
#include "gxfcmap1.h"
25
#include "gxfont.h"
26
#include "ialloc.h"
27
#include "icid.h"
28
#include "iddict.h"
29
#include "idparam.h"
30
#include "ifont.h"    /* for zfont_mark_glyph_name */
31
#include "iname.h"
32
#include "store.h"
33
34
/* Exported for zfont0.c */
35
int ztype0_get_cmap(const gs_cmap_t ** ppcmap, const ref * pfdepvector,
36
                    const ref * op, gs_memory_t *imem);
37
38
/*
39
 * Define whether to check the compatibility of CIDSystemInfo between the
40
 * CMap and the composite font.  PLRM2 says compatibility is required, but
41
 * PLRM3 says the interpreter doesn't check it.
42
 */
43
/*#define CHECK_CID_SYSTEM_INFO_COMPATIBILITY*/
44
45
/* ---------------- Internal procedures ---------------- */
46
47
/* Free a code map in case of VMerror. */
48
static void
49
free_code_map(gx_code_map_t * pcmap, gs_memory_t * mem)
50
0
{
51
0
    if (pcmap->lookup) {
52
0
        int i;
53
54
0
        for (i = 0; i < pcmap->num_lookup; ++i) {
55
0
            gx_cmap_lookup_range_t *pclr = &pcmap->lookup[i];
56
57
0
            if (pclr->value_type == CODE_VALUE_GLYPH)
58
0
                gs_free_string(mem, pclr->values.data, pclr->values.size,
59
0
                               "free_code_map(values)");
60
0
        }
61
0
        gs_free_object(mem, pcmap->lookup, "free_code_map(map)");
62
0
    }
63
0
}
64
65
/* Convert code ranges to internal form. */
66
static int
67
acquire_code_ranges(gs_cmap_adobe1_t *cmap, const ref *pref, gs_memory_t *mem)
68
0
{
69
0
    uint num_ranges = 0;
70
0
    gx_code_space_range_t *ranges;
71
0
    uint i, j, elem_sz;
72
0
    ref elem;
73
74
0
    if (!r_is_array(pref))
75
0
        return_error(gs_error_rangecheck);
76
0
    for (i=0; i < r_size(pref); i++) {
77
0
        int code = array_get(mem, pref, i, &elem);
78
0
        if (code < 0)
79
0
            return code;
80
0
        elem_sz = r_size(&elem);
81
0
        if (elem_sz & 1 || elem_sz > max_uint - num_ranges)
82
0
            return_error(gs_error_rangecheck);
83
0
        num_ranges += elem_sz;
84
0
    }
85
0
    if (num_ranges == 0)
86
0
        return_error(gs_error_rangecheck);
87
0
    num_ranges >>= 1;
88
89
0
    ranges = (gx_code_space_range_t *)
90
0
        gs_alloc_byte_array(mem, num_ranges, sizeof(gx_code_space_range_t),
91
0
                            "acquire_code_ranges");
92
0
    if (ranges == 0)
93
0
        return_error(gs_error_VMerror);
94
0
    cmap->code_space.ranges = ranges;
95
0
    cmap->code_space.num_ranges = num_ranges;
96
97
0
    for (i = 0; i < r_size(pref); i++) {
98
0
        array_get(mem, pref, i, &elem);
99
0
        elem_sz = r_size(&elem);
100
0
        for (j = 0; j < elem_sz; j += 2) {
101
0
        ref rfirst, rlast;
102
0
        int size;
103
104
0
            array_get(mem, &elem, j, &rfirst);
105
0
            array_get(mem, &elem, j + 1, &rlast);
106
0
        if (!r_has_type(&rfirst, t_string) ||
107
0
            !r_has_type(&rlast, t_string) ||
108
0
            (size = r_size(&rfirst)) == 0 || size > MAX_CMAP_CODE_SIZE ||
109
0
            r_size(&rlast) != size ||
110
0
            memcmp(rfirst.value.bytes, rlast.value.bytes, size) > 0)
111
0
            return_error(gs_error_rangecheck);
112
0
        memcpy(ranges->first, rfirst.value.bytes, size);
113
0
        memcpy(ranges->last, rlast.value.bytes, size);
114
0
        ranges->size = size;
115
0
            ++ranges;
116
0
        }
117
0
    }
118
0
    return 0;
119
0
}
120
121
/* Convert a code map to internal form. */
122
static int
123
acquire_code_map(gx_code_map_t *pcmap, const ref *pref, gs_cmap_adobe1_t *root,
124
                 gs_memory_t *mem)
125
0
{
126
0
    uint num_lookup = 0;
127
0
    gx_cmap_lookup_range_t *pclr;
128
0
    long i;
129
0
    ref elem;
130
0
    uint elem_sz;
131
132
0
    if (!r_is_array(pref))
133
0
        return_error(gs_error_rangecheck);
134
0
    for (i=0; i < r_size(pref); i++) {
135
0
        int code = array_get(mem, pref, i, &elem);
136
0
        if (code < 0)
137
0
            return code;
138
0
        elem_sz = r_size(&elem);
139
0
        if (elem_sz % 5 != 0 || elem_sz > max_uint - num_lookup)
140
0
            return_error(gs_error_rangecheck);
141
0
        num_lookup += elem_sz;
142
0
    }
143
0
    num_lookup /= 5;
144
0
    pclr = gs_alloc_struct_array(mem, num_lookup, gx_cmap_lookup_range_t,
145
0
                                 &st_cmap_lookup_range_element,
146
0
                                 "acquire_code_map(lookup ranges)");
147
0
    if (pclr == 0)
148
0
        return_error(gs_error_VMerror);
149
0
    memset(pclr, 0, sizeof(*pclr) * num_lookup);
150
0
    pcmap->lookup = pclr;
151
0
    pcmap->num_lookup = num_lookup;
152
153
0
    for (i = 0; i < r_size(pref); i++) {
154
0
        uint j;
155
0
        array_get(mem, pref, i, &elem);
156
0
        elem_sz = r_size(&elem);
157
0
        for (j = 0; j < elem_sz; j += 5) {
158
0
        ref rprefix, rmisc, rkeys, rvalues, rfxs;
159
160
0
            array_get(mem, &elem, j, &rprefix);
161
0
            array_get(mem, &elem, j + 1, &rmisc);
162
0
            array_get(mem, &elem, j + 2, &rkeys);
163
0
            array_get(mem, &elem, j + 3, &rvalues);
164
0
            array_get(mem, &elem, j + 4, &rfxs);
165
166
0
        if (!r_has_type(&rprefix, t_string) ||
167
0
            !r_has_type(&rmisc, t_string) ||
168
0
            !r_has_type(&rkeys, t_string) ||
169
0
            !(r_has_type(&rvalues, t_string) || r_is_array(&rvalues)) ||
170
0
            !r_has_type(&rfxs, t_integer)
171
0
            )
172
0
            return_error(gs_error_typecheck);
173
0
        if (r_size(&rmisc) != 4 ||
174
0
            rmisc.value.bytes[0] > MAX_CMAP_CODE_SIZE - r_size(&rprefix) ||
175
0
            rmisc.value.bytes[1] > 1 ||
176
0
            rmisc.value.bytes[2] > CODE_VALUE_MAX ||
177
0
            rmisc.value.bytes[3] == 0)
178
0
            return_error(gs_error_rangecheck);
179
0
        pclr->cmap = root;
180
0
        pclr->key_size = rmisc.value.bytes[0];
181
0
        pclr->key_prefix_size = r_size(&rprefix);
182
0
        memcpy(pclr->key_prefix, rprefix.value.bytes, pclr->key_prefix_size);
183
0
        pclr->key_is_range = rmisc.value.bytes[1];
184
0
        if (pclr->key_size == 0) {
185
            /* This is a single entry consisting only of the prefix. */
186
0
            if (r_size(&rkeys) != 0)
187
0
                return_error(gs_error_rangecheck);
188
0
            pclr->num_entries = 1;
189
0
        } else {
190
0
            int step = pclr->key_size * (pclr->key_is_range ? 2 : 1);
191
192
0
            if (r_size(&rkeys) % step != 0)
193
0
                return_error(gs_error_rangecheck);
194
0
            pclr->num_entries = r_size(&rkeys) / step;
195
0
        }
196
0
        pclr->keys.data = rkeys.value.bytes,
197
0
            pclr->keys.size = r_size(&rkeys);
198
0
        pclr->value_type = rmisc.value.bytes[2];
199
0
        pclr->value_size = rmisc.value.bytes[3];
200
0
        if (r_has_type(&rvalues, t_string)) {
201
0
            if (pclr->value_type == CODE_VALUE_GLYPH)
202
0
                return_error(gs_error_rangecheck);
203
0
            if (r_size(&rvalues) % pclr->num_entries != 0 ||
204
0
                r_size(&rvalues) / pclr->num_entries != pclr->value_size)
205
0
                return_error(gs_error_rangecheck);
206
0
            pclr->values.data = rvalues.value.bytes,
207
0
                pclr->values.size = r_size(&rvalues);
208
0
        } else {
209
0
            uint values_size = pclr->num_entries * pclr->value_size;
210
0
            long k;
211
0
            byte *pvalue;
212
213
0
            if (pclr->value_type != CODE_VALUE_GLYPH ||
214
0
                r_size(&rvalues) != pclr->num_entries ||
215
0
                pclr->value_size > sizeof(gs_glyph))
216
0
                return_error(gs_error_rangecheck);
217
0
            pclr->values.data = gs_alloc_string(mem, values_size,
218
0
                                                "acquire_code_map(values)");
219
0
            if (pclr->values.data == 0)
220
0
                return_error(gs_error_VMerror);
221
0
            pclr->values.size = values_size;
222
0
            pvalue = pclr->values.data;
223
0
            for (k = 0; k < pclr->num_entries; ++k) {
224
0
                ref rvalue;
225
0
                gs_glyph value;
226
0
                int i;
227
228
0
                array_get(mem, &rvalues, k, &rvalue);
229
0
                if (!r_has_type(&rvalue, t_name))
230
0
                    return_error(gs_error_rangecheck);
231
0
                value = name_index(mem, &rvalue);
232
                /*
233
                 * We need a special check here because some CPUs cannot
234
                 * shift by the full size of an int or long.
235
                 */
236
0
                if (pclr->value_size < sizeof(value) &&
237
0
                    (value >> (pclr->value_size * 8)) != 0
238
0
                    )
239
0
                    return_error(gs_error_rangecheck);
240
0
                for (i = pclr->value_size; --i >= 0; )
241
0
                    *pvalue++ = (byte)(value >> (i * 8));
242
0
            }
243
0
        }
244
0
        check_int_leu_only(rfxs, 0xff);
245
0
        pclr->font_index = (int)rfxs.value.intval;
246
0
            ++pclr;
247
0
        }
248
0
    }
249
0
    return 0;
250
0
}
251
252
/*
253
 * Acquire the CIDSystemInfo array from a dictionary.  If missing, fabricate
254
 * a 0-element array and return 1.
255
 */
256
static int
257
acquire_cid_system_info(ref *psia, const ref *op)
258
0
{
259
0
    ref *prcidsi;
260
261
0
    if (dict_find_string(op, "CIDSystemInfo", &prcidsi) <= 0) {
262
0
        make_empty_array(psia, a_readonly);
263
0
        return 1;
264
0
    }
265
0
    if (r_has_type(prcidsi, t_dictionary)) {
266
0
        make_array(psia, a_readonly, 1, prcidsi);
267
0
        return 0;
268
0
    }
269
0
    if (!r_is_array(prcidsi))
270
0
        return_error(gs_error_typecheck);
271
0
    *psia = *prcidsi;
272
0
    return 0;
273
0
}
274
275
/*
276
 * Get one element of a CIDSystemInfo array.  If the element is missing or
277
 * null, return 1.
278
 */
279
static int
280
get_cid_system_info(const gs_memory_t *mem, gs_cid_system_info_t *pcidsi, const ref *psia, uint index)
281
0
{
282
0
    ref rcidsi;
283
0
    int code = array_get(mem, psia, (long)index, &rcidsi);
284
285
0
    if (code < 0 || r_has_type(&rcidsi, t_null)) {
286
0
        cid_system_info_set_null(pcidsi);
287
0
        return 1;
288
0
    }
289
0
    return cid_system_info_param(pcidsi, &rcidsi);
290
0
}
291
292
#ifdef CHECK_CID_SYSTEM_INFO_COMPATIBILITY
293
294
/* Check compatibility of CIDSystemInfo. */
295
static bool
296
bytes_eq(const gs_const_string *pcs1, const gs_const_string *pcs2)
297
{
298
    return !bytes_compare(pcs1->data, pcs1->size,
299
                          pcs2->data, pcs2->size);
300
}
301
static bool
302
cid_system_info_compatible(const gs_cid_system_info_t * psi1,
303
                           const gs_cid_system_info_t * psi2)
304
{
305
    return bytes_eq(&psi1->Registry, &psi2->Registry) &&
306
        bytes_eq(&psi1->Ordering, &psi2->Ordering);
307
}
308
309
#endif /* CHECK_CID_SYSTEM_INFO_COMPATIBILITY */
310
311
/* ---------------- (Semi-)public procedures ---------------- */
312
313
extern_st(st_cmap_tt_16bit_format4);
314
extern_st(st_cmap_identity);
315
extern_st(st_cmap_ToUnicode);
316
317
/* Get the CodeMap from a Type 0 font, and check the CIDSystemInfo of */
318
/* its subsidiary fonts. */
319
int
320
ztype0_get_cmap(const gs_cmap_t **ppcmap, const ref *pfdepvector,
321
                const ref *op, gs_memory_t *imem)
322
0
{
323
0
    ref *prcmap;
324
0
    ref *pcodemap;
325
0
    const gs_cmap_t *pcmap;
326
0
    int code;
327
0
    uint num_fonts;
328
0
    uint i;
329
330
0
    if (dict_find_string(op, "CMap", &prcmap) <= 0 ||
331
0
        !r_has_type(prcmap, t_dictionary) ||
332
0
        dict_find_string(prcmap, "CodeMap", &pcodemap) <= 0 ||
333
0
        !r_is_struct(pcodemap) || (!r_has_stype(pcodemap, imem, st_cmap_tt_16bit_format4) &&
334
0
        !r_has_stype(pcodemap, imem, st_cmap_identity) && !r_has_stype(pcodemap, imem, st_cmap_ToUnicode) &&
335
0
        !r_has_stype(pcodemap, imem, st_cmap_adobe1))
336
0
        )
337
0
        return_error(gs_error_invalidfont);
338
0
    pcmap = r_ptr(pcodemap, gs_cmap_t);
339
0
    num_fonts = r_size(pfdepvector);
340
0
    for (i = 0; i < num_fonts; ++i) {
341
0
        ref rfdep, rfsi;
342
343
0
        array_get(imem, pfdepvector, (long)i, &rfdep);
344
0
        code = acquire_cid_system_info(&rfsi, &rfdep);
345
0
        if (code < 0)
346
0
            return code;
347
0
        if (code == 0) {
348
0
            if (r_size(&rfsi) != 1)
349
0
                return_error(gs_error_rangecheck);
350
#ifdef CHECK_CID_SYSTEM_INFO_COMPATIBILITY
351
            {
352
                gs_cid_system_info_t cidsi;
353
354
                get_cid_system_info(&cidsi, &rfsi, 0);
355
                if (!cid_system_info_is_null(&cidsi) &&
356
                    !cid_system_info_compatible(&cidsi,
357
                                                pcmap->CIDSystemInfo + i))
358
                    return_error(gs_error_rangecheck);
359
            }
360
#endif
361
0
        }
362
0
    }
363
0
    *ppcmap = pcmap;
364
0
    return 0;
365
0
}
366
367
/* ---------------- Operators ---------------- */
368
369
/* <CMap> .buildcmap <CMap> */
370
/*
371
 * Create the internal form of a CMap.  The initial CMap must be read-write
372
 * and have an entry with key = CodeMap and value = null; the result is
373
 * read-only and has a real CodeMap.
374
 *
375
 * This operator reads the CMapType, CMapName, CIDSystemInfo, CMapVersion,
376
 * UIDOffset, XUID, WMode, and .CodeMapData elements of the CMap dictionary.
377
 * For details, see lib/gs_cmap.ps and the Adobe documentation.
378
 */
379
static int
380
zfcmap_glyph_name(const gs_memory_t *mem,
381
                  gs_glyph glyph, gs_const_string *pstr, void *proc_data)
382
0
{
383
0
    ref nref, nsref;
384
385
0
    name_index_ref(mem, (uint)glyph, &nref);
386
0
    name_string_ref(mem, &nref, &nsref);
387
0
    pstr->data = nsref.value.const_bytes;
388
0
    pstr->size = r_size(&nsref);
389
0
    return 0;
390
0
}
391
static int
392
zbuildcmap(i_ctx_t *i_ctx_p)
393
0
{
394
0
    os_ptr op = osp;
395
0
    int code;
396
0
    ref *pcmapname;
397
0
    ref *puidoffset;
398
0
    ref *pcodemapdata;
399
0
    ref *pcodemap;
400
0
    ref rname, rcidsi, rcoderanges, rdefs, rnotdefs;
401
0
    gs_cmap_adobe1_t *pcmap = 0;
402
0
    ref rcmap;
403
0
    uint i;
404
405
0
    check_op(1);
406
0
    check_type(*op, t_dictionary);
407
0
    check_dict_write(*op);
408
0
    if ((code = dict_find_string(op, "CMapName", &pcmapname)) <= 0) {
409
0
        code = gs_note_error(gs_error_rangecheck);
410
0
        goto fail;
411
0
    }
412
0
    if (!r_has_type(pcmapname, t_name)) {
413
0
        code = gs_note_error(gs_error_typecheck);
414
0
        goto fail;
415
0
    }
416
0
    name_string_ref(imemory, pcmapname, &rname);
417
0
    if (dict_find_string(op, ".CodeMapData", &pcodemapdata) <= 0 ||
418
0
        !r_has_type(pcodemapdata, t_array) ||
419
0
        r_size(pcodemapdata) != 3 ||
420
0
        dict_find_string(op, "CodeMap", &pcodemap) <= 0 ||
421
0
        !r_has_type(pcodemap, t_null)
422
0
        ) {
423
0
        code = gs_note_error(gs_error_rangecheck);
424
0
        goto fail;
425
0
    }
426
0
    if ((code = acquire_cid_system_info(&rcidsi, op)) < 0)
427
0
        goto fail;
428
0
    if ((code = gs_cmap_adobe1_alloc(&pcmap, 0, rname.value.const_bytes,
429
0
                                     r_size(&rname), r_size(&rcidsi),
430
0
                                     0, 0, 0, 0, 0, imemory)) < 0)
431
0
        goto fail;
432
0
    if ((code = dict_int_param(op, "CMapType", 0, 1, 0, &pcmap->CMapType)) < 0 ||
433
0
        (code = dict_float_param(op, "CMapVersion", 0.0, &pcmap->CMapVersion)) < 0 ||
434
0
        (code = dict_uid_param(op, &pcmap->uid, 0, imemory, i_ctx_p)) < 0 ||
435
0
        (code = dict_int_param(op, "WMode", 0, 1, 0, &pcmap->WMode)) < 0
436
0
        )
437
0
        goto fail;
438
0
    if (dict_find_string(op, "UIDOffset", &puidoffset) > 0) {
439
0
        if (!r_has_type(puidoffset, t_integer)) {
440
0
            code = gs_note_error(gs_error_typecheck);
441
0
            goto fail;
442
0
        }
443
0
        pcmap->UIDOffset = puidoffset->value.intval; /* long, not int */
444
0
    }
445
0
    for (i = 0; i < r_size(&rcidsi); ++i) {
446
0
        code = get_cid_system_info(imemory, pcmap->CIDSystemInfo + i, &rcidsi, i);
447
0
        if (code < 0)
448
0
            goto fail;
449
0
    }
450
0
    array_get(imemory, pcodemapdata, 0L, &rcoderanges);
451
0
    array_get(imemory, pcodemapdata, 1L, &rdefs);
452
0
    array_get(imemory, pcodemapdata, 2L, &rnotdefs);
453
0
    if ((code = acquire_code_ranges(pcmap, &rcoderanges, imemory)) < 0)
454
0
        goto fail;
455
0
    if ((code = acquire_code_map(&pcmap->def, &rdefs, pcmap, imemory)) < 0)
456
0
        goto fail;
457
0
    if ((code = acquire_code_map(&pcmap->notdef, &rnotdefs, pcmap, imemory)) < 0)
458
0
        goto fail;
459
0
    if (!bytes_compare(pcmap->CIDSystemInfo->Registry.data, pcmap->CIDSystemInfo->Registry.size,
460
0
                    (const byte *)"Artifex", 7) &&
461
0
        !bytes_compare(pcmap->CIDSystemInfo->Ordering.data, pcmap->CIDSystemInfo->Ordering.size,
462
0
                    (const byte *)"Unicode", 7))
463
0
        pcmap->from_Unicode = true;
464
0
    pcmap->mark_glyph = zfont_mark_glyph_name;
465
0
    pcmap->mark_glyph_data = 0;
466
0
    pcmap->glyph_name = zfcmap_glyph_name;
467
0
    pcmap->glyph_name_data = 0;
468
0
    make_istruct_new(&rcmap, a_readonly, pcmap);
469
0
    code = idict_put_string(op, "CodeMap", &rcmap);
470
0
    if (code < 0)
471
0
        goto fail;
472
0
    return zreadonly(i_ctx_p);
473
0
fail:
474
0
    if (pcmap) {
475
0
        free_code_map(&pcmap->notdef, imemory);
476
0
        free_code_map(&pcmap->def, imemory);
477
0
        ifree_object(pcmap->CIDSystemInfo, "zbuildcmap(CIDSystemInfo)");
478
0
        ifree_object(pcmap, "zbuildcmap(cmap)");
479
0
    }
480
0
    return code;
481
0
}
482
483
/* ------ Initialization procedure ------ */
484
485
const op_def zfcmap_op_defs[] =
486
{
487
    {"1.buildcmap", zbuildcmap},
488
    op_def_end(0)
489
};