mirror of https://github.com/arendst/Tasmota.git
Berry add class `MD5` (#12296)
* Berry add class `MD5` * Fix compilation Co-authored-by: Stephan Hadinger <stephan.hadinger@gmail.com>
This commit is contained in:
parent
4ad1c40a23
commit
22c9f2ece2
|
@ -0,0 +1,45 @@
|
|||
/********************************************************************
|
||||
* Berry module `webserver`
|
||||
*
|
||||
* To use: `import webserver`
|
||||
*
|
||||
* Allows to respond to HTTP request
|
||||
*******************************************************************/
|
||||
#include "be_constobj.h"
|
||||
|
||||
extern int m_md5_init(bvm *vm);
|
||||
extern int m_md5_update(bvm *vm);
|
||||
extern int m_md5_finish(bvm *vm);
|
||||
|
||||
#if BE_USE_PRECOMPILED_OBJECT
|
||||
#include "../generate/be_fixed_be_class_md5.h"
|
||||
#endif
|
||||
|
||||
void be_load_md5_lib(bvm *vm) {
|
||||
#if !BE_USE_PRECOMPILED_OBJECT
|
||||
static const bnfuncinfo members[] = {
|
||||
{ ".p", NULL },
|
||||
|
||||
{ "init", m_md5_init },
|
||||
{ "update", m_md5_update },
|
||||
{ "finish", m_md5_finish },
|
||||
|
||||
{ NULL, NULL }
|
||||
};
|
||||
be_regclass(vm, "MD5", members);
|
||||
#else
|
||||
be_pushntvclass(vm, &be_class_md5);
|
||||
be_setglobal(vm, "MD5");
|
||||
be_pop(vm, 1);
|
||||
#endif
|
||||
}
|
||||
/* @const_object_info_begin
|
||||
|
||||
class be_class_md5 (scope: global, name: MD5) {
|
||||
.p, var
|
||||
|
||||
init, func(m_md5_init)
|
||||
update, func(m_md5_update)
|
||||
finish, func(m_md5_finish)
|
||||
}
|
||||
@const_object_info_end */
|
|
@ -96,6 +96,7 @@ extern void be_load_tasmota_ntvlib(bvm *vm);
|
|||
extern void be_load_wirelib(bvm *vm);
|
||||
extern void be_load_driverlib(bvm *vm);
|
||||
extern void be_load_driver_i2c_lib(bvm *vm);
|
||||
extern void be_load_md5_lib(bvm *vm);
|
||||
|
||||
#ifdef USE_LVGL
|
||||
extern void be_load_lvgl_color_lib(bvm *vm);
|
||||
|
@ -114,6 +115,7 @@ BERRY_API void be_load_custom_libs(bvm *vm)
|
|||
#endif
|
||||
be_load_tasmota_ntvlib(vm);
|
||||
be_load_driverlib(vm);
|
||||
be_load_md5_lib(vm);
|
||||
#ifdef USE_I2C
|
||||
be_load_wirelib(vm);
|
||||
be_load_driver_i2c_lib(vm);
|
||||
|
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,20 @@
|
|||
#include "be_constobj.h"
|
||||
|
||||
static be_define_const_map_slots(be_class_md5_map) {
|
||||
{ be_const_key(update, -1), be_const_func(m_md5_update) },
|
||||
{ be_const_key(dot_p, -1), be_const_int(0) },
|
||||
{ be_const_key(finish, -1), be_const_func(m_md5_finish) },
|
||||
{ be_const_key(init, 1), be_const_func(m_md5_init) },
|
||||
};
|
||||
|
||||
static be_define_const_map(
|
||||
be_class_md5_map,
|
||||
4
|
||||
);
|
||||
|
||||
BE_EXPORT_VARIABLE be_define_const_class(
|
||||
be_class_md5,
|
||||
1,
|
||||
NULL,
|
||||
MD5
|
||||
);
|
|
@ -0,0 +1,105 @@
|
|||
/*
|
||||
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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
|
||||
#ifdef USE_BERRY
|
||||
|
||||
#include <berry.h>
|
||||
#include "mbedtls/md5.h"
|
||||
#include "be_mem.h"
|
||||
|
||||
/*********************************************************************************************\
|
||||
* Md5 class
|
||||
*
|
||||
\*********************************************************************************************/
|
||||
extern "C" {
|
||||
|
||||
int free_ctx(bvm* vm) {
|
||||
int argc = be_top(vm);
|
||||
if (argc > 0) {
|
||||
mbedtls_md5_context * ctx = (mbedtls_md5_context*) 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) {
|
||||
|
||||
mbedtls_md5_context * ctx = (mbedtls_md5_context*) be_os_malloc(sizeof(mbedtls_md5_context));
|
||||
if (!ctx) {
|
||||
be_throw(vm, BE_MALLOC_FAIL);
|
||||
}
|
||||
mbedtls_md5_init(ctx);
|
||||
mbedtls_md5_starts_ret(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");
|
||||
mbedtls_md5_context * ctx;
|
||||
ctx = (mbedtls_md5_context*) be_tocomptr(vm, -1);
|
||||
if (!ctx) break;
|
||||
|
||||
if (length > 0) {
|
||||
mbedtls_md5_update_ret(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");
|
||||
mbedtls_md5_context * ctx;
|
||||
ctx = (mbedtls_md5_context*) be_tocomptr(vm, -1);
|
||||
|
||||
uint8_t output[16];
|
||||
mbedtls_md5_finish_ret(ctx, output);
|
||||
be_pushbytes(vm, output, sizeof(output));
|
||||
be_return(vm);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // USE_BERRY
|
Loading…
Reference in New Issue