generated from cloudwego/.github
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa02bf2
commit a9358d3
Showing
6 changed files
with
550 additions
and
407 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package base64x | ||
|
||
import "encoding/base64" | ||
|
||
/** Encoder Functions **/ | ||
|
||
// Encode encodes src using the specified encoding, writing | ||
// EncodedLen(len(src)) bytes to out. | ||
// | ||
// The encoding pads the output to a multiple of 4 bytes, | ||
// so Encode is not appropriate for use on individual blocks | ||
// of a large data stream. | ||
// | ||
// If out is not large enough to contain the encoded result, | ||
// it will panic. | ||
func (self Encoding) Encode(out []byte, src []byte) { | ||
if len(src) != 0 { | ||
if buf := out[:0:len(out)]; self.EncodedLen(len(src)) <= len(out) { | ||
self.EncodeUnsafe(&buf, src) | ||
} else { | ||
panic("encoder output buffer is too small") | ||
} | ||
} | ||
} | ||
|
||
// EncodeUnsafe behaves like Encode, except it does NOT check if | ||
// out is large enough to contain the encoded result. | ||
// | ||
// It will also update the length of out. | ||
func (self Encoding) EncodeUnsafe(out *[]byte, src []byte) { | ||
b64encode(out, &src, int(self)|archFlags) | ||
} | ||
|
||
// EncodeToString returns the base64 encoding of src. | ||
func (self Encoding) EncodeToString(src []byte) string { | ||
nbs := len(src) | ||
ret := make([]byte, 0, self.EncodedLen(nbs)) | ||
|
||
/* encode in native code */ | ||
self.EncodeUnsafe(&ret, src) | ||
return mem2str(ret) | ||
} | ||
|
||
// EncodedLen returns the length in bytes of the base64 encoding | ||
// of an input buffer of length n. | ||
func (self Encoding) EncodedLen(n int) int { | ||
if (self & _MODE_RAW) == 0 { | ||
return (n + 2) / 3 * 4 | ||
} else { | ||
return (n*8 + 5) / 6 | ||
} | ||
} | ||
|
||
/** Decoder Functions **/ | ||
|
||
// Decode decodes src using the encoding enc. It writes at most | ||
// DecodedLen(len(src)) bytes to out and returns the number of bytes | ||
// written. If src contains invalid base64 data, it will return the | ||
// number of bytes successfully written and base64.CorruptInputError. | ||
// | ||
// New line characters (\r and \n) are ignored. | ||
// | ||
// If out is not large enough to contain the encoded result, | ||
// it will panic. | ||
func (self Encoding) Decode(out []byte, src []byte) (int, error) { | ||
if len(src) == 0 { | ||
return 0, nil | ||
} else if buf := out[:0:len(out)]; self.DecodedLen(len(src)) <= len(out) { | ||
return self.DecodeUnsafe(&buf, src) | ||
} else { | ||
panic("decoder output buffer is too small") | ||
} | ||
} | ||
|
||
// DecodeUnsafe behaves like Decode, except it does NOT check if | ||
// out is large enough to contain the decoded result. | ||
// | ||
// It will also update the length of out. | ||
func (self Encoding) DecodeUnsafe(out *[]byte, src []byte) (int, error) { | ||
if n := b64decode(out, mem2addr(src), len(src), int(self)|archFlags); n >= 0 { | ||
return n, nil | ||
} else { | ||
return 0, base64.CorruptInputError(-n - 1) | ||
} | ||
} | ||
|
||
// DecodeString returns the bytes represented by the base64 string s. | ||
func (self Encoding) DecodeString(s string) ([]byte, error) { | ||
src := str2mem(s) | ||
ret := make([]byte, 0, self.DecodedLen(len(s))) | ||
|
||
/* decode into the allocated buffer */ | ||
if _, err := self.DecodeUnsafe(&ret, src); err != nil { | ||
return nil, err | ||
} else { | ||
return ret, nil | ||
} | ||
} | ||
|
||
// DecodedLen returns the maximum length in bytes of the decoded data | ||
// corresponding to n bytes of base64-encoded data. | ||
func (self Encoding) DecodedLen(n int) int { | ||
if (self & _MODE_RAW) == 0 { | ||
return n / 4 * 3 | ||
} else { | ||
return n * 6 / 8 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
//go:build !amd64 || !go1.17 || go1.24 | ||
// +build !amd64 !go1.17 go1.24 | ||
|
||
package base64x | ||
|
||
import ( | ||
"encoding/base64" | ||
) | ||
|
||
/** Encoder Functions **/ | ||
|
||
// Encode encodes src using the specified encoding, writing | ||
// EncodedLen(len(src)) bytes to out. | ||
// | ||
// The encoding pads the output to a multiple of 4 bytes, | ||
// so Encode is not appropriate for use on individual blocks | ||
// of a large data stream. | ||
func (self Encoding) Encode(out []byte, src []byte) { | ||
switch self { | ||
case 0, _MODE_JSON: | ||
base64.StdEncoding.Encode(out, src) | ||
case _MODE_URL: | ||
base64.URLEncoding.Encode(out, src) | ||
case _MODE_RAW: | ||
base64.RawStdEncoding.Encode(out, src) | ||
case _MODE_RAW | _MODE_URL: | ||
base64.RawURLEncoding.Encode(out, src) | ||
default: | ||
base64.StdEncoding.Encode(out, src) | ||
} | ||
} | ||
|
||
// EncodeUnsafe behaves like Encode | ||
func (self Encoding) EncodeUnsafe(out *[]byte, src []byte) { | ||
self.Encode(*out, src) | ||
} | ||
|
||
// EncodeToString returns the base64 encoding of src. | ||
func (self Encoding) EncodeToString(src []byte) string { | ||
out := make([]byte, self.EncodedLen(len(src))) | ||
self.Encode(out, src) | ||
return mem2str(out) | ||
} | ||
|
||
// EncodedLen returns the length in bytes of the base64 encoding | ||
// of an input buffer of length n. | ||
func (self Encoding) EncodedLen(n int) int { | ||
switch self { | ||
case 0, _MODE_JSON: | ||
return base64.StdEncoding.EncodedLen(n) | ||
case _MODE_URL: | ||
return base64.URLEncoding.EncodedLen(n) | ||
case _MODE_RAW: | ||
return base64.RawStdEncoding.EncodedLen(n) | ||
case _MODE_RAW | _MODE_URL: | ||
return base64.RawURLEncoding.EncodedLen(n) | ||
default: | ||
return base64.StdEncoding.EncodedLen(n) | ||
} | ||
} | ||
|
||
/** Decoder Functions **/ | ||
|
||
// Decode decodes src using the encoding enc. It writes at most | ||
// DecodedLen(len(src)) bytes to out and returns the number of bytes | ||
// written. If src contains invalid base64 data, it will return the | ||
// number of bytes successfully written and base64.CorruptInputError. | ||
// | ||
// New line characters (\r and \n) are ignored. | ||
func (self Encoding) Decode(out []byte, src []byte) (int, error) { | ||
switch self { | ||
case 0, _MODE_JSON: | ||
return base64.StdEncoding.Decode(out, src) | ||
case _MODE_URL: | ||
return base64.URLEncoding.Decode(out, src) | ||
case _MODE_RAW: | ||
return base64.RawStdEncoding.Decode(out, src) | ||
case _MODE_RAW | _MODE_URL: | ||
return base64.RawURLEncoding.Decode(out, src) | ||
default: | ||
return base64.StdEncoding.Decode(out, src) | ||
} | ||
} | ||
|
||
// DecodeUnsafe behaves like Decode | ||
func (self Encoding) DecodeUnsafe(out *[]byte, src []byte) (int, error) { | ||
return self.Decode(*out, src) | ||
} | ||
|
||
// DecodeString returns the bytes represented by the base64 string s. | ||
func (self Encoding) DecodeString(s string) ([]byte, error) { | ||
switch self { | ||
case 0, _MODE_JSON: | ||
return base64.StdEncoding.DecodeString(s) | ||
case _MODE_URL: | ||
return base64.URLEncoding.DecodeString(s) | ||
case _MODE_RAW: | ||
return base64.RawStdEncoding.DecodeString(s) | ||
case _MODE_RAW | _MODE_URL: | ||
return base64.RawURLEncoding.DecodeString(s) | ||
default: | ||
return base64.StdEncoding.DecodeString(s) | ||
} | ||
} | ||
|
||
// DecodedLen returns the maximum length in bytes of the decoded data | ||
// corresponding to n bytes of base64-encoded data. | ||
func (self Encoding) DecodedLen(n int) int { | ||
switch self { | ||
case 0, _MODE_JSON: | ||
return base64.StdEncoding.DecodedLen(n) | ||
case _MODE_URL: | ||
return base64.URLEncoding.DecodedLen(n) | ||
case _MODE_RAW: | ||
return base64.RawStdEncoding.DecodedLen(n) | ||
case _MODE_RAW | _MODE_URL: | ||
return base64.RawURLEncoding.DecodedLen(n) | ||
default: | ||
return base64.StdEncoding.DecodedLen(n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.