From d805cef66e0d63c5f4245b232469a0c82de529ed Mon Sep 17 00:00:00 2001 From: s-hadinger <49731213+s-hadinger@users.noreply.github.com> Date: Sat, 15 Feb 2025 21:09:40 +0100 Subject: [PATCH] Berry bytes add appendb64 (#23008) * Berry Leds_panel minor fix * Fix * Berry add 'bytes().appendb64()' * fix lib --- CHANGELOG.md | 1 + lib/libesp32/berry/src/be_byteslib.c | 40 ++++++++++++++++++++++++++++ lib/libesp32/berry/tests/bytes.be | 19 +++++++++++++ 3 files changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 085ced921..e9b6c414a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ All notable changes to this project will be documented in this file. - Support for Lithuanian language translations by zzdovydas (#22971) - `MqttTLS` field in `Status 6` to indicate if the MQTT connection is encrypted (#22995) - Support for WiZ Smart Remote using `#define USE_WIZMOTE` and command `SetOption164 1` +- Berry add `bytes().appendb64()` (#22767) ### Breaking Changed diff --git a/lib/libesp32/berry/src/be_byteslib.c b/lib/libesp32/berry/src/be_byteslib.c index 161d76046..584914157 100644 --- a/lib/libesp32/berry/src/be_byteslib.c +++ b/lib/libesp32/berry/src/be_byteslib.c @@ -1376,6 +1376,43 @@ static int m_appendhex(bvm *vm) be_return_nil(vm); /* return self */ } +static int m_appendb64(bvm *vm) +{ + int argc = be_top(vm); + buf_impl attr = m_read_attributes(vm, 1); + check_ptr_modifiable(vm, &attr); + if (attr.fixed) { be_raise(vm, BYTES_RESIZE_ERROR, BYTES_RESIZE_MESSAGE); } + if (argc >= 2 && be_isbytes(vm, 2)) { + buf_impl attr2 = m_read_attributes(vm, 2); + check_ptr(vm, &attr2); + int32_t idx = 0; /* start from index 0 */ + int32_t len = attr2.len; /* entire len */ + if (argc >= 3 && be_isint(vm, 3)) { /* read optional idx and len */ + idx = be_toint(vm, 3); + if (idx < 0) { idx = attr2.len + idx; } /* if negative, count from end */ + if (idx < 0) { idx = 0; } /* guardrails */ + if (idx > attr2.len) { idx = attr2.len; } + if (argc >= 4 && be_isint(vm, 4)) { + len = be_toint(vm, 4); + if (len < 0) { len = 0; } + } + if (idx + len >= attr2.len) { len = attr2.len - idx; } + } + if (len > 0) { /* only if there is something to encode */ + bytes_resize(vm, &attr, attr.len + encode_base64_length(len)); /* resize */ + + size_t converted = encode_base64(attr2.bufptr + idx, len, (unsigned char*)(attr.bufptr + attr.len)); + attr.len += converted; + + m_write_attributes(vm, 1, &attr); /* update instance */ + } + be_pushvalue(vm, 1); + be_return(vm); /* return self */ + } + be_raise(vm, "type_error", "operand must be bytes"); + be_return_nil(vm); /* return self */ +} + static int bytes_equal(bvm *vm, bbool iseq) { bbool ret; @@ -1862,6 +1899,8 @@ void be_load_byteslib(bvm *vm) { "reverse", m_reverse }, { "copy", m_copy }, { "append", m_connect }, + { "appendhex", m_appendhex }, + { "appendb64", m_appendb64 }, { "+", m_merge }, { "..", m_connect }, { "==", m_equal }, @@ -1916,6 +1955,7 @@ class be_class_bytes (scope: global, name: bytes) { copy, func(m_copy) append, func(m_connect) appendhex, func(m_appendhex) + appendb64, func(m_appendb64) +, func(m_merge) .., func(m_connect) ==, func(m_equal) diff --git a/lib/libesp32/berry/tests/bytes.be b/lib/libesp32/berry/tests/bytes.be index 23c442802..0cdc8334b 100644 --- a/lib/libesp32/berry/tests/bytes.be +++ b/lib/libesp32/berry/tests/bytes.be @@ -330,3 +330,22 @@ assert(bytes("02")) a = bytes("01020304") assert(a.get(1, 3) == 0x040302) assert(a.get(1, -3) == 0x020304) + +# append base64 +b = bytes("AABBCC") +c = bytes("001122") +assert(bytes().fromstring(bytes("001122").tob64()) == bytes('41424569')) +assert(b.appendb64(c) == bytes("AABBCC41424569")) +assert(b.appendb64(bytes()) == bytes("AABBCC41424569")) + +b = bytes("AABBCC") +assert(bytes().fromstring(bytes("1122").tob64()) == bytes('4553493D')) +assert(b.appendb64(c, 1) == bytes("AABBCC4553493D")) + +b = bytes("AABBCC") +assert(bytes().fromstring(bytes("22").tob64()) == bytes('49673D3D')) +assert(b.appendb64(c, 2) == bytes("AABBCC49673D3D")) + +b = bytes("AABBCC") +assert(bytes().fromstring(bytes("11").tob64()) == bytes('45513D3D')) +assert(b.appendb64(c, 1, 1) == bytes("AABBCC45513D3D"))