fix base64

This commit is contained in:
blueloveTH 2025-04-05 03:07:24 +08:00
parent 4bf59223b3
commit 95d59cd4c6
3 changed files with 14 additions and 4 deletions

View File

@ -7,6 +7,6 @@ label: base64
Encode bytes-like object `b` using the standard Base64 alphabet. Encode bytes-like object `b` using the standard Base64 alphabet.
### `base64.b64decode(b: bytes) -> bytes` ### `base64.b64decode(b: str | bytes) -> bytes`
Decode Base64 encoded bytes-like object `b`. Decode Base64 encoded bytes-like object `b`.

View File

@ -178,9 +178,17 @@ static bool base64_b64encode(int argc, py_Ref argv) {
static bool base64_b64decode(int argc, py_Ref argv) { static bool base64_b64decode(int argc, py_Ref argv) {
PY_CHECK_ARGC(1); PY_CHECK_ARGC(1);
PY_CHECK_ARG_TYPE(0, tp_bytes);
int src_size; int src_size;
unsigned char* src_data = py_tobytes(argv, &src_size); void* src_data;
if(py_istype(argv, tp_str)) {
c11_sv sv = py_tosv(argv);
src_data = (void*)sv.data;
src_size = sv.size;
} else if(py_istype(argv, tp_bytes)) {
src_data = py_tobytes(argv, &src_size);
} else {
return TypeError("expect bytes or str, got %t", argv->type);
}
unsigned char* dst_data = py_newbytes(py_retval(), src_size); unsigned char* dst_data = py_newbytes(py_retval(), src_size);
int size = base64_decode((const char*)src_data, src_size, dst_data); int size = base64_decode((const char*)src_data, src_size, dst_data);
py_bytes_resize(py_retval(), size); py_bytes_resize(py_retval(), size);

View File

@ -18,3 +18,5 @@ assert encoded.decode() == res
decoded = base64.b64decode(encoded) decoded = base64.b64decode(encoded)
assert decoded == data assert decoded == data
assert base64.b64decode('8J+llQ==') == '🥕'.encode()