Debian Codegraph @

decoder.go

raw symbols SCIP
1 //go:generate go run generate_decode_map.go
2
3 // Package base64dec contains a universal base64 decoder that works on both the standard and url-safe variants, padded and raw.
4 // The code is based on the standard encoding/base64 package.
5 package base64dec
6
7 import (
8 "encoding/base64"
9 "encoding/binary"
10 "strconv"
11 )
12
13 const padChar = '='
14
15 type ByteSeq interface {
16 []byte | string
17 }
18
19 // DecodeBase64 decodes src and writes at most base64.RawStdEncoding.DecodedLen(len(src))
20 // bytes to dst and returns the number of bytes written. If src contains invalid base64 data, it will return the
21 // number of bytes successfully written and base64.CorruptInputError.
22 // New line characters (\r and \n) are ignored.
23 // The input can be in the standard or the alternate (aka url-safe) encoding. It can be padded or un-padded.
24 // If there is a correct padding, it is consumed and no error is returned. If there is no padding where it's required,
25 // no error is returned. If there is an incorrect padding (i.e. too many or too few characters) it is treated
26 // as garbage at the end (i.e. the error will point to the first padding character).
27 func DecodeBase64[T ByteSeq](dst []byte, src T) (n int, err error) {
28 if len(src) == 0 {
29 return 0, nil
30 }
31
32 si := 0
33 for strconv.IntSize >= 64 && len(src)-si >= 8 && len(dst)-n >= 8 {
34 src2 := src[si : si+8]
35 if dn, ok := assemble64(
36 decodeMap[src2[0]],
37 decodeMap[src2[1]],
38 decodeMap[src2[2]],
39 decodeMap[src2[3]],
40 decodeMap[src2[4]],
41 decodeMap[src2[5]],
42 decodeMap[src2[6]],
43 decodeMap[src2[7]],
44 ); ok {
45 binary.BigEndian.PutUint64(dst[n:], dn)
46 n += 6
47 si += 8
48 } else {
49 var ninc int
50 si, ninc, err = decodeQuantum(dst[n:], src, si)
51 n += ninc
52 if err != nil {
53 return n, err
54 }
55 }
56 }
57
58 for len(src)-si >= 4 && len(dst)-n >= 4 {
59 src2 := src[si : si+4]
60 if dn, ok := assemble32(
61 decodeMap[src2[0]],
62 decodeMap[src2[1]],
63 decodeMap[src2[2]],
64 decodeMap[src2[3]],
65 ); ok {
66 binary.BigEndian.PutUint32(dst[n:], dn)
67 n += 3
68 si += 4
69 } else {
70 var ninc int
71 si, ninc, err = decodeQuantum(dst[n:], src, si)
72 n += ninc
73 if err != nil {
74 return n, err
75 }
76 }
77 }
78
79 for si < len(src) {
80 var ninc int
81 si, ninc, err = decodeQuantum(dst[n:], src, si)
82 n += ninc
83 if err != nil {
84 return n, err
85 }
86 }
87 return n, err
88 }
89
90 // assemble32 assembles 4 base64 digits into 3 bytes.
91 // Each digit comes from the decode map, and will be 0xff
92 // if it came from an invalid character.
93 func assemble32(n1, n2, n3, n4 byte) (dn uint32, ok bool) {
94 // Check that all the digits are valid. If any of them was 0xff, their
95 // bitwise OR will be 0xff.
96 if n1|n2|n3|n4 == 0xff {
97 return 0, false
98 }
99 return uint32(n1)<<26 |
100 uint32(n2)<<20 |
101 uint32(n3)<<14 |
102 uint32(n4)<<8,
103 true
104 }
105
106 // assemble64 assembles 8 base64 digits into 6 bytes.
107 // Each digit comes from the decode map, and will be 0xff
108 // if it came from an invalid character.
109 func assemble64(n1, n2, n3, n4, n5, n6, n7, n8 byte) (dn uint64, ok bool) {
110 // Check that all the digits are valid. If any of them was 0xff, their
111 // bitwise OR will be 0xff.
112 if n1|n2|n3|n4|n5|n6|n7|n8 == 0xff {
113 return 0, false
114 }
115 return uint64(n1)<<58 |
116 uint64(n2)<<52 |
117 uint64(n3)<<46 |
118 uint64(n4)<<40 |
119 uint64(n5)<<34 |
120 uint64(n6)<<28 |
121 uint64(n7)<<22 |
122 uint64(n8)<<16,
123 true
124 }
125
126 // decodeQuantum decodes up to 4 base64 bytes. The received parameters are
127 // the destination buffer dst, the source buffer src and an index in the
128 // source buffer si.
129 // It returns the number of bytes read from src, the number of bytes written
130 // to dst, and an error, if any.
131 func decodeQuantum[T ByteSeq](dst []byte, src T, si int) (nsi, n int, err error) {
132 // Decode quantum using the base64 alphabet
133 var dbuf [4]byte
134 dlen := 4
135
136 for j := 0; j < len(dbuf); j++ {
137 if len(src) == si {
138 if j == 0 {
139 return si, 0, nil
140 }
141 dlen = j
142 break
143 }
144 in := src[si]
145 si++
146
147 out := decodeMap[in]
148 if out != 0xff {
149 dbuf[j] = out
150 continue
151 }
152
153 if in == '\n' || in == '\r' {
154 j--
155 continue
156 }
157
158 dlen = j
159
160 if rune(in) != padChar {
161 err = base64.CorruptInputError(si - 1)
162 break
163 }
164
165 // We've reached the end and there's padding
166 switch j {
167 case 0, 1:
168 // incorrect padding
169 err = base64.CorruptInputError(si - 1)
170 case 2:
171 // "==" is expected, the first "=" is already consumed.
172 // skip over newlines
173 for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
174 si++
175 }
176 if si == len(src) {
177 // not enough padding
178 err = base64.CorruptInputError(si - 1)
179 break
180 } else if rune(src[si]) != padChar {
181 // incorrect padding
182 err = base64.CorruptInputError(si - 1)
183 break
184 }
185
186 si++
187 }
188
189 if err == nil {
190 // skip over newlines
191 for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
192 si++
193 }
194 if si < len(src) {
195 // trailing garbage
196 err = base64.CorruptInputError(si)
197 }
198 }
199 break
200 }
201
202 if dlen == 0 {
203 return si, 0, err
204 }
205
206 // Convert 4x 6bit source bytes into 3 bytes
207 val := uint(dbuf[0])<<18 | uint(dbuf[1])<<12 | uint(dbuf[2])<<6 | uint(dbuf[3])
208 dbuf[2], dbuf[1], dbuf[0] = byte(val>>0), byte(val>>8), byte(val>>16)
209 switch dlen {
210 case 4:
211 dst[2] = dbuf[2]
212 dbuf[2] = 0
213 fallthrough
214 case 3:
215 dst[1] = dbuf[1]
216 dbuf[1] = 0
217 fallthrough
218 case 2:
219 dst[0] = dbuf[0]
220 }
221
222 return si, dlen - 1, err
223 }