xref: /onnv-gate/usr/src/common/openssl/crypto/asn1/a_mbstr.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* a_mbstr.c */
2*0Sstevel@tonic-gate /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3*0Sstevel@tonic-gate  * project 1999.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate /* ====================================================================
6*0Sstevel@tonic-gate  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7*0Sstevel@tonic-gate  *
8*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
9*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
10*0Sstevel@tonic-gate  * are met:
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
13*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
14*0Sstevel@tonic-gate  *
15*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
16*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
17*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
18*0Sstevel@tonic-gate  *    distribution.
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
21*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
22*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
23*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
27*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
28*0Sstevel@tonic-gate  *    licensing@OpenSSL.org.
29*0Sstevel@tonic-gate  *
30*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
31*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
32*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
33*0Sstevel@tonic-gate  *
34*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
35*0Sstevel@tonic-gate  *    acknowledgment:
36*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
37*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
51*0Sstevel@tonic-gate  * ====================================================================
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
54*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
55*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
56*0Sstevel@tonic-gate  *
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <stdio.h>
60*0Sstevel@tonic-gate #include <ctype.h>
61*0Sstevel@tonic-gate #include "cryptlib.h"
62*0Sstevel@tonic-gate #include <openssl/asn1.h>
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate static int traverse_string(const unsigned char *p, int len, int inform,
65*0Sstevel@tonic-gate 		 int (*rfunc)(unsigned long value, void *in), void *arg);
66*0Sstevel@tonic-gate static int in_utf8(unsigned long value, void *arg);
67*0Sstevel@tonic-gate static int out_utf8(unsigned long value, void *arg);
68*0Sstevel@tonic-gate static int type_str(unsigned long value, void *arg);
69*0Sstevel@tonic-gate static int cpy_asc(unsigned long value, void *arg);
70*0Sstevel@tonic-gate static int cpy_bmp(unsigned long value, void *arg);
71*0Sstevel@tonic-gate static int cpy_univ(unsigned long value, void *arg);
72*0Sstevel@tonic-gate static int cpy_utf8(unsigned long value, void *arg);
73*0Sstevel@tonic-gate static int is_printable(unsigned long value);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /* These functions take a string in UTF8, ASCII or multibyte form and
76*0Sstevel@tonic-gate  * a mask of permissible ASN1 string types. It then works out the minimal
77*0Sstevel@tonic-gate  * type (using the order Printable < IA5 < T61 < BMP < Universal < UTF8)
78*0Sstevel@tonic-gate  * and creates a string of the correct type with the supplied data.
79*0Sstevel@tonic-gate  * Yes this is horrible: it has to be :-(
80*0Sstevel@tonic-gate  * The 'ncopy' form checks minimum and maximum size limits too.
81*0Sstevel@tonic-gate  */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate int ASN1_mbstring_copy(ASN1_STRING **out, const unsigned char *in, int len,
84*0Sstevel@tonic-gate 					int inform, unsigned long mask)
85*0Sstevel@tonic-gate {
86*0Sstevel@tonic-gate 	return ASN1_mbstring_ncopy(out, in, len, inform, mask, 0, 0);
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len,
90*0Sstevel@tonic-gate 					int inform, unsigned long mask,
91*0Sstevel@tonic-gate 					long minsize, long maxsize)
92*0Sstevel@tonic-gate {
93*0Sstevel@tonic-gate 	int str_type;
94*0Sstevel@tonic-gate 	int ret;
95*0Sstevel@tonic-gate 	char free_out;
96*0Sstevel@tonic-gate 	int outform, outlen;
97*0Sstevel@tonic-gate 	ASN1_STRING *dest;
98*0Sstevel@tonic-gate 	unsigned char *p;
99*0Sstevel@tonic-gate 	int nchar;
100*0Sstevel@tonic-gate 	char strbuf[32];
101*0Sstevel@tonic-gate 	int (*cpyfunc)(unsigned long,void *) = NULL;
102*0Sstevel@tonic-gate 	if(len == -1) len = strlen((const char *)in);
103*0Sstevel@tonic-gate 	if(!mask) mask = DIRSTRING_TYPE;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	/* First do a string check and work out the number of characters */
106*0Sstevel@tonic-gate 	switch(inform) {
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 		case MBSTRING_BMP:
109*0Sstevel@tonic-gate 		if(len & 1) {
110*0Sstevel@tonic-gate 			ASN1err(ASN1_F_ASN1_MBSTRING_COPY,
111*0Sstevel@tonic-gate 					 ASN1_R_INVALID_BMPSTRING_LENGTH);
112*0Sstevel@tonic-gate 			return -1;
113*0Sstevel@tonic-gate 		}
114*0Sstevel@tonic-gate 		nchar = len >> 1;
115*0Sstevel@tonic-gate 		break;
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 		case MBSTRING_UNIV:
118*0Sstevel@tonic-gate 		if(len & 3) {
119*0Sstevel@tonic-gate 			ASN1err(ASN1_F_ASN1_MBSTRING_COPY,
120*0Sstevel@tonic-gate 					 ASN1_R_INVALID_UNIVERSALSTRING_LENGTH);
121*0Sstevel@tonic-gate 			return -1;
122*0Sstevel@tonic-gate 		}
123*0Sstevel@tonic-gate 		nchar = len >> 2;
124*0Sstevel@tonic-gate 		break;
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 		case MBSTRING_UTF8:
127*0Sstevel@tonic-gate 		nchar = 0;
128*0Sstevel@tonic-gate 		/* This counts the characters and does utf8 syntax checking */
129*0Sstevel@tonic-gate 		ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar);
130*0Sstevel@tonic-gate 		if(ret < 0) {
131*0Sstevel@tonic-gate 			ASN1err(ASN1_F_ASN1_MBSTRING_COPY,
132*0Sstevel@tonic-gate 						 ASN1_R_INVALID_UTF8STRING);
133*0Sstevel@tonic-gate 			return -1;
134*0Sstevel@tonic-gate 		}
135*0Sstevel@tonic-gate 		break;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 		case MBSTRING_ASC:
138*0Sstevel@tonic-gate 		nchar = len;
139*0Sstevel@tonic-gate 		break;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 		default:
142*0Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_MBSTRING_COPY, ASN1_R_UNKNOWN_FORMAT);
143*0Sstevel@tonic-gate 		return -1;
144*0Sstevel@tonic-gate 	}
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate 	if((minsize > 0) && (nchar < minsize)) {
147*0Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_MBSTRING_COPY, ASN1_R_STRING_TOO_SHORT);
148*0Sstevel@tonic-gate 		BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize);
149*0Sstevel@tonic-gate 		ERR_add_error_data(2, "minsize=", strbuf);
150*0Sstevel@tonic-gate 		return -1;
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	if((maxsize > 0) && (nchar > maxsize)) {
154*0Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_MBSTRING_COPY, ASN1_R_STRING_TOO_LONG);
155*0Sstevel@tonic-gate 		BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize);
156*0Sstevel@tonic-gate 		ERR_add_error_data(2, "maxsize=", strbuf);
157*0Sstevel@tonic-gate 		return -1;
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	/* Now work out minimal type (if any) */
161*0Sstevel@tonic-gate 	if(traverse_string(in, len, inform, type_str, &mask) < 0) {
162*0Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_MBSTRING_COPY, ASN1_R_ILLEGAL_CHARACTERS);
163*0Sstevel@tonic-gate 		return -1;
164*0Sstevel@tonic-gate 	}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/* Now work out output format and string type */
168*0Sstevel@tonic-gate 	outform = MBSTRING_ASC;
169*0Sstevel@tonic-gate 	if(mask & B_ASN1_PRINTABLESTRING) str_type = V_ASN1_PRINTABLESTRING;
170*0Sstevel@tonic-gate 	else if(mask & B_ASN1_IA5STRING) str_type = V_ASN1_IA5STRING;
171*0Sstevel@tonic-gate 	else if(mask & B_ASN1_T61STRING) str_type = V_ASN1_T61STRING;
172*0Sstevel@tonic-gate 	else if(mask & B_ASN1_BMPSTRING) {
173*0Sstevel@tonic-gate 		str_type = V_ASN1_BMPSTRING;
174*0Sstevel@tonic-gate 		outform = MBSTRING_BMP;
175*0Sstevel@tonic-gate 	} else if(mask & B_ASN1_UNIVERSALSTRING) {
176*0Sstevel@tonic-gate 		str_type = V_ASN1_UNIVERSALSTRING;
177*0Sstevel@tonic-gate 		outform = MBSTRING_UNIV;
178*0Sstevel@tonic-gate 	} else {
179*0Sstevel@tonic-gate 		str_type = V_ASN1_UTF8STRING;
180*0Sstevel@tonic-gate 		outform = MBSTRING_UTF8;
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 	if(!out) return str_type;
183*0Sstevel@tonic-gate 	if(*out) {
184*0Sstevel@tonic-gate 		free_out = 0;
185*0Sstevel@tonic-gate 		dest = *out;
186*0Sstevel@tonic-gate 		if(dest->data) {
187*0Sstevel@tonic-gate 			dest->length = 0;
188*0Sstevel@tonic-gate 			OPENSSL_free(dest->data);
189*0Sstevel@tonic-gate 			dest->data = NULL;
190*0Sstevel@tonic-gate 		}
191*0Sstevel@tonic-gate 		dest->type = str_type;
192*0Sstevel@tonic-gate 	} else {
193*0Sstevel@tonic-gate 		free_out = 1;
194*0Sstevel@tonic-gate 		dest = ASN1_STRING_type_new(str_type);
195*0Sstevel@tonic-gate 		if(!dest) {
196*0Sstevel@tonic-gate 			ASN1err(ASN1_F_ASN1_MBSTRING_COPY,
197*0Sstevel@tonic-gate 							ERR_R_MALLOC_FAILURE);
198*0Sstevel@tonic-gate 			return -1;
199*0Sstevel@tonic-gate 		}
200*0Sstevel@tonic-gate 		*out = dest;
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate 	/* If both the same type just copy across */
203*0Sstevel@tonic-gate 	if(inform == outform) {
204*0Sstevel@tonic-gate 		if(!ASN1_STRING_set(dest, in, len)) {
205*0Sstevel@tonic-gate 			ASN1err(ASN1_F_ASN1_MBSTRING_COPY,ERR_R_MALLOC_FAILURE);
206*0Sstevel@tonic-gate 			return -1;
207*0Sstevel@tonic-gate 		}
208*0Sstevel@tonic-gate 		return str_type;
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	/* Work out how much space the destination will need */
212*0Sstevel@tonic-gate 	switch(outform) {
213*0Sstevel@tonic-gate 		case MBSTRING_ASC:
214*0Sstevel@tonic-gate 		outlen = nchar;
215*0Sstevel@tonic-gate 		cpyfunc = cpy_asc;
216*0Sstevel@tonic-gate 		break;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 		case MBSTRING_BMP:
219*0Sstevel@tonic-gate 		outlen = nchar << 1;
220*0Sstevel@tonic-gate 		cpyfunc = cpy_bmp;
221*0Sstevel@tonic-gate 		break;
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 		case MBSTRING_UNIV:
224*0Sstevel@tonic-gate 		outlen = nchar << 2;
225*0Sstevel@tonic-gate 		cpyfunc = cpy_univ;
226*0Sstevel@tonic-gate 		break;
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		case MBSTRING_UTF8:
229*0Sstevel@tonic-gate 		outlen = 0;
230*0Sstevel@tonic-gate 		traverse_string(in, len, inform, out_utf8, &outlen);
231*0Sstevel@tonic-gate 		cpyfunc = cpy_utf8;
232*0Sstevel@tonic-gate 		break;
233*0Sstevel@tonic-gate 	}
234*0Sstevel@tonic-gate 	if(!(p = OPENSSL_malloc(outlen + 1))) {
235*0Sstevel@tonic-gate 		if(free_out) ASN1_STRING_free(dest);
236*0Sstevel@tonic-gate 		ASN1err(ASN1_F_ASN1_MBSTRING_COPY,ERR_R_MALLOC_FAILURE);
237*0Sstevel@tonic-gate 		return -1;
238*0Sstevel@tonic-gate 	}
239*0Sstevel@tonic-gate 	dest->length = outlen;
240*0Sstevel@tonic-gate 	dest->data = p;
241*0Sstevel@tonic-gate 	p[outlen] = 0;
242*0Sstevel@tonic-gate 	traverse_string(in, len, inform, cpyfunc, &p);
243*0Sstevel@tonic-gate 	return str_type;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate 
246*0Sstevel@tonic-gate /* This function traverses a string and passes the value of each character
247*0Sstevel@tonic-gate  * to an optional function along with a void * argument.
248*0Sstevel@tonic-gate  */
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate static int traverse_string(const unsigned char *p, int len, int inform,
251*0Sstevel@tonic-gate 		 int (*rfunc)(unsigned long value, void *in), void *arg)
252*0Sstevel@tonic-gate {
253*0Sstevel@tonic-gate 	unsigned long value;
254*0Sstevel@tonic-gate 	int ret;
255*0Sstevel@tonic-gate 	while(len) {
256*0Sstevel@tonic-gate 		if(inform == MBSTRING_ASC) {
257*0Sstevel@tonic-gate 			value = *p++;
258*0Sstevel@tonic-gate 			len--;
259*0Sstevel@tonic-gate 		} else if(inform == MBSTRING_BMP) {
260*0Sstevel@tonic-gate 			value = *p++ << 8;
261*0Sstevel@tonic-gate 			value |= *p++;
262*0Sstevel@tonic-gate 			len -= 2;
263*0Sstevel@tonic-gate 		} else if(inform == MBSTRING_UNIV) {
264*0Sstevel@tonic-gate 			value = ((unsigned long)*p++) << 24;
265*0Sstevel@tonic-gate 			value |= ((unsigned long)*p++) << 16;
266*0Sstevel@tonic-gate 			value |= *p++ << 8;
267*0Sstevel@tonic-gate 			value |= *p++;
268*0Sstevel@tonic-gate 			len -= 4;
269*0Sstevel@tonic-gate 		} else {
270*0Sstevel@tonic-gate 			ret = UTF8_getc(p, len, &value);
271*0Sstevel@tonic-gate 			if(ret < 0) return -1;
272*0Sstevel@tonic-gate 			len -= ret;
273*0Sstevel@tonic-gate 			p += ret;
274*0Sstevel@tonic-gate 		}
275*0Sstevel@tonic-gate 		if(rfunc) {
276*0Sstevel@tonic-gate 			ret = rfunc(value, arg);
277*0Sstevel@tonic-gate 			if(ret <= 0) return ret;
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 	return 1;
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate /* Various utility functions for traverse_string */
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate /* Just count number of characters */
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate static int in_utf8(unsigned long value, void *arg)
288*0Sstevel@tonic-gate {
289*0Sstevel@tonic-gate 	int *nchar;
290*0Sstevel@tonic-gate 	nchar = arg;
291*0Sstevel@tonic-gate 	(*nchar)++;
292*0Sstevel@tonic-gate 	return 1;
293*0Sstevel@tonic-gate }
294*0Sstevel@tonic-gate 
295*0Sstevel@tonic-gate /* Determine size of output as a UTF8 String */
296*0Sstevel@tonic-gate 
297*0Sstevel@tonic-gate static int out_utf8(unsigned long value, void *arg)
298*0Sstevel@tonic-gate {
299*0Sstevel@tonic-gate 	int *outlen;
300*0Sstevel@tonic-gate 	outlen = arg;
301*0Sstevel@tonic-gate 	*outlen += UTF8_putc(NULL, -1, value);
302*0Sstevel@tonic-gate 	return 1;
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate /* Determine the "type" of a string: check each character against a
306*0Sstevel@tonic-gate  * supplied "mask".
307*0Sstevel@tonic-gate  */
308*0Sstevel@tonic-gate 
309*0Sstevel@tonic-gate static int type_str(unsigned long value, void *arg)
310*0Sstevel@tonic-gate {
311*0Sstevel@tonic-gate 	unsigned long types;
312*0Sstevel@tonic-gate 	types = *((unsigned long *)arg);
313*0Sstevel@tonic-gate 	if((types & B_ASN1_PRINTABLESTRING) && !is_printable(value))
314*0Sstevel@tonic-gate 					types &= ~B_ASN1_PRINTABLESTRING;
315*0Sstevel@tonic-gate 	if((types & B_ASN1_IA5STRING) && (value > 127))
316*0Sstevel@tonic-gate 					types &= ~B_ASN1_IA5STRING;
317*0Sstevel@tonic-gate 	if((types & B_ASN1_T61STRING) && (value > 0xff))
318*0Sstevel@tonic-gate 					types &= ~B_ASN1_T61STRING;
319*0Sstevel@tonic-gate 	if((types & B_ASN1_BMPSTRING) && (value > 0xffff))
320*0Sstevel@tonic-gate 					types &= ~B_ASN1_BMPSTRING;
321*0Sstevel@tonic-gate 	if(!types) return -1;
322*0Sstevel@tonic-gate 	*((unsigned long *)arg) = types;
323*0Sstevel@tonic-gate 	return 1;
324*0Sstevel@tonic-gate }
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate /* Copy one byte per character ASCII like strings */
327*0Sstevel@tonic-gate 
328*0Sstevel@tonic-gate static int cpy_asc(unsigned long value, void *arg)
329*0Sstevel@tonic-gate {
330*0Sstevel@tonic-gate 	unsigned char **p, *q;
331*0Sstevel@tonic-gate 	p = arg;
332*0Sstevel@tonic-gate 	q = *p;
333*0Sstevel@tonic-gate 	*q = (unsigned char) value;
334*0Sstevel@tonic-gate 	(*p)++;
335*0Sstevel@tonic-gate 	return 1;
336*0Sstevel@tonic-gate }
337*0Sstevel@tonic-gate 
338*0Sstevel@tonic-gate /* Copy two byte per character BMPStrings */
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate static int cpy_bmp(unsigned long value, void *arg)
341*0Sstevel@tonic-gate {
342*0Sstevel@tonic-gate 	unsigned char **p, *q;
343*0Sstevel@tonic-gate 	p = arg;
344*0Sstevel@tonic-gate 	q = *p;
345*0Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 8) & 0xff);
346*0Sstevel@tonic-gate 	*q = (unsigned char) (value & 0xff);
347*0Sstevel@tonic-gate 	*p += 2;
348*0Sstevel@tonic-gate 	return 1;
349*0Sstevel@tonic-gate }
350*0Sstevel@tonic-gate 
351*0Sstevel@tonic-gate /* Copy four byte per character UniversalStrings */
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate static int cpy_univ(unsigned long value, void *arg)
354*0Sstevel@tonic-gate {
355*0Sstevel@tonic-gate 	unsigned char **p, *q;
356*0Sstevel@tonic-gate 	p = arg;
357*0Sstevel@tonic-gate 	q = *p;
358*0Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 24) & 0xff);
359*0Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 16) & 0xff);
360*0Sstevel@tonic-gate 	*q++ = (unsigned char) ((value >> 8) & 0xff);
361*0Sstevel@tonic-gate 	*q = (unsigned char) (value & 0xff);
362*0Sstevel@tonic-gate 	*p += 4;
363*0Sstevel@tonic-gate 	return 1;
364*0Sstevel@tonic-gate }
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate /* Copy to a UTF8String */
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate static int cpy_utf8(unsigned long value, void *arg)
369*0Sstevel@tonic-gate {
370*0Sstevel@tonic-gate 	unsigned char **p;
371*0Sstevel@tonic-gate 	int ret;
372*0Sstevel@tonic-gate 	p = arg;
373*0Sstevel@tonic-gate 	/* We already know there is enough room so pass 0xff as the length */
374*0Sstevel@tonic-gate 	ret = UTF8_putc(*p, 0xff, value);
375*0Sstevel@tonic-gate 	*p += ret;
376*0Sstevel@tonic-gate 	return 1;
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate 
379*0Sstevel@tonic-gate /* Return 1 if the character is permitted in a PrintableString */
380*0Sstevel@tonic-gate static int is_printable(unsigned long value)
381*0Sstevel@tonic-gate {
382*0Sstevel@tonic-gate 	int ch;
383*0Sstevel@tonic-gate 	if(value > 0x7f) return 0;
384*0Sstevel@tonic-gate 	ch = (int) value;
385*0Sstevel@tonic-gate 	/* Note: we can't use 'isalnum' because certain accented
386*0Sstevel@tonic-gate 	 * characters may count as alphanumeric in some environments.
387*0Sstevel@tonic-gate 	 */
388*0Sstevel@tonic-gate #ifndef CHARSET_EBCDIC
389*0Sstevel@tonic-gate 	if((ch >= 'a') && (ch <= 'z')) return 1;
390*0Sstevel@tonic-gate 	if((ch >= 'A') && (ch <= 'Z')) return 1;
391*0Sstevel@tonic-gate 	if((ch >= '0') && (ch <= '9')) return 1;
392*0Sstevel@tonic-gate 	if ((ch == ' ') || strchr("'()+,-./:=?", ch)) return 1;
393*0Sstevel@tonic-gate #else /*CHARSET_EBCDIC*/
394*0Sstevel@tonic-gate 	if((ch >= os_toascii['a']) && (ch <= os_toascii['z'])) return 1;
395*0Sstevel@tonic-gate 	if((ch >= os_toascii['A']) && (ch <= os_toascii['Z'])) return 1;
396*0Sstevel@tonic-gate 	if((ch >= os_toascii['0']) && (ch <= os_toascii['9'])) return 1;
397*0Sstevel@tonic-gate 	if ((ch == os_toascii[' ']) || strchr("'()+,-./:=?", os_toebcdic[ch])) return 1;
398*0Sstevel@tonic-gate #endif /*CHARSET_EBCDIC*/
399*0Sstevel@tonic-gate 	return 0;
400*0Sstevel@tonic-gate }
401