Coverage Report

Created: 2025-08-28 07:06

/src/ghostpdl/psi/zfcmap.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-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
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
            r_size(&rprefix) > 4 ||
175
0
            rmisc.value.bytes[0] > MAX_CMAP_CODE_SIZE - r_size(&rprefix) ||
176
0
            rmisc.value.bytes[1] > 1 ||
177
0
            rmisc.value.bytes[2] > CODE_VALUE_MAX ||
178
0
            rmisc.value.bytes[3] == 0)
179
0
            return_error(gs_error_rangecheck);
180
0
        pclr->cmap = root;
181
0
        pclr->key_size = rmisc.value.bytes[0];
182
0
        pclr->key_prefix_size = r_size(&rprefix);
183
0
        memcpy(pclr->key_prefix, rprefix.value.bytes, pclr->key_prefix_size);
184
0
        pclr->key_is_range = rmisc.value.bytes[1];
185
0
        if (pclr->key_size == 0) {
186
            /* This is a single entry consisting only of the prefix. */
187
0
            if (r_size(&rkeys) != 0)
188
0
                return_error(gs_error_rangecheck);
189
0
            pclr->num_entries = 1;
190
0
        } else {
191
0
            int step = pclr->key_size * (pclr->key_is_range ? 2 : 1);
192
193
0
            if (r_size(&rkeys) % step != 0)
194
0
                return_error(gs_error_rangecheck);
195
0
            pclr->num_entries = r_size(&rkeys) / step;
196
0
        }
197
0
        pclr->keys.data = rkeys.value.bytes,
198
0
            pclr->keys.size = r_size(&rkeys);
199
0
        pclr->value_type = rmisc.value.bytes[2];
200
0
        pclr->value_size = rmisc.value.bytes[3];
201
0
        if (r_has_type(&rvalues, t_string)) {
202
0
            if (pclr->value_type == CODE_VALUE_GLYPH)
203
0
                return_error(gs_error_rangecheck);
204
0
            if (r_size(&rvalues) % pclr->num_entries != 0 ||
205
0
                r_size(&rvalues) / pclr->num_entries != pclr->value_size)
206
0
                return_error(gs_error_rangecheck);
207
0
            pclr->values.data = rvalues.value.bytes,
208
0
                pclr->values.size = r_size(&rvalues);
209
0
        } else {
210
0
            uint values_size = pclr->num_entries * pclr->value_size;
211
0
            long k;
212
0
            byte *pvalue;
213
214
0
            if (pclr->value_type != CODE_VALUE_GLYPH ||
215
0
                r_size(&rvalues) != pclr->num_entries ||
216
0
                pclr->value_size > sizeof(gs_glyph))
217
0
                return_error(gs_error_rangecheck);
218
0
            pclr->values.data = gs_alloc_string(mem, values_size,
219
0
                                                "acquire_code_map(values)");
220
0
            if (pclr->values.data == 0)
221
0
                return_error(gs_error_VMerror);
222
0
            pclr->values.size = values_size;
223
0
            pvalue = pclr->values.data;
224
0
            for (k = 0; k < pclr->num_entries; ++k) {
225
0
                ref rvalue;
226
0
                gs_glyph value;
227
0
                int i;
228
229
0
                array_get(mem, &rvalues, k, &rvalue);
230
0
                if (!r_has_type(&rvalue, t_name))
231
0
                    return_error(gs_error_rangecheck);
232
0
                value = name_index(mem, &rvalue);
233
                /*
234
                 * We need a special check here because some CPUs cannot
235
                 * shift by the full size of an int or long.
236
                 */
237
0
                if (pclr->value_size < sizeof(value) &&
238
0
                    (value >> (pclr->value_size * 8)) != 0
239
0
                    )
240
0
                    return_error(gs_error_rangecheck);
241
0
                for (i = pclr->value_size; --i >= 0; )
242
0
                    *pvalue++ = (byte)(value >> (i * 8));
243
0
            }
244
0
        }
245
0
        check_int_leu_only(rfxs, 0xff);
246
0
        pclr->font_index = (int)rfxs.value.intval;
247
0
            ++pclr;
248
0
        }
249
0
    }
250
0
    return 0;
251
0
}
252
253
/*
254
 * Acquire the CIDSystemInfo array from a dictionary.  If missing, fabricate
255
 * a 0-element array and return 1.
256
 */
257
static int
258
acquire_cid_system_info(ref *psia, const ref *op)
259
0
{
260
0
    ref *prcidsi;
261
262
0
    if (dict_find_string(op, "CIDSystemInfo", &prcidsi) <= 0) {
263
0
        make_empty_array(psia, a_readonly);
264
0
        return 1;
265
0
    }
266
0
    if (r_has_type(prcidsi, t_dictionary)) {
267
0
        make_array(psia, a_readonly, 1, prcidsi);
268
0
        return 0;
269
0
    }
270
0
    if (!r_is_array(prcidsi))
271
0
        return_error(gs_error_typecheck);
272
0
    *psia = *prcidsi;
273
0
    return 0;
274
0
}
275
276
/*
277
 * Get one element of a CIDSystemInfo array.  If the element is missing or
278
 * null, return 1.
279
 */
280
static int
281
get_cid_system_info(const gs_memory_t *mem, gs_cid_system_info_t *pcidsi, const ref *psia, uint index)
282
0
{
283
0
    ref rcidsi;
284
0
    int code = array_get(mem, psia, (long)index, &rcidsi);
285
286
0
    if (code < 0 || r_has_type(&rcidsi, t_null)) {
287
0
        cid_system_info_set_null(pcidsi);
288
0
        return 1;
289
0
    }
290
0
    return cid_system_info_param(pcidsi, &rcidsi);
291
0
}
292
293
#ifdef CHECK_CID_SYSTEM_INFO_COMPATIBILITY
294
295
/* Check compatibility of CIDSystemInfo. */
296
static bool
297
bytes_eq(const gs_const_string *pcs1, const gs_const_string *pcs2)
298
{
299
    return !bytes_compare(pcs1->data, pcs1->size,
300
                          pcs2->data, pcs2->size);
301
}
302
static bool
303
cid_system_info_compatible(const gs_cid_system_info_t * psi1,
304
                           const gs_cid_system_info_t * psi2)
305
{
306
    return bytes_eq(&psi1->Registry, &psi2->Registry) &&
307
        bytes_eq(&psi1->Ordering, &psi2->Ordering);
308
}
309
310
#endif /* CHECK_CID_SYSTEM_INFO_COMPATIBILITY */
311
312
/* ---------------- (Semi-)public procedures ---------------- */
313
314
extern_st(st_cmap_tt_16bit_format4);
315
extern_st(st_cmap_identity);
316
extern_st(st_cmap_ToUnicode);
317
318
/* Get the CodeMap from a Type 0 font, and check the CIDSystemInfo of */
319
/* its subsidiary fonts. */
320
int
321
ztype0_get_cmap(const gs_cmap_t **ppcmap, const ref *pfdepvector,
322
                const ref *op, gs_memory_t *imem)
323
0
{
324
0
    ref *prcmap;
325
0
    ref *pcodemap;
326
0
    const gs_cmap_t *pcmap;
327
0
    int code;
328
0
    uint num_fonts;
329
0
    uint i;
330
331
0
    if (dict_find_string(op, "CMap", &prcmap) <= 0 ||
332
0
        !r_has_type(prcmap, t_dictionary) ||
333
0
        dict_find_string(prcmap, "CodeMap", &pcodemap) <= 0 ||
334
0
        !r_is_struct(pcodemap) || (!r_has_stype(pcodemap, imem, st_cmap_tt_16bit_format4) &&
335
0
        !r_has_stype(pcodemap, imem, st_cmap_identity) && !r_has_stype(pcodemap, imem, st_cmap_ToUnicode) &&
336
0
        !r_has_stype(pcodemap, imem, st_cmap_adobe1))
337
0
        )
338
0
        return_error(gs_error_invalidfont);
339
0
    pcmap = r_ptr(pcodemap, gs_cmap_t);
340
0
    num_fonts = r_size(pfdepvector);
341
0
    for (i = 0; i < num_fonts; ++i) {
342
0
        ref rfdep, rfsi;
343
344
0
        array_get(imem, pfdepvector, (long)i, &rfdep);
345
0
        code = acquire_cid_system_info(&rfsi, &rfdep);
346
0
        if (code < 0)
347
0
            return code;
348
0
        if (code == 0) {
349
0
            if (r_size(&rfsi) != 1)
350
0
                return_error(gs_error_rangecheck);
351
#ifdef CHECK_CID_SYSTEM_INFO_COMPATIBILITY
352
            {
353
                gs_cid_system_info_t cidsi;
354
355
                get_cid_system_info(&cidsi, &rfsi, 0);
356
                if (!cid_system_info_is_null(&cidsi) &&
357
                    !cid_system_info_compatible(&cidsi,
358
                                                pcmap->CIDSystemInfo + i))
359
                    return_error(gs_error_rangecheck);
360
            }
361
#endif
362
0
        }
363
0
    }
364
0
    *ppcmap = pcmap;
365
0
    return 0;
366
0
}
367
368
/* ---------------- Operators ---------------- */
369
370
/* <CMap> .buildcmap <CMap> */
371
/*
372
 * Create the internal form of a CMap.  The initial CMap must be read-write
373
 * and have an entry with key = CodeMap and value = null; the result is
374
 * read-only and has a real CodeMap.
375
 *
376
 * This operator reads the CMapType, CMapName, CIDSystemInfo, CMapVersion,
377
 * UIDOffset, XUID, WMode, and .CodeMapData elements of the CMap dictionary.
378
 * For details, see lib/gs_cmap.ps and the Adobe documentation.
379
 */
380
static int
381
zfcmap_glyph_name(const gs_memory_t *mem,
382
                  gs_glyph glyph, gs_const_string *pstr, void *proc_data)
383
0
{
384
0
    ref nref, nsref;
385
386
0
    name_index_ref(mem, (uint)glyph, &nref);
387
0
    name_string_ref(mem, &nref, &nsref);
388
0
    pstr->data = nsref.value.const_bytes;
389
0
    pstr->size = r_size(&nsref);
390
0
    return 0;
391
0
}
392
static int
393
zbuildcmap(i_ctx_t *i_ctx_p)
394
0
{
395
0
    os_ptr op = osp;
396
0
    int code;
397
0
    ref *pcmapname;
398
0
    ref *puidoffset;
399
0
    ref *pcodemapdata;
400
0
    ref *pcodemap;
401
0
    ref rname, rcidsi, rcoderanges, rdefs, rnotdefs;
402
0
    gs_cmap_adobe1_t *pcmap = 0;
403
0
    ref rcmap;
404
0
    uint i;
405
406
0
    check_op(1);
407
0
    check_type(*op, t_dictionary);
408
0
    check_dict_write(*op);
409
0
    if ((code = dict_find_string(op, "CMapName", &pcmapname)) <= 0) {
410
0
        code = gs_note_error(gs_error_rangecheck);
411
0
        goto fail;
412
0
    }
413
0
    if (!r_has_type(pcmapname, t_name)) {
414
0
        code = gs_note_error(gs_error_typecheck);
415
0
        goto fail;
416
0
    }
417
0
    name_string_ref(imemory, pcmapname, &rname);
418
0
    if (dict_find_string(op, ".CodeMapData", &pcodemapdata) <= 0 ||
419
0
        !r_has_type(pcodemapdata, t_array) ||
420
0
        r_size(pcodemapdata) != 3 ||
421
0
        dict_find_string(op, "CodeMap", &pcodemap) <= 0 ||
422
0
        !r_has_type(pcodemap, t_null)
423
0
        ) {
424
0
        code = gs_note_error(gs_error_rangecheck);
425
0
        goto fail;
426
0
    }
427
0
    if ((code = acquire_cid_system_info(&rcidsi, op)) < 0)
428
0
        goto fail;
429
0
    if ((code = gs_cmap_adobe1_alloc(&pcmap, 0, rname.value.const_bytes,
430
0
                                     r_size(&rname), r_size(&rcidsi),
431
0
                                     0, 0, 0, 0, 0, imemory)) < 0)
432
0
        goto fail;
433
0
    if ((code = dict_int_param(op, "CMapType", 0, 1, 0, &pcmap->CMapType)) < 0 ||
434
0
        (code = dict_float_param(op, "CMapVersion", 0.0, &pcmap->CMapVersion)) < 0 ||
435
0
        (code = dict_uid_param(op, &pcmap->uid, 0, imemory, i_ctx_p)) < 0 ||
436
0
        (code = dict_int_param(op, "WMode", 0, 1, 0, &pcmap->WMode)) < 0
437
0
        )
438
0
        goto fail;
439
0
    if (dict_find_string(op, "UIDOffset", &puidoffset) > 0) {
440
0
        if (!r_has_type(puidoffset, t_integer)) {
441
0
            code = gs_note_error(gs_error_typecheck);
442
0
            goto fail;
443
0
        }
444
0
        pcmap->UIDOffset = puidoffset->value.intval; /* long, not int */
445
0
    }
446
0
    for (i = 0; i < r_size(&rcidsi); ++i) {
447
0
        code = get_cid_system_info(imemory, pcmap->CIDSystemInfo + i, &rcidsi, i);
448
0
        if (code < 0)
449
0
            goto fail;
450
0
    }
451
0
    array_get(imemory, pcodemapdata, 0L, &rcoderanges);
452
0
    array_get(imemory, pcodemapdata, 1L, &rdefs);
453
0
    array_get(imemory, pcodemapdata, 2L, &rnotdefs);
454
0
    if ((code = acquire_code_ranges(pcmap, &rcoderanges, imemory)) < 0)
455
0
        goto fail;
456
0
    if ((code = acquire_code_map(&pcmap->def, &rdefs, pcmap, imemory)) < 0)
457
0
        goto fail;
458
0
    if ((code = acquire_code_map(&pcmap->notdef, &rnotdefs, pcmap, imemory)) < 0)
459
0
        goto fail;
460
0
    if (!bytes_compare(pcmap->CIDSystemInfo->Registry.data, pcmap->CIDSystemInfo->Registry.size,
461
0
                    (const byte *)"Artifex", 7) &&
462
0
        !bytes_compare(pcmap->CIDSystemInfo->Ordering.data, pcmap->CIDSystemInfo->Ordering.size,
463
0
                    (const byte *)"Unicode", 7))
464
0
        pcmap->from_Unicode = true;
465
0
    pcmap->mark_glyph = zfont_mark_glyph_name;
466
0
    pcmap->mark_glyph_data = 0;
467
0
    pcmap->glyph_name = zfcmap_glyph_name;
468
0
    pcmap->glyph_name_data = 0;
469
0
    make_istruct_new(&rcmap, a_readonly, pcmap);
470
0
    code = idict_put_string(op, "CodeMap", &rcmap);
471
0
    if (code < 0)
472
0
        goto fail;
473
0
    return zreadonly(i_ctx_p);
474
0
fail:
475
0
    if (pcmap) {
476
0
        free_code_map(&pcmap->notdef, imemory);
477
0
        free_code_map(&pcmap->def, imemory);
478
0
        ifree_object(pcmap->CIDSystemInfo, "zbuildcmap(CIDSystemInfo)");
479
0
        ifree_object(pcmap, "zbuildcmap(cmap)");
480
0
    }
481
0
    return code;
482
0
}
483
484
/* ------ Initialization procedure ------ */
485
486
const op_def zfcmap_op_defs[] =
487
{
488
    {"1.buildcmap", zbuildcmap},
489
    op_def_end(0)
490
};