| 1 |
// Copyright 2011 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
// Package base32 implements base32 encoding as specified by RFC 4648. |
| 6 |
package base32 |
| 7 |
|
| 8 |
import ( |
| 9 |
"io" |
| 10 |
"slices" |
| 11 |
"strconv" |
| 12 |
) |
| 13 |
|
| 14 |
/* |
| 15 |
* Encodings |
| 16 |
*/ |
| 17 |
|
| 18 |
// An Encoding is a radix 32 encoding/decoding scheme, defined by a |
| 19 |
// 32-character alphabet. The most common is the "base32" encoding |
| 20 |
// introduced for SASL GSSAPI and standardized in RFC 4648. |
| 21 |
// The alternate "base32hex" encoding is used in DNSSEC. |
| 22 |
type Encoding struct { |
| 23 |
encode [32]byte // mapping of symbol index to symbol byte value |
| 24 |
decodeMap [256]uint8 // mapping of symbol byte value to symbol index |
| 25 |
padChar rune |
| 26 |
} |
| 27 |
|
| 28 |
const ( |
| 29 |
StdPadding rune = '=' // Standard padding character |
| 30 |
NoPadding rune = -1 // No padding |
| 31 |
) |
| 32 |
|
| 33 |
const ( |
| 34 |
decodeMapInitialize = "" + |
| 35 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 36 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 37 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 38 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 39 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 40 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 41 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 42 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 43 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 44 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 45 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 46 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 47 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 48 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 49 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" + |
| 50 |
"\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff" |
| 51 |
invalidIndex = '\xff' |
| 52 |
) |
| 53 |
|
| 54 |
// NewEncoding returns a new padded Encoding defined by the given alphabet, |
| 55 |
// which must be a 32-byte string that contains unique byte values and |
| 56 |
// does not contain the padding character or CR / LF ('\r', '\n'). |
| 57 |
// The alphabet is treated as a sequence of byte values |
| 58 |
// without any special treatment for multi-byte UTF-8. |
| 59 |
// The resulting Encoding uses the default padding character ('='), |
| 60 |
// which may be changed or disabled via [Encoding.WithPadding]. |
| 61 |
func NewEncoding(encoder string) *Encoding { |
| 62 |
if len(encoder) != 32 { |
| 63 |
panic("encoding alphabet is not 32-bytes long") |
| 64 |
} |
| 65 |
|
| 66 |
e := new(Encoding) |
| 67 |
e.padChar = StdPadding |
| 68 |
copy(e.encode[:], encoder) |
| 69 |
copy(e.decodeMap[:], decodeMapInitialize) |
| 70 |
|
| 71 |
for i := 0; i < len(encoder); i++ { |
| 72 |
// Note: While we document that the alphabet cannot contain |
| 73 |
// the padding character, we do not enforce it since we do not know |
| 74 |
// if the caller intends to switch the padding from StdPadding later. |
| 75 |
switch { |
| 76 |
case encoder[i] == '\n' || encoder[i] == '\r': |
| 77 |
panic("encoding alphabet contains newline character") |
| 78 |
case e.decodeMap[encoder[i]] != invalidIndex: |
| 79 |
panic("encoding alphabet includes duplicate symbols") |
| 80 |
} |
| 81 |
e.decodeMap[encoder[i]] = uint8(i) |
| 82 |
} |
| 83 |
return e |
| 84 |
} |
| 85 |
|
| 86 |
// StdEncoding is the standard base32 encoding, as defined in RFC 4648. |
| 87 |
var StdEncoding = NewEncoding("ABCDEFGHIJKLMNOPQRSTUVWXYZ234567") |
| 88 |
|
| 89 |
// HexEncoding is the “Extended Hex Alphabet” defined in RFC 4648. |
| 90 |
// It is typically used in DNS. |
| 91 |
var HexEncoding = NewEncoding("0123456789ABCDEFGHIJKLMNOPQRSTUV") |
| 92 |
|
| 93 |
// WithPadding creates a new encoding identical to enc except |
| 94 |
// with a specified padding character, or NoPadding to disable padding. |
| 95 |
// The padding character must not be '\r' or '\n', |
| 96 |
// must not be contained in the encoding's alphabet, |
| 97 |
// must not be negative, and must be a rune equal or below '\xff'. |
| 98 |
// Padding characters above '\x7f' are encoded as their exact byte value |
| 99 |
// rather than using the UTF-8 representation of the codepoint. |
| 100 |
func (enc Encoding) WithPadding(padding rune) *Encoding { |
| 101 |
switch { |
| 102 |
case padding < NoPadding || padding == '\r' || padding == '\n' || padding > 0xff: |
| 103 |
panic("invalid padding") |
| 104 |
case padding != NoPadding && enc.decodeMap[byte(padding)] != invalidIndex: |
| 105 |
panic("padding contained in alphabet") |
| 106 |
} |
| 107 |
enc.padChar = padding |
| 108 |
return &enc |
| 109 |
} |
| 110 |
|
| 111 |
/* |
| 112 |
* Encoder |
| 113 |
*/ |
| 114 |
|
| 115 |
// Encode encodes src using the encoding enc, |
| 116 |
// writing [Encoding.EncodedLen](len(src)) bytes to dst. |
| 117 |
// |
| 118 |
// The encoding pads the output to a multiple of 8 bytes, |
| 119 |
// so Encode is not appropriate for use on individual blocks |
| 120 |
// of a large data stream. Use [NewEncoder] instead. |
| 121 |
func (enc *Encoding) Encode(dst, src []byte) { |
| 122 |
if len(src) == 0 { |
| 123 |
return |
| 124 |
} |
| 125 |
// enc is a pointer receiver, so the use of enc.encode within the hot |
| 126 |
// loop below means a nil check at every operation. Lift that nil check |
| 127 |
// outside of the loop to speed up the encoder. |
| 128 |
_ = enc.encode |
| 129 |
|
| 130 |
di, si := 0, 0 |
| 131 |
n := (len(src) / 5) * 5 |
| 132 |
for si < n { |
| 133 |
// Combining two 32 bit loads allows the same code to be used |
| 134 |
// for 32 and 64 bit platforms. |
| 135 |
hi := uint32(src[si+0])<<24 | uint32(src[si+1])<<16 | uint32(src[si+2])<<8 | uint32(src[si+3]) |
| 136 |
lo := hi<<8 | uint32(src[si+4]) |
| 137 |
|
| 138 |
dst[di+0] = enc.encode[(hi>>27)&0x1F] |
| 139 |
dst[di+1] = enc.encode[(hi>>22)&0x1F] |
| 140 |
dst[di+2] = enc.encode[(hi>>17)&0x1F] |
| 141 |
dst[di+3] = enc.encode[(hi>>12)&0x1F] |
| 142 |
dst[di+4] = enc.encode[(hi>>7)&0x1F] |
| 143 |
dst[di+5] = enc.encode[(hi>>2)&0x1F] |
| 144 |
dst[di+6] = enc.encode[(lo>>5)&0x1F] |
| 145 |
dst[di+7] = enc.encode[(lo)&0x1F] |
| 146 |
|
| 147 |
si += 5 |
| 148 |
di += 8 |
| 149 |
} |
| 150 |
|
| 151 |
// Add the remaining small block |
| 152 |
remain := len(src) - si |
| 153 |
if remain == 0 { |
| 154 |
return |
| 155 |
} |
| 156 |
|
| 157 |
// Encode the remaining bytes in reverse order. |
| 158 |
val := uint32(0) |
| 159 |
switch remain { |
| 160 |
case 4: |
| 161 |
val |= uint32(src[si+3]) |
| 162 |
dst[di+6] = enc.encode[val<<3&0x1F] |
| 163 |
dst[di+5] = enc.encode[val>>2&0x1F] |
| 164 |
fallthrough |
| 165 |
case 3: |
| 166 |
val |= uint32(src[si+2]) << 8 |
| 167 |
dst[di+4] = enc.encode[val>>7&0x1F] |
| 168 |
fallthrough |
| 169 |
case 2: |
| 170 |
val |= uint32(src[si+1]) << 16 |
| 171 |
dst[di+3] = enc.encode[val>>12&0x1F] |
| 172 |
dst[di+2] = enc.encode[val>>17&0x1F] |
| 173 |
fallthrough |
| 174 |
case 1: |
| 175 |
val |= uint32(src[si+0]) << 24 |
| 176 |
dst[di+1] = enc.encode[val>>22&0x1F] |
| 177 |
dst[di+0] = enc.encode[val>>27&0x1F] |
| 178 |
} |
| 179 |
|
| 180 |
// Pad the final quantum |
| 181 |
if enc.padChar != NoPadding { |
| 182 |
nPad := (remain * 8 / 5) + 1 |
| 183 |
for i := nPad; i < 8; i++ { |
| 184 |
dst[di+i] = byte(enc.padChar) |
| 185 |
} |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
// AppendEncode appends the base32 encoded src to dst |
| 190 |
// and returns the extended buffer. |
| 191 |
func (enc *Encoding) AppendEncode(dst, src []byte) []byte { |
| 192 |
n := enc.EncodedLen(len(src)) |
| 193 |
dst = slices.Grow(dst, n) |
| 194 |
enc.Encode(dst[len(dst):][:n], src) |
| 195 |
return dst[:len(dst)+n] |
| 196 |
} |
| 197 |
|
| 198 |
// EncodeToString returns the base32 encoding of src. |
| 199 |
func (enc *Encoding) EncodeToString(src []byte) string { |
| 200 |
buf := make([]byte, enc.EncodedLen(len(src))) |
| 201 |
enc.Encode(buf, src) |
| 202 |
return string(buf) |
| 203 |
} |
| 204 |
|
| 205 |
type encoder struct { |
| 206 |
err error |
| 207 |
enc *Encoding |
| 208 |
w io.Writer |
| 209 |
buf [5]byte // buffered data waiting to be encoded |
| 210 |
nbuf int // number of bytes in buf |
| 211 |
out [1024]byte // output buffer |
| 212 |
} |
| 213 |
|
| 214 |
func (e *encoder) Write(p []byte) (n int, err error) { |
| 215 |
if e.err != nil { |
| 216 |
return 0, e.err |
| 217 |
} |
| 218 |
|
| 219 |
// Leading fringe. |
| 220 |
if e.nbuf > 0 { |
| 221 |
var i int |
| 222 |
for i = 0; i < len(p) && e.nbuf < 5; i++ { |
| 223 |
e.buf[e.nbuf] = p[i] |
| 224 |
e.nbuf++ |
| 225 |
} |
| 226 |
n += i |
| 227 |
p = p[i:] |
| 228 |
if e.nbuf < 5 { |
| 229 |
return |
| 230 |
} |
| 231 |
e.enc.Encode(e.out[0:], e.buf[0:]) |
| 232 |
if _, e.err = e.w.Write(e.out[0:8]); e.err != nil { |
| 233 |
return n, e.err |
| 234 |
} |
| 235 |
e.nbuf = 0 |
| 236 |
} |
| 237 |
|
| 238 |
// Large interior chunks. |
| 239 |
for len(p) >= 5 { |
| 240 |
nn := len(e.out) / 8 * 5 |
| 241 |
if nn > len(p) { |
| 242 |
nn = len(p) |
| 243 |
nn -= nn % 5 |
| 244 |
} |
| 245 |
e.enc.Encode(e.out[0:], p[0:nn]) |
| 246 |
if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil { |
| 247 |
return n, e.err |
| 248 |
} |
| 249 |
n += nn |
| 250 |
p = p[nn:] |
| 251 |
} |
| 252 |
|
| 253 |
// Trailing fringe. |
| 254 |
copy(e.buf[:], p) |
| 255 |
e.nbuf = len(p) |
| 256 |
n += len(p) |
| 257 |
return |
| 258 |
} |
| 259 |
|
| 260 |
// Close flushes any pending output from the encoder. |
| 261 |
// It is an error to call Write after calling Close. |
| 262 |
func (e *encoder) Close() error { |
| 263 |
// If there's anything left in the buffer, flush it out |
| 264 |
if e.err == nil && e.nbuf > 0 { |
| 265 |
e.enc.Encode(e.out[0:], e.buf[0:e.nbuf]) |
| 266 |
encodedLen := e.enc.EncodedLen(e.nbuf) |
| 267 |
e.nbuf = 0 |
| 268 |
_, e.err = e.w.Write(e.out[0:encodedLen]) |
| 269 |
} |
| 270 |
return e.err |
| 271 |
} |
| 272 |
|
| 273 |
// NewEncoder returns a new base32 stream encoder. Data written to |
| 274 |
// the returned writer will be encoded using enc and then written to w. |
| 275 |
// Base32 encodings operate in 5-byte blocks; when finished |
| 276 |
// writing, the caller must Close the returned encoder to flush any |
| 277 |
// partially written blocks. |
| 278 |
func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser { |
| 279 |
return &encoder{enc: enc, w: w} |
| 280 |
} |
| 281 |
|
| 282 |
// EncodedLen returns the length in bytes of the base32 encoding |
| 283 |
// of an input buffer of length n. |
| 284 |
func (enc *Encoding) EncodedLen(n int) int { |
| 285 |
if enc.padChar == NoPadding { |
| 286 |
return n/5*8 + (n%5*8+4)/5 |
| 287 |
} |
| 288 |
return (n + 4) / 5 * 8 |
| 289 |
} |
| 290 |
|
| 291 |
/* |
| 292 |
* Decoder |
| 293 |
*/ |
| 294 |
|
| 295 |
type CorruptInputError int64 |
| 296 |
|
| 297 |
func (e CorruptInputError) Error() string { |
| 298 |
return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10) |
| 299 |
} |
| 300 |
|
| 301 |
// decode is like Decode but returns an additional 'end' value, which |
| 302 |
// indicates if end-of-message padding was encountered and thus any |
| 303 |
// additional data is an error. This method assumes that src has been |
| 304 |
// stripped of all supported whitespace ('\r' and '\n'). |
| 305 |
func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { |
| 306 |
// Lift the nil check outside of the loop. |
| 307 |
_ = enc.decodeMap |
| 308 |
|
| 309 |
dsti := 0 |
| 310 |
olen := len(src) |
| 311 |
|
| 312 |
for len(src) > 0 && !end { |
| 313 |
// Decode quantum using the base32 alphabet |
| 314 |
var dbuf [8]byte |
| 315 |
dlen := 8 |
| 316 |
|
| 317 |
for j := 0; j < 8; { |
| 318 |
|
| 319 |
if len(src) == 0 { |
| 320 |
if enc.padChar != NoPadding { |
| 321 |
// We have reached the end and are missing padding |
| 322 |
return n, false, CorruptInputError(olen - len(src) - j) |
| 323 |
} |
| 324 |
// We have reached the end and are not expecting any padding |
| 325 |
dlen, end = j, true |
| 326 |
break |
| 327 |
} |
| 328 |
in := src[0] |
| 329 |
src = src[1:] |
| 330 |
if in == byte(enc.padChar) && j >= 2 && len(src) < 8 { |
| 331 |
// We've reached the end and there's padding |
| 332 |
if len(src)+j < 8-1 { |
| 333 |
// not enough padding |
| 334 |
return n, false, CorruptInputError(olen) |
| 335 |
} |
| 336 |
for k := 0; k < 8-1-j; k++ { |
| 337 |
if len(src) > k && src[k] != byte(enc.padChar) { |
| 338 |
// incorrect padding |
| 339 |
return n, false, CorruptInputError(olen - len(src) + k - 1) |
| 340 |
} |
| 341 |
} |
| 342 |
dlen, end = j, true |
| 343 |
// 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not |
| 344 |
// valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing |
| 345 |
// the five valid padding lengths, and Section 9 "Illustrations and |
| 346 |
// Examples" for an illustration for how the 1st, 3rd and 6th base32 |
| 347 |
// src bytes do not yield enough information to decode a dst byte. |
| 348 |
if dlen == 1 || dlen == 3 || dlen == 6 { |
| 349 |
return n, false, CorruptInputError(olen - len(src) - 1) |
| 350 |
} |
| 351 |
break |
| 352 |
} |
| 353 |
dbuf[j] = enc.decodeMap[in] |
| 354 |
if dbuf[j] == 0xFF { |
| 355 |
return n, false, CorruptInputError(olen - len(src) - 1) |
| 356 |
} |
| 357 |
j++ |
| 358 |
} |
| 359 |
|
| 360 |
// Pack 8x 5-bit source blocks into 5 byte destination |
| 361 |
// quantum |
| 362 |
switch dlen { |
| 363 |
case 8: |
| 364 |
dst[dsti+4] = dbuf[6]<<5 | dbuf[7] |
| 365 |
n++ |
| 366 |
fallthrough |
| 367 |
case 7: |
| 368 |
dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3 |
| 369 |
n++ |
| 370 |
fallthrough |
| 371 |
case 5: |
| 372 |
dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1 |
| 373 |
n++ |
| 374 |
fallthrough |
| 375 |
case 4: |
| 376 |
dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4 |
| 377 |
n++ |
| 378 |
fallthrough |
| 379 |
case 2: |
| 380 |
dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2 |
| 381 |
n++ |
| 382 |
} |
| 383 |
dsti += 5 |
| 384 |
} |
| 385 |
return n, end, nil |
| 386 |
} |
| 387 |
|
| 388 |
// Decode decodes src using the encoding enc. It writes at most |
| 389 |
// [Encoding.DecodedLen](len(src)) bytes to dst and returns the number of bytes |
| 390 |
// written. The caller must ensure that dst is large enough to hold all |
| 391 |
// the decoded data. If src contains invalid base32 data, it will return the |
| 392 |
// number of bytes successfully written and [CorruptInputError]. |
| 393 |
// Newline characters (\r and \n) are ignored. |
| 394 |
func (enc *Encoding) Decode(dst, src []byte) (n int, err error) { |
| 395 |
buf := make([]byte, len(src)) |
| 396 |
l := stripNewlines(buf, src) |
| 397 |
n, _, err = enc.decode(dst, buf[:l]) |
| 398 |
return |
| 399 |
} |
| 400 |
|
| 401 |
// AppendDecode appends the base32 decoded src to dst |
| 402 |
// and returns the extended buffer. |
| 403 |
// If the input is malformed, it returns the partially decoded src and an error. |
| 404 |
// New line characters (\r and \n) are ignored. |
| 405 |
func (enc *Encoding) AppendDecode(dst, src []byte) ([]byte, error) { |
| 406 |
// Compute the output size without padding to avoid over allocating. |
| 407 |
n := len(src) |
| 408 |
for n > 0 && rune(src[n-1]) == enc.padChar { |
| 409 |
n-- |
| 410 |
} |
| 411 |
n = decodedLen(n, NoPadding) |
| 412 |
|
| 413 |
dst = slices.Grow(dst, n) |
| 414 |
n, err := enc.Decode(dst[len(dst):][:n], src) |
| 415 |
return dst[:len(dst)+n], err |
| 416 |
} |
| 417 |
|
| 418 |
// DecodeString returns the bytes represented by the base32 string s. |
| 419 |
// If the input is malformed, it returns the partially decoded data and |
| 420 |
// [CorruptInputError]. New line characters (\r and \n) are ignored. |
| 421 |
func (enc *Encoding) DecodeString(s string) ([]byte, error) { |
| 422 |
buf := []byte(s) |
| 423 |
l := stripNewlines(buf, buf) |
| 424 |
n, _, err := enc.decode(buf, buf[:l]) |
| 425 |
return buf[:n], err |
| 426 |
} |
| 427 |
|
| 428 |
type decoder struct { |
| 429 |
err error |
| 430 |
enc *Encoding |
| 431 |
r io.Reader |
| 432 |
end bool // saw end of message |
| 433 |
buf [1024]byte // leftover input |
| 434 |
nbuf int |
| 435 |
out []byte // leftover decoded output |
| 436 |
outbuf [1024 / 8 * 5]byte |
| 437 |
} |
| 438 |
|
| 439 |
func readEncodedData(r io.Reader, buf []byte, min int, expectsPadding bool) (n int, err error) { |
| 440 |
for n < min && err == nil { |
| 441 |
var nn int |
| 442 |
nn, err = r.Read(buf[n:]) |
| 443 |
n += nn |
| 444 |
} |
| 445 |
// data was read, less than min bytes could be read |
| 446 |
if n < min && n > 0 && err == io.EOF { |
| 447 |
err = io.ErrUnexpectedEOF |
| 448 |
} |
| 449 |
// no data was read, the buffer already contains some data |
| 450 |
// when padding is disabled this is not an error, as the message can be of |
| 451 |
// any length |
| 452 |
if expectsPadding && min < 8 && n == 0 && err == io.EOF { |
| 453 |
err = io.ErrUnexpectedEOF |
| 454 |
} |
| 455 |
return |
| 456 |
} |
| 457 |
|
| 458 |
func (d *decoder) Read(p []byte) (n int, err error) { |
| 459 |
// Use leftover decoded output from last read. |
| 460 |
if len(d.out) > 0 { |
| 461 |
n = copy(p, d.out) |
| 462 |
d.out = d.out[n:] |
| 463 |
if len(d.out) == 0 { |
| 464 |
return n, d.err |
| 465 |
} |
| 466 |
return n, nil |
| 467 |
} |
| 468 |
|
| 469 |
if d.err != nil { |
| 470 |
return 0, d.err |
| 471 |
} |
| 472 |
|
| 473 |
// Read a chunk. |
| 474 |
nn := (len(p) + 4) / 5 * 8 |
| 475 |
if nn < 8 { |
| 476 |
nn = 8 |
| 477 |
} |
| 478 |
if nn > len(d.buf) { |
| 479 |
nn = len(d.buf) |
| 480 |
} |
| 481 |
|
| 482 |
// Minimum amount of bytes that needs to be read each cycle |
| 483 |
var min int |
| 484 |
var expectsPadding bool |
| 485 |
if d.enc.padChar == NoPadding { |
| 486 |
min = 1 |
| 487 |
expectsPadding = false |
| 488 |
} else { |
| 489 |
min = 8 - d.nbuf |
| 490 |
expectsPadding = true |
| 491 |
} |
| 492 |
|
| 493 |
nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], min, expectsPadding) |
| 494 |
d.nbuf += nn |
| 495 |
if d.nbuf < min { |
| 496 |
return 0, d.err |
| 497 |
} |
| 498 |
if nn > 0 && d.end { |
| 499 |
return 0, CorruptInputError(0) |
| 500 |
} |
| 501 |
|
| 502 |
// Decode chunk into p, or d.out and then p if p is too small. |
| 503 |
var nr int |
| 504 |
if d.enc.padChar == NoPadding { |
| 505 |
nr = d.nbuf |
| 506 |
} else { |
| 507 |
nr = d.nbuf / 8 * 8 |
| 508 |
} |
| 509 |
nw := d.enc.DecodedLen(d.nbuf) |
| 510 |
|
| 511 |
if nw > len(p) { |
| 512 |
nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr]) |
| 513 |
d.out = d.outbuf[0:nw] |
| 514 |
n = copy(p, d.out) |
| 515 |
d.out = d.out[n:] |
| 516 |
} else { |
| 517 |
n, d.end, err = d.enc.decode(p, d.buf[0:nr]) |
| 518 |
} |
| 519 |
d.nbuf -= nr |
| 520 |
for i := 0; i < d.nbuf; i++ { |
| 521 |
d.buf[i] = d.buf[i+nr] |
| 522 |
} |
| 523 |
|
| 524 |
if err != nil && (d.err == nil || d.err == io.EOF) { |
| 525 |
d.err = err |
| 526 |
} |
| 527 |
|
| 528 |
if len(d.out) > 0 { |
| 529 |
// We cannot return all the decoded bytes to the caller in this |
| 530 |
// invocation of Read, so we return a nil error to ensure that Read |
| 531 |
// will be called again. The error stored in d.err, if any, will be |
| 532 |
// returned with the last set of decoded bytes. |
| 533 |
return n, nil |
| 534 |
} |
| 535 |
|
| 536 |
return n, d.err |
| 537 |
} |
| 538 |
|
| 539 |
type newlineFilteringReader struct { |
| 540 |
wrapped io.Reader |
| 541 |
} |
| 542 |
|
| 543 |
// stripNewlines removes newline characters and returns the number |
| 544 |
// of non-newline characters copied to dst. |
| 545 |
func stripNewlines(dst, src []byte) int { |
| 546 |
offset := 0 |
| 547 |
for _, b := range src { |
| 548 |
if b == '\r' || b == '\n' { |
| 549 |
continue |
| 550 |
} |
| 551 |
dst[offset] = b |
| 552 |
offset++ |
| 553 |
} |
| 554 |
return offset |
| 555 |
} |
| 556 |
|
| 557 |
func (r *newlineFilteringReader) Read(p []byte) (int, error) { |
| 558 |
n, err := r.wrapped.Read(p) |
| 559 |
for n > 0 { |
| 560 |
s := p[0:n] |
| 561 |
offset := stripNewlines(s, s) |
| 562 |
if err != nil || offset > 0 { |
| 563 |
return offset, err |
| 564 |
} |
| 565 |
// Previous buffer entirely whitespace, read again |
| 566 |
n, err = r.wrapped.Read(p) |
| 567 |
} |
| 568 |
return n, err |
| 569 |
} |
| 570 |
|
| 571 |
// NewDecoder constructs a new base32 stream decoder. |
| 572 |
func NewDecoder(enc *Encoding, r io.Reader) io.Reader { |
| 573 |
return &decoder{enc: enc, r: &newlineFilteringReader{r}} |
| 574 |
} |
| 575 |
|
| 576 |
// DecodedLen returns the maximum length in bytes of the decoded data |
| 577 |
// corresponding to n bytes of base32-encoded data. |
| 578 |
func (enc *Encoding) DecodedLen(n int) int { |
| 579 |
return decodedLen(n, enc.padChar) |
| 580 |
} |
| 581 |
|
| 582 |
func decodedLen(n int, padChar rune) int { |
| 583 |
if padChar == NoPadding { |
| 584 |
return n/8*5 + n%8*5/8 |
| 585 |
} |
| 586 |
return n / 8 * 5 |
| 587 |
} |