base64.h 4.3 KB

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