github.com/remobjects/goldbaselibrary@v0.0.0-20230924164425-d458680a936b/Source/Gold/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 [32]byte 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 if len(encoder) != 32 { 41 panic("encoding alphabet is not 32-bytes long") 42 } 43 44 e := new(Encoding) 45 copy(e.encode[:], encoder) 46 e.padChar = StdPadding 47 48 for i := 0; i < len(e.decodeMap); i++ { 49 e.decodeMap[i] = 0xFF 50 } 51 for i := 0; i < len(encoder); i++ { 52 e.decodeMap[encoder[i]] = byte(i) 53 } 54 return e 55 } 56 57 // StdEncoding is the standard base32 encoding, as defined in 58 // RFC 4648. 59 var StdEncoding = NewEncoding(encodeStd) 60 61 // HexEncoding is the ``Extended Hex Alphabet'' defined in RFC 4648. 62 // It is typically used in DNS. 63 var HexEncoding = NewEncoding(encodeHex) 64 65 var removeNewlinesMapper = func(r rune) rune { 66 if r == '\r' || r == '\n' { 67 return -1 68 } 69 return r 70 } 71 72 // WithPadding creates a new encoding identical to enc except 73 // with a specified padding character, or NoPadding to disable padding. 74 // The padding character must not be '\r' or '\n', must not 75 // be contained in the encoding's alphabet and must be a rune equal or 76 // below '\xff'. 77 func (enc Encoding) WithPadding(padding rune) *Encoding { 78 if padding == '\r' || padding == '\n' || padding > 0xff { 79 panic("invalid padding") 80 } 81 82 for i := 0; i < len(enc.encode); i++ { 83 if rune(enc.encode[i]) == padding { 84 panic("padding contained in alphabet") 85 } 86 } 87 88 enc.padChar = padding 89 return &enc 90 } 91 92 /* 93 * Encoder 94 */ 95 96 // Encode encodes src using the encoding enc, writing 97 // EncodedLen(len(src)) bytes to dst. 98 // 99 // The encoding pads the output to a multiple of 8 bytes, 100 // so Encode is not appropriate for use on individual blocks 101 // of a large data stream. Use NewEncoder() instead. 102 func (enc *Encoding) Encode(dst, src []byte) { 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]&31] 137 dst[1] = enc.encode[b[1]&31] 138 dst[2] = enc.encode[b[2]&31] 139 dst[3] = enc.encode[b[3]&31] 140 dst[4] = enc.encode[b[4]&31] 141 dst[5] = enc.encode[b[5]&31] 142 dst[6] = enc.encode[b[6]&31] 143 dst[7] = enc.encode[b[7]&31] 144 } else { 145 for i := 0; i < size; i++ { 146 dst[i] = enc.encode[b[i]&31] 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 encodedLen := e.enc.EncodedLen(e.nbuf) 248 e.nbuf = 0 249 _, e.err = e.w.Write(e.out[0:encodedLen]) 250 } 251 return e.err 252 } 253 254 // NewEncoder returns a new base32 stream encoder. Data written to 255 // the returned writer will be encoded using enc and then written to w. 256 // Base32 encodings operate in 5-byte blocks; when finished 257 // writing, the caller must Close the returned encoder to flush any 258 // partially written blocks. 259 func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser { 260 return &encoder{enc: enc, w: w} 261 } 262 263 // EncodedLen returns the length in bytes of the base32 encoding 264 // of an input buffer of length n. 265 func (enc *Encoding) EncodedLen(n int) int { 266 if enc.padChar == NoPadding { 267 return (n*8 + 4) / 5 268 } 269 return (n + 4) / 5 * 8 270 } 271 272 /* 273 * Decoder 274 */ 275 276 type CorruptInputError int64 277 278 func (e CorruptInputError) Error() string { 279 return "illegal base32 data at input byte " + strconv.FormatInt(int64(e), 10) 280 } 281 282 // decode is like Decode but returns an additional 'end' value, which 283 // indicates if end-of-message padding was encountered and thus any 284 // additional data is an error. This method assumes that src has been 285 // stripped of all supported whitespace ('\r' and '\n'). 286 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) { 287 // Lift the nil check outside of the loop. 288 _ = enc.decodeMap 289 290 dsti := 0 291 olen := len(src) 292 293 for len(src) > 0 && !end { 294 // Decode quantum using the base32 alphabet 295 var dbuf [8]byte 296 dlen := 8 297 298 for j := 0; j < 8; { 299 300 if len(src) == 0 { 301 if enc.padChar != NoPadding { 302 // We have reached the end and are missing padding 303 return n, false, CorruptInputError(olen - len(src) - j) 304 } 305 // We have reached the end and are not expecing any padding 306 dlen, end = j, true 307 break 308 } 309 in := src[0] 310 src = src[1:] 311 if in == byte(enc.padChar) && j >= 2 && len(src) < 8 { 312 // We've reached the end and there's padding 313 if len(src)+j < 8-1 { 314 // not enough padding 315 return n, false, CorruptInputError(olen) 316 } 317 for k := 0; k < 8-1-j; k++ { 318 if len(src) > k && src[k] != byte(enc.padChar) { 319 // incorrect padding 320 return n, false, CorruptInputError(olen - len(src) + k - 1) 321 } 322 } 323 dlen, end = j, true 324 // 7, 5 and 2 are not valid padding lengths, and so 1, 3 and 6 are not 325 // valid dlen values. See RFC 4648 Section 6 "Base 32 Encoding" listing 326 // the five valid padding lengths, and Section 9 "Illustrations and 327 // Examples" for an illustration for how the 1st, 3rd and 6th base32 328 // src bytes do not yield enough information to decode a dst byte. 329 if dlen == 1 || dlen == 3 || dlen == 6 { 330 return n, false, CorruptInputError(olen - len(src) - 1) 331 } 332 break 333 } 334 dbuf[j] = enc.decodeMap[in] 335 if dbuf[j] == 0xFF { 336 return n, false, CorruptInputError(olen - len(src) - 1) 337 } 338 j++ 339 } 340 341 // Pack 8x 5-bit source blocks into 5 byte destination 342 // quantum 343 switch dlen { 344 case 8: 345 dst[dsti+4] = dbuf[6]<<5 | dbuf[7] 346 n++ 347 fallthrough 348 case 7: 349 dst[dsti+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3 350 n++ 351 fallthrough 352 case 5: 353 dst[dsti+2] = dbuf[3]<<4 | dbuf[4]>>1 354 n++ 355 fallthrough 356 case 4: 357 dst[dsti+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4 358 n++ 359 fallthrough 360 case 2: 361 dst[dsti+0] = dbuf[0]<<3 | dbuf[1]>>2 362 n++ 363 } 364 dsti += 5 365 } 366 return n, end, nil 367 } 368 369 // Decode decodes src using the encoding enc. It writes at most 370 // DecodedLen(len(src)) bytes to dst and returns the number of bytes 371 // written. If src contains invalid base32 data, it will return the 372 // number of bytes successfully written and CorruptInputError. 373 // New line characters (\r and \n) are ignored. 374 func (enc *Encoding) Decode(dst, src []byte) (n int, err error) { 375 src = bytes.Map(removeNewlinesMapper, src) 376 n, _, err = enc.decode(dst, src) 377 return 378 } 379 380 // DecodeString returns the bytes represented by the base32 string s. 381 func (enc *Encoding) DecodeString(s string) ([]byte, error) { 382 s = strings.Map(removeNewlinesMapper, s) 383 dbuf := make([]byte, enc.DecodedLen(len(s))) 384 n, _, err := enc.decode(dbuf, []byte(s)) 385 return dbuf[:n], err 386 } 387 388 type decoder struct { 389 err error 390 enc *Encoding 391 r io.Reader 392 end bool // saw end of message 393 buf [1024]byte // leftover input 394 nbuf int 395 out []byte // leftover decoded output 396 outbuf [1024 / 8 * 5]byte 397 } 398 399 func readEncodedData(r io.Reader, buf []byte, min int, expectsPadding bool) (n int, err error) { 400 for n < min && err == nil { 401 var nn int 402 nn, err = r.Read(buf[n:]) 403 n += nn 404 } 405 // data was read, less than min bytes could be read 406 if n < min && n > 0 && err == io.EOF { 407 err = io.ErrUnexpectedEOF 408 } 409 // no data was read, the buffer already contains some data 410 // when padding is disabled this is not an error, as the message can be of 411 // any length 412 if expectsPadding && min < 8 && n == 0 && err == io.EOF { 413 err = io.ErrUnexpectedEOF 414 } 415 return 416 } 417 418 func (d *decoder) Read(p []byte) (n int, err error) { 419 // Use leftover decoded output from last read. 420 if len(d.out) > 0 { 421 n = copy(p, d.out) 422 d.out = d.out[n:] 423 if len(d.out) == 0 { 424 return n, d.err 425 } 426 return n, nil 427 } 428 429 if d.err != nil { 430 return 0, d.err 431 } 432 433 // Read a chunk. 434 nn := len(p) / 5 * 8 435 if nn < 8 { 436 nn = 8 437 } 438 if nn > len(d.buf) { 439 nn = len(d.buf) 440 } 441 442 // Minimum amount of bytes that needs to be read each cycle 443 var min int 444 var expectsPadding bool 445 if d.enc.padChar == NoPadding { 446 min = 1 447 expectsPadding = false 448 } else { 449 min = 8 - d.nbuf 450 expectsPadding = true 451 } 452 453 nn, d.err = readEncodedData(d.r, d.buf[d.nbuf:nn], min, expectsPadding) 454 d.nbuf += nn 455 if d.nbuf < min { 456 return 0, d.err 457 } 458 459 // Decode chunk into p, or d.out and then p if p is too small. 460 var nr int 461 if d.enc.padChar == NoPadding { 462 nr = d.nbuf 463 } else { 464 nr = d.nbuf / 8 * 8 465 } 466 nw := d.enc.DecodedLen(d.nbuf) 467 468 if nw > len(p) { 469 nw, d.end, err = d.enc.decode(d.outbuf[0:], d.buf[0:nr]) 470 d.out = d.outbuf[0:nw] 471 n = copy(p, d.out) 472 d.out = d.out[n:] 473 } else { 474 n, d.end, err = d.enc.decode(p, d.buf[0:nr]) 475 } 476 d.nbuf -= nr 477 for i := 0; i < d.nbuf; i++ { 478 d.buf[i] = d.buf[i+nr] 479 } 480 481 if err != nil && (d.err == nil || d.err == io.EOF) { 482 d.err = err 483 } 484 485 if len(d.out) > 0 { 486 // We cannot return all the decoded bytes to the caller in this 487 // invocation of Read, so we return a nil error to ensure that Read 488 // will be called again. The error stored in d.err, if any, will be 489 // returned with the last set of decoded bytes. 490 return n, nil 491 } 492 493 return n, d.err 494 } 495 496 type newlineFilteringReader struct { 497 wrapped io.Reader 498 } 499 500 func (r *newlineFilteringReader) Read(p []byte) (int, error) { 501 n, err := r.wrapped.Read(p) 502 for n > 0 { 503 offset := 0 504 for i, b := range p[0:n] { 505 if b != '\r' && b != '\n' { 506 if i != offset { 507 p[offset] = b 508 } 509 offset++ 510 } 511 } 512 if err != nil || offset > 0 { 513 return offset, err 514 } 515 // Previous buffer entirely whitespace, read again 516 n, err = r.wrapped.Read(p) 517 } 518 return n, err 519 } 520 521 // NewDecoder constructs a new base32 stream decoder. 522 func NewDecoder(enc *Encoding, r io.Reader) io.Reader { 523 return &decoder{enc: enc, r: &newlineFilteringReader{r}} 524 } 525 526 // DecodedLen returns the maximum length in bytes of the decoded data 527 // corresponding to n bytes of base32-encoded data. 528 func (enc *Encoding) DecodedLen(n int) int { 529 if enc.padChar == NoPadding { 530 return n * 5 / 8 531 } 532 533 return n / 8 * 5 534 }