/
cmap_parser.go
579 lines (533 loc) · 12.1 KB
/
cmap_parser.go
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
package cmap
import (
"errors"
"fmt"
"io"
"github.com/nareshrajaram/unipdf/common"
)
// parse parses the CMap file and loads into the CMap structure.
func (cmap *CMap) parse() error {
var prev cmapObject
for {
o, err := cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
common.Log.Debug("ERROR: parsing CMap: %v", err)
return err
}
switch t := o.(type) {
case cmapOperand:
op := t
switch op.Operand {
case begincodespacerange:
err := cmap.parseCodespaceRange()
if err != nil {
return err
}
case begincidrange:
err := cmap.parseCIDRange()
if err != nil {
return err
}
case beginbfchar:
err := cmap.parseBfchar()
if err != nil {
return err
}
case beginbfrange:
err := cmap.parseBfrange()
if err != nil {
return err
}
case usecmap:
if prev == nil {
common.Log.Debug("ERROR: usecmap with no arg")
return ErrBadCMap
}
name, ok := prev.(cmapName)
if !ok {
common.Log.Debug("ERROR: usecmap arg not a name %#v", prev)
return ErrBadCMap
}
cmap.usecmap = name.Name
case cidSystemInfo:
// Some PDF generators leave the "/"" off CIDSystemInfo
// e.g. ~/testdata/459474_809.pdf
err := cmap.parseSystemInfo()
if err != nil {
return err
}
}
case cmapName:
n := t
switch n.Name {
case cidSystemInfo:
err := cmap.parseSystemInfo()
if err != nil {
return err
}
case cmapname:
err := cmap.parseName()
if err != nil {
return err
}
case cmaptype:
err := cmap.parseType()
if err != nil {
return err
}
case cmapversion:
err := cmap.parseVersion()
if err != nil {
return err
}
}
}
prev = o
}
return nil
}
// parseName parses a cmap name and adds it to `cmap`.
// cmap names are defined like this: /CMapName /83pv-RKSJ-H def
func (cmap *CMap) parseName() error {
name := ""
done := false
for i := 0; i < 10 && !done; i++ {
o, err := cmap.parseObject()
if err != nil {
return err
}
switch t := o.(type) {
case cmapOperand:
switch t.Operand {
case "def":
done = true
default:
// This is not an error because some PDF files don't have valid PostScript names.
// e.g. ~/testdata/Papercut vs Equitrac.pdf
// /CMapName /Adobe-SI-*Courier New-6164-0 def
// We just append the non-existant operator "New-6164-0" to the name
common.Log.Debug("parseName: State error. o=%#v name=%#q", o, name)
if name != "" {
name = fmt.Sprintf("%s %s", name, t.Operand)
}
common.Log.Debug("parseName: Recovered. name=%#q", name)
}
case cmapName:
name = t.Name
}
}
if !done {
common.Log.Debug("ERROR: parseName: No def ")
return ErrBadCMap
}
cmap.name = name
return nil
}
// parseType parses a cmap type and adds it to `cmap`.
// cmap names are defined like this: /CMapType 1 def
func (cmap *CMap) parseType() error {
ctype := 0
done := false
for i := 0; i < 3 && !done; i++ {
o, err := cmap.parseObject()
if err != nil {
return err
}
switch t := o.(type) {
case cmapOperand:
switch t.Operand {
case "def":
done = true
default:
common.Log.Error("parseType: state error. o=%#v", o)
return ErrBadCMap
}
case cmapInt:
ctype = int(t.val)
}
}
cmap.ctype = ctype
return nil
}
// parseVersion parses a cmap version and adds it to `cmap`.
// cmap names are defined like this: /CMapType 1 def
// We don't need the version. We do this to eat up the version code in the cmap definition
// to reduce unhandled parse object warnings.
func (cmap *CMap) parseVersion() error {
version := ""
done := false
for i := 0; i < 3 && !done; i++ {
o, err := cmap.parseObject()
if err != nil {
return err
}
switch t := o.(type) {
case cmapOperand:
switch t.Operand {
case "def":
done = true
default:
common.Log.Debug("ERROR: parseVersion: state error. o=%#v", o)
return ErrBadCMap
}
case cmapInt:
version = fmt.Sprintf("%d", t.val)
case cmapFloat:
version = fmt.Sprintf("%f", t.val)
case cmapString:
version = t.String
default:
common.Log.Debug("ERROR: parseVersion: Bad type. o=%#v", o)
}
}
cmap.version = version
return nil
}
// parseSystemInfo parses a cmap CIDSystemInfo and adds it to `cmap`.
// cmap CIDSystemInfo is define like this:
// /CIDSystemInfo 3 dict dup begin
// /Registry (Adobe) def
// /Ordering (Japan1) def
// /Supplement 1 def
// end def
func (cmap *CMap) parseSystemInfo() error {
inDict := false
inDef := false
name := ""
done := false
systemInfo := CIDSystemInfo{}
// 50 is a generous but arbitrary limit to prevent an endless loop on badly formed cmap files.
for i := 0; i < 50 && !done; i++ {
o, err := cmap.parseObject()
if err != nil {
return err
}
switch t := o.(type) {
case cmapDict:
d := t.Dict
r, ok := d["Registry"]
if !ok {
common.Log.Debug("ERROR: Bad System Info")
return ErrBadCMap
}
rr, ok := r.(cmapString)
if !ok {
common.Log.Debug("ERROR: Bad System Info")
return ErrBadCMap
}
systemInfo.Registry = rr.String
r, ok = d["Ordering"]
if !ok {
common.Log.Debug("ERROR: Bad System Info")
return ErrBadCMap
}
rr, ok = r.(cmapString)
if !ok {
common.Log.Debug("ERROR: Bad System Info")
return ErrBadCMap
}
systemInfo.Ordering = rr.String
s, ok := d["Supplement"]
if !ok {
common.Log.Debug("ERROR: Bad System Info")
return ErrBadCMap
}
ss, ok := s.(cmapInt)
if !ok {
common.Log.Debug("ERROR: Bad System Info")
return ErrBadCMap
}
systemInfo.Supplement = int(ss.val)
done = true
case cmapOperand:
switch t.Operand {
case "begin":
inDict = true
case "end":
done = true
case "def":
inDef = false
}
case cmapName:
if inDict {
name = t.Name
inDef = true
}
case cmapString:
if inDef {
switch name {
case "Registry":
systemInfo.Registry = t.String
case "Ordering":
systemInfo.Ordering = t.String
}
}
case cmapInt:
if inDef {
switch name {
case "Supplement":
systemInfo.Supplement = int(t.val)
}
}
}
}
if !done {
common.Log.Debug("ERROR: Parsed System Info dict incorrectly")
return ErrBadCMap
}
cmap.systemInfo = systemInfo
return nil
}
// parseCodespaceRange parses the codespace range section of a CMap.
func (cmap *CMap) parseCodespaceRange() error {
for {
o, err := cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
hexLow, ok := o.(cmapHexString)
if !ok {
if op, isOperand := o.(cmapOperand); isOperand {
if op.Operand == endcodespacerange {
return nil
}
return errors.New("unexpected operand")
}
}
o, err = cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
hexHigh, ok := o.(cmapHexString)
if !ok {
return errors.New("non-hex high")
}
if len(hexLow.b) != len(hexHigh.b) {
return errors.New("unequal number of bytes in range")
}
low := hexToCharCode(hexLow)
high := hexToCharCode(hexHigh)
if high < low {
common.Log.Debug("ERROR: Bad codespace. low=0x%02x high=0x%02x", low, high)
return ErrBadCMap
}
numBytes := hexHigh.numBytes
cspace := Codespace{NumBytes: numBytes, Low: low, High: high}
cmap.codespaces = append(cmap.codespaces, cspace)
common.Log.Trace("Codespace low: 0x%X, high: 0x%X", low, high)
}
if len(cmap.codespaces) == 0 {
common.Log.Debug("ERROR: No codespaces in cmap.")
return ErrBadCMap
}
return nil
}
// parseCIDRange parses the CID range section of a CMap.
func (cmap *CMap) parseCIDRange() error {
for {
// Parse character code interval start.
o, err := cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
hexStart, ok := o.(cmapHexString)
if !ok {
if op, isOperand := o.(cmapOperand); isOperand {
if op.Operand == endcidrange {
return nil
}
return errors.New("cid interval start must be a hex string")
}
}
charcodeStart := hexToCharCode(hexStart)
// Parse character code interval end.
o, err = cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
hexEnd, ok := o.(cmapHexString)
if !ok {
return errors.New("cid interval end must be a hex string")
}
if len(hexStart.b) != len(hexEnd.b) {
return errors.New("unequal number of bytes in range")
}
charcodeEnd := hexToCharCode(hexEnd)
if charcodeStart > charcodeEnd {
common.Log.Debug("ERROR: invalid CID range. start=0x%02x end=0x%02x", charcodeStart, charcodeEnd)
return ErrBadCMap
}
// Parse interval start CID.
o, err = cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
cidStart, ok := o.(cmapInt)
if !ok {
return errors.New("cid start value must be an decimal number")
}
if cidStart.val < 0 {
return errors.New("invalid cid start value")
}
// Fill charcode to CID map.
cid := cidStart.val
for charcode := charcodeStart; charcode <= charcodeEnd; charcode++ {
cmap.codeToCID[charcode] = CharCode(cid)
cid++
}
common.Log.Trace("CID range: <0x%X> <0x%X> %d", charcodeStart, charcodeEnd, cidStart.val)
}
return nil
}
// parseBfchar parses a bfchar section of a CMap file.
func (cmap *CMap) parseBfchar() error {
for {
// Src code.
o, err := cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
var code CharCode
switch v := o.(type) {
case cmapOperand:
if v.Operand == endbfchar {
return nil
}
return errors.New("unexpected operand")
case cmapHexString:
code = hexToCharCode(v)
default:
return errors.New("unexpected type")
}
// Target code.
o, err = cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
var target rune
switch v := o.(type) {
case cmapOperand:
if v.Operand == endbfchar {
return nil
}
common.Log.Debug("ERROR: Unexpected operand. %#v", v)
return ErrBadCMap
case cmapHexString:
target = hexToRune(v)
case cmapName:
common.Log.Debug("ERROR: Unexpected name. %#v", v)
target = MissingCodeRune
default:
common.Log.Debug("ERROR: Unexpected type. %#v", o)
return ErrBadCMap
}
cmap.codeToUnicode[code] = target
}
return nil
}
// parseBfrange parses a bfrange section of a CMap file.
func (cmap *CMap) parseBfrange() error {
for {
// The specifications are in triplets.
// <srcCodeFrom> <srcCodeTo> <target>
// where target can be either <destFrom> as a hex code, or a list.
// Src code from.
var srcCodeFrom CharCode
o, err := cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
switch v := o.(type) {
case cmapOperand:
if v.Operand == endbfrange {
return nil
}
return errors.New("unexpected operand")
case cmapHexString:
srcCodeFrom = hexToCharCode(v)
default:
return errors.New("unexpected type")
}
// Src code to.
var srcCodeTo CharCode
o, err = cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
switch v := o.(type) {
case cmapOperand:
common.Log.Debug("ERROR: Imcomplete bfrange triplet")
return ErrBadCMap
case cmapHexString:
srcCodeTo = hexToCharCode(v)
default:
common.Log.Debug("ERROR: Unexpected type %T", o)
return ErrBadCMap
}
// target(s).
o, err = cmap.parseObject()
if err != nil {
if err == io.EOF {
break
}
return err
}
switch v := o.(type) {
case cmapArray:
if len(v.Array) != int(srcCodeTo-srcCodeFrom)+1 {
common.Log.Debug("ERROR: Invalid number of items in array")
return ErrBadCMap
}
for code := srcCodeFrom; code <= srcCodeTo; code++ {
o := v.Array[code-srcCodeFrom]
hexs, ok := o.(cmapHexString)
if !ok {
return errors.New("non-hex string in array")
}
r := hexToRune(hexs)
cmap.codeToUnicode[code] = r
}
case cmapHexString:
// <codeFrom> <codeTo> <dst>, maps [from,to] to [dst,dst+to-from].
r := hexToRune(v)
for code := srcCodeFrom; code <= srcCodeTo; code++ {
cmap.codeToUnicode[code] = r
r++
}
default:
common.Log.Debug("ERROR: Unexpected type %T", o)
return ErrBadCMap
}
}
return nil
}