forked from MathieuTurcotte/sourcemap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base64_vlq.go
58 lines (49 loc) · 1.48 KB
/
base64_vlq.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
// Copyright (c) 2013 Mathieu Turcotte
// Licensed under the MIT license.
package sourcemap
import (
"io"
)
var decodeMap [256]byte
func init() {
// Use a custom map to decode base64 encoded data instead of the default
// go implementation in order to read each character into a single byte
// instead of decoding everything into a slice of bytes where the values
// are interleaved over a chunk of 4 bytes.
base64 := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
for i := 0; i < len(base64); i++ {
decodeMap[base64[i]] = byte(i)
}
}
const vqlBaseShift = 5
const vqlBase = 1 << vqlBaseShift // 00100000, i.e. 32
const vqlBaseMask = vqlBase - 1 // 00011111
const vqlContMask = vqlBase // 00100000
func fromVQLSigned(val int) int {
signed := (val & 1) == 1
val = val >> 1
if signed {
return -val
} else {
return val
}
}
// Decode the next base 64 VQL value from the reader. An error is returned if
// the byte reader reaches the end of its input while decoding the VQL value.
func decodeVQL(reader io.ByteReader) (result int, err error) {
continuation := true
shift := uint(0)
for continuation {
b, err := reader.ReadByte()
if err != nil {
return -1, err
}
b = decodeMap[b]
continuation = (b & vqlContMask) != 0
result += int(b&vqlBaseMask) << shift
// The VLQ base values are arranged most significant first in the
// stream, so shift left by 5 more bits at each iteration.
shift += vqlBaseShift
}
return fromVQLSigned(result), nil
}