b64.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "b64.h"
  85. /* aaaack but it's fast and const should make it shared text page. */
  86. /* ASCII table */
  87. static const unsigned char pr2six[256] = {
  88. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  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, 62, 64, 64, 64, 63,
  91. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  92. 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  93. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  94. 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  95. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  96. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 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. };
  105. int Base64decode_len(const char *bufcoded) {
  106. int nbytesdecoded;
  107. register const unsigned char *bufin;
  108. register int nprbytes;
  109. bufin = (const unsigned char *) bufcoded;
  110. while (pr2six[*(bufin++)] <= 63);
  111. nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
  112. nbytesdecoded = ((nprbytes + 3) / 4) * 3;
  113. return nbytesdecoded + 1;
  114. }
  115. int Base64decode(char *bufplain, const char *bufcoded) {
  116. int nbytesdecoded;
  117. register const unsigned char *bufin;
  118. register unsigned char *bufout;
  119. register int nprbytes;
  120. bufin = (const unsigned char *) bufcoded;
  121. while (pr2six[*(bufin++)] <= 63);
  122. nprbytes = (bufin - (const unsigned char *) bufcoded) - 1;
  123. nbytesdecoded = ((nprbytes + 3) / 4) * 3;
  124. bufout = (unsigned char *) bufplain;
  125. bufin = (const unsigned char *) bufcoded;
  126. while (nprbytes > 4) {
  127. *(bufout++) = (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  128. *(bufout++) = (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  129. *(bufout++) = (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  130. bufin += 4;
  131. nprbytes -= 4;
  132. }
  133. /* Note: (nprbytes == 1) would be an error, so just ingore that case */
  134. if (nprbytes > 1) {
  135. *(bufout++) = (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4);
  136. }
  137. if (nprbytes > 2) {
  138. *(bufout++) = (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2);
  139. }
  140. if (nprbytes > 3) {
  141. *(bufout++) = (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]);
  142. }
  143. *(bufout++) = '\0';
  144. nbytesdecoded -= (4 - nprbytes) & 3;
  145. return nbytesdecoded;
  146. }
  147. static const char basis_64[] =
  148. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  149. int Base64encode_len(int len) {
  150. return ((len + 2) / 3 * 4) + 1;
  151. }
  152. int Base64encode(char *encoded, const char *string, int len) {
  153. int i;
  154. char *p;
  155. p = encoded;
  156. for (i = 0; i < len - 2; i += 3) {
  157. *p++ = basis_64[(string[i] >> 2) & 0x3F];
  158. *p++ = basis_64[((string[i] & 0x3) << 4) |
  159. ((int) (string[i + 1] & 0xF0) >> 4)];
  160. *p++ = basis_64[((string[i + 1] & 0xF) << 2) |
  161. ((int) (string[i + 2] & 0xC0) >> 6)];
  162. *p++ = basis_64[string[i + 2] & 0x3F];
  163. }
  164. if (i < len) {
  165. *p++ = basis_64[(string[i] >> 2) & 0x3F];
  166. if (i == (len - 1)) {
  167. *p++ = basis_64[((string[i] & 0x3) << 4)];
  168. *p++ = '=';
  169. }
  170. else {
  171. *p++ = basis_64[((string[i] & 0x3) << 4) |
  172. ((int) (string[i + 1] & 0xF0) >> 4)];
  173. *p++ = basis_64[((string[i + 1] & 0xF) << 2)];
  174. }
  175. *p++ = '=';
  176. }
  177. *p++ = '\0';
  178. return p - encoded;
  179. }