github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/encoding/base32/base32.go (about) 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 "bytes" 10 "io" 11 "strconv" 12 "strings" 13 ) 14 15 /* 16 * Encodings 17 */ 18 19 // An Encoding is a radix 32 encoding/decoding scheme, defined by a 20 // 32-character alphabet. The most common is the "base32" encoding 21 // introduced for SASL GSSAPI and standardized in RFC 4648. 22 // The alternate "base32hex" encoding is used in DNSSEC. 23 type Encoding struct { 24 encode string 25 decodeMap [256]byte 26 padChar rune 27 } 28 29 const ( 30 StdPadding rune = '=' // Standard padding character 31 NoPadding rune = -1 // No padding 32 ) 33 34 const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" 35 const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV" 36 37 // NewEncoding returns a new Encoding defined by the given alphabet, 38 // which must be a 32-byte string. 39 func NewEncoding(encoder string) *Encoding { 40 e := new(Encoding) 41 e.encode = encoder 42 e.padChar = StdPadding 43 44 for i := 0; i < len(e.decodeMap); i++ { 45 e.decodeMap[i] = 0xFF 46 } 47 for i := 0; i < len(encoder); i++ { 48 e.decodeMap[encoder[i]] = byte(i) 49 } 50 return e 51 } 52 53 // StdEncoding is the standard base32 encoding, as defined in 54 // RFC 4648. 55 var StdEncoding = NewEncoding(encodeStd) 56 57 // HexEncoding is the ``Extended Hex Alphabet'' defined in RFC 4648. 58 // It is typically used in DNS. 59 var HexEncoding = NewEncoding(encodeHex) 60 61 var removeNewlinesMapper = func(r rune) rune { 62 if r == '\r' || r == '\n' { 63 return -1 64 } 65 return r 66 } 67 68 // WithPadding creates a new encoding identical to enc except 69 // with a specified padding character, or NoPadding to disable padding. 70 // The padding character must not be '\r' or '\n', must not 71 // be contained in the encoding's alphabet and must be a rune equal or 72 // below '\xff'. 73 func (enc Encoding) WithPadding(padding rune) *Encoding { 74 if padding == '\r' || padding == '\n' || padding > 0xff { 75 panic("invalid padding") 76 } 77 78 for i := 0; i < len(enc.encode); i++ { 79 if rune(enc.encode[i]) == padding { 80 panic("padding contained in alphabet") 81 } 82 } 83 84 enc.padChar = padding 85 return &enc 86 } 87 88 /* 89 * Encoder 90 */ 91 92 // Encode encodes src using the encoding enc, writing 93 // EncodedLen(len(src)) bytes to dst. 94 // 95 // The encoding pads the output to a multiple of 8 bytes, 96 // so Encode is not appropriate for use on individual blocks 97 // of a large data stream. Use NewEncoder() instead. 98 func (enc *Encoding) Encode(dst, src []byte) { 99 if len(src) == 0 { 100 return 101 } 102 103 for len(src) > 0 { 104 var b [8]byte 105 106 // Unpack 8x 5-bit source blocks into a 5 byte 107 // destination quantum 108 switch len(src) { 109 default: 110 b[7] = src[4] & 0x1F 111 b[6] = src[4] >> 5 112 fallthrough 113 case 4: 114 b[6] |= (src[3] << 3) & 0x1F 115 b[5] = (src[3] >> 2) & 0x1F 116 b[4] = src[3] >> 7 117 fallthrough 118 case 3: 119 b[4] |= (src[2] << 1) & 0x1F 120 b[3] = (src[2] >> 4) & 0x1F 121 fallthrough 122 case 2: 123 b[3] |= (src[1] << 4) & 0x1F 124 b[2] = (src[1] >> 1) & 0x1F 125 b[1] = (src[1] >> 6) & 0x1F 126 fallthrough 127 case 1: 128 b[1] |= (src[0] << 2) & 0x1F 129 b[0] = src[0] >> 3 130 } 131 132 // Encode 5-bit blocks using the base32 alphabet 133 size := len(dst) 134 if size >= 8 { 135 // Common case, unrolled for extra performance 136 dst[0] = enc.encode[b[0]] 137 dst[1] = enc.encode[b[1]] 138 dst[2] = enc.encode[b[2]] 139 dst[3] = enc.encode[b[3]] 140 dst[4] = enc.encode[b[4]] 141 dst[5] = enc.encode[b[5]] 142 dst[6] = enc.encode[b[6]] 143 dst[7] = enc.encode[b[7]] 144 } else { 145 for i := 0; i < size; i++ { 146 dst[i] = enc.encode[b[i]] 147 } 148 } 149 150 // Pad the final quantum 151 if len(src) < 5 { 152 if enc.padChar == NoPadding { 153 break 154 } 155 156 dst[7] = byte(enc.padChar) 157 if len(src) < 4 { 158 dst[6] = byte(enc.padChar) 159 dst[5] = byte(enc.padChar) 160 if len(src) < 3 { 161 dst[4] = byte(enc.padChar) 162 if len(src) < 2 { 163 dst[3] = byte(enc.padChar) 164 dst[2] = byte(enc.padChar) 165 } 166 } 167 } 168 169 break 170 } 171 172 src = src[5:] 173 dst = dst[8:] 174 } 175 } 176 177 // EncodeToString returns the base32 encoding of src. 178 func (enc *Encoding) EncodeToString(src []byte) string { 179 buf := make([]byte, enc.EncodedLen(len(src))) 180 enc.Encode(buf, src) 181 return string(buf) 182 } 183 184 type encoder struct { 185 err error 186 enc *Encoding 187 w io.Writer 188 buf [5]byte // buffered data waiting to be encoded 189 nbuf int // number of bytes in buf 190 out [1024]byte // output buffer 191 } 192 193 func (e *encoder) Write(p []byte) (n int, err error) { 194 if e.err != nil { 195 return 0, e.err 196 } 197 198 // Leading fringe. 199 if e.nbuf > 0 { 200 var i int 201 for i = 0; i < len(p) && e.nbuf < 5; i++ { 202 e.buf[e.nbuf] = p[i] 203 e.nbuf++ 204 } 205 n += i 206 p = p[i:] 207 if e.nbuf < 5 { 208 return 209 } 210 e.enc.Encode(e.out[0:], e.buf[0:]) 211 if _, e.err = e.w.Write(e.out[0:8]); e.err != nil { 212 return n, e.err 213 } 214 e.nbuf = 0 215 } 216 217 // Large interior chunks. 218 for len(p) >= 5 { 219 nn := len(e.out) / 8 * 5 220 if nn > len(p) { 221 nn = len(p) 222 nn -= nn % 5 223 } 224 e.enc.Encode(e.out[0:], p[0:nn]) 225 if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil { 226 return n, e.err 227 } 228 n += nn 229 p = p[nn:] 230 } 231 232 // Trailing fringe. 233 for i := 0; i < len(p); i++ { 234 e.buf[i] = p[i] 235 } 236 e.nbuf = len(p) 237 n += len(p) 238 return 239 } 240 241 // Close flushes any pending output from the encoder. 242 // It is an error to call Write after calling Close. 243 func (e *encoder) Close() error { 244 // If there's anything left in the buffer, flush it out 245 if e.err == nil && e.nbuf > 0 { 246 e.enc.Encode(e.out[0:], e.buf[0:e.nbuf]) 247 e.nbuf = 0 248 _, e.err = e.w.Write(e.out[0:8]) 249 } 250 return e.err 251 } 252 253 // NewEncoder returns a new base32 stream encoder. Data written to 254 // the returned writer will be encoded using enc and then written to w. 255 // Base32 encodings operate in 5-byte blocks; when finished 256 // writing, the caller must Close the returned encoder to flush any 257 // partially written blocks. 258 func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser { 259 return &encoder{enc: enc, w: w} 260 } 261 262 // EncodedLen returns the length in bytes of the base32 encoding 263 // of an input buffer of length n. 264 func (enc *Encoding) EncodedLen(n int) int { 265 if enc.padChar == NoPadding { 266 return (n*8 + 4) / 5 267 } 268 return (n + 4) / 5 * 8 269 } 270 271 /* 272 * Decoder 273 */ 274 275 type CorruptInputError int64 276 277 func (e CorruptInputError) Error() string { 278 return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10) 279 } 280 281 // decode is like Decode but returns an additional 'end' value, which 282 // indicates if end-of-message padding was encountered and thus any 283 // additional data is an error. This method assumes that src has been 284 // stripped of all supported whitespace ('\r' and '\n'). 285 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { 286 olen := len(src) 287 for len(src) > 0 && !end { 288 // Decode quantum using the base32 alphabet 289 var dbuf [8]byte 290 dlen := 8 291 292 for j := 0; j < 8; { 293 // We have reached the end and are missing padding 294 if len(src) == 0 && enc.padChar != NoPadding { 295 return n, false, CorruptInputError(olen - len(src) - j) 296 } 297 298 // We have reached the end and are not expecing any padding 299 if len(src) == 0 && enc.padChar == NoPadding { 300 dlen, end = j, true 301 break 302 } 303 304 in := src[0] 305 src = src[1:] 306 if in == byte(enc.padChar) && j >= 2 && len(src) < 8 { 307 // We've reached the end and there's padding 308 if len(src)+j < 8-1 { 309 // not enough padding 310 return n, false, CorruptInputError(olen) 311 } 312 for k := 0; k < 8-1-j; k++ { 313 if len(src) > k && src[k] != byte(enc.padChar) { 314 // incorrect padding 315 return n, false, CorruptInputError(olen - len(src) + k - 1) 316 } 317 } 318 dlen, end = j, true 319 // 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not 320 // valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing 321 // the five valid padding lengths, and Section 9 "Illustrations and 322 // Examples" for an illustration for how the 1st, 3rd and 6th base32 323 // src bytes do not yield enough information to decode a dst byte. 324 if dlen == 1 || dlen == 3 || dlen == 6 { 325 return n, false, CorruptInputError(olen - len(src) - 1) 326 } 327 break 328 } 329 dbuf[j] = enc.decodeMap[in] 330 if dbuf[j] == 0xFF { 331 return n, false, CorruptInputError(olen - len(src) - 1) 332 } 333 j++ 334 } 335 336 // Pack 8x 5-bit source blocks into 5 byte destination 337 // quantum 338 switch dlen { 339 case 8: 340 dst[4] = dbuf[6]<<5 | dbuf[7] 341 fallthrough 342 case 7: 343 dst[3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3 344 fallthrough 345 case 5: 346 dst[2] = dbuf[3]<<4 | dbuf[4]>>1 347 fallthrough 348 case 4: 349 dst[1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4 350 fallthrough 351 case 2: 352 dst[0] = dbuf[0]<<3 | dbuf[1]>>2 353 } 354 355 if !end { 356 dst = dst[5:] 357 } 358 359 switch dlen { 360 case 2: 361 n += 1 362 case 4: 363 n += 2 364 case 5: 365 n += 3 366 case 7: 367 n += 4 368 case 8: 369 n += 5 370 } 371 } 372 return n, end, nil 373 } 374 375 // Decode decodes src using the encoding enc. It writes at most 376 // DecodedLen(len(src)) bytes to dst and returns the number of bytes 377 // written. If src contains invalid base32 data, it will return the 378 // number of bytes successfully written and CorruptInputError. 379 // New line characters (\r and \n) are ignored. 380 func (enc *Encoding) Decode(dst, src []byte) (n int, err error) { 381 src = bytes.Map(removeNewlinesMapper, src) 382 n, _, err = enc.decode(dst, src) 383 return 384 } 385 386 // DecodeString returns the bytes represented by the base32 string s. 387 func (enc *Encoding) DecodeString(s string) ([]byte, error) { 388 s = strings.Map(removeNewlinesMapper, s) 389 dbuf := make([]byte, enc.DecodedLen(len(s))) 390 n, _, err := enc.decode(dbuf, []byte(s)) 391 return dbuf[:n], err 392 } 393 394 type decoder struct { 395 err error 396 enc *Encoding 397 r io.Reader 398 end bool // saw end of message 399 buf [1024]byte // leftover input 400 nbuf int 401 out []byte // leftover decoded output 402 outbuf [1024 / 8 * 5]byte 403 } 404 405 func readEncodedData(r io.Reader, buf []byte, min int) (n int, err error) { 406 for n < min && err == nil { 407 var nn int 408 nn, err = r.Read(buf[n:]) 409 n += nn 410 } 411 if n < min && n > 0 && err == io.EOF { 412 err = io.ErrUnexpectedEOF 413 } 414 return 415 } 416 417 func (d *decoder) Read(p []byte) (n int, err error) { 418 // Use leftover decoded output from last read. 419 if len(d.out) > 0 { 420 n = copy(p, d.out) 421 d.out = d.out[n:] 422 if len(d.out) == 0 { 423 return n, d.err 424 } 425 return n, nil 426 } 427 428 if d.err != nil { 429 return 0, d.err 430 } 431 432 // Read a chunk. 433 nn := len(p) / 5 * 8 434 if nn < 8 { 435 nn = 8 436 } 437 if nn > len(d.buf) { 438 nn = len(d.buf) 439 } 440 441 nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], 8-d.nbuf) 442 d.nbuf += nn 443 if d.nbuf < 8 { 444 return 0, d.err 445 } 446 447 // Decode chunk into p, or d.out and then p if p is too small. 448 nr := d.nbuf / 8 * 8 449 nw := d.nbuf / 8 * 5 450 if nw > len(p) { 451 nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr]) 452 d.out = d.outbuf[0:nw] 453 n = copy(p, d.out) 454 d.out = d.out[n:] 455 } else { 456 n, d.end, err = d.enc.decode(p, d.buf[0:nr]) 457 } 458 d.nbuf -= nr 459 for i := 0; i < d.nbuf; i++ { 460 d.buf[i] = d.buf[i+nr] 461 } 462 463 if err != nil && (d.err == nil || d.err == io.EOF) { 464 d.err = err 465 } 466 467 if len(d.out) > 0 { 468 // We cannot return all the decoded bytes to the caller in this 469 // invocation of Read, so we return a nil error to ensure that Read 470 // will be called again. The error stored in d.err, if any, will be 471 // returned with the last set of decoded bytes. 472 return n, nil 473 } 474 475 return n, d.err 476 } 477 478 type newlineFilteringReader struct { 479 wrapped io.Reader 480 } 481 482 func (r *newlineFilteringReader) Read(p []byte) (int, error) { 483 n, err := r.wrapped.Read(p) 484 for n > 0 { 485 offset := 0 486 for i, b := range p[0:n] { 487 if b != '\r' && b != '\n' { 488 if i != offset { 489 p[offset] = b 490 } 491 offset++ 492 } 493 } 494 if err != nil || offset > 0 { 495 return offset, err 496 } 497 // Previous buffer entirely whitespace, read again 498 n, err = r.wrapped.Read(p) 499 } 500 return n, err 501 } 502 503 // NewDecoder constructs a new base32 stream decoder. 504 func NewDecoder(enc *Encoding, r io.Reader) io.Reader { 505 return &decoder{enc: enc, r: &newlineFilteringReader{r}} 506 } 507 508 // DecodedLen returns the maximum length in bytes of the decoded data 509 // corresponding to n bytes of base32-encoded data. 510 func (enc *Encoding) DecodedLen(n int) int { 511 if enc.padChar == NoPadding { 512 return n * 5 / 8 513 } 514 515 return n / 8 * 5 516 }