base64.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*
  2. * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
  3. *
  4. * @APPLE_LICENSE_HEADER_START@
  5. *
  6. * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
  7. *
  8. * This file contains Original Code and/or Modifications of Original Code
  9. * as defined in and that are subject to the Apple Public Source License
  10. * Version 2.0 (the 'License'). You may not use this file except in
  11. * compliance with the License. Please obtain a copy of the License at
  12. * http://www.opensource.apple.com/apsl/ and read it before using this
  13. * file.
  14. *
  15. * The Original Code and all software distributed under the License are
  16. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  17. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  18. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  20. * Please see the License for the specific language governing rights and
  21. * limitations under the License.
  22. *
  23. * @APPLE_LICENSE_HEADER_END@
  24. */
  25. /* ====================================================================
  26. * Copyright (c) 1995-1999 The Apache Group. All rights reserved.
  27. *
  28. * Redistribution and use in source and binary forms, with or without
  29. * modification, are permitted provided that the following conditions
  30. * are met:
  31. *
  32. * 1. Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * 2. Redistributions in binary form must reproduce the above copyright
  36. * notice, this list of conditions and the following disclaimer in
  37. * the documentation and/or other materials provided with the
  38. * distribution.
  39. *
  40. * 3. All advertising materials mentioning features or use of this
  41. * software must display the following acknowledgment:
  42. * "This product includes software developed by the Apache Group
  43. * for use in the Apache HTTP server project (http://www.apache.org/)."
  44. *
  45. * 4. The names "Apache Server" and "Apache Group" must not be used to
  46. * endorse or promote products derived from this software without
  47. * prior written permission. For written permission, please contact
  48. * apache@apache.org.
  49. *
  50. * 5. Products derived from this software may not be called "Apache"
  51. * nor may "Apache" appear in their names without prior written
  52. * permission of the Apache Group.
  53. *
  54. * 6. Redistributions of any form whatsoever must retain the following
  55. * acknowledgment:
  56. * "This product includes software developed by the Apache Group
  57. * for use in the Apache HTTP server project (http://www.apache.org/)."
  58. *
  59. * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY
  60. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  61. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  62. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR
  63. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  64. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  65. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  66. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  67. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  68. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  69. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  70. * OF THE POSSIBILITY OF SUCH DAMAGE.
  71. * ====================================================================
  72. *
  73. * This software consists of voluntary contributions made by many
  74. * individuals on behalf of the Apache Group and was originally based
  75. * on public domain software written at the National Center for
  76. * Supercomputing Applications, University of Illinois, Urbana-Champaign.
  77. * For more information on the Apache Group and the Apache HTTP server
  78. * project, please see <http://www.apache.org/>.
  79. *
  80. */
  81. /* Base64 encoder/decoder. Originally Apache file ap_base64.c
  82. */
  83. #include <string.h>
  84. #include "base64.h"
  85. /* aaaack but it's fast and const should make it shared text page. */
  86. static const unsigned char pr2six[256] =
  87. {
  88. /* ASCII table */
  89. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  90. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  91. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63,
  92. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  93. 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  94. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  95. 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  96. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  97. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  98. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  99. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  100. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  101. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  102. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  103. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  104. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  105. };
  106. int Base64decode_len(const char *bufcoded)
  107. {
  108. int nbytesdecoded;
  109. register const unsigned char *bufin;
  110. register int nprbytes;
  111. bufin = (const unsigned char *) bufcoded;
  112. while (pr2six[*(bufin++)] <= 63);
  113. nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
  114. nbytesdecoded = ((nprbytes + 3) / 4) * 3;
  115. return nbytesdecoded + 1;
  116. }
  117. int Base64decode(char *bufplain, const char *bufcoded)
  118. {
  119. int nbytesdecoded;
  120. register const unsigned char *bufin;
  121. register unsigned char *bufout;
  122. register int nprbytes;
  123. bufin = (const unsigned char *) bufcoded;
  124. while (pr2six[*(bufin++)] <= 63);
  125. nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
  126. nbytesdecoded = ((nprbytes + 3) / 4) * 3;
  127. bufout = (unsigned char *) bufplain;
  128. bufin = (const unsigned char *) bufcoded;
  129. while (nprbytes > 4) {
  130. *(bufout++) =
  131. (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  132. *(bufout++) =
  133. (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  134. *(bufout++) =
  135. (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  136. bufin += 4;
  137. nprbytes -= 4;
  138. }
  139. /* Note: (nprbytes == 1) would be an error, so just ingore that case */
  140. if (nprbytes > 1) {
  141. *(bufout++) =
  142. (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  143. }
  144. if (nprbytes > 2) {
  145. *(bufout++) =
  146. (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  147. }
  148. if (nprbytes > 3) {
  149. *(bufout++) =
  150. (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  151. }
  152. *(bufout++) = '\0';
  153. nbytesdecoded -= (4 - nprbytes) & 3;
  154. return nbytesdecoded;
  155. }
  156. static const char basis_64[] =
  157. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  158. int Base64encode_len(int len)
  159. {
  160. return ((len + 2) / 3 * 4) + 1;
  161. }
  162. int Base64encode(char *encoded, const char *string, int len)
  163. {
  164. int i;
  165. char *p;
  166. p = encoded;
  167. for (i = 0; i < len - 2; i += 3) {
  168. *p++ = basis_64[(string[i] >> 2) & 0x3F];
  169. *p++ = basis_64[((string[i] & 0x3) << 4) |
  170. ((int) (string[i + 1] & 0xF0) >> 4)];
  171. *p++ = basis_64[((string[i + 1] & 0xF) << 2) |
  172. ((int) (string[i + 2] & 0xC0) >> 6)];
  173. *p++ = basis_64[string[i + 2] & 0x3F];
  174. }
  175. if (i < len) {
  176. *p++ = basis_64[(string[i] >> 2) & 0x3F];
  177. if (i == (len - 1)) {
  178. *p++ = basis_64[((string[i] & 0x3) << 4)];
  179. *p++ = '=';
  180. }
  181. else {
  182. *p++ = basis_64[((string[i] & 0x3) << 4) |
  183. ((int) (string[i + 1] & 0xF0) >> 4)];
  184. *p++ = basis_64[((string[i + 1] & 0xF) << 2)];
  185. }
  186. *p++ = '=';
  187. }
  188. *p++ = '\0';
  189. return p - encoded;
  190. }