oficina / estilo Public

Clone
git clone https://amadan.net/oficina/estilo

Plain git — no account needed to clone.

Download

Download this file

1package typeface
2
3import (
4 "bytes"
5 "encoding/binary"
6 "errors"
7 "fmt"
8 "testing"
9)
10
11// An sfnt whose directory points several tables at the same bytes passes
12// a per-table bounds check, yet a reader that copies each table (an
13// app renaming the font for its PDF, say) copies those bytes once per
14// table: a 10 MiB file became 4.8 GB to copy (final review, C1). Tables
15// may not overlap, nor sum past the file.
16
17// pointedAt is data with the directory entries of the given tags set to
18// the offset and length of the table at.
19func pointedAt(t *testing.T, data []byte, at string, tags ...string) []byte {
20 t.Helper()
21 out := bytes.Clone(data)
22 n := int(binary.BigEndian.Uint16(out[4:]))
23 var off, length uint32
24 found := false
25 for i := 0; i < n; i++ {
26 rec := out[12+16*i:]
27 if string(rec[:4]) == at {
28 off, length, found = binary.BigEndian.Uint32(rec[8:]), binary.BigEndian.Uint32(rec[12:]), true
29 }
30 }
31 if !found {
32 t.Fatalf("no %q table", at)
33 }
34 for i := 0; i < n; i++ {
35 rec := out[12+16*i:]
36 for _, tag := range tags {
37 if string(rec[:4]) == tag {
38 binary.BigEndian.PutUint32(rec[8:], off)
39 binary.BigEndian.PutUint32(rec[12:], length)
40 }
41 }
42 }
43 return out
44}
45
46// withFillers is Liberation plus n small tables named z000, z001, ...
47func withFillers(t *testing.T, n int) ([]byte, []string) {
48 t.Helper()
49 v, ts := tablesOf(t, liberation(t))
50 var tags []string
51 for i := 0; i < n; i++ {
52 tag := fmt.Sprintf("z%03d", i)
53 tags = append(tags, tag)
54 ts = append(ts, table{tag, []byte{0, 0, 0, 0}})
55 }
56 return buildSFNT(v, ts), tags
57}
58
59func TestInspectRefusesTablesThatShareBytes(t *testing.T) {
60 // One table over another: the sum still fits the file.
61 two := pointedAt(t, liberation(t), "hhea", "gasp")
62
63 // A hundred tables over glyf, the amplification itself.
64 many, tags := withFillers(t, 100)
65 many = pointedAt(t, many, "glyf", tags...)
66
67 // A table inside the table directory.
68 inDir := bytes.Clone(liberation(t))
69 binary.BigEndian.PutUint32(inDir[12+8:], 0) // the first table at offset 0
70
71 // WOFF: two identical tables whose compressed bytes are one range.
72 v, ts := tablesOf(t, liberation(t))
73 same := bytes.Repeat([]byte("same"), 64)
74 woff := buildWOFF(t, buildSFNT(v, append(ts, table{"zzz1", same}, table{"zzz2", same})))
75 {
76 n := int(binary.BigEndian.Uint16(woff[12:]))
77 var off []byte
78 for i := 0; i < n; i++ {
79 rec := woff[44+20*i:]
80 switch string(rec[:4]) {
81 case "zzz1":
82 off = bytes.Clone(rec[4:8])
83 case "zzz2":
84 copy(rec[4:8], off)
85 }
86 }
87 }
88
89 for _, c := range []struct {
90 name string
91 data []byte
92 }{
93 {"two tables over one range", two},
94 {"a hundred tables over glyf", many},
95 {"a table over the directory", inDir},
96 {"a WOFF with two tables over one range", woff},
97 } {
98 t.Run(c.name, func(t *testing.T) {
99 f, err := Inspect(c.data)
100 if !errors.Is(err, ErrMalformed) {
101 t.Fatalf("Inspect = %+v, %v; want ErrMalformed", f, err)
102 }
103 t.Log(err)
104 })
105 }
106
107 // Tables that only touch, and empty ones, are fine.
108 touching, _ := withFillers(t, 3)
109 mustInspect(t, touching)
110 mustInspect(t, buildSFNT(v, append(ts, table{"zero", nil})))
111}
112