base64.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* base64.c
  2. * Michael McTernan, mm7323@bris.ac.uk
  3. * Functions to encode and decode strings
  4. * into base 64 (RFC1521 section 7).
  5. */
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <netinet/in.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11. #include <syslog.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include "base64.h"
  16. /* Lookup tables for fast translation */
  17. static const char base64EncTable[64]="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  18. "abcdefghijklmnopqrstuvwxyz"
  19. "0123456789+/";
  20. static const char base64DecTable[256] = {
  21. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  22. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  23. 00,00,00,00, 00,00,00,00, 00,00,00,62, 00,00,00,63,
  24. 52,53,54,55, 56,57,58,59, 60,61,00,00, 00,00,00,00,
  25. 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  26. 15,16,17,18, 19,20,21,22, 23,24,25,00, 00,00,00,00,
  27. 00,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  28. 41,42,43,44, 45,46,47,48, 49,50,51,00, 00,00,00,00,
  29. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  30. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  31. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  32. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  33. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  34. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  35. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  36. 00,00,00,00, 00,00,00,00, 00,00,00,00, 00,00,00,00,
  37. };
  38. /*B64_Decode*/
  39. /* Decode string into a new buffer and
  40. * return it. Memory for the new
  41. * buffer is allocated.
  42. */
  43. char *B64_Decode(const char *string) {
  44. int stringLen=strlen(string);
  45. char *result=malloc((ceil(stringLen/4.0)*3)+1);
  46. int t,o;
  47. if(result==NULL) {
  48. /* TODO: Some proper handling here */
  49. abort();
  50. }
  51. t=o=0;
  52. do {
  53. result[o++]=((base64DecTable[(int)string[t]] << 2 ) & 0xfc ) |
  54. ((base64DecTable[(int)string[t+1]] >> 4 ) & 0x03 );
  55. result[o++]=((base64DecTable[(int)string[t+1]] << 4 ) & 0xf0 ) |
  56. ((base64DecTable[(int)string[t+2]] >> 2 ) & 0x0f );
  57. result[o++]=((base64DecTable[(int)string[t+2]] << 6 ) & 0xc0 ) |
  58. ((base64DecTable[(int)string[t+3]]));
  59. t+=4;
  60. } while(t<strlen(string));
  61. return result;
  62. }
  63. /*B64_Encode*/
  64. /* Takes string and encodes it in base 64.
  65. * Returns the new string in allocated memory.
  66. */
  67. char *B64_Encode(const char *string) {
  68. int stringLen=strlen(string);
  69. char *result=malloc((int)(ceil(stringLen/3.0)*4)+1);
  70. int t,o;
  71. if(result==NULL) {
  72. /* TODO: Some proper handling here */
  73. abort();
  74. }
  75. t=o=0;
  76. do {
  77. result[o++]=base64EncTable[(string[t] >> 2) & 0x3f];
  78. result[o++]=base64EncTable[((string[t] << 4) & 0x30) |
  79. ((string[t+1] >> 4) & 0x0f)];
  80. if(t+1<stringLen)
  81. result[o++]=base64EncTable[((string[t+1] << 2) & 0x3c) |
  82. ((string[t+2] >> 6) & 0x03)];
  83. else
  84. result[o++]='=';
  85. if(t+2<stringLen)
  86. result[o++]=base64EncTable[((string[t+2] ) & 0x3f)];
  87. else
  88. result[o++]='=';
  89. t+=3;
  90. } while(t<strlen(string));
  91. /* Null terminate */
  92. result[o++]='\0';
  93. return result;
  94. }