1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
|
// Copyright (c) 2010-2017 The OTS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// A parser for the Type 2 Charstring Format.
// http://www.adobe.com/devnet/font/pdfs/5177.Type2.pdf
#include "cff_charstring.h"
#include <climits>
#include <cstdio>
#include <cstring>
#include <stack>
#include <string>
#include <utility>
#define TABLE_NAME "CFF"
namespace {
// Type 2 Charstring Implementation Limits. See Appendix. B in Adobe Technical
// Note #5177.
const int32_t kMaxSubrsCount = 65536;
const size_t kMaxCharStringLength = 65535;
const size_t kMaxNumberOfStemHints = 96;
const size_t kMaxSubrNesting = 10;
// |dummy_result| should be a huge positive integer so callsubr and callgsubr
// will fail with the dummy value.
const int32_t dummy_result = INT_MAX;
bool ExecuteCharString(ots::OpenTypeCFF& cff,
size_t call_depth,
const ots::CFFIndex& global_subrs_index,
const ots::CFFIndex& local_subrs_index,
ots::Buffer *cff_table,
ots::Buffer *char_string,
std::stack<int32_t> *argument_stack,
ots::CharStringContext& cs_ctx);
bool ArgumentStackOverflows(std::stack<int32_t> *argument_stack, bool cff2) {
if ((cff2 && argument_stack->size() > ots::kMaxCFF2ArgumentStack) ||
(!cff2 && argument_stack->size() > ots::kMaxCFF1ArgumentStack)) {
return true;
}
return false;
}
#ifdef DUMP_T2CHARSTRING
// Converts |op| to a string and returns it.
const char *CharStringOperatorToString(ots::CharStringOperator op) {
switch (op) {
case ots::kHStem:
return "hstem";
case ots::kVStem:
return "vstem";
case ots::kVMoveTo:
return "vmoveto";
case ots::kRLineTo:
return "rlineto";
case ots::kHLineTo:
return "hlineto";
case ots::kVLineTo:
return "vlineto";
case ots::kRRCurveTo:
return "rrcurveto";
case ots::kCallSubr:
return "callsubr";
case ots::kReturn:
return "return";
case ots::kEndChar:
return "endchar";
case ots::kVSIndex:
return "vsindex";
case ots::kBlend:
return "blend";
case ots::kHStemHm:
return "hstemhm";
case ots::kHintMask:
return "hintmask";
case ots::kCntrMask:
return "cntrmask";
case ots::kRMoveTo:
return "rmoveto";
case ots::kHMoveTo:
return "hmoveto";
case ots::kVStemHm:
return "vstemhm";
case ots::kRCurveLine:
return "rcurveline";
case ots::kRLineCurve:
return "rlinecurve";
case ots::kVVCurveTo:
return "VVCurveTo";
case ots::kHHCurveTo:
return "hhcurveto";
case ots::kCallGSubr:
return "callgsubr";
case ots::kVHCurveTo:
return "vhcurveto";
case ots::kHVCurveTo:
return "HVCurveTo";
case ots::kDotSection:
return "dotsection";
case ots::kAnd:
return "and";
case ots::kOr:
return "or";
case ots::kNot:
return "not";
case ots::kAbs:
return "abs";
case ots::kAdd:
return "add";
case ots::kSub:
return "sub";
case ots::kDiv:
return "div";
case ots::kNeg:
return "neg";
case ots::kEq:
return "eq";
case ots::kDrop:
return "drop";
case ots::kPut:
return "put";
case ots::kGet:
return "get";
case ots::kIfElse:
return "ifelse";
case ots::kRandom:
return "random";
case ots::kMul:
return "mul";
case ots::kSqrt:
return "sqrt";
case ots::kDup:
return "dup";
case ots::kExch:
return "exch";
case ots::kIndex:
return "index";
case ots::kRoll:
return "roll";
case ots::kHFlex:
return "hflex";
case ots::kFlex:
return "flex";
case ots::kHFlex1:
return "hflex1";
case ots::kFlex1:
return "flex1";
}
return "UNKNOWN";
}
#endif
// Read one or more bytes from the |char_string| buffer and stores the number
// read on |out_number|. If the number read is an operator (ex 'vstem'), sets
// true on |out_is_operator|. Returns true if the function read a number.
bool ReadNextNumberFromCharString(ots::Buffer *char_string,
int32_t *out_number,
bool *out_is_operator) {
uint8_t v = 0;
if (!char_string->ReadU8(&v)) {
return OTS_FAILURE();
}
*out_is_operator = false;
// The conversion algorithm is described in Adobe Technical Note #5177, page
// 13, Table 1.
if (v <= 11) {
*out_number = v;
*out_is_operator = true;
} else if (v == 12) {
uint16_t result = (v << 8);
if (!char_string->ReadU8(&v)) {
return OTS_FAILURE();
}
result += v;
*out_number = result;
*out_is_operator = true;
} else if (v <= 27) {
// Special handling for v==19 and v==20 are implemented in
// ExecuteCharStringOperator().
*out_number = v;
*out_is_operator = true;
} else if (v == 28) {
if (!char_string->ReadU8(&v)) {
return OTS_FAILURE();
}
uint16_t result = (v << 8);
if (!char_string->ReadU8(&v)) {
return OTS_FAILURE();
}
result += v;
*out_number = result;
} else if (v <= 31) {
*out_number = v;
*out_is_operator = true;
} else if (v <= 246) {
*out_number = static_cast<int32_t>(v) - 139;
} else if (v <= 250) {
uint8_t w = 0;
if (!char_string->ReadU8(&w)) {
return OTS_FAILURE();
}
*out_number = ((static_cast<int32_t>(v) - 247) * 256) +
static_cast<int32_t>(w) + 108;
} else if (v <= 254) {
uint8_t w = 0;
if (!char_string->ReadU8(&w)) {
return OTS_FAILURE();
}
*out_number = -((static_cast<int32_t>(v) - 251) * 256) -
static_cast<int32_t>(w) - 108;
} else if (v == 255) {
// TODO(yusukes): We should not skip the 4 bytes. Note that when v is 255,
// we should treat the following 4-bytes as a 16.16 fixed-point number
// rather than 32bit signed int.
if (!char_string->Skip(4)) {
return OTS_FAILURE();
}
*out_number = dummy_result;
} else {
return OTS_FAILURE();
}
return true;
}
bool ValidCFF2Operator(int32_t op) {
switch (op) {
case ots::kReturn:
case ots::kEndChar:
case ots::kAbs:
case ots::kAdd:
case ots::kSub:
case ots::kDiv:
case ots::kNeg:
case ots::kRandom:
case ots::kMul:
case ots::kSqrt:
case ots::kDrop:
case ots::kExch:
case ots::kIndex:
case ots::kRoll:
case ots::kDup:
case ots::kPut:
case ots::kGet:
case ots::kDotSection:
case ots::kAnd:
case ots::kOr:
case ots::kNot:
case ots::kEq:
case ots::kIfElse:
return false;
}
return true;
}
// Executes |op| and updates |argument_stack|. Returns true if the execution
// succeeds. If the |op| is kCallSubr or kCallGSubr, the function recursively
// calls ExecuteCharString() function. The |cs_ctx| argument holds values that
// need to persist through these calls (see CharStringContext for details)
bool ExecuteCharStringOperator(ots::OpenTypeCFF& cff,
int32_t op,
size_t call_depth,
const ots::CFFIndex& global_subrs_index,
const ots::CFFIndex& local_subrs_index,
ots::Buffer *cff_table,
ots::Buffer *char_string,
std::stack<int32_t> *argument_stack,
ots::CharStringContext& cs_ctx) {
ots::Font* font = cff.GetFont();
const size_t stack_size = argument_stack->size();
if (cs_ctx.cff2 && !ValidCFF2Operator(op)) {
return OTS_FAILURE();
}
switch (op) {
case ots::kCallSubr:
case ots::kCallGSubr: {
const ots::CFFIndex& subrs_index =
(op == ots::kCallSubr ? local_subrs_index : global_subrs_index);
if (stack_size < 1) {
return OTS_FAILURE();
}
int32_t subr_number = argument_stack->top();
argument_stack->pop();
if (subr_number == dummy_result) {
// For safety, we allow subr calls only with immediate subr numbers for
// now. For example, we allow "123 callgsubr", but does not allow "100 12
// add callgsubr". Please note that arithmetic and conditional operators
// always push the |dummy_result| in this implementation.
return OTS_FAILURE();
}
// See Adobe Technical Note #5176 (CFF), "16. Local/GlobalSubrs INDEXes."
int32_t bias = 32768;
if (subrs_index.count < 1240) {
bias = 107;
} else if (subrs_index.count < 33900) {
bias = 1131;
}
subr_number += bias;
// Sanity checks of |subr_number|.
if (subr_number < 0) {
return OTS_FAILURE();
}
if (subr_number >= kMaxSubrsCount) {
return OTS_FAILURE();
}
if (subrs_index.offsets.size() <= static_cast<size_t>(subr_number + 1)) {
return OTS_FAILURE(); // The number is out-of-bounds.
}
// Prepare ots::Buffer where we're going to jump.
const size_t length =
subrs_index.offsets[subr_number + 1] - subrs_index.offsets[subr_number];
if (length > kMaxCharStringLength) {
return OTS_FAILURE();
}
const size_t offset = subrs_index.offsets[subr_number];
cff_table->set_offset(offset);
if (!cff_table->Skip(length)) {
return OTS_FAILURE();
}
ots::Buffer char_string_to_jump(cff_table->buffer() + offset, length);
return ExecuteCharString(cff,
call_depth + 1,
global_subrs_index,
local_subrs_index,
cff_table,
&char_string_to_jump,
argument_stack,
cs_ctx);
}
case ots::kReturn:
return true;
case ots::kEndChar:
cs_ctx.endchar_seen = true;
cs_ctx.width_seen = true; // just in case.
return true;
case ots::kVSIndex: {
if (!cs_ctx.cff2) {
return OTS_FAILURE();
}
if (stack_size != 1) {
return OTS_FAILURE();
}
if (cs_ctx.blend_seen || cs_ctx.vsindex_seen) {
return OTS_FAILURE();
}
if (argument_stack->top() < 0 ||
argument_stack->top() >= (int32_t)cff.region_index_count.size()) {
return OTS_FAILURE();
}
cs_ctx.vsindex_seen = true;
cs_ctx.vsindex = argument_stack->top();
while (!argument_stack->empty())
argument_stack->pop();
return true;
}
case ots::kBlend: {
if (!cs_ctx.cff2) {
return OTS_FAILURE();
}
if (stack_size < 1) {
return OTS_FAILURE();
}
if (cs_ctx.vsindex >= (int32_t)cff.region_index_count.size()) {
return OTS_FAILURE();
}
uint16_t k = cff.region_index_count.at(cs_ctx.vsindex);
uint16_t n = argument_stack->top();
if (stack_size < n * (k + 1u) + 1u) {
return OTS_FAILURE();
}
// Keep the 1st n operands on the stack for the next operator to use and
// pop the rest. There can be multiple consecutive blend operators, so this
// makes sure the operands of all of them are kept on the stack.
while (argument_stack->size() > stack_size - ((n * k) + 1))
argument_stack->pop();
cs_ctx.blend_seen = true;
return true;
}
case ots::kHStem:
case ots::kVStem:
case ots::kHStemHm:
case ots::kVStemHm: {
bool successful = false;
if (stack_size < 2) {
return OTS_FAILURE();
}
if ((stack_size % 2) == 0) {
successful = true;
} else if ((!(cs_ctx.width_seen)) && (((stack_size - 1) % 2) == 0)) {
// The -1 is for "width" argument. For details, see Adobe Technical Note
// #5177, page 16, note 4.
successful = true;
}
cs_ctx.num_stems += (stack_size / 2);
if ((cs_ctx.num_stems) > kMaxNumberOfStemHints) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
cs_ctx.width_seen = true; // always set true since "w" might be 0 byte.
return successful ? true : OTS_FAILURE();
}
case ots::kRMoveTo: {
bool successful = false;
if (stack_size == 2) {
successful = true;
} else if ((!(cs_ctx.width_seen)) && (stack_size - 1 == 2)) {
successful = true;
}
while (!argument_stack->empty())
argument_stack->pop();
cs_ctx.width_seen = true;
return successful ? true : OTS_FAILURE();
}
case ots::kVMoveTo:
case ots::kHMoveTo: {
bool successful = false;
if (stack_size == 1) {
successful = true;
} else if ((!(cs_ctx.width_seen)) && (stack_size - 1 == 1)) {
successful = true;
}
while (!argument_stack->empty())
argument_stack->pop();
cs_ctx.width_seen = true;
return successful ? true : OTS_FAILURE();
}
case ots::kHintMask:
case ots::kCntrMask: {
bool successful = false;
if (stack_size == 0) {
successful = true;
} else if ((!(cs_ctx.width_seen)) && (stack_size == 1)) {
// A number for "width" is found.
successful = true;
} else if ((!(cs_ctx.width_seen)) || // in this case, any sizes are ok.
((stack_size % 2) == 0)) {
// The numbers are vstem definition.
// See Adobe Technical Note #5177, page 24, hintmask.
cs_ctx.num_stems += (stack_size / 2);
if ((cs_ctx.num_stems) > kMaxNumberOfStemHints) {
return OTS_FAILURE();
}
successful = true;
}
if (!successful) {
return OTS_FAILURE();
}
if ((cs_ctx.num_stems) == 0) {
return OTS_FAILURE();
}
const size_t mask_bytes = (cs_ctx.num_stems + 7) / 8;
if (!char_string->Skip(mask_bytes)) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
cs_ctx.width_seen = true;
return true;
}
case ots::kRLineTo:
if (!(cs_ctx.width_seen)) {
// The first stack-clearing operator should be one of hstem, hstemhm,
// vstem, vstemhm, cntrmask, hintmask, hmoveto, vmoveto, rmoveto, or
// endchar. For details, see Adobe Technical Note #5177, page 16, note 4.
return OTS_FAILURE();
}
if (stack_size < 2) {
return OTS_FAILURE();
}
if ((stack_size % 2) != 0) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kHLineTo:
case ots::kVLineTo:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size < 1) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kRRCurveTo:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size < 6) {
return OTS_FAILURE();
}
if ((stack_size % 6) != 0) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kRCurveLine:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size < 8) {
return OTS_FAILURE();
}
if (((stack_size - 2) % 6) != 0) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kRLineCurve:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size < 8) {
return OTS_FAILURE();
}
if (((stack_size - 6) % 2) != 0) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kVVCurveTo:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size < 4) {
return OTS_FAILURE();
}
if (((stack_size % 4) != 0) &&
(((stack_size - 1) % 4) != 0)) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kHHCurveTo: {
bool successful = false;
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size < 4) {
return OTS_FAILURE();
}
if ((stack_size % 4) == 0) {
// {dxa dxb dyb dxc}+
successful = true;
} else if (((stack_size - 1) % 4) == 0) {
// dy1? {dxa dxb dyb dxc}+
successful = true;
}
while (!argument_stack->empty())
argument_stack->pop();
return successful ? true : OTS_FAILURE();
}
case ots::kVHCurveTo:
case ots::kHVCurveTo: {
bool successful = false;
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size < 4) {
return OTS_FAILURE();
}
if (((stack_size - 4) % 8) == 0) {
// dx1 dx2 dy2 dy3 {dya dxb dyb dxc dxd dxe dye dyf}*
successful = true;
} else if ((stack_size >= 5) &&
((stack_size - 5) % 8) == 0) {
// dx1 dx2 dy2 dy3 {dya dxb dyb dxc dxd dxe dye dyf}* dxf
successful = true;
} else if ((stack_size >= 8) &&
((stack_size - 8) % 8) == 0) {
// {dxa dxb dyb dyc dyd dxe dye dxf}+
successful = true;
} else if ((stack_size >= 9) &&
((stack_size - 9) % 8) == 0) {
// {dxa dxb dyb dyc dyd dxe dye dxf}+ dyf?
successful = true;
}
while (!argument_stack->empty())
argument_stack->pop();
return successful ? true : OTS_FAILURE();
}
case ots::kDotSection:
// Deprecated operator but harmless, we probably should drop it some how.
if (stack_size != 0) {
return OTS_FAILURE();
}
return true;
case ots::kAnd:
case ots::kOr:
case ots::kEq:
case ots::kAdd:
case ots::kSub:
if (stack_size < 2) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->pop();
argument_stack->push(dummy_result);
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kNot:
case ots::kAbs:
case ots::kNeg:
if (stack_size < 1) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->push(dummy_result);
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kDiv:
// TODO(yusukes): Should detect div-by-zero errors.
if (stack_size < 2) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->pop();
argument_stack->push(dummy_result);
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kDrop:
if (stack_size < 1) {
return OTS_FAILURE();
}
argument_stack->pop();
return true;
case ots::kPut:
case ots::kGet:
case ots::kIndex:
// For now, just call OTS_FAILURE since there is no way to check whether the
// index argument, |i|, is out-of-bounds or not. Fortunately, no OpenType
// fonts I have (except malicious ones!) use the operators.
// TODO(yusukes): Implement them in a secure way.
return OTS_FAILURE();
case ots::kRoll:
// Likewise, just call OTS_FAILURE for kRoll since there is no way to check
// whether |N| is smaller than the current stack depth or not.
// TODO(yusukes): Implement them in a secure way.
return OTS_FAILURE();
case ots::kRandom:
// For now, we don't handle the 'random' operator since the operator makes
// it hard to analyze hinting code statically.
return OTS_FAILURE();
case ots::kIfElse:
if (stack_size < 4) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->pop();
argument_stack->pop();
argument_stack->pop();
argument_stack->push(dummy_result);
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kMul:
// TODO(yusukes): Should detect overflows.
if (stack_size < 2) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->pop();
argument_stack->push(dummy_result);
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kSqrt:
// TODO(yusukes): Should check if the argument is negative.
if (stack_size < 1) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->push(dummy_result);
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kDup:
if (stack_size < 1) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->push(dummy_result);
argument_stack->push(dummy_result);
if (ArgumentStackOverflows(argument_stack, cs_ctx.cff2)) {
return OTS_FAILURE();
}
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kExch:
if (stack_size < 2) {
return OTS_FAILURE();
}
argument_stack->pop();
argument_stack->pop();
argument_stack->push(dummy_result);
argument_stack->push(dummy_result);
// TODO(yusukes): Implement this. We should push a real value for all
// arithmetic and conditional operations.
return true;
case ots::kHFlex:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size != 7) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kFlex:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size != 13) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kHFlex1:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size != 9) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
case ots::kFlex1:
if (!(cs_ctx.width_seen)) {
return OTS_FAILURE();
}
if (stack_size != 11) {
return OTS_FAILURE();
}
while (!argument_stack->empty())
argument_stack->pop();
return true;
}
return OTS_FAILURE_MSG("Undefined operator: %d (0x%x)", op, op);
}
// Executes |char_string| and updates |argument_stack|.
//
// cff: parent OpenTypeCFF reference
// call_depth: The current call depth. Initial value is zero.
// global_subrs_index: Global subroutines.
// local_subrs_index: Local subroutines for the current glyph.
// cff_table: A whole CFF table which contains all global and local subroutines.
// char_string: A charstring we'll execute. |char_string| can be a main routine
// in CharString INDEX, or a subroutine in GlobalSubr/LocalSubr.
// argument_stack: The stack which an operator in |char_string| operates.
// cs_ctx: a CharStringContext holding values to persist across subrs, etc.
// endchar_seen: true is set if |char_string| contains 'endchar'.
// width_seen: true is set if |char_string| contains 'width' byte (which
// is 0 or 1 byte) or if cff2
// num_stems: total number of hstems and vstems processed so far.
// cff2: true if this is a CFF2 table
// blend_seen: initially false; set to true if 'blend' operator encountered.
// vsindex_seen: initially false; set to true if 'vsindex' encountered.
// vsindex: initially = PrivateDICT's vsindex; may be changed by 'vsindex'
// operator in CharString
bool ExecuteCharString(ots::OpenTypeCFF& cff,
size_t call_depth,
const ots::CFFIndex& global_subrs_index,
const ots::CFFIndex& local_subrs_index,
ots::Buffer *cff_table,
ots::Buffer *char_string,
std::stack<int32_t> *argument_stack,
ots::CharStringContext& cs_ctx) {
if (call_depth > kMaxSubrNesting) {
return OTS_FAILURE();
}
cs_ctx.endchar_seen = false;
const size_t length = char_string->length();
while (char_string->offset() < length) {
int32_t operator_or_operand = 0;
bool is_operator = false;
if (!ReadNextNumberFromCharString(char_string,
&operator_or_operand,
&is_operator)) {
return OTS_FAILURE();
}
#ifdef DUMP_T2CHARSTRING
/*
You can dump all operators and operands (except mask bytes for hintmask
and cntrmask) by the following code:
*/
if (!is_operator) {
std::fprintf(stderr, "%d ", operator_or_operand);
} else {
std::fprintf(stderr, "%s\n",
CharStringOperatorToString(
ots::CharStringOperator(operator_or_operand))
);
}
#endif
if (!is_operator) {
argument_stack->push(operator_or_operand);
if (ArgumentStackOverflows(argument_stack, cs_ctx.cff2)) {
return OTS_FAILURE();
}
continue;
}
// An operator is found. Execute it.
if (!ExecuteCharStringOperator(cff,
operator_or_operand,
call_depth,
global_subrs_index,
local_subrs_index,
cff_table,
char_string,
argument_stack,
cs_ctx)) {
return OTS_FAILURE();
}
if (cs_ctx.endchar_seen) {
return true;
}
if (operator_or_operand == ots::kReturn) {
return true;
}
}
// No endchar operator is found (CFF1 only)
if (cs_ctx.cff2)
return true;
return OTS_FAILURE();
}
// Selects a set of subroutines for |glyph_index| from |cff| and sets it on
// |out_local_subrs_to_use|. Returns true on success.
bool SelectLocalSubr(const ots::OpenTypeCFF& cff,
uint16_t glyph_index, // 0-origin
const ots::CFFIndex **out_local_subrs_to_use) {
bool cff2 = (cff.major == 2);
*out_local_subrs_to_use = NULL;
// First, find local subrs from |local_subrs_per_font|.
if ((cff.fd_select.size() > 0) &&
(!cff.local_subrs_per_font.empty())) {
// Look up FDArray index for the glyph.
const auto& iter = cff.fd_select.find(glyph_index);
if (iter == cff.fd_select.end()) {
return OTS_FAILURE();
}
const auto fd_index = iter->second;
if (fd_index >= cff.local_subrs_per_font.size()) {
return OTS_FAILURE();
}
*out_local_subrs_to_use = cff.local_subrs_per_font.at(fd_index);
} else if (cff.local_subrs) {
// Second, try to use |local_subrs|. Most Latin fonts don't have FDSelect
// entries. If The font has a local subrs index associated with the Top
// DICT (not FDArrays), use it.
*out_local_subrs_to_use = cff.local_subrs;
} else if (cff2 && cff.local_subrs_per_font.size() == 1) {
*out_local_subrs_to_use = cff.local_subrs_per_font.at(0);
} else {
// Just return NULL.
*out_local_subrs_to_use = NULL;
}
return true;
}
} // namespace
namespace ots {
bool ValidateCFFCharStrings(
ots::OpenTypeCFF& cff,
const CFFIndex& global_subrs_index,
Buffer* cff_table) {
const CFFIndex& char_strings_index = *(cff.charstrings_index);
if (char_strings_index.offsets.size() == 0) {
return OTS_FAILURE(); // no charstring.
}
// For each glyph, validate the corresponding charstring.
for (unsigned i = 1; i < char_strings_index.offsets.size(); ++i) {
// Prepare a Buffer object, |char_string|, which contains the charstring
// for the |i|-th glyph.
const size_t length =
char_strings_index.offsets[i] - char_strings_index.offsets[i - 1];
if (length > kMaxCharStringLength) {
return OTS_FAILURE();
}
const size_t offset = char_strings_index.offsets[i - 1];
cff_table->set_offset(offset);
if (!cff_table->Skip(length)) {
return OTS_FAILURE();
}
Buffer char_string(cff_table->buffer() + offset, length);
// Get a local subrs for the glyph.
const unsigned glyph_index = i - 1; // index in the map is 0-origin.
const CFFIndex *local_subrs_to_use = NULL;
if (!SelectLocalSubr(cff,
glyph_index,
&local_subrs_to_use)) {
return OTS_FAILURE();
}
// If |local_subrs_to_use| is still NULL, use an empty one.
CFFIndex default_empty_subrs;
if (!local_subrs_to_use){
local_subrs_to_use = &default_empty_subrs;
}
// Check a charstring for the |i|-th glyph.
std::stack<int32_t> argument_stack;
// Context to store values that must persist across subrs, etc.
CharStringContext cs_ctx;
cs_ctx.cff2 = (cff.major == 2);
// CFF2 CharString has no value for width, so we start with true here to
// error out if width is found.
cs_ctx.width_seen = cs_ctx.cff2;
// CFF2 CharStrings' default vsindex comes from the associated PrivateDICT
if (cs_ctx.cff2) {
const auto& iter = cff.fd_select.find(glyph_index);
auto fd_index = 0;
if (iter != cff.fd_select.end()) {
fd_index = iter->second;
}
if (fd_index >= (int32_t)cff.vsindex_per_font.size()) {
// shouldn't get this far with a font in this condition, but just in case
return OTS_FAILURE(); // fd_index out-of-range
}
cs_ctx.vsindex = cff.vsindex_per_font.at(fd_index);
}
#ifdef DUMP_T2CHARSTRING
fprintf(stderr, "\n---- CharString %*d ----\n", 5, glyph_index);
#endif
if (!ExecuteCharString(cff,
0 /* initial call_depth is zero */,
global_subrs_index, *local_subrs_to_use,
cff_table, &char_string, &argument_stack,
cs_ctx)) {
return OTS_FAILURE();
}
if (!cs_ctx.cff2 && !cs_ctx.endchar_seen) {
return OTS_FAILURE();
}
}
return true;
}
} // namespace ots
#undef TABLE_NAME
|