Source File
base64.go
Belonging Package
encoding/base64
package base64
import (
)
type Encoding struct {
encode [64]byte
decodeMap [256]byte
padChar rune
strict bool
}
const (
StdPadding rune = '=' // Standard padding character
NoPadding rune = -1 // No padding
)
const encodeStd = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
const encodeURL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
func ( string) *Encoding {
if len() != 64 {
panic("encoding alphabet is not 64-bytes long")
}
for := 0; < len(); ++ {
if [] == '\n' || [] == '\r' {
panic("encoding alphabet contains newline character")
}
}
:= new(Encoding)
.padChar = StdPadding
copy(.encode[:], )
for := 0; < len(.decodeMap); ++ {
.decodeMap[] = 0xFF
}
for := 0; < len(); ++ {
.decodeMap[[]] = byte()
}
return
}
var StdEncoding = NewEncoding(encodeStd)
var URLEncoding = NewEncoding(encodeURL)
func ( *Encoding) ( []byte) string {
:= make([]byte, .EncodedLen(len()))
.Encode(, )
return string()
}
type encoder struct {
err error
enc *Encoding
w io.Writer
buf [3]byte // buffered data waiting to be encoded
nbuf int // number of bytes in buf
out [1024]byte // output buffer
}
func ( *encoder) ( []byte) ( int, error) {
if .err != nil {
return 0, .err
}
type CorruptInputError int64
func ( CorruptInputError) () string {
return "illegal base64 data at input byte " + strconv.FormatInt(int64(), 10)
}
var [4]byte
:= 4
_ = .decodeMap
for := 0; < len(); ++ {
if len() == {
switch {
case == 0:
return , 0, nil
case == 1, .padChar != NoPadding:
return , 0, CorruptInputError( - )
}
=
break
}
:= []
++
:= .decodeMap[]
if != 0xff {
[] =
continue
}
if == '\n' || == '\r' {
--
continue
}
if rune() != .padChar {
return , 0, CorruptInputError( - 1)
}
switch {
return , 0, CorruptInputError( - 1)
for < len() && ([] == '\n' || [] == '\r') {
++
}
return , 0, CorruptInputError(len())
}
return , 0, CorruptInputError( - 1)
}
++
}
for < len() && ([] == '\n' || [] == '\r') {
++
}
= CorruptInputError()
}
=
break
}
:= uint([0])<<18 | uint([1])<<12 | uint([2])<<6 | uint([3])
[2], [1], [0] = byte(>>0), byte(>>8), byte(>>16)
switch {
case 4:
[2] = [2]
[2] = 0
fallthrough
case 3:
[1] = [1]
if .strict && [2] != 0 {
return , 0, CorruptInputError( - 1)
}
[1] = 0
fallthrough
case 2:
[0] = [0]
if .strict && ([1] != 0 || [2] != 0) {
return , 0, CorruptInputError( - 2)
}
}
return , - 1,
}
func ( *Encoding) ( string) ([]byte, error) {
:= make([]byte, .DecodedLen(len()))
, := .Decode(, []byte())
return [:],
}
type decoder struct {
err error
readErr error // error from r.Read
enc *Encoding
r io.Reader
buf [1024]byte // leftover input
nbuf int
out []byte // leftover decoded output
outbuf [1024 / 4 * 3]byte
}
_ = .decodeMap
:= 0
for strconv.IntSize >= 64 && len()- >= 8 && len()- >= 8 {
:= [ : +8]
if , := assemble64(
.decodeMap[[0]],
.decodeMap[[1]],
.decodeMap[[2]],
.decodeMap[[3]],
.decodeMap[[4]],
.decodeMap[[5]],
.decodeMap[[6]],
.decodeMap[[7]],
); {
binary.BigEndian.PutUint64([:], )
+= 6
+= 8
} else {
var int
, , = .decodeQuantum([:], , )
+=
if != nil {
return ,
}
}
}
for len()- >= 4 && len()- >= 4 {
:= [ : +4]
if , := assemble32(
.decodeMap[[0]],
.decodeMap[[1]],
.decodeMap[[2]],
.decodeMap[[3]],
); {
binary.BigEndian.PutUint32([:], )
+= 3
+= 4
} else {
var int
, , = .decodeQuantum([:], , )
+=
if != nil {
return ,
}
}
}
for < len() {
var int
, , = .decodeQuantum([:], , )
+=
if != nil {
return ,
}
}
return ,
}
if ||||||| == 0xff {
return 0, false
}
return uint64()<<58 |
uint64()<<52 |
uint64()<<46 |
uint64()<<40 |
uint64()<<34 |
uint64()<<28 |
uint64()<<22 |
uint64()<<16,
true
}
type newlineFilteringReader struct {
wrapped io.Reader
}
func ( *newlineFilteringReader) ( []byte) (int, error) {
, := .wrapped.Read()
for > 0 {
:= 0
for , := range [:] {
if != '\r' && != '\n' {
if != {
[] =
}
++
}
}
if > 0 {
return ,
return * 6 / 8
return / 4 * 3
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |