base64.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "precompile.h"
  2. #include "base64.h"
  3. /* aaaack but it's fast and const should make it shared text page. */
  4. static const unsigned char pr2six[256] =
  5. {
  6. /* ASCII table */
  7. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  8. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  9. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  10. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  11. 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  12. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  13. 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  14. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  15. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  16. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  17. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  18. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  19. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  20. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  21. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  22. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  23. };
  24. static const char basis_64[] =
  25. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  26. TOOLKIT_API int base64_encode(char * coded_dst, const char *plain_src, int len_plain_src)
  27. {
  28. int i;
  29. char *p;
  30. p = coded_dst;
  31. for (i = 0; i < len_plain_src - 2; i += 3) {
  32. *p++ = basis_64[(plain_src[i] >> 2) & 0x3F];
  33. *p++ = basis_64[((plain_src[i] & 0x3) << 4) |
  34. ((int) (plain_src[i + 1] & 0xF0) >> 4)];
  35. *p++ = basis_64[((plain_src[i + 1] & 0xF) << 2) |
  36. ((int) (plain_src[i + 2] & 0xC0) >> 6)];
  37. *p++ = basis_64[plain_src[i + 2] & 0x3F];
  38. }
  39. if (i < len_plain_src) {
  40. *p++ = basis_64[(plain_src[i] >> 2) & 0x3F];
  41. if (i == (len_plain_src - 1)) {
  42. *p++ = basis_64[((plain_src[i] & 0x3) << 4)];
  43. *p++ = '=';
  44. }
  45. else {
  46. *p++ = basis_64[((plain_src[i] & 0x3) << 4) |
  47. ((int) (plain_src[i + 1] & 0xF0) >> 4)];
  48. *p++ = basis_64[((plain_src[i + 1] & 0xF) << 2)];
  49. }
  50. *p++ = '=';
  51. }
  52. *p++ = '\0';
  53. return (int)(p - coded_dst);
  54. }
  55. TOOLKIT_API int base64_decode_len(const char * bufcoded)
  56. {
  57. int nbytesdecoded;
  58. register const unsigned char *bufin;
  59. register int nprbytes;
  60. bufin = (const unsigned char *) bufcoded;
  61. while (pr2six[*(bufin++)] <= 63);
  62. nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
  63. nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
  64. return nbytesdecoded + 1;
  65. }
  66. TOOLKIT_API int base64_decode(char * bufplain, const char *bufcoded)
  67. {
  68. int nbytesdecoded;
  69. register const unsigned char *bufin;
  70. register unsigned char *bufout;
  71. register int nprbytes;
  72. bufin = (const unsigned char *) bufcoded;
  73. while (pr2six[*(bufin++)] <= 63);
  74. nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
  75. nbytesdecoded = (((int)nprbytes + 3) / 4) * 3;
  76. bufout = (unsigned char *) bufplain;
  77. bufin = (const unsigned char *) bufcoded;
  78. while (nprbytes > 4) {
  79. *(bufout++) =
  80. (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  81. *(bufout++) =
  82. (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  83. *(bufout++) =
  84. (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  85. bufin += 4;
  86. nprbytes -= 4;
  87. }
  88. /* Note: (nprbytes == 1) would be an error, so just ingore that case */
  89. if (nprbytes > 1) {
  90. *(bufout++) =
  91. (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  92. }
  93. if (nprbytes > 2) {
  94. *(bufout++) =
  95. (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  96. }
  97. if (nprbytes > 3) {
  98. *(bufout++) =
  99. (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  100. }
  101. nbytesdecoded -= (4 - (int)nprbytes) & 3;
  102. *(bufout++) = '\0';
  103. return nbytesdecoded + 1;
  104. }