Adiciona tinyblocks-base64 e tinyblocks-curves (listas de pontos)

This commit is contained in:
mguinhos committed 2026-10-20 20:59:17 -03:00
1 parent a14c7f9937
commit b1b15a4c49
12 files changed
+815 -9

No files matched your search

+141
View File
@@ -0,0 +1,141 @@
/*
* TinyBlocks - tb_base64.c
* Categoria "Base64": Base64.Encode / Base64.Decode.
*/
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tinyblocks/tinyblocks.h>
#include "tb_base64.h"
static const char B64_ALPHA[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static char s_err[160] = "";
const char* tb_base64_last_error(void) { return s_err; }
char* tb_base64_encode(const void* data, size_t n) {
s_err[0] = 0;
const unsigned char* p = (const unsigned char*)data;
size_t out = ((n + 2) / 3) * 4;
char* buf = (char*)malloc(out + 1);
if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
size_t o = 0;
for (size_t i = 0; i < n; i += 3) {
unsigned v = (unsigned)p[i] << 16;
if (i + 1 < n) v |= (unsigned)p[i + 1] << 8;
if (i + 2 < n) v |= (unsigned)p[i + 2];
buf[o++] = B64_ALPHA[(v >> 18) & 63];
buf[o++] = B64_ALPHA[(v >> 12) & 63];
buf[o++] = (i + 1 < n) ? B64_ALPHA[(v >> 6) & 63] : '=';
buf[o++] = (i + 2 < n) ? B64_ALPHA[v & 63] : '=';
}
buf[o] = 0;
return buf;
}
static int b64_val(int c) {
if (c >= 'A' && c <= 'Z') return c - 'A';
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
if (c >= '0' && c <= '9') return c - '0' + 52;
if (c == '+') return 62;
if (c == '/') return 63;
return -1;
}
unsigned char* tb_base64_decode(const char* text, size_t* out_len) {
s_err[0] = 0;
if (out_len) *out_len = 0;
if (!text) { snprintf(s_err, sizeof(s_err), "entrada nula"); return NULL; }
size_t n = strlen(text);
unsigned char* buf = (unsigned char*)malloc(n / 4 * 3 + 4);
if (!buf) { snprintf(s_err, sizeof(s_err), "sem memoria"); return NULL; }
size_t o = 0;
int quad[4], qn = 0;
for (size_t i = 0; i < n; i++) {
unsigned char c = (unsigned char)text[i];
if (c == '\n' || c == '\r' || c == ' ' || c == '\t') continue;
if (c == '=') { quad[qn++] = -2; }
else {
int v = b64_val(c);
if (v < 0) { free(buf); snprintf(s_err, sizeof(s_err), "caractere invalido '%c'", c); return NULL; }
quad[qn++] = v;
}
if (qn == 4) {
int q0 = quad[0] < 0 ? 0 : quad[0];
int q1 = quad[1] < 0 ? 0 : quad[1];
int q2 = quad[2] < 0 ? 0 : quad[2];
int q3 = quad[3] < 0 ? 0 : quad[3];
if (quad[0] >= 0) buf[o++] = (unsigned char)((q0 << 2) | ((q1 & 0x30) >> 4));
if (quad[2] >= 0) buf[o++] = (unsigned char)(((q1 & 0x0F) << 4) | ((q2 & 0x3C) >> 2));
if (quad[3] >= 0) buf[o++] = (unsigned char)(((q2 & 0x03) << 6) | (q3 & 0x3F));
qn = 0;
}
}
buf[o] = 0;
if (out_len) *out_len = o;
return buf;
}
char* tb_base64_encode_str(const char* s) {
return tb_base64_encode(s ? s : "", s ? strlen(s) : 0);
}
char* tb_base64_decode_str(const char* s, size_t* out_len) {
return (char*)tb_base64_decode(s, out_len);
}
/* -- Blocos -------------------------------------------------------------- */
static const char* in_text(TinyBlocks_Block* self, TinyBlocks* rt, uint32_t i) {
TinyBlocks_Value v = tb_block_get_input(self, rt, i);
return (v.kind == TB_VALUE_TEXT && v.as.text) ? v.as.text : "";
}
static void eval_encode(TinyBlocks_Block* self, TinyBlocks* rt) {
char* s = tb_base64_encode_str(in_text(self, rt, 0));
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
free(s);
self->dirty = false;
}
static void eval_decode(TinyBlocks_Block* self, TinyBlocks* rt) {
char* s = tb_base64_decode_str(in_text(self, rt, 0), NULL);
tb_block_set_output(self, 0, tb_value_text(s ? s : ""));
free(s);
self->dirty = false;
}
/* -- Registro ------------------------------------------------------------ */
static bool reg(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib, const char* name,
const TinyBlocks_SlotDesc* in, uint32_t ni,
const TinyBlocks_SlotDesc* out, uint32_t no,
void (*fn)(TinyBlocks_Block*, TinyBlocks*)) {
TinyBlocks_TypeBlock* t = tb_type_block_create(rt, name, in, ni, out, no, fn);
return t && tb_library_add(lib, &t->base);
}
static bool base64_init(TinyBlocks* rt, TinyBlocks_LibraryBlock* lib) {
static const TinyBlocks_Category cat = {
"Base64",
{ 0/255.f, 150/255.f, 136/255.f, 1.f},
{ 0/255.f, 121/255.f, 107/255.f, 1.f},
};
tb_library_set_category(lib, &cat);
static const TinyBlocks_SlotDesc in_text_s[] = {{"texto", TB_VALUE_TEXT}};
static const TinyBlocks_SlotDesc out_text_s[] = {{"texto", TB_VALUE_TEXT}};
return reg(rt, lib, "Base64.Encode", in_text_s, 1, out_text_s, 1, eval_encode)
&& reg(rt, lib, "Base64.Decode", in_text_s, 1, out_text_s, 1, eval_decode);
}
TINYBLOCKS_DEFINE_LIBRARY(base64, "tinyblocks-base64", "1.0.0", base64_init, NULL)
const TinyBlocks_LibraryDescriptor* tb_base64_get_library(void) {
return &_tb_lib_desc_base64;
}