xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/a_mbstr.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* a_mbstr.c */
20Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
30Sstevel@tonic-gate  * project 1999.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <ctype.h>
610Sstevel@tonic-gate #include "cryptlib.h"
620Sstevel@tonic-gate #include <openssl/asn1.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate static int traverse_string(const unsigned char *p, int len, int inform,
650Sstevel@tonic-gate 		 int (*rfunc)(unsigned long value, void *in), void *arg);
660Sstevel@tonic-gate static int in_utf8(unsigned long value, void *arg);
670Sstevel@tonic-gate static int out_utf8(unsigned long value, void *arg);
680Sstevel@tonic-gate static int type_str(unsigned long value, void *arg);
690Sstevel@tonic-gate static int cpy_asc(unsigned long value, void *arg);
700Sstevel@tonic-gate static int cpy_bmp(unsigned long value, void *arg);
710Sstevel@tonic-gate static int cpy_univ(unsigned long value, void *arg);
720Sstevel@tonic-gate static int cpy_utf8(unsigned long value, void *arg);
730Sstevel@tonic-gate static int is_printable(unsigned long value);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate /* These functions take a string in UTF8, ASCII or multibyte form and
760Sstevel@tonic-gate  * a mask of permissible ASN1 string types. It then works out the minimal
770Sstevel@tonic-gate  * type (using the order Printable < IA5 < T61 < BMP < Universal < UTF8)
780Sstevel@tonic-gate  * and creates a string of the correct type with the supplied data.
790Sstevel@tonic-gate  * Yes this is horrible: it has to be :-(
800Sstevel@tonic-gate  * The 'ncopy' form checks minimum and maximum size limits too.
810Sstevel@tonic-gate  */
820Sstevel@tonic-gate 
ASN1_mbstring_copy(ASN1_STRING ** out,const unsigned char * in,int len,int inform,unsigned long mask)830Sstevel@tonic-gate int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
840Sstevel@tonic-gate 					int inform, unsigned long mask)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate 	return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
870Sstevel@tonic-gate }
880Sstevel@tonic-gate 
ASN1_mbstring_ncopy(ASN1_STRING ** out,const unsigned char * in,int len,int inform,unsigned long mask,long minsize,long maxsize)890Sstevel@tonic-gate int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
900Sstevel@tonic-gate 					int inform, unsigned long mask,
910Sstevel@tonic-gate 					long minsize, long maxsize)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	int str_type;
940Sstevel@tonic-gate 	int ret;
950Sstevel@tonic-gate 	char free_out;
960Sstevel@tonic-gate 	int outform, outlen;
970Sstevel@tonic-gate 	ASN1_STRING *dest;
980Sstevel@tonic-gate 	unsigned char *p;
990Sstevel@tonic-gate 	int nchar;
1000Sstevel@tonic-gate 	char strbuf[32];
1010Sstevel@tonic-gate 	int (*cpyfunc)(unsigned long,void *) = NULL;
1020Sstevel@tonic-gate 	if(len == -1) len = strlen((const char *)in);
1030Sstevel@tonic-gate 	if(!mask) mask = DIRSTRING_TYPE;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	/* First do a string check and work out the number of characters */
1060Sstevel@tonic-gate 	switch(inform) {
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		case MBSTRING_BMP:
1090Sstevel@tonic-gate 		if(len & 1) {
110*2139Sjp161948 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
1110Sstevel@tonic-gate 					 ASN1_R_INVALID_BMPSTRING_LENGTH);
1120Sstevel@tonic-gate 			return -1;
1130Sstevel@tonic-gate 		}
1140Sstevel@tonic-gate 		nchar = len >> 1;
1150Sstevel@tonic-gate 		break;
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 		case MBSTRING_UNIV:
1180Sstevel@tonic-gate 		if(len & 3) {
119*2139Sjp161948 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
1200Sstevel@tonic-gate 					 ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
1210Sstevel@tonic-gate 			return -1;
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 		nchar = len >> 2;
1240Sstevel@tonic-gate 		break;
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 		case MBSTRING_UTF8:
1270Sstevel@tonic-gate 		nchar = 0;
1280Sstevel@tonic-gate 		/* This counts the characters and does utf8 syntax checking */
1290Sstevel@tonic-gate 		ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
1300Sstevel@tonic-gate 		if(ret < 0) {
131*2139Sjp161948 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
1320Sstevel@tonic-gate 						 ASN1_R_INVALID_UTF8STRING);
1330Sstevel@tonic-gate 			return -1;
1340Sstevel@tonic-gate 		}
1350Sstevel@tonic-gate 		break;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 		case MBSTRING_ASC:
1380Sstevel@tonic-gate 		nchar = len;
1390Sstevel@tonic-gate 		break;
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		default:
142*2139Sjp161948 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT);
1430Sstevel@tonic-gate 		return -1;
1440Sstevel@tonic-gate 	}
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	if((minsize > 0) && (nchar < minsize)) {
147*2139Sjp161948 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT);
1480Sstevel@tonic-gate 		BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
1490Sstevel@tonic-gate 		ERR_add_error_data(2, "minsize=", strbuf);
1500Sstevel@tonic-gate 		return -1;
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 
1530Sstevel@tonic-gate 	if((maxsize > 0) && (nchar > maxsize)) {
154*2139Sjp161948 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG);
1550Sstevel@tonic-gate 		BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
1560Sstevel@tonic-gate 		ERR_add_error_data(2, "maxsize=", strbuf);
1570Sstevel@tonic-gate 		return -1;
1580Sstevel@tonic-gate 	}
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 	/* Now work out minimal type (if any) */
1610Sstevel@tonic-gate 	if(traverse_string(in, len, inform, type_str, &mask) < 0) {
162*2139Sjp161948 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS);
1630Sstevel@tonic-gate 		return -1;
1640Sstevel@tonic-gate 	}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	/* Now work out output format and string type */
1680Sstevel@tonic-gate 	outform = MBSTRING_ASC;
1690Sstevel@tonic-gate 	if(mask & B_ASN1_PRINTABLESTRING) str_type = V_ASN1_PRINTABLESTRING;
1700Sstevel@tonic-gate 	else if(mask & B_ASN1_IA5STRING) str_type = V_ASN1_IA5STRING;
1710Sstevel@tonic-gate 	else if(mask & B_ASN1_T61STRING) str_type = V_ASN1_T61STRING;
1720Sstevel@tonic-gate 	else if(mask & B_ASN1_BMPSTRING) {
1730Sstevel@tonic-gate 		str_type = V_ASN1_BMPSTRING;
1740Sstevel@tonic-gate 		outform = MBSTRING_BMP;
1750Sstevel@tonic-gate 	} else if(mask & B_ASN1_UNIVERSALSTRING) {
1760Sstevel@tonic-gate 		str_type = V_ASN1_UNIVERSALSTRING;
1770Sstevel@tonic-gate 		outform = MBSTRING_UNIV;
1780Sstevel@tonic-gate 	} else {
1790Sstevel@tonic-gate 		str_type = V_ASN1_UTF8STRING;
1800Sstevel@tonic-gate 		outform = MBSTRING_UTF8;
1810Sstevel@tonic-gate 	}
1820Sstevel@tonic-gate 	if(!out) return str_type;
1830Sstevel@tonic-gate 	if(*out) {
1840Sstevel@tonic-gate 		free_out = 0;
1850Sstevel@tonic-gate 		dest = *out;
1860Sstevel@tonic-gate 		if(dest->data) {
1870Sstevel@tonic-gate 			dest->length = 0;
1880Sstevel@tonic-gate 			OPENSSL_free(dest->data);
1890Sstevel@tonic-gate 			dest->data = NULL;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 		dest->type = str_type;
1920Sstevel@tonic-gate 	} else {
1930Sstevel@tonic-gate 		free_out = 1;
1940Sstevel@tonic-gate 		dest = ASN1_STRING_type_new(str_type);
1950Sstevel@tonic-gate 		if(!dest) {
196*2139Sjp161948 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,
1970Sstevel@tonic-gate 							ERR_R_MALLOC_FAILURE);
1980Sstevel@tonic-gate 			return -1;
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 		*out = dest;
2010Sstevel@tonic-gate 	}
2020Sstevel@tonic-gate 	/* If both the same type just copy across */
2030Sstevel@tonic-gate 	if(inform == outform) {
2040Sstevel@tonic-gate 		if(!ASN1_STRING_set(dest, in, len)) {
205*2139Sjp161948 			ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,ERR_R_MALLOC_FAILURE);
2060Sstevel@tonic-gate 			return -1;
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 		return str_type;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate 
2110Sstevel@tonic-gate 	/* Work out how much space the destination will need */
2120Sstevel@tonic-gate 	switch(outform) {
2130Sstevel@tonic-gate 		case MBSTRING_ASC:
2140Sstevel@tonic-gate 		outlen = nchar;
2150Sstevel@tonic-gate 		cpyfunc = cpy_asc;
2160Sstevel@tonic-gate 		break;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 		case MBSTRING_BMP:
2190Sstevel@tonic-gate 		outlen = nchar << 1;
2200Sstevel@tonic-gate 		cpyfunc = cpy_bmp;
2210Sstevel@tonic-gate 		break;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 		case MBSTRING_UNIV:
2240Sstevel@tonic-gate 		outlen = nchar << 2;
2250Sstevel@tonic-gate 		cpyfunc = cpy_univ;
2260Sstevel@tonic-gate 		break;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 		case MBSTRING_UTF8:
2290Sstevel@tonic-gate 		outlen = 0;
2300Sstevel@tonic-gate 		traverse_string(in, len, inform, out_utf8, &outlen);
2310Sstevel@tonic-gate 		cpyfunc = cpy_utf8;
2320Sstevel@tonic-gate 		break;
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 	if(!(p = OPENSSL_malloc(outlen + 1))) {
2350Sstevel@tonic-gate 		if(free_out) ASN1_STRING_free(dest);
236*2139Sjp161948 		ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY,ERR_R_MALLOC_FAILURE);
2370Sstevel@tonic-gate 		return -1;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 	dest->length = outlen;
2400Sstevel@tonic-gate 	dest->data = p;
2410Sstevel@tonic-gate 	p[outlen] = 0;
2420Sstevel@tonic-gate 	traverse_string(in, len, inform, cpyfunc, &p);
2430Sstevel@tonic-gate 	return str_type;
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate /* This function traverses a string and passes the value of each character
2470Sstevel@tonic-gate  * to an optional function along with a void * argument.
2480Sstevel@tonic-gate  */
2490Sstevel@tonic-gate 
traverse_string(const unsigned char * p,int len,int inform,int (* rfunc)(unsigned long value,void * in),void * arg)2500Sstevel@tonic-gate static int traverse_string(const unsigned char *p, int len, int inform,
2510Sstevel@tonic-gate 		 int (*rfunc)(unsigned long value, void *in), void *arg)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	unsigned long value;
2540Sstevel@tonic-gate 	int ret;
2550Sstevel@tonic-gate 	while(len) {
2560Sstevel@tonic-gate 		if(inform == MBSTRING_ASC) {
2570Sstevel@tonic-gate 			value = *p++;
2580Sstevel@tonic-gate 			len--;
2590Sstevel@tonic-gate 		} else if(inform == MBSTRING_BMP) {
2600Sstevel@tonic-gate 			value = *p++ << 8;
2610Sstevel@tonic-gate 			value |= *p++;
2620Sstevel@tonic-gate 			len -= 2;
2630Sstevel@tonic-gate 		} else if(inform == MBSTRING_UNIV) {
2640Sstevel@tonic-gate 			value = ((unsigned long)*p++) << 24;
2650Sstevel@tonic-gate 			value |= ((unsigned long)*p++) << 16;
2660Sstevel@tonic-gate 			value |= *p++ << 8;
2670Sstevel@tonic-gate 			value |= *p++;
2680Sstevel@tonic-gate 			len -= 4;
2690Sstevel@tonic-gate 		} else {
2700Sstevel@tonic-gate 			ret = UTF8_getc(p, len, &value);
2710Sstevel@tonic-gate 			if(ret < 0) return -1;
2720Sstevel@tonic-gate 			len -= ret;
2730Sstevel@tonic-gate 			p += ret;
2740Sstevel@tonic-gate 		}
2750Sstevel@tonic-gate 		if(rfunc) {
2760Sstevel@tonic-gate 			ret = rfunc(value, arg);
2770Sstevel@tonic-gate 			if(ret <= 0) return ret;
2780Sstevel@tonic-gate 		}
2790Sstevel@tonic-gate 	}
2800Sstevel@tonic-gate 	return 1;
2810Sstevel@tonic-gate }
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate /* Various utility functions for traverse_string */
2840Sstevel@tonic-gate 
2850Sstevel@tonic-gate /* Just count number of characters */
2860Sstevel@tonic-gate 
in_utf8(unsigned long value,void * arg)2870Sstevel@tonic-gate static int in_utf8(unsigned long value, void *arg)
2880Sstevel@tonic-gate {
2890Sstevel@tonic-gate 	int *nchar;
2900Sstevel@tonic-gate 	nchar = arg;
2910Sstevel@tonic-gate 	(*nchar)++;
2920Sstevel@tonic-gate 	return 1;
2930Sstevel@tonic-gate }
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate /* Determine size of output as a UTF8 String */
2960Sstevel@tonic-gate 
out_utf8(unsigned long value,void * arg)2970Sstevel@tonic-gate static int out_utf8(unsigned long value, void *arg)
2980Sstevel@tonic-gate {
2990Sstevel@tonic-gate 	int *outlen;
3000Sstevel@tonic-gate 	outlen = arg;
3010Sstevel@tonic-gate 	*outlen += UTF8_putc(NULL, -1, value);
3020Sstevel@tonic-gate 	return 1;
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /* Determine the "type" of a string: check each character against a
3060Sstevel@tonic-gate  * supplied "mask".
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate 
type_str(unsigned long value,void * arg)3090Sstevel@tonic-gate static int type_str(unsigned long value, void *arg)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	unsigned long types;
3120Sstevel@tonic-gate 	types = *((unsigned long *)arg);
3130Sstevel@tonic-gate 	if((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
3140Sstevel@tonic-gate 					types &= ~B_ASN1_PRINTABLESTRING;
3150Sstevel@tonic-gate 	if((types & B_ASN1_IA5STRING) && (value > 127))
3160Sstevel@tonic-gate 					types &= ~B_ASN1_IA5STRING;
3170Sstevel@tonic-gate 	if((types & B_ASN1_T61STRING) && (value > 0xff))
3180Sstevel@tonic-gate 					types &= ~B_ASN1_T61STRING;
3190Sstevel@tonic-gate 	if((types & B_ASN1_BMPSTRING) && (value > 0xffff))
3200Sstevel@tonic-gate 					types &= ~B_ASN1_BMPSTRING;
3210Sstevel@tonic-gate 	if(!types) return -1;
3220Sstevel@tonic-gate 	*((unsigned long *)arg) = types;
3230Sstevel@tonic-gate 	return 1;
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate 
3260Sstevel@tonic-gate /* Copy one byte per character ASCII like strings */
3270Sstevel@tonic-gate 
cpy_asc(unsigned long value,void * arg)3280Sstevel@tonic-gate static int cpy_asc(unsigned long value, void *arg)
3290Sstevel@tonic-gate {
3300Sstevel@tonic-gate 	unsigned char **p, *q;
3310Sstevel@tonic-gate 	p = arg;
3320Sstevel@tonic-gate 	q = *p;
3330Sstevel@tonic-gate 	*q = (unsigned char) value;
3340Sstevel@tonic-gate 	(*p)++;
3350Sstevel@tonic-gate 	return 1;
3360Sstevel@tonic-gate }
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate /* Copy two byte per character BMPStrings */
3390Sstevel@tonic-gate 
cpy_bmp(unsigned long value,void * arg)3400Sstevel@tonic-gate static int cpy_bmp(unsigned long value, void *arg)
3410Sstevel@tonic-gate {
3420Sstevel@tonic-gate 	unsigned char **p, *q;
3430Sstevel@tonic-gate 	p = arg;
3440Sstevel@tonic-gate 	q = *p;
3450Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 8) & 0xff);
3460Sstevel@tonic-gate 	*q = (unsigned char) (value & 0xff);
3470Sstevel@tonic-gate 	*p += 2;
3480Sstevel@tonic-gate 	return 1;
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /* Copy four byte per character UniversalStrings */
3520Sstevel@tonic-gate 
cpy_univ(unsigned long value,void * arg)3530Sstevel@tonic-gate static int cpy_univ(unsigned long value, void *arg)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	unsigned char **p, *q;
3560Sstevel@tonic-gate 	p = arg;
3570Sstevel@tonic-gate 	q = *p;
3580Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 24) & 0xff);
3590Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 16) & 0xff);
3600Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 8) & 0xff);
3610Sstevel@tonic-gate 	*q = (unsigned char) (value & 0xff);
3620Sstevel@tonic-gate 	*p += 4;
3630Sstevel@tonic-gate 	return 1;
3640Sstevel@tonic-gate }
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /* Copy to a UTF8String */
3670Sstevel@tonic-gate 
cpy_utf8(unsigned long value,void * arg)3680Sstevel@tonic-gate static int cpy_utf8(unsigned long value, void *arg)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	unsigned char **p;
3710Sstevel@tonic-gate 	int ret;
3720Sstevel@tonic-gate 	p = arg;
3730Sstevel@tonic-gate 	/* We already know there is enough room so pass 0xff as the length */
3740Sstevel@tonic-gate 	ret = UTF8_putc(*p, 0xff, value);
3750Sstevel@tonic-gate 	*p += ret;
3760Sstevel@tonic-gate 	return 1;
3770Sstevel@tonic-gate }
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate /* Return 1 if the character is permitted in a PrintableString */
is_printable(unsigned long value)3800Sstevel@tonic-gate static int is_printable(unsigned long value)
3810Sstevel@tonic-gate {
3820Sstevel@tonic-gate 	int ch;
3830Sstevel@tonic-gate 	if(value > 0x7f) return 0;
3840Sstevel@tonic-gate 	ch = (int) value;
3850Sstevel@tonic-gate 	/* Note: we can't use 'isalnum' because certain accented
3860Sstevel@tonic-gate 	 * characters may count as alphanumeric in some environments.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate #ifndef CHARSET_EBCDIC
3890Sstevel@tonic-gate 	if((ch >= 'a') && (ch <= 'z')) return 1;
3900Sstevel@tonic-gate 	if((ch >= 'A') && (ch <= 'Z')) return 1;
3910Sstevel@tonic-gate 	if((ch >= '0') && (ch <= '9')) return 1;
3920Sstevel@tonic-gate 	if ((ch == ' ') || strchr("'()+,-./:=?", ch)) return 1;
3930Sstevel@tonic-gate #else /*CHARSET_EBCDIC*/
3940Sstevel@tonic-gate 	if((ch >= os_toascii['a']) && (ch <= os_toascii['z'])) return 1;
3950Sstevel@tonic-gate 	if((ch >= os_toascii['A']) && (ch <= os_toascii['Z'])) return 1;
3960Sstevel@tonic-gate 	if((ch >= os_toascii['0']) && (ch <= os_toascii['9'])) return 1;
3970Sstevel@tonic-gate 	if ((ch == os_toascii[' ']) || strchr("'()+,-./:=?", os_toebcdic[ch])) return 1;
3980Sstevel@tonic-gate #endif /*CHARSET_EBCDIC*/
3990Sstevel@tonic-gate 	return 0;
4000Sstevel@tonic-gate }
401