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