xref: /titanic_44/usr/src/cmd/ssh/libopenbsd-compat/common/base64.c (revision 442d23f49355a5d0694c758975be57af39f91a61)
17c478bd9Sstevel@tonic-gate /*	$OpenBSD: base64.c,v 1.4 2002/01/02 23:00:10 deraadt Exp $	*/
27c478bd9Sstevel@tonic-gate 
37c478bd9Sstevel@tonic-gate /*
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1996 by Internet Software Consortium.
57c478bd9Sstevel@tonic-gate  *
67c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
77c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
87c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
97c478bd9Sstevel@tonic-gate  *
107c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
117c478bd9Sstevel@tonic-gate  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
127c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
137c478bd9Sstevel@tonic-gate  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
147c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
157c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
167c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
177c478bd9Sstevel@tonic-gate  * SOFTWARE.
187c478bd9Sstevel@tonic-gate  */
197c478bd9Sstevel@tonic-gate 
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1995 by International Business Machines, Inc.
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * International Business Machines, Inc. (hereinafter called IBM) grants
247c478bd9Sstevel@tonic-gate  * permission under its copyrights to use, copy, modify, and distribute this
257c478bd9Sstevel@tonic-gate  * Software with or without fee, provided that the above copyright notice and
267c478bd9Sstevel@tonic-gate  * all paragraphs of this notice appear in all copies, and that the name of IBM
277c478bd9Sstevel@tonic-gate  * not be used in connection with the marketing of any product incorporating
287c478bd9Sstevel@tonic-gate  * the Software or modifications thereof, without specific, written prior
297c478bd9Sstevel@tonic-gate  * permission.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * To the extent it has a right to do so, IBM grants an immunity from suit
327c478bd9Sstevel@tonic-gate  * under its patents, if any, for the use, sale or manufacture of products to
337c478bd9Sstevel@tonic-gate  * the extent that such products are used for performing Domain Name System
347c478bd9Sstevel@tonic-gate  * dynamic updates in TCP/IP networks by means of the Software.  No immunity is
357c478bd9Sstevel@tonic-gate  * granted for any product per se or for any other function of any product.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS", AND IBM DISCLAIMS ALL WARRANTIES,
387c478bd9Sstevel@tonic-gate  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
397c478bd9Sstevel@tonic-gate  * PARTICULAR PURPOSE.  IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL,
407c478bd9Sstevel@tonic-gate  * DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER ARISING
417c478bd9Sstevel@tonic-gate  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, EVEN
427c478bd9Sstevel@tonic-gate  * IF IBM IS APPRISED OF THE POSSIBILITY OF SUCH DAMAGES.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate 
45*442d23f4Sjp161948 #pragma ident	"%Z%%M%	%I%	%E% SMI"
46*442d23f4Sjp161948 
477c478bd9Sstevel@tonic-gate #include "includes.h"
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate #if !defined(HAVE_B64_NTOP) && !defined(HAVE___B64_NTOP)
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <sys/types.h>
527c478bd9Sstevel@tonic-gate #include <sys/param.h>
537c478bd9Sstevel@tonic-gate #include <sys/socket.h>
547c478bd9Sstevel@tonic-gate #include <netinet/in.h>
557c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #include <ctype.h>
587c478bd9Sstevel@tonic-gate #include <stdio.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #include <stdlib.h>
617c478bd9Sstevel@tonic-gate #include <string.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include "base64.h"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /* XXX abort illegal in library */
667c478bd9Sstevel@tonic-gate #define Assert(Cond) if (!(Cond)) abort()
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate static const char Base64[] =
697c478bd9Sstevel@tonic-gate 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
707c478bd9Sstevel@tonic-gate static const char Pad64 = '=';
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
737c478bd9Sstevel@tonic-gate    The following encoding technique is taken from RFC 1521 by Borenstein
747c478bd9Sstevel@tonic-gate    and Freed.  It is reproduced here in a slightly edited form for
757c478bd9Sstevel@tonic-gate    convenience.
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate    A 65-character subset of US-ASCII is used, enabling 6 bits to be
787c478bd9Sstevel@tonic-gate    represented per printable character. (The extra 65th character, "=",
797c478bd9Sstevel@tonic-gate    is used to signify a special processing function.)
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate    The encoding process represents 24-bit groups of input bits as output
827c478bd9Sstevel@tonic-gate    strings of 4 encoded characters. Proceeding from left to right, a
837c478bd9Sstevel@tonic-gate    24-bit input group is formed by concatenating 3 8-bit input groups.
847c478bd9Sstevel@tonic-gate    These 24 bits are then treated as 4 concatenated 6-bit groups, each
857c478bd9Sstevel@tonic-gate    of which is translated into a single digit in the base64 alphabet.
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate    Each 6-bit group is used as an index into an array of 64 printable
887c478bd9Sstevel@tonic-gate    characters. The character referenced by the index is placed in the
897c478bd9Sstevel@tonic-gate    output string.
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate                          Table 1: The Base64 Alphabet
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate       Value Encoding  Value Encoding  Value Encoding  Value Encoding
947c478bd9Sstevel@tonic-gate           0 A            17 R            34 i            51 z
957c478bd9Sstevel@tonic-gate           1 B            18 S            35 j            52 0
967c478bd9Sstevel@tonic-gate           2 C            19 T            36 k            53 1
977c478bd9Sstevel@tonic-gate           3 D            20 U            37 l            54 2
987c478bd9Sstevel@tonic-gate           4 E            21 V            38 m            55 3
997c478bd9Sstevel@tonic-gate           5 F            22 W            39 n            56 4
1007c478bd9Sstevel@tonic-gate           6 G            23 X            40 o            57 5
1017c478bd9Sstevel@tonic-gate           7 H            24 Y            41 p            58 6
1027c478bd9Sstevel@tonic-gate           8 I            25 Z            42 q            59 7
1037c478bd9Sstevel@tonic-gate           9 J            26 a            43 r            60 8
1047c478bd9Sstevel@tonic-gate          10 K            27 b            44 s            61 9
1057c478bd9Sstevel@tonic-gate          11 L            28 c            45 t            62 +
1067c478bd9Sstevel@tonic-gate          12 M            29 d            46 u            63 /
1077c478bd9Sstevel@tonic-gate          13 N            30 e            47 v
1087c478bd9Sstevel@tonic-gate          14 O            31 f            48 w         (pad) =
1097c478bd9Sstevel@tonic-gate          15 P            32 g            49 x
1107c478bd9Sstevel@tonic-gate          16 Q            33 h            50 y
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate    Special processing is performed if fewer than 24 bits are available
1137c478bd9Sstevel@tonic-gate    at the end of the data being encoded.  A full encoding quantum is
1147c478bd9Sstevel@tonic-gate    always completed at the end of a quantity.  When fewer than 24 input
1157c478bd9Sstevel@tonic-gate    bits are available in an input group, zero bits are added (on the
1167c478bd9Sstevel@tonic-gate    right) to form an integral number of 6-bit groups.  Padding at the
1177c478bd9Sstevel@tonic-gate    end of the data is performed using the '=' character.
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate    Since all base64 input is an integral number of octets, only the
1207c478bd9Sstevel@tonic-gate          -------------------------------------------------
1217c478bd9Sstevel@tonic-gate    following cases can arise:
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate        (1) the final quantum of encoding input is an integral
1247c478bd9Sstevel@tonic-gate            multiple of 24 bits; here, the final unit of encoded
1257c478bd9Sstevel@tonic-gate 	   output will be an integral multiple of 4 characters
1267c478bd9Sstevel@tonic-gate 	   with no "=" padding,
1277c478bd9Sstevel@tonic-gate        (2) the final quantum of encoding input is exactly 8 bits;
1287c478bd9Sstevel@tonic-gate            here, the final unit of encoded output will be two
1297c478bd9Sstevel@tonic-gate 	   characters followed by two "=" padding characters, or
1307c478bd9Sstevel@tonic-gate        (3) the final quantum of encoding input is exactly 16 bits;
1317c478bd9Sstevel@tonic-gate            here, the final unit of encoded output will be three
1327c478bd9Sstevel@tonic-gate 	   characters followed by one "=" padding character.
1337c478bd9Sstevel@tonic-gate    */
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate int
b64_ntop(u_char const * src,size_t srclength,char * target,size_t targsize)1367c478bd9Sstevel@tonic-gate b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	size_t datalength = 0;
1397c478bd9Sstevel@tonic-gate 	u_char input[3];
1407c478bd9Sstevel@tonic-gate 	u_char output[4];
1417c478bd9Sstevel@tonic-gate 	int i;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	while (2 < srclength) {
1447c478bd9Sstevel@tonic-gate 		input[0] = *src++;
1457c478bd9Sstevel@tonic-gate 		input[1] = *src++;
1467c478bd9Sstevel@tonic-gate 		input[2] = *src++;
1477c478bd9Sstevel@tonic-gate 		srclength -= 3;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 		output[0] = input[0] >> 2;
1507c478bd9Sstevel@tonic-gate 		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
1517c478bd9Sstevel@tonic-gate 		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
1527c478bd9Sstevel@tonic-gate 		output[3] = input[2] & 0x3f;
1537c478bd9Sstevel@tonic-gate 		Assert(output[0] < 64);
1547c478bd9Sstevel@tonic-gate 		Assert(output[1] < 64);
1557c478bd9Sstevel@tonic-gate 		Assert(output[2] < 64);
1567c478bd9Sstevel@tonic-gate 		Assert(output[3] < 64);
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		if (datalength + 4 > targsize)
1597c478bd9Sstevel@tonic-gate 			return (-1);
1607c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[0]];
1617c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[1]];
1627c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[2]];
1637c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[3]];
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* Now we worry about padding. */
1677c478bd9Sstevel@tonic-gate 	if (0 != srclength) {
1687c478bd9Sstevel@tonic-gate 		/* Get what's left. */
1697c478bd9Sstevel@tonic-gate 		input[0] = input[1] = input[2] = '\0';
1707c478bd9Sstevel@tonic-gate 		for (i = 0; i < srclength; i++)
1717c478bd9Sstevel@tonic-gate 			input[i] = *src++;
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 		output[0] = input[0] >> 2;
1747c478bd9Sstevel@tonic-gate 		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
1757c478bd9Sstevel@tonic-gate 		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
1767c478bd9Sstevel@tonic-gate 		Assert(output[0] < 64);
1777c478bd9Sstevel@tonic-gate 		Assert(output[1] < 64);
1787c478bd9Sstevel@tonic-gate 		Assert(output[2] < 64);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 		if (datalength + 4 > targsize)
1817c478bd9Sstevel@tonic-gate 			return (-1);
1827c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[0]];
1837c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[1]];
1847c478bd9Sstevel@tonic-gate 		if (srclength == 1)
1857c478bd9Sstevel@tonic-gate 			target[datalength++] = Pad64;
1867c478bd9Sstevel@tonic-gate 		else
1877c478bd9Sstevel@tonic-gate 			target[datalength++] = Base64[output[2]];
1887c478bd9Sstevel@tonic-gate 		target[datalength++] = Pad64;
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 	if (datalength >= targsize)
1917c478bd9Sstevel@tonic-gate 		return (-1);
1927c478bd9Sstevel@tonic-gate 	target[datalength] = '\0';	/* Returned value doesn't count \0. */
1937c478bd9Sstevel@tonic-gate 	return (datalength);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /* skips all whitespace anywhere.
1977c478bd9Sstevel@tonic-gate    converts characters, four at a time, starting at (or after)
1987c478bd9Sstevel@tonic-gate    src from base - 64 numbers into three 8 bit bytes in the target area.
1997c478bd9Sstevel@tonic-gate    it returns the number of data bytes stored at the target, or -1 on error.
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate int
b64_pton(u_char const * src,u_char * target,size_t targsize)203*442d23f4Sjp161948 b64_pton(u_char const *src, u_char *target, size_t targsize)
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	int tarindex, state, ch;
2067c478bd9Sstevel@tonic-gate 	char *pos;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	state = 0;
2097c478bd9Sstevel@tonic-gate 	tarindex = 0;
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
2127c478bd9Sstevel@tonic-gate 		if (isspace(ch))	/* Skip whitespace anywhere. */
2137c478bd9Sstevel@tonic-gate 			continue;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		if (ch == Pad64)
2167c478bd9Sstevel@tonic-gate 			break;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 		pos = strchr(Base64, ch);
2197c478bd9Sstevel@tonic-gate 		if (pos == 0) 		/* A non-base64 character. */
2207c478bd9Sstevel@tonic-gate 			return (-1);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 		switch (state) {
2237c478bd9Sstevel@tonic-gate 		case 0:
2247c478bd9Sstevel@tonic-gate 			if (target) {
2257c478bd9Sstevel@tonic-gate 				if (tarindex >= targsize)
2267c478bd9Sstevel@tonic-gate 					return (-1);
2277c478bd9Sstevel@tonic-gate 				target[tarindex] = (pos - Base64) << 2;
2287c478bd9Sstevel@tonic-gate 			}
2297c478bd9Sstevel@tonic-gate 			state = 1;
2307c478bd9Sstevel@tonic-gate 			break;
2317c478bd9Sstevel@tonic-gate 		case 1:
2327c478bd9Sstevel@tonic-gate 			if (target) {
2337c478bd9Sstevel@tonic-gate 				if (tarindex + 1 >= targsize)
2347c478bd9Sstevel@tonic-gate 					return (-1);
2357c478bd9Sstevel@tonic-gate 				target[tarindex]   |=  (pos - Base64) >> 4;
2367c478bd9Sstevel@tonic-gate 				target[tarindex+1]  = ((pos - Base64) & 0x0f)
2377c478bd9Sstevel@tonic-gate 							<< 4 ;
2387c478bd9Sstevel@tonic-gate 			}
2397c478bd9Sstevel@tonic-gate 			tarindex++;
2407c478bd9Sstevel@tonic-gate 			state = 2;
2417c478bd9Sstevel@tonic-gate 			break;
2427c478bd9Sstevel@tonic-gate 		case 2:
2437c478bd9Sstevel@tonic-gate 			if (target) {
2447c478bd9Sstevel@tonic-gate 				if (tarindex + 1 >= targsize)
2457c478bd9Sstevel@tonic-gate 					return (-1);
2467c478bd9Sstevel@tonic-gate 				target[tarindex]   |=  (pos - Base64) >> 2;
2477c478bd9Sstevel@tonic-gate 				target[tarindex+1]  = ((pos - Base64) & 0x03)
2487c478bd9Sstevel@tonic-gate 							<< 6;
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 			tarindex++;
2517c478bd9Sstevel@tonic-gate 			state = 3;
2527c478bd9Sstevel@tonic-gate 			break;
2537c478bd9Sstevel@tonic-gate 		case 3:
2547c478bd9Sstevel@tonic-gate 			if (target) {
2557c478bd9Sstevel@tonic-gate 				if (tarindex >= targsize)
2567c478bd9Sstevel@tonic-gate 					return (-1);
2577c478bd9Sstevel@tonic-gate 				target[tarindex] |= (pos - Base64);
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 			tarindex++;
2607c478bd9Sstevel@tonic-gate 			state = 0;
2617c478bd9Sstevel@tonic-gate 			break;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * We are done decoding Base-64 chars.  Let's see if we ended
2677c478bd9Sstevel@tonic-gate 	 * on a byte boundary, and/or with erroneous trailing characters.
2687c478bd9Sstevel@tonic-gate 	 */
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	if (ch == Pad64) {		/* We got a pad char. */
2717c478bd9Sstevel@tonic-gate 		ch = *src++;		/* Skip it, get next. */
2727c478bd9Sstevel@tonic-gate 		switch (state) {
2737c478bd9Sstevel@tonic-gate 		case 0:		/* Invalid = in first position */
2747c478bd9Sstevel@tonic-gate 		case 1:		/* Invalid = in second position */
2757c478bd9Sstevel@tonic-gate 			return (-1);
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 		case 2:		/* Valid, means one byte of info */
2787c478bd9Sstevel@tonic-gate 			/* Skip any number of spaces. */
2797c478bd9Sstevel@tonic-gate 			for (; ch != '\0'; ch = *src++)
2807c478bd9Sstevel@tonic-gate 				if (!isspace(ch))
2817c478bd9Sstevel@tonic-gate 					break;
2827c478bd9Sstevel@tonic-gate 			/* Make sure there is another trailing = sign. */
2837c478bd9Sstevel@tonic-gate 			if (ch != Pad64)
2847c478bd9Sstevel@tonic-gate 				return (-1);
2857c478bd9Sstevel@tonic-gate 			ch = *src++;		/* Skip the = */
2867c478bd9Sstevel@tonic-gate 			/* Fall through to "single trailing =" case. */
2877c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 		case 3:		/* Valid, means two bytes of info */
2907c478bd9Sstevel@tonic-gate 			/*
2917c478bd9Sstevel@tonic-gate 			 * We know this char is an =.  Is there anything but
2927c478bd9Sstevel@tonic-gate 			 * whitespace after it?
2937c478bd9Sstevel@tonic-gate 			 */
2947c478bd9Sstevel@tonic-gate 			for (; ch != '\0'; ch = *src++)
2957c478bd9Sstevel@tonic-gate 				if (!isspace(ch))
2967c478bd9Sstevel@tonic-gate 					return (-1);
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 			/*
2997c478bd9Sstevel@tonic-gate 			 * Now make sure for cases 2 and 3 that the "extra"
3007c478bd9Sstevel@tonic-gate 			 * bits that slopped past the last full byte were
3017c478bd9Sstevel@tonic-gate 			 * zeros.  If we don't check them, they become a
3027c478bd9Sstevel@tonic-gate 			 * subliminal channel.
3037c478bd9Sstevel@tonic-gate 			 */
3047c478bd9Sstevel@tonic-gate 			if (target && target[tarindex] != 0)
3057c478bd9Sstevel@tonic-gate 				return (-1);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 	} else {
3087c478bd9Sstevel@tonic-gate 		/*
3097c478bd9Sstevel@tonic-gate 		 * We ended by seeing the end of the string.  Make sure we
3107c478bd9Sstevel@tonic-gate 		 * have no partial bytes lying around.
3117c478bd9Sstevel@tonic-gate 		 */
3127c478bd9Sstevel@tonic-gate 		if (state != 0)
3137c478bd9Sstevel@tonic-gate 			return (-1);
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	return (tarindex);
3177c478bd9Sstevel@tonic-gate }
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate #endif /* !defined(HAVE_B64_NTOP) && !defined(HAVE___B64_NTOP) */
320