package typeface import ( "bytes" "compress/zlib" "encoding/binary" "fmt" "io" "github.com/andybalholm/brotli" ) // WOFF and WOFF2 unwrapping, as far as Inspect needs it: the table // directory, and the bytes of the tables it reads. Neither rebuilds the // sfnt. // readWOFF reads a WOFF 1.0 file (zlib per table). Every table must lie // inside the file and inflate to exactly the length the directory gives // it (or be stored, when its packed and unpacked lengths are equal); only // the tables Inspect reads are kept. func readWOFF(data []byte) (fontTables, error) { const headerLen, entryLen = 44, 20 if len(data) < headerLen { return fontTables{}, malformed("WOFF header cut short") } flavor := binary.BigEndian.Uint32(data[4:]) if err := checkFlavor(flavor); err != nil { return fontTables{}, err } if int64(binary.BigEndian.Uint32(data[8:])) != int64(len(data)) { return fontTables{}, malformed("WOFF length is not the file's") } n := int(binary.BigEndian.Uint16(data[12:])) if n == 0 { return fontTables{}, malformed("no tables") } if headerLen+entryLen*n > len(data) { return fontTables{}, malformed("WOFF table directory cut short: %d tables", n) } ft := fontTables{version: flavor, has: map[string]bool{}, data: map[string][]byte{}} spans := make([]span, 0, n) for i := 0; i < n; i++ { e := data[headerLen+entryLen*i:] spans = append(spans, span{string(e[:4]), uint64(binary.BigEndian.Uint32(e[4:])), uint64(binary.BigEndian.Uint32(e[8:]))}) } for _, s := range spans { if s.off+s.length > uint64(len(data)) { return fontTables{}, malformed("table %q runs past the end of the file", s.tag) } } if err := disjoint(spans, uint64(headerLen+entryLen*n), uint64(len(data))); err != nil { return fontTables{}, err } var unpacked uint64 for i := 0; i < n; i++ { e := data[headerLen+entryLen*i:] tag := string(e[:4]) off := uint64(binary.BigEndian.Uint32(e[4:])) compLen := uint64(binary.BigEndian.Uint32(e[8:])) origLen := uint64(binary.BigEndian.Uint32(e[12:])) if off+compLen > uint64(len(data)) { return fontTables{}, malformed("table %q runs past the end of the file", tag) } if compLen > origLen { return fontTables{}, malformed("table %q is bigger packed than unpacked", tag) } if unpacked += origLen; unpacked > maxUnpacked { return fontTables{}, fmt.Errorf("%w: unpacks to over %d bytes", ErrTooBig, maxUnpacked) } if ft.has[tag] { return fontTables{}, malformed("table %q appears twice", tag) } ft.has[tag] = true packed := data[off : off+compLen] if compLen == origLen { if wanted(tag) { ft.data[tag] = packed // stored, not compressed } continue } b, err := inflate(packed, int(origLen), wanted(tag)) if err != nil { return fontTables{}, malformed("table %q: %v", tag, err) } if wanted(tag) { ft.data[tag] = b } } return ft, nil } // inflate unpacks a zlib stream that must hold exactly n bytes, returning // them if keep is set and otherwise only checking them. func inflate(packed []byte, n int, keep bool) ([]byte, error) { r, err := zlib.NewReader(bytes.NewReader(packed)) if err != nil { return nil, err } defer r.Close() if keep { return readExactly(r, n) } got, err := io.Copy(io.Discard, io.LimitReader(r, int64(n)+1)) if err != nil { return nil, err } if got != int64(n) { return nil, fmt.Errorf("unpacks to %d bytes, not %d", got, n) } return nil, nil } // readExactly reads n bytes from r and checks that r then ends cleanly. // It reads no more than n+1 bytes whatever r holds. func readExactly(r io.Reader, n int) ([]byte, error) { b, err := io.ReadAll(io.LimitReader(r, int64(n)+1)) if err != nil { return nil, err } if len(b) > n { return nil, fmt.Errorf("unpacks to more than the %d bytes its tables hold", n) } if len(b) < n { return nil, fmt.Errorf("unpacks to %d bytes, not %d", len(b), n) } return b, nil } // woff2Tags is WOFF2's known-tag table: a directory entry's flags name a // tag by its index here, or 63 for a tag written out in full. var woff2Tags = [63]string{ "cmap", "head", "hhea", "hmtx", "maxp", "name", "OS/2", "post", "cvt ", "fpgm", "glyf", "loca", "prep", "CFF ", "VORG", "EBDT", "EBLC", "gasp", "hdmx", "kern", "LTSH", "PCLT", "VDMX", "vhea", "vmtx", "BASE", "GDEF", "GPOS", "GSUB", "EBSC", "JSTF", "MATH", "CBDT", "CBLC", "COLR", "CPAL", "SVG ", "sbix", "acnt", "avar", "bdat", "bloc", "bsln", "cvar", "fdsc", "feat", "fmtx", "fvar", "gvar", "hsty", "just", "lcar", "mort", "morx", "opbd", "prop", "trak", "Zapf", "Silf", "Glat", "Gloc", "Feat", "Sill", } // readWOFF2 reads a WOFF2 file: the table directory, then the one brotli // stream holding every table end to end. The stream must unpack to // exactly the tables' total length, and that total may not pass // maxUnpacked. Only glyf, loca and hmtx may be transformed; their // transformed bytes are not rebuilt, so the outline checks skip them. Any // other table Inspect reads that says it is transformed is refused, // since its bytes are not the table. func readWOFF2(data []byte) (fontTables, error) { const headerLen = 48 if len(data) < headerLen { return fontTables{}, malformed("WOFF2 header cut short") } flavor := binary.BigEndian.Uint32(data[4:]) if err := checkFlavor(flavor); err != nil { return fontTables{}, err } if int64(binary.BigEndian.Uint32(data[8:])) != int64(len(data)) { return fontTables{}, malformed("WOFF2 length is not the file's") } n := int(binary.BigEndian.Uint16(data[12:])) if n == 0 { return fontTables{}, malformed("no tables") } compressedLen := uint64(binary.BigEndian.Uint32(data[20:])) type entry struct { tag string off, size uint64 // in the unpacked stream } entries := make([]entry, 0, n) has, transforms := map[string]bool{}, map[string]bool{} p := data[headerLen:] var total uint64 for i := 0; i < n; i++ { if len(p) < 1 { return fontTables{}, malformed("WOFF2 table directory cut short") } flags := p[0] p = p[1:] var tag string if idx := flags & 0x3F; idx == 63 { if len(p) < 4 { return fontTables{}, malformed("WOFF2 table directory cut short") } tag, p = string(p[:4]), p[4:] } else { tag = woff2Tags[idx] } origLen, rest, err := base128(p) if err != nil { return fontTables{}, err } p = rest size := uint64(origLen) version := flags >> 6 transformed := version != 0 if tag == "glyf" || tag == "loca" { transformed = version != 3 } if transformed { transformLen, rest, err := base128(p) if err != nil { return fontTables{}, err } p, size = rest, uint64(transformLen) switch { case tag == "glyf" || tag == "loca" || tag == "hmtx": transforms[tag] = true // not rebuilt, so not checked case wanted(tag): return fontTables{}, malformed("table %q is transformed", tag) } } if uint64(origLen) > maxUnpacked { return fontTables{}, fmt.Errorf("%w: table %q unpacks to %d bytes", ErrTooBig, tag, origLen) } if has[tag] { return fontTables{}, malformed("table %q appears twice", tag) } has[tag] = true entries = append(entries, entry{tag, total, size}) if total += size; total > maxUnpacked { return fontTables{}, fmt.Errorf("%w: unpacks to over %d bytes", ErrTooBig, maxUnpacked) } } start := uint64(len(data) - len(p)) if start+compressedLen > uint64(len(data)) { return fontTables{}, malformed("WOFF2 stream runs past the end of the file") } stream, err := readExactly(brotli.NewReader(bytes.NewReader(data[start:start+compressedLen])), int(total)) if err != nil { return fontTables{}, malformed("WOFF2 stream: %v", err) } ft := fontTables{version: flavor, has: has, data: map[string][]byte{}, transformed: transforms} for _, e := range entries { if wanted(e.tag) && !transforms[e.tag] { ft.data[e.tag] = stream[e.off : e.off+e.size] } } return ft, nil } // base128 reads WOFF2's UIntBase128: at most five bytes, no leading zero // byte, and a value that fits 32 bits. func base128(p []byte) (uint32, []byte, error) { var v uint32 for i := 0; i < 5; i++ { if i >= len(p) { return 0, nil, malformed("WOFF2 table directory cut short") } b := p[i] if i == 0 && b == 0x80 { return 0, nil, malformed("UIntBase128 with a leading zero") } if v&0xFE000000 != 0 { return 0, nil, malformed("UIntBase128 past 32 bits") } v = v<<7 | uint32(b&0x7F) if b&0x80 == 0 { return v, p[i+1:], nil } } return 0, nil, malformed("UIntBase128 longer than five bytes") }