base64.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #include "common.h"
  3. #if PK_MODULE_BASE64
  4. #include "cffi.h"
  5. namespace pkpy {
  6. // https://github.com/zhicheng/base64/blob/master/base64.c
  7. const char BASE64_PAD = '=';
  8. const char BASE64DE_FIRST = '+';
  9. const char BASE64DE_LAST = 'z';
  10. /* BASE 64 encode table */
  11. const char base64en[] = {
  12. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  13. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  14. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  15. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  16. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  17. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  18. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  19. '4', '5', '6', '7', '8', '9', '+', '/',
  20. };
  21. /* ASCII order for BASE 64 decode, 255 in unused character */
  22. const unsigned char base64de[] = {
  23. /* nul, soh, stx, etx, eot, enq, ack, bel, */
  24. 255, 255, 255, 255, 255, 255, 255, 255,
  25. /* bs, ht, nl, vt, np, cr, so, si, */
  26. 255, 255, 255, 255, 255, 255, 255, 255,
  27. /* dle, dc1, dc2, dc3, dc4, nak, syn, etb, */
  28. 255, 255, 255, 255, 255, 255, 255, 255,
  29. /* can, em, sub, esc, fs, gs, rs, us, */
  30. 255, 255, 255, 255, 255, 255, 255, 255,
  31. /* sp, '!', '"', '#', '$', '%', '&', ''', */
  32. 255, 255, 255, 255, 255, 255, 255, 255,
  33. /* '(', ')', '*', '+', ',', '-', '.', '/', */
  34. 255, 255, 255, 62, 255, 255, 255, 63,
  35. /* '0', '1', '2', '3', '4', '5', '6', '7', */
  36. 52, 53, 54, 55, 56, 57, 58, 59,
  37. /* '8', '9', ':', ';', '<', '=', '>', '?', */
  38. 60, 61, 255, 255, 255, 255, 255, 255,
  39. /* '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', */
  40. 255, 0, 1, 2, 3, 4, 5, 6,
  41. /* 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', */
  42. 7, 8, 9, 10, 11, 12, 13, 14,
  43. /* 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', */
  44. 15, 16, 17, 18, 19, 20, 21, 22,
  45. /* 'X', 'Y', 'Z', '[', '\', ']', '^', '_', */
  46. 23, 24, 25, 255, 255, 255, 255, 255,
  47. /* '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', */
  48. 255, 26, 27, 28, 29, 30, 31, 32,
  49. /* 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', */
  50. 33, 34, 35, 36, 37, 38, 39, 40,
  51. /* 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', */
  52. 41, 42, 43, 44, 45, 46, 47, 48,
  53. /* 'x', 'y', 'z', '{', '|', '}', '~', del, */
  54. 49, 50, 51, 255, 255, 255, 255, 255
  55. };
  56. inline static unsigned int
  57. base64_encode(const unsigned char *in, unsigned int inlen, char *out)
  58. {
  59. int s;
  60. unsigned int i;
  61. unsigned int j;
  62. unsigned char c;
  63. unsigned char l;
  64. s = 0;
  65. l = 0;
  66. for (i = j = 0; i < inlen; i++) {
  67. c = in[i];
  68. switch (s) {
  69. case 0:
  70. s = 1;
  71. out[j++] = base64en[(c >> 2) & 0x3F];
  72. break;
  73. case 1:
  74. s = 2;
  75. out[j++] = base64en[((l & 0x3) << 4) | ((c >> 4) & 0xF)];
  76. break;
  77. case 2:
  78. s = 0;
  79. out[j++] = base64en[((l & 0xF) << 2) | ((c >> 6) & 0x3)];
  80. out[j++] = base64en[c & 0x3F];
  81. break;
  82. }
  83. l = c;
  84. }
  85. switch (s) {
  86. case 1:
  87. out[j++] = base64en[(l & 0x3) << 4];
  88. out[j++] = BASE64_PAD;
  89. out[j++] = BASE64_PAD;
  90. break;
  91. case 2:
  92. out[j++] = base64en[(l & 0xF) << 2];
  93. out[j++] = BASE64_PAD;
  94. break;
  95. }
  96. out[j] = 0;
  97. return j;
  98. }
  99. inline static unsigned int
  100. base64_decode(const char *in, unsigned int inlen, unsigned char *out)
  101. {
  102. unsigned int i;
  103. unsigned int j;
  104. unsigned char c;
  105. if (inlen & 0x3) {
  106. return 0;
  107. }
  108. for (i = j = 0; i < inlen; i++) {
  109. if (in[i] == BASE64_PAD) {
  110. break;
  111. }
  112. if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST) {
  113. return 0;
  114. }
  115. c = base64de[(unsigned char)in[i]];
  116. if (c == 255) {
  117. return 0;
  118. }
  119. switch (i & 0x3) {
  120. case 0:
  121. out[j] = (c << 2) & 0xFF;
  122. break;
  123. case 1:
  124. out[j++] |= (c >> 4) & 0x3;
  125. out[j] = (c & 0xF) << 4;
  126. break;
  127. case 2:
  128. out[j++] |= (c >> 2) & 0xF;
  129. out[j] = (c & 0x3) << 6;
  130. break;
  131. case 3:
  132. out[j++] |= c;
  133. break;
  134. }
  135. }
  136. return j;
  137. }
  138. inline void add_module_base64(VM* vm){
  139. PyObject* mod = vm->new_module("base64");
  140. // b64encode
  141. vm->bind_func<1>(mod, "b64encode", [](VM* vm, ArgsView args){
  142. Bytes& b = CAST(Bytes&, args[0]);
  143. std::vector<char> out(b.size() * 2);
  144. int size = base64_encode((const unsigned char*)b.data(), b.size(), out.data());
  145. out.resize(size);
  146. return VAR(Bytes(std::move(out)));
  147. });
  148. // b64decode
  149. vm->bind_func<1>(mod, "b64decode", [](VM* vm, ArgsView args){
  150. Bytes& b = CAST(Bytes&, args[0]);
  151. std::vector<char> out(b.size());
  152. int size = base64_decode(b.data(), b.size(), (unsigned char*)out.data());
  153. out.resize(size);
  154. return VAR(Bytes(std::move(out)));
  155. });
  156. }
  157. } // namespace pkpy
  158. #else
  159. ADD_MODULE_PLACEHOLDER(base64)
  160. #endif