base64.cpp 4.3 KB

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