base64.c 4.8 KB

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