Debian Codegraph @

podofo/3rdparty/adobe/afdko/source/t1cstr/t1cstr.c

raw
1 /* Copyright 2014 Adobe Systems Incorporated (http://www.adobe.com/). All Rights Reserved.
2 This software is licensed as OpenSource, under the Apache License, Version 2.0.
3 This license is available at: http://opensource.org/licenses/Apache-2.0. */
4
5 /*
6 * Type 1 charstring support.
7 */
8
9 #if defined(_WIN32) && !defined(_USE_MATH_DEFINES)
10 #define _USE_MATH_DEFINES /* Needed to define M_PI under Windows */
11 #endif
12
13 #include "t1cstr.h"
14 #include "txops.h"
15 #include "ctutil.h"
16
17 #include <string.h>
18 #include <math.h>
19 #include <limits.h>
20 #include <stdlib.h>
21
22 /* Make up operator for internal use */
23 #define t2_cntroff t2_reservedESC33
24 enum /* seac operator conversion phase */
25 {
26 seacNone, /* No seac conversion */
27 seacBase, /* In base char */
28 seacAccentPreMove, /* In accent char before first moveto */
29 seacAccentPostMove /* In accent char after first moveto */
30 };
31
32 /* Module context */
33 typedef struct t1cCtx *t1cCtx;
34 struct t1cCtx {
35 long flags; /* Control flags */
36 #define IN_FLEX (1<<0) /* In flex sequence */
37 #define PEND_MOVETO (1<<1) /* Pending moveto */
38 #define PEND_HINTSUBS (1<<2) /* Pending hint substitution */
39 #define SEEN_HINTSUBS (1<<3) /* Seen hint substitution */
40 #define SEEN_MOVETO (1<<4) /* Seen moveto (path data) */
41 #define SEEN_ENDCHAR (1<<5) /* Seen endchar operator */
42 #define SEEN_BLEND (1<<6) /* Seen blend operator: need to round position after each op. */
43 #define NEW_GROUP (1<<7) /* Flags new counter group */
44 #define USE_MATRIX (1<<12) /* Apply transform to */
45 #define USE_GLOBAL_MATRIX (1<<13) /* Transform is applied to entire charstring */
46 struct /* Operand stack */
47 {
48 int cnt;
49 float array[T1_MAX_OP_STACK];
50 } stack;
51 double x; /* Path x-coord */
52 double y; /* Path y-coord */
53 long maxOpStack;
54 int subrDepth;
55 float transformMatrix[6];
56 struct
57 {
58 int useLEStart;
59 float x; /* start x-coord for previous LE */
60 float y; /* start y-coord for previous LE */
61 } le_start;
62 struct /* Left side bearing from hsbw/sbw */
63 {
64 float x;
65 float y;
66 } lsb;
67 struct /* Counter control args */
68 {
69 int cnt;
70 float array[T2_MAX_STEMS * 2 + 2];
71 } cntr;
72 struct /* seac conversion data */
73 {
74 float adx;
75 float ady;
76 float bsb_x; /* base component hsb */
77 float bsb_y; /* base component hsb */
78 float asb_x; /* accent component hsb */
79 float asb_y; /* accent component hsb */
80 float xshift; /* shift = ((seac.adx + seac.bsb) - seac.asb)- accent component hsb */
81 int phase;
82 } seac;
83 float BCA[TX_BCA_LENGTH]; /* BuildCharArray */
84 struct /* Flex args */
85 {
86 long cnt;
87 float args[17];
88 } flex;
89 struct /* Source data */
90 {
91 char *buf; /* Buffer */
92 long length; /* Buffer length */
93 long offset; /* Buffer offset */
94 } src;
95 t1cAuxData *aux; /* Auxiliary parse data */
96 abfGlyphCallbacks *glyph; /* Glyph callbacks */
97 };
98
99 /* Check stack contains at least n elements. */
100 #define CHKUFLOW(n) \
101 do { \
102 if (h->stack.cnt < (n)) { \
103 dummyDebug(h->stack.cnt); \
104 return t1cErrStackUnderflow; \
105 } \
106 } while (0)
107
108 /* Check stack has room for n elements. */
109 #define CHKOFLOW(n) \
110 do { \
111 if (h->stack.cnt + (n) > h->maxOpStack) return t1cErrStackOverflow; \
112 } while (0)
113
114 /* Stack access without check. */
115 #define INDEX(i) (h->stack.array[i])
116 #define POP() (h->stack.array[--h->stack.cnt])
117 #define PUSH(v) (h->stack.array[h->stack.cnt++] = (float)(v))
118
119 #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
120 #define RND(v) ((double)floor((v) + 0.5))
121
122 /* Transform coordinates by matrix. */
123 #define TX(x, y) \
124 RND(h->transformMatrix[0] * (x) + h->transformMatrix[2] * (y) + h->transformMatrix[4])
125 #define TY(x, y) \
126 RND(h->transformMatrix[1] * (x) + h->transformMatrix[3] * (y) + h->transformMatrix[5])
127
128 /* Scale coordinates by matrix. */
129 #define SX(x) RND(h->transformMatrix[0] * (x))
130 #define SY(y) RND(h->transformMatrix[3] * (y))
131
132 /* Flex arguments */
133 #define CHKFLEX(n) \
134 do { \
135 if (h->flex.cnt + (n) > (long)ARRAY_LEN(h->flex.args)) \
136 return t1cErrFlex; \
137 } while (0)
138 #define PUSHFLEX(v) (h->flex.args[h->flex.cnt++] = (float)(v))
139
140 static int t1Decode(t1cCtx h, long offset);
141
142 /* ------------------------------- Data Input ------------------------------ */
143
144 /* Refill input buffer. Return NULL on stream error else pointer to buffer. */
145 static unsigned char *refill(t1cCtx h, unsigned char **end) {
146 /* Read buffer */
147 /* 64-bit warning fixed by cast here HO */
148 h->src.length = (long)h->aux->stm->read(h->aux->stm, h->aux->src, &h->src.buf);
149 if (h->src.length == 0)
150 return NULL; /* Stream error */
151
152 /* Update offset of next read */
153 h->src.offset += h->src.length;
154
155 /* Set buffer limit */
156 *end = (unsigned char *)h->src.buf + h->src.length;
157
158 return (unsigned char *)h->src.buf;
159 }
160
161 /* Seek to offset on source stream. */
162 static int srcSeek(t1cCtx h, long offset) {
163 if (h->aux->stm->seek(h->aux->stm, h->aux->src, offset))
164 return 1;
165 h->src.offset = offset;
166 return 0;
167 }
168
169 /* Check next byte available and refill buffer if not. */
170 #define CHKBYTE(h) \
171 do \
172 if (next == end) { \
173 next = refill(h, &end); \
174 if (next == NULL) \
175 return t1cErrSrcStream; \
176 } \
177 while (0)
178
179 static int dummyDebug(int debugErr) {
180 return debugErr;
181 }
182
183 /* ------------------------------- Callbacks ------------------------------- */
184
185 /* Callback char width. Return non-0 to end parse else 0. */
186 static int callbackWidth(t1cCtx h, float width) {
187 if (width < -32000.0f || width > 32000.0f)
188 /* This is an rare case where a number in the charstring was specified
189 as a 5-byte number but was never followed by a "div" operator which
190 would have reduced its magnitude. The value is erroneous and cannot
191 be represented in Type 2 so we reduce to a safe range so as not to
192 cause subsequent processing problems. */
193 width /= 65536.0f;
194
195 if (h->flags & USE_MATRIX)
196 width = SX(width);
197 else if (h->flags & SEEN_BLEND)
198 width = RND(width);
199 h->glyph->width(h->glyph, width);
200 return h->aux->flags & T1C_WIDTH_ONLY;
201 }
202
203 /* Callback path move. */
204 static void callbackMove(t1cCtx h, float dx, float dy) {
205 double x;
206 double y;
207
208 h->flags &= ~PEND_MOVETO;
209 h->flags |= SEEN_MOVETO;
210
211 x = h->x + dx;
212 y = h->y + dy;
213
214 x = (double)RND_ON_READ(x);
215 y = (double)RND_ON_READ(y);
216
217 if (h->seac.phase == seacAccentPreMove) {
218 x += h->seac.xshift;
219 y += h->seac.ady;
220 h->seac.phase = seacAccentPostMove;
221 }
222
223 h->x = x;
224 h->y = y;
225
226 if (h->flags & USE_MATRIX) {
227 /* if START_PATH_MATRIX is not set, then this either global, or left over from a previous path. */
228 if (h->flags & USE_GLOBAL_MATRIX) {
229 int i;
230 /* restore the global matrix values */
231 for (i = 0; i < 6; i++) {
232 h->transformMatrix[i] = h->aux->matrix[i];
233 }
234 } else {
235 /* If USE_GLOBAL_MATRIX is not set, then the USE_MATRIX is left over from the previous path. Clear it. */
236 h->flags &= ~USE_MATRIX;
237 h->transformMatrix[0] = h->transformMatrix[3] = 1.0;
238 h->transformMatrix[1] = h->transformMatrix[2] = h->transformMatrix[4] = h->transformMatrix[5] = 1.0;
239 }
240 }
241
242 if (h->flags & USE_MATRIX)
243 h->glyph->move(h->glyph, TX(x, y), TY(x, y));
244 else if (h->flags & SEEN_BLEND)
245 h->glyph->move(h->glyph, RND(x), RND(y));
246 else
247 h->glyph->move(h->glyph, x, y);
248 }
249
250 /* Callback path line. */
251 static void callbackLine(t1cCtx h, float dx, float dy) {
252 double x;
253 double y;
254
255 if (h->flags & PEND_MOVETO)
256 callbackMove(h, 0, 0); /* Insert missing move */
257
258 x = h->x + dx;
259 y = h->y + dy;
260
261 x = (double)RND_ON_READ(x);
262 y = (double)RND_ON_READ(y);
263
264 h->x = x;
265 h->y = y;
266
267 if (h->flags & USE_MATRIX)
268 h->glyph->line(h->glyph, TX(x, y), TY(x, y));
269 else if (h->flags & SEEN_BLEND)
270 h->glyph->line(h->glyph, RND(x), RND(y));
271 else
272 h->glyph->line(h->glyph, x, y);
273 }
274
275 /* Callback path curve. */
276 static void callbackCurve(t1cCtx h,
277 float dx1, float dy1,
278 float dx2, float dy2,
279 float dx3, float dy3) {
280 double x1;
281 double y1;
282 double x2;
283 double y2;
284 double x3;
285 double y3;
286
287 if (h->flags & PEND_MOVETO)
288 callbackMove(h, 0, 0); /* Insert missing move */
289
290 x1 = h->x + dx1;
291 y1 = h->y + dy1;
292 x2 = x1 + dx2;
293 y2 = y1 + dy2;
294 x3 = x2 + dx3;
295 y3 = y2 + dy3;
296 x3 = (double)RND_ON_READ(x3);
297 y3 = (double)RND_ON_READ(y3);
298 h->x = x3;
299 h->y = y3;
300
301 if (h->flags & USE_MATRIX)
302 h->glyph->curve(h->glyph,
303 TX(x1, y1), TY(x1, y1),
304 TX(x2, y2), TY(x2, y2),
305 TX(x3, y3), TY(x3, y3));
306 else if (h->flags & SEEN_BLEND)
307 h->glyph->curve(h->glyph,
308 RND(x1), RND(y1),
309 RND(x2), RND(y2),
310 RND(x3), RND(y3));
311 else
312 h->glyph->curve(h->glyph, x1, y1, x2, y2, x3, y3);
313 }
314
315 /* Callback stem hint. */
316 static void callbackStem(t1cCtx h, int flags, float edge, float width) {
317 float edge0;
318 float edge1;
319
320 if (h->glyph->stem == NULL)
321 return;
322
323 if (!(h->flags & SEEN_HINTSUBS) && h->flags & SEEN_MOVETO)
324 /* Initial unsubstituted hints after drawing ops; force substitution */
325 h->flags |= (PEND_HINTSUBS | SEEN_HINTSUBS);
326
327 /* Adjust offset */
328 if (h->seac.phase == seacBase) {
329 edge += (flags & ABF_VERT_STEM) ? h->seac.bsb_x : h->seac.bsb_y;
330 } else if (h->seac.phase > seacBase) {
331 float shift = ((flags & ABF_VERT_STEM) ? h->seac.xshift : h->seac.ady) + ((flags & ABF_VERT_STEM) ? h->seac.asb_x : h->seac.asb_y);
332 edge += (flags & ABF_VERT_STEM) ? shift : h->seac.ady;
333 } else {
334 edge += (flags & ABF_VERT_STEM) ? h->lsb.x : h->lsb.y;
335 }
336
337 edge0 = edge;
338 edge1 = edge + width;
339
340 if (h->flags & USE_MATRIX) {
341 /* Apply matrix */
342 if (flags & ABF_VERT_STEM) {
343 edge0 = SX(edge0);
344 edge1 = SX(edge1);
345 } else {
346 edge0 = SY(edge0);
347 edge1 = SY(edge1);
348 }
349 } else if (h->flags & SEEN_BLEND) {
350 edge0 = RND(edge0);
351 edge1 = RND(edge1);
352 }
353
354 /* Set flags */
355 if (h->flags & PEND_HINTSUBS) {
356 flags |= ABF_NEW_HINTS;
357 h->flags &= ~PEND_HINTSUBS;
358 }
359 if (h->flags & NEW_GROUP) {
360 flags |= ABF_NEW_GROUP;
361 h->flags &= ~NEW_GROUP;
362 }
363
364 h->glyph->stem(h->glyph, flags, edge0, edge1);
365 }
366
367 /* Callback path flex. */
368 static void callbackFlex(t1cCtx h) {
369 float *args = h->flex.args;
370 float dx1 = args[0] + args[2];
371 float dy1 = args[1] + args[3];
372 float dx2 = args[4];
373 float dy2 = args[5];
374 float dx3 = args[6];
375 float dy3 = args[7];
376 float dx4 = args[8];
377 float dy4 = args[9];
378 float dx5 = args[10];
379 float dy5 = args[11];
380 float dx6 = args[12];
381 float dy6 = args[13];
382 float depth = args[14];
383
384 if (h->flags & PEND_MOVETO)
385 callbackMove(h, 0, 0); /* Insert missing move */
386
387 if (h->glyph->flex == NULL) {
388 /* Callback as 2 curves */
389 callbackCurve(h,
390 dx1, dy1,
391 dx2, dy2,
392 dx3, dy3);
393 callbackCurve(h,
394 dx4, dy4,
395 dx5, dy5,
396 dx6, dy6);
397 } else {
398 /* Callback as flex hint; convert to absolute coordinates */
399 double x1 = h->x + dx1;
400 double y1 = h->y + dy1;
401 double x2 = x1 + dx2;
402 double y2 = y1 + dy2;
403 double x3 = x2 + dx3;
404 double y3 = y2 + dy3;
405 double x4 = x3 + dx4;
406 double y4 = y3 + dy4;
407 double x5 = x4 + dx5;
408 double y5 = y4 + dy5;
409 double x6 = x5 + dx6;
410 double y6 = y5 + dy6;
411 x6 = (double)RND_ON_READ(x6);
412 y6 = (double)RND_ON_READ(y6);
413 h->x = x6;
414 h->y = y6;
415
416 if (h->flags & USE_MATRIX) {
417 x1 = TX(x1, y1);
418 y1 = TY(x1, y1);
419 x2 = TX(x2, y2);
420 y2 = TY(x2, y2);
421 x3 = TX(x3, y3);
422 y3 = TY(x3, y3);
423 x4 = TX(x4, y4);
424 y4 = TY(x4, y4);
425 x5 = TX(x5, y5);
426 y5 = TY(x5, y5);
427 x6 = TX(x6, y6);
428 y6 = TY(x6, y6);
429 /* depth not in char space units so not transformed! */
430 } else if (h->flags & SEEN_BLEND) {
431 x1 = RND(x1);
432 y1 = RND(y1);
433 x2 = RND(x2);
434 y2 = RND(y2);
435 x3 = RND(x3);
436 y3 = RND(y3);
437 x4 = RND(x4);
438 y4 = RND(y4);
439 x5 = RND(x5);
440 y5 = RND(y5);
441 x6 = RND(x6);
442 y6 = RND(y6);
443 depth = RND(depth);
444 }
445
446 h->glyph->flex(h->glyph, depth,
447 x1, y1,
448 x2, y2,
449 x3, y3,
450 x4, y4,
451 x5, y5,
452 x6, y6);
453 }
454
455 /* The Type 1 flex mechanism specifies the new current point as a pair of
456 absolute coordinates via operands 15 and 16. This information is
457 redundant since it could be calculated by adding all the deltas but I
458 have found that there is sometimes disagreement between this calculation
459 and the specified operands, the operands being in error. I have chosen
460 to use the operands to reset the current point because it mimics the way
461 that the rasterizer functions and ensures that incorrectly constructed
462 fonts will be faithfully reproduced even when wrong! The major source of
463 these errors is a result of a BuildFont off-by-one bug when moving from
464 a closepoint-terminated subpath to a new subpath in MM fonts. */
465
466 if (h->seac.phase <= seacBase) {
467 h->x = (double)RND_ON_READ(args[15]);
468 h->y = (double)RND_ON_READ(args[16]);
469 }
470 }
471
472 /* Callback operator and args. */
473 static void callbackOp(t1cCtx h, int op) {
474 h->glyph->genop(h->glyph, h->stack.cnt, h->stack.array, op);
475 }
476
477 /* Callback seac operator. */
478 static void callback_seac(t1cCtx h, float adx, float ady, int bchar, int achar) {
479 if (h->flags & USE_MATRIX) {
480 adx = TX(adx, ady);
481 ady = TY(adx, ady);
482 } else if (h->flags & SEEN_BLEND) {
483 adx = RND(adx);
484 ady = RND(ady);
485 }
486 h->glyph->seac(h->glyph, adx, ady, bchar, achar);
487 }
488
489 /* ---------------------------- Charstring Parse --------------------------- */
490
491 /* Execute "and" op. Return 0 on success else error code. */
492 static int do_and(t1cCtx h) {
493 int b;
494 int a;
495 CHKUFLOW(2);
496 b = POP() != 0.0;
497 a = POP() != 0.0;
498 PUSH(a && b);
499 return 0;
500 }
501
502 /* Execute "or" op. Return 0 on success else error code. */
503 static int do_or(t1cCtx h) {
504 int b;
505 int a;
506 CHKUFLOW(2);
507 b = POP() != 0.0;
508 a = POP() != 0.0;
509 PUSH(a || b);
510 return 0;
511 }
512
513 /* Execute "not" op. Return 0 on success else error code. */
514 static int do_not(t1cCtx h) {
515 int a;
516 CHKUFLOW(1);
517 a = POP() == 0.0;
518 PUSH(a);
519 return 0;
520 }
521
522 /* Select registry item. Return NULL on invalid selector. */
523 static float *selRegItem(t1cCtx h, int reg, int *size) {
524 switch (reg) {
525 case T1_REG_WV:
526 *size = T1_MAX_MASTERS;
527 return h->aux->WV;
528 case T1_REG_NDV:
529 *size = T1_MAX_AXES;
530 return h->aux->NDV;
531 case T1_REG_UDV:
532 *size = T1_MAX_AXES;
533 return h->aux->UDV;
534 }
535 return NULL;
536 }
537
538 /* Execute "store" op. Return 0 on success else error code. */
539 static int do_store(t1cCtx h) {
540 int size;
541 int count;
542 int i;
543 int j;
544 int reg;
545 float *array;
546 int temp_length;
547 int temp_size;
548
549 CHKUFLOW(4);
550
551 count = (int)POP();
552 i = (int)POP();
553 j = (int)POP();
554 reg = (int)POP();
555 array = selRegItem(h, reg, &size);
556
557 temp_length = i + count + 1;
558 temp_size = j + count + 1;
559 if (array == NULL || count < 0 ||
560 i < 0 || temp_length < 0 || temp_length >= TX_BCA_LENGTH ||
561 j < 0 || temp_size < 0 || temp_size >= size)
562 return t1cErrStoreBounds;
563
564 memcpy(&array[j], &h->BCA[i], sizeof(float) * count);
565 return 0;
566 }
567
568 /* Execute "abs" op. Return 0 on success else error code. */
569 static int do_abs(t1cCtx h) {
570 float a;
571 CHKUFLOW(1);
572 a = POP();
573 PUSH((a < 0.0) ? -a : a);
574 return 0;
575 }
576
577 /* Execute "add" op. Return 0 on success else error code. */
578 static int do_add(t1cCtx h) {
579 float b;
580 float a;
581 CHKUFLOW(2);
582 b = POP();
583 a = POP();
584 PUSH(a + b);
585 return 0;
586 }
587
588 /* Execute "sub" op. Return 0 on success else error code. */
589 static int do_sub(t1cCtx h) {
590 float b;
591 float a;
592 CHKUFLOW(2);
593 b = POP();
594 a = POP();
595 PUSH(a - b);
596 return 0;
597 }
598
599 /* Execute "load" op. Return 0 on success else error code. */
600 static int do_load(t1cCtx h) {
601 int size;
602 int count;
603 int i;
604 int reg;
605 float *array;
606
607 CHKUFLOW(3);
608
609 count = (int)POP();
610 i = (int)POP();
611 reg = (int)POP();
612 array = selRegItem(h, reg, &size);
613
614 if (array == NULL || count < 0 ||
615 i < 0 || i + count - 1 >= TX_BCA_LENGTH || count > size)
616 return t1cErrLoadBounds;
617
618 memcpy(&h->BCA[i], array, sizeof(float) * count);
619
620 return 0;
621 }
622
623 /* Execute "neg" op. Return 0 on success else error code. */
624 static int do_neg(t1cCtx h) {
625 float a;
626 CHKUFLOW(1);
627 a = POP();
628 PUSH(-a);
629 return 0;
630 }
631
632 /* Execute "eq" op. Return 0 on success else error code. */
633 static int do_eq(t1cCtx h) {
634 float b;
635 float a;
636 CHKUFLOW(2);
637 b = POP();
638 a = POP();
639 PUSH(a == b);
640 return 0;
641 }
642
643 /* Execute "put" op. Return 0 on success else error code. */
644 static int do_put(t1cCtx h) {
645 int i;
646 CHKUFLOW(2);
647 i = (int)POP();
648 if (i < 0 || i >= TX_BCA_LENGTH)
649 return t1cErrPutBounds;
650 h->BCA[i] = POP();
651 return 0;
652 }
653
654 /* Execute "get" op. Return 0 on success else error code. */
655 static int do_get(t1cCtx h) {
656 int i;
657 CHKUFLOW(1);
658 i = (int)POP();
659 if (i < 0 || i >= TX_BCA_LENGTH)
660 return t1cErrGetBounds;
661 PUSH(h->BCA[i]);
662 return 0;
663 }
664
665 /* Execute "ifelse" op. Return 0 on success else error code. */
666 static int do_ifelse(t1cCtx h) {
667 float v2;
668 float v1;
669 float s2;
670 float s1;
671 CHKUFLOW(4);
672 v2 = POP();
673 v1 = POP();
674 s2 = POP();
675 s1 = POP();
676 PUSH((v1 <= v2) ? s1 : s2);
677 return 0;
678 }
679
680 /* Execute "random" op. Return 0 on success else error code. */
681 static int do_random(t1cCtx h) {
682 CHKOFLOW(1);
683 PUSH(((float)rand() + 1) / ((float)RAND_MAX + 1));
684 return 0;
685 }
686
687 /* Execute "mul" op. Return 0 on success else error code. */
688 static int do_mul(t1cCtx h) {
689 float b;
690 float a;
691 CHKUFLOW(2);
692 b = POP();
693 a = POP();
694 PUSH(a * b);
695 return 0;
696 }
697
698 /* Execute "dup" op. Return 0 on success else error code. */
699 static int do_dup(t1cCtx h) {
700 float a;
701 CHKUFLOW(1);
702 a = POP();
703 CHKOFLOW(2);
704 PUSH(a);
705 PUSH(a);
706 return 0;
707 }
708
709 /* Execute "exch" op. Return 0 on success else error code. */
710 static int do_exch(t1cCtx h) {
711 float b;
712 float a;
713 CHKUFLOW(2);
714 b = POP();
715 a = POP();
716 PUSH(b);
717 PUSH(a);
718 return 0;
719 }
720
721 /* Reverse stack elements between index i and j (i < j). */
722 static void reverse(t1cCtx h, int i, int j) {
723 while (i < j) {
724 float tmp = h->stack.array[i];
725 h->stack.array[i++] = h->stack.array[j];
726 h->stack.array[j--] = tmp;
727 }
728 }
729
730 /* Callback h/vstem3 stems. */
731 static int callbackStem3(t1cCtx h, int flags) {
732 CHKUFLOW(6);
733 callbackStem(h, flags, INDEX(0), INDEX(1));
734 callbackStem(h, flags, INDEX(2), INDEX(3));
735 callbackStem(h, flags, INDEX(4), INDEX(5));
736 return 0;
737 }
738
739 /* Parse seac component glyph. */
740 static int parseSeacComponent(t1cCtx h, int stdcode) {
741 long offset;
742
743 if (stdcode < 0 || stdcode > 255 ||
744 (offset = h->aux->getStdEncGlyphOffset(h->aux->ctx, stdcode)) == -1)
745 return t1cErrBadSeacComp;
746
747 h->stack.cnt = 0;
748 return t1Decode(h, offset);
749 }
750
751 /* Execute "div" op. Return 0 on success else error code. */
752 static int do_div(t1cCtx h) {
753 float b;
754 float a;
755
756 CHKUFLOW(2);
757 b = POP();
758 a = POP();
759 PUSH(a / b);
760
761 return 0;
762 }
763
764 /* Callback stems of one direction in group. Return index of start of next
765 group. */
766 static int addPartialGroup(t1cCtx h, int flags, int i) {
767 float edge1 = 0;
768 for (;;) {
769 float edge0 = edge1 + h->cntr.array[i--];
770 float width = h->cntr.array[i--];
771 edge1 = edge0 + width;
772
773 if (width < 0) {
774 /* End of group */
775 callbackStem(h, flags, edge1, -width);
776 break;
777 } else
778 callbackStem(h, flags, edge0, width);
779 }
780 return i;
781 }
782
783 /* Add counter control list (othersubrs 12/13). Return 1 on error else 0. */
784 static int addCntrCntl(t1cCtx h, int last, int argcnt) {
785 int i;
786 int j;
787 int nHoriz;
788 int iHoriz;
789 int nVert;
790 int iVert;
791
792 if (h->stack.cnt != argcnt ||
793 h->cntr.cnt + argcnt > (int)ARRAY_LEN(h->cntr.array))
794 return 1;
795
796 /* Copy stack into argument list reversing order */
797 for (i = h->stack.cnt - 1; i >= 0; i--)
798 h->cntr.array[h->cntr.cnt++] = INDEX(i);
799
800 if (!last)
801 return 0; /* More counter ops to follow */
802
803 if (h->cntr.cnt < 2 || h->cntr.cnt & 1)
804 return 1;
805
806 /* Search backwards for start of vertical stems (if any) */
807 i = h->cntr.cnt - 1;
808 nHoriz = (int)h->cntr.array[i];
809 iHoriz = i - 1;
810 for (j = 0; j < nHoriz; j++)
811 do {
812 i -= 2;
813 if (i < 1)
814 return 1;
815 } while (h->cntr.array[i] >= 0);
816 nVert = (int)h->cntr.array[--i];
817 iVert = i - 1;
818
819 /* Validate vertical groups */
820 for (j = 0; j < nVert; j++)
821 do {
822 i -= 2;
823 if (i < 0)
824 return 1;
825 } while (h->cntr.array[i] >= 0);
826
827 if (nHoriz == 0 && nVert == 0)
828 h->glyph->genop(h->glyph, 0, NULL, t2_cntroff);
829 else
830 do {
831 h->flags |= NEW_GROUP;
832 if (nHoriz-- > 0)
833 iHoriz = addPartialGroup(h, ABF_CNTR_STEM, iHoriz);
834 if (nVert-- > 0)
835 iVert = addPartialGroup(h, ABF_CNTR_STEM | ABF_VERT_STEM, iVert);
836 } while (nHoriz > 0 || nVert > 0);
837
838 return 0;
839 }
840
841 /* Execute "blend" op. Return 0 on success else error code. */
842 static int do_blend(t1cCtx h, int nBlends) {
843 int i;
844 int nElements = nBlends * h->aux->nMasters;
845 int iBase = h->stack.cnt - nElements;
846 int k = iBase + nBlends;
847
848 if (h->aux->nMasters <= 1)
849 return t1cErrInvalidWV;
850 CHKUFLOW(nElements);
851
852 for (i = 0; i < nBlends; i++) {
853 int j;
854 float x = INDEX(iBase + i);
855 for (j = 1; j < h->aux->nMasters; j++)
856 x += INDEX(k++) * h->aux->WV[j];
857 INDEX(iBase + i) = x;
858 }
859
860 h->stack.cnt = iBase + nBlends;
861
862 h->flags |= SEEN_BLEND;
863 return 0;
864 }
865
866 /* Decode type 1 charstring. Called recursively. Return 1 on endchar else 0. */
867 static int t1Decode(t1cCtx h, long offset) {
868 unsigned char *end;
869 unsigned char *next;
870
871 /* Fetch charstring */
872 if (srcSeek(h, offset))
873 return t1cErrSrcStream;
874 next = refill(h, &end);
875 if (next == NULL)
876 return t1cErrSrcStream;
877
878 for (;;) {
879 int result;
880 int byte0;
881 CHKBYTE(h);
882 byte0 = *next++;
883 switch (byte0) {
884 case tx_hstem:
885 CHKUFLOW(2);
886 callbackStem(h, 0, INDEX(0), INDEX(1));
887 break;
888 case tx_vstem:
889 CHKUFLOW(2);
890 callbackStem(h, ABF_VERT_STEM, INDEX(0), INDEX(1));
891 break;
892 case tx_vmoveto:
893 CHKUFLOW(1);
894 if (h->flags & (long)IN_FLEX) {
895 CHKFLEX(2);
896 PUSHFLEX(0);
897 PUSHFLEX(INDEX(0));
898 } else
899 callbackMove(h, 0, INDEX(0));
900 break;
901 case tx_rlineto:
902 CHKUFLOW(2);
903 callbackLine(h, INDEX(0), INDEX(1));
904 break;
905 case tx_hlineto:
906 case tx_vlineto:
907 CHKUFLOW(1);
908 {
909 int horz = byte0 == tx_hlineto;
910 if (horz & 1)
911 callbackLine(h, INDEX(0), 0);
912 else
913 callbackLine(h, 0, INDEX(0));
914 }
915 break;
916 case tx_rrcurveto:
917 CHKUFLOW(6);
918 callbackCurve(h,
919 INDEX(0), INDEX(1),
920 INDEX(2), INDEX(3),
921 INDEX(4), INDEX(5));
922 break;
923 case t1_closepath:
924 h->flags |= PEND_MOVETO;
925 break;
926 case tx_callsubr:
927 CHKUFLOW(1);
928 {
929 long saveoff = h->src.offset - (end - next);
930 long num = (long)POP();
931
932 if (num < 0 || num >= h->aux->subrs.cnt)
933 return t1cErrCallsubr;
934
935 h->subrDepth++;
936 if (h->subrDepth > TX_MAX_SUBR_DEPTH) {
937 printf("subr depth: %d\n", h->subrDepth);
938 return t1cErrSubrDepth;
939 }
940 result = t1Decode(h, h->aux->subrs.offset[num]);
941 h->subrDepth--;
942
943 if (result || h->flags & SEEN_ENDCHAR)
944 return result;
945 else if (srcSeek(h, saveoff))
946 return t1cErrSrcStream;
947 next = refill(h, &end);
948 if (next == NULL)
949 return t1cErrSrcStream;
950 }
951 continue;
952 case tx_return:
953 return 0;
954 case tx_escape: {
955 /* Process escaped operator */
956 int escop;
957 CHKBYTE(h);
958 escop = tx_ESC(*next++);
959 switch (escop) {
960 case tx_dotsection:
961 if (!(h->aux->flags & T1C_UPDATE_OPS ||
962 h->flags & PEND_MOVETO ||
963 h->glyph->stem == NULL))
964 callbackOp(h, tx_dotsection);
965 break;
966 case tx_and:
967 result = do_and(h);
968 if (result)
969 return result;
970 continue;
971 case tx_or:
972 result = do_or(h);
973 if (result)
974 return result;
975 continue;
976 case tx_not:
977 result = do_not(h);
978 if (result)
979 return result;
980 continue;
981 case t1_store:
982 result = do_store(h);
983 if (result)
984 return result;
985 continue;
986 case tx_abs:
987 result = do_abs(h);
988 if (result)
989 return result;
990 continue;
991 case tx_add:
992 result = do_add(h);
993 if (result)
994 return result;
995 continue;
996 case tx_sub:
997 result = do_sub(h);
998 if (result)
999 return result;
1000 continue;
1001 case t1_load:
1002 result = do_load(h);
1003 if (result)
1004 return result;
1005 continue;
1006 case tx_neg:
1007 result = do_neg(h);
1008 if (result)
1009 return result;
1010 continue;
1011 case tx_eq:
1012 result = do_eq(h);
1013 if (result)
1014 return result;
1015 continue;
1016 case tx_drop:
1017 CHKUFLOW(1);
1018 h->stack.cnt--;
1019 continue;
1020 case tx_put:
1021 result = do_put(h);
1022 if (result)
1023 return result;
1024 continue;
1025 case tx_get:
1026 result = do_get(h);
1027 if (result)
1028 return result;
1029 continue;
1030 case tx_ifelse:
1031 result = do_ifelse(h);
1032 if (result)
1033 return result;
1034 continue;
1035 case tx_random:
1036 result = do_random(h);
1037 if (result)
1038 return result;
1039 continue;
1040 case tx_mul:
1041 result = do_mul(h);
1042 if (result)
1043 return result;
1044 continue;
1045 case tx_sqrt:
1046 CHKUFLOW(1);
1047 {
1048 float a = POP();
1049 if (a < 0.0)
1050 return t1cErrSqrtDomain;
1051 PUSH(sqrt(a));
1052 }
1053 continue;
1054 case tx_dup:
1055 result = do_dup(h);
1056 if (result)
1057 return result;
1058 continue;
1059 case tx_exch:
1060 result = do_exch(h);
1061 if (result)
1062 return result;
1063 continue;
1064 case tx_index:
1065 CHKUFLOW(1);
1066 {
1067 int i = (int)POP();
1068 if (i < 0)
1069 i = 0; /* Duplicate top element */
1070 if (i >= h->stack.cnt)
1071 return t1cErrIndexBounds;
1072 {
1073 float t = h->stack.array[h->stack.cnt - 1 - i];
1074 PUSH(t);
1075 }
1076 }
1077 continue;
1078 case tx_roll:
1079 CHKUFLOW(2);
1080 {
1081 int j = (int)POP();
1082 int n = (int)POP();
1083 int iTop = h->stack.cnt - 1;
1084 int iBottom = h->stack.cnt - n;
1085
1086 if (n <= 0 || iBottom < 0)
1087 return t1cErrRollBounds;
1088
1089 /* Constrain j to [0,n) */
1090 if (j < 0)
1091 j = n - (-j % n);
1092 j %= n;
1093
1094 reverse(h, iTop - j + 1, iTop);
1095 reverse(h, iBottom, iTop - j);
1096 reverse(h, iBottom, iTop);
1097 }
1098 continue;
1099 case t1_vstem3:
1100 result = callbackStem3(h, ABF_STEM3_STEM | ABF_VERT_STEM);
1101 if (result)
1102 return result;
1103 break;
1104 case t1_hstem3:
1105 result = callbackStem3(h, ABF_STEM3_STEM);
1106 if (result)
1107 return result;
1108 break;
1109 case t1_seac:
1110 CHKUFLOW(5);
1111
1112 /* Save component character codes */
1113 h->aux->bchar = (unsigned char)INDEX(3);
1114 h->aux->achar = (unsigned char)INDEX(4);
1115
1116 if (h->aux->flags & T1C_UPDATE_OPS) {
1117 h->seac.asb_x = INDEX(0);
1118 h->seac.asb_y = 0;
1119 h->seac.adx = INDEX(1);
1120 h->seac.ady = INDEX(2);
1121
1122 h->seac.xshift = h->seac.adx + h->lsb.x - h->seac.asb_x;
1123
1124 /* Parse base component */
1125 h->seac.phase = seacBase;
1126 result = parseSeacComponent(h, h->aux->bchar);
1127 if (result)
1128 return result;
1129
1130 h->flags &= ~SEEN_ENDCHAR;
1131 h->flags |= PEND_HINTSUBS;
1132
1133 /* Parse accent component */
1134 h->seac.phase = seacAccentPreMove;
1135 result = parseSeacComponent(h, h->aux->achar);
1136 if (result)
1137 return result;
1138 } else {
1139 INDEX(1) += -INDEX(0) + h->lsb.x;
1140 callback_seac(h, INDEX(1), INDEX(2),
1141 h->aux->bchar, h->aux->achar);
1142 }
1143 return 0;
1144 case t1_sbw:
1145 if (h->seac.phase == seacNone) {
1146 CHKUFLOW(4);
1147 h->x = h->lsb.x = INDEX(0);
1148 h->y = h->lsb.y = INDEX(1);
1149 if (callbackWidth(h, INDEX(2)))
1150 return t1cSuccess;
1151 } else if (h->seac.phase == seacAccentPreMove) {
1152 /* shift == (seac.adx + seac.bsb) - accent component hsb. path.x is now equal to accent hsb.*/
1153 /* We should now set the current path ' h->path.x' to (path.x + (( h->lsb.x + h->seac.adx) - accent hsb))
1154 Since path.x is now == to h->seac.asb, this reduces to setting ' h->path.x' to (h->seac.bsb + h->seac.adx) */
1155 CHKUFLOW(4);
1156 h->x = INDEX(0); /* h->x is now the accent hsb. */
1157 h->y = INDEX(1);
1158 } else if (h->seac.phase == seacBase) {
1159 CHKUFLOW(4);
1160 h->x = h->seac.bsb_x = INDEX(0);
1161 h->y = h->seac.bsb_y = INDEX(1);
1162 }
1163 h->x = (float)RND_ON_READ(h->x);
1164 h->y = (float)RND_ON_READ(h->y);
1165 break;
1166 case tx_div:
1167 result = do_div(h);
1168 if (result)
1169 return result;
1170 continue;
1171 case t1_callother:
1172 /* Process othersubr */
1173 CHKUFLOW(2);
1174 {
1175 int othersubr = (int)POP();
1176 int argcnt = (int)POP();
1177 switch (othersubr) {
1178 case t1_otherFlex:
1179 CHKUFLOW(3);
1180 if (h->flex.cnt != 14)
1181 return t1cErrFlex;
1182 PUSHFLEX(INDEX(0));
1183 PUSHFLEX(INDEX(1));
1184 PUSHFLEX(INDEX(2));
1185 callbackFlex(h);
1186 break;
1187 case t1_otherPreflex1:
1188 h->flex.cnt = 0;
1189 h->flags |= IN_FLEX;
1190 continue;
1191 case t1_otherPreflex2:
1192 continue; /* Discard */
1193 case t1_otherHintSubs:
1194 h->flags |= (PEND_HINTSUBS | SEEN_HINTSUBS);
1195 continue;
1196 case t1_otherGlobalColor:
1197 callbackOp(h, t2_cntron);
1198 break;
1199 case t1_otherCntr1:
1200 if (addCntrCntl(h, 0, argcnt))
1201 return t1cErrBadCntrCntl;
1202 break;
1203 case t1_otherCntr2:
1204 if (addCntrCntl(h, 1, argcnt))
1205 return t1cErrBadCntrCntl;
1206 break;
1207 case t1_otherBlend1:
1208 result = do_blend(h, 1);
1209 if (result)
1210 return result;
1211 continue;
1212 case t1_otherBlend2:
1213 result = do_blend(h, 2);
1214 if (result)
1215 return result;
1216 continue;
1217 case t1_otherBlend3:
1218 result = do_blend(h, 3);
1219 if (result)
1220 return result;
1221 continue;
1222 case t1_otherBlend4:
1223 result = do_blend(h, 4);
1224 if (result)
1225 return result;
1226 continue;
1227 case t1_otherBlend6:
1228 result = do_blend(h, 6);
1229 if (result)
1230 return result;
1231 continue;
1232 case t1_otherAdd:
1233 result = do_add(h);
1234 if (result)
1235 return result;
1236 continue;
1237 case t1_otherSub:
1238 result = do_sub(h);
1239 if (result)
1240 return result;
1241 continue;
1242 case t1_otherMul:
1243 result = do_mul(h);
1244 if (result)
1245 return result;
1246 continue;
1247 case t1_otherDiv:
1248 result = do_div(h);
1249 if (result)
1250 return result;
1251 continue;
1252 case t1_otherPut:
1253 result = do_put(h);
1254 if (result)
1255 return result;
1256 continue;
1257 case t1_otherGet:
1258 result = do_get(h);
1259 if (result)
1260 return result;
1261 continue;
1262 case t1_otherIfelse:
1263 result = do_ifelse(h);
1264 if (result)
1265 return result;
1266 continue;
1267 case t1_otherRandom:
1268 result = do_random(h);
1269 if (result)
1270 return result;
1271 continue;
1272 case t1_otherDup:
1273 result = do_dup(h);
1274 if (result)
1275 return result;
1276 continue;
1277 case t1_otherExch:
1278 result = do_exch(h);
1279 if (result)
1280 return result;
1281 continue;
1282 case t1_otherStoreWV: {
1283 int iBase = (int)INDEX(0);
1284 if (iBase < 0 || iBase + h->aux->nMasters > TX_BCA_LENGTH)
1285 return t1cErrStoreWVBounds;
1286 memcpy(&h->BCA[iBase], h->aux->WV,
1287 h->aux->nMasters * sizeof(h->aux->WV[0]));
1288 } break;
1289 case t1_otherReserved4:
1290 case t1_otherReserved7:
1291 case t1_otherReserved8:
1292 case t1_otherReserved9:
1293 case t1_otherReserved10:
1294 case t1_otherReserved11:
1295 case t1_otherReserved26:
1296 default:
1297 return t1cErrInvalidOp;
1298 } /* End: switch(othersubr) */
1299 } /* End: case t1_callother: */
1300 break;
1301 case t1_pop:
1302 continue; /* Discard */
1303 case t1_div2:
1304 result = do_div(h);
1305 if (result)
1306 return result;
1307 continue;
1308 case t1_setcurrentpt:
1309 if (h->flags & IN_FLEX)
1310 h->flags &= ~IN_FLEX;
1311 else {
1312 /* Use illegal setcurrentpt operator (Apple driver)
1313 to set the current point */
1314 CHKUFLOW(2);
1315 h->x = INDEX(0);
1316 h->y = INDEX(1);
1317 h->x = (float)RND_ON_READ(h->x);
1318 h->y = (float)RND_ON_READ(h->y);
1319 }
1320 break;
1321 default:
1322 return t1cErrInvalidOp;
1323 } /* End: switch (escop) */
1324 } /* End: case tx_escape: */
1325 break;
1326
1327 case t1_hsbw:
1328 if (h->seac.phase == seacNone) {
1329 CHKUFLOW(2);
1330 h->x = h->lsb.x = INDEX(0);
1331 h->y = h->lsb.y = 0;
1332 if (callbackWidth(h, INDEX(1)))
1333 return t1cSuccess;
1334 } else if (h->seac.phase == seacAccentPreMove) {
1335 /* shift == (seac.adx + composite glyph hsb) - accent component hsb.
1336 path.x is now equal to accent hsb. */
1337 /* We should now set the current path
1338 'h->path.x' to (path.x + ((h->lsb.x + h->seac.adx) - accent hsb))
1339 Since path.x is now == to h->seac.asb, this reduces to setting
1340 'h->path.x' to (h->seac.bsb + h->seac.adx) */
1341 CHKUFLOW(2);
1342 h->x = INDEX(0); /* h->x is now the accent hsb. */
1343 h->y = 0;
1344 } else if (h->seac.phase == seacBase) {
1345 CHKUFLOW(2);
1346 h->x = h->seac.bsb_x = INDEX(0);
1347 h->y = h->seac.bsb_y = 0;
1348 }
1349 h->x = (float)RND_ON_READ(h->x);
1350 h->y = (float)RND_ON_READ(h->y);
1351 break;
1352 case tx_endchar:
1353 h->flags |= SEEN_ENDCHAR;
1354 return t1cSuccess;
1355 case t1_moveto:
1356 /* Deprecated absolute moveto; convert to rmoveto */
1357 CHKUFLOW(2);
1358 INDEX(0) -= h->x;
1359 INDEX(1) -= h->y;
1360 /* Fall through */
1361 case tx_rmoveto:
1362 CHKUFLOW(2);
1363 if (h->flags & (long)IN_FLEX) {
1364 CHKFLEX(2);
1365 PUSHFLEX(INDEX(0));
1366 PUSHFLEX(INDEX(1));
1367 } else
1368 callbackMove(h, INDEX(0), INDEX(1));
1369 break;
1370 case tx_hmoveto:
1371 CHKUFLOW(1);
1372 if (h->flags & (long)IN_FLEX) {
1373 CHKFLEX(2);
1374 PUSHFLEX(INDEX(0));
1375 PUSHFLEX(0);
1376 } else
1377 callbackMove(h, INDEX(0), 0);
1378 break;
1379 case tx_vhcurveto:
1380 CHKUFLOW(4);
1381 callbackCurve(h,
1382 0, INDEX(0),
1383 INDEX(1), INDEX(2),
1384 INDEX(3), 0);
1385 break;
1386 case tx_hvcurveto:
1387 CHKUFLOW(4);
1388 callbackCurve(h,
1389 INDEX(0), 0,
1390 INDEX(1), INDEX(2),
1391 0, INDEX(3));
1392 break;
1393 default:
1394 /* 1 byte number */
1395 CHKOFLOW(1);
1396 PUSH(byte0 - 139);
1397 continue;
1398 case 247:
1399 case 248:
1400 case 249:
1401 case 250:
1402 /* Positive 2 byte number */
1403 CHKOFLOW(1);
1404 CHKBYTE(h);
1405 PUSH(108 + 256 * (byte0 - 247) + *next++);
1406 continue;
1407 case 251:
1408 case 252:
1409 case 253:
1410 case 254:
1411 /* Negative 2 byte number */
1412 CHKOFLOW(1);
1413 CHKBYTE(h);
1414 PUSH(-108 - 256 * (byte0 - 251) - *next++);
1415 continue;
1416 case 255:
1417 /* 5 byte number */
1418 CHKOFLOW(1);
1419 {
1420 long value;
1421 CHKBYTE(h);
1422 value = *next++;
1423 CHKBYTE(h);
1424 value = value << 8 | *next++;
1425 CHKBYTE(h);
1426 value = value << 8 | *next++;
1427 CHKBYTE(h);
1428 value = value << 8 | *next++;
1429 #if LONG_MAX > 2147483647
1430 /* long greater that 4 bytes; handle negative range */
1431 if (value > 2417483647)
1432 value -= 4294967296;
1433 #endif
1434 PUSH(value);
1435 }
1436 continue;
1437 } /* End: switch (byte0) */
1438 h->stack.cnt = 0; /* Clear stack */
1439 } /* End: while (cstr < end) */
1440 }
1441
1442 /* Parse Type 1 charstring. */
1443 int t1cParse(long offset, t1cAuxData *aux, abfGlyphCallbacks *glyph) {
1444 struct t1cCtx h = {0};
1445 int retVal;
1446
1447 /* Initialize */
1448 h.flags = PEND_MOVETO;
1449 h.stack.cnt = 0;
1450 h.x = h.lsb.x = 0;
1451 h.y = h.lsb.y = 0;
1452 h.le_start.useLEStart = 0;
1453 h.le_start.x = 0;
1454 h.le_start.y = 0;
1455 h.cntr.cnt = 0;
1456 h.seac.phase = seacNone;
1457 h.aux = aux;
1458 h.glyph = glyph;
1459 h.maxOpStack = T1_MAX_OP_STACK;
1460
1461 aux->bchar = 0;
1462 aux->achar = 0;
1463
1464 if (aux->flags & T1C_USE_MATRIX) {
1465 if ((fabs(1 - aux->matrix[0]) > 0.0001) ||
1466 (fabs(1 - aux->matrix[3]) > 0.0001) ||
1467 (aux->matrix[1] != 0) ||
1468 (aux->matrix[2] != 0) ||
1469 (aux->matrix[4] != 0) ||
1470 (aux->matrix[5] != 0))
1471
1472 {
1473 int i;
1474 h.flags |= USE_MATRIX;
1475 h.flags |= USE_GLOBAL_MATRIX;
1476 for (i = 0; i < 6; i++) {
1477 h.transformMatrix[i] = aux->matrix[i];
1478 }
1479 }
1480 }
1481
1482 retVal = t1Decode(&h, offset);
1483
1484 // Clear transform flag,
1485 if (h.flags & USE_MATRIX)
1486 h.flags &= ~USE_MATRIX;
1487
1488 return retVal;
1489 }
1490
1491 /* ------------------------- Charstring Decryption ------------------------- */
1492
1493 /* Decrypt Type 1 charstring. */
1494 int t1cDecrypt(int lenIV, long *length, char *cipher, char *plain) {
1495 if (lenIV < 0 || lenIV > *length)
1496 /* Invalid lenIV */
1497 return t1cErrBadLenIV;
1498 else {
1499 /* Encrypted */
1500 unsigned char *c = (unsigned char *)cipher;
1501 unsigned char *p = (unsigned char *)plain;
1502 unsigned short r = 4330; /* Initial state */
1503 long cnt = *length -= lenIV;
1504
1505 /* Prime state from random initial bytes */
1506 while (lenIV--)
1507 r = (*c++ + r) * 52845 + 22719;
1508
1509 /* Decrypt and copy bytes */
1510 while (cnt--) {
1511 unsigned char c1 = *c++;
1512 *p++ = c1 ^ (r >> 8);
1513 r = (c1 + r) * 52845 + 22719;
1514 }
1515 }
1516 return t1cSuccess;
1517 }
1518
1519 /* Decrypt protected (doubly-encrypted CJK fonts) charstring. */
1520 int t1cUnprotect(int lenIV, long *length, char *cipher, char *plain) {
1521 if (lenIV < 0 || lenIV > *length)
1522 /* Invalid lenIV */
1523 return t1cErrBadLenIV;
1524 else {
1525 /* Doubly-encrypted */
1526 unsigned char single;
1527 unsigned char *c = (unsigned char *)cipher;
1528 unsigned char *p = (unsigned char *)plain;
1529 unsigned short r1 = 4330; /* Single decryption key */
1530 unsigned short r2 = 54261; /* Double decryption key */
1531 long cnt = *length -= lenIV;
1532
1533 /* Prime state from random initial bytes */
1534 while (lenIV--) {
1535 single = *c ^ (r2 >> 8);
1536 r2 = (*c++ + r2) * 16477 + 21483;
1537 r1 = (single + r1) * 52845 + 22719;
1538 }
1539
1540 /* Double decrypt and copy bytes */
1541 while (cnt--) {
1542 unsigned char c1 = *c++;
1543 single = c1 ^ (r2 >> 8);
1544 r2 = (c1 + r2) * 16477 + 21483;
1545 *p++ = single ^ (r1 >> 8);
1546 r1 = (single + r1) * 52845 + 22719;
1547 }
1548 }
1549 return t1cSuccess;
1550 }
1551
1552 /* Get version numbers of libraries. */
1553 void t1cGetVersion(ctlVersionCallbacks *cb) {
1554 if (cb->called & 1 << T1C_LIB_ID)
1555 return; /* Already enumerated */
1556
1557 /* This library */
1558 cb->getversion(cb, T1C_VERSION, "t1cstr");
1559
1560 /* Record this call */
1561 cb->called |= 1 << T1C_LIB_ID;
1562 }
1563
1564 /* Map error code to error string. */
1565 char *t1cErrStr(int err_code) {
1566 static char *errstrs[] =
1567 {
1568 #undef CTL_DCL_ERR
1569 #define CTL_DCL_ERR(name, string) string,
1570 #include "t1cerr.h"
1571 };
1572 return (err_code < 0 || err_code >= (int)ARRAY_LEN(errstrs)) ? "unknown error" : errstrs[err_code];
1573 }
1574
1575 /* ----------------------------- Debug Support ----------------------------- */
1576
1577 #if T1C_DEBUG
1578
1579 #include <stdio.h>
1580
1581 static void dbt1cstack(t1cCtx h) {
1582 printf("--- stack\n");
1583 if (h->stack.cnt == 0)
1584 printf("empty\n");
1585 else {
1586 int i;
1587 for (i = 0; i < h->stack.cnt; i++)
1588 printf("[%d]=%g ", i, h->stack.array[i]);
1589 printf("\n");
1590 }
1591 }
1592
1593 /* This function just serves to suppress annoying "defined but not used"
1594 compiler messages when debugging */
1595 static void CTL_CDECL dbuse(int arg, ...) {
1596 dbuse(0, dbt1cstack);
1597 }
1598
1599 #endif /* T1C_DEBUG */