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