base64.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include <stdio.h>
  2. #include "base64.h"
  3. /* BASE 64 encode table */
  4. static const char base64en[] = {
  5. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
  6. 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
  7. 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
  8. 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
  9. 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
  10. 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
  11. 'w', 'x', 'y', 'z', '0', '1', '2', '3',
  12. '4', '5', '6', '7', '8', '9', '+', '/',
  13. };
  14. #define BASE64_PAD '='
  15. #define BASE64DE_FIRST '+'
  16. #define BASE64DE_LAST 'z'
  17. /* ASCII order for BASE 64 decode, -1 in unused character */
  18. static const signed char base64de[] = {
  19. /* '+', ',', '-', '.', '/', '0', '1', '2', */
  20. 62, -1, -1, -1, 63, 52, 53, 54,
  21. /* '3', '4', '5', '6', '7', '8', '9', ':', */
  22. 55, 56, 57, 58, 59, 60, 61, -1,
  23. /* ';', '<', '=', '>', '?', '@', 'A', 'B', */
  24. -1, -1, -1, -1, -1, -1, 0, 1,
  25. /* 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', */
  26. 2, 3, 4, 5, 6, 7, 8, 9,
  27. /* 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', */
  28. 10, 11, 12, 13, 14, 15, 16, 17,
  29. /* 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', */
  30. 18, 19, 20, 21, 22, 23, 24, 25,
  31. /* '[', '\', ']', '^', '_', '`', 'a', 'b', */
  32. -1, -1, -1, -1, -1, -1, 26, 27,
  33. /* 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', */
  34. 28, 29, 30, 31, 32, 33, 34, 35,
  35. /* 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', */
  36. 36, 37, 38, 39, 40, 41, 42, 43,
  37. /* 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', */
  38. 44, 45, 46, 47, 48, 49, 50, 51,
  39. };
  40. int base64_encode(const unsigned char *in, unsigned int inlen, char *out) {
  41. unsigned int i, j;
  42. for (i = j = 0; i < inlen; i++) {
  43. int s = i % 3;
  44. switch (s) {
  45. case 0:
  46. out[j++] = base64en[(in[i] >> 2) & 0x3F];
  47. continue;
  48. case 1:
  49. out[j++] = base64en[((in[i-1] & 0x3) << 4) + ((in[i] >> 4) & 0xF)];
  50. continue;
  51. case 2:
  52. out[j++] = base64en[((in[i-1] & 0xF) << 2) + ((in[i] >> 6) & 0x3)];
  53. out[j++] = base64en[in[i] & 0x3F];
  54. }
  55. }
  56. /* move back */
  57. i -= 1;
  58. /* check the last and add padding */
  59. if ((i % 3) == 0) {
  60. out[j++] = base64en[(in[i] & 0x3) << 4];
  61. out[j++] = BASE64_PAD;
  62. out[j++] = BASE64_PAD;
  63. } else if ((i % 3) == 1) {
  64. out[j++] = base64en[(in[i] & 0xF) << 2];
  65. out[j++] = BASE64_PAD;
  66. }
  67. return BASE64_OK;
  68. }
  69. int base64_decode(const char *in, unsigned int inlen, unsigned char *out) {
  70. unsigned int i, j;
  71. for (i = j = 0; i < inlen; i++) {
  72. int c;
  73. int s = i % 4;
  74. if (in[i] == '=')
  75. return BASE64_OK;
  76. if (in[i] < BASE64DE_FIRST || in[i] > BASE64DE_LAST ||
  77. (c = base64de[in[i] - BASE64DE_FIRST]) == -1)
  78. return BASE64_INVALID;
  79. switch (s) {
  80. case 0:
  81. out[j] = ((unsigned int)c << 2) & 0xFF;
  82. continue;
  83. case 1:
  84. out[j++] += ((unsigned int)c >> 4) & 0x3;
  85. /* if not last char with padding */
  86. if (i < (inlen - 3) || in[inlen - 2] != '=')
  87. out[j] = ((unsigned int)c & 0xF) << 4;
  88. continue;
  89. case 2:
  90. out[j++] += ((unsigned int)c >> 2) & 0xF;
  91. /* if not last char with padding */
  92. if (i < (inlen - 2) || in[inlen - 1] != '=')
  93. out[j] = ((unsigned int)c & 0x3) << 6;
  94. continue;
  95. case 3:
  96. out[j++] += (unsigned char)c;
  97. }
  98. }
  99. return BASE64_OK;
  100. }