xref: /illumos-gate/usr/src/lib/gss_mechs/mech_krb5/include/k5-utf8.h (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
1*ba7b222eSGlenn Barry /*
2*ba7b222eSGlenn Barry  * Copyright (C) 2008 by the Massachusetts Institute of Technology,
3*ba7b222eSGlenn Barry  * Cambridge, MA, USA.  All Rights Reserved.
4*ba7b222eSGlenn Barry  *
5*ba7b222eSGlenn Barry  * This software is being provided to you, the LICENSEE, by the
6*ba7b222eSGlenn Barry  * Massachusetts Institute of Technology (M.I.T.) under the following
7*ba7b222eSGlenn Barry  * license.  By obtaining, using and/or copying this software, you agree
8*ba7b222eSGlenn Barry  * that you have read, understood, and will comply with these terms and
9*ba7b222eSGlenn Barry  * conditions:
10*ba7b222eSGlenn Barry  *
11*ba7b222eSGlenn Barry  * Export of this software from the United States of America may
12*ba7b222eSGlenn Barry  * require a specific license from the United States Government.
13*ba7b222eSGlenn Barry  * It is the responsibility of any person or organization contemplating
14*ba7b222eSGlenn Barry  * export to obtain such a license before exporting.
15*ba7b222eSGlenn Barry  *
16*ba7b222eSGlenn Barry  * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
17*ba7b222eSGlenn Barry  * this software and its documentation for any purpose and without fee or
18*ba7b222eSGlenn Barry  * royalty is hereby granted, provided that you agree to comply with the
19*ba7b222eSGlenn Barry  * following copyright notice and statements, including the disclaimer, and
20*ba7b222eSGlenn Barry  * that the same appear on ALL copies of the software and documentation,
21*ba7b222eSGlenn Barry  * including modifications that you make for internal use or for
22*ba7b222eSGlenn Barry  * distribution:
23*ba7b222eSGlenn Barry  *
24*ba7b222eSGlenn Barry  * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
25*ba7b222eSGlenn Barry  * OR WARRANTIES, EXPRESS OR IMPLIED.  By way of example, but not
26*ba7b222eSGlenn Barry  * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
27*ba7b222eSGlenn Barry  * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
28*ba7b222eSGlenn Barry  * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
29*ba7b222eSGlenn Barry  * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
30*ba7b222eSGlenn Barry  *
31*ba7b222eSGlenn Barry  * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
32*ba7b222eSGlenn Barry  * be used in advertising or publicity pertaining to distribution of the
33*ba7b222eSGlenn Barry  * software.  Title to copyright in this software and any associated
34*ba7b222eSGlenn Barry  * documentation shall at all times remain with M.I.T., and USER agrees to
35*ba7b222eSGlenn Barry  * preserve same.
36*ba7b222eSGlenn Barry  *
37*ba7b222eSGlenn Barry  * Furthermore if you modify this software you must label
38*ba7b222eSGlenn Barry  * your software as modified software and not distribute it in such a
39*ba7b222eSGlenn Barry  * fashion that it might be confused with the original M.I.T. software.
40*ba7b222eSGlenn Barry  */
41*ba7b222eSGlenn Barry /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
42*ba7b222eSGlenn Barry  *
43*ba7b222eSGlenn Barry  * Copyright 1998-2008 The OpenLDAP Foundation.
44*ba7b222eSGlenn Barry  * All rights reserved.
45*ba7b222eSGlenn Barry  *
46*ba7b222eSGlenn Barry  * Redistribution and use in source and binary forms, with or without
47*ba7b222eSGlenn Barry  * modification, are permitted only as authorized by the OpenLDAP
48*ba7b222eSGlenn Barry  * Public License.
49*ba7b222eSGlenn Barry  *
50*ba7b222eSGlenn Barry  * A copy of this license is available in file LICENSE in the
51*ba7b222eSGlenn Barry  * top-level directory of the distribution or, alternatively, at
52*ba7b222eSGlenn Barry  * <http://www.OpenLDAP.org/license.html>.
53*ba7b222eSGlenn Barry  */
54*ba7b222eSGlenn Barry /* This notice applies to changes, created by or for Novell, Inc.,
55*ba7b222eSGlenn Barry  * to preexisting works for which notices appear elsewhere in this file.
56*ba7b222eSGlenn Barry  *
57*ba7b222eSGlenn Barry  * Copyright (C) 2000 Novell, Inc. All Rights Reserved.
58*ba7b222eSGlenn Barry  *
59*ba7b222eSGlenn Barry  * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
60*ba7b222eSGlenn Barry  * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
61*ba7b222eSGlenn Barry  * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
62*ba7b222eSGlenn Barry  * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
63*ba7b222eSGlenn Barry  * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
64*ba7b222eSGlenn Barry  * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
65*ba7b222eSGlenn Barry  * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
66*ba7b222eSGlenn Barry  * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
67*ba7b222eSGlenn Barry  */
68*ba7b222eSGlenn Barry 
69*ba7b222eSGlenn Barry #ifndef K5_UTF8_H
70*ba7b222eSGlenn Barry #define K5_UTF8_H
71*ba7b222eSGlenn Barry 
72*ba7b222eSGlenn Barry #include "autoconf.h"
73*ba7b222eSGlenn Barry 
74*ba7b222eSGlenn Barry #ifdef HAVE_SYS_TYPES_H
75*ba7b222eSGlenn Barry #include <sys/types.h>
76*ba7b222eSGlenn Barry #endif
77*ba7b222eSGlenn Barry 
78*ba7b222eSGlenn Barry #ifdef HAVE_UNISTD_H
79*ba7b222eSGlenn Barry #include <unistd.h>
80*ba7b222eSGlenn Barry #endif
81*ba7b222eSGlenn Barry 
82*ba7b222eSGlenn Barry #ifdef HAVE_STDLIB_H
83*ba7b222eSGlenn Barry #include <stdlib.h>
84*ba7b222eSGlenn Barry #endif
85*ba7b222eSGlenn Barry 
86*ba7b222eSGlenn Barry #if INT_MAX == 0x7fff
87*ba7b222eSGlenn Barry typedef	unsigned int	krb5_ucs2;
88*ba7b222eSGlenn Barry #elif SHRT_MAX == 0x7fff
89*ba7b222eSGlenn Barry typedef	unsigned short	krb5_ucs2;
90*ba7b222eSGlenn Barry #else
91*ba7b222eSGlenn Barry #error undefined 16 bit type
92*ba7b222eSGlenn Barry #endif
93*ba7b222eSGlenn Barry 
94*ba7b222eSGlenn Barry #if INT_MAX == 0x7fffffffL
95*ba7b222eSGlenn Barry typedef int	krb5_ucs4;
96*ba7b222eSGlenn Barry #elif LONG_MAX == 0x7fffffffL
97*ba7b222eSGlenn Barry typedef long	krb5_ucs4;
98*ba7b222eSGlenn Barry #elif SHRT_MAX == 0x7fffffffL
99*ba7b222eSGlenn Barry typedef short	krb5_ucs4;
100*ba7b222eSGlenn Barry #else
101*ba7b222eSGlenn Barry #error: undefined 32 bit type
102*ba7b222eSGlenn Barry #endif
103*ba7b222eSGlenn Barry 
104*ba7b222eSGlenn Barry #define KRB5_MAX_UTF8_LEN   (sizeof(krb5_ucs2) * 3/2)
105*ba7b222eSGlenn Barry 
106*ba7b222eSGlenn Barry int krb5int_utf8_to_ucs2(const char *p, krb5_ucs2 *out);
107*ba7b222eSGlenn Barry size_t krb5int_ucs2_to_utf8(krb5_ucs2 c, char *buf);
108*ba7b222eSGlenn Barry 
109*ba7b222eSGlenn Barry int krb5int_utf8_to_ucs4(const char *p, krb5_ucs4 *out);
110*ba7b222eSGlenn Barry size_t krb5int_ucs4_to_utf8(krb5_ucs4 c, char *buf);
111*ba7b222eSGlenn Barry 
112*ba7b222eSGlenn Barry int
113*ba7b222eSGlenn Barry krb5int_ucs2s_to_utf8s(const krb5_ucs2 *ucs2s,
114*ba7b222eSGlenn Barry 		       char **utf8s,
115*ba7b222eSGlenn Barry 		       size_t *utf8slen);
116*ba7b222eSGlenn Barry 
117*ba7b222eSGlenn Barry int
118*ba7b222eSGlenn Barry krb5int_ucs2cs_to_utf8s(const krb5_ucs2 *ucs2s,
119*ba7b222eSGlenn Barry 			size_t ucs2slen,
120*ba7b222eSGlenn Barry 		        char **utf8s,
121*ba7b222eSGlenn Barry 		        size_t *utf8slen);
122*ba7b222eSGlenn Barry 
123*ba7b222eSGlenn Barry int
124*ba7b222eSGlenn Barry krb5int_ucs2les_to_utf8s(const unsigned char *ucs2les,
125*ba7b222eSGlenn Barry 			 char **utf8s,
126*ba7b222eSGlenn Barry 			 size_t *utf8slen);
127*ba7b222eSGlenn Barry 
128*ba7b222eSGlenn Barry int
129*ba7b222eSGlenn Barry krb5int_ucs2lecs_to_utf8s(const unsigned char *ucs2les,
130*ba7b222eSGlenn Barry 			  size_t ucs2leslen,
131*ba7b222eSGlenn Barry 			  char **utf8s,
132*ba7b222eSGlenn Barry 			  size_t *utf8slen);
133*ba7b222eSGlenn Barry 
134*ba7b222eSGlenn Barry int
135*ba7b222eSGlenn Barry krb5int_utf8s_to_ucs2s(const char *utf8s,
136*ba7b222eSGlenn Barry 		       krb5_ucs2 **ucs2s,
137*ba7b222eSGlenn Barry 		       size_t *ucs2chars);
138*ba7b222eSGlenn Barry 
139*ba7b222eSGlenn Barry int
140*ba7b222eSGlenn Barry krb5int_utf8cs_to_ucs2s(const char *utf8s,
141*ba7b222eSGlenn Barry 			size_t utf8slen,
142*ba7b222eSGlenn Barry 		        krb5_ucs2 **ucs2s,
143*ba7b222eSGlenn Barry 		        size_t *ucs2chars);
144*ba7b222eSGlenn Barry 
145*ba7b222eSGlenn Barry int
146*ba7b222eSGlenn Barry krb5int_utf8s_to_ucs2les(const char *utf8s,
147*ba7b222eSGlenn Barry 			 unsigned char **ucs2les,
148*ba7b222eSGlenn Barry 		         size_t *ucs2leslen);
149*ba7b222eSGlenn Barry 
150*ba7b222eSGlenn Barry int
151*ba7b222eSGlenn Barry krb5int_utf8cs_to_ucs2les(const char *utf8s,
152*ba7b222eSGlenn Barry 			  size_t utf8slen,
153*ba7b222eSGlenn Barry 		          unsigned char **ucs2les,
154*ba7b222eSGlenn Barry 			  size_t *ucs2leslen);
155*ba7b222eSGlenn Barry 
156*ba7b222eSGlenn Barry /* returns the number of bytes in the UTF-8 string */
157*ba7b222eSGlenn Barry size_t krb5int_utf8_bytes(const char *);
158*ba7b222eSGlenn Barry /* returns the number of UTF-8 characters in the string */
159*ba7b222eSGlenn Barry size_t krb5int_utf8_chars(const char *);
160*ba7b222eSGlenn Barry /* returns the number of UTF-8 characters in the counted string */
161*ba7b222eSGlenn Barry size_t krb5int_utf8c_chars(const char *, size_t);
162*ba7b222eSGlenn Barry /* returns the length (in bytes) of the UTF-8 character */
163*ba7b222eSGlenn Barry int krb5int_utf8_offset(const char *);
164*ba7b222eSGlenn Barry /* returns the length (in bytes) indicated by the UTF-8 character */
165*ba7b222eSGlenn Barry int krb5int_utf8_charlen(const char *);
166*ba7b222eSGlenn Barry 
167*ba7b222eSGlenn Barry /* returns the length (in bytes) indicated by the UTF-8 character
168*ba7b222eSGlenn Barry  * also checks that shortest possible encoding was used
169*ba7b222eSGlenn Barry  */
170*ba7b222eSGlenn Barry int krb5int_utf8_charlen2(const char *);
171*ba7b222eSGlenn Barry 
172*ba7b222eSGlenn Barry /* copies a UTF-8 character and returning number of bytes copied */
173*ba7b222eSGlenn Barry int krb5int_utf8_copy(char *, const char *);
174*ba7b222eSGlenn Barry 
175*ba7b222eSGlenn Barry /* returns pointer of next UTF-8 character in string */
176*ba7b222eSGlenn Barry char *krb5int_utf8_next( const char *);
177*ba7b222eSGlenn Barry /* returns pointer of previous UTF-8 character in string */
178*ba7b222eSGlenn Barry char *krb5int_utf8_prev( const char *);
179*ba7b222eSGlenn Barry 
180*ba7b222eSGlenn Barry /* primitive ctype routines -- not aware of non-ascii characters */
181*ba7b222eSGlenn Barry int krb5int_utf8_isascii( const char *);
182*ba7b222eSGlenn Barry int krb5int_utf8_isalpha( const char *);
183*ba7b222eSGlenn Barry int krb5int_utf8_isalnum( const char *);
184*ba7b222eSGlenn Barry int krb5int_utf8_isdigit( const char *);
185*ba7b222eSGlenn Barry int krb5int_utf8_isxdigit( const char *);
186*ba7b222eSGlenn Barry int krb5int_utf8_isspace( const char *);
187*ba7b222eSGlenn Barry 
188*ba7b222eSGlenn Barry /* span characters not in set, return bytes spanned */
189*ba7b222eSGlenn Barry size_t krb5int_utf8_strcspn( const char* str, const char *set);
190*ba7b222eSGlenn Barry /* span characters in set, return bytes spanned */
191*ba7b222eSGlenn Barry size_t krb5int_utf8_strspn( const char* str, const char *set);
192*ba7b222eSGlenn Barry /* return first occurance of character in string */
193*ba7b222eSGlenn Barry char *krb5int_utf8_strchr( const char* str, const char *chr);
194*ba7b222eSGlenn Barry /* return first character of set in string */
195*ba7b222eSGlenn Barry char *krb5int_utf8_strpbrk( const char* str, const char *set);
196*ba7b222eSGlenn Barry /* reentrant tokenizer */
197*ba7b222eSGlenn Barry char *krb5int_utf8_strtok( char* sp, const char* sep, char **last);
198*ba7b222eSGlenn Barry 
199*ba7b222eSGlenn Barry /* Optimizations */
200*ba7b222eSGlenn Barry extern const char krb5int_utf8_lentab[128];
201*ba7b222eSGlenn Barry extern const char krb5int_utf8_mintab[32];
202*ba7b222eSGlenn Barry 
203*ba7b222eSGlenn Barry #define KRB5_UTF8_ISASCII(p) ( !(*(const unsigned char *)(p) & 0x80 ) )
204*ba7b222eSGlenn Barry #define KRB5_UTF8_CHARLEN(p) ( KRB5_UTF8_ISASCII(p) \
205*ba7b222eSGlenn Barry 	? 1 : krb5int_utf8_lentab[*(const unsigned char *)(p) ^ 0x80] )
206*ba7b222eSGlenn Barry 
207*ba7b222eSGlenn Barry /* This is like CHARLEN but additionally validates to make sure
208*ba7b222eSGlenn Barry  * the char used the shortest possible encoding.
209*ba7b222eSGlenn Barry  * 'l' is used to temporarily hold the result of CHARLEN.
210*ba7b222eSGlenn Barry  */
211*ba7b222eSGlenn Barry #define KRB5_UTF8_CHARLEN2(p, l) ( ( ( l = KRB5_UTF8_CHARLEN( p )) < 3 || \
212*ba7b222eSGlenn Barry 	( krb5int_utf8_mintab[*(const unsigned char *)(p) & 0x1f] & (p)[1] ) ) ? \
213*ba7b222eSGlenn Barry 	l : 0 )
214*ba7b222eSGlenn Barry 
215*ba7b222eSGlenn Barry #define KRB5_UTF8_OFFSET(p) ( KRB5_UTF8_ISASCII(p) \
216*ba7b222eSGlenn Barry 	? 1 : krb5int_utf8_offset((p)) )
217*ba7b222eSGlenn Barry 
218*ba7b222eSGlenn Barry #define KRB5_UTF8_COPY(d,s) ( KRB5_UTF8_ISASCII(s) \
219*ba7b222eSGlenn Barry 	? (*(d) = *(s), 1) : krb5int_utf8_copy((d),(s)) )
220*ba7b222eSGlenn Barry 
221*ba7b222eSGlenn Barry #define KRB5_UTF8_NEXT(p) (	KRB5_UTF8_ISASCII(p) \
222*ba7b222eSGlenn Barry 	? (char *)(p)+1 : krb5int_utf8_next((p)) )
223*ba7b222eSGlenn Barry 
224*ba7b222eSGlenn Barry #define KRB5_UTF8_INCR(p) ((p) = KRB5_UTF8_NEXT(p))
225*ba7b222eSGlenn Barry 
226*ba7b222eSGlenn Barry /* For symmetry */
227*ba7b222eSGlenn Barry #define KRB5_UTF8_PREV(p) (krb5int_utf8_prev((p)))
228*ba7b222eSGlenn Barry #define KRB5_UTF8_DECR(p) ((p)=KRB5_UTF8_PREV((p)))
229*ba7b222eSGlenn Barry 
230*ba7b222eSGlenn Barry /*
231*ba7b222eSGlenn Barry  * these macros assume 'x' is an ASCII x
232*ba7b222eSGlenn Barry  * and assume the "C" locale
233*ba7b222eSGlenn Barry  */
234*ba7b222eSGlenn Barry #define KRB5_ASCII(c)		(!((c) & 0x80))
235*ba7b222eSGlenn Barry #define KRB5_SPACE(c)		((c) == ' ' || (c) == '\t' || (c) == '\n')
236*ba7b222eSGlenn Barry #define KRB5_DIGIT(c)		((c) >= '0' && (c) <= '9')
237*ba7b222eSGlenn Barry #define KRB5_LOWER(c)		((c) >= 'a' && (c) <= 'z')
238*ba7b222eSGlenn Barry #define KRB5_UPPER(c)		((c) >= 'A' && (c) <= 'Z')
239*ba7b222eSGlenn Barry #define KRB5_ALPHA(c)		(KRB5_LOWER(c) || KRB5_UPPER(c))
240*ba7b222eSGlenn Barry #define KRB5_ALNUM(c)		(KRB5_ALPHA(c) || KRB5_DIGIT(c))
241*ba7b222eSGlenn Barry 
242*ba7b222eSGlenn Barry #define KRB5_LDH(c)		(KRB5_ALNUM(c) || (c) == '-')
243*ba7b222eSGlenn Barry 
244*ba7b222eSGlenn Barry #define KRB5_HEXLOWER(c)	((c) >= 'a' && (c) <= 'f')
245*ba7b222eSGlenn Barry #define KRB5_HEXUPPER(c)	((c) >= 'A' && (c) <= 'F')
246*ba7b222eSGlenn Barry #define KRB5_HEX(c)		(KRB5_DIGIT(c) || \
247*ba7b222eSGlenn Barry 				KRB5_HEXLOWER(c) || KRB5_HEXUPPER(c))
248*ba7b222eSGlenn Barry 
249*ba7b222eSGlenn Barry #endif /* K5_UTF8_H */
250