base64.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Hgskolan
  3. * (Royal Institute of Technology, Stockholm, Sweden).
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. *
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this software
  18. * must display the following acknowledgement:
  19. * This product includes software developed by the Kungliga Tekniska
  20. * Hgskolan and its contributors.
  21. *
  22. * 4. Neither the name of the Institute nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
  27. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  32. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  33. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  35. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  36. * SUCH DAMAGE.
  37. */
  38. #ifdef HAVE_CONFIG_H
  39. #include <config.h>
  40. /*RCSID("$Id: base64.c,v 1.1 2005/02/11 07:34:35 jpm Exp jpm $");*/
  41. #endif
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include "base64.h"
  45. static char base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  46. static int pos(char c)
  47. {
  48. char *p;
  49. for(p = base64; *p; p++)
  50. if(*p == c)
  51. return p - base64;
  52. return -1;
  53. }
  54. int base64_encode(const void *data, int size, char **str)
  55. {
  56. char *s, *p;
  57. int i;
  58. int c;
  59. unsigned char *q;
  60. p = s = (char*)malloc(size*4/3+4);
  61. if (p == NULL)
  62. return -1;
  63. q = (unsigned char*)data;
  64. i=0;
  65. for(i = 0; i < size;){
  66. c=q[i++];
  67. c*=256;
  68. if(i < size)
  69. c+=q[i];
  70. i++;
  71. c*=256;
  72. if(i < size)
  73. c+=q[i];
  74. i++;
  75. p[0]=base64[(c&0x00fc0000) >> 18];
  76. p[1]=base64[(c&0x0003f000) >> 12];
  77. p[2]=base64[(c&0x00000fc0) >> 6];
  78. p[3]=base64[(c&0x0000003f) >> 0];
  79. if(i > size)
  80. p[3]='=';
  81. if(i > size+1)
  82. p[2]='=';
  83. p+=4;
  84. }
  85. *p=0;
  86. *str = s;
  87. return strlen(s);
  88. }
  89. int base64_decode(const char *str, void *data)
  90. {
  91. const char *p;
  92. unsigned char *q;
  93. int c;
  94. int x;
  95. int done = 0;
  96. q=(unsigned char*)data;
  97. for(p=str; *p && !done; p+=4){
  98. x = pos(p[0]);
  99. if(x >= 0)
  100. c = x;
  101. else{
  102. done = 3;
  103. break;
  104. }
  105. c*=64;
  106. x = pos(p[1]);
  107. if(x >= 0)
  108. c += x;
  109. else
  110. return -1;
  111. c*=64;
  112. if(p[2] == '=')
  113. done++;
  114. else{
  115. x = pos(p[2]);
  116. if(x >= 0)
  117. c += x;
  118. else
  119. return -1;
  120. }
  121. c*=64;
  122. if(p[3] == '=')
  123. done++;
  124. else{
  125. if(done)
  126. return -1;
  127. x = pos(p[3]);
  128. if(x >= 0)
  129. c += x;
  130. else
  131. return -1;
  132. }
  133. if(done < 3)
  134. *q++=(c&0x00ff0000)>>16;
  135. if(done < 2)
  136. *q++=(c&0x0000ff00)>>8;
  137. if(done < 1)
  138. *q++=(c&0x000000ff)>>0;
  139. }
  140. return q - (unsigned char*)data;
  141. }