forked from dhowden/tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
102 lines (88 loc) · 1.8 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// 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 (
"bytes"
"encoding/binary"
"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
}
func readUint64LittleEndian(r io.Reader) (uint64, error) {
b, err := readBytes(r, 8)
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint64(b), nil
}
// readBytesMaxUpfront is the max up-front allocation allowed
const readBytesMaxUpfront = 10 << 20 // 10MB
func readBytes(r io.Reader, n uint) ([]byte, error) {
if n > readBytesMaxUpfront {
b := &bytes.Buffer{}
if _, err := io.CopyN(b, r, int64(n)); err != nil {
return nil, err
}
return b.Bytes(), nil
}
b := make([]byte, n)
_, err := io.ReadFull(r, b)
if err != nil {
return nil, err
}
return b, nil
}
func readString(r io.Reader, n uint) (string, error) {
b, err := readBytes(r, n)
if err != nil {
return "", err
}
return string(b), nil
}
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) {
b, err := readBytes(r, n)
if err != nil {
return 0, err
}
return getInt(b), nil
}
func read7BitChunkedUint(r io.Reader, n uint) (uint, error) {
b, err := readBytes(r, n)
if err != nil {
return 0, err
}
return uint(get7BitChunkedInt(b)), nil
}
func readUint32LittleEndian(r io.Reader) (uint32, error) {
b, err := readBytes(r, 4)
if err != nil {
return 0, err
}
return binary.LittleEndian.Uint32(b), nil
}