2
0

base64.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include <string.h>
  2. #include "int.h"
  3. #include "xmlrpc-c/base64_int.h"
  4. void
  5. xmlrpc_base64Encode(const char * const chars,
  6. char * const base64) {
  7. /* Conversion table. */
  8. static char tbl[64] = {
  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. unsigned int i;
  19. uint32_t length;
  20. char * p;
  21. const char * s;
  22. length = strlen(chars); /* initial value */
  23. s = &chars[0]; /* initial value */
  24. p = &base64[0]; /* initial value */
  25. /* Transform the 3x8 bits to 4x6 bits, as required by base64. */
  26. for (i = 0; i < length; i += 3) {
  27. *p++ = tbl[s[0] >> 2];
  28. *p++ = tbl[((s[0] & 3) << 4) + (s[1] >> 4)];
  29. *p++ = tbl[((s[1] & 0xf) << 2) + (s[2] >> 6)];
  30. *p++ = tbl[s[2] & 0x3f];
  31. s += 3;
  32. }
  33. /* Pad the result if necessary... */
  34. if (i == length + 1)
  35. *(p - 1) = '=';
  36. else if (i == length + 2)
  37. *(p - 1) = *(p - 2) = '=';
  38. /* ...and zero-terminate it. */
  39. *p = '\0';
  40. }