OLD | NEW |
(Empty) | |
| 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 "os" |
| 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 string |
| 24 decodeMap [256]byte |
| 25 } |
| 26 |
| 27 const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567" |
| 28 const encodeHex = "0123456789ABCDEFGHIJKLMNOPQRSTUV" |
| 29 |
| 30 // NewEncoding returns a new Encoding defined by the given alphabet, |
| 31 // which must be a 32-byte string. |
| 32 func NewEncoding(encoder string) *Encoding { |
| 33 e := new(Encoding) |
| 34 e.encode = encoder |
| 35 for i := 0; i < len(e.decodeMap); i++ { |
| 36 e.decodeMap[i] = 0xFF |
| 37 } |
| 38 for i := 0; i < len(encoder); i++ { |
| 39 e.decodeMap[encoder[i]] = byte(i) |
| 40 } |
| 41 return e |
| 42 } |
| 43 |
| 44 // StdEncoding is the standard base32 encoding, as defined in |
| 45 // RFC 4648. |
| 46 var StdEncoding = NewEncoding(encodeStd) |
| 47 |
| 48 // HexEncoding is the ``Extended Hex Alphabet'' defined in RFC 4648. |
| 49 // It is typically used in DNS. |
| 50 var HexEncoding = NewEncoding(encodeHex) |
| 51 |
| 52 /* |
| 53 * Encoder |
| 54 */ |
| 55 |
| 56 // Encode encodes src using the encoding enc, writing |
| 57 // EncodedLen(len(src)) bytes to dst. |
| 58 // |
| 59 // The encoding pads the output to a multiple of 8 bytes, |
| 60 // so Encode is not appropriate for use on individual blocks |
| 61 // of a large data stream. Use NewEncoder() instead. |
| 62 func (enc *Encoding) Encode(dst, src []byte) { |
| 63 if len(src) == 0 { |
| 64 return |
| 65 } |
| 66 |
| 67 for len(src) > 0 { |
| 68 dst[0] = 0 |
| 69 dst[1] = 0 |
| 70 dst[2] = 0 |
| 71 dst[3] = 0 |
| 72 dst[4] = 0 |
| 73 dst[5] = 0 |
| 74 dst[6] = 0 |
| 75 dst[7] = 0 |
| 76 |
| 77 // Unpack 8x 5-bit source blocks into a 5 byte |
| 78 // destination quantum |
| 79 switch len(src) { |
| 80 default: |
| 81 dst[7] |= src[4] & 0x1F |
| 82 dst[6] |= src[4] >> 5 |
| 83 fallthrough |
| 84 case 4: |
| 85 dst[6] |= (src[3] << 3) & 0x1F |
| 86 dst[5] |= (src[3] >> 2) & 0x1F |
| 87 dst[4] |= src[3] >> 7 |
| 88 fallthrough |
| 89 case 3: |
| 90 dst[4] |= (src[2] << 1) & 0x1F |
| 91 dst[3] |= (src[2] >> 4) & 0x1F |
| 92 fallthrough |
| 93 case 2: |
| 94 dst[3] |= (src[1] << 4) & 0x1F |
| 95 dst[2] |= (src[1] >> 1) & 0x1F |
| 96 dst[1] |= (src[1] >> 6) & 0x1F |
| 97 fallthrough |
| 98 case 1: |
| 99 dst[1] |= (src[0] << 2) & 0x1F |
| 100 dst[0] |= src[0] >> 3 |
| 101 } |
| 102 |
| 103 // Encode 5-bit blocks using the base32 alphabet |
| 104 for j := 0; j < 8; j++ { |
| 105 dst[j] = enc.encode[dst[j]] |
| 106 } |
| 107 |
| 108 // Pad the final quantum |
| 109 if len(src) < 5 { |
| 110 dst[7] = '=' |
| 111 if len(src) < 4 { |
| 112 dst[6] = '=' |
| 113 dst[5] = '=' |
| 114 if len(src) < 3 { |
| 115 dst[4] = '=' |
| 116 if len(src) < 2 { |
| 117 dst[3] = '=' |
| 118 dst[2] = '=' |
| 119 } |
| 120 } |
| 121 } |
| 122 break |
| 123 } |
| 124 src = src[5:] |
| 125 dst = dst[8:] |
| 126 } |
| 127 } |
| 128 |
| 129 type encoder struct { |
| 130 err os.Error |
| 131 enc *Encoding |
| 132 w io.Writer |
| 133 buf [5]byte // buffered data waiting to be encoded |
| 134 nbuf int // number of bytes in buf |
| 135 out [1024]byte // output buffer |
| 136 } |
| 137 |
| 138 func (e *encoder) Write(p []byte) (n int, err os.Error) { |
| 139 if e.err != nil { |
| 140 return 0, e.err |
| 141 } |
| 142 |
| 143 // Leading fringe. |
| 144 if e.nbuf > 0 { |
| 145 var i int |
| 146 for i = 0; i < len(p) && e.nbuf < 5; i++ { |
| 147 e.buf[e.nbuf] = p[i] |
| 148 e.nbuf++ |
| 149 } |
| 150 n += i |
| 151 p = p[i:] |
| 152 if e.nbuf < 5 { |
| 153 return |
| 154 } |
| 155 e.enc.Encode(e.out[0:], e.buf[0:]) |
| 156 if _, e.err = e.w.Write(e.out[0:8]); e.err != nil { |
| 157 return n, e.err |
| 158 } |
| 159 e.nbuf = 0 |
| 160 } |
| 161 |
| 162 // Large interior chunks. |
| 163 for len(p) >= 5 { |
| 164 nn := len(e.out) / 8 * 5 |
| 165 if nn > len(p) { |
| 166 nn = len(p) |
| 167 } |
| 168 nn -= nn % 5 |
| 169 if nn > 0 { |
| 170 e.enc.Encode(e.out[0:], p[0:nn]) |
| 171 if _, e.err = e.w.Write(e.out[0 : nn/5*8]); e.err != nil
{ |
| 172 return n, e.err |
| 173 } |
| 174 } |
| 175 n += nn |
| 176 p = p[nn:] |
| 177 } |
| 178 |
| 179 // Trailing fringe. |
| 180 for i := 0; i < len(p); i++ { |
| 181 e.buf[i] = p[i] |
| 182 } |
| 183 e.nbuf = len(p) |
| 184 n += len(p) |
| 185 return |
| 186 } |
| 187 |
| 188 // Close flushes any pending output from the encoder. |
| 189 // It is an error to call Write after calling Close. |
| 190 func (e *encoder) Close() os.Error { |
| 191 // If there's anything left in the buffer, flush it out |
| 192 if e.err == nil && e.nbuf > 0 { |
| 193 e.enc.Encode(e.out[0:], e.buf[0:e.nbuf]) |
| 194 e.nbuf = 0 |
| 195 _, e.err = e.w.Write(e.out[0:8]) |
| 196 } |
| 197 return e.err |
| 198 } |
| 199 |
| 200 // NewEncoder returns a new base32 stream encoder. Data written to |
| 201 // the returned writer will be encoded using enc and then written to w. |
| 202 // Base32 encodings operate in 5-byte blocks; when finished |
| 203 // writing, the caller must Close the returned encoder to flush any |
| 204 // partially written blocks. |
| 205 func NewEncoder(enc *Encoding, w io.Writer) io.WriteCloser { |
| 206 return &encoder{enc: enc, w: w} |
| 207 } |
| 208 |
| 209 // EncodedLen returns the length in bytes of the base32 encoding |
| 210 // of an input buffer of length n. |
| 211 func (enc *Encoding) EncodedLen(n int) int { return (n + 4) / 5 * 8 } |
| 212 |
| 213 /* |
| 214 * Decoder |
| 215 */ |
| 216 |
| 217 type CorruptInputError int64 |
| 218 |
| 219 func (e CorruptInputError) String() string { |
| 220 return "illegal base32 data at input byte " + strconv.Itoa64(int64(e)) |
| 221 } |
| 222 |
| 223 // decode is like Decode but returns an additional 'end' value, which |
| 224 // indicates if end-of-message padding was encountered and thus any |
| 225 // additional data is an error. decode also assumes len(src)%8==0, |
| 226 // since it is meant for internal use. |
| 227 func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err os.Error) { |
| 228 for i := 0; i < len(src)/8 && !end; i++ { |
| 229 // Decode quantum using the base32 alphabet |
| 230 var dbuf [8]byte |
| 231 dlen := 8 |
| 232 |
| 233 // do the top bytes contain any data? |
| 234 dbufloop: |
| 235 for j := 0; j < 8; j++ { |
| 236 in := src[i*8+j] |
| 237 if in == '=' && j >= 2 && i == len(src)/8-1 { |
| 238 // We've reached the end and there's |
| 239 // padding, the rest should be padded |
| 240 for k := j; k < 8; k++ { |
| 241 if src[i*8+k] != '=' { |
| 242 return n, false, CorruptInputErr
or(i*8 + j) |
| 243 } |
| 244 } |
| 245 dlen = j |
| 246 end = true |
| 247 break dbufloop |
| 248 } |
| 249 dbuf[j] = enc.decodeMap[in] |
| 250 if dbuf[j] == 0xFF { |
| 251 return n, false, CorruptInputError(i*8 + j) |
| 252 } |
| 253 } |
| 254 |
| 255 // Pack 8x 5-bit source blocks into 5 byte destination |
| 256 // quantum |
| 257 switch dlen { |
| 258 case 7, 8: |
| 259 dst[i*5+4] = dbuf[6]<<5 | dbuf[7] |
| 260 fallthrough |
| 261 case 6, 5: |
| 262 dst[i*5+3] = dbuf[4]<<7 | dbuf[5]<<2 | dbuf[6]>>3 |
| 263 fallthrough |
| 264 case 4: |
| 265 dst[i*5+2] = dbuf[3]<<4 | dbuf[4]>>1 |
| 266 fallthrough |
| 267 case 3: |
| 268 dst[i*5+1] = dbuf[1]<<6 | dbuf[2]<<1 | dbuf[3]>>4 |
| 269 fallthrough |
| 270 case 2: |
| 271 dst[i*5+0] = dbuf[0]<<3 | dbuf[1]>>2 |
| 272 } |
| 273 switch dlen { |
| 274 case 2: |
| 275 n += 1 |
| 276 case 3, 4: |
| 277 n += 2 |
| 278 case 5: |
| 279 n += 3 |
| 280 case 6, 7: |
| 281 n += 4 |
| 282 case 8: |
| 283 n += 5 |
| 284 } |
| 285 } |
| 286 return n, end, nil |
| 287 } |
| 288 |
| 289 // Decode decodes src using the encoding enc. It writes at most |
| 290 // DecodedLen(len(src)) bytes to dst and returns the number of bytes |
| 291 // written. If src contains invalid base32 data, it will return the |
| 292 // number of bytes successfully written and CorruptInputError. |
| 293 func (enc *Encoding) Decode(dst, src []byte) (n int, err os.Error) { |
| 294 if len(src)%8 != 0 { |
| 295 return 0, CorruptInputError(len(src) / 8 * 8) |
| 296 } |
| 297 |
| 298 n, _, err = enc.decode(dst, src) |
| 299 return |
| 300 } |
| 301 |
| 302 type decoder struct { |
| 303 err os.Error |
| 304 enc *Encoding |
| 305 r io.Reader |
| 306 end bool // saw end of message |
| 307 buf [1024]byte // leftover input |
| 308 nbuf int |
| 309 out []byte // leftover decoded output |
| 310 outbuf [1024 / 8 * 5]byte |
| 311 } |
| 312 |
| 313 func (d *decoder) Read(p []byte) (n int, err os.Error) { |
| 314 if d.err != nil { |
| 315 return 0, d.err |
| 316 } |
| 317 |
| 318 // Use leftover decoded output from last read. |
| 319 if len(d.out) > 0 { |
| 320 n = copy(p, d.out) |
| 321 d.out = d.out[n:] |
| 322 return n, nil |
| 323 } |
| 324 |
| 325 // Read a chunk. |
| 326 nn := len(p) / 5 * 8 |
| 327 if nn < 8 { |
| 328 nn = 8 |
| 329 } |
| 330 if nn > len(d.buf) { |
| 331 nn = len(d.buf) |
| 332 } |
| 333 nn, d.err = io.ReadAtLeast(d.r, d.buf[d.nbuf:nn], 8-d.nbuf) |
| 334 d.nbuf += nn |
| 335 if d.nbuf < 8 { |
| 336 return 0, d.err |
| 337 } |
| 338 |
| 339 // Decode chunk into p, or d.out and then p if p is too small. |
| 340 nr := d.nbuf / 8 * 8 |
| 341 nw := d.nbuf / 8 * 5 |
| 342 if nw > len(p) { |
| 343 nw, d.end, d.err = d.enc.decode(d.outbuf[0:], d.buf[0:nr]) |
| 344 d.out = d.outbuf[0:nw] |
| 345 n = copy(p, d.out) |
| 346 d.out = d.out[n:] |
| 347 } else { |
| 348 n, d.end, d.err = d.enc.decode(p, d.buf[0:nr]) |
| 349 } |
| 350 d.nbuf -= nr |
| 351 for i := 0; i < d.nbuf; i++ { |
| 352 d.buf[i] = d.buf[i+nr] |
| 353 } |
| 354 |
| 355 if d.err == nil { |
| 356 d.err = err |
| 357 } |
| 358 return n, d.err |
| 359 } |
| 360 |
| 361 // NewDecoder constructs a new base32 stream decoder. |
| 362 func NewDecoder(enc *Encoding, r io.Reader) io.Reader { |
| 363 return &decoder{enc: enc, r: r} |
| 364 } |
| 365 |
| 366 // DecodedLen returns the maximum length in bytes of the decoded data |
| 367 // corresponding to n bytes of base32-encoded data. |
| 368 func (enc *Encoding) DecodedLen(n int) int { return n / 8 * 5 } |
OLD | NEW |