Base64.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*=========================================================================
  2. Program: KWSys - Kitware System Library
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
  8. See http://www.cmake.org/HTML/Copyright.html for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. #define KWSYS_IN_BASE64_C
  14. #include "kwsysPrivate.h"
  15. #include KWSYS_HEADER(Base64.h)
  16. /*--------------------------------------------------------------------------*/
  17. static const unsigned char kwsysBase64EncodeTable[65] =
  18. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  19. "abcdefghijklmnopqrstuvwxyz"
  20. "0123456789+/";
  21. /*--------------------------------------------------------------------------*/
  22. static const unsigned char kwsysBase64DecodeTable[256] =
  23. {
  24. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  25. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  26. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  27. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  28. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  29. 0xFF,0xFF,0xFF,0x3E,0xFF,0xFF,0xFF,0x3F,
  30. 0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,
  31. 0x3C,0x3D,0xFF,0xFF,0xFF,0x00,0xFF,0xFF,
  32. 0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06,
  33. 0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,
  34. 0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
  35. 0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF,
  36. 0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F,0x20,
  37. 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,
  38. 0x29,0x2A,0x2B,0x2C,0x2D,0x2E,0x2F,0x30,
  39. 0x31,0x32,0x33,0xFF,0xFF,0xFF,0xFF,0xFF,
  40. /*------------------------------------*/
  41. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  42. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  43. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  44. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  45. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  47. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  48. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  49. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  50. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  51. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  52. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  54. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  55. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  56. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  57. };
  58. /*--------------------------------------------------------------------------*/
  59. static unsigned char kwsysBase64EncodeChar(int c)
  60. {
  61. return kwsysBase64EncodeTable[(unsigned char)c];
  62. }
  63. /*--------------------------------------------------------------------------*/
  64. static unsigned char kwsysBase64DecodeChar(unsigned char c)
  65. {
  66. return kwsysBase64DecodeTable[c];
  67. }
  68. /*--------------------------------------------------------------------------*/
  69. /* Encode 3 bytes into a 4 byte string. */
  70. void kwsysBase64_Encode3(const unsigned char *src, unsigned char *dest)
  71. {
  72. dest[0] = kwsysBase64EncodeChar((src[0] >> 2) & 0x3F);
  73. dest[1] = kwsysBase64EncodeChar(((src[0] << 4) & 0x30)|((src[1] >> 4) & 0x0F));
  74. dest[2] = kwsysBase64EncodeChar(((src[1] << 2) & 0x3C)|((src[2] >> 6) & 0x03));
  75. dest[3] = kwsysBase64EncodeChar(src[2] & 0x3F);
  76. }
  77. /*--------------------------------------------------------------------------*/
  78. /* Encode 2 bytes into a 4 byte string. */
  79. void kwsysBase64_Encode2(const unsigned char *src, unsigned char *dest)
  80. {
  81. dest[0] = kwsysBase64EncodeChar((src[0] >> 2) & 0x3F);
  82. dest[1] = kwsysBase64EncodeChar(((src[0] << 4) & 0x30)|((src[1] >> 4) & 0x0F));
  83. dest[2] = kwsysBase64EncodeChar(((src[1] << 2) & 0x3C)|((src[2] >> 6) & 0x03));
  84. dest[3] = '=';
  85. }
  86. /*--------------------------------------------------------------------------*/
  87. /* Encode 1 bytes into a 4 byte string. */
  88. void kwsysBase64_Encode1(const unsigned char *src, unsigned char *dest)
  89. {
  90. dest[0] = kwsysBase64EncodeChar((src[0] >> 2) & 0x3F);
  91. dest[1] = kwsysBase64EncodeChar(((src[0] << 4) & 0x30));
  92. dest[2] = '=';
  93. dest[3] = '=';
  94. }
  95. /*--------------------------------------------------------------------------*/
  96. /* Encode 'length' bytes from the input buffer and store the
  97. encoded stream into the output buffer. Return the length of the encoded
  98. buffer (output). Note that the output buffer must be allocated by the caller
  99. (length * 1.5 should be a safe estimate). If 'mark_end' is true than an
  100. extra set of 4 bytes is added to the end of the stream if the input is a
  101. multiple of 3 bytes. These bytes are invalid chars and therefore they will
  102. stop the decoder thus enabling the caller to decode a stream without
  103. actually knowing how much data to expect (if the input is not a multiple of
  104. 3 bytes then the extra padding needed to complete the encode 4 bytes will
  105. stop the decoding anyway). */
  106. unsigned long kwsysBase64_Encode(const unsigned char *input,
  107. unsigned long length,
  108. unsigned char *output,
  109. int mark_end)
  110. {
  111. const unsigned char *ptr = input;
  112. const unsigned char *end = input + length;
  113. unsigned char *optr = output;
  114. /* Encode complete triplet */
  115. while ((end - ptr) >= 3)
  116. {
  117. kwsysBase64_Encode3(ptr, optr);
  118. ptr += 3;
  119. optr += 4;
  120. }
  121. /* Encodes a 2-byte ending into 3 bytes and 1 pad byte and writes. */
  122. if (end - ptr == 2)
  123. {
  124. kwsysBase64_Encode2(ptr, optr);
  125. optr += 4;
  126. }
  127. /* Encodes a 1-byte ending into 2 bytes and 2 pad bytes */
  128. else if (end - ptr == 1)
  129. {
  130. kwsysBase64_Encode1(ptr, optr);
  131. optr += 4;
  132. }
  133. /* Do we need to mark the end */
  134. else if (mark_end)
  135. {
  136. optr[0] = optr[1] = optr[2] = optr[3] = '=';
  137. optr += 4;
  138. }
  139. return (unsigned long)(optr - output);
  140. }
  141. /*--------------------------------------------------------------------------*/
  142. /* Decode 4 bytes into a 3 byte string. */
  143. int kwsysBase64_Decode3(const unsigned char *src, unsigned char *dest)
  144. {
  145. unsigned char d0, d1, d2, d3;
  146. d0 = kwsysBase64DecodeChar(src[0]);
  147. d1 = kwsysBase64DecodeChar(src[1]);
  148. d2 = kwsysBase64DecodeChar(src[2]);
  149. d3 = kwsysBase64DecodeChar(src[3]);
  150. /* Make sure all characters were valid */
  151. if (d0 == 0xFF || d1 == 0xFF || d2 == 0xFF || d3 == 0xFF)
  152. {
  153. return 0;
  154. }
  155. /* Decode the 3 bytes */
  156. dest[0] = (unsigned char)(((d0 << 2) & 0xFC) | ((d1 >> 4) & 0x03));
  157. dest[1] = (unsigned char)(((d1 << 4) & 0xF0) | ((d2 >> 2) & 0x0F));
  158. dest[2] = (unsigned char)(((d2 << 6) & 0xC0) | ((d3 >> 0) & 0x3F));
  159. /* Return the number of bytes actually decoded */
  160. if (src[2] == '=')
  161. {
  162. return 1;
  163. }
  164. if (src[3] == '=')
  165. {
  166. return 2;
  167. }
  168. return 3;
  169. }
  170. /*--------------------------------------------------------------------------*/
  171. /* Decode bytes from the input buffer and store the decoded stream
  172. into the output buffer until 'length' bytes have been decoded. Return the
  173. real length of the decoded stream (which should be equal to 'length'). Note
  174. that the output buffer must be allocated by the caller. If
  175. 'max_input_length' is not null, then it specifies the number of encoded
  176. bytes that should be at most read from the input buffer. In that case the
  177. 'length' parameter is ignored. This enables the caller to decode a stream
  178. without actually knowing how much decoded data to expect (of course, the
  179. buffer must be large enough). */
  180. unsigned long kwsysBase64_Decode(const unsigned char *input,
  181. unsigned long length,
  182. unsigned char *output,
  183. unsigned long max_input_length)
  184. {
  185. const unsigned char *ptr = input;
  186. unsigned char *optr = output;
  187. /* Decode complete triplet */
  188. if (max_input_length)
  189. {
  190. const unsigned char *end = input + max_input_length;
  191. while (ptr < end)
  192. {
  193. int len = kwsysBase64_Decode3(ptr, optr);
  194. optr += len;
  195. if(len < 3)
  196. {
  197. return (unsigned long)(optr - output);
  198. }
  199. ptr += 4;
  200. }
  201. }
  202. else
  203. {
  204. unsigned char *oend = output + length;
  205. while ((oend - optr) >= 3)
  206. {
  207. int len = kwsysBase64_Decode3(ptr, optr);
  208. optr += len;
  209. if(len < 3)
  210. {
  211. return (unsigned long)(optr - output);
  212. }
  213. ptr += 4;
  214. }
  215. /* Decode the last triplet */
  216. if (oend - optr == 2)
  217. {
  218. unsigned char temp[3];
  219. int len = kwsysBase64_Decode3(ptr, temp);
  220. optr[0] = temp[0];
  221. optr[1] = temp[1];
  222. optr += (len > 2 ? 2 : len);
  223. }
  224. else if (oend - optr == 1)
  225. {
  226. unsigned char temp[3];
  227. int len = kwsysBase64_Decode3(ptr, temp);
  228. optr[0] = temp[0];
  229. optr += (len > 2 ? 2 : len);
  230. }
  231. }
  232. return (unsigned long)(optr - output);
  233. }