encoder.c 4.6 KB

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