Added OpenSSL as an extension, but just implemented MD5. (0.38.12)
This commit is contained in:
parent
15082bdc3e
commit
53698478d9
15 changed files with 182 additions and 5 deletions
|
|
@ -169,6 +169,7 @@ static const char *opcode_name(OpCode op) {
|
|||
case OP_PCRE2_TEST: return "PCRE2_TEST";
|
||||
case OP_PCRE2_MATCH: return "PCRE2_MATCH";
|
||||
case OP_PCRE2_FINDALL: return "PCRE2_FINDALL";
|
||||
case OP_OPENSSL_MD5: return "OPENSSL_MD5";
|
||||
case OP_INI_LOAD: return "INI_LOAD";
|
||||
case OP_INI_FREE: return "INI_FREE";
|
||||
case OP_INI_GET_STRING: return "INI_GET_STRING";
|
||||
|
|
|
|||
|
|
@ -181,6 +181,9 @@ typedef enum {
|
|||
OP_PCRE2_MATCH, // pops flags, text, pattern; pushes match map or Nil
|
||||
OP_PCRE2_FINDALL, // pops flags, text, pattern; pushes array of match maps
|
||||
|
||||
// OpenSSL (optional)
|
||||
OP_OPENSSL_MD5, // pops data string; pushes md5 hex string
|
||||
|
||||
// INI (iniparser 4.2.6) optional
|
||||
OP_INI_LOAD, // pops path; pushes handle (>0) or 0
|
||||
OP_INI_FREE, // pops handle; pushes 1/0
|
||||
|
|
|
|||
47
src/external/opensssl.c
vendored
Normal file
47
src/external/opensssl.c
vendored
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-02-19
|
||||
*/
|
||||
|
||||
/*
|
||||
* OpenSSL integration helpers (currently MD5 only)
|
||||
*/
|
||||
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
# include <openssl/md5.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Compute MD5 hex of input buffer; returns malloc'ed C string (lowercase hex).
|
||||
* Caller must free(). Returns NULL on failure. When OpenSSL is disabled,
|
||||
* returns an allocated empty string ("") to keep behavior consistent with
|
||||
* other optional extensions. */
|
||||
static char *fun_openssl_md5_hex(const unsigned char *data, size_t len) {
|
||||
static const char hexdig[] = "0123456789abcdef";
|
||||
if (!data && len != 0) return NULL;
|
||||
#ifdef FUN_WITH_OPENSSL
|
||||
unsigned char digest[MD5_DIGEST_LENGTH];
|
||||
MD5_CTX ctx;
|
||||
if (MD5_Init(&ctx) != 1) return NULL;
|
||||
if (len && MD5_Update(&ctx, data, len) != 1) return NULL;
|
||||
if (MD5_Final(digest, &ctx) != 1) return NULL;
|
||||
char *hex = (char*)malloc(MD5_DIGEST_LENGTH * 2 + 1);
|
||||
if (!hex) return NULL;
|
||||
for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
|
||||
hex[2*i] = hexdig[(digest[i] >> 4) & 0xF];
|
||||
hex[2*i+1] = hexdig[digest[i] & 0xF];
|
||||
}
|
||||
hex[MD5_DIGEST_LENGTH * 2] = '\0';
|
||||
return hex;
|
||||
#else
|
||||
char *hex = (char*)malloc(1);
|
||||
if (hex) hex[0] = '\0';
|
||||
return hex;
|
||||
#endif
|
||||
}
|
||||
|
|
@ -1197,6 +1197,15 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
|
|||
free(name);
|
||||
return 1;
|
||||
}
|
||||
/* OpenSSL (md5) */
|
||||
if (strcmp(name, "openssl_md5") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "openssl_md5 expects (data)"); free(name); return 0; }
|
||||
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after openssl_md5 arg"); free(name); return 0; }
|
||||
bytecode_add_instruction(bc, OP_OPENSSL_MD5, 0);
|
||||
free(name);
|
||||
return 1;
|
||||
}
|
||||
/* PCSC builtins */
|
||||
if (strcmp(name, "pcsc_establish") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
|
|
|
|||
4
src/vm.c
4
src/vm.c
|
|
@ -73,6 +73,7 @@
|
|||
#include "external/sqlite.c"
|
||||
#include "external/tcltk.c"
|
||||
#include "external/xml2.c"
|
||||
#include "external/opensssl.c"
|
||||
|
||||
/* forward declarations for include mapping used in error reporting */
|
||||
extern char *preprocess_includes(const char *src);
|
||||
|
|
@ -868,6 +869,9 @@ void vm_run(VM *vm, Bytecode *entry) {
|
|||
#include "vm/curl/download.c"
|
||||
#endif
|
||||
|
||||
/* OpenSSL ops (md5) */
|
||||
#include "vm/openssl/md5.c"
|
||||
|
||||
/* Tk (Tcl/Tk) ops */
|
||||
#ifdef FUN_WITH_TCLTK
|
||||
#include "vm/tk/eval.c"
|
||||
|
|
|
|||
16
src/vm/openssl/md5.c
Normal file
16
src/vm/openssl/md5.c
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
/**
|
||||
* OpenSSL MD5 builtin
|
||||
*/
|
||||
case OP_OPENSSL_MD5: {
|
||||
Value vdata = pop_value(vm);
|
||||
char *s = value_to_string_alloc(&vdata);
|
||||
free_value(vdata);
|
||||
if (!s) { push_value(vm, make_string("")); break; }
|
||||
char *hex = fun_openssl_md5_hex((const unsigned char*)s, strlen(s));
|
||||
free(s);
|
||||
if (!hex) { push_value(vm, make_string("")); break; }
|
||||
Value out = make_string(hex);
|
||||
free(hex);
|
||||
push_value(vm, out);
|
||||
break;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue