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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
//go:generate go run generate_decode_map.go
// Package base64dec contains a universal base64 decoder that works on both the standard and url-safe variants, padded and raw.
// The code is based on the standard encoding/base64 package.
package base64dec
import (
"encoding/base64"
"encoding/binary"
"strconv"
)
const padChar = '='
type ByteSeq interface {
[]byte | string
}
// DecodeBase64 decodes src and writes at most base64.RawStdEncoding.DecodedLen(len(src))
// bytes to dst and returns the number of bytes written. If src contains invalid base64 data, it will return the
// number of bytes successfully written and base64.CorruptInputError.
// New line characters (\r and \n) are ignored.
// The input can be in the standard or the alternate (aka url-safe) encoding. It can be padded or un-padded.
// If there is a correct padding, it is consumed and no error is returned. If there is no padding where it's required,
// no error is returned. If there is an incorrect padding (i.e. too many or too few characters) it is treated
// as garbage at the end (i.e. the error will point to the first padding character).
func DecodeBase64[T ByteSeq](dst []byte, src T) (n int, err error) {
if len(src) == 0 {
return 0, nil
}
si := 0
for strconv.IntSize >= 64 && len(src)-si >= 8 && len(dst)-n >= 8 {
src2 := src[si : si+8]
if dn, ok := assemble64(
decodeMap[src2[0]],
decodeMap[src2[1]],
decodeMap[src2[2]],
decodeMap[src2[3]],
decodeMap[src2[4]],
decodeMap[src2[5]],
decodeMap[src2[6]],
decodeMap[src2[7]],
); ok {
binary.BigEndian.PutUint64(dst[n:], dn)
n += 6
si += 8
} else {
var ninc int
si, ninc, err = decodeQuantum(dst[n:], src, si)
n += ninc
if err != nil {
return n, err
}
}
}
for len(src)-si >= 4 && len(dst)-n >= 4 {
src2 := src[si : si+4]
if dn, ok := assemble32(
decodeMap[src2[0]],
decodeMap[src2[1]],
decodeMap[src2[2]],
decodeMap[src2[3]],
); ok {
binary.BigEndian.PutUint32(dst[n:], dn)
n += 3
si += 4
} else {
var ninc int
si, ninc, err = decodeQuantum(dst[n:], src, si)
n += ninc
if err != nil {
return n, err
}
}
}
for si < len(src) {
var ninc int
si, ninc, err = decodeQuantum(dst[n:], src, si)
n += ninc
if err != nil {
return n, err
}
}
return n, err
}
// assemble32 assembles 4 base64 digits into 3 bytes.
// Each digit comes from the decode map, and will be 0xff
// if it came from an invalid character.
func assemble32(n1, n2, n3, n4 byte) (dn uint32, ok bool) {
// Check that all the digits are valid. If any of them was 0xff, their
// bitwise OR will be 0xff.
if n1|n2|n3|n4 == 0xff {
return 0, false
}
return uint32(n1)<<26 |
uint32(n2)<<20 |
uint32(n3)<<14 |
uint32(n4)<<8,
true
}
// assemble64 assembles 8 base64 digits into 6 bytes.
// Each digit comes from the decode map, and will be 0xff
// if it came from an invalid character.
func assemble64(n1, n2, n3, n4, n5, n6, n7, n8 byte) (dn uint64, ok bool) {
// Check that all the digits are valid. If any of them was 0xff, their
// bitwise OR will be 0xff.
if n1|n2|n3|n4|n5|n6|n7|n8 == 0xff {
return 0, false
}
return uint64(n1)<<58 |
uint64(n2)<<52 |
uint64(n3)<<46 |
uint64(n4)<<40 |
uint64(n5)<<34 |
uint64(n6)<<28 |
uint64(n7)<<22 |
uint64(n8)<<16,
true
}
// decodeQuantum decodes up to 4 base64 bytes. The received parameters are
// the destination buffer dst, the source buffer src and an index in the
// source buffer si.
// It returns the number of bytes read from src, the number of bytes written
// to dst, and an error, if any.
func decodeQuantum[T ByteSeq](dst []byte, src T, si int) (nsi, n int, err error) {
// Decode quantum using the base64 alphabet
var dbuf [4]byte
dlen := 4
for j := 0; j < len(dbuf); j++ {
if len(src) == si {
if j == 0 {
return si, 0, nil
}
dlen = j
break
}
in := src[si]
si++
out := decodeMap[in]
if out != 0xff {
dbuf[j] = out
continue
}
if in == '\n' || in == '\r' {
j--
continue
}
dlen = j
if rune(in) != padChar {
err = base64.CorruptInputError(si - 1)
break
}
// We've reached the end and there's padding
switch j {
case 0, 1:
// incorrect padding
err = base64.CorruptInputError(si - 1)
case 2:
// "==" is expected, the first "=" is already consumed.
// skip over newlines
for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
si++
}
if si == len(src) {
// not enough padding
err = base64.CorruptInputError(si - 1)
break
} else if rune(src[si]) != padChar {
// incorrect padding
err = base64.CorruptInputError(si - 1)
break
}
si++
}
if err == nil {
// skip over newlines
for si < len(src) && (src[si] == '\n' || src[si] == '\r') {
si++
}
if si < len(src) {
// trailing garbage
err = base64.CorruptInputError(si)
}
}
break
}
if dlen == 0 {
return si, 0, err
}
// Convert 4x 6bit source bytes into 3 bytes
val := uint(dbuf[0])<<18 | uint(dbuf[1])<<12 | uint(dbuf[2])<<6 | uint(dbuf[3])
dbuf[2], dbuf[1], dbuf[0] = byte(val>>0), byte(val>>8), byte(val>>16)
switch dlen {
case 4:
dst[2] = dbuf[2]
dbuf[2] = 0
fallthrough
case 3:
dst[1] = dbuf[1]
dbuf[1] = 0
fallthrough
case 2:
dst[0] = dbuf[0]
}
return si, dlen - 1, err
}
|