2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A/*
2N/A * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
2N/A * Copyright (c) 1996-1999 by Internet Software Consortium.
2N/A *
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 *
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 */
2N/A
2N/A/*
2N/A * Portions Copyright (c) 1995 by International Business Machines, Inc.
2N/A *
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 * permission.
2N/A *
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 *
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 */
2N/A
2N/A#if !defined(LINT) && !defined(CODECENTER)
2N/Astatic const char rcsid[] = "$Id: base64.c,v 1.4 2005/04/27 04:56:34 sra Exp $";
2N/A#endif /* not lint */
2N/A
2N/A#include "port_before.h"
2N/A
2N/A#include <sys/types.h>
2N/A#include <sys/param.h>
2N/A#include <sys/socket.h>
2N/A
2N/A#include <netinet/in.h>
2N/A#include <arpa/inet.h>
2N/A#include <arpa/nameser.h>
2N/A
2N/A#include <ctype.h>
2N/A#include <resolv.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A
2N/A#include "port_after.h"
2N/A
2N/A#ifndef ORIGINAL_ISC_CODE
2N/A#pragma weak __b64_ntop = b64_ntop
2N/A#pragma weak __b64_pton = b64_pton
2N/A#endif /* ORIGINAL_ISC_CODE */
2N/A
2N/A#define Assert(Cond) if (!(Cond)) abort()
2N/A
2N/Astatic const char Base64[] =
2N/A "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2N/Astatic const char Pad64 = '=';
2N/A
2N/A/* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
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 convenience.
2N/A
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
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
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 output string.
2N/A
2N/A Table 1: The Base64 Alphabet
2N/A
2N/A Value Encoding Value Encoding Value Encoding Value Encoding
2N/A 0 A 17 R 34 i 51 z
2N/A 1 B 18 S 35 j 52 0
2N/A 2 C 19 T 36 k 53 1
2N/A 3 D 20 U 37 l 54 2
2N/A 4 E 21 V 38 m 55 3
2N/A 5 F 22 W 39 n 56 4
2N/A 6 G 23 X 40 o 57 5
2N/A 7 H 24 Y 41 p 58 6
2N/A 8 I 25 Z 42 q 59 7
2N/A 9 J 26 a 43 r 60 8
2N/A 10 K 27 b 44 s 61 9
2N/A 11 L 28 c 45 t 62 +
2N/A 12 M 29 d 46 u 63 /
2N/A 13 N 30 e 47 v
2N/A 14 O 31 f 48 w (pad) =
2N/A 15 P 32 g 49 x
2N/A 16 Q 33 h 50 y
2N/A
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
2N/A Since all base64 input is an integral number of octets, only the
2N/A -------------------------------------------------
2N/A following cases can arise:
2N/A
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 */
2N/A
2N/Aint
2N/Ab64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {
2N/A size_t datalength = 0;
2N/A u_char input[3];
2N/A u_char output[4];
2N/A size_t i;
2N/A
2N/A while (2U < srclength) {
2N/A input[0] = *src++;
2N/A input[1] = *src++;
2N/A input[2] = *src++;
2N/A srclength -= 3;
2N/A
2N/A output[0] = input[0] >> 2;
2N/A output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
2N/A output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
2N/A output[3] = input[2] & 0x3f;
2N/A Assert(output[0] < 64);
2N/A Assert(output[1] < 64);
2N/A Assert(output[2] < 64);
2N/A Assert(output[3] < 64);
2N/A
2N/A if (datalength + 4 > targsize)
2N/A return (-1);
2N/A target[datalength++] = Base64[output[0]];
2N/A target[datalength++] = Base64[output[1]];
2N/A target[datalength++] = Base64[output[2]];
2N/A target[datalength++] = Base64[output[3]];
2N/A }
2N/A
2N/A /* Now we worry about padding. */
2N/A if (0U != srclength) {
2N/A /* Get what's left. */
2N/A input[0] = input[1] = input[2] = '\0';
2N/A for (i = 0; i < srclength; i++)
2N/A input[i] = *src++;
2N/A
2N/A output[0] = input[0] >> 2;
2N/A output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
2N/A output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
2N/A Assert(output[0] < 64);
2N/A Assert(output[1] < 64);
2N/A Assert(output[2] < 64);
2N/A
2N/A if (datalength + 4 > targsize)
2N/A return (-1);
2N/A target[datalength++] = Base64[output[0]];
2N/A target[datalength++] = Base64[output[1]];
2N/A if (srclength == 1U)
2N/A target[datalength++] = Pad64;
2N/A else
2N/A target[datalength++] = Base64[output[2]];
2N/A target[datalength++] = Pad64;
2N/A }
2N/A if (datalength >= targsize)
2N/A return (-1);
2N/A target[datalength] = '\0'; /*%< Returned value doesn't count \\0. */
2N/A return (datalength);
2N/A}
2N/A
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 */
2N/A
2N/Aint
2N/Ab64_pton(src, target, targsize)
2N/A char const *src;
2N/A u_char *target;
2N/A size_t targsize;
2N/A{
2N/A int tarindex, state, ch;
2N/A char *pos;
2N/A
2N/A state = 0;
2N/A tarindex = 0;
2N/A
2N/A while ((ch = *src++) != '\0') {
2N/A if (isspace(ch)) /*%< Skip whitespace anywhere. */
2N/A continue;
2N/A
2N/A if (ch == Pad64)
2N/A break;
2N/A
2N/A pos = strchr(Base64, ch);
2N/A if (pos == 0) /*%< A non-base64 character. */
2N/A return (-1);
2N/A
2N/A switch (state) {
2N/A case 0:
2N/A if (target) {
2N/A if ((size_t)tarindex >= targsize)
2N/A return (-1);
2N/A target[tarindex] = (pos - Base64) << 2;
2N/A }
2N/A state = 1;
2N/A break;
2N/A case 1:
2N/A if (target) {
2N/A if ((size_t)tarindex + 1 >= targsize)
2N/A return (-1);
2N/A target[tarindex] |= (pos - Base64) >> 4;
2N/A target[tarindex+1] = ((pos - Base64) & 0x0f)
2N/A << 4 ;
2N/A }
2N/A tarindex++;
2N/A state = 2;
2N/A break;
2N/A case 2:
2N/A if (target) {
2N/A if ((size_t)tarindex + 1 >= targsize)
2N/A return (-1);
2N/A target[tarindex] |= (pos - Base64) >> 2;
2N/A target[tarindex+1] = ((pos - Base64) & 0x03)
2N/A << 6;
2N/A }
2N/A tarindex++;
2N/A state = 3;
2N/A break;
2N/A case 3:
2N/A if (target) {
2N/A if ((size_t)tarindex >= targsize)
2N/A return (-1);
2N/A target[tarindex] |= (pos - Base64);
2N/A }
2N/A tarindex++;
2N/A state = 0;
2N/A break;
2N/A default:
2N/A abort();
2N/A }
2N/A }
2N/A
2N/A /*
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 */
2N/A
2N/A if (ch == Pad64) { /*%< We got a pad char. */
2N/A ch = *src++; /*%< Skip it, get next. */
2N/A switch (state) {
2N/A case 0: /*%< Invalid = in first position */
2N/A case 1: /*%< Invalid = in second position */
2N/A return (-1);
2N/A
2N/A case 2: /*%< Valid, means one byte of info */
2N/A /* Skip any number of spaces. */
2N/A for ((void)NULL; ch != '\0'; ch = *src++)
2N/A if (!isspace(ch))
2N/A break;
2N/A /* Make sure there is another trailing = sign. */
2N/A if (ch != Pad64)
2N/A return (-1);
2N/A ch = *src++; /*%< Skip the = */
2N/A /* Fall through to "single trailing =" case. */
2N/A /* FALLTHROUGH */
2N/A
2N/A case 3: /*%< Valid, means two bytes of info */
2N/A /*
2N/A * We know this char is an =. Is there anything but
2N/A * whitespace after it?
2N/A */
2N/A for ((void)NULL; ch != '\0'; ch = *src++)
2N/A if (!isspace(ch))
2N/A return (-1);
2N/A
2N/A /*
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 */
2N/A if (target && target[tarindex] != 0)
2N/A return (-1);
2N/A }
2N/A } else {
2N/A /*
2N/A * We ended by seeing the end of the string. Make sure we
2N/A * have no partial bytes lying around.
2N/A */
2N/A if (state != 0)
2N/A return (-1);
2N/A }
2N/A
2N/A return (tarindex);
2N/A}
2N/A
2N/A/*! \file */