~ubuntu-branches/ubuntu/utopic/golang/utopic

« back to all changes in this revision

Viewing changes to src/pkg/encoding/base64/base64.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
package base64
7
7
 
8
8
import (
 
9
        "bytes"
9
10
        "io"
10
11
        "strconv"
 
12
        "strings"
11
13
)
12
14
 
13
15
/*
49
51
// It is typically used in URLs and file names.
50
52
var URLEncoding = NewEncoding(encodeURL)
51
53
 
 
54
var removeNewlinesMapper = func(r rune) rune {
 
55
        if r == '\r' || r == '\n' {
 
56
                return -1
 
57
        }
 
58
        return r
 
59
}
 
60
 
52
61
/*
53
62
 * Encoder
54
63
 */
208
217
 
209
218
// decode is like Decode but returns an additional 'end' value, which
210
219
// indicates if end-of-message padding was encountered and thus any
211
 
// additional data is an error.
 
220
// additional data is an error. This method assumes that src has been
 
221
// stripped of all supported whitespace ('\r' and '\n').
212
222
func (enc *Encoding) decode(dst, src []byte) (n int, end bool, err error) {
213
 
        osrc := src
 
223
        olen := len(src)
214
224
        for len(src) > 0 && !end {
215
225
                // Decode quantum using the base64 alphabet
216
226
                var dbuf [4]byte
217
227
                dlen := 4
218
228
 
219
 
        dbufloop:
220
229
                for j := 0; j < 4; {
221
230
                        if len(src) == 0 {
222
 
                                return n, false, CorruptInputError(len(osrc) - len(src) - j)
 
231
                                return n, false, CorruptInputError(olen - len(src) - j)
223
232
                        }
224
233
                        in := src[0]
225
234
                        src = src[1:]
226
 
                        if in == '\r' || in == '\n' {
227
 
                                // Ignore this character.
228
 
                                continue
229
 
                        }
230
235
                        if in == '=' && j >= 2 && len(src) < 4 {
231
 
                                // We've reached the end and there's
232
 
                                // padding
233
 
                                if len(src) == 0 && j == 2 {
 
236
                                // We've reached the end and there's padding
 
237
                                if len(src)+j < 4-1 {
234
238
                                        // not enough padding
235
 
                                        return n, false, CorruptInputError(len(osrc))
 
239
                                        return n, false, CorruptInputError(olen)
236
240
                                }
237
241
                                if len(src) > 0 && src[0] != '=' {
238
242
                                        // incorrect padding
239
 
                                        return n, false, CorruptInputError(len(osrc) - len(src) - 1)
 
243
                                        return n, false, CorruptInputError(olen - len(src) - 1)
240
244
                                }
241
 
                                dlen = j
242
 
                                end = true
243
 
                                break dbufloop
 
245
                                dlen, end = j, true
 
246
                                break
244
247
                        }
245
248
                        dbuf[j] = enc.decodeMap[in]
246
249
                        if dbuf[j] == 0xFF {
247
 
                                return n, false, CorruptInputError(len(osrc) - len(src) - 1)
 
250
                                return n, false, CorruptInputError(olen - len(src) - 1)
248
251
                        }
249
252
                        j++
250
253
                }
274
277
// number of bytes successfully written and CorruptInputError.
275
278
// New line characters (\r and \n) are ignored.
276
279
func (enc *Encoding) Decode(dst, src []byte) (n int, err error) {
 
280
        src = bytes.Map(removeNewlinesMapper, src)
277
281
        n, _, err = enc.decode(dst, src)
278
282
        return
279
283
}
280
284
 
281
285
// DecodeString returns the bytes represented by the base64 string s.
282
286
func (enc *Encoding) DecodeString(s string) ([]byte, error) {
 
287
        s = strings.Map(removeNewlinesMapper, s)
283
288
        dbuf := make([]byte, enc.DecodedLen(len(s)))
284
289
        n, err := enc.Decode(dbuf, []byte(s))
285
290
        return dbuf[:n], err
344
349
        return n, d.err
345
350
}
346
351
 
 
352
type newlineFilteringReader struct {
 
353
        wrapped io.Reader
 
354
}
 
355
 
 
356
func (r *newlineFilteringReader) Read(p []byte) (int, error) {
 
357
        n, err := r.wrapped.Read(p)
 
358
        for n > 0 {
 
359
                offset := 0
 
360
                for i, b := range p[0:n] {
 
361
                        if b != '\r' && b != '\n' {
 
362
                                if i != offset {
 
363
                                        p[offset] = b
 
364
                                }
 
365
                                offset++
 
366
                        }
 
367
                }
 
368
                if offset > 0 {
 
369
                        return offset, err
 
370
                }
 
371
                // Previous buffer entirely whitespace, read again
 
372
                n, err = r.wrapped.Read(p)
 
373
        }
 
374
        return n, err
 
375
}
 
376
 
347
377
// NewDecoder constructs a new base64 stream decoder.
348
378
func NewDecoder(enc *Encoding, r io.Reader) io.Reader {
349
 
        return &decoder{enc: enc, r: r}
 
379
        return &decoder{enc: enc, r: &newlineFilteringReader{r}}
350
380
}
351
381
 
352
382
// DecodedLen returns the maximum length in bytes of the decoded data