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