xref: /illumos-gate/usr/src/lib/libresolv2/common/isc/base64.c (revision 9525b14bcdeb5b5f6f95ab27c2f48f18bd2ec829)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
3*9525b14bSRao Shoaib  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6*9525b14bSRao Shoaib 
77c478bd9Sstevel@tonic-gate /*
8*9525b14bSRao 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  *
15*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
16*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
17*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
18*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21*9525b14bSRao 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 #if !defined(LINT) && !defined(CODECENTER)
50*9525b14bSRao Shoaib static const char rcsid[] = "$Id: base64.c,v 1.4 2005/04/27 04:56:34 sra Exp $";
517c478bd9Sstevel@tonic-gate #endif /* not lint */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include "port_before.h"
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate #include <sys/types.h>
567c478bd9Sstevel@tonic-gate #include <sys/param.h>
577c478bd9Sstevel@tonic-gate #include <sys/socket.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <netinet/in.h>
607c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
617c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <ctype.h>
647c478bd9Sstevel@tonic-gate #include <resolv.h>
657c478bd9Sstevel@tonic-gate #include <stdio.h>
667c478bd9Sstevel@tonic-gate #include <stdlib.h>
677c478bd9Sstevel@tonic-gate #include <string.h>
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #include "port_after.h"
707c478bd9Sstevel@tonic-gate 
71*9525b14bSRao Shoaib #ifndef	ORIGINAL_ISC_CODE
727c478bd9Sstevel@tonic-gate #pragma weak	__b64_ntop	=	b64_ntop
737c478bd9Sstevel@tonic-gate #pragma weak	__b64_pton	=	b64_pton
747c478bd9Sstevel@tonic-gate #endif	/* ORIGINAL_ISC_CODE */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define Assert(Cond) if (!(Cond)) abort()
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate static const char Base64[] =
797c478bd9Sstevel@tonic-gate 	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
807c478bd9Sstevel@tonic-gate static const char Pad64 = '=';
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /* (From RFC1521 and draft-ietf-dnssec-secext-03.txt)
837c478bd9Sstevel@tonic-gate    The following encoding technique is taken from RFC1521 by Borenstein
847c478bd9Sstevel@tonic-gate    and Freed.  It is reproduced here in a slightly edited form for
857c478bd9Sstevel@tonic-gate    convenience.
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate    A 65-character subset of US-ASCII is used, enabling 6 bits to be
887c478bd9Sstevel@tonic-gate    represented per printable character. (The extra 65th character, "=",
897c478bd9Sstevel@tonic-gate    is used to signify a special processing function.)
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate    The encoding process represents 24-bit groups of input bits as output
927c478bd9Sstevel@tonic-gate    strings of 4 encoded characters. Proceeding from left to right, a
937c478bd9Sstevel@tonic-gate    24-bit input group is formed by concatenating 3 8-bit input groups.
947c478bd9Sstevel@tonic-gate    These 24 bits are then treated as 4 concatenated 6-bit groups, each
957c478bd9Sstevel@tonic-gate    of which is translated into a single digit in the base64 alphabet.
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate    Each 6-bit group is used as an index into an array of 64 printable
987c478bd9Sstevel@tonic-gate    characters. The character referenced by the index is placed in the
997c478bd9Sstevel@tonic-gate    output string.
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate                          Table 1: The Base64 Alphabet
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate       Value Encoding  Value Encoding  Value Encoding  Value Encoding
1047c478bd9Sstevel@tonic-gate           0 A            17 R            34 i            51 z
1057c478bd9Sstevel@tonic-gate           1 B            18 S            35 j            52 0
1067c478bd9Sstevel@tonic-gate           2 C            19 T            36 k            53 1
1077c478bd9Sstevel@tonic-gate           3 D            20 U            37 l            54 2
1087c478bd9Sstevel@tonic-gate           4 E            21 V            38 m            55 3
1097c478bd9Sstevel@tonic-gate           5 F            22 W            39 n            56 4
1107c478bd9Sstevel@tonic-gate           6 G            23 X            40 o            57 5
1117c478bd9Sstevel@tonic-gate           7 H            24 Y            41 p            58 6
1127c478bd9Sstevel@tonic-gate           8 I            25 Z            42 q            59 7
1137c478bd9Sstevel@tonic-gate           9 J            26 a            43 r            60 8
1147c478bd9Sstevel@tonic-gate          10 K            27 b            44 s            61 9
1157c478bd9Sstevel@tonic-gate          11 L            28 c            45 t            62 +
1167c478bd9Sstevel@tonic-gate          12 M            29 d            46 u            63 /
1177c478bd9Sstevel@tonic-gate          13 N            30 e            47 v
1187c478bd9Sstevel@tonic-gate          14 O            31 f            48 w         (pad) =
1197c478bd9Sstevel@tonic-gate          15 P            32 g            49 x
1207c478bd9Sstevel@tonic-gate          16 Q            33 h            50 y
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate    Special processing is performed if fewer than 24 bits are available
1237c478bd9Sstevel@tonic-gate    at the end of the data being encoded.  A full encoding quantum is
1247c478bd9Sstevel@tonic-gate    always completed at the end of a quantity.  When fewer than 24 input
1257c478bd9Sstevel@tonic-gate    bits are available in an input group, zero bits are added (on the
1267c478bd9Sstevel@tonic-gate    right) to form an integral number of 6-bit groups.  Padding at the
1277c478bd9Sstevel@tonic-gate    end of the data is performed using the '=' character.
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate    Since all base64 input is an integral number of octets, only the
1307c478bd9Sstevel@tonic-gate          -------------------------------------------------
1317c478bd9Sstevel@tonic-gate    following cases can arise:
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate        (1) the final quantum of encoding input is an integral
1347c478bd9Sstevel@tonic-gate            multiple of 24 bits; here, the final unit of encoded
1357c478bd9Sstevel@tonic-gate 	   output will be an integral multiple of 4 characters
1367c478bd9Sstevel@tonic-gate 	   with no "=" padding,
1377c478bd9Sstevel@tonic-gate        (2) the final quantum of encoding input is exactly 8 bits;
1387c478bd9Sstevel@tonic-gate            here, the final unit of encoded output will be two
1397c478bd9Sstevel@tonic-gate 	   characters followed by two "=" padding characters, or
1407c478bd9Sstevel@tonic-gate        (3) the final quantum of encoding input is exactly 16 bits;
1417c478bd9Sstevel@tonic-gate            here, the final unit of encoded output will be three
1427c478bd9Sstevel@tonic-gate 	   characters followed by one "=" padding character.
1437c478bd9Sstevel@tonic-gate    */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate int
1467c478bd9Sstevel@tonic-gate b64_ntop(u_char const *src, size_t srclength, char *target, size_t targsize) {
1477c478bd9Sstevel@tonic-gate 	size_t datalength = 0;
1487c478bd9Sstevel@tonic-gate 	u_char input[3];
1497c478bd9Sstevel@tonic-gate 	u_char output[4];
1507c478bd9Sstevel@tonic-gate 	size_t i;
1517c478bd9Sstevel@tonic-gate 
152*9525b14bSRao Shoaib 	while (2U < srclength) {
1537c478bd9Sstevel@tonic-gate 		input[0] = *src++;
1547c478bd9Sstevel@tonic-gate 		input[1] = *src++;
1557c478bd9Sstevel@tonic-gate 		input[2] = *src++;
1567c478bd9Sstevel@tonic-gate 		srclength -= 3;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		output[0] = input[0] >> 2;
1597c478bd9Sstevel@tonic-gate 		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
1607c478bd9Sstevel@tonic-gate 		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
1617c478bd9Sstevel@tonic-gate 		output[3] = input[2] & 0x3f;
1627c478bd9Sstevel@tonic-gate 		Assert(output[0] < 64);
1637c478bd9Sstevel@tonic-gate 		Assert(output[1] < 64);
1647c478bd9Sstevel@tonic-gate 		Assert(output[2] < 64);
1657c478bd9Sstevel@tonic-gate 		Assert(output[3] < 64);
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 		if (datalength + 4 > targsize)
1687c478bd9Sstevel@tonic-gate 			return (-1);
1697c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[0]];
1707c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[1]];
1717c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[2]];
1727c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[3]];
1737c478bd9Sstevel@tonic-gate 	}
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* Now we worry about padding. */
176*9525b14bSRao Shoaib 	if (0U != srclength) {
1777c478bd9Sstevel@tonic-gate 		/* Get what's left. */
1787c478bd9Sstevel@tonic-gate 		input[0] = input[1] = input[2] = '\0';
1797c478bd9Sstevel@tonic-gate 		for (i = 0; i < srclength; i++)
1807c478bd9Sstevel@tonic-gate 			input[i] = *src++;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 		output[0] = input[0] >> 2;
1837c478bd9Sstevel@tonic-gate 		output[1] = ((input[0] & 0x03) << 4) + (input[1] >> 4);
1847c478bd9Sstevel@tonic-gate 		output[2] = ((input[1] & 0x0f) << 2) + (input[2] >> 6);
1857c478bd9Sstevel@tonic-gate 		Assert(output[0] < 64);
1867c478bd9Sstevel@tonic-gate 		Assert(output[1] < 64);
1877c478bd9Sstevel@tonic-gate 		Assert(output[2] < 64);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		if (datalength + 4 > targsize)
1907c478bd9Sstevel@tonic-gate 			return (-1);
1917c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[0]];
1927c478bd9Sstevel@tonic-gate 		target[datalength++] = Base64[output[1]];
193*9525b14bSRao Shoaib 		if (srclength == 1U)
1947c478bd9Sstevel@tonic-gate 			target[datalength++] = Pad64;
1957c478bd9Sstevel@tonic-gate 		else
1967c478bd9Sstevel@tonic-gate 			target[datalength++] = Base64[output[2]];
1977c478bd9Sstevel@tonic-gate 		target[datalength++] = Pad64;
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 	if (datalength >= targsize)
2007c478bd9Sstevel@tonic-gate 		return (-1);
201*9525b14bSRao Shoaib 	target[datalength] = '\0';	/*%< Returned value doesn't count \\0. */
2027c478bd9Sstevel@tonic-gate 	return (datalength);
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate /* skips all whitespace anywhere.
2067c478bd9Sstevel@tonic-gate    converts characters, four at a time, starting at (or after)
2077c478bd9Sstevel@tonic-gate    src from base - 64 numbers into three 8 bit bytes in the target area.
2087c478bd9Sstevel@tonic-gate    it returns the number of data bytes stored at the target, or -1 on error.
2097c478bd9Sstevel@tonic-gate  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate int
2127c478bd9Sstevel@tonic-gate b64_pton(src, target, targsize)
2137c478bd9Sstevel@tonic-gate 	char const *src;
2147c478bd9Sstevel@tonic-gate 	u_char *target;
2157c478bd9Sstevel@tonic-gate 	size_t targsize;
2167c478bd9Sstevel@tonic-gate {
2177c478bd9Sstevel@tonic-gate 	int tarindex, state, ch;
2187c478bd9Sstevel@tonic-gate 	char *pos;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 	state = 0;
2217c478bd9Sstevel@tonic-gate 	tarindex = 0;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
224*9525b14bSRao Shoaib 		if (isspace(ch))	/*%< Skip whitespace anywhere. */
2257c478bd9Sstevel@tonic-gate 			continue;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		if (ch == Pad64)
2287c478bd9Sstevel@tonic-gate 			break;
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 		pos = strchr(Base64, ch);
231*9525b14bSRao Shoaib 		if (pos == 0) 		/*%< A non-base64 character. */
2327c478bd9Sstevel@tonic-gate 			return (-1);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 		switch (state) {
2357c478bd9Sstevel@tonic-gate 		case 0:
2367c478bd9Sstevel@tonic-gate 			if (target) {
2377c478bd9Sstevel@tonic-gate 				if ((size_t)tarindex >= targsize)
2387c478bd9Sstevel@tonic-gate 					return (-1);
2397c478bd9Sstevel@tonic-gate 				target[tarindex] = (pos - Base64) << 2;
2407c478bd9Sstevel@tonic-gate 			}
2417c478bd9Sstevel@tonic-gate 			state = 1;
2427c478bd9Sstevel@tonic-gate 			break;
2437c478bd9Sstevel@tonic-gate 		case 1:
2447c478bd9Sstevel@tonic-gate 			if (target) {
2457c478bd9Sstevel@tonic-gate 				if ((size_t)tarindex + 1 >= targsize)
2467c478bd9Sstevel@tonic-gate 					return (-1);
2477c478bd9Sstevel@tonic-gate 				target[tarindex]   |=  (pos - Base64) >> 4;
2487c478bd9Sstevel@tonic-gate 				target[tarindex+1]  = ((pos - Base64) & 0x0f)
2497c478bd9Sstevel@tonic-gate 							<< 4 ;
2507c478bd9Sstevel@tonic-gate 			}
2517c478bd9Sstevel@tonic-gate 			tarindex++;
2527c478bd9Sstevel@tonic-gate 			state = 2;
2537c478bd9Sstevel@tonic-gate 			break;
2547c478bd9Sstevel@tonic-gate 		case 2:
2557c478bd9Sstevel@tonic-gate 			if (target) {
2567c478bd9Sstevel@tonic-gate 				if ((size_t)tarindex + 1 >= targsize)
2577c478bd9Sstevel@tonic-gate 					return (-1);
2587c478bd9Sstevel@tonic-gate 				target[tarindex]   |=  (pos - Base64) >> 2;
2597c478bd9Sstevel@tonic-gate 				target[tarindex+1]  = ((pos - Base64) & 0x03)
2607c478bd9Sstevel@tonic-gate 							<< 6;
2617c478bd9Sstevel@tonic-gate 			}
2627c478bd9Sstevel@tonic-gate 			tarindex++;
2637c478bd9Sstevel@tonic-gate 			state = 3;
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 		case 3:
2667c478bd9Sstevel@tonic-gate 			if (target) {
2677c478bd9Sstevel@tonic-gate 				if ((size_t)tarindex >= targsize)
2687c478bd9Sstevel@tonic-gate 					return (-1);
2697c478bd9Sstevel@tonic-gate 				target[tarindex] |= (pos - Base64);
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 			tarindex++;
2727c478bd9Sstevel@tonic-gate 			state = 0;
2737c478bd9Sstevel@tonic-gate 			break;
2747c478bd9Sstevel@tonic-gate 		default:
2757c478bd9Sstevel@tonic-gate 			abort();
2767c478bd9Sstevel@tonic-gate 		}
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * We are done decoding Base-64 chars.  Let's see if we ended
2817c478bd9Sstevel@tonic-gate 	 * on a byte boundary, and/or with erroneous trailing characters.
2827c478bd9Sstevel@tonic-gate 	 */
2837c478bd9Sstevel@tonic-gate 
284*9525b14bSRao Shoaib 	if (ch == Pad64) {		/*%< We got a pad char. */
285*9525b14bSRao Shoaib 		ch = *src++;		/*%< Skip it, get next. */
2867c478bd9Sstevel@tonic-gate 		switch (state) {
287*9525b14bSRao Shoaib 		case 0:		/*%< Invalid = in first position */
288*9525b14bSRao Shoaib 		case 1:		/*%< Invalid = in second position */
2897c478bd9Sstevel@tonic-gate 			return (-1);
2907c478bd9Sstevel@tonic-gate 
291*9525b14bSRao Shoaib 		case 2:		/*%< Valid, means one byte of info */
2927c478bd9Sstevel@tonic-gate 			/* Skip any number of spaces. */
2937c478bd9Sstevel@tonic-gate 			for ((void)NULL; ch != '\0'; ch = *src++)
2947c478bd9Sstevel@tonic-gate 				if (!isspace(ch))
2957c478bd9Sstevel@tonic-gate 					break;
2967c478bd9Sstevel@tonic-gate 			/* Make sure there is another trailing = sign. */
2977c478bd9Sstevel@tonic-gate 			if (ch != Pad64)
2987c478bd9Sstevel@tonic-gate 				return (-1);
299*9525b14bSRao Shoaib 			ch = *src++;		/*%< Skip the = */
3007c478bd9Sstevel@tonic-gate 			/* Fall through to "single trailing =" case. */
3017c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
3027c478bd9Sstevel@tonic-gate 
303*9525b14bSRao Shoaib 		case 3:		/*%< Valid, means two bytes of info */
3047c478bd9Sstevel@tonic-gate 			/*
3057c478bd9Sstevel@tonic-gate 			 * We know this char is an =.  Is there anything but
3067c478bd9Sstevel@tonic-gate 			 * whitespace after it?
3077c478bd9Sstevel@tonic-gate 			 */
3087c478bd9Sstevel@tonic-gate 			for ((void)NULL; ch != '\0'; ch = *src++)
3097c478bd9Sstevel@tonic-gate 				if (!isspace(ch))
3107c478bd9Sstevel@tonic-gate 					return (-1);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 			/*
3137c478bd9Sstevel@tonic-gate 			 * Now make sure for cases 2 and 3 that the "extra"
3147c478bd9Sstevel@tonic-gate 			 * bits that slopped past the last full byte were
3157c478bd9Sstevel@tonic-gate 			 * zeros.  If we don't check them, they become a
3167c478bd9Sstevel@tonic-gate 			 * subliminal channel.
3177c478bd9Sstevel@tonic-gate 			 */
3187c478bd9Sstevel@tonic-gate 			if (target && target[tarindex] != 0)
3197c478bd9Sstevel@tonic-gate 				return (-1);
3207c478bd9Sstevel@tonic-gate 		}
3217c478bd9Sstevel@tonic-gate 	} else {
3227c478bd9Sstevel@tonic-gate 		/*
3237c478bd9Sstevel@tonic-gate 		 * We ended by seeing the end of the string.  Make sure we
3247c478bd9Sstevel@tonic-gate 		 * have no partial bytes lying around.
3257c478bd9Sstevel@tonic-gate 		 */
3267c478bd9Sstevel@tonic-gate 		if (state != 0)
3277c478bd9Sstevel@tonic-gate 			return (-1);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	return (tarindex);
3317c478bd9Sstevel@tonic-gate }
332*9525b14bSRao Shoaib 
333*9525b14bSRao Shoaib /*! \file */
334