/
base128.go
153 lines (127 loc) · 4.12 KB
/
base128.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package enc
// NOTE: This is a translation of base64.c from IODINE project into go.
/*
* Copyright (c) 2006-2014 Erik Ekman <yarrick@kryo.se>,
* 2006-2009 Bjorn Andersson <flex@kryo.se>
* Mostly rewritten 2009 J.A.Bezemer@opensourcepartners.nl
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
import (
"fmt"
"github.com/pkg/errors"
"go.chromium.org/luci/common/data/base128"
"sync"
)
const (
/*
* Don't use '-' (restricted to middle of labels), prefer iso_8859-1
* accent chars since they might readily be entered in normal use,
* don't use 254-255 because of possible function overloading in DNS systems.
*/
cb128 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" +
"\274\275\276\277" +
"\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317" +
"\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337" +
"\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357" +
"\360\361\362\363\364\365\366\367\370\371\372\373\374\375"
)
var cb128Invert map[byte]byte
var cbInitialized sync.Once
func init() {
setupCb128Invert()
}
func setupCb128Invert() {
cbInitialized.Do(func() {
cb128Invert = make(map[byte]byte)
for i, v := range []byte(cb128) {
cb128Invert[v] = byte(i)
}
})
}
// -------------------------------------------------------
// Base128Encoder encodes 7 bytes to 8 characters
type Base128Encoder struct {
}
func (b *Base128Encoder) Name() string {
return "Base128"
}
func (b *Base128Encoder) String() string {
return fmt.Sprintf("%v(%v)", b.Name(), string(b.Code()))
}
func (b *Base128Encoder) Code() byte {
return 'V'
}
func (b *Base128Encoder) Encode(src []byte) []byte {
dst := make([]byte, 0)
whichByte := uint(1)
bufByte := byte(0)
for _, val := range src {
// Take the current buffer, add current value, shifted.
// E.g. first round is first 7 bits of value
elem := bufByte | (val >> whichByte)
dst = append(dst, elem)
// Prepare the remaining data for the next buffer.
// E.g. first round is the remaining bit
bufByte = val & ((1 << whichByte) - 1)
// Shift the remaining value to the left
bufByte = bufByte << (7 - whichByte)
if whichByte == 7 {
dst = append(dst, bufByte)
bufByte = 0
whichByte = 0
}
whichByte++
}
dst = append(dst, bufByte)
dst = escape128(dst)
return dst
}
func escape128(src []byte) []byte {
res := make([]byte, len(src))
for i, v := range src {
res[i] = cb128[v]
}
return res
}
func unescape128(src []byte) []byte {
if len(cb128Invert) == 0 {
setupCb128Invert()
}
res := make([]byte, len(src))
for i, v := range src {
res[i] = cb128Invert[v]
}
return res
}
func (b *Base128Encoder) Decode(data []byte) ([]byte, error) {
src := unescape128([]byte(data))
res, err := base128.DecodeString(string(src))
if err != nil {
err = errors.WithStack(err)
return nil, err
}
return res, nil
}
func (b *Base128Encoder) TestPatterns() [][]byte {
return [][]byte{
[]byte("aA-Aaahhh-Drink-mal-ein-J\344germeister-"),
[]byte("aA-La-fl\373te-na\357ve-fran\347aise-est-retir\351-\340-Cr\350te"),
[]byte("aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyYzZ"),
[]byte("aA0123456789\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317"),
[]byte("aA\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375"),
}
}
func (b *Base128Encoder) Ratio() float64 {
return 8.0 / 7.0
}