encoder.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Module encoders
  2. #include "module.h"
  3. #include "lauxlib.h"
  4. #include "lmem.h"
  5. #include "c_string.h"
  6. #define BASE64_INVALID '\xff'
  7. #define BASE64_PADDING '='
  8. #define ISBASE64(c) (unbytes64[c] != BASE64_INVALID)
  9. static const uint8 b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  10. static uint8 *toBase64 ( lua_State* L, const uint8 *msg, size_t *len){
  11. size_t i, n = *len;
  12. if (!n) // handle empty string case
  13. return NULL;
  14. uint8 * q, *out = (uint8 *)luaM_malloc(L, (n + 2) / 3 * 4);
  15. uint8 bytes64[sizeof(b64)];
  16. c_memcpy(bytes64, b64, sizeof(b64)); //Avoid lots of flash unaligned fetches
  17. for (i = 0, q = out; i < n; i += 3) {
  18. int a = msg[i];
  19. int b = (i + 1 < n) ? msg[i + 1] : 0;
  20. int c = (i + 2 < n) ? msg[i + 2] : 0;
  21. *q++ = bytes64[a >> 2];
  22. *q++ = bytes64[((a & 3) << 4) | (b >> 4)];
  23. *q++ = (i + 1 < n) ? bytes64[((b & 15) << 2) | (c >> 6)] : BASE64_PADDING;
  24. *q++ = (i + 2 < n) ? bytes64[(c & 63)] : BASE64_PADDING;
  25. }
  26. *len = q - out;
  27. return out;
  28. }
  29. static uint8 *fromBase64 ( lua_State* L, const uint8 *enc_msg, size_t *len){
  30. int i, n = *len, blocks = (n>>2), pad = 0;
  31. const uint8 *p;
  32. uint8 unbytes64[UCHAR_MAX+1], *msg, *q;
  33. if (!n) // handle empty string case
  34. return NULL;
  35. if (n & 3)
  36. luaL_error (L, "Invalid base64 string");
  37. c_memset(unbytes64, BASE64_INVALID, sizeof(unbytes64));
  38. for (i = 0; i < sizeof(b64)-1; i++) unbytes64[b64[i]] = i; // sequential so no exceptions
  39. if (enc_msg[n-1] == BASE64_PADDING) {
  40. pad = (enc_msg[n-2] != BASE64_PADDING) ? 1 : 2;
  41. blocks--; //exclude padding block
  42. }
  43. for (i = 0; i < n - pad; i++) if (!ISBASE64(enc_msg[i])) luaL_error (L, "Invalid base64 string");
  44. unbytes64[BASE64_PADDING] = 0;
  45. msg = q = (uint8 *) luaM_malloc(L, 1+ (3 * n / 4));
  46. for (i = 0, p = enc_msg; i<blocks; i++) {
  47. uint8 a = unbytes64[*p++];
  48. uint8 b = unbytes64[*p++];
  49. uint8 c = unbytes64[*p++];
  50. uint8 d = unbytes64[*p++];
  51. *q++ = (a << 2) | (b >> 4);
  52. *q++ = (b << 4) | (c >> 2);
  53. *q++ = (c << 6) | d;
  54. }
  55. if (pad) { //now process padding block bytes
  56. uint8 a = unbytes64[*p++];
  57. uint8 b = unbytes64[*p++];
  58. *q++ = (a << 2) | (b >> 4);
  59. if (pad == 1) *q++ = (b << 4) | (unbytes64[*p] >> 2);
  60. }
  61. *len = q - msg;
  62. return msg;
  63. }
  64. static inline uint8 to_hex_nibble(uint8 b) {
  65. return b + ( b < 10 ? '0' : 'a' - 10 );
  66. }
  67. static uint8 *toHex ( lua_State* L, const uint8 *msg, size_t *len){
  68. int i, n = *len;
  69. uint8 *q, *out = (uint8 *)luaM_malloc(L, n * 2);
  70. for (i = 0, q = out; i < n; i++) {
  71. *q++ = to_hex_nibble(msg[i] >> 4);
  72. *q++ = to_hex_nibble(msg[i] & 0xf);
  73. }
  74. *len = 2*n;
  75. return out;
  76. }
  77. static uint8 *fromHex ( lua_State* L, const uint8 *msg, size_t *len){
  78. int i, n = *len;
  79. const uint8 *p;
  80. uint8 b, *q, *out = (uint8 *)luaM_malloc(L, n * 2);
  81. uint8 c;
  82. if (n &1)
  83. luaL_error (L, "Invalid hex string");
  84. for (i = 0, p = msg, q = out; i < n; i++) {
  85. if (*p >= '0' && *p <= '9') {
  86. b = *p++ - '0';
  87. } else if (*p >= 'a' && *p <= 'f') {
  88. b = *p++ - ('a' - 10);
  89. } else if (*p >= 'A' && *p <= 'F') {
  90. b = *p++ - ('A' - 10);
  91. } else {
  92. luaL_error (L, "Invalid hex string");
  93. }
  94. if ((i&1) == 0) {
  95. c = b<<4;
  96. } else {
  97. *q++ = c+ b;
  98. }
  99. }
  100. *len = n>>1;
  101. return out;
  102. }
  103. // All encoder functions are of the form:
  104. // Lua: output_string = encoder.function(input_string)
  105. // Where input string maybe empty, but not nil
  106. // Hence these all call the do_func wrapper
  107. static int do_func (lua_State *L, uint8 * (*conv_func)(lua_State *, const uint8 *, size_t *)) {
  108. size_t l;
  109. const uint8 *input = luaL_checklstring(L, 1, &l);
  110. // luaL_argcheck(L, l>0, 1, "input string empty");
  111. uint8 *output = conv_func(L, input, &l);
  112. if (output) {
  113. lua_pushlstring(L, output, l);
  114. luaM_free(L, output);
  115. } else {
  116. lua_pushstring(L, "");
  117. }
  118. return 1;
  119. }
  120. #define DECLARE_FUNCTION(f) static int encoder_ ## f (lua_State *L) \
  121. { return do_func(L, f); }
  122. DECLARE_FUNCTION(fromBase64);
  123. DECLARE_FUNCTION(toBase64);
  124. DECLARE_FUNCTION(fromHex);
  125. DECLARE_FUNCTION(toHex);
  126. // Module function map
  127. static const LUA_REG_TYPE encoder_map[] = {
  128. { LSTRKEY("fromBase64"), LFUNCVAL(encoder_fromBase64) },
  129. { LSTRKEY("toBase64"), LFUNCVAL(encoder_toBase64) },
  130. { LSTRKEY("fromHex"), LFUNCVAL(encoder_fromHex) },
  131. { LSTRKEY("toHex"), LFUNCVAL(encoder_toHex) },
  132. { LNILKEY, LNILVAL }
  133. };
  134. NODEMCU_MODULE(ENCODER, "encoder", encoder_map, NULL);