-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
68 lines (59 loc) · 1.29 KB
/
utils.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
package dotlite
import (
"io"
)
func min(val ...int) int {
var m = val[0]
for _, i := range val[1:] {
if i < m {
m = i
}
}
return m
}
// Varint computes a 64-bit integer value from the given source.
//
// It differs slightly from binary.ReadVarint and follows sqlite's logic for
// computing the integer value from the bytes.
//
// see: https://www.sqlite.org/fileformat.html#varint description for more details
func Varint(r io.Reader) (_ int64, err error) {
var readByte = func(r io.Reader) (_ byte, err error) {
var buf [1]byte
if _, err = r.Read(buf[:]); err == nil {
return buf[0], nil
}
return 0, err
}
var b byte
var val uint64
for i := 0; i < 8; i++ {
if b, err = readByte(r); err != nil {
return 0, err
}
val = (val << 7) | uint64(b&0x7f)
if b < 0x80 {
return int64(val), nil
}
}
if b, err = readByte(r); err != nil {
return 0, err
}
return int64((val << 8) | uint64(b)), nil
}
// returns the size of serial type v, as defined under https://www.sqlite.org/fileformat.html#record_format
func typeSize(v int64) int64 {
if v > 0 && v <= 4 {
return v
} else if v == 5 {
return 6
} else if v == 6 || v == 7 {
return 8
} else if v >= 12 && v%2 == 0 {
return (v - 12) / 2
} else if v >= 13 && v%2 == 1 {
return (v - 13) / 2
} else {
return 0
}
}