base64.c 2.6 KB

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