Skip to content

Commit

Permalink
feat: compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Ismail14098 committed Dec 27, 2024
1 parent aa02bf2 commit a9358d3
Show file tree
Hide file tree
Showing 6 changed files with 550 additions and 407 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,10 @@ native_amd64.s: ${NATIVE_SRC} ${NATIVE_ASM} native_amd64.go
python3 tools/asm2asm/asm2asm.py -r native_amd64.go output/native.s ${NATIVE_ASM}
awk '{gsub(/Text__native_entry__/, "text__native_entry__")}1' native_text_amd64.go > native_text_amd64.go.tmp && mv native_text_amd64.go.tmp native_text_amd64.go
awk '{gsub(/Funcs/, "funcs")}1' native_subr_amd64.go > native_subr_amd64.go.tmp && mv native_subr_amd64.go.tmp native_subr_amd64.go

test:
go clean -testcache
GOARCH=arm64 go test ./...
echo 'arm64 passed'
GOARCH=amd64 go test ./...
echo 'amd64 passed'
108 changes: 108 additions & 0 deletions base64_amd64.go
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
}
}
121 changes: 121 additions & 0 deletions base64_compat.go
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)
}
}
121 changes: 6 additions & 115 deletions base64x.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@

package base64x

import (
`encoding/base64`
)

// An Encoding is a radix 64 encoding/decoding scheme, defined by a
// 64-character alphabet. The most common encoding is the "base64"
// encoding defined in RFC 4648 and used in MIME (RFC 2045) and PEM
Expand All @@ -28,10 +24,10 @@ import (
type Encoding int

const (
_MODE_URL = 1 << 0
_MODE_RAW = 1 << 1
_MODE_AVX2 = 1 << 2
_MODE_JSON = 1 << 3
_MODE_URL = 1 << 0
_MODE_RAW = 1 << 1
_MODE_AVX2 = 1 << 2
_MODE_JSON = 1 << 3
)

// StdEncoding is the standard base64 encoding, as defined in
Expand All @@ -55,113 +51,8 @@ const RawStdEncoding Encoding = _MODE_RAW
const RawURLEncoding Encoding = _MODE_RAW | _MODE_URL

// JSONStdEncoding is the StdEncoding and encoded as JSON string as RFC 8259.
const JSONStdEncoding Encoding = _MODE_JSON;
const JSONStdEncoding Encoding = _MODE_JSON

var (
archFlags = 0
archFlags = 0
)

/** 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
}
}
Loading

0 comments on commit a9358d3

Please sign in to comment.