-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathradix64.go
69 lines (58 loc) · 1.11 KB
/
radix64.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
// Copyright 2012 Kevin Gillette. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package radix64
import (
"errors"
)
const ord = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"
var table [256]byte
const (
// starting offsets
upper = '9' - '0' + 1
lower = upper + 'Z' - 'A' + 1
)
const costMask = ^uint64(63)
var (
ErrBufLimReached = errors.New("Buffer limit reached")
ErrInvalidByte = errors.New("Input cannot be decoded")
)
func Encode(n uint64, b []byte) error {
for i := len(b) - 1; i >= 0; i-- {
b[i] = ord[n&63]
n >>= 6
}
if n > 0 {
return ErrBufLimReached
}
return nil
}
func Decode(b []byte) (n uint64, err error) {
for _, c := range b {
c = table[c]
if c&64 == 0 {
return 0, ErrInvalidByte
} else {
n = n<<6 | uint64(c&63)
}
}
return
}
func Cost(n uint64) (bytes, remainder int) {
bytes = 1
for n&costMask != 0 {
n >>= 6
bytes++
}
remainder = 6
for n != 0 {
n >>= 1
remainder--
}
return
}
func init() {
for i := 0; i < len(ord); i++ {
table[ord[i]] = byte(i | 64)
}
}