base64.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include <string.h>
  2. char b64string[] =
  3. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  4. long base64_encode (to, from, len)
  5. char *to, *from;
  6. unsigned int len;
  7. {
  8. char *fromp = from;
  9. char *top = to;
  10. unsigned char cbyte;
  11. unsigned char obyte;
  12. char end[3];
  13. for (; len >= 3; len -= 3) {
  14. cbyte = *fromp++;
  15. *top++ = b64string[(int)(cbyte >> 2)];
  16. obyte = (cbyte << 4) & 0x30; /* 0011 0000 */
  17. cbyte = *fromp++;
  18. obyte |= (cbyte >> 4); /* 0000 1111 */
  19. *top++ = b64string[(int)obyte];
  20. obyte = (cbyte << 2) & 0x3C; /* 0011 1100 */
  21. cbyte = *fromp++;
  22. obyte |= (cbyte >> 6); /* 0000 0011 */
  23. *top++ = b64string[(int)obyte];
  24. *top++ = b64string[(int)(cbyte & 0x3F)];/* 0011 1111 */
  25. }
  26. if (len) {
  27. end[0] = *fromp++;
  28. if (--len) end[1] = *fromp++; else end[1] = 0;
  29. end[2] = 0;
  30. cbyte = end[0];
  31. *top++ = b64string[(int)(cbyte >> 2)];
  32. obyte = (cbyte << 4) & 0x30; /* 0011 0000 */
  33. cbyte = end[1];
  34. obyte |= (cbyte >> 4);
  35. *top++ = b64string[(int)obyte];
  36. obyte = (cbyte << 2) & 0x3C; /* 0011 1100 */
  37. if (len) *top++ = b64string[(int)obyte];
  38. else *top++ = '=';
  39. *top++ = '=';
  40. }
  41. *top = 0;
  42. return top - to;
  43. }
  44. /* badchar(): check if c is decent; puts either the */
  45. /* location of c or null into p. */
  46. #define badchar(c,p) (!(p = memchr(b64string, c, 64)))
  47. long base64_decode (to, from, len)
  48. char *to, *from;
  49. unsigned int len;
  50. {
  51. char *fromp = from;
  52. char *top = to;
  53. char *p;
  54. unsigned char cbyte;
  55. unsigned char obyte;
  56. int padding = 0;
  57. for (; len >= 4; len -= 4) {
  58. if ((cbyte = *fromp++) == '=') cbyte = 0;
  59. else {
  60. if (badchar(cbyte, p)) return -1;
  61. cbyte = (p - b64string);
  62. }
  63. obyte = cbyte << 2; /* 1111 1100 */
  64. if ((cbyte = *fromp++) == '=') cbyte = 0;
  65. else {
  66. if (badchar(cbyte, p)) return -1;
  67. cbyte = p - b64string;
  68. }
  69. obyte |= cbyte >> 4; /* 0000 0011 */
  70. *top++ = obyte;
  71. obyte = cbyte << 4; /* 1111 0000 */
  72. if ((cbyte = *fromp++) == '=') { cbyte = 0; padding++; }
  73. else {
  74. padding = 0;
  75. if (badchar (cbyte, p)) return -1;
  76. cbyte = p - b64string;
  77. }
  78. obyte |= cbyte >> 2; /* 0000 1111 */
  79. *top++ = obyte;
  80. obyte = cbyte << 6; /* 1100 0000 */
  81. if ((cbyte = *fromp++) == '=') { cbyte = 0; padding++; }
  82. else {
  83. padding = 0;
  84. if (badchar (cbyte, p)) return -1;
  85. cbyte = p - b64string;
  86. }
  87. obyte |= cbyte; /* 0011 1111 */
  88. *top++ = obyte;
  89. }
  90. *top = 0;
  91. if (len) return -1;
  92. return (top - to) - padding;
  93. }