예제 #1
0
func readVarint(r *io.SectionReader, offset int64) (v int64, n int, err error) {
	var buf [16]byte // 109 bits should be enough for everybody.
	_, err = r.ReadAt(buf[:], offset)
	if err == io.EOF || err == io.ErrUnexpectedEOF {
		err = nil
	}
	if err != nil {
		return
	}
	u, n := binary.Uvarint(buf[:])
	v = int64(u)
	return
}
예제 #2
0
func (pk *PackReader) checkIdxMagic(idx *io.SectionReader) (err error) {
	var buf [idxHeaderSize]byte
	_, err = idx.ReadAt(buf[:], 0)
	if err != nil {
		return
	}
	magic := [4]byte{buf[0], buf[1], buf[2], buf[3]}
	if magic != ([4]byte{'\xff', 't', 'O', 'c'}) {
		return errBadIdxMagic
	}
	for i := range pk.idxFanout {
		pk.idxFanout[i] = binary.BigEndian.Uint32(buf[8+4*i:])
	}
	return nil
}
예제 #3
0
func checkPackMagic(pack *io.SectionReader) (version, count uint32, err error) {
	var buf [12]byte
	_, err = pack.ReadAt(buf[:], 0)
	if err != nil {
		return
	}
	magic := [4]byte{buf[0], buf[1], buf[2], buf[3]}
	if magic != ([4]byte{'P', 'A', 'C', 'K'}) {
		err = errBadPackMagic
	}
	version = binary.BigEndian.Uint32(buf[4:8])
	if version != 2 {
		err = errUnsupportedPackVersion
	}
	count = binary.BigEndian.Uint32(buf[4:8])
	return
}
예제 #4
0
// readVaroffset reads the pseudo-varint used to encode offsets for delta bases.
// It is a big-endian form: 1|a0, ..., 1|a_{n-1}, 0|a_n.
// representing:
//  (a0+1)<<7*n + ... + (a_{n-1}+1)<<7 + a_n
func readVaroffset(r *io.SectionReader, offset int64) (v int64, n int, err error) {
	var buf [16]byte // 109 bits should be enough for everybody.
	n, err = r.ReadAt(buf[:], offset)
	if err == io.EOF || err == io.ErrUnexpectedEOF {
		err = nil
	}
	if err != nil {
		return
	}
	u := uint64(0)
	for i, b := range buf[:n] {
		if i > 0 {
			u++
		}
		u <<= 7
		u |= uint64(b &^ 0x80)
		if b&0x80 == 0 {
			return int64(u), i + 1, nil
		}
	}
	return int64(u), len(buf), io.ErrUnexpectedEOF
}
예제 #5
0
파일: cmapfunc.go 프로젝트: mandel59/gofont
func cmapParser(_ SFNT, r *io.SectionReader) Table {
	t := new(Cmap)
	header := CmapHeader{}
	if err := binary.Read(r, binary.BigEndian, &header); err != nil {
		return nil
	}
	t.Version = header.Version
	numTables := header.NumTables
	encodingRecord := make([]EncodingRecord, numTables)
	if err := binary.Read(r, binary.BigEndian, encodingRecord); err != nil {
		return nil
	}
	t.Subtable = make([]CmapSubtable, numTables)
	for i, v := range encodingRecord {
		pid, eid, offset := v.PlatformID, v.EncodingID, int64(v.Offset)
		format := make([]byte, 8)
		r.ReadAt(format, offset)
		var length int64
		switch binary.BigEndian.Uint16(format[:2]) {
		case 0, 2, 4, 6:
			length = int64(binary.BigEndian.Uint16(format[2:4]))
			break
		case 8, 10, 12, 13:
			length = int64(binary.BigEndian.Uint32(format[4:8]))
			break
		case 14:
			length = int64(binary.BigEndian.Uint32(format[2:6]))
			break
		default:
			return nil
		}
		sr := io.NewSectionReader(r, offset, length)
		t.Subtable[i] = CmapSubtable{pid, eid, &SubtableReader{sr}}
	}
	return Table(t)
}