2016-02-21 05:18:27 +01:00
|
|
|
// Module encoders
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "lauxlib.h"
|
|
|
|
#include "lmem.h"
|
|
|
|
#include "c_string.h"
|
|
|
|
#define BASE64_INVALID '\xff'
|
|
|
|
#define BASE64_PADDING '='
|
|
|
|
#define ISBASE64(c) (unbytes64[c] != BASE64_INVALID)
|
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
static const uint8 b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
2016-02-21 05:18:27 +01:00
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
static uint8 *toBase64 ( lua_State* L, const uint8 *msg, size_t *len){
|
2016-02-21 05:18:27 +01:00
|
|
|
size_t i, n = *len;
|
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
if (!n) // handle empty string case
|
2016-02-21 05:18:27 +01:00
|
|
|
return NULL;
|
|
|
|
|
2019-01-20 12:45:30 +01:00
|
|
|
int buf_size = (n + 2) / 3 * 4; // estimated encoded size
|
|
|
|
uint8 * q, *out = (uint8 *)luaM_malloc(L, buf_size);
|
2016-02-26 01:44:33 +01:00
|
|
|
uint8 bytes64[sizeof(b64)];
|
2016-02-21 05:18:27 +01:00
|
|
|
c_memcpy(bytes64, b64, sizeof(b64)); //Avoid lots of flash unaligned fetches
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-02-21 05:18:27 +01:00
|
|
|
for (i = 0, q = out; i < n; i += 3) {
|
|
|
|
int a = msg[i];
|
|
|
|
int b = (i + 1 < n) ? msg[i + 1] : 0;
|
|
|
|
int c = (i + 2 < n) ? msg[i + 2] : 0;
|
|
|
|
*q++ = bytes64[a >> 2];
|
|
|
|
*q++ = bytes64[((a & 3) << 4) | (b >> 4)];
|
|
|
|
*q++ = (i + 1 < n) ? bytes64[((b & 15) << 2) | (c >> 6)] : BASE64_PADDING;
|
|
|
|
*q++ = (i + 2 < n) ? bytes64[(c & 63)] : BASE64_PADDING;
|
|
|
|
}
|
|
|
|
*len = q - out;
|
2019-01-20 12:45:30 +01:00
|
|
|
out = luaM_realloc_(L, out, buf_size, *len); //reallocate to actual encoded length
|
2016-02-21 05:18:27 +01:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
static uint8 *fromBase64 ( lua_State* L, const uint8 *enc_msg, size_t *len){
|
|
|
|
int i, n = *len, blocks = (n>>2), pad = 0;
|
|
|
|
const uint8 *p;
|
|
|
|
uint8 unbytes64[UCHAR_MAX+1], *msg, *q;
|
2016-02-21 05:18:27 +01:00
|
|
|
|
2019-02-17 19:26:29 +01:00
|
|
|
if (!n) // handle empty string case
|
2016-02-21 05:18:27 +01:00
|
|
|
return NULL;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
if (n & 3)
|
2019-02-17 19:26:29 +01:00
|
|
|
luaL_error (L, "Invalid base64 string");
|
|
|
|
|
2016-02-21 05:18:27 +01:00
|
|
|
c_memset(unbytes64, BASE64_INVALID, sizeof(unbytes64));
|
2019-02-17 19:26:29 +01:00
|
|
|
for (i = 0; i < sizeof(b64)-1; i++) unbytes64[b64[i]] = i; // sequential so no exceptions
|
|
|
|
|
2016-02-21 05:18:27 +01:00
|
|
|
if (enc_msg[n-1] == BASE64_PADDING) {
|
|
|
|
pad = (enc_msg[n-2] != BASE64_PADDING) ? 1 : 2;
|
2016-02-26 01:44:33 +01:00
|
|
|
blocks--; //exclude padding block
|
2019-02-17 19:26:29 +01:00
|
|
|
}
|
2016-02-21 05:18:27 +01:00
|
|
|
|
|
|
|
for (i = 0; i < n - pad; i++) if (!ISBASE64(enc_msg[i])) luaL_error (L, "Invalid base64 string");
|
|
|
|
unbytes64[BASE64_PADDING] = 0;
|
|
|
|
|
2019-01-20 12:45:30 +01:00
|
|
|
int buf_size=1+ (3 * n / 4); // estimate decoded length
|
2019-02-17 19:26:29 +01:00
|
|
|
msg = q = (uint8 *) luaM_malloc(L, buf_size);
|
2016-02-26 01:44:33 +01:00
|
|
|
for (i = 0, p = enc_msg; i<blocks; i++) {
|
2019-02-17 19:26:29 +01:00
|
|
|
uint8 a = unbytes64[*p++];
|
|
|
|
uint8 b = unbytes64[*p++];
|
|
|
|
uint8 c = unbytes64[*p++];
|
2016-02-26 01:44:33 +01:00
|
|
|
uint8 d = unbytes64[*p++];
|
2016-02-21 05:18:27 +01:00
|
|
|
*q++ = (a << 2) | (b >> 4);
|
|
|
|
*q++ = (b << 4) | (c >> 2);
|
|
|
|
*q++ = (c << 6) | d;
|
|
|
|
}
|
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
if (pad) { //now process padding block bytes
|
|
|
|
uint8 a = unbytes64[*p++];
|
|
|
|
uint8 b = unbytes64[*p++];
|
2016-02-21 05:18:27 +01:00
|
|
|
*q++ = (a << 2) | (b >> 4);
|
2016-02-26 01:44:33 +01:00
|
|
|
if (pad == 1) *q++ = (b << 4) | (unbytes64[*p] >> 2);
|
2016-02-21 05:18:27 +01:00
|
|
|
}
|
|
|
|
*len = q - msg;
|
2019-01-20 12:45:30 +01:00
|
|
|
msg = luaM_realloc_(L, msg, buf_size, *len); //reallocate to actual decoded length
|
2016-02-26 01:44:33 +01:00
|
|
|
return msg;
|
2016-02-21 05:18:27 +01:00
|
|
|
}
|
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
static inline uint8 to_hex_nibble(uint8 b) {
|
2016-02-21 05:18:27 +01:00
|
|
|
return b + ( b < 10 ? '0' : 'a' - 10 );
|
|
|
|
}
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
static uint8 *toHex ( lua_State* L, const uint8 *msg, size_t *len){
|
2016-02-21 05:18:27 +01:00
|
|
|
int i, n = *len;
|
2019-01-20 12:45:30 +01:00
|
|
|
*len <<= 1;
|
|
|
|
uint8 *q, *out = (uint8 *)luaM_malloc(L, *len);
|
2016-02-21 05:18:27 +01:00
|
|
|
for (i = 0, q = out; i < n; i++) {
|
|
|
|
*q++ = to_hex_nibble(msg[i] >> 4);
|
|
|
|
*q++ = to_hex_nibble(msg[i] & 0xf);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2016-02-26 01:44:33 +01:00
|
|
|
static uint8 *fromHex ( lua_State* L, const uint8 *msg, size_t *len){
|
2016-02-21 05:18:27 +01:00
|
|
|
int i, n = *len;
|
2016-02-26 01:44:33 +01:00
|
|
|
const uint8 *p;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-02-21 05:18:27 +01:00
|
|
|
if (n &1)
|
|
|
|
luaL_error (L, "Invalid hex string");
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2019-01-20 12:45:30 +01:00
|
|
|
*len >>= 1;
|
|
|
|
uint8 b, *q, *out = (uint8 *)luaM_malloc(L, *len);
|
|
|
|
uint8 c = 0;
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-02-21 05:18:27 +01:00
|
|
|
for (i = 0, p = msg, q = out; i < n; i++) {
|
|
|
|
if (*p >= '0' && *p <= '9') {
|
|
|
|
b = *p++ - '0';
|
|
|
|
} else if (*p >= 'a' && *p <= 'f') {
|
|
|
|
b = *p++ - ('a' - 10);
|
|
|
|
} else if (*p >= 'A' && *p <= 'F') {
|
|
|
|
b = *p++ - ('A' - 10);
|
|
|
|
} else {
|
2019-01-20 12:45:30 +01:00
|
|
|
luaM_freearray(L, out, *len, uint8);
|
2016-02-21 05:18:27 +01:00
|
|
|
luaL_error (L, "Invalid hex string");
|
|
|
|
}
|
|
|
|
if ((i&1) == 0) {
|
|
|
|
c = b<<4;
|
|
|
|
} else {
|
|
|
|
*q++ = c+ b;
|
2019-02-17 19:26:29 +01:00
|
|
|
}
|
2016-02-21 05:18:27 +01:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// All encoder functions are of the form:
|
2019-02-17 19:26:29 +01:00
|
|
|
// Lua: output_string = encoder.function(input_string)
|
2016-02-21 05:18:27 +01:00
|
|
|
// Where input string maybe empty, but not nil
|
2016-02-26 01:44:33 +01:00
|
|
|
// Hence these all call the do_func wrapper
|
|
|
|
static int do_func (lua_State *L, uint8 * (*conv_func)(lua_State *, const uint8 *, size_t *)) {
|
2019-01-20 12:45:30 +01:00
|
|
|
size_t len;
|
|
|
|
const uint8 *input = luaL_checklstring(L, 1, &len);
|
|
|
|
uint8 *output = conv_func(L, input, &len);
|
2019-02-17 19:26:29 +01:00
|
|
|
|
2016-02-21 05:18:27 +01:00
|
|
|
if (output) {
|
2019-01-20 12:45:30 +01:00
|
|
|
lua_pushlstring(L, output, len);
|
|
|
|
luaM_freearray(L, output, len, uint8);
|
2016-02-21 05:18:27 +01:00
|
|
|
} else {
|
|
|
|
lua_pushstring(L, "");
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DECLARE_FUNCTION(f) static int encoder_ ## f (lua_State *L) \
|
|
|
|
{ return do_func(L, f); }
|
|
|
|
|
|
|
|
DECLARE_FUNCTION(fromBase64);
|
|
|
|
DECLARE_FUNCTION(toBase64);
|
|
|
|
DECLARE_FUNCTION(fromHex);
|
|
|
|
DECLARE_FUNCTION(toHex);
|
|
|
|
|
|
|
|
// Module function map
|
2019-05-08 13:08:20 +02:00
|
|
|
LROT_BEGIN(encoder)
|
|
|
|
LROT_FUNCENTRY( fromBase64, encoder_fromBase64 )
|
|
|
|
LROT_FUNCENTRY( toBase64, encoder_toBase64 )
|
|
|
|
LROT_FUNCENTRY( fromHex, encoder_fromHex )
|
|
|
|
LROT_FUNCENTRY( toHex, encoder_toHex )
|
|
|
|
LROT_END( encoder, NULL, 0 )
|
|
|
|
|
|
|
|
|
|
|
|
NODEMCU_MODULE(ENCODER, "encoder", encoder, NULL);
|