2015-03-19 13:21:53 +01:00
|
|
|
// Copyright 2015, David Howden
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tag
|
|
|
|
|
|
|
|
import (
|
2015-04-14 16:09:58 +02:00
|
|
|
"encoding/binary"
|
2015-03-19 13:21:53 +01:00
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getBit(b byte, n uint) bool {
|
|
|
|
x := byte(1 << n)
|
|
|
|
return (b & x) == x
|
|
|
|
}
|
|
|
|
|
|
|
|
func get7BitChunkedInt(b []byte) int {
|
|
|
|
var n int
|
|
|
|
for _, x := range b {
|
|
|
|
n = n << 7
|
|
|
|
n |= int(x)
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
|
|
|
func getInt(b []byte) int {
|
|
|
|
var n int
|
|
|
|
for _, x := range b {
|
|
|
|
n = n << 8
|
|
|
|
n |= int(x)
|
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:50:59 +01:00
|
|
|
func readUint64LittleEndian(r io.Reader) (uint64, error) {
|
|
|
|
b, err := readBytes(r, 8)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
2018-11-04 23:57:29 +01:00
|
|
|
}
|
2019-11-22 12:50:59 +01:00
|
|
|
return binary.LittleEndian.Uint64(b), nil
|
2018-11-04 23:57:29 +01:00
|
|
|
}
|
|
|
|
|
2019-11-22 12:50:59 +01:00
|
|
|
func readBytes(r io.Reader, n uint) ([]byte, error) {
|
2015-03-19 13:21:53 +01:00
|
|
|
b := make([]byte, n)
|
|
|
|
_, err := io.ReadFull(r, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:50:59 +01:00
|
|
|
func readString(r io.Reader, n uint) (string, error) {
|
2015-03-19 13:21:53 +01:00
|
|
|
b, err := readBytes(r, n)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return string(b), nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:50:59 +01:00
|
|
|
func readUint(r io.Reader, n uint) (uint, error) {
|
|
|
|
x, err := readInt(r, n)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return uint(x), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readInt(r io.Reader, n uint) (int, error) {
|
2015-03-19 13:21:53 +01:00
|
|
|
b, err := readBytes(r, n)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return getInt(b), nil
|
|
|
|
}
|
|
|
|
|
2019-11-22 12:50:59 +01:00
|
|
|
func read7BitChunkedUint(r io.Reader, n uint) (uint, error) {
|
2015-03-19 13:21:53 +01:00
|
|
|
b, err := readBytes(r, n)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2019-11-22 12:50:59 +01:00
|
|
|
return uint(get7BitChunkedInt(b)), nil
|
2015-03-19 13:21:53 +01:00
|
|
|
}
|
2015-04-14 16:09:58 +02:00
|
|
|
|
2019-11-20 14:19:27 +01:00
|
|
|
func readUint32LittleEndian(r io.Reader) (uint32, error) {
|
|
|
|
b, err := readBytes(r, 4)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return binary.LittleEndian.Uint32(b), nil
|
2015-04-14 16:09:58 +02:00
|
|
|
}
|