Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: compatibility #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
124 changes: 124 additions & 0 deletions base64_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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
}
}
137 changes: 137 additions & 0 deletions base64_compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//go:build !amd64 || !go1.17 || go1.24
// +build !amd64 !go1.17 go1.24

/*
* Copyright 2024 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

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:
AsterDY marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
Loading
Loading