2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC") 2N/A * Copyright (c) 1996-1999 by Internet Software Consortium. 2N/A * Permission to use, copy, modify, and distribute this software for any 2N/A * purpose with or without fee is hereby granted, provided that the above 2N/A * copyright notice and this permission notice appear in all copies. 2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES 2N/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 2N/A * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR 2N/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 2N/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 2N/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT 2N/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 2N/A * Portions Copyright (c) 1995 by International Business Machines, Inc. 2N/A * International Business Machines, Inc. (hereinafter called IBM) grants 2N/A * permission under its copyrights to use, copy, modify, and distribute this 2N/A * Software with or without fee, provided that the above copyright notice and 2N/A * all paragraphs of this notice appear in all copies, and that the name of IBM 2N/A * not be used in connection with the marketing of any product incorporating 2N/A * the Software or modifications thereof, without specific, written prior 2N/A * To the extent it has a right to do so, IBM grants an immunity from suit 2N/A * under its patents, if any, for the use, sale or manufacture of products to 2N/A * the extent that such products are used for performing Domain Name System 2N/A * dynamic updates in TCP/IP networks by means of the Software. No immunity is 2N/A * granted for any product per se or for any other function of any product. 2N/A * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES, 2N/A * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 2N/A * PARTICULAR PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, 2N/A * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING 2N/A * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN 2N/A * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES. 2N/A#
endif /* not lint */ 2N/A#
endif /* ORIGINAL_ISC_CODE */ 2N/A "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2N/A The following encoding technique is taken from RFC1521 by Borenstein 2N/A and Freed. It is reproduced here in a slightly edited form for 2N/A A 65-character subset of US-ASCII is used, enabling 6 bits to be 2N/A represented per printable character. (The extra 65th character, "=", 2N/A is used to signify a special processing function.) 2N/A The encoding process represents 24-bit groups of input bits as output 2N/A strings of 4 encoded characters. Proceeding from left to right, a 2N/A 24-bit input group is formed by concatenating 3 8-bit input groups. 2N/A These 24 bits are then treated as 4 concatenated 6-bit groups, each 2N/A of which is translated into a single digit in the base64 alphabet. 2N/A Each 6-bit group is used as an index into an array of 64 printable 2N/A characters. The character referenced by the index is placed in the 2N/A Table 1: The Base64 Alphabet 2N/A Value Encoding Value Encoding Value Encoding Value Encoding 2N/A 14 O 31 f 48 w (pad) = 2N/A Special processing is performed if fewer than 24 bits are available 2N/A at the end of the data being encoded. A full encoding quantum is 2N/A always completed at the end of a quantity. When fewer than 24 input 2N/A bits are available in an input group, zero bits are added (on the 2N/A right) to form an integral number of 6-bit groups. Padding at the 2N/A end of the data is performed using the '=' character. 2N/A Since all base64 input is an integral number of octets, only the 2N/A ------------------------------------------------- 2N/A following cases can arise: 2N/A (1) the final quantum of encoding input is an integral 2N/A multiple of 24 bits; here, the final unit of encoded 2N/A output will be an integral multiple of 4 characters 2N/A with no "=" padding, 2N/A (2) the final quantum of encoding input is exactly 8 bits; 2N/A here, the final unit of encoded output will be two 2N/A characters followed by two "=" padding characters, or 2N/A (3) the final quantum of encoding input is exactly 16 bits; 2N/A here, the final unit of encoded output will be three 2N/A characters followed by one "=" padding character. 2N/A /* Now we worry about padding. */ 2N/A /* Get what's left. */ 2N/A/* skips all whitespace anywhere. 2N/A converts characters, four at a time, starting at (or after) 2N/A src from base - 64 numbers into three 8 bit bytes in the target area. 2N/A it returns the number of data bytes stored at the target, or -1 on error. 2N/A if (
pos == 0)
/*%< A non-base64 character. */ 2N/A * We are done decoding Base-64 chars. Let's see if we ended 2N/A * on a byte boundary, and/or with erroneous trailing characters. 2N/A case 0:
/*%< Invalid = in first position */ 2N/A case 1:
/*%< Invalid = in second position */ 2N/A case 2:
/*%< Valid, means one byte of info */ 2N/A /* Skip any number of spaces. */ 2N/A /* Make sure there is another trailing = sign. */ 2N/A /* Fall through to "single trailing =" case. */ 2N/A case 3:
/*%< Valid, means two bytes of info */ 2N/A * We know this char is an =. Is there anything but 2N/A * whitespace after it? 2N/A * Now make sure for cases 2 and 3 that the "extra" 2N/A * bits that slopped past the last full byte were 2N/A * zeros. If we don't check them, they become a 2N/A * subliminal channel. 2N/A * We ended by seeing the end of the string. Make sure we 2N/A * have no partial bytes lying around.