| 1 | package fonts |
| 2 | |
| 3 | import ( |
| 4 | "encoding/binary" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "runtime" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/slides/internal/slides/fonts/fontstest" |
| 11 | ) |
| 12 | |
| 13 | // Renaming copies every table into a new file for the PDF, so a file |
| 14 | // whose tables share bytes, or whose name records share one string, |
| 15 | // grew past anything its size allows: a 10 MiB upload became 4.8 GB to |
| 16 | // copy, in Go, before the PDF module's own limits (final review, C1). |
| 17 | // The copy is never more than twice the file. |
| 18 | |
| 19 | // sharedTables is the serif face plus n tables over its glyf. |
| 20 | func sharedTables(t *testing.T, n int) []byte { |
| 21 | t.Helper() |
| 22 | data := serifBytes() |
| 23 | for i := 0; i < n; i++ { |
| 24 | data = fontstest.WithTable(t, data, fmt.Sprintf("z%03d", i), []byte{0, 0, 0, 0}) |
| 25 | } |
| 26 | count := int(binary.BigEndian.Uint16(data[4:])) |
| 27 | var off, length uint32 |
| 28 | for i := 0; i < count; i++ { |
| 29 | rec := data[12+16*i:] |
| 30 | if string(rec[:4]) == "glyf" { |
| 31 | off, length = binary.BigEndian.Uint32(rec[8:]), binary.BigEndian.Uint32(rec[12:]) |
| 32 | } |
| 33 | } |
| 34 | for i := 0; i < count; i++ { |
| 35 | rec := data[12+16*i:] |
| 36 | if rec[0] == 'z' { |
| 37 | binary.BigEndian.PutUint32(rec[8:], off) |
| 38 | binary.BigEndian.PutUint32(rec[12:], length) |
| 39 | } |
| 40 | } |
| 41 | return data |
| 42 | } |
| 43 | |
| 44 | // sharedNames is a name table of n PostScript-name records, each in its |
| 45 | // own language, all pointing at one string of size bytes. |
| 46 | func sharedNames(n, size int) []byte { |
| 47 | b := make([]byte, 6+12*n) |
| 48 | binary.BigEndian.PutUint16(b[2:], uint16(n)) |
| 49 | binary.BigEndian.PutUint16(b[4:], uint16(len(b))) |
| 50 | for i := 0; i < n; i++ { |
| 51 | at := 6 + 12*i |
| 52 | for j, v := range []uint16{3, 1, uint16(i + 1), 6, uint16(size), 0} { |
| 53 | binary.BigEndian.PutUint16(b[at+2*j:], v) |
| 54 | } |
| 55 | } |
| 56 | return append(b, make([]byte, size)...) |
| 57 | } |
| 58 | |
| 59 | func TestRenamingIsBoundedByTheFile(t *testing.T) { |
| 60 | for _, c := range []struct { |
| 61 | name string |
| 62 | data []byte |
| 63 | refused bool // too large even once the names are bounded |
| 64 | }{ |
| 65 | {"a hundred tables over glyf", sharedTables(t, 100), true}, |
| 66 | {"a thousand PostScript names over one string", fontstest.WithTable(t, serifBytes(), "name", sharedNames(1000, 30000)), false}, |
| 67 | } { |
| 68 | t.Run(c.name, func(t *testing.T) { |
| 69 | f, err := Parse(c.data) |
| 70 | if err != nil { |
| 71 | t.Fatalf("Parse: %v", err) |
| 72 | } |
| 73 | var before, after runtime.MemStats |
| 74 | runtime.ReadMemStats(&before) |
| 75 | out, err := f.Renamed("Upload", 400, false) |
| 76 | runtime.ReadMemStats(&after) |
| 77 | if alloc := after.TotalAlloc - before.TotalAlloc; alloc > uint64(3*len(c.data)) { |
| 78 | t.Errorf("renaming %d bytes allocated %d", len(c.data), alloc) |
| 79 | } |
| 80 | if c.refused { |
| 81 | if !errors.Is(err, errTooLarge) { |
| 82 | t.Fatalf("Renamed = %d bytes, %v; want errTooLarge", len(out), err) |
| 83 | } |
| 84 | return |
| 85 | } |
| 86 | if err != nil { |
| 87 | t.Fatal(err) |
| 88 | } |
| 89 | if len(out) > 2*len(c.data) { |
| 90 | t.Fatalf("renaming %d bytes made %d", len(c.data), len(out)) |
| 91 | } |
| 92 | renamed, err := Parse(out) |
| 93 | if err != nil { |
| 94 | t.Fatal(err) |
| 95 | } |
| 96 | if n := len(renamed.tables["name"]); n > 6+12*maxNameRecords+maxNameStorage { |
| 97 | t.Errorf("the renamed name table is %d bytes", n) |
| 98 | } |
| 99 | if names := nameStrings(t, renamed); len(names[6]) == 0 || len(names[1]) != 1 || names[1][0] != "Upload" { |
| 100 | t.Errorf("PostScript names %d, families %q; want a PostScript name and Upload", len(names[6]), names[1]) |
| 101 | } |
| 102 | }) |
| 103 | } |
| 104 | } |
| 105 | |