| 1 | package typeface |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "compress/zlib" |
| 6 | "encoding/binary" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | |
| 10 | "github.com/andybalholm/brotli" |
| 11 | ) |
| 12 | |
| 13 | // WOFF and WOFF2 unwrapping, as far as Inspect needs it: the table |
| 14 | // directory, and the bytes of the tables it reads. Neither rebuilds the |
| 15 | // sfnt. |
| 16 | |
| 17 | // readWOFF reads a WOFF 1.0 file (zlib per table). Every table must lie |
| 18 | // inside the file and inflate to exactly the length the directory gives |
| 19 | // it (or be stored, when its packed and unpacked lengths are equal); only |
| 20 | // the tables Inspect reads are kept. |
| 21 | func readWOFF(data []byte) (fontTables, error) { |
| 22 | const headerLen, entryLen = 44, 20 |
| 23 | if len(data) < headerLen { |
| 24 | return fontTables{}, malformed("WOFF header cut short") |
| 25 | } |
| 26 | flavor := binary.BigEndian.Uint32(data[4:]) |
| 27 | if err := checkFlavor(flavor); err != nil { |
| 28 | return fontTables{}, err |
| 29 | } |
| 30 | if int64(binary.BigEndian.Uint32(data[8:])) != int64(len(data)) { |
| 31 | return fontTables{}, malformed("WOFF length is not the file's") |
| 32 | } |
| 33 | n := int(binary.BigEndian.Uint16(data[12:])) |
| 34 | if n == 0 { |
| 35 | return fontTables{}, malformed("no tables") |
| 36 | } |
| 37 | if headerLen+entryLen*n > len(data) { |
| 38 | return fontTables{}, malformed("WOFF table directory cut short: %d tables", n) |
| 39 | } |
| 40 | ft := fontTables{version: flavor, has: map[string]bool{}, data: map[string][]byte{}} |
| 41 | spans := make([]span, 0, n) |
| 42 | for i := 0; i < n; i++ { |
| 43 | e := data[headerLen+entryLen*i:] |
| 44 | spans = append(spans, span{string(e[:4]), uint64(binary.BigEndian.Uint32(e[4:])), uint64(binary.BigEndian.Uint32(e[8:]))}) |
| 45 | } |
| 46 | for _, s := range spans { |
| 47 | if s.off+s.length > uint64(len(data)) { |
| 48 | return fontTables{}, malformed("table %q runs past the end of the file", s.tag) |
| 49 | } |
| 50 | } |
| 51 | if err := disjoint(spans, uint64(headerLen+entryLen*n), uint64(len(data))); err != nil { |
| 52 | return fontTables{}, err |
| 53 | } |
| 54 | var unpacked uint64 |
| 55 | for i := 0; i < n; i++ { |
| 56 | e := data[headerLen+entryLen*i:] |
| 57 | tag := string(e[:4]) |
| 58 | off := uint64(binary.BigEndian.Uint32(e[4:])) |
| 59 | compLen := uint64(binary.BigEndian.Uint32(e[8:])) |
| 60 | origLen := uint64(binary.BigEndian.Uint32(e[12:])) |
| 61 | if off+compLen > uint64(len(data)) { |
| 62 | return fontTables{}, malformed("table %q runs past the end of the file", tag) |
| 63 | } |
| 64 | if compLen > origLen { |
| 65 | return fontTables{}, malformed("table %q is bigger packed than unpacked", tag) |
| 66 | } |
| 67 | if unpacked += origLen; unpacked > maxUnpacked { |
| 68 | return fontTables{}, fmt.Errorf("%w: unpacks to over %d bytes", ErrTooBig, maxUnpacked) |
| 69 | } |
| 70 | if ft.has[tag] { |
| 71 | return fontTables{}, malformed("table %q appears twice", tag) |
| 72 | } |
| 73 | ft.has[tag] = true |
| 74 | packed := data[off : off+compLen] |
| 75 | if compLen == origLen { |
| 76 | if wanted(tag) { |
| 77 | ft.data[tag] = packed // stored, not compressed |
| 78 | } |
| 79 | continue |
| 80 | } |
| 81 | b, err := inflate(packed, int(origLen), wanted(tag)) |
| 82 | if err != nil { |
| 83 | return fontTables{}, malformed("table %q: %v", tag, err) |
| 84 | } |
| 85 | if wanted(tag) { |
| 86 | ft.data[tag] = b |
| 87 | } |
| 88 | } |
| 89 | return ft, nil |
| 90 | } |
| 91 | |
| 92 | // inflate unpacks a zlib stream that must hold exactly n bytes, returning |
| 93 | // them if keep is set and otherwise only checking them. |
| 94 | func inflate(packed []byte, n int, keep bool) ([]byte, error) { |
| 95 | r, err := zlib.NewReader(bytes.NewReader(packed)) |
| 96 | if err != nil { |
| 97 | return nil, err |
| 98 | } |
| 99 | defer r.Close() |
| 100 | if keep { |
| 101 | return readExactly(r, n) |
| 102 | } |
| 103 | got, err := io.Copy(io.Discard, io.LimitReader(r, int64(n)+1)) |
| 104 | if err != nil { |
| 105 | return nil, err |
| 106 | } |
| 107 | if got != int64(n) { |
| 108 | return nil, fmt.Errorf("unpacks to %d bytes, not %d", got, n) |
| 109 | } |
| 110 | return nil, nil |
| 111 | } |
| 112 | |
| 113 | // readExactly reads n bytes from r and checks that r then ends cleanly. |
| 114 | // It reads no more than n+1 bytes whatever r holds. |
| 115 | func readExactly(r io.Reader, n int) ([]byte, error) { |
| 116 | b, err := io.ReadAll(io.LimitReader(r, int64(n)+1)) |
| 117 | if err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | if len(b) > n { |
| 121 | return nil, fmt.Errorf("unpacks to more than the %d bytes its tables hold", n) |
| 122 | } |
| 123 | if len(b) < n { |
| 124 | return nil, fmt.Errorf("unpacks to %d bytes, not %d", len(b), n) |
| 125 | } |
| 126 | return b, nil |
| 127 | } |
| 128 | |
| 129 | // woff2Tags is WOFF2's known-tag table: a directory entry's flags name a |
| 130 | // tag by its index here, or 63 for a tag written out in full. |
| 131 | var woff2Tags = [63]string{ |
| 132 | "cmap", "head", "hhea", "hmtx", "maxp", "name", "OS/2", "post", |
| 133 | "cvt ", "fpgm", "glyf", "loca", "prep", "CFF ", "VORG", "EBDT", |
| 134 | "EBLC", "gasp", "hdmx", "kern", "LTSH", "PCLT", "VDMX", "vhea", |
| 135 | "vmtx", "BASE", "GDEF", "GPOS", "GSUB", "EBSC", "JSTF", "MATH", |
| 136 | "CBDT", "CBLC", "COLR", "CPAL", "SVG ", "sbix", "acnt", "avar", |
| 137 | "bdat", "bloc", "bsln", "cvar", "fdsc", "feat", "fmtx", "fvar", |
| 138 | "gvar", "hsty", "just", "lcar", "mort", "morx", "opbd", "prop", |
| 139 | "trak", "Zapf", "Silf", "Glat", "Gloc", "Feat", "Sill", |
| 140 | } |
| 141 | |
| 142 | // readWOFF2 reads a WOFF2 file: the table directory, then the one brotli |
| 143 | // stream holding every table end to end. The stream must unpack to |
| 144 | // exactly the tables' total length, and that total may not pass |
| 145 | // maxUnpacked. Only glyf, loca and hmtx may be transformed; their |
| 146 | // transformed bytes are not rebuilt, so the outline checks skip them. Any |
| 147 | // other table Inspect reads that says it is transformed is refused, |
| 148 | // since its bytes are not the table. |
| 149 | func readWOFF2(data []byte) (fontTables, error) { |
| 150 | const headerLen = 48 |
| 151 | if len(data) < headerLen { |
| 152 | return fontTables{}, malformed("WOFF2 header cut short") |
| 153 | } |
| 154 | flavor := binary.BigEndian.Uint32(data[4:]) |
| 155 | if err := checkFlavor(flavor); err != nil { |
| 156 | return fontTables{}, err |
| 157 | } |
| 158 | if int64(binary.BigEndian.Uint32(data[8:])) != int64(len(data)) { |
| 159 | return fontTables{}, malformed("WOFF2 length is not the file's") |
| 160 | } |
| 161 | n := int(binary.BigEndian.Uint16(data[12:])) |
| 162 | if n == 0 { |
| 163 | return fontTables{}, malformed("no tables") |
| 164 | } |
| 165 | compressedLen := uint64(binary.BigEndian.Uint32(data[20:])) |
| 166 | |
| 167 | type entry struct { |
| 168 | tag string |
| 169 | off, size uint64 // in the unpacked stream |
| 170 | } |
| 171 | entries := make([]entry, 0, n) |
| 172 | has, transforms := map[string]bool{}, map[string]bool{} |
| 173 | p := data[headerLen:] |
| 174 | var total uint64 |
| 175 | for i := 0; i < n; i++ { |
| 176 | if len(p) < 1 { |
| 177 | return fontTables{}, malformed("WOFF2 table directory cut short") |
| 178 | } |
| 179 | flags := p[0] |
| 180 | p = p[1:] |
| 181 | var tag string |
| 182 | if idx := flags & 0x3F; idx == 63 { |
| 183 | if len(p) < 4 { |
| 184 | return fontTables{}, malformed("WOFF2 table directory cut short") |
| 185 | } |
| 186 | tag, p = string(p[:4]), p[4:] |
| 187 | } else { |
| 188 | tag = woff2Tags[idx] |
| 189 | } |
| 190 | origLen, rest, err := base128(p) |
| 191 | if err != nil { |
| 192 | return fontTables{}, err |
| 193 | } |
| 194 | p = rest |
| 195 | size := uint64(origLen) |
| 196 | version := flags >> 6 |
| 197 | transformed := version != 0 |
| 198 | if tag == "glyf" || tag == "loca" { |
| 199 | transformed = version != 3 |
| 200 | } |
| 201 | if transformed { |
| 202 | transformLen, rest, err := base128(p) |
| 203 | if err != nil { |
| 204 | return fontTables{}, err |
| 205 | } |
| 206 | p, size = rest, uint64(transformLen) |
| 207 | switch { |
| 208 | case tag == "glyf" || tag == "loca" || tag == "hmtx": |
| 209 | transforms[tag] = true // not rebuilt, so not checked |
| 210 | case wanted(tag): |
| 211 | return fontTables{}, malformed("table %q is transformed", tag) |
| 212 | } |
| 213 | } |
| 214 | if uint64(origLen) > maxUnpacked { |
| 215 | return fontTables{}, fmt.Errorf("%w: table %q unpacks to %d bytes", ErrTooBig, tag, origLen) |
| 216 | } |
| 217 | if has[tag] { |
| 218 | return fontTables{}, malformed("table %q appears twice", tag) |
| 219 | } |
| 220 | has[tag] = true |
| 221 | entries = append(entries, entry{tag, total, size}) |
| 222 | if total += size; total > maxUnpacked { |
| 223 | return fontTables{}, fmt.Errorf("%w: unpacks to over %d bytes", ErrTooBig, maxUnpacked) |
| 224 | } |
| 225 | } |
| 226 | start := uint64(len(data) - len(p)) |
| 227 | if start+compressedLen > uint64(len(data)) { |
| 228 | return fontTables{}, malformed("WOFF2 stream runs past the end of the file") |
| 229 | } |
| 230 | stream, err := readExactly(brotli.NewReader(bytes.NewReader(data[start:start+compressedLen])), int(total)) |
| 231 | if err != nil { |
| 232 | return fontTables{}, malformed("WOFF2 stream: %v", err) |
| 233 | } |
| 234 | ft := fontTables{version: flavor, has: has, data: map[string][]byte{}, transformed: transforms} |
| 235 | for _, e := range entries { |
| 236 | if wanted(e.tag) && !transforms[e.tag] { |
| 237 | ft.data[e.tag] = stream[e.off : e.off+e.size] |
| 238 | } |
| 239 | } |
| 240 | return ft, nil |
| 241 | } |
| 242 | |
| 243 | // base128 reads WOFF2's UIntBase128: at most five bytes, no leading zero |
| 244 | // byte, and a value that fits 32 bits. |
| 245 | func base128(p []byte) (uint32, []byte, error) { |
| 246 | var v uint32 |
| 247 | for i := 0; i < 5; i++ { |
| 248 | if i >= len(p) { |
| 249 | return 0, nil, malformed("WOFF2 table directory cut short") |
| 250 | } |
| 251 | b := p[i] |
| 252 | if i == 0 && b == 0x80 { |
| 253 | return 0, nil, malformed("UIntBase128 with a leading zero") |
| 254 | } |
| 255 | if v&0xFE000000 != 0 { |
| 256 | return 0, nil, malformed("UIntBase128 past 32 bits") |
| 257 | } |
| 258 | v = v<<7 | uint32(b&0x7F) |
| 259 | if b&0x80 == 0 { |
| 260 | return v, p[i+1:], nil |
| 261 | } |
| 262 | } |
| 263 | return 0, nil, malformed("UIntBase128 longer than five bytes") |
| 264 | } |
| 265 | |