forked from nique_372/CryptoByLeo
//+------------------------------------------------------------------+
| |
//| Base64.mqh |
| |
//| Copyright 2026, Niquel Mendoza. |
| |
//| https://www.mql5.com |
| |
//+------------------------------------------------------------------+
| |
#property copyright "Copyright 2026, Niquel Mendoza."
| |
#property link "https://www.mql5.com"
| |
#property strict
| |
| |
#ifndef CRYPTOBYLEO_SRC_B64_BASE64_MQH
| |
#define CRYPTOBYLEO_SRC_B64_BASE64_MQH
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#include "Def.mqh"
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
namespace TSN
| |
{
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
class CBase64
| |
{
| |
public:
| |
CBase64(void) {}
| |
~CBase64(void) {}
| |
| |
| |
//---
| |
// src: buffer de entrada
| |
// n: tamaño del buffer
| |
// i: posicion de lectura ()
| |
// dst: buffer de salida
| |
// j: posicion de salida (Escritura)
| |
// --- retorna: J osea el offset de escitura actual
| |
static int Encode(const uchar& src[], int n, int i, uchar& dst[], int j);
| |
| |
//---
| |
/* static int EncodeInPlace(uchar& src[], int i, int n);*/
| |
};
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
#define BASE64_UNROLLED_IT \
| |
if(i + 2 >= n) \
| |
break; \
| |
{ \
| |
const uint v = ((uint)src[i] << 16) | ((uint)src[i + 1] << 8) | (uint)src[i + 2]; \
| |
dst[j++] = g_b64_encode[(v >> 18) & 63]; \
| |
dst[j++] = g_b64_encode[(v >> 12) & 63]; \
| |
dst[j++] = g_b64_encode[(v >> 6) & 63]; \
| |
dst[j++] = g_b64_encode[v & 63]; \
| |
i += 3; \
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
static int CBase64::Encode(const uchar& src[], int n, int i, uchar& dst[], int j)
| |
{
| |
//--- la idea es reservar un 133% (en este cas parae giard divs un 150%)
| |
const int req = j + (n + ((n + 4) >> 1));
| |
if(req > ArraySize(dst))
| |
ArrayResize(dst, req);
| |
| |
//---
| |
n += i;
| |
| |
//---
| |
while(true)
| |
{
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
BASE64_UNROLLED_IT
| |
}
| |
| |
//---
| |
const int p = n - i;
| |
if(p == 1)
| |
{
| |
uint v = (uint)src[i] << 16;
| |
dst[j++] = g_b64_encode[(v >> 18) & 63];
| |
dst[j++] = g_b64_encode[(v >> 12) & 63];
| |
dst[j++] = '=';
| |
dst[j++] = '=';
| |
}
| |
else
| |
if(p == 2)
| |
{
| |
uint v = ((uint)src[i] << 16) | ((uint)src[i + 1] << 8);
| |
dst[j++] = g_b64_encode[(v >> 18) & 63];
| |
dst[j++] = g_b64_encode[(v >> 12) & 63];
| |
dst[j++] = g_b64_encode[(v >> 6) & 63];
| |
dst[j++] = '=';
| |
}
| |
| |
//---
| |
return j;
| |
}
| |
| |
//+------------------------------------------------------------------+
| |
//| |
| |
//+------------------------------------------------------------------+
| |
//---
| |
/*
| |
Bueno por alguna razon esta version no llega a ser mas rapida a lo mucho un
| |
25 % menos rapida que la nativa y nativa +arraycopy aun sigue un 4-5%
| |
asi que creo qeu no commpezna lo mejro seira utildia en todo case
| |
B64 normal
| |
| |
Ahora si me lo preguntas...
| |
Es raro pro qeu su "bucle caliente" en teoria hace el mismo trabajo.. literal hace el mismo
| |
Lo que si es que la verison inplace paga varios branchs.. iniiclaes
| |
y por ahi una div etc.. pero consideo ruqe ese no es el cuello de bootella
| |
si no qizas el hecho de uqe leamos de atras implicuqe algo mas complejo para el cpu..
| |
quizas....
| |
| |
//---
| |
#define BASE64_INSITU_MUL3(V) (V*3)
| |
| |
| |
//---
| |
#define BASE64_INSITU \
| |
if(--i < 0) \
| |
break; \
| |
{ \
| |
const uint v = ((uint)buf[p] << 16) | ((uint)buf[p + 1] << 8) | (uint)buf[p + 2]; \
| |
buf[o] = g_b64_encode[v >> 18]; \
| |
buf[o + 1] = g_b64_encode[(v >> 12) & 0x3F]; \
| |
buf[o + 2] = g_b64_encode[(v >> 6) & 0x3F]; \
| |
buf[o + 3] = g_b64_encode[v & 0x3F]; \
| |
} \
| |
p -= 3; o -= 4;
| |
| |
//+------------------------------------------------------------------+
| |
static int CBase64::EncodeInPlace(uchar &buf[], int j, int n)
| |
{
| |
//--- (50% mas)
| |
const int req = j + (n + ((n + 4) >> 1));
| |
if(req > ArraySize(buf))
| |
ArrayResize(buf, req);
| |
| |
//--- Basicos
| |
const int full = int(((ulong)n * 2863311531ull) >> 33);
| |
const int rem = n - BASE64_INSITU_MUL3(full);
| |
int os = full << 2;
| |
| |
//--- Remainder
| |
if(rem == 1)
| |
{
| |
const uchar b0 = buf[BASE64_INSITU_MUL3(full)];
| |
buf[os++] = g_b64_encode[b0 >> 2];
| |
buf[os++] = g_b64_encode[(b0 & 0x03) << 4];
| |
buf[os++] = '=';
| |
buf[os++] = '=';
| |
}
| |
else
| |
if(rem == 2)
| |
{
| |
const uchar b0 = buf[BASE64_INSITU_MUL3(full)];
| |
const uchar b1 = buf[BASE64_INSITU_MUL3(full) + 1];
| |
buf[os++] = g_b64_encode[b0 >> 2];
| |
buf[os++] = g_b64_encode[((b0 & 0x03) << 4) | (b1 >> 4)];
| |
buf[os++] = g_b64_encode[(b1 & 0x0F) << 2];
| |
buf[os++] = '=';
| |
}
| |
| |
//--- Grupos completos
| |
// os apartir de atraas hacia adletna [][][][x][][][][x]
| |
int p = BASE64_INSITU_MUL3(full) - 3;
| |
int o = (full << 2) - 4;
| |
int i = full;
| |
| |
//---
| |
while(true)
| |
{
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
BASE64_INSITU
| |
}
| |
| |
//---
| |
return os;
| |
}
| |
*/
| |
| |
| |
//---
| |
}
| |
| |
#endif // CRYPTOBYLEO_SRC_B64_BASE64_MQH
|