xmlrpc_base64.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright (C) 2001 by First Peer, Inc. All rights reserved.
  2. **
  3. ** Redistribution and use in source and binary forms, with or without
  4. ** modification, are permitted provided that the following conditions
  5. ** are met:
  6. ** 1. Redistributions of source code must retain the above copyright
  7. ** notice, this list of conditions and the following disclaimer.
  8. ** 2. Redistributions in binary form must reproduce the above copyright
  9. ** notice, this list of conditions and the following disclaimer in the
  10. ** documentation and/or other materials provided with the distribution.
  11. ** 3. The name of the author may not be used to endorse or promote products
  12. ** derived from this software without specific prior written permission.
  13. **
  14. ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ** ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. ** SUCH DAMAGE.
  25. **
  26. ** There is more copyright information in the bottom half of this file.
  27. ** Please see it for more details. */
  28. /*=========================================================================
  29. ** XML-RPC Base64 Utilities
  30. **=========================================================================
  31. ** This code was swiped from Jack Jansen's code in Python 1.5.2 and
  32. ** modified to work with our data types.
  33. */
  34. #include "xmlrpc_config.h"
  35. #include "xmlrpc.h"
  36. #define CRLF "\015\012"
  37. #define CR '\015'
  38. #define LF '\012'
  39. /***********************************************************
  40. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  41. Amsterdam, The Netherlands.
  42. All Rights Reserved
  43. Permission to use, copy, modify, and distribute this software and its
  44. documentation for any purpose and without fee is hereby granted,
  45. provided that the above copyright notice appear in all copies and that
  46. both that copyright notice and this permission notice appear in
  47. supporting documentation, and that the names of Stichting Mathematisch
  48. Centrum or CWI or Corporation for National Research Initiatives or
  49. CNRI not be used in advertising or publicity pertaining to
  50. distribution of the software without specific, written prior
  51. permission.
  52. While CWI is the initial source for this software, a modified version
  53. is made available by the Corporation for National Research Initiatives
  54. (CNRI) at the Internet address ftp://ftp.python.org.
  55. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  56. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  57. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  58. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  59. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  60. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  61. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  62. PERFORMANCE OF THIS SOFTWARE.
  63. ******************************************************************/
  64. static char table_a2b_base64[] = {
  65. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  66. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  67. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  68. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, /* Note PAD->0 */
  69. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  70. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  71. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  72. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  73. };
  74. #define BASE64_PAD '='
  75. #define BASE64_MAXBIN 57 /* Max binary chunk size (76 char line) */
  76. #define BASE64_LINE_SZ 128 /* Buffer size for a single line. */
  77. static unsigned char table_b2a_base64[] =
  78. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  79. static xmlrpc_mem_block *
  80. xmlrpc_base64_encode_internal (xmlrpc_env *env,
  81. unsigned char *bin_data,
  82. size_t bin_len,
  83. int want_newlines)
  84. {
  85. size_t chunk_start, chunk_left;
  86. unsigned char *ascii_data;
  87. int leftbits;
  88. unsigned char this_ch;
  89. unsigned int leftchar;
  90. xmlrpc_mem_block *output;
  91. unsigned char line_buffer[BASE64_LINE_SZ];
  92. /* Create a block to hold our lines when we finish them. */
  93. output = xmlrpc_mem_block_new(env, 0);
  94. XMLRPC_FAIL_IF_FAULT(env);
  95. /* Deal with empty data blocks gracefully. Yuck. */
  96. if (bin_len == 0) {
  97. if (want_newlines)
  98. XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, output, CRLF, 2);
  99. goto cleanup;
  100. }
  101. /* Process our binary data in line-sized chunks. */
  102. for (chunk_start=0; chunk_start < bin_len; chunk_start += BASE64_MAXBIN) {
  103. /* Set up our per-line state. */
  104. ascii_data = &line_buffer[0];
  105. chunk_left = bin_len - chunk_start;
  106. if (chunk_left > BASE64_MAXBIN)
  107. chunk_left = BASE64_MAXBIN;
  108. leftbits = 0;
  109. leftchar = 0;
  110. for(; chunk_left > 0; chunk_left--, bin_data++) {
  111. /* Shift the data into our buffer */
  112. leftchar = (leftchar << 8) | *bin_data;
  113. leftbits += 8;
  114. /* See if there are 6-bit groups ready */
  115. while (leftbits >= 6) {
  116. this_ch = (leftchar >> (leftbits-6)) & 0x3f;
  117. leftbits -= 6;
  118. *ascii_data++ = table_b2a_base64[this_ch];
  119. }
  120. }
  121. if (leftbits == 2) {
  122. *ascii_data++ = table_b2a_base64[(leftchar&3) << 4];
  123. *ascii_data++ = BASE64_PAD;
  124. *ascii_data++ = BASE64_PAD;
  125. } else if (leftbits == 4) {
  126. *ascii_data++ = table_b2a_base64[(leftchar&0xf) << 2];
  127. *ascii_data++ = BASE64_PAD;
  128. }
  129. /* Append a courtesy CRLF. */
  130. if (want_newlines) {
  131. *ascii_data++ = CR;
  132. *ascii_data++ = LF;
  133. }
  134. /* Save our line. */
  135. XMLRPC_TYPED_MEM_BLOCK_APPEND(char, env, output, line_buffer,
  136. ascii_data - &line_buffer[0]);
  137. XMLRPC_FAIL_IF_FAULT(env);
  138. }
  139. cleanup:
  140. if (env->fault_occurred) {
  141. if (output)
  142. xmlrpc_mem_block_free(output);
  143. return NULL;
  144. }
  145. return output;
  146. }
  147. xmlrpc_mem_block *
  148. xmlrpc_base64_encode (xmlrpc_env *env, unsigned char *bin_data, size_t bin_len)
  149. {
  150. return xmlrpc_base64_encode_internal(env, bin_data, bin_len, 1);
  151. }
  152. xmlrpc_mem_block *
  153. xmlrpc_base64_encode_without_newlines (xmlrpc_env *env,
  154. unsigned char *bin_data,
  155. size_t bin_len)
  156. {
  157. return xmlrpc_base64_encode_internal(env, bin_data, bin_len, 0);
  158. }
  159. xmlrpc_mem_block *
  160. xmlrpc_base64_decode (xmlrpc_env *env,
  161. char *ascii_data,
  162. size_t ascii_len)
  163. {
  164. unsigned char *bin_data;
  165. int leftbits;
  166. unsigned char this_ch;
  167. unsigned int leftchar;
  168. size_t npad;
  169. size_t bin_len, buffer_size;
  170. xmlrpc_mem_block *output;
  171. /* Create a block to hold our chunks when we finish them.
  172. ** We overestimate the size now, and fix it later. */
  173. buffer_size = ((ascii_len+3)/4)*3;
  174. output = xmlrpc_mem_block_new(env, buffer_size);
  175. XMLRPC_FAIL_IF_FAULT(env);
  176. /* Set up our decoder state. */
  177. leftbits = 0;
  178. leftchar = 0;
  179. npad = 0;
  180. bin_data = XMLRPC_TYPED_MEM_BLOCK_CONTENTS(unsigned char, output);
  181. bin_len = 0;
  182. for( ; ascii_len > 0 ; ascii_len--, ascii_data++ ) {
  183. /* Skip some punctuation. */
  184. this_ch = (*ascii_data & 0x7f);
  185. if ( this_ch == '\r' || this_ch == '\n' || this_ch == ' ' )
  186. continue;
  187. if ( this_ch == BASE64_PAD )
  188. npad++;
  189. this_ch = table_a2b_base64[(*ascii_data) & 0x7f];
  190. /* XXX - We just throw away invalid characters. Is this right? */
  191. if ( this_ch == (unsigned char) -1 ) continue;
  192. /* Shift it in on the low end, and see if there's
  193. ** a byte ready for output. */
  194. leftchar = (leftchar << 6) | (this_ch);
  195. leftbits += 6;
  196. if ( leftbits >= 8 ) {
  197. leftbits -= 8;
  198. XMLRPC_ASSERT(bin_len < buffer_size);
  199. *bin_data++ = (leftchar >> leftbits) & 0xFF;
  200. leftchar &= ((1 << leftbits) - 1);
  201. bin_len++;
  202. }
  203. }
  204. /* Check that no bits are left. */
  205. if ( leftbits )
  206. XMLRPC_FAIL(env, XMLRPC_PARSE_ERROR, "Incorrect Base64 padding");
  207. /* Check to make sure we have a sane amount of padding. */
  208. if (npad > bin_len || npad > 2)
  209. XMLRPC_FAIL(env, XMLRPC_PARSE_ERROR, "Malformed Base64 data");
  210. /* Remove any padding and set the correct size. */
  211. bin_len -= npad;
  212. XMLRPC_TYPED_MEM_BLOCK_RESIZE(char, env, output, bin_len);
  213. XMLRPC_ASSERT(!env->fault_occurred);
  214. cleanup:
  215. if (env->fault_occurred) {
  216. if (output)
  217. xmlrpc_mem_block_free(output);
  218. return NULL;
  219. }
  220. return output;
  221. }