From bcc970b559d06de97fd997b0af1832e5dba147ae Mon Sep 17 00:00:00 2001 From: Stephan Hadinger Date: Tue, 14 Dec 2021 19:56:28 +0100 Subject: [PATCH] Berry use ROM MD5 --- lib/libesp32/berry/default/be_md5_lib.c | 76 ++++++++++++++++- tasmota/xdrv_52_3_berry_md5.ino | 105 ------------------------ 2 files changed, 73 insertions(+), 108 deletions(-) delete mode 100644 tasmota/xdrv_52_3_berry_md5.ino diff --git a/lib/libesp32/berry/default/be_md5_lib.c b/lib/libesp32/berry/default/be_md5_lib.c index 2c8dfd77b..2db161a0b 100644 --- a/lib/libesp32/berry/default/be_md5_lib.c +++ b/lib/libesp32/berry/default/be_md5_lib.c @@ -6,10 +6,80 @@ * Allows to respond to HTTP request *******************************************************************/ #include "be_constobj.h" +#include "be_mem.h" +#include "be_exec.h" +#include "esp_rom_md5.h" -extern int m_md5_init(bvm *vm); -extern int m_md5_update(bvm *vm); -extern int m_md5_finish(bvm *vm); +int free_ctx(bvm* vm) { + int argc = be_top(vm); + if (argc > 0) { + be_getmember(vm, 1, ".p"); + md5_context_t * ctx = (md5_context_t *) be_tocomptr(vm, -1); + if (ctx != NULL) { + be_os_free(ctx); + } + } + be_return_nil(vm); +} + +// `Md5.init() -> ` +int32_t m_md5_init(struct bvm *vm); +int32_t m_md5_init(struct bvm *vm) { + + md5_context_t * ctx = (md5_context_t *) be_os_malloc(sizeof(md5_context_t)); + if (!ctx) { + be_throw(vm, BE_MALLOC_FAIL); + } + esp_rom_md5_init(ctx); + + be_newcomobj(vm, ctx, &free_ctx); + be_setmember(vm, 1, ".p"); + be_return_nil(vm); +} + +// `Md5.update(content:bytes()) -> nil` +// +// Add raw bytes to the MD5 calculation +int32_t m_md5_update(struct bvm *vm); +int32_t m_md5_update(struct bvm *vm) { + int32_t argc = be_top(vm); // Get the number of arguments + if (argc >= 2 && be_isinstance(vm, 2)) { + do { + be_getglobal(vm, "bytes"); /* get the bytes class */ /* TODO eventually replace with be_getbuiltin */ + if (!be_isderived(vm, 2)) break; + size_t length = 0; + const void * bytes = be_tobytes(vm, 2, &length); + if (!bytes) break; + + be_getmember(vm, 1, ".p"); + md5_context_t * ctx; + ctx = (md5_context_t *) be_tocomptr(vm, -1); + if (!ctx) break; + + if (length > 0) { + esp_rom_md5_update(ctx, (const uint8_t*) bytes, length); + } + be_return_nil(vm); + // success + } while (0); + } + be_raise(vm, "value_error", NULL); +} + +// `Md5.update(content:bytes()) -> nil` +// +// Add raw bytes to the MD5 calculation +int32_t m_md5_finish(struct bvm *vm); +int32_t m_md5_finish(struct bvm *vm) { + be_getmember(vm, 1, ".p"); + md5_context_t * ctx; + ctx = (md5_context_t *) be_tocomptr(vm, -1); + + uint8_t output[16]; + esp_rom_md5_final(output, ctx); + be_pushbytes(vm, output, sizeof(output)); + be_return(vm); +} #include "../generate/be_fixed_be_class_md5.h" diff --git a/tasmota/xdrv_52_3_berry_md5.ino b/tasmota/xdrv_52_3_berry_md5.ino deleted file mode 100644 index a4adee029..000000000 --- a/tasmota/xdrv_52_3_berry_md5.ino +++ /dev/null @@ -1,105 +0,0 @@ -/* - xdrv_52_3_berry_md5.ino - Berry scripting language, Md5 class - - Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - - -#ifdef USE_BERRY - -#include -#include "MD5Builder.h" -#include "be_mem.h" - -/*********************************************************************************************\ - * Md5 class - * -\*********************************************************************************************/ -extern "C" { - - int free_ctx(bvm* vm) { - int argc = be_top(vm); - if (argc > 0) { - be_getmember(vm, 1, ".p"); - struct MD5Context * ctx = (struct MD5Context *) be_tocomptr(vm, -1); - if (ctx != NULL) { - be_os_free(ctx); - } - } - be_return_nil(vm); - } - - // `Md5.init() -> ` - int32_t m_md5_init(struct bvm *vm); - int32_t m_md5_init(struct bvm *vm) { - - struct MD5Context * ctx = (struct MD5Context *) be_os_malloc(sizeof(struct MD5Context)); - if (!ctx) { - be_throw(vm, BE_MALLOC_FAIL); - } - MD5Init(ctx); - - be_newcomobj(vm, ctx, &free_ctx); - be_setmember(vm, 1, ".p"); - be_return_nil(vm); - } - - // `Md5.update(content:bytes()) -> nil` - // - // Add raw bytes to the MD5 calculation - int32_t m_md5_update(struct bvm *vm); - int32_t m_md5_update(struct bvm *vm) { - int32_t argc = be_top(vm); // Get the number of arguments - if (argc >= 2 && be_isinstance(vm, 2)) { - do { - be_getglobal(vm, "bytes"); /* get the bytes class */ /* TODO eventually replace with be_getbuiltin */ - if (!be_isderived(vm, 2)) break; - size_t length = 0; - const void * bytes = be_tobytes(vm, 2, &length); - if (!bytes) break; - - be_getmember(vm, 1, ".p"); - struct MD5Context * ctx; - ctx = (struct MD5Context *) be_tocomptr(vm, -1); - if (!ctx) break; - - if (length > 0) { - MD5Update(ctx, (const uint8_t*) bytes, length); - } - be_return_nil(vm); - // success - } while (0); - } - be_raise(vm, kTypeError, nullptr); - } - - // `Md5.update(content:bytes()) -> nil` - // - // Add raw bytes to the MD5 calculation - int32_t m_md5_finish(struct bvm *vm); - int32_t m_md5_finish(struct bvm *vm) { - be_getmember(vm, 1, ".p"); - struct MD5Context * ctx; - ctx = (struct MD5Context *) be_tocomptr(vm, -1); - - uint8_t output[16]; - MD5Final(output, ctx); - be_pushbytes(vm, output, sizeof(output)); - be_return(vm); - } -} - -#endif // USE_BERRY