Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/psi/zfont2.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
/* Type 2 font creation operators */
18
#include "ghost.h"
19
#include "string_.h"  /* for CFF parser */
20
#include "oper.h"
21
#include "gxfixed.h"
22
#include "gsmatrix.h"
23
#include "gxfont.h"
24
#include "gxfont1.h"
25
#include "bfont.h"
26
#include "idict.h"
27
#include "idparam.h"
28
#include "ifont1.h"
29
#include "ifont2.h"
30
#include "ialloc.h"
31
#include "iname.h"   /* for CFF parser */
32
#include "iddict.h"  /* for CFF parser */
33
#include "store.h"   /* for CFF parser */
34
#include "iscannum.h"
35
36
/* Private utilities */
37
static uint
38
subr_bias(const ref * psubrs)
39
0
{
40
0
    uint size = r_size(psubrs);
41
42
0
    return (size < 1240 ? 107 : size < 33900 ? 1131 : 32768);
43
0
}
44
45
/*
46
 * Get the additional parameters for a Type 2 font (or FontType 2 FDArray
47
 * entry in a CIDFontType 0 font), beyond those common to Type 1 and Type 2
48
 * fonts.
49
 */
50
int
51
type2_font_params(const_os_ptr op, charstring_font_refs_t *pfr,
52
                  gs_type1_data *pdata1)
53
0
{
54
0
    int code;
55
0
    float dwx, nwx;
56
0
    ref *temp;
57
58
0
    pdata1->interpret = gs_type2_interpret;
59
0
    pdata1->lenIV = DEFAULT_LENIV_2;
60
0
    pdata1->subroutineNumberBias = subr_bias(pfr->Subrs);
61
    /* Get information specific to Type 2 fonts. */
62
0
    if (dict_find_string(pfr->Private, "GlobalSubrs", &temp) > 0) {
63
0
        if (!r_is_array(temp))
64
0
            return_error(gs_error_typecheck);
65
0
        pfr->GlobalSubrs = temp;
66
0
    }
67
0
    pdata1->gsubrNumberBias = subr_bias(pfr->GlobalSubrs);
68
0
    if ((code = dict_uint_param(pfr->Private, "gsubrNumberBias",
69
0
                                0, max_uint, pdata1->gsubrNumberBias,
70
0
                                &pdata1->gsubrNumberBias)) < 0 ||
71
0
        (code = dict_float_param(pfr->Private, "defaultWidthX", 0.0,
72
0
                                 &dwx)) < 0 ||
73
0
        (code = dict_float_param(pfr->Private, "nominalWidthX", 0.0,
74
0
                                 &nwx)) < 0
75
0
        )
76
0
        return code;
77
0
    pdata1->defaultWidthX = float2fixed(dwx);
78
0
    pdata1->nominalWidthX = float2fixed(nwx);
79
0
    {
80
0
        ref *pirs;
81
82
0
        if (dict_find_string(pfr->Private, "initialRandomSeed", &pirs) <= 0)
83
0
            pdata1->initialRandomSeed = 0;
84
0
        else if (!r_has_type(pirs, t_integer))
85
0
            return_error(gs_error_typecheck);
86
0
        else
87
0
            pdata1->initialRandomSeed = pirs->value.intval;
88
0
    }
89
0
    return 0;
90
0
}
91
92
/* <string|name> <font_dict> .buildfont2 <string|name> <font> */
93
/* Build a type 2 (compact Adobe encrypted) font. */
94
static int
95
zbuildfont2(i_ctx_t *i_ctx_p)
96
11
{
97
11
    os_ptr op = osp;
98
11
    charstring_font_refs_t refs;
99
11
    build_proc_refs build;
100
11
    int code = build_proc_name_refs(imemory, &build,
101
11
                                    "%Type2BuildChar", "%Type2BuildGlyph");
102
11
    gs_type1_data data1;
103
104
11
    if (code < 0)
105
0
        return code;
106
11
    code = charstring_font_get_refs(op, &refs);
107
11
    if (code < 0)
108
11
        return code;
109
0
    code = type2_font_params(op, &refs, &data1);
110
0
    if (code < 0)
111
0
        return code;
112
0
    return build_charstring_font(i_ctx_p, op, &build, ft_encrypted2, &refs,
113
0
                                 &data1, bf_notdef_required);
114
0
}
115
116
/* CFF parser */
117
118
0
#define STR2MEM(s)    s, (sizeof(s) - 1)
119
0
#define MAXOP 50   /* Max value according to TN 5176 is 48 */
120
121
static const char * const standard_strings[] = {
122
    /*   0 */ ".notdef",
123
    /*   1 */ "space",
124
    /*   2 */ "exclam",
125
    /*   3 */ "quotedbl",
126
    /*   4 */ "numbersign",
127
    /*   5 */ "dollar",
128
    /*   6 */ "percent",
129
    /*   7 */ "ampersand",
130
    /*   8 */ "quoteright",
131
    /*   9 */ "parenleft",
132
    /*  10 */ "parenright",
133
    /*  11 */ "asterisk",
134
    /*  12 */ "plus",
135
    /*  13 */ "comma",
136
    /*  14 */ "hyphen",
137
    /*  15 */ "period",
138
    /*  16 */ "slash",
139
    /*  17 */ "zero",
140
    /*  18 */ "one",
141
    /*  19 */ "two",
142
    /*  20 */ "three",
143
    /*  21 */ "four",
144
    /*  22 */ "five",
145
    /*  23 */ "six",
146
    /*  24 */ "seven",
147
    /*  25 */ "eight",
148
    /*  26 */ "nine",
149
    /*  27 */ "colon",
150
    /*  28 */ "semicolon",
151
    /*  29 */ "less",
152
    /*  30 */ "equal",
153
    /*  31 */ "greater",
154
    /*  32 */ "question",
155
    /*  33 */ "at",
156
    /*  34 */ "A",
157
    /*  35 */ "B",
158
    /*  36 */ "C",
159
    /*  37 */ "D",
160
    /*  38 */ "E",
161
    /*  39 */ "F",
162
    /*  40 */ "G",
163
    /*  41 */ "H",
164
    /*  42 */ "I",
165
    /*  43 */ "J",
166
    /*  44 */ "K",
167
    /*  45 */ "L",
168
    /*  46 */ "M",
169
    /*  47 */ "N",
170
    /*  48 */ "O",
171
    /*  49 */ "P",
172
    /*  50 */ "Q",
173
    /*  51 */ "R",
174
    /*  52 */ "S",
175
    /*  53 */ "T",
176
    /*  54 */ "U",
177
    /*  55 */ "V",
178
    /*  56 */ "W",
179
    /*  57 */ "X",
180
    /*  58 */ "Y",
181
    /*  59 */ "Z",
182
    /*  60 */ "bracketleft",
183
    /*  61 */ "backslash",
184
    /*  62 */ "bracketright",
185
    /*  63 */ "asciicircum",
186
    /*  64 */ "underscore",
187
    /*  65 */ "quoteleft",
188
    /*  66 */ "a",
189
    /*  67 */ "b",
190
    /*  68 */ "c",
191
    /*  69 */ "d",
192
    /*  70 */ "e",
193
    /*  71 */ "f",
194
    /*  72 */ "g",
195
    /*  73 */ "h",
196
    /*  74 */ "i",
197
    /*  75 */ "j",
198
    /*  76 */ "k",
199
    /*  77 */ "l",
200
    /*  78 */ "m",
201
    /*  79 */ "n",
202
    /*  80 */ "o",
203
    /*  81 */ "p",
204
    /*  82 */ "q",
205
    /*  83 */ "r",
206
    /*  84 */ "s",
207
    /*  85 */ "t",
208
    /*  86 */ "u",
209
    /*  87 */ "v",
210
    /*  88 */ "w",
211
    /*  89 */ "x",
212
    /*  90 */ "y",
213
    /*  91 */ "z",
214
    /*  92 */ "braceleft",
215
    /*  93 */ "bar",
216
    /*  94 */ "braceright",
217
    /*  95 */ "asciitilde",
218
    /*  96 */ "exclamdown",
219
    /*  97 */ "cent",
220
    /*  98 */ "sterling",
221
    /*  99 */ "fraction",
222
    /* 100 */ "yen",
223
    /* 101 */ "florin",
224
    /* 102 */ "section",
225
    /* 103 */ "currency",
226
    /* 104 */ "quotesingle",
227
    /* 105 */ "quotedblleft",
228
    /* 106 */ "guillemotleft",
229
    /* 107 */ "guilsinglleft",
230
    /* 108 */ "guilsinglright",
231
    /* 109 */ "fi",
232
    /* 110 */ "fl",
233
    /* 111 */ "endash",
234
    /* 112 */ "dagger",
235
    /* 113 */ "daggerdbl",
236
    /* 114 */ "periodcentered",
237
    /* 115 */ "paragraph",
238
    /* 116 */ "bullet",
239
    /* 117 */ "quotesinglbase",
240
    /* 118 */ "quotedblbase",
241
    /* 119 */ "quotedblright",
242
    /* 120 */ "guillemotright",
243
    /* 121 */ "ellipsis",
244
    /* 122 */ "perthousand",
245
    /* 123 */ "questiondown",
246
    /* 124 */ "grave",
247
    /* 125 */ "acute",
248
    /* 126 */ "circumflex",
249
    /* 127 */ "tilde",
250
    /* 128 */ "macron",
251
    /* 129 */ "breve",
252
    /* 130 */ "dotaccent",
253
    /* 131 */ "dieresis",
254
    /* 132 */ "ring",
255
    /* 133 */ "cedilla",
256
    /* 134 */ "hungarumlaut",
257
    /* 135 */ "ogonek",
258
    /* 136 */ "caron",
259
    /* 137 */ "emdash",
260
    /* 138 */ "AE",
261
    /* 139 */ "ordfeminine",
262
    /* 140 */ "Lslash",
263
    /* 141 */ "Oslash",
264
    /* 142 */ "OE",
265
    /* 143 */ "ordmasculine",
266
    /* 144 */ "ae",
267
    /* 145 */ "dotlessi",
268
    /* 146 */ "lslash",
269
    /* 147 */ "oslash",
270
    /* 148 */ "oe",
271
    /* 149 */ "germandbls",
272
    /* 150 */ "onesuperior",
273
    /* 151 */ "logicalnot",
274
    /* 152 */ "mu",
275
    /* 153 */ "trademark",
276
    /* 154 */ "Eth",
277
    /* 155 */ "onehalf",
278
    /* 156 */ "plusminus",
279
    /* 157 */ "Thorn",
280
    /* 158 */ "onequarter",
281
    /* 159 */ "divide",
282
    /* 160 */ "brokenbar",
283
    /* 161 */ "degree",
284
    /* 162 */ "thorn",
285
    /* 163 */ "threequarters",
286
    /* 164 */ "twosuperior",
287
    /* 165 */ "registered",
288
    /* 166 */ "minus",
289
    /* 167 */ "eth",
290
    /* 168 */ "multiply",
291
    /* 169 */ "threesuperior",
292
    /* 170 */ "copyright",
293
    /* 171 */ "Aacute",
294
    /* 172 */ "Acircumflex",
295
    /* 173 */ "Adieresis",
296
    /* 174 */ "Agrave",
297
    /* 175 */ "Aring",
298
    /* 176 */ "Atilde",
299
    /* 177 */ "Ccedilla",
300
    /* 178 */ "Eacute",
301
    /* 179 */ "Ecircumflex",
302
    /* 180 */ "Edieresis",
303
    /* 181 */ "Egrave",
304
    /* 182 */ "Iacute",
305
    /* 183 */ "Icircumflex",
306
    /* 184 */ "Idieresis",
307
    /* 185 */ "Igrave",
308
    /* 186 */ "Ntilde",
309
    /* 187 */ "Oacute",
310
    /* 188 */ "Ocircumflex",
311
    /* 189 */ "Odieresis",
312
    /* 190 */ "Ograve",
313
    /* 191 */ "Otilde",
314
    /* 192 */ "Scaron",
315
    /* 193 */ "Uacute",
316
    /* 194 */ "Ucircumflex",
317
    /* 195 */ "Udieresis",
318
    /* 196 */ "Ugrave",
319
    /* 197 */ "Yacute",
320
    /* 198 */ "Ydieresis",
321
    /* 199 */ "Zcaron",
322
    /* 200 */ "aacute",
323
    /* 201 */ "acircumflex",
324
    /* 202 */ "adieresis",
325
    /* 203 */ "agrave",
326
    /* 204 */ "aring",
327
    /* 205 */ "atilde",
328
    /* 206 */ "ccedilla",
329
    /* 207 */ "eacute",
330
    /* 208 */ "ecircumflex",
331
    /* 209 */ "edieresis",
332
    /* 210 */ "egrave",
333
    /* 211 */ "iacute",
334
    /* 212 */ "icircumflex",
335
    /* 213 */ "idieresis",
336
    /* 214 */ "igrave",
337
    /* 215 */ "ntilde",
338
    /* 216 */ "oacute",
339
    /* 217 */ "ocircumflex",
340
    /* 218 */ "odieresis",
341
    /* 219 */ "ograve",
342
    /* 220 */ "otilde",
343
    /* 221 */ "scaron",
344
    /* 222 */ "uacute",
345
    /* 223 */ "ucircumflex",
346
    /* 224 */ "udieresis",
347
    /* 225 */ "ugrave",
348
    /* 226 */ "yacute",
349
    /* 227 */ "ydieresis",
350
    /* 228 */ "zcaron",
351
    /* 229 */ "exclamsmall",
352
    /* 230 */ "Hungarumlautsmall",
353
    /* 231 */ "dollaroldstyle",
354
    /* 232 */ "dollarsuperior",
355
    /* 233 */ "ampersandsmall",
356
    /* 234 */ "Acutesmall",
357
    /* 235 */ "parenleftsuperior",
358
    /* 236 */ "parenrightsuperior",
359
    /* 237 */ "twodotenleader",
360
    /* 238 */ "onedotenleader",
361
    /* 239 */ "zerooldstyle",
362
    /* 240 */ "oneoldstyle",
363
    /* 241 */ "twooldstyle",
364
    /* 242 */ "threeoldstyle",
365
    /* 243 */ "fouroldstyle",
366
    /* 244 */ "fiveoldstyle",
367
    /* 245 */ "sixoldstyle",
368
    /* 246 */ "sevenoldstyle",
369
    /* 247 */ "eightoldstyle",
370
    /* 248 */ "nineoldstyle",
371
    /* 249 */ "commasuperior",
372
    /* 250 */ "threequartersemdash",
373
    /* 251 */ "periodsuperior",
374
    /* 252 */ "questionsmall",
375
    /* 253 */ "asuperior",
376
    /* 254 */ "bsuperior",
377
    /* 255 */ "centsuperior",
378
    /* 256 */ "dsuperior",
379
    /* 257 */ "esuperior",
380
    /* 258 */ "isuperior",
381
    /* 259 */ "lsuperior",
382
    /* 260 */ "msuperior",
383
    /* 261 */ "nsuperior",
384
    /* 262 */ "osuperior",
385
    /* 263 */ "rsuperior",
386
    /* 264 */ "ssuperior",
387
    /* 265 */ "tsuperior",
388
    /* 266 */ "ff",
389
    /* 267 */ "ffi",
390
    /* 268 */ "ffl",
391
    /* 269 */ "parenleftinferior",
392
    /* 270 */ "parenrightinferior",
393
    /* 271 */ "Circumflexsmall",
394
    /* 272 */ "hyphensuperior",
395
    /* 273 */ "Gravesmall",
396
    /* 274 */ "Asmall",
397
    /* 275 */ "Bsmall",
398
    /* 276 */ "Csmall",
399
    /* 277 */ "Dsmall",
400
    /* 278 */ "Esmall",
401
    /* 279 */ "Fsmall",
402
    /* 280 */ "Gsmall",
403
    /* 281 */ "Hsmall",
404
    /* 282 */ "Ismall",
405
    /* 283 */ "Jsmall",
406
    /* 284 */ "Ksmall",
407
    /* 285 */ "Lsmall",
408
    /* 286 */ "Msmall",
409
    /* 287 */ "Nsmall",
410
    /* 288 */ "Osmall",
411
    /* 289 */ "Psmall",
412
    /* 290 */ "Qsmall",
413
    /* 291 */ "Rsmall",
414
    /* 292 */ "Ssmall",
415
    /* 293 */ "Tsmall",
416
    /* 294 */ "Usmall",
417
    /* 295 */ "Vsmall",
418
    /* 296 */ "Wsmall",
419
    /* 297 */ "Xsmall",
420
    /* 298 */ "Ysmall",
421
    /* 299 */ "Zsmall",
422
    /* 300 */ "colonmonetary",
423
    /* 301 */ "onefitted",
424
    /* 302 */ "rupiah",
425
    /* 303 */ "Tildesmall",
426
    /* 304 */ "exclamdownsmall",
427
    /* 305 */ "centoldstyle",
428
    /* 306 */ "Lslashsmall",
429
    /* 307 */ "Scaronsmall",
430
    /* 308 */ "Zcaronsmall",
431
    /* 309 */ "Dieresissmall",
432
    /* 310 */ "Brevesmall",
433
    /* 311 */ "Caronsmall",
434
    /* 312 */ "Dotaccentsmall",
435
    /* 313 */ "Macronsmall",
436
    /* 314 */ "figuredash",
437
    /* 315 */ "hypheninferior",
438
    /* 316 */ "Ogoneksmall",
439
    /* 317 */ "Ringsmall",
440
    /* 318 */ "Cedillasmall",
441
    /* 319 */ "questiondownsmall",
442
    /* 320 */ "oneeighth",
443
    /* 321 */ "threeeighths",
444
    /* 322 */ "fiveeighths",
445
    /* 323 */ "seveneighths",
446
    /* 324 */ "onethird",
447
    /* 325 */ "twothirds",
448
    /* 326 */ "zerosuperior",
449
    /* 327 */ "foursuperior",
450
    /* 328 */ "fivesuperior",
451
    /* 329 */ "sixsuperior",
452
    /* 330 */ "sevensuperior",
453
    /* 331 */ "eightsuperior",
454
    /* 332 */ "ninesuperior",
455
    /* 333 */ "zeroinferior",
456
    /* 334 */ "oneinferior",
457
    /* 335 */ "twoinferior",
458
    /* 336 */ "threeinferior",
459
    /* 337 */ "fourinferior",
460
    /* 338 */ "fiveinferior",
461
    /* 339 */ "sixinferior",
462
    /* 340 */ "seveninferior",
463
    /* 341 */ "eightinferior",
464
    /* 342 */ "nineinferior",
465
    /* 343 */ "centinferior",
466
    /* 344 */ "dollarinferior",
467
    /* 345 */ "periodinferior",
468
    /* 346 */ "commainferior",
469
    /* 347 */ "Agravesmall",
470
    /* 348 */ "Aacutesmall",
471
    /* 349 */ "Acircumflexsmall",
472
    /* 350 */ "Atildesmall",
473
    /* 351 */ "Adieresissmall",
474
    /* 352 */ "Aringsmall",
475
    /* 353 */ "AEsmall",
476
    /* 354 */ "Ccedillasmall",
477
    /* 355 */ "Egravesmall",
478
    /* 356 */ "Eacutesmall",
479
    /* 357 */ "Ecircumflexsmall",
480
    /* 358 */ "Edieresissmall",
481
    /* 359 */ "Igravesmall",
482
    /* 360 */ "Iacutesmall",
483
    /* 361 */ "Icircumflexsmall",
484
    /* 362 */ "Idieresissmall",
485
    /* 363 */ "Ethsmall",
486
    /* 364 */ "Ntildesmall",
487
    /* 365 */ "Ogravesmall",
488
    /* 366 */ "Oacutesmall",
489
    /* 367 */ "Ocircumflexsmall",
490
    /* 368 */ "Otildesmall",
491
    /* 369 */ "Odieresissmall",
492
    /* 370 */ "OEsmall",
493
    /* 371 */ "Oslashsmall",
494
    /* 372 */ "Ugravesmall",
495
    /* 373 */ "Uacutesmall",
496
    /* 374 */ "Ucircumflexsmall",
497
    /* 375 */ "Udieresissmall",
498
    /* 376 */ "Yacutesmall",
499
    /* 377 */ "Thornsmall",
500
    /* 378 */ "Ydieresissmall",
501
    /* 379 */ "001.000",
502
    /* 380 */ "001.001",
503
    /* 381 */ "001.002",
504
    /* 382 */ "001.003",
505
    /* 383 */ "Black",
506
    /* 384 */ "Bold",
507
    /* 385 */ "Book",
508
    /* 386 */ "Light",
509
    /* 387 */ "Medium",
510
    /* 388 */ "Regular",
511
    /* 389 */ "Roman",
512
    /* 390 */ "Semibold"
513
};
514
515
const static unsigned short expert_charset[] = {
516
    1,   /* space */
517
    229, /* exclamsmall */
518
    230, /* Hungarumlautsmall */
519
    231, /* dollaroldstyle */
520
    232, /* dollarsuperior */
521
    233, /* ampersandsmall */
522
    234, /* Acutesmall */
523
    235, /* parenleftsuperior */
524
    236, /* parenrightsuperior */
525
    237, /* twodotenleader */
526
    238, /* onedotenleader */
527
    13,  /* comma */
528
    14,  /* hyphen */
529
    15,  /* period */
530
    99,  /* fraction */
531
    239, /* zerooldstyle */
532
    240, /* oneoldstyle */
533
    241, /* twooldstyle */
534
    242, /* threeoldstyle */
535
    243, /* fouroldstyle */
536
    244, /* fiveoldstyle */
537
    245, /* sixoldstyle */
538
    246, /* sevenoldstyle */
539
    247, /* eightoldstyle */
540
    248, /* nineoldstyle */
541
    27,  /* colon */
542
    28,  /* semicolon */
543
    249, /* commasuperior */
544
    250, /* threequartersemdash */
545
    251, /* periodsuperior */
546
    252, /* questionsmall */
547
    253, /* asuperior */
548
    254, /* bsuperior */
549
    255, /* centsuperior */
550
    256, /* dsuperior */
551
    257, /* esuperior */
552
    258, /* isuperior */
553
    259, /* lsuperior */
554
    260, /* msuperior */
555
    261, /* nsuperior */
556
    262, /* osuperior */
557
    263, /* rsuperior */
558
    264, /* ssuperior */
559
    265, /* tsuperior */
560
    266, /* ff */
561
    109, /* fi */
562
    110, /* fl */
563
    267, /* ffi */
564
    268, /* ffl */
565
    269, /* parenleftinferior */
566
    270, /* parenrightinferior */
567
    271, /* Circumflexsmall */
568
    272, /* hyphensuperior */
569
    273, /* Gravesmall */
570
    274, /* Asmall */
571
    275, /* Bsmall */
572
    276, /* Csmall */
573
    277, /* Dsmall */
574
    278, /* Esmall */
575
    279, /* Fsmall */
576
    280, /* Gsmall */
577
    281, /* Hsmall */
578
    282, /* Ismall */
579
    283, /* Jsmall */
580
    284, /* Ksmall */
581
    285, /* Lsmall */
582
    286, /* Msmall */
583
    287, /* Nsmall */
584
    288, /* Osmall */
585
    289, /* Psmall */
586
    290, /* Qsmall */
587
    291, /* Rsmall */
588
    292, /* Ssmall */
589
    293, /* Tsmall */
590
    294, /* Usmall */
591
    295, /* Vsmall */
592
    296, /* Wsmall */
593
    297, /* Xsmall */
594
    298, /* Ysmall */
595
    299, /* Zsmall */
596
    300, /* colonmonetary */
597
    301, /* onefitted */
598
    302, /* rupiah */
599
    303, /* Tildesmall */
600
    304, /* exclamdownsmall */
601
    305, /* centoldstyle */
602
    306, /* Lslashsmall */
603
    307, /* Scaronsmall */
604
    308, /* Zcaronsmall */
605
    309, /* Dieresissmall */
606
    310, /* Brevesmall */
607
    311, /* Caronsmall */
608
    312, /* Dotaccentsmall */
609
    313, /* Macronsmall */
610
    314, /* figuredash */
611
    315, /* hypheninferior */
612
    316, /* Ogoneksmall */
613
    317, /* Ringsmall */
614
    318, /* Cedillasmall */
615
    158, /* onequarter */
616
    155, /* onehalf */
617
    163, /* threequarters */
618
    319, /* questiondownsmall */
619
    320, /* oneeighth */
620
    321, /* threeeighths */
621
    322, /* fiveeighths */
622
    323, /* seveneighths */
623
    324, /* onethird */
624
    325, /* twothirds */
625
    326, /* zerosuperior */
626
    150, /* onesuperior */
627
    164, /* twosuperior */
628
    169, /* threesuperior */
629
    327, /* foursuperior */
630
    328, /* fivesuperior */
631
    329, /* sixsuperior */
632
    330, /* sevensuperior */
633
    331, /* eightsuperior */
634
    332, /* ninesuperior */
635
    333, /* zeroinferior */
636
    334, /* oneinferior */
637
    335, /* twoinferior */
638
    336, /* threeinferior */
639
    337, /* fourinferior */
640
    338, /* fiveinferior */
641
    339, /* sixinferior */
642
    340, /* seveninferior */
643
    341, /* eightinferior */
644
    342, /* nineinferior */
645
    343, /* centinferior */
646
    344, /* dollarinferior */
647
    345, /* periodinferior */
648
    346, /* commainferior */
649
    347, /* Agravesmall */
650
    348, /* Aacutesmall */
651
    349, /* Acircumflexsmall */
652
    350, /* Atildesmall */
653
    351, /* Adieresissmall */
654
    352, /* Aringsmall */
655
    353, /* AEsmall */
656
    354, /* Ccedillasmall */
657
    355, /* Egravesmall */
658
    356, /* Eacutesmall */
659
    357, /* Ecircumflexsmall */
660
    358, /* Edieresissmall */
661
    359, /* Igravesmall */
662
    360, /* Iacutesmall */
663
    361, /* Icircumflexsmall */
664
    362, /* Idieresissmall */
665
    363, /* Ethsmall */
666
    364, /* Ntildesmall */
667
    365, /* Ogravesmall */
668
    366, /* Oacutesmall */
669
    367, /* Ocircumflexsmall */
670
    368, /* Otildesmall */
671
    369, /* Odieresissmall */
672
    370, /* OEsmall */
673
    371, /* Oslashsmall */
674
    372, /* Ugravesmall */
675
    373, /* Uacutesmall */
676
    374, /* Ucircumflexsmall */
677
    375, /* Udieresissmall */
678
    376, /* Yacutesmall */
679
    377, /* Thornsmall */
680
    378  /* Ydieresissmall */
681
};
682
683
const static unsigned short expert_subset_charset[] = {
684
    1,   /*space */
685
    231, /*dollaroldstyle */
686
    232, /*dollarsuperior */
687
    235, /*parenleftsuperior */
688
    236, /*parenrightsuperior */
689
    237, /*twodotenleader */
690
    238, /*onedotenleader */
691
    13,  /*comma */
692
    14,  /*hyphen */
693
    15,  /*period */
694
    99,  /*fraction */
695
    239, /*zerooldstyle */
696
    240, /*oneoldstyle */
697
    241, /*twooldstyle */
698
    242, /*threeoldstyle */
699
    243, /*fouroldstyle */
700
    244, /*fiveoldstyle */
701
    245, /*sixoldstyle */
702
    246, /*sevenoldstyle */
703
    247, /*eightoldstyle */
704
    248, /*nineoldstyle */
705
    27,  /*colon */
706
    28,  /*semicolon */
707
    249, /*commasuperior */
708
    250, /*threequartersemdash */
709
    251, /*periodsuperior */
710
    253, /*asuperior */
711
    254, /*bsuperior */
712
    255, /*centsuperior */
713
    256, /*dsuperior */
714
    257, /*esuperior */
715
    258, /*isuperior */
716
    259, /*lsuperior */
717
    260, /*msuperior */
718
    261, /*nsuperior */
719
    262, /*osuperior */
720
    263, /*rsuperior */
721
    264, /*ssuperior */
722
    265, /*tsuperior */
723
    266, /*ff */
724
    109, /*fi */
725
    110, /*fl */
726
    267, /*ffi */
727
    268, /*ffl */
728
    269, /*parenleftinferior */
729
    270, /*parenrightinferior */
730
    272, /*hyphensuperior */
731
    300, /*colonmonetary */
732
    301, /*onefitted */
733
    302, /*rupiah */
734
    305, /*centoldstyle */
735
    314, /*figuredash */
736
    315, /*hypheninferior */
737
    158, /*onequarter */
738
    155, /*onehalf */
739
    163, /*threequarters */
740
    320, /*oneeighth */
741
    321, /*threeeighths */
742
    322, /*fiveeighths */
743
    323, /*seveneighths */
744
    324, /*onethird */
745
    325, /*twothirds */
746
    326, /*zerosuperior */
747
    150, /*onesuperior */
748
    164, /*twosuperior */
749
    169, /*threesuperior */
750
    327, /*foursuperior */
751
    328, /*fivesuperior */
752
    329, /*sixsuperior */
753
    330, /*sevensuperior */
754
    331, /*eightsuperior */
755
    332, /*ninesuperior */
756
    333, /*zeroinferior */
757
    334, /*oneinferior */
758
    335, /*twoinferior */
759
    336, /*threeinferior */
760
    337, /*fourinferior */
761
    338, /*fiveinferior */
762
    339, /*sixinferior */
763
    340, /*seveninferior */
764
    341, /*eightinferior */
765
    342, /*nineinferior */
766
    343, /*centinferior */
767
    344, /*dollarinferior */
768
    345, /*periodinferior */
769
    346  /*commainferior */
770
};
771
772
static const unsigned short standard_encoding[] = {
773
  /*   0 */   0, /* .notdef */
774
  /*   1 */   0, /* .notdef */
775
  /*   2 */   0, /* .notdef */
776
  /*   3 */   0, /* .notdef */
777
  /*   4 */   0, /* .notdef */
778
  /*   5 */   0, /* .notdef */
779
  /*   6 */   0, /* .notdef */
780
  /*   7 */   0, /* .notdef */
781
  /*   8 */   0, /* .notdef */
782
  /*   9 */   0, /* .notdef */
783
  /*  10 */   0, /* .notdef */
784
  /*  11 */   0, /* .notdef */
785
  /*  12 */   0, /* .notdef */
786
  /*  13 */   0, /* .notdef */
787
  /*  14 */   0, /* .notdef */
788
  /*  15 */   0, /* .notdef */
789
  /*  16 */   0, /* .notdef */
790
  /*  17 */   0, /* .notdef */
791
  /*  18 */   0, /* .notdef */
792
  /*  19 */   0, /* .notdef */
793
  /*  20 */   0, /* .notdef */
794
  /*  21 */   0, /* .notdef */
795
  /*  22 */   0, /* .notdef */
796
  /*  23 */   0, /* .notdef */
797
  /*  24 */   0, /* .notdef */
798
  /*  25 */   0, /* .notdef */
799
  /*  26 */   0, /* .notdef */
800
  /*  27 */   0, /* .notdef */
801
  /*  28 */   0, /* .notdef */
802
  /*  29 */   0, /* .notdef */
803
  /*  30 */   0, /* .notdef */
804
  /*  31 */   0, /* .notdef */
805
  /*  32 */   1, /* space */
806
  /*  33 */   2, /* exclam */
807
  /*  34 */   3, /* quotedbl */
808
  /*  35 */   4, /* numbersign */
809
  /*  36 */   5, /* dollar */
810
  /*  37 */   6, /* percent */
811
  /*  38 */   7, /* ampersand */
812
  /*  39 */   8, /* quoteright */
813
  /*  40 */   9, /* parenleft */
814
  /*  41 */  10, /* parenright */
815
  /*  42 */  11, /* asterisk */
816
  /*  43 */  12, /* plus */
817
  /*  44 */  13, /* comma */
818
  /*  45 */  14, /* hyphen */
819
  /*  46 */  15, /* period */
820
  /*  47 */  16, /* slash */
821
  /*  48 */  17, /* zero */
822
  /*  49 */  18, /* one */
823
  /*  50 */  19, /* two */
824
  /*  51 */  20, /* three */
825
  /*  52 */  21, /* four */
826
  /*  53 */  22, /* five */
827
  /*  54 */  23, /* six */
828
  /*  55 */  24, /* seven */
829
  /*  56 */  25, /* eight */
830
  /*  57 */  26, /* nine */
831
  /*  58 */  27, /* colon */
832
  /*  59 */  28, /* semicolon */
833
  /*  60 */  29, /* less */
834
  /*  61 */  30, /* equal */
835
  /*  62 */  31, /* greater */
836
  /*  63 */  32, /* question */
837
  /*  64 */  33, /* at */
838
  /*  65 */  34, /* A */
839
  /*  66 */  35, /* B */
840
  /*  67 */  36, /* C */
841
  /*  68 */  37, /* D */
842
  /*  69 */  38, /* E */
843
  /*  70 */  39, /* F */
844
  /*  71 */  40, /* G */
845
  /*  72 */  41, /* H */
846
  /*  73 */  42, /* I */
847
  /*  74 */  43, /* J */
848
  /*  75 */  44, /* K */
849
  /*  76 */  45, /* L */
850
  /*  77 */  46, /* M */
851
  /*  78 */  47, /* N */
852
  /*  79 */  48, /* O */
853
  /*  80 */  49, /* P */
854
  /*  81 */  50, /* Q */
855
  /*  82 */  51, /* R */
856
  /*  83 */  52, /* S */
857
  /*  84 */  53, /* T */
858
  /*  85 */  54, /* U */
859
  /*  86 */  55, /* V */
860
  /*  87 */  56, /* W */
861
  /*  88 */  57, /* X */
862
  /*  89 */  58, /* Y */
863
  /*  90 */  59, /* Z */
864
  /*  91 */  60, /* bracketleft */
865
  /*  92 */  61, /* backslash */
866
  /*  93 */  62, /* bracketright */
867
  /*  94 */  63, /* asciicircum */
868
  /*  95 */  64, /* underscore */
869
  /*  96 */  65, /* quoteleft */
870
  /*  97 */  66, /* a */
871
  /*  98 */  67, /* b */
872
  /*  99 */  68, /* c */
873
  /* 100 */  69, /* d */
874
  /* 101 */  70, /* e */
875
  /* 102 */  71, /* f */
876
  /* 103 */  72, /* g */
877
  /* 104 */  73, /* h */
878
  /* 105 */  74, /* i */
879
  /* 106 */  75, /* j */
880
  /* 107 */  76, /* k */
881
  /* 108 */  77, /* l */
882
  /* 109 */  78, /* m */
883
  /* 110 */  79, /* n */
884
  /* 111 */  80, /* o */
885
  /* 112 */  81, /* p */
886
  /* 113 */  82, /* q */
887
  /* 114 */  83, /* r */
888
  /* 115 */  84, /* s */
889
  /* 116 */  85, /* t */
890
  /* 117 */  86, /* u */
891
  /* 118 */  87, /* v */
892
  /* 119 */  88, /* w */
893
  /* 120 */  89, /* x */
894
  /* 121 */  90, /* y */
895
  /* 122 */  91, /* z */
896
  /* 123 */  92, /* braceleft */
897
  /* 124 */  93, /* bar */
898
  /* 125 */  94, /* braceright */
899
  /* 126 */  95, /* asciitilde */
900
  /* 127 */   0, /* .notdef */
901
  /* 128 */   0, /* .notdef */
902
  /* 129 */   0, /* .notdef */
903
  /* 130 */   0, /* .notdef */
904
  /* 131 */   0, /* .notdef */
905
  /* 132 */   0, /* .notdef */
906
  /* 133 */   0, /* .notdef */
907
  /* 134 */   0, /* .notdef */
908
  /* 135 */   0, /* .notdef */
909
  /* 136 */   0, /* .notdef */
910
  /* 137 */   0, /* .notdef */
911
  /* 138 */   0, /* .notdef */
912
  /* 139 */   0, /* .notdef */
913
  /* 140 */   0, /* .notdef */
914
  /* 141 */   0, /* .notdef */
915
  /* 142 */   0, /* .notdef */
916
  /* 143 */   0, /* .notdef */
917
  /* 144 */   0, /* .notdef */
918
  /* 145 */   0, /* .notdef */
919
  /* 146 */   0, /* .notdef */
920
  /* 147 */   0, /* .notdef */
921
  /* 148 */   0, /* .notdef */
922
  /* 149 */   0, /* .notdef */
923
  /* 150 */   0, /* .notdef */
924
  /* 151 */   0, /* .notdef */
925
  /* 152 */   0, /* .notdef */
926
  /* 153 */   0, /* .notdef */
927
  /* 154 */   0, /* .notdef */
928
  /* 155 */   0, /* .notdef */
929
  /* 156 */   0, /* .notdef */
930
  /* 157 */   0, /* .notdef */
931
  /* 158 */   0, /* .notdef */
932
  /* 159 */   0, /* .notdef */
933
  /* 160 */   0, /* .notdef */
934
  /* 161 */  96, /* exclamdown */
935
  /* 162 */  97, /* cent */
936
  /* 163 */  98, /* sterling */
937
  /* 164 */  99, /* fraction */
938
  /* 165 */ 100, /* yen */
939
  /* 166 */ 101, /* florin */
940
  /* 167 */ 102, /* section */
941
  /* 168 */ 103, /* currency */
942
  /* 169 */ 104, /* quotesingle */
943
  /* 170 */ 105, /* quotedblleft */
944
  /* 171 */ 106, /* guillemotleft */
945
  /* 172 */ 107, /* guilsinglleft */
946
  /* 173 */ 108, /* guilsinglright */
947
  /* 174 */ 109, /* fi */
948
  /* 175 */ 110, /* fl */
949
  /* 176 */   0, /* .notdef */
950
  /* 177 */ 111, /* endash */
951
  /* 178 */ 112, /* dagger */
952
  /* 179 */ 113, /* daggerdbl */
953
  /* 180 */ 114, /* periodcentered */
954
  /* 181 */   0, /* .notdef */
955
  /* 182 */ 115, /* paragraph */
956
  /* 183 */ 116, /* bullet */
957
  /* 184 */ 117, /* quotesinglbase */
958
  /* 185 */ 118, /* quotedblbase */
959
  /* 186 */ 119, /* quotedblright */
960
  /* 187 */ 120, /* guillemotright */
961
  /* 188 */ 121, /* ellipsis */
962
  /* 189 */ 122, /* perthousand */
963
  /* 190 */   0, /* .notdef */
964
  /* 191 */ 123, /* questiondown */
965
  /* 192 */   0, /* .notdef */
966
  /* 193 */ 124, /* grave */
967
  /* 194 */ 125, /* acute */
968
  /* 195 */ 126, /* circumflex */
969
  /* 196 */ 127, /* tilde */
970
  /* 197 */ 128, /* macron */
971
  /* 198 */ 129, /* breve */
972
  /* 199 */ 130, /* dotaccent */
973
  /* 200 */ 131, /* dieresis */
974
  /* 201 */   0, /* .notdef */
975
  /* 202 */ 132, /* ring */
976
  /* 203 */ 133, /* cedilla */
977
  /* 204 */   0, /* .notdef */
978
  /* 205 */ 134, /* hungarumlaut */
979
  /* 206 */ 135, /* ogonek */
980
  /* 207 */ 136, /* caron */
981
  /* 208 */ 137, /* emdash */
982
  /* 209 */   0, /* .notdef */
983
  /* 210 */   0, /* .notdef */
984
  /* 211 */   0, /* .notdef */
985
  /* 212 */   0, /* .notdef */
986
  /* 213 */   0, /* .notdef */
987
  /* 214 */   0, /* .notdef */
988
  /* 215 */   0, /* .notdef */
989
  /* 216 */   0, /* .notdef */
990
  /* 217 */   0, /* .notdef */
991
  /* 218 */   0, /* .notdef */
992
  /* 219 */   0, /* .notdef */
993
  /* 220 */   0, /* .notdef */
994
  /* 221 */   0, /* .notdef */
995
  /* 222 */   0, /* .notdef */
996
  /* 223 */   0, /* .notdef */
997
  /* 224 */   0, /* .notdef */
998
  /* 225 */ 138, /* AE */
999
  /* 226 */   0, /* .notdef */
1000
  /* 227 */ 139, /* ordfeminine */
1001
  /* 228 */   0, /* .notdef */
1002
  /* 229 */   0, /* .notdef */
1003
  /* 230 */   0, /* .notdef */
1004
  /* 231 */   0, /* .notdef */
1005
  /* 232 */ 140, /* Lslash */
1006
  /* 233 */ 141, /* Oslash */
1007
  /* 234 */ 142, /* OE */
1008
  /* 235 */ 143, /* ordmasculine */
1009
  /* 236 */   0, /* .notdef */
1010
  /* 237 */   0, /* .notdef */
1011
  /* 238 */   0, /* .notdef */
1012
  /* 239 */   0, /* .notdef */
1013
  /* 240 */   0, /* .notdef */
1014
  /* 241 */ 144, /* ae */
1015
  /* 242 */   0, /* .notdef */
1016
  /* 243 */   0, /* .notdef */
1017
  /* 244 */   0, /* .notdef */
1018
  /* 245 */ 145, /* dotlessi */
1019
  /* 246 */   0, /* .notdef */
1020
  /* 247 */   0, /* .notdef */
1021
  /* 248 */ 146, /* lslash */
1022
  /* 249 */ 147, /* oslash */
1023
  /* 250 */ 148, /* oe */
1024
  /* 251 */ 149, /* germandbls */
1025
  /* 252 */   0, /* .notdef */
1026
  /* 253 */   0, /* .notdef */
1027
  /* 254 */   0, /* .notdef */
1028
  /* 255 */   0  /* .notdef */
1029
};
1030
1031
static const unsigned short expert_encoding[] = {
1032
  /*   0 */   0, /* .notdef */
1033
  /*   1 */   0, /* .notdef */
1034
  /*   2 */   0, /* .notdef */
1035
  /*   3 */   0, /* .notdef */
1036
  /*   4 */   0, /* .notdef */
1037
  /*   5 */   0, /* .notdef */
1038
  /*   6 */   0, /* .notdef */
1039
  /*   7 */   0, /* .notdef */
1040
  /*   8 */   0, /* .notdef */
1041
  /*   9 */   0, /* .notdef */
1042
  /*  10 */   0, /* .notdef */
1043
  /*  11 */   0, /* .notdef */
1044
  /*  12 */   0, /* .notdef */
1045
  /*  13 */   0, /* .notdef */
1046
  /*  14 */   0, /* .notdef */
1047
  /*  15 */   0, /* .notdef */
1048
  /*  16 */   0, /* .notdef */
1049
  /*  17 */   0, /* .notdef */
1050
  /*  18 */   0, /* .notdef */
1051
  /*  19 */   0, /* .notdef */
1052
  /*  20 */   0, /* .notdef */
1053
  /*  21 */   0, /* .notdef */
1054
  /*  22 */   0, /* .notdef */
1055
  /*  23 */   0, /* .notdef */
1056
  /*  24 */   0, /* .notdef */
1057
  /*  25 */   0, /* .notdef */
1058
  /*  26 */   0, /* .notdef */
1059
  /*  27 */   0, /* .notdef */
1060
  /*  28 */   0, /* .notdef */
1061
  /*  29 */   0, /* .notdef */
1062
  /*  30 */   0, /* .notdef */
1063
  /*  31 */   0, /* .notdef */
1064
  /*  32 */   1, /* space */
1065
  /*  33 */ 229, /* exclamsmall */
1066
  /*  34 */ 230, /* Hungarumlautsmall */
1067
  /*  35 */   0, /* .notdef */
1068
  /*  36 */ 231, /* dollaroldstyle */
1069
  /*  37 */ 232, /* dollarsuperior */
1070
  /*  38 */ 233, /* ampersandsmall */
1071
  /*  39 */ 234, /* Acutesmall */
1072
  /*  40 */ 235, /* parenleftsuperior */
1073
  /*  41 */ 236, /* parenrightsuperior */
1074
  /*  42 */ 237, /* twodotenleader */
1075
  /*  43 */ 238, /* onedotenleader */
1076
  /*  44 */  13, /* comma */
1077
  /*  45 */  14, /* hyphen */
1078
  /*  46 */  15, /* period */
1079
  /*  47 */  99, /* fraction */
1080
  /*  48 */ 239, /* zerooldstyle */
1081
  /*  49 */ 240, /* oneoldstyle */
1082
  /*  50 */ 241, /* twooldstyle */
1083
  /*  51 */ 242, /* threeoldstyle */
1084
  /*  52 */ 243, /* fouroldstyle */
1085
  /*  53 */ 244, /* fiveoldstyle */
1086
  /*  54 */ 245, /* sixoldstyle */
1087
  /*  55 */ 246, /* sevenoldstyle */
1088
  /*  56 */ 247, /* eightoldstyle */
1089
  /*  57 */ 248, /* nineoldstyle */
1090
  /*  58 */  27, /* colon */
1091
  /*  59 */ 28, /* semicolon */
1092
  /*  60 */ 249, /* commasuperior */
1093
  /*  61 */ 250, /* threequartersemdash */
1094
  /*  62 */ 251, /* periodsuperior */
1095
  /*  63 */ 252, /* questionsmall */
1096
  /*  64 */   0, /* .notdef */
1097
  /*  65 */ 253, /* asuperior */
1098
  /*  66 */ 254, /* bsuperior */
1099
  /*  67 */ 255, /* centsuperior */
1100
  /*  68 */ 256, /* dsuperior */
1101
  /*  69 */ 257, /* esuperior */
1102
  /*  70 */   0, /* .notdef */
1103
  /*  71 */   0, /* .notdef */
1104
  /*  72 */   0, /* .notdef */
1105
  /*  73 */ 258, /* isuperior */
1106
  /*  74 */   0, /* .notdef */
1107
  /*  75 */   0, /* .notdef */
1108
  /*  76 */ 259, /* lsuperior */
1109
  /*  77 */ 260, /* msuperior */
1110
  /*  78 */ 261, /* nsuperior */
1111
  /*  79 */ 262, /* osuperior */
1112
  /*  80 */   0, /* .notdef */
1113
  /*  81 */   0, /* .notdef */
1114
  /*  82 */ 263, /* rsuperior */
1115
  /*  83 */ 264, /* ssuperior */
1116
  /*  84 */ 265, /* tsuperior */
1117
  /*  85 */   0, /* .notdef */
1118
  /*  86 */ 266, /* ff */
1119
  /*  87 */ 109, /* fi */
1120
  /*  88 */ 110, /* fl */
1121
  /*  89 */ 267, /* ffi */
1122
  /*  90 */ 268, /* ffl */
1123
  /*  91 */ 269, /* parenleftinferior */
1124
  /*  92 */   0, /* .notdef */
1125
  /*  93 */ 270, /* parenrightinferior */
1126
  /*  94 */ 271, /* Circumflexsmall */
1127
  /*  95 */ 272, /* hyphensuperior */
1128
  /*  96 */ 273, /* Gravesmall */
1129
  /*  97 */ 274, /* Asmall */
1130
  /*  98 */ 275, /* Bsmall */
1131
  /*  99 */ 276, /* Csmall */
1132
  /* 100 */ 277, /* Dsmall */
1133
  /* 101 */ 278, /* Esmall */
1134
  /* 102 */ 279, /* Fsmall */
1135
  /* 103 */ 280, /* Gsmall */
1136
  /* 104 */ 281, /* Hsmall */
1137
  /* 105 */ 282, /* Ismall */
1138
  /* 106 */ 283, /* Jsmall */
1139
  /* 107 */ 284, /* Ksmall */
1140
  /* 108 */ 285, /* Lsmall */
1141
  /* 109 */ 286, /* Msmall */
1142
  /* 110 */ 287, /* Nsmall */
1143
  /* 111 */ 288, /* Osmall */
1144
  /* 112 */ 289, /* Psmall */
1145
  /* 113 */ 290, /* Qsmall */
1146
  /* 114 */ 291, /* Rsmall */
1147
  /* 115 */ 292, /* Ssmall */
1148
  /* 116 */ 293, /* Tsmall */
1149
  /* 117 */ 294, /* Usmall */
1150
  /* 118 */ 295, /* Vsmall */
1151
  /* 119 */ 296, /* Wsmall */
1152
  /* 120 */ 297, /* Xsmall */
1153
  /* 121 */ 298, /* Ysmall */
1154
  /* 122 */ 299, /* Zsmall */
1155
  /* 123 */ 300, /* colonmonetary */
1156
  /* 124 */ 301, /* onefitted */
1157
  /* 125 */ 302, /* rupiah */
1158
  /* 126 */ 303, /* Tildesmall */
1159
  /* 127 */   0, /* .notdef */
1160
  /* 128 */   0, /* .notdef */
1161
  /* 129 */   0, /* .notdef */
1162
  /* 130 */   0, /* .notdef */
1163
  /* 131 */   0, /* .notdef */
1164
  /* 132 */   0, /* .notdef */
1165
  /* 133 */   0, /* .notdef */
1166
  /* 134 */   0, /* .notdef */
1167
  /* 135 */   0, /* .notdef */
1168
  /* 136 */   0, /* .notdef */
1169
  /* 137 */   0, /* .notdef */
1170
  /* 138 */   0, /* .notdef */
1171
  /* 139 */   0, /* .notdef */
1172
  /* 140 */   0, /* .notdef */
1173
  /* 141 */   0, /* .notdef */
1174
  /* 142 */   0, /* .notdef */
1175
  /* 143 */   0, /* .notdef */
1176
  /* 144 */   0, /* .notdef */
1177
  /* 145 */   0, /* .notdef */
1178
  /* 146 */   0, /* .notdef */
1179
  /* 147 */   0, /* .notdef */
1180
  /* 148 */   0, /* .notdef */
1181
  /* 149 */   0, /* .notdef */
1182
  /* 150 */   0, /* .notdef */
1183
  /* 151 */   0, /* .notdef */
1184
  /* 152 */   0, /* .notdef */
1185
  /* 153 */   0, /* .notdef */
1186
  /* 154 */   0, /* .notdef */
1187
  /* 155 */   0, /* .notdef */
1188
  /* 156 */   0, /* .notdef */
1189
  /* 157 */   0, /* .notdef */
1190
  /* 158 */   0, /* .notdef */
1191
  /* 159 */   0, /* .notdef */
1192
  /* 160 */   0, /* .notdef */
1193
  /* 161 */ 304, /* exclamdownsmall */
1194
  /* 162 */ 305, /* centoldstyle */
1195
  /* 163 */ 306, /* Lslashsmall */
1196
  /* 164 */   0, /* .notdef */
1197
  /* 165 */   0, /* .notdef */
1198
  /* 166 */ 307, /* Scaronsmall */
1199
  /* 167 */ 308, /* Zcaronsmall */
1200
  /* 168 */ 309, /* Dieresissmall */
1201
  /* 169 */ 310, /* Brevesmall */
1202
  /* 170 */ 311, /* Caronsmall */
1203
  /* 171 */   0, /* .notdef */
1204
  /* 172 */ 312, /* Dotaccentsmall */
1205
  /* 173 */   0, /* .notdef */
1206
  /* 174 */   0, /* .notdef */
1207
  /* 175 */ 313, /* Macronsmall */
1208
  /* 176 */   0, /* .notdef */
1209
  /* 177 */   0, /* .notdef */
1210
  /* 178 */ 314, /* figuredash */
1211
  /* 179 */ 315, /* hypheninferior */
1212
  /* 180 */   0, /* .notdef */
1213
  /* 181 */   0, /* .notdef */
1214
  /* 182 */ 316, /* Ogoneksmall */
1215
  /* 183 */ 317, /* Ringsmall */
1216
  /* 184 */ 318, /* Cedillasmall */
1217
  /* 185 */   0, /* .notdef */
1218
  /* 186 */   0, /* .notdef */
1219
  /* 187 */   0, /* .notdef */
1220
  /* 188 */ 158, /* onequarter */
1221
  /* 189 */ 155, /* onehalf */
1222
  /* 190 */ 163, /* threequarters */
1223
  /* 191 */ 319, /* questiondownsmall */
1224
  /* 192 */ 320, /* oneeighth */
1225
  /* 193 */ 321, /* threeeighths */
1226
  /* 194 */ 322, /* fiveeighths */
1227
  /* 195 */ 323, /* seveneighths */
1228
  /* 196 */ 324, /* onethird */
1229
  /* 197 */ 325, /* twothirds */
1230
  /* 198 */   0, /* .notdef */
1231
  /* 199 */   0, /* .notdef */
1232
  /* 200 */ 326, /* zerosuperior */
1233
  /* 201 */ 150, /* onesuperior */
1234
  /* 202 */ 164, /* twosuperior */
1235
  /* 203 */ 169, /* threesuperior */
1236
  /* 204 */ 327, /* foursuperior */
1237
  /* 205 */ 328, /* fivesuperior */
1238
  /* 206 */ 329, /* sixsuperior */
1239
  /* 207 */ 330, /* sevensuperior */
1240
  /* 208 */ 331, /* eightsuperior */
1241
  /* 209 */ 332, /* ninesuperior */
1242
  /* 210 */ 333, /* zeroinferior */
1243
  /* 211 */ 334, /* oneinferior */
1244
  /* 212 */ 335, /* twoinferior */
1245
  /* 213 */ 336, /* threeinferior */
1246
  /* 214 */ 337, /* fourinferior */
1247
  /* 215 */ 338, /* fiveinferior */
1248
  /* 216 */ 339, /* sixinferior */
1249
  /* 217 */ 340, /* seveninferior */
1250
  /* 218 */ 341, /* eightinferior */
1251
  /* 219 */ 342, /* nineinferior */
1252
  /* 220 */ 343, /* centinferior */
1253
  /* 221 */ 344, /* dollarinferior */
1254
  /* 222 */ 345, /* periodinferior */
1255
  /* 223 */ 346, /* commainferior */
1256
  /* 224 */ 347, /* Agravesmall */
1257
  /* 225 */ 348, /* Aacutesmall */
1258
  /* 226 */ 349, /* Acircumflexsmall */
1259
  /* 227 */ 350, /* Atildesmall */
1260
  /* 228 */ 351, /* Adieresissmall */
1261
  /* 229 */ 352, /* Aringsmall */
1262
  /* 230 */ 353, /* AEsmall */
1263
  /* 231 */ 354, /* Ccedillasmall */
1264
  /* 232 */ 355, /* Egravesmall */
1265
  /* 233 */ 356, /* Eacutesmall */
1266
  /* 234 */ 357, /* Ecircumflexsmall */
1267
  /* 235 */ 358, /* Edieresissmall */
1268
  /* 236 */ 359, /* Igravesmall */
1269
  /* 237 */ 360, /* Iacutesmall */
1270
  /* 238 */ 361, /* Icircumflexsmall */
1271
  /* 239 */ 362, /* Idieresissmall */
1272
  /* 240 */ 363, /* Ethsmall */
1273
  /* 241 */ 364, /* Ntildesmall */
1274
  /* 242 */ 365, /* Ogravesmall */
1275
  /* 243 */ 366, /* Oacutesmall */
1276
  /* 244 */ 367, /* Ocircumflexsmall */
1277
  /* 245 */ 368, /* Otildesmall */
1278
  /* 246 */ 369, /* Odieresissmall */
1279
  /* 247 */ 370, /* OEsmall */
1280
  /* 248 */ 371, /* Oslashsmall */
1281
  /* 249 */ 372, /* Ugravesmall */
1282
  /* 250 */ 373, /* Uacutesmall */
1283
  /* 251 */ 374, /* Ucircumflexsmall */
1284
  /* 252 */ 375, /* Udieresissmall */
1285
  /* 253 */ 376, /* Yacutesmall */
1286
  /* 254 */ 377, /* Thornsmall */
1287
  /* 255 */ 378  /* Ydieresissmall */
1288
};
1289
1290
typedef struct tag_cff_data_t {
1291
  ref *blk_ref;
1292
  unsigned int length;
1293
  unsigned int shift;
1294
  unsigned int mask;
1295
} cff_data_t;
1296
1297
typedef struct tag_cff_index_t {
1298
  unsigned int start, end, data;
1299
  unsigned int  offsize, count;
1300
} cff_index_t;
1301
1302
static int
1303
card8(unsigned int *u, const cff_data_t *o, unsigned p, unsigned pe)
1304
0
{
1305
0
    if (pe > o->length || p > pe - 1)
1306
0
        return_error(gs_error_rangecheck); /* out of range access */
1307
0
    *u = o->blk_ref[p >> o->shift].value.bytes[p & o->mask];
1308
0
    return 0;
1309
0
}
1310
1311
static int
1312
card16(unsigned int *u, const cff_data_t *o, unsigned p, unsigned pe)
1313
0
{
1314
0
    if (pe > o->length || p > pe - 2)
1315
0
        return_error(gs_error_rangecheck); /* out of range access */
1316
0
    *u = (o->blk_ref[ p      >> o->shift].value.bytes[ p      & o->mask]) << 8 |
1317
0
          o->blk_ref[(p + 1) >> o->shift].value.bytes[(p + 1) & o->mask];
1318
0
    return 0;
1319
0
}
1320
1321
static int
1322
card24(unsigned int *u, const cff_data_t *o, unsigned p, unsigned pe)
1323
0
{
1324
0
    if (pe > o->length || p > pe - 3)
1325
0
        return_error(gs_error_rangecheck); /* out of range access */
1326
0
    *u = (o->blk_ref[ p      >> o->shift].value.bytes[ p      & o->mask]) << 16 |
1327
0
         (o->blk_ref[(p + 1) >> o->shift].value.bytes[(p + 1) & o->mask]) << 8  |
1328
0
          o->blk_ref[(p + 2) >> o->shift].value.bytes[(p + 2) & o->mask];
1329
0
    return 0;
1330
0
}
1331
static int
1332
card32(unsigned int *u, const cff_data_t *o, unsigned p, unsigned pe)
1333
0
{
1334
0
    if (pe > o->length || p > pe - 4)
1335
0
        return_error(gs_error_rangecheck); /* out of range access */
1336
0
    *u = (o->blk_ref[ p      >> o->shift].value.bytes[ p      & o->mask]) << 24 |
1337
0
         (o->blk_ref[(p + 1) >> o->shift].value.bytes[(p + 1) & o->mask]) << 16 |
1338
0
         (o->blk_ref[(p + 2) >> o->shift].value.bytes[(p + 2) & o->mask]) << 8  |
1339
0
          o->blk_ref[(p + 3) >> o->shift].value.bytes[(p + 3) & o->mask];
1340
0
    return 0;
1341
0
}
1342
1343
static int (* const offset_procs[])(unsigned int *, const cff_data_t *, unsigned, unsigned) = {
1344
    0, card8, card16, card24, card32
1345
};
1346
1347
static int
1348
get_cff_string(unsigned char *dst, const cff_data_t *o, unsigned p, unsigned len)
1349
0
{
1350
0
    if (p + len > o->length)
1351
0
        return_error(gs_error_rangecheck); /* out of range access */
1352
0
    while (len) {
1353
0
        unsigned chunk_len = o->mask + 1 - (p & o->mask);
1354
0
        unsigned char *pos = o->blk_ref[p >> o->shift].value.bytes + (p & o->mask);
1355
0
        if (chunk_len > len)
1356
0
            chunk_len = len;
1357
0
        memcpy(dst, pos, chunk_len);
1358
0
        dst += chunk_len;
1359
0
        len -= chunk_len;
1360
0
        p += chunk_len;
1361
0
    }
1362
0
    return 0;
1363
0
}
1364
1365
static int
1366
parse_index(cff_index_t *x, const cff_data_t *data, unsigned p, unsigned pe)
1367
0
{  int code;
1368
1369
0
   if (p == 0) {
1370
       /* Make an empty index when the offset to the index is not defined. */
1371
0
       memset(x, 0, sizeof(*x));
1372
0
       return 0;
1373
0
   }
1374
0
   x->start = p;
1375
0
   if ((code = card16(&x->count, data, p, pe)) < 0)
1376
0
       return code;
1377
0
   if (x->count) {
1378
0
       unsigned int eod;
1379
1380
0
       if ((code = card8(&x->offsize, data, p + 2, pe)) < 0)
1381
0
           return code;
1382
0
       if (x->offsize == 0) {  /* skip incorrect index, bug 689854 */
1383
0
           x->count = 0;
1384
0
           x->data = 0;
1385
0
           x->end = p + 3;
1386
0
       } else if( x->offsize > 4) {
1387
0
           return_error(gs_error_rangecheck); /* Invalid offest size */
1388
0
       } else {
1389
0
           x->data = p + 2 + x->offsize*(x->count+1);
1390
0
           code = (*offset_procs[x->offsize])(&eod, data, p + 3 + x->offsize*x->count, pe);
1391
0
           if (code < 0)
1392
0
               return code;
1393
0
           x->end = x->data + eod;
1394
0
       }
1395
0
   } else {
1396
0
       x->offsize = 0;
1397
0
       x->data = 0;
1398
0
       x->end = p + 2;
1399
0
   }
1400
0
   return 0;
1401
0
}
1402
1403
static int
1404
peek_index(unsigned *pp, unsigned int *len, const cff_index_t *x, const cff_data_t *data, unsigned int i)
1405
0
{
1406
0
    int code;
1407
0
    unsigned int off1, off2;
1408
1409
0
    if (i >= x->count)
1410
0
        return_error(gs_error_rangecheck); /* wrong index */
1411
0
    if((code = (*offset_procs[x->offsize])(&off1, data, x->start + 3 + x->offsize*i, x->end)) < 0)
1412
0
        return code;
1413
0
    if((code = (*offset_procs[x->offsize])(&off2, data, x->start + 3 + x->offsize*(i + 1), x->end)) < 0)
1414
0
        return code;
1415
0
    if (off2 < off1)
1416
0
        return_error(gs_error_rangecheck); /* Decreasing offsets */
1417
0
    if (x->data + off2 > x->end)
1418
0
        return_error(gs_error_rangecheck); /* Element exceeds index size */
1419
0
    *len = off2 - off1;
1420
0
    *pp  = x->data + off1;
1421
0
    return 0;
1422
0
}
1423
1424
static int
1425
get_int(int *v,  const cff_data_t *data, unsigned p, unsigned pe)
1426
0
{
1427
0
    int code;
1428
0
    unsigned int c, u;
1429
0
    const int ext16 = ~0 << 15;  /* sign extension constant */
1430
0
    const int ext32 = ~0 << 31;  /* sign extension constant */
1431
1432
0
    if ((code = card8(&c, data, p, pe)) < 0)
1433
0
        return code;
1434
1435
0
    if (c == 28) {
1436
0
        if (pe - p > 2) {
1437
0
            if ((code = card16(&u, data, p + 1, pe)) < 0)
1438
0
                return code;
1439
0
        }
1440
0
        else {
1441
0
            if ((code = card8(&u, data, p + 1, pe)) < 0)
1442
0
                return code;
1443
0
        }
1444
0
        *v = ((int)u + ext16) ^ ext16;
1445
0
        return 3;
1446
0
    }
1447
0
    if (c == 29) {
1448
0
        switch (pe - p) {
1449
0
            case 2:
1450
0
              if ((code = card8(&u, data, p + 1, pe)) < 0)
1451
0
                 return code;
1452
0
              break;
1453
0
            case 3:
1454
0
              if ((code = card16(&u, data, p + 1, pe)) < 0)
1455
0
                 return code;
1456
0
              break;
1457
0
            case 4:
1458
0
              if ((code = card24(&u, data, p + 1, pe)) < 0)
1459
0
                 return code;
1460
0
              break;
1461
0
            default:
1462
0
              if ((code = card32(&u, data, p + 1, pe)) < 0)
1463
0
                 return code;
1464
0
        }
1465
0
        *v = ((int)u + ext32) ^ ext32;
1466
0
        return 5;
1467
0
    }
1468
0
    if (c < 32)
1469
0
        return_error(gs_error_rangecheck); /* out of range */
1470
0
    if (c < 247) {
1471
0
        *v = ((int)c - 139);
1472
0
        return 1;
1473
0
    }
1474
0
    if (c < 251) {
1475
0
        if ((code = card8(&u, data, p + 1, pe)) < 0)
1476
0
            return code;
1477
0
        *v = ((int)c - 247)*256 + (int)u + 108;
1478
0
        return 2;
1479
0
    }
1480
0
    if (c < 255) {
1481
0
        if ((code = card8(&u, data, p + 1, pe)) < 0)
1482
0
            return code;
1483
0
        *v = -((int)c - 251)*256 - (int)u - 108;
1484
0
        return 2;
1485
0
    }
1486
0
    return_error(gs_error_rangecheck); /* out of range */
1487
0
}
1488
1489
static int
1490
get_float(ref *fnum, const cff_data_t *data, unsigned p, unsigned pe)
1491
0
{
1492
0
    int code;
1493
0
    unsigned int c, i;
1494
0
    char buf[80];
1495
0
    const unsigned p0 = p;
1496
0
    char *q = buf;
1497
1498
0
    for(;;) {
1499
0
        unsigned int u;
1500
1501
0
        if ((code = card8(&u, data, p, pe)) < 0)
1502
0
            return code;
1503
0
        for (i=0; i<2 && q < buf + sizeof(buf) - 1; i++) {
1504
0
            c = i ? u & 15 : u >> 4 ;
1505
0
            switch(c) {
1506
0
               case 0: case 1: case 2: case 3: case 4: /* 0 .. 9 */
1507
0
               case 5: case 6: case 7: case 8: case 9:
1508
0
                   *q++ = '0' + c;
1509
0
                   break;
1510
0
               case 10: /* . */
1511
0
                   *q++ = '.';
1512
0
                   break;
1513
0
               case 11: /* E */
1514
0
                   *q++ = 'e';
1515
0
                   break;
1516
0
               case 12: /* E- */
1517
0
                   *q++ = 'e';
1518
0
                   *q++ = '-';
1519
0
                   break;
1520
0
               case 13: /* invalid, skip */
1521
0
                   break;
1522
0
               case 14: /* - */
1523
0
                   *q++ = '-';
1524
0
                   break;
1525
0
               case 15: { /* end */
1526
0
                   int sign = 0;
1527
0
                   char *eptr, *bptr = buf;
1528
1529
0
                   if (q > buf && buf[0] == '-'){
1530
0
                       sign = -1;
1531
0
                       bptr = &(buf[1]);
1532
0
                   }
1533
1534
0
                   if (q > buf) {
1535
0
                       code = scan_number ((const byte *)bptr, (const byte *)q, sign, fnum, (const byte **)&eptr, 0);
1536
0
                   }
1537
0
                   else {
1538
0
                       code = 0;
1539
0
                       make_int(fnum, 0);
1540
0
                   }
1541
0
                   if (code < 0) {
1542
0
                       return(code);
1543
0
                   }
1544
0
                   else {
1545
0
                       return p - p0 + 1;
1546
0
                   }
1547
0
                   break;
1548
0
                }
1549
0
            }
1550
0
        }
1551
0
        p++;
1552
0
    }
1553
0
}
1554
1555
static int
1556
idict_put_c_name(i_ctx_t *i_ctx_p, ref *dst, const char *c_name, unsigned int len, const ref *src)
1557
0
{
1558
0
    int code;
1559
0
    ref ps_name;
1560
1561
0
    if ((code = name_ref(imemory, (const unsigned char *)c_name, len, &ps_name, 0)) < 0)
1562
0
      return code;
1563
0
    return idict_put(dst, &ps_name, src);
1564
0
}
1565
1566
static int
1567
idict_undef_c_name(i_ctx_t *i_ctx_p, ref *dst, const char *c_name, unsigned int len)
1568
0
{
1569
0
    int code;
1570
0
    ref ps_name;
1571
1572
0
    if ((code = name_ref(imemory, (const unsigned char *)c_name, len, &ps_name, 0)) < 0)
1573
0
      return code;
1574
0
    code = idict_undef(dst, &ps_name);
1575
0
    if (code < 0 && code != gs_error_undefined) /* ignore undefined error */
1576
0
        return code;
1577
0
    return 0;
1578
0
}
1579
1580
static int
1581
idict_move_c_name(i_ctx_t *i_ctx_p, ref *dst, ref *src, const char *c_name, unsigned int len)
1582
0
{
1583
0
    int code;
1584
0
    ref ps_name, *pvalue;
1585
1586
0
    if ((code = name_ref(imemory, (const unsigned char *)c_name, len, &ps_name, 0)) < 0)
1587
0
      return code;
1588
0
    if (dict_find(src, &ps_name, &pvalue) > 0) {
1589
0
        if ((code = idict_put(dst, &ps_name, pvalue)) < 0)
1590
0
            return code;
1591
0
        if ((code = idict_undef(src, &ps_name)) < 0)
1592
0
            return code;
1593
0
    }
1594
0
    return 0;
1595
0
}
1596
1597
static int
1598
make_string_from_index(i_ctx_t *i_ctx_p, ref *dst, const cff_index_t *index, const cff_data_t *data, unsigned int id, int fd_num)
1599
0
{
1600
0
    int code;
1601
0
    unsigned int len, doff;
1602
0
    byte *sbody;
1603
0
    int fdoff = fd_num >= 0;
1604
1605
0
    if ((code = peek_index(&doff, &len, index, data, id)) < 0)
1606
0
        return code;
1607
0
    if (len + fdoff> 65535)
1608
0
        return_error(gs_error_limitcheck);
1609
0
    if ((sbody = ialloc_string(len + fdoff, "make_string_from_index")) == 0)
1610
0
        return_error(gs_error_VMerror);
1611
0
    make_string(dst, icurrent_space | a_readonly, len + fdoff, sbody);
1612
0
    if ((code = get_cff_string(sbody + fdoff, data, doff, len)) < 0)
1613
0
        return code;
1614
0
    if (fdoff)
1615
0
        sbody[0] = fd_num;
1616
0
    return 0;
1617
0
}
1618
1619
static int
1620
make_string_from_sid(i_ctx_t *i_ctx_p, ref *dst, const cff_index_t *strings, const cff_data_t *data, unsigned int sid)
1621
0
{
1622
0
    if (sid < count_of(standard_strings)) {
1623
0
        make_string(dst, avm_foreign | a_readonly,
1624
0
          strlen(standard_strings[sid]), (unsigned char *)standard_strings[sid]);  /* break const */
1625
0
        return 0;
1626
0
    } else
1627
0
        return make_string_from_index(i_ctx_p, dst, strings, data, sid - count_of(standard_strings), -1);
1628
0
}
1629
1630
static int
1631
make_name_from_sid(i_ctx_t *i_ctx_p, ref *dst, const cff_index_t *strings, const cff_data_t *data, unsigned int sid)
1632
0
{
1633
0
    if (sid < count_of(standard_strings)) {
1634
0
        return name_ref(imemory, (const unsigned char *)standard_strings[sid],
1635
0
                                         strlen(standard_strings[sid]), dst, 0);
1636
0
    } else {
1637
0
        int code;
1638
0
        unsigned int off, len;
1639
0
        byte buf[200];
1640
1641
0
        if ((code = peek_index(&off, &len, strings, data, sid - count_of(standard_strings))) < 0)
1642
0
            return code;
1643
0
        if (len > sizeof(buf))
1644
0
            return_error(gs_error_limitcheck);
1645
0
        if ((code = get_cff_string(buf, data, off, len)) < 0)
1646
0
            return code;
1647
0
        return name_ref(imemory, buf, len, dst, 1);
1648
0
    }
1649
0
}
1650
1651
static int
1652
make_stringarray_from_index(i_ctx_t *i_ctx_p, ref *dst, const cff_index_t *index, const cff_data_t *data)
1653
0
{
1654
0
    int code;
1655
0
    unsigned int i;
1656
1657
0
    if ((code = ialloc_ref_array(dst, a_readonly, index->count, "make_stringarray_from_index")) < 0)
1658
0
        return code;
1659
0
    for (i = 0; i < index->count; i++) {
1660
0
        unsigned int subr, len;
1661
1662
0
        if ((code = peek_index(&subr, &len, index, data, i)) < 0)
1663
0
            return code;
1664
0
        if ((code = make_string_from_index(i_ctx_p, dst->value.refs + i, index, data, i, -1)) < 0)
1665
0
            return code;
1666
0
    }
1667
0
    return 0;
1668
0
}
1669
1670
static void
1671
undelta(ref *ops, unsigned int cnt)
1672
0
{
1673
0
    unsigned int i;
1674
1675
0
    for (i = 0; i < cnt; i++) {
1676
0
        if (!r_has_type(&ops[i], t_real)) {
1677
            /* Strictly speaking assigning one element of union
1678
             * to another, overlapping element of a different size is
1679
             * undefined behavior, hence assign to an intermediate variable
1680
             */
1681
0
            float fl = (float)ops[i].value.intval;
1682
0
            make_real(&ops[i], fl);
1683
0
        }
1684
0
    }
1685
0
    for (i = 1; i < cnt; i++) {
1686
0
        make_real(&ops[i], ops[i].value.realval + ops[i - 1].value.realval);
1687
0
    }
1688
0
}
1689
1690
static int
1691
iso_adobe_charset_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned i)
1692
0
{
1693
0
    if (i < 228)
1694
0
        return i + 1;
1695
0
    else
1696
0
        return_error(gs_error_rangecheck);
1697
0
}
1698
1699
static int
1700
expert_charset_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned i)
1701
0
{
1702
0
    if (i < sizeof(expert_charset)/sizeof(*expert_charset))
1703
0
        return expert_charset[i];
1704
0
    else
1705
0
        return_error(gs_error_rangecheck);
1706
0
}
1707
1708
static int
1709
expert_subset_charset_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned int i)
1710
0
{
1711
0
    if (i < sizeof(expert_subset_charset)/sizeof(*expert_subset_charset))
1712
0
        return expert_subset_charset[i];
1713
0
    else
1714
0
        return_error(gs_error_rangecheck);
1715
0
}
1716
1717
static int
1718
format0_charset_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned int i)
1719
0
{
1720
0
    int code;
1721
0
    unsigned u;
1722
1723
0
    if ((code = card16(&u, data, p + 2*i, pe)) < 0)
1724
0
        return code;
1725
0
    return (int)u;
1726
0
}
1727
1728
static int
1729
format1_charset_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned int i)
1730
0
{
1731
0
    int code;
1732
0
    unsigned int cid = 0;
1733
1734
0
    while( p < pe - 3) {
1735
0
        unsigned int first, count;
1736
1737
0
        if ((code = card16(&first, data, p, pe)) < 0)
1738
0
            return code;
1739
0
        if ((code = card8(&count, data, p + 2, pe)) < 0)
1740
0
            return code;
1741
0
        ++count;
1742
0
        if (i < cid + count)
1743
0
            return first + i - cid;
1744
0
        p += 3;
1745
0
        cid += count;
1746
0
    }
1747
0
    return_error(gs_error_rangecheck);
1748
0
}
1749
1750
static int
1751
format2_charset_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned int i)
1752
0
{
1753
0
    int code;
1754
0
    unsigned int cid = 0;
1755
1756
0
    while( p < pe - 4) {
1757
0
        unsigned int first, count;
1758
1759
0
        if ((code = card16(&first, data, p, pe)) < 0)
1760
0
            return code;
1761
0
        if ((code = card16(&count, data, p + 2, pe)) < 0)
1762
0
            return code;
1763
0
        ++count;
1764
1765
0
        if (i < cid + count)
1766
0
            return first + i - cid;
1767
0
        p += 4;
1768
0
        cid += count;
1769
0
    }
1770
0
    return_error(gs_error_rangecheck);
1771
0
}
1772
1773
static int
1774
format0_fdselect_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned int i)
1775
0
{
1776
0
    int code;
1777
0
    unsigned u;
1778
1779
0
    if ((code = card8(&u, data, p + i, pe)) < 0)
1780
0
        return code;
1781
0
    return (int)u;
1782
0
}
1783
1784
static int
1785
format3_fdselect_proc(const cff_data_t *data, unsigned p, unsigned pe, unsigned int i)
1786
0
{
1787
0
    int code;
1788
0
    unsigned int u, n_ranges;
1789
1790
0
    if ((code = card16(&n_ranges, data, p, pe)) < 0)
1791
0
        return code;
1792
0
    p += 2;
1793
1794
0
    while (n_ranges-- &&  p + 5 <= pe) {
1795
0
        unsigned int first, last;
1796
1797
0
        if ((code = card16(&first, data, p, pe)) < 0)
1798
0
            return code;
1799
0
        if ((code = card16(&last, data, p + 3, pe)) < 0)
1800
0
            return code;
1801
0
        if (i >= first && i < last) {
1802
0
            if ((code = card8(&u, data, p + 2, pe)) < 0)
1803
0
                return code;
1804
0
            return (int)u;
1805
0
        }
1806
0
        p += 3;
1807
0
    }
1808
0
    return_error(gs_error_rangecheck);
1809
0
}
1810
1811
static int
1812
find_font_dict(i_ctx_t *i_ctx_p, ref *topdict, ref **fontdict, const char *key)
1813
0
{
1814
0
    int code;
1815
0
    ref val;
1816
1817
0
    if (*fontdict)
1818
0
        return 0;
1819
0
    if (dict_find_string(topdict, key, fontdict) > 0)
1820
0
        return 0;
1821
0
    if ((code = dict_create(8,  &val)) < 0)
1822
0
        return code;
1823
0
    if ((code = idict_put_c_name(i_ctx_p, topdict, key, strlen(key), &val)) < 0)
1824
0
        return code;
1825
0
    if ((code = dict_find_string(topdict, key, fontdict)) == 0)
1826
0
        return_error(gs_error_undefined); /* can't happen */
1827
0
    return code;
1828
0
}
1829
1830
#define DEFINE_KEYS(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z, \
1831
                    A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V) \
1832
static const char * const font_keys[] = { \
1833
  #a,#b,#c,#d,#e,#f,#g,#h,#i,#j,#k,#l,#m,#n,#o,#p,#q,#r,#s,#t,#u,#v,#w,#x,#y,#z, \
1834
  #A,#B,#C,#D,#E,#F,#G,#H,#I,#J,#K,#L,#M,#N,#O,#P,#Q,#R,#S,#T,#U,#V \
1835
}; \
1836
static const short font_keys_sz[] = { \
1837
  sizeof(#a)-1, sizeof(#b)-1, sizeof(#c)-1, sizeof(#d)-1, sizeof(#e)-1, \
1838
  sizeof(#f)-1, sizeof(#g)-1, sizeof(#h)-1, sizeof(#i)-1, sizeof(#j)-1, \
1839
  sizeof(#k)-1, sizeof(#l)-1, sizeof(#m)-1, sizeof(#n)-1, sizeof(#o)-1, \
1840
  sizeof(#p)-1, sizeof(#q)-1, sizeof(#r)-1, sizeof(#s)-1, sizeof(#t)-1, \
1841
  sizeof(#u)-1, sizeof(#v)-1, sizeof(#w)-1, sizeof(#x)-1, sizeof(#y)-1, \
1842
  sizeof(#z)-1, sizeof(#A)-1, sizeof(#B)-1, sizeof(#C)-1, sizeof(#D)-1, \
1843
  sizeof(#E)-1, sizeof(#F)-1, sizeof(#G)-1, sizeof(#H)-1, sizeof(#I)-1, \
1844
  sizeof(#J)-1, sizeof(#K)-1, sizeof(#L)-1, sizeof(#M)-1, sizeof(#N)-1, \
1845
  sizeof(#O)-1, sizeof(#P)-1, sizeof(#Q)-1, sizeof(#R)-1, sizeof(#S)-1, \
1846
  sizeof(#T)-1, sizeof(#U)-1, sizeof(#V)-1 \
1847
}; \
1848
typedef enum { \
1849
  k_##a = 0, k_##b, k_##c, k_##d, k_##e, k_##f, k_##g, k_##h, k_##i, k_##j, k_##k, k_##l, k_##m, \
1850
  k_##n,     k_##o, k_##p, k_##q, k_##r, k_##s, k_##t, k_##u, k_##v, k_##w, k_##x, k_##y, k_##z, \
1851
  k_##A,     k_##B, k_##C, k_##D, k_##E, k_##F, k_##G, k_##H, k_##I, k_##J, k_##K, k_##L, k_##M, \
1852
  k_##N,     k_##O, k_##P, k_##Q, k_##R, k_##S, k_##T, k_##U, k_##V\
1853
} font_keys_dummy_t;
1854
1855
DEFINE_KEYS(
1856
   version,
1857
   Notice,
1858
   FullName,
1859
   FamilyName,
1860
   Weight,
1861
   FontBBox,
1862
   BlueValues,
1863
   OtherBlues,
1864
   FamilyBlues,
1865
   FamilyOtherBlues,
1866
   StdHW,
1867
   StdVW,
1868
   Copyright,
1869
   isFixedPitch,
1870
   ItalicAngle,
1871
   UnderlinePosition,
1872
   UnderlineThickness,
1873
   PaintType,
1874
   CharstringType,
1875
   FontMatrix,
1876
   StrokeWidth,
1877
   BlueScale,
1878
   BlueShift,
1879
   BlueFuzz,
1880
   StemSnapH,
1881
   StemSnapV,
1882
   ForceBold,
1883
   LanguageGroup,
1884
   ExpansionFactor,
1885
   initialRandomSeed,
1886
   SyntheticBase,
1887
   PostScript,
1888
   BaseFontName,
1889
   BaseFontBlend,
1890
   CIDFontVersion,
1891
   CIDFontRevision,
1892
   CIDFontType,
1893
   CIDCount,
1894
   UIDBase,
1895
   FDArray,
1896
   FDSelect,
1897
   FontName,
1898
   UniqueID,
1899
   XUID,
1900
   defaultWidthX,
1901
   nominalWidthX,
1902
   FontType,
1903
   Private
1904
)
1905
#undef DEFINE_KEYS
1906
1907
typedef enum {
1908
    k_topdict = 0, k_fontinfodict = 0x10000, k_privatedict = 0x20000, k_sid = 0x40000,
1909
    k_array = 0x80000, k_delta = 0x100000, k_int = 0x200000, k_bool=0x400000
1910
} font_flags_dummy_t;
1911
1912
0
#define CONTROL(id, n_op, flags) ((flags) | (n_op) | ((k_##id) << 8))
1913
1914
typedef enum {
1915
  k_0, k_1, k_2, k_7, k_50, k_neg_100, k_8720, k_0_039625, k_0_06, k_false, k_box,
1916
  k_matrix, k_matrix_1, k_emptydict
1917
} font_defaults_dummy_t;
1918
1919
typedef struct tag_font_defaults {
1920
    unsigned short key;
1921
    unsigned short value;
1922
} font_defaults_t;
1923
1924
static const font_defaults_t fontinfo_font_defaults[] = {
1925
    { k_isFixedPitch,       k_false },
1926
    { k_ItalicAngle,        k_0 },
1927
    { k_UnderlinePosition,  k_neg_100 },
1928
    { k_UnderlineThickness, k_50 }
1929
};
1930
1931
static const font_defaults_t simple_font_defaults[] = {
1932
    { k_PaintType,          k_0 },
1933
    { k_CharstringType,     k_2 },
1934
    { k_FontMatrix,         k_matrix }, /*  0.001 0 0 0.001 0 0 */
1935
    { k_FontBBox,           k_box },    /*  0 0 0 0 */
1936
    { k_StrokeWidth,        k_0 }
1937
};
1938
1939
static const font_defaults_t cid_font_defaults[] = {
1940
    { k_CIDFontVersion,     k_0 },
1941
    { k_CIDFontRevision,    k_0 },
1942
    { k_CIDFontType,        k_0 },
1943
    { k_CIDCount,           k_8720 }
1944
};
1945
1946
static const font_defaults_t fd_font_defaults[] = {
1947
    { k_FontMatrix,         k_matrix },  /* 0.001 0 0 0.001 0 0 */
1948
    { k_PaintType,          k_0      },  /* gs needs this */
1949
    { k_FontType,           k_2      },  /* gs needs this */
1950
    { k_Private,            k_emptydict} /* following gs implementation */
1951
};
1952
1953
static const font_defaults_t set_unit_matrix[] = {
1954
    { k_FontMatrix,         k_matrix_1 }   /* 1 0 0 1 0 0 */
1955
};
1956
1957
static const font_defaults_t private_font_defaults[] = {
1958
    { k_BlueScale,          k_0_039625 },
1959
    { k_BlueShift,          k_7 },
1960
    { k_BlueFuzz,           k_1 },
1961
    { k_ForceBold,          k_false },
1962
    { k_LanguageGroup,      k_0 },
1963
    { k_ExpansionFactor,    k_0_06 },
1964
    { k_initialRandomSeed,  k_0 },
1965
    { k_defaultWidthX,      k_0 },
1966
    { k_nominalWidthX,      k_0 }
1967
};
1968
1969
static int
1970
set_defaults(i_ctx_t *i_ctx_p, ref *dest, const font_defaults_t *def, int count)
1971
0
{
1972
0
    int i;
1973
1974
0
    for (i = 0; i < count; i++) {
1975
0
        ref name, *dummy, value;
1976
0
        int code;
1977
0
        float xx;
1978
1979
0
        if ((code = name_ref(imemory, (const unsigned char *)font_keys[def[i].key],
1980
0
                                             font_keys_sz[def[i].key], &name, 0)) < 0)
1981
0
            return code;
1982
0
        if (dict_find(dest, &name, &dummy) <= 0) {
1983
0
           switch (def[i].value) {
1984
0
               default:
1985
0
               case k_0:
1986
0
                   make_int(&value, 0);
1987
0
                   break;
1988
0
               case k_1:
1989
0
                   make_int(&value, 1);
1990
0
                   break;
1991
0
               case k_2:
1992
0
                   make_int(&value, 2);
1993
0
                   break;
1994
0
               case k_7:
1995
0
                   make_int(&value, 7);
1996
0
                   break;
1997
0
               case k_50:
1998
0
                   make_int(&value, 50);
1999
0
                   break;
2000
0
               case k_neg_100:
2001
0
                   make_int(&value, -100);
2002
0
                   break;
2003
0
               case k_8720:
2004
0
                   make_int(&value, 8720);
2005
0
                   break;
2006
0
               case k_0_039625:
2007
0
                   make_real(&value, (float)0.039625);
2008
0
                   break;
2009
0
               case k_0_06:
2010
0
                   make_real(&value, (float)0.06);
2011
0
                   break;
2012
0
               case k_false:
2013
0
                   make_bool(&value, 0);
2014
0
                   break;
2015
0
               case k_box:
2016
0
                   if ((code = ialloc_ref_array(&value, a_readonly, 4, "parsecff.default_bbox")) < 0)
2017
0
                        return code;
2018
0
                   make_int(&value.value.refs[0], 0);
2019
0
                   value.value.refs[1] = value.value.refs[2] = value.value.refs[3] = value.value.refs[0];
2020
0
                   break;
2021
0
               case k_matrix:
2022
0
                   xx = (float)0.001;
2023
0
                   goto make_matrix;
2024
0
               case k_matrix_1:
2025
0
                   xx = (float)1.;
2026
0
                 make_matrix:;
2027
0
                   if ((code = ialloc_ref_array(&value, a_readonly, 6, "parsecff.default_bbox")) < 0)
2028
0
                        return code;
2029
0
                   make_real(&value.value.refs[0], xx);
2030
0
                   value.value.refs[3] = value.value.refs[0];
2031
0
                   make_real(&value.value.refs[1], (float)0.);
2032
0
                   value.value.refs[2] = value.value.refs[4] = value.value.refs[5] = value.value.refs[1];
2033
0
                   break;
2034
0
               case k_emptydict:
2035
0
                   if ((code = dict_create(0, &value)) < 0)
2036
0
                       return code;
2037
0
           }
2038
0
           if ((code = idict_put(dest, &name, &value)) < 0)
2039
0
               return code;
2040
0
        }
2041
0
    }
2042
0
    return 0;
2043
0
}
2044
2045
typedef struct tag_font_offsets_t {
2046
    unsigned int fdarray_off;
2047
    unsigned int fdselect_off;
2048
    unsigned int charset_off;
2049
    unsigned int encoding_off;
2050
    unsigned int private_off;
2051
    unsigned int charstrings_off;
2052
    unsigned int local_subrs_off;
2053
    unsigned int private_size;
2054
    bool have_ros;
2055
} font_offsets_t;
2056
2057
static int
2058
parse_dict(i_ctx_t *i_ctx_p,  ref *topdict, font_offsets_t *offsets,
2059
  const cff_index_t *strings, const cff_data_t *data,
2060
  const unsigned p0, const unsigned pe)
2061
0
{
2062
0
    ref *fontinfodict = 0, *privatedict = 0, arg, ops[MAXOP];
2063
0
    int op_i = 0;
2064
2065
0
    unsigned c;
2066
0
    unsigned p = p0;
2067
0
    unsigned control;
2068
0
    int code;
2069
2070
0
    while(p < pe) {
2071
0
        if ((code = card8(&c, data, p++, pe)) < 0)
2072
0
            return code;
2073
0
        switch(c) {
2074
0
            case 0: /* version 0      SID -, FontInfo */
2075
0
                control = CONTROL(version, 1, k_fontinfodict | k_sid);
2076
0
                break;
2077
0
            case 1: /* Notice 1       SID -, FontInfo */
2078
0
                control = CONTROL(Notice, 1, k_fontinfodict | k_sid);
2079
0
                break;
2080
0
            case 2: /* FullName 2     SID -, FontInfo */
2081
0
                control = CONTROL(FullName, 1, k_fontinfodict | k_sid);
2082
0
                break;
2083
0
            case 3: /* FamilyName 3   SID -, FontInfo */
2084
0
                control = CONTROL(FamilyName, 1, k_fontinfodict | k_sid);
2085
0
                break;
2086
0
            case 4: /* Weight 4       SID -, FontInfo */
2087
0
                control = CONTROL(FamilyName, 1, k_fontinfodict | k_sid);
2088
0
                break;
2089
0
            case 5: /* FontBBox 5 array 0 0 0 0 */
2090
0
                control = CONTROL(FontBBox, 4, k_topdict | k_array);
2091
0
                break;
2092
0
            case 6: /* BlueValues 6 delta -, Private */
2093
0
                control = CONTROL(BlueValues, 0, k_privatedict | k_array | k_delta);
2094
0
                break;
2095
0
            case 7: /* OtherBlues 7 delta -, Private */
2096
0
                control = CONTROL(OtherBlues, 0, k_privatedict | k_array | k_delta);
2097
0
                break;
2098
0
            case 8: /* FamilyBlues 8 delta -, Private */
2099
0
                control = CONTROL(FamilyBlues, 0, k_privatedict | k_array | k_delta);
2100
0
                break;
2101
0
            case 9: /* FamilyOtherBlues 9 delta -, Private */
2102
0
                control = CONTROL(FamilyOtherBlues, 0, k_privatedict | k_array | k_delta);
2103
0
                break;
2104
0
            case 10: /* StdHW 10 number -, Private */ /* converts to array */
2105
0
                control = CONTROL(StdHW, 1, k_privatedict | k_array);
2106
0
                break;
2107
0
            case 11: /* StdVW 11 number -, Private */ /* converts to array */
2108
0
                control = CONTROL(StdVW, 1, k_privatedict | k_array);
2109
0
                break;
2110
0
            case 12:
2111
0
                if (p >= pe)
2112
0
                    return_error(gs_error_rangecheck);
2113
0
                if ((code = card8(&c, data, p++, pe)) < 0)
2114
0
                    return code;
2115
0
                switch(c) {
2116
0
                    case 0: /* Copyright    12 0 SID -, FontInfo */
2117
0
                        control = CONTROL(Copyright, 1, k_fontinfodict | k_sid);
2118
0
                        break;
2119
0
                    case 1: /* isFixedPitch 12 1 boolean 0 (false), FontInfo */
2120
0
                        control = CONTROL(isFixedPitch, 1, k_fontinfodict | k_int | k_bool);
2121
0
                        break;
2122
0
                    case 2: /* ItalicAngle  12 2 number 0, FontInfo */
2123
0
                        control = CONTROL(ItalicAngle, 1, k_fontinfodict);
2124
0
                        break;
2125
0
                    case 3: /* UnderlinePosition 12 3 number -100, FontInfo */
2126
0
                        control = CONTROL(UnderlinePosition, 1, k_fontinfodict);
2127
0
                        break;
2128
0
                    case 4: /* UnderlineThickness 12 4 number 50, FontInfo */
2129
0
                        control = CONTROL(UnderlineThickness, 1, k_fontinfodict);
2130
0
                        break;
2131
0
                    case 5: /* PaintType    12 5 number 0 */
2132
0
                        control = CONTROL(PaintType, 1, k_fontinfodict);
2133
0
                        break;
2134
0
                    case 6: /* CharstringType 12 6 number 2 */
2135
0
                        control = CONTROL(CharstringType, 1, k_fontinfodict | k_int);
2136
0
                        break;
2137
0
                    case 7: /* FontMatrix   12 7 array 0.001 0 0 0.001 0 0 */
2138
0
                        control = CONTROL(FontMatrix, 6, k_topdict | k_array);
2139
0
                        break;
2140
0
                    case 8: /* StrokeWidth  12 8 number 0 */
2141
0
                        control = CONTROL(StrokeWidth, 1, k_topdict);
2142
0
                        break;
2143
0
                    case 9: /* BlueScale 12 9 number 0.039625, Private */
2144
0
                        control = CONTROL(BlueScale, 1, k_privatedict);
2145
0
                        break;
2146
0
                    case 10: /* BlueShift 12 10 number 7, Private */
2147
0
                        control = CONTROL(BlueShift, 1, k_privatedict);
2148
0
                        break;
2149
0
                    case 11: /* BlueFuzz 12 11 number 1, Private */
2150
0
                        control = CONTROL(BlueFuzz, 1, k_privatedict);
2151
0
                        break;
2152
0
                    case 12: /* StemSnapH 12 12 delta -, Private */
2153
0
                        control = CONTROL(StemSnapH, 0, k_privatedict | k_array | k_delta);
2154
0
                        break;
2155
0
                    case 13: /* StemSnapV 12 13 delta -, Private */
2156
0
                        control = CONTROL(StemSnapV, 0, k_privatedict | k_array | k_delta);
2157
0
                        break;
2158
0
                    case 14: /* ForceBold 12 14 boolean false, Private */
2159
0
                        control = CONTROL(ForceBold, 1, k_privatedict | k_int | k_bool);
2160
0
                        break;
2161
0
                    case 17: /* LanguageGroup 12 17 number 0, Private */
2162
0
                        control = CONTROL(LanguageGroup, 1, k_privatedict | k_int);
2163
0
                        break;
2164
0
                    case 18: /* ExpansionFactor 12 18 number 0.06, Private */
2165
0
                        control = CONTROL(ExpansionFactor, 1, k_privatedict);
2166
0
                        break;
2167
0
                    case 19: /* initialRandomSeed 12 19 number 0, Private */
2168
0
                        control = CONTROL(initialRandomSeed, 1, k_privatedict | k_int);
2169
0
                        break;
2170
0
                    case 20: /* SyntheticBase 12 20 number -, synthetic base font index */
2171
0
                        control = CONTROL(SyntheticBase, 1, k_privatedict | k_int);
2172
0
                        break;
2173
0
                    case 21: /* PostScript 12 21 SID -, embedded PostScript language code */
2174
0
                        control = CONTROL(PostScript, 1, k_topdict | k_int | k_sid);
2175
0
                        break;
2176
0
                    case 22: /* BaseFontName 12 22 SID -, (added as needed by Adobe-based technology) */
2177
0
                        control = CONTROL(BaseFontName, 1, k_topdict | k_int | k_sid);
2178
0
                        break;
2179
0
                    case 23: /* BaseFontBlend 12 23 delta -, (added as needed by Adobe-based technology) */
2180
0
                        control = CONTROL(BaseFontBlend, 0, k_topdict | k_array | k_delta);
2181
0
                        break;
2182
2183
                    /* CIDFont */
2184
0
                    case 30: {/* ROS 12 30 SID SID number -, Registry Ordering Supplement */
2185
0
                        ref ros;
2186
2187
0
                        if ((op_i -= 3) < 0)
2188
0
                            return_error(gs_error_stackunderflow);
2189
0
                        if ((code = dict_create(3, &ros)) < 0)
2190
0
                            return code;
2191
0
                        check_type(ops[op_i], t_integer);
2192
0
                        if ((code = make_string_from_sid(i_ctx_p, &arg, strings, data, ops[op_i].value.intval)) < 0)
2193
0
                            return code;
2194
0
                        if ((code = idict_put_c_name(i_ctx_p, &ros, STR2MEM("Registry"), &arg)) < 0)
2195
0
                            return code;
2196
0
                        if ((code = make_string_from_sid(i_ctx_p, &arg, strings, data, ops[op_i + 1].value.intval)) < 0)
2197
0
                            return code;
2198
0
                        if ((code = idict_put_c_name(i_ctx_p, &ros, STR2MEM("Ordering"), &arg)) < 0)
2199
0
                            return code;
2200
0
                        if ((code = idict_put_c_name(i_ctx_p, &ros, STR2MEM("Supplement"), &ops[op_i + 2])) < 0)
2201
0
                            return code;
2202
0
                        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("CIDSystemInfo"), &ros)) < 0)
2203
0
                            return code;
2204
0
                        offsets->have_ros = true;
2205
0
                        }
2206
0
                        continue;
2207
0
                    case 31: /* CIDFontVersion 12 31 number 0 */
2208
0
                        control = CONTROL(CIDFontVersion, 1, k_topdict);
2209
0
                        break;
2210
0
                    case 32: /* CIDFontRevision 12 32 number 0 */
2211
0
                        control = CONTROL(CIDFontRevision, 1, k_topdict);
2212
0
                        break;
2213
0
                    case 33: /* CIDFontType 12 33 number 0 */
2214
0
                        control = CONTROL(CIDFontType, 1, k_topdict | k_int);
2215
0
                        break;
2216
0
                    case 34: /* CIDCount 12 34 number 8720 */
2217
0
                        control = CONTROL(CIDCount, 1, k_topdict | k_int);
2218
0
                        break;
2219
0
                    case 35: /* UIDBase 12 35 number - */
2220
0
                        control = CONTROL(UIDBase, 1, k_topdict | k_int);
2221
0
                        break;
2222
0
                    case 36: /* FDArray 12 36 number -, Font DICT (FD) INDEX offset (0) */
2223
0
                        control = 0;
2224
0
                        if (--op_i < 0)
2225
0
                            return_error(gs_error_stackunderflow);
2226
0
                        check_type(ops[op_i], t_integer);
2227
0
                        offsets->fdarray_off = ops[op_i].value.intval;
2228
0
                        continue;
2229
0
                    case 37: /* FDSelect 12 37 number -, FDSelect offset (0) */
2230
0
                        control = 0;
2231
0
                        if (--op_i < 0)
2232
0
                            return_error(gs_error_stackunderflow);
2233
0
                        check_type(ops[op_i], t_integer);
2234
0
                        offsets->fdselect_off = ops[op_i].value.intval;
2235
0
                        continue;
2236
0
                    case 38: /* FontName 12 38 SID -, FD FontName */
2237
0
                        control = CONTROL(FontName, 1, k_topdict | k_int | k_sid);
2238
0
                        break;
2239
0
                    default:
2240
0
                        continue;
2241
0
                }
2242
0
                break;
2243
0
            case 13: /* UniqueID 13 number - */
2244
0
                control = CONTROL(UniqueID, 1, k_topdict | k_int);
2245
0
                break;
2246
0
            case 14: /* XUID     14 array - */
2247
0
                control = CONTROL(XUID, 0, k_topdict | k_array);
2248
0
                break;
2249
0
            case 15: /* charset  15 number 0, charset offset (0) */
2250
0
                if (--op_i < 0)
2251
0
                    return_error(gs_error_stackunderflow);
2252
0
                check_type(ops[op_i], t_integer);
2253
0
                offsets->charset_off = ops[op_i].value.intval;
2254
0
                continue;
2255
0
            case 16: /* Encoding 16 number 0, encoding offset (0) */
2256
0
                if (--op_i < 0)
2257
0
                    return_error(gs_error_stackunderflow);
2258
0
                check_type(ops[op_i], t_integer);
2259
0
                offsets->encoding_off = ops[op_i].value.intval;
2260
0
                continue;
2261
0
            case 17: /* CharStrings 17 number -, CharStrings offset (0) */
2262
0
                if (--op_i < 0)
2263
0
                    return_error(gs_error_stackunderflow);
2264
0
                check_type(ops[op_i], t_integer);
2265
0
                offsets->charstrings_off = ops[op_i].value.intval;
2266
0
                continue;
2267
0
            case 18: /* Private 18 number, number -, Private DICT size and offset (0)*/
2268
0
                if ((op_i -= 2) < 0)
2269
0
                    return_error(gs_error_stackunderflow);
2270
0
                check_type(ops[op_i], t_integer);
2271
0
                check_type(ops[op_i + 1], t_integer);
2272
0
                offsets->private_size = ops[op_i].value.intval;
2273
0
                offsets->private_off  = ops[op_i + 1].value.intval;
2274
0
                continue;
2275
0
            case 19: /* Local Subrs 19 number -, Offset (self) to local subrs */
2276
0
                if (--op_i < 0)
2277
0
                    return_error(gs_error_stackunderflow);
2278
0
                check_type(ops[op_i], t_integer);
2279
0
                offsets->local_subrs_off = ops[op_i].value.intval;
2280
0
                continue;
2281
0
            case 20: /* defaultWidthX 20 number 0 */
2282
0
                control = CONTROL(defaultWidthX, 1, k_privatedict);
2283
0
                break;
2284
0
            case 21: /* nominalWidthX 21 number 0 */
2285
0
                control = CONTROL(nominalWidthX, 1, k_privatedict);
2286
0
                break;
2287
0
            case 22: case 23: case 24: case 25: case 26: case 27: /* reserved */
2288
0
                continue;
2289
0
            case 30: { /* float operand */
2290
0
                if (op_i >= MAXOP)
2291
0
                    return_error(gs_error_limitcheck);
2292
2293
0
                if ((code = get_float(&ops[op_i], data, p, pe)) < 0)
2294
0
                    return code;
2295
2296
0
                op_i++;
2297
0
                p += code;
2298
0
                }
2299
0
                continue;
2300
0
            case 31: case 255: /* reserved */
2301
0
                continue;
2302
0
            default:
2303
0
                {  int n;  /* int operand */
2304
2305
0
                   if (op_i >= MAXOP)
2306
0
                       return_error(gs_error_limitcheck);
2307
0
                   if ((code = get_int(&n, data, p - 1, pe)) < 0)
2308
0
                       return code;
2309
0
                   make_int(&ops[op_i], n);
2310
0
                   op_i++;
2311
0
                   p += code - 1;
2312
0
                }
2313
0
                continue;
2314
0
        }
2315
0
        {
2316
0
            unsigned int n_op = control & 255;
2317
0
            unsigned int string_id =  (control >> 8) & 255;
2318
0
            ref arg, *dict;
2319
2320
0
            if (n_op == 0)
2321
0
                n_op = op_i;
2322
0
            else if (op_i < n_op)
2323
0
                return_error(gs_error_stackunderflow);
2324
0
            if (control & k_delta)
2325
0
                undelta(ops + op_i - n_op, n_op);
2326
0
            op_i -= n_op;
2327
0
            if (control & (k_sid | k_int | k_bool)) {
2328
0
                if (!r_has_type(&ops[op_i], t_integer))
2329
0
                    return_error(gs_error_typecheck);
2330
0
            }
2331
0
            if (control & k_bool)
2332
0
                make_bool(&arg, !!ops[op_i].value.intval);
2333
0
            if (control & k_sid) {
2334
0
                if ((code = make_string_from_sid(i_ctx_p, &arg, strings, data, ops[op_i].value.intval)) < 0)
2335
0
                    return code;
2336
0
            }
2337
0
            if (control & k_array) {
2338
0
                if ((code = ialloc_ref_array(&arg, a_readonly, n_op, "parsecff.array")) < 0)
2339
0
                    return code;
2340
0
                memcpy(arg.value.refs, ops + op_i, n_op*sizeof(ref));
2341
0
            }
2342
0
            if (control & k_privatedict) {
2343
0
                if ((code = find_font_dict(i_ctx_p, topdict, &privatedict, "Private")) < 0)
2344
0
                    return code;
2345
0
                dict = privatedict;
2346
0
            } else if (control & k_fontinfodict) {
2347
0
                if ((code = find_font_dict(i_ctx_p, topdict, &fontinfodict, "FontInfo")) < 0)
2348
0
                    return code;
2349
0
                dict = fontinfodict;
2350
0
            } else
2351
0
                dict = topdict;
2352
0
            code = idict_put_c_name(i_ctx_p, dict, font_keys[string_id], font_keys_sz[string_id],
2353
0
              ((control & (k_bool | k_sid | k_array)) == 0 ? &ops[op_i] : &arg));
2354
0
            if (code < 0)
2355
0
                return code;
2356
0
        }
2357
0
    }
2358
0
    return 0;
2359
0
}
2360
2361
static int
2362
parse_font(i_ctx_t *i_ctx_p,  ref *topdict,
2363
  const cff_index_t *strings, const ref *global_subrs,
2364
  const cff_data_t *data,
2365
  const unsigned p_top, const unsigned pe_top,
2366
  const unsigned p_all, const unsigned pe_all,
2367
  const bool force_cidfont)
2368
0
{
2369
0
    int code = 0;
2370
0
    ref *privatedict = 0, *fontinfodict = 0;
2371
0
    cff_index_t local_subrs, charstrings_index;
2372
0
    int (*peek_charset_proc)(const cff_data_t *, unsigned p, unsigned pe, unsigned i);
2373
0
    font_offsets_t offsets;
2374
2375
0
    memset(&offsets, 0, sizeof(offsets)); /* set defaults for charset_off, encoding_off */
2376
2377
0
    if ((code = parse_dict(i_ctx_p, topdict, &offsets, strings, data, p_top, pe_top)) < 0)
2378
0
        return code;
2379
2380
    /* if the values for the /Private dict are nonsensical, ignore it
2381
     * and hope the font doesn't actually need it!
2382
     */
2383
0
    if ((offsets.private_off + offsets.private_size) <= pe_all) {
2384
0
        if ((code = parse_dict(i_ctx_p, topdict, &offsets, strings, data,
2385
0
                      p_all + offsets.private_off,
2386
0
                      p_all + offsets.private_off + offsets.private_size)) < 0)
2387
0
            return code;
2388
0
    }
2389
0
    if ((code = parse_index(&local_subrs, data,
2390
0
               (offsets.local_subrs_off ? p_all + offsets.private_off + offsets.local_subrs_off : 0),
2391
0
                pe_all)) < 0)
2392
0
        return code;
2393
2394
0
    if ((code = find_font_dict(i_ctx_p, topdict, &privatedict, "Private")) < 0)
2395
0
        return code;
2396
0
    if ((code = find_font_dict(i_ctx_p, topdict, &fontinfodict, "FontInfo")) < 0)
2397
0
        return code;
2398
0
    if (local_subrs.count) {
2399
0
        ref r;
2400
2401
0
        if (( code = make_stringarray_from_index(i_ctx_p, &r, &local_subrs, data)) < 0)
2402
0
            return code;
2403
0
        if ((code = idict_put_c_name(i_ctx_p, privatedict, STR2MEM("Subrs"), &r)) < 0)
2404
0
            return code;
2405
0
    }
2406
0
    if (global_subrs && !offsets.have_ros) {
2407
0
        if ((code = idict_put_c_name(i_ctx_p, privatedict, STR2MEM("GlobalSubrs"), global_subrs)) < 0)
2408
0
            return code;
2409
0
    }
2410
0
    if (offsets.charstrings_off <= 0)
2411
0
        return_error(gs_error_invalidfont);
2412
0
    if ((code = parse_index(&charstrings_index, data, p_all + offsets.charstrings_off, pe_all)) < 0)
2413
0
        return code;
2414
2415
0
    switch (offsets.charset_off) {
2416
0
        case 0:
2417
0
            peek_charset_proc = iso_adobe_charset_proc;
2418
0
            break;
2419
0
        case 1:
2420
0
            peek_charset_proc = expert_charset_proc;
2421
0
            break;
2422
0
        case 2:
2423
0
            peek_charset_proc = expert_subset_charset_proc;
2424
0
            break;
2425
0
        default: {
2426
0
            unsigned u;
2427
2428
0
            if ((code = card8(&u, data, p_all + offsets.charset_off, pe_all)) < 0)
2429
0
                return code;
2430
0
            switch (u) {
2431
0
                case 0:
2432
0
                    peek_charset_proc = format0_charset_proc;
2433
0
                    break;
2434
0
                case 1:
2435
0
                    peek_charset_proc = format1_charset_proc;
2436
0
                    break;
2437
0
                case 2:
2438
0
                    peek_charset_proc = format2_charset_proc;
2439
0
                    break;
2440
0
                default:
2441
0
                    return_error(gs_error_rangecheck);
2442
0
            }
2443
0
        }
2444
0
    }
2445
2446
0
    if (offsets.have_ros) {  /* CIDFont */
2447
0
        unsigned int i;
2448
0
        cff_index_t fdarray;
2449
0
        ref int_ref, cstr_ref, fdarray_ref, glyph_directory_ref;
2450
0
        unsigned int fdselect_off = offsets.fdselect_off;
2451
0
        unsigned int fdselect_code;
2452
0
        int (*fdselect_proc)(const cff_data_t *, unsigned p, unsigned pe, unsigned int i);
2453
0
        ref *array_FontMatrix, name_FontMatrix;
2454
0
        bool top_has_FontMatrix;
2455
2456
0
        if (offsets.fdarray_off == 0 || fdselect_off == 0)
2457
0
            return_error(gs_error_invalidfont);
2458
0
        if ((code = parse_index(&fdarray, data, p_all + offsets.fdarray_off,  pe_all)) < 0)
2459
0
            return code;
2460
0
        if ((code = ialloc_ref_array(&fdarray_ref, a_readonly, fdarray.count, "parsecff.fdarray")) < 0)
2461
0
            return code;
2462
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("FDArray"), &fdarray_ref)) < 0)
2463
0
            return code;
2464
0
        if ((code = dict_create(charstrings_index.count + 1, &glyph_directory_ref)) < 0)
2465
0
            return code;
2466
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("GlyphDirectory"), &glyph_directory_ref)) < 0)
2467
0
            return code;
2468
0
        if ((code = name_ref(imemory, (const unsigned char *)font_keys[k_FontMatrix], font_keys_sz[k_FontMatrix], &name_FontMatrix, 0)) < 0)
2469
0
            return code;
2470
0
        top_has_FontMatrix = dict_find(topdict, &name_FontMatrix, &array_FontMatrix) > 0;
2471
2472
0
        for (i = 0; i < fdarray.count; i++) {
2473
0
            unsigned int len;
2474
0
            unsigned doff;
2475
0
            cff_index_t fd_subrs;
2476
0
            ref *fdprivate = 0;
2477
0
            ref *fdfont = &fdarray_ref.value.refs[i];
2478
2479
0
            offsets.local_subrs_off = offsets.private_off = 0;
2480
0
            if ((code = peek_index(&doff, &len, &fdarray, data, i)) < 0)
2481
0
                return code;
2482
0
            if ((code = dict_create(5, fdfont)) < 0)
2483
0
                return code;
2484
0
            if ((code = parse_dict(i_ctx_p, fdfont, &offsets, strings, data, doff, doff + len)) < 0)
2485
0
                return code;
2486
0
            if ((code = parse_dict(i_ctx_p, fdfont, &offsets, strings, data,
2487
0
              p_all + offsets.private_off, p_all + offsets.private_off + offsets.private_size)) < 0)
2488
0
                return code;
2489
0
            if ((code = find_font_dict(i_ctx_p, fdfont, &fdprivate, "Private")) < 0)
2490
0
                return code;
2491
0
            if (offsets.local_subrs_off) {
2492
0
                if ((code = parse_index(&fd_subrs, data,
2493
0
                  p_all + offsets.private_off + offsets.local_subrs_off, pe_all)) < 0)
2494
0
                    return code;
2495
0
                if (fd_subrs.count) {
2496
0
                    ref r;
2497
2498
0
                    if (( code = make_stringarray_from_index(i_ctx_p, &r, &fd_subrs, data)) < 0)
2499
0
                        return code;
2500
0
                    if ((code = idict_put_c_name(i_ctx_p, fdprivate, STR2MEM("Subrs"), &r)) < 0)
2501
0
                        return code;
2502
0
                }
2503
0
            }
2504
0
            if (global_subrs) {
2505
0
                if ((code = idict_put_c_name(i_ctx_p, fdprivate, STR2MEM("GlobalSubrs"), global_subrs)) < 0)
2506
0
                    return code;
2507
0
            }
2508
           /* There is no explicit description what to do with FontMatrix of CFF CIDFont in
2509
            * Adobe tech note #5176 (CFF Spec), Ken and Masaki have figured out this is what
2510
            * it should be:
2511
            *
2512
            * 1) If both Top DICT and Font DICT does _not_ have FontMatrix, then Top DICT =
2513
            * [0.001 0 0 0.001 0 0], Font DICT= [1 0 0 1 0 0].  (Or, Top DICT = (absent),
2514
            * Font DICT = [0.001 0 0 0.001 0 0] then let '/CIDFont defineresource'
2515
            * make Top DICT = [0.001 0 0 0.001 0 0], Font DICT = [1 0 0 1 0 0].)
2516
            *
2517
            * 2) If Top DICT has FontMatrix and Font DICT doesn't, then Top DICT = (supplied
2518
            * matrix), Font DICT = [1 0 0 1 0 0].
2519
            *
2520
            * 3) If Top DICT does not have FontMatrix but Font DICT does, then Top DICT = [1
2521
            * 0 0 1 0 0], Font DICT = (supplied matrix).  (Or, Top DICT = (absent),
2522
            * Font DICT = (supplied matrix) then let '/CIDFont defineresource'
2523
            * make Top DICT = [0.001 0 0 0.001 0 0], Font DICT = (supplied matrix 1000 times
2524
            * larger).)
2525
            *
2526
            * 4) If both Top DICT and Font DICT _does_ have FontMatrix, then Top DICT =
2527
            * (supplied matrix), Font DICT = (supplied matrix).
2528
            */
2529
0
            if (top_has_FontMatrix) {
2530
0
                if (dict_find(fdfont, &name_FontMatrix, &array_FontMatrix) <= 0) {
2531
0
                    if ((code = set_defaults(i_ctx_p, fdfont, set_unit_matrix, count_of(set_unit_matrix))) < 0)
2532
0
                        return code;
2533
0
                }
2534
0
            }
2535
0
            if ((code = set_defaults(i_ctx_p, fdfont, fd_font_defaults, count_of(fd_font_defaults))) < 0)
2536
0
                return code;
2537
0
        }
2538
2539
0
        if ((code = card8(&fdselect_code, data, p_all + fdselect_off, pe_all)) < 0)
2540
0
          return code;
2541
0
        if (fdselect_code == 0)
2542
0
            fdselect_proc = format0_fdselect_proc;
2543
0
        else if (fdselect_code == 3)
2544
0
            fdselect_proc = format3_fdselect_proc;
2545
0
        else
2546
0
            return_error(gs_error_rangecheck);
2547
2548
0
        make_int(&int_ref, 0);
2549
0
        for (i = 0; i < charstrings_index.count; i++) {
2550
0
            int fd;
2551
2552
0
            if (fdarray.count <= 1)
2553
0
               fd = -1;
2554
0
            else {
2555
0
               fd = (*fdselect_proc)(data, p_all + fdselect_off + 1, pe_all, i);
2556
0
               if (fd < 0)
2557
0
                   return fd;
2558
0
            }
2559
0
            if ((code = make_string_from_index(i_ctx_p, &cstr_ref, &charstrings_index, data, i, fd)) < 0)
2560
0
                return code;
2561
0
            if (i > 0)
2562
0
                int_ref.value.intval = (*peek_charset_proc)(data, p_all + offsets.charset_off + 1, pe_all, i - 1);
2563
0
            if ( int_ref.value.intval < 0)
2564
0
                return int_ref.value.intval;
2565
0
            if ((code = idict_put(&glyph_directory_ref, &int_ref, &cstr_ref)) < 0)
2566
0
                return code;
2567
0
        }
2568
0
        int_ref.value.intval = fdarray.count > 1;
2569
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("FDBytes"), &int_ref)) < 0)
2570
0
            return code;
2571
0
        if ((code = set_defaults(i_ctx_p, topdict, cid_font_defaults, count_of(cid_font_defaults))) < 0)
2572
0
            return code;
2573
0
    } else if (force_cidfont) {
2574
        /* Convert simple font to CIDFont */
2575
0
        ref int_ref, str_ref, glyph_directory_ref, fdarray_ref, cidsysteminfo_ref;
2576
2577
0
        if ((code = idict_undef_c_name(i_ctx_p, topdict, STR2MEM("UniqueID"))) < 0)
2578
0
            return code;
2579
0
        if ((code = idict_undef_c_name(i_ctx_p, topdict, STR2MEM("XUID"))) < 0)
2580
0
            return code;
2581
0
        if ((code = ialloc_ref_array(&fdarray_ref, a_readonly, 1, "force_cid.fdarray")) < 0)
2582
0
            return code;
2583
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("FDArray"), &fdarray_ref)) < 0)
2584
0
            return code;
2585
0
        if ((code = dict_create(4, &fdarray_ref.value.refs[0])) < 0)
2586
0
            return code;
2587
0
        if ((code = idict_move_c_name(i_ctx_p, &fdarray_ref.value.refs[0], topdict, STR2MEM("FontMatrix"))) < 0)
2588
0
            return code;
2589
0
        if ((code = idict_move_c_name(i_ctx_p, &fdarray_ref.value.refs[0], topdict, STR2MEM("Private"))) < 0)
2590
0
            return code;
2591
0
        if ((code = idict_move_c_name(i_ctx_p, &fdarray_ref.value.refs[0], topdict, STR2MEM("FontType"))) < 0)
2592
0
            return code;
2593
0
        if ((code = idict_move_c_name(i_ctx_p, &fdarray_ref.value.refs[0], topdict, STR2MEM("PaintType"))) < 0)
2594
0
            return code;
2595
0
        if ((code = set_defaults(i_ctx_p, &fdarray_ref.value.refs[0], fd_font_defaults, count_of(fd_font_defaults))) < 0)
2596
0
            return code;
2597
0
        if ((code = dict_create(3, &cidsysteminfo_ref)) < 0)
2598
0
            return code;
2599
0
        make_string(&str_ref, avm_foreign | a_readonly, 5, (unsigned char *)"Adobe");
2600
0
        if ((code = idict_put_c_name(i_ctx_p, &cidsysteminfo_ref, STR2MEM("Registry"), &str_ref)) < 0)
2601
0
            return code;
2602
0
        make_string(&str_ref, avm_foreign | a_readonly, 8, (unsigned char *)"Identity");
2603
0
        if ((code = idict_put_c_name(i_ctx_p, &cidsysteminfo_ref, STR2MEM("Ordering"), &str_ref)) < 0)
2604
0
            return code;
2605
0
        make_int(&int_ref, 0);
2606
0
        if ((code = idict_put_c_name(i_ctx_p, &cidsysteminfo_ref, STR2MEM("Supplement"), &int_ref)) < 0)
2607
0
            return code;
2608
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("CIDSystemInfo"), &cidsysteminfo_ref)) < 0)
2609
0
            return code;
2610
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("FDBytes"), &int_ref)) < 0)
2611
0
            return code;
2612
2613
0
        if ((code = dict_create(charstrings_index.count + 1, &glyph_directory_ref)) < 0)
2614
0
            return code;
2615
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("GlyphDirectory"), &glyph_directory_ref)) < 0)
2616
0
            return code;
2617
0
        make_int(&int_ref, 0);
2618
0
        for ( ; int_ref.value.intval < (int)charstrings_index.count; int_ref.value.intval++) {
2619
0
            ref cstr_ref;
2620
2621
0
            if ((code = make_string_from_index(i_ctx_p, &cstr_ref, &charstrings_index, data, int_ref.value.intval, -1)) < 0)
2622
0
                return code;
2623
0
            if ((code = idict_put(&glyph_directory_ref, &int_ref, &cstr_ref)) < 0)
2624
0
                return code;
2625
0
        }
2626
0
        make_int(&int_ref, charstrings_index.count);
2627
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("CIDCount"), &int_ref)) < 0)
2628
0
            return code;
2629
0
        if ((code = set_defaults(i_ctx_p, topdict, cid_font_defaults, count_of(cid_font_defaults))) < 0)
2630
0
            return code;
2631
0
    } else {
2632
        /* Simple font */
2633
0
        unsigned int i, gid, enc_format = 0;
2634
0
        int sid;
2635
0
        ref ccoderef, name, cstr, cffcharstrings_dict, charstrings_dict, encoding, notdef;
2636
0
        unsigned char gid2char[256];
2637
0
        unsigned supp_enc_offset = 0;
2638
2639
0
        if ((code = name_ref(imemory, (unsigned char *)".notdef", 7, &notdef, 0)) < 0)
2640
0
            return code;
2641
0
        if ((code = make_string_from_index(i_ctx_p, &cstr, &charstrings_index, data, 0, -1)) < 0)
2642
0
            return code;
2643
0
        if ((code = dict_create(charstrings_index.count + 1, &charstrings_dict)) < 0)
2644
0
            return code;
2645
0
        if ((code = dict_create(charstrings_index.count + 1, &cffcharstrings_dict)) < 0)
2646
0
            return code;
2647
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("CharStrings"), &charstrings_dict)) < 0)
2648
0
            return code;
2649
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("CFFCharStrings"), &cffcharstrings_dict)) < 0)
2650
0
            return code;
2651
0
        make_int(&ccoderef, 0);
2652
0
        if ((code = idict_put(&charstrings_dict, &notdef, &ccoderef)) < 0)
2653
0
            return code;
2654
0
        if ((code = idict_put(&cffcharstrings_dict, &ccoderef, &cstr)) < 0)
2655
0
            return code;
2656
0
        if (offsets.encoding_off <= 1) {
2657
0
            if ((code = ialloc_ref_array(&encoding, a_readonly, 256, "cff_parser.encoding")) < 0)
2658
0
                return code;
2659
0
            for (i = 0; i < 256; i++) {
2660
0
                if ((code = make_name_from_sid(i_ctx_p, &encoding.value.refs[i],
2661
0
                  strings, data, (offsets.encoding_off ? expert_encoding : standard_encoding)[i])) < 0)
2662
0
                    return code;
2663
0
            }
2664
0
        } else {
2665
0
            if ((code = ialloc_ref_array(&encoding, a_readonly, 256, "cff_parser.encoding")) < 0)
2666
0
                return code;
2667
0
            for (i = 0; i < 256; i++)
2668
0
                encoding.value.refs[i] = notdef;
2669
0
            if ((code = card8(&enc_format, data, p_all + offsets.encoding_off, pe_all)) < 0)
2670
0
                return code;
2671
0
            if ((enc_format & 0x7f) == 0) {
2672
0
                unsigned int n_codes;
2673
2674
0
                if ((code = card8(&n_codes, data, p_all + offsets.encoding_off + 1, pe_all)) < 0)
2675
0
                    return code;
2676
0
                gid2char[0] = 0;
2677
0
                for (i = 0; i < n_codes; i++) {
2678
0
                    unsigned int charcode;
2679
2680
0
                    if ((code = card8(&charcode, data, p_all + offsets.encoding_off + 2 + i, pe_all)) < 0)
2681
0
                        return code;
2682
0
                    gid2char[i + 1] = charcode;
2683
0
                }
2684
0
                memset(gid2char + n_codes + 1, 0, sizeof(gid2char) - n_codes - 1);
2685
0
                supp_enc_offset = p_all + offsets.encoding_off + 2 + n_codes;
2686
0
            } else if ((enc_format & 0x7f) == 1) {
2687
0
                unsigned int n_ranges, first, left, j, k = 1;
2688
2689
0
                if ((code = card8(&n_ranges, data, p_all + offsets.encoding_off + 1, pe_all)) < 0)
2690
0
                    return code;
2691
0
                gid2char[0] = 0;
2692
0
                for (i = 0; i < n_ranges; i++) {
2693
0
                    if ((code = card8(&first, data, p_all + offsets.encoding_off + 2 + 2*i, pe_all)) < 0)
2694
0
                        return code;
2695
0
                    if ((code = card8(&left,  data, p_all + offsets.encoding_off + 3 + 2*i, pe_all)) < 0)
2696
0
                        return code;
2697
0
                    for (j = 0; j <= left && k < 256; j++)
2698
0
                         gid2char[k++] = first + j;
2699
0
                }
2700
0
                memset(gid2char + k, 0, sizeof(gid2char) - k);
2701
0
                supp_enc_offset = p_all + offsets.encoding_off + 2*n_ranges + 2;
2702
0
            } else
2703
0
                return_error(gs_error_rangecheck);
2704
0
        }
2705
0
        if ((code = idict_put_c_name(i_ctx_p, topdict, STR2MEM("Encoding"), &encoding)) < 0)
2706
0
            return code;
2707
0
        for (gid = 1; gid < charstrings_index.count; gid++) {
2708
0
            if ((code = make_string_from_index(i_ctx_p, &cstr, &charstrings_index, data, gid, -1)) < 0)
2709
0
                return code;
2710
0
            sid = (*peek_charset_proc)(data, p_all + offsets.charset_off + 1, pe_all, gid - 1);
2711
0
            if (sid < 0)
2712
0
                return sid;
2713
0
            if ((code = make_name_from_sid(i_ctx_p, &name, strings, data, sid)) < 0) {
2714
0
               char buf[40];
2715
0
               int len = gs_snprintf(buf, sizeof(buf), "sid-%d", sid);
2716
2717
0
               if ((code = name_ref(imemory, (unsigned char *)buf, len, &name, 1)) < 0)
2718
0
                   return code;
2719
0
            }
2720
0
            make_int(&ccoderef, gid);
2721
0
            if ((code = idict_put(&charstrings_dict, &name, &ccoderef)) < 0)
2722
0
                return code;
2723
0
            if ((code = idict_put(&cffcharstrings_dict, &ccoderef, &cstr)) < 0)
2724
0
                return code;
2725
0
            if (offsets.encoding_off > 1 && gid < 256) {
2726
0
                encoding.value.refs[gid2char[gid]] = name;
2727
0
            }
2728
0
        }
2729
0
        if (offsets.encoding_off > 1 && (enc_format & 0x80)) {
2730
0
            unsigned int n_supp, charcode, sid;
2731
2732
0
            if ((code = card8(&n_supp, data, supp_enc_offset, pe_all)) < 0)
2733
0
                return code;
2734
0
            for (i = 0; i < n_supp; i++) {
2735
0
                if ((code = card8(&charcode, data, supp_enc_offset + 1 + 3*i, pe_all)) < 0)
2736
0
                    return code;
2737
0
                if ((code = card16(&sid, data, supp_enc_offset + 2 + 3*i, pe_all)) < 0)
2738
0
                    return code;
2739
0
                if ((code = make_name_from_sid(i_ctx_p, &encoding.value.refs[charcode], strings, data, sid)) < 0)
2740
0
                    return code;
2741
0
            }
2742
0
        }
2743
0
        if ((code = set_defaults(i_ctx_p, fontinfodict, fontinfo_font_defaults, count_of(fontinfo_font_defaults))) < 0)
2744
0
            return code;
2745
0
        if ((code = set_defaults(i_ctx_p, privatedict, private_font_defaults, count_of(private_font_defaults))) < 0)
2746
0
            return code;
2747
0
        if ((code = set_defaults(i_ctx_p, topdict, simple_font_defaults, count_of(simple_font_defaults))) < 0)
2748
0
            return code;
2749
0
    }
2750
0
    return 0;
2751
0
}
2752
2753
/* <force_cidfont> <string> .parsecff <dict> */
2754
/* Parse CFF font stored in a single string */
2755
static int
2756
zparsecff(i_ctx_t *i_ctx_p)
2757
0
{
2758
0
    os_ptr op = osp;
2759
0
    unsigned p, pe;
2760
0
    unsigned int hdr_ver, hdr_size, off_size;
2761
0
    cff_index_t names, topdicts, strings, gsubrs;
2762
0
    int code;
2763
0
    unsigned int i_font;
2764
0
    ref gsubrs_array, fontset;
2765
0
    bool force_cidfont;
2766
0
    cff_data_t data;
2767
0
    ref blk_wrap[1];
2768
2769
0
    check_op(2);
2770
0
    check_read(*op);
2771
2772
0
    if (r_has_type(op, t_array)) {  /* no packedarrays */
2773
0
        int i, blk_sz, blk_cnt;
2774
2775
0
        if (op->value.refs == NULL)
2776
0
            return_error(gs_error_typecheck);
2777
2778
0
        data.blk_ref = op->value.refs;
2779
0
        blk_cnt  = r_size(op);
2780
0
        blk_sz = r_size(data.blk_ref);
2781
0
        data.length = 0;
2782
0
        for (i = 0; i < blk_cnt; i++) {
2783
0
            int len;
2784
2785
0
            check_read_type(data.blk_ref[i], t_string);
2786
0
            len = r_size(&data.blk_ref[i]);
2787
0
            if (len > blk_sz || (len < blk_sz && i < blk_cnt - 1))
2788
0
               return_error(gs_error_rangecheck); /* last block can be smaller */
2789
0
            data.length += len;
2790
0
        }
2791
0
        if (data.length == 0)
2792
0
            return_error(gs_error_rangecheck);
2793
2794
0
        if (blk_cnt == 1) {
2795
0
           data.mask   = 0xffff; /* stub */
2796
0
           data.shift  = 16;     /* stub */
2797
0
        } else {
2798
0
            static const int mod2shift[] = {
2799
0
              0, 0, 1, 26, 2, 23, 27, 0, 3, 16, 24, 30, 28, 11, 0, 13, 4, 7, 17,
2800
0
              0, 25, 22, 31, 15, 29, 10, 12, 6, 0, 21, 14, 9, 5, 20, 8, 19, 18
2801
0
            };
2802
0
            data.mask = blk_sz - 1;
2803
0
            if (data.mask & blk_sz)
2804
0
                return_error(gs_error_rangecheck);  /* not a power of 2 */
2805
0
            data.shift = mod2shift[blk_sz % 37];
2806
0
        }
2807
0
    } else if (r_has_type(op, t_string)) {
2808
0
       data.blk_ref = blk_wrap;
2809
0
       blk_wrap[0] = *op;
2810
0
       data.length = r_size(op);
2811
0
       data.mask   = 0xffff; /* stub */
2812
0
       data.shift  = 16;     /* stub */
2813
0
    } else
2814
0
        return_error(gs_error_typecheck);
2815
2816
0
    check_type(op[-1], t_boolean);
2817
0
    force_cidfont = op[-1].value.boolval;
2818
0
    p  = 0;
2819
0
    pe = p + data.length;
2820
2821
0
    if (pe < 8) /* arbitrary, to avoid underflow in pe - 4 */
2822
0
        return_error(gs_error_rangecheck);
2823
0
    if ((code = card8(&hdr_ver, &data, p, pe)) < 0)
2824
0
        return code;
2825
0
    if (hdr_ver != 1)
2826
0
        return_error(gs_error_rangecheck); /* Unsupported version. */
2827
0
    if ((code = card8(&hdr_size, &data, p + 2, pe)) < 0)
2828
0
        return code;
2829
0
    if ((code = card8(&off_size, &data, p + 3, pe)) < 0)
2830
0
        return code;
2831
2832
0
    if ((code = parse_index(&names, &data, p + hdr_size, pe)) < 0)
2833
0
        return code;
2834
0
    if ((code = parse_index(&topdicts, &data, names.end, pe)) < 0)
2835
0
        return code;
2836
0
    if ((code = parse_index(&strings, &data, topdicts.end, pe)) < 0)
2837
0
        return code;
2838
0
    if ((code = parse_index(&gsubrs, &data, strings.end, pe)) < 0)
2839
0
        return code;
2840
0
    if (gsubrs.count) {
2841
0
        if (( code = make_stringarray_from_index(i_ctx_p, &gsubrs_array, &gsubrs, &data)) < 0)
2842
0
            return code;
2843
0
    }
2844
0
    if (names.count >= 65535)
2845
0
        return_error(gs_error_limitcheck);
2846
0
    if ((code = dict_create(names.count, &fontset)) < 0)
2847
0
        return code;
2848
2849
0
    for (i_font = 0; i_font < names.count; i_font++) {
2850
0
        unsigned int name_data;
2851
0
        unsigned int name_len;
2852
0
        ref name;
2853
0
        unsigned int topdict_data;
2854
0
        unsigned int topdict_len;
2855
0
        ref topdict;
2856
0
        unsigned char buf[200];
2857
2858
0
        if ((code = peek_index(&name_data, &name_len, &names, &data, i_font)) < 0)
2859
0
            return code;
2860
0
        if (name_len == 0) /* deleted entry */
2861
0
            continue;
2862
0
        if (name_len > sizeof(buf))
2863
0
            return_error(gs_error_limitcheck);
2864
0
        if ((code = get_cff_string(buf, &data, name_data, name_len)) < 0)
2865
0
            return code;
2866
0
        if (buf[0] == 0)   /* deleted entry */
2867
0
            continue;
2868
0
        if ((code = name_ref(imemory, buf, name_len, &name, 1)) < 0)
2869
0
            return code;
2870
0
        if ((code = peek_index(&topdict_data, &topdict_len, &topdicts, &data, i_font)) < 0)
2871
0
            return code;
2872
2873
0
        if ((code = dict_create(21, &topdict)) < 0)
2874
0
            return code;
2875
2876
0
        if ((code = idict_put(&fontset, &name, &topdict)) < 0)
2877
0
            return code;
2878
2879
0
        if ((code = parse_font(i_ctx_p, &topdict, &strings, (gsubrs.count ? &gsubrs_array : 0),
2880
0
                 &data, topdict_data, topdict_data + topdict_len, p, pe, force_cidfont)) < 0)
2881
0
            return code;
2882
0
    }
2883
0
    ref_assign(op - 1, &fontset);
2884
0
    pop(1);
2885
0
    return 0;
2886
0
}
2887
2888
/* ------ Initialization procedure ------ */
2889
2890
const op_def zfont2_op_defs[] =
2891
{
2892
    {"2.buildfont2", zbuildfont2},
2893
    {"2.parsecff", zparsecff},
2894
    op_def_end(0)
2895
};