Debian Codegraph @

base58.go

raw symbols SCIP
1 package base58
2
3 import (
4 "fmt"
5 )
6
7 type Alphabet struct {
8 Decode [128]int8
9 Encode [58]byte
10 }
11
12 func NewAlphabet(src string) (*Alphabet, error) {
13 if len(src) != 58 {
14 return nil, fmt.Errorf("invalid alphabet: base58 alphabets must be 58 bytes long")
15 }
16
17 var alphabet Alphabet
18 copy(alphabet.Encode[:], src)
19
20 for i := range alphabet.Decode {
21 alphabet.Decode[i] = -1
22 }
23
24 for i, b := range alphabet.Encode {
25 alphabet.Decode[b] = int8(i)
26 }
27
28 return &alphabet, nil
29 }
30
31 var (
32 // BitcoinAlphabet is the bitcoin alphabet.
33 BitcoinAlphabet, _ = NewAlphabet("123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz")
34
35 // FlickrAlphabet is the flickr alphabet.
36 FlickrAlphabet, _ = NewAlphabet("123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ")
37 )
38
39 func EncodeAlphabet(src []byte, alphabet *Alphabet) string {
40 zero := alphabet.Encode[0]
41 srcSize := len(src)
42 var i, j, zcount, high, carry int
43
44 for zcount < srcSize && src[zcount] == 0 {
45 zcount++
46 }
47
48 size := ((srcSize-zcount)*138/100 + 1)
49 buf := make([]byte, size*2+zcount)
50
51 tmp := buf[size+zcount:]
52
53 high = size - 1
54 for i = zcount; i < srcSize; i++ {
55 j = size - 1
56 for carry = int(src[i]); j > high || carry != 0; j-- {
57 carry = carry + 256*int(tmp[j])
58 tmp[j] = byte(carry % 58)
59 carry /= 58
60 }
61 high = j
62 }
63
64 for j = 0; j < size && tmp[j] == 0; j++ {
65 }
66
67 b58 := buf[:size-j+zcount]
68
69 if zcount != 0 {
70 for i = 0; i < zcount; i++ {
71 b58[i] = zero
72 }
73 }
74
75 for i = zcount; j < size; i++ {
76 b58[i] = alphabet.Encode[tmp[j]]
77 j++
78 }
79
80 return string(b58)
81 }
82
83 func Encode(src []byte) string {
84 return EncodeAlphabet(src, BitcoinAlphabet)
85 }
86
87 func DecodeAlphabet(src string, alphabet *Alphabet) ([]byte, error) {
88 if len(src) == 0 {
89 return nil, fmt.Errorf("invalid encoded srcing: length must be greater than 0")
90 }
91
92 var (
93 t, c uint64
94 zmask uint32
95 zcount int
96 b58u = []rune(src)
97 b58sz = len(b58u)
98 outisz = (b58sz + 3) >> 2
99 binu = make([]byte, (b58sz+3)*3)
100 bytesleft = b58sz & 3
101 zero = rune(alphabet.Encode[0])
102 )
103
104 if bytesleft > 0 {
105 zmask = 0xffffffff << uint32(bytesleft*8)
106 } else {
107 bytesleft = 4
108 }
109
110 var outi = make([]uint32, outisz)
111
112 for i := 0; i < b58sz && b58u[i] == zero; i++ {
113 zcount++
114 }
115
116 for _, r := range b58u {
117 if r > 127 {
118 return nil, fmt.Errorf("high-bit set on invalid digit")
119 }
120
121 if alphabet.Decode[r] == -1 {
122 return nil, fmt.Errorf("invalid base58 digit (%q)", r)
123 }
124
125 c = uint64(alphabet.Decode[r])
126
127 for j := outisz - 1; j >= 0; j-- {
128 t = uint64(outi[j])*58 + c
129 c = (t >> 32) & 0x3f
130 outi[j] = uint32(t & 0xffffffff)
131 }
132
133 if c > 0 {
134 return nil, fmt.Errorf("output number too big (carry to the next int32)")
135 }
136
137 if outi[0]&zmask != 0 {
138 return nil, fmt.Errorf("output number too big (last int32 filled too far)")
139 }
140 }
141
142 var j, cnt int
143
144 for j, cnt = 0, 0; j < outisz; j++ {
145 for mask := byte(bytesleft-1) * 8; mask <= 0x18; mask, cnt = mask-8, cnt+1 {
146 binu[cnt] = byte(outi[j] >> mask)
147 }
148 if j == 0 {
149 bytesleft = 4
150 }
151 }
152
153 for n, v := range binu {
154 if v > 0 {
155 start := n - zcount
156 if start < 0 {
157 start = 0
158 }
159 return binu[start:cnt], nil
160 }
161 }
162
163 return binu[:cnt], nil
164 }
165
166 func Decode(src string) ([]byte, error) {
167 return DecodeAlphabet(src, BitcoinAlphabet)
168 }