2N/A/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2N/A/*
2N/A * Copyright (C) 2008 by the Massachusetts Institute of Technology,
2N/A * Cambridge, MA, USA. All Rights Reserved.
2N/A *
2N/A * This software is being provided to you, the LICENSEE, by the
2N/A * Massachusetts Institute of Technology (M.I.T.) under the following
2N/A * license. By obtaining, using and/or copying this software, you agree
2N/A * that you have read, understood, and will comply with these terms and
2N/A * conditions:
2N/A *
2N/A * Export of this software from the United States of America may
2N/A * require a specific license from the United States Government.
2N/A * It is the responsibility of any person or organization contemplating
2N/A * export to obtain such a license before exporting.
2N/A *
2N/A * WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute
2N/A * this software and its documentation for any purpose and without fee or
2N/A * royalty is hereby granted, provided that you agree to comply with the
2N/A * following copyright notice and statements, including the disclaimer, and
2N/A * that the same appear on ALL copies of the software and documentation,
2N/A * including modifications that you make for internal use or for
2N/A * distribution:
2N/A *
2N/A * THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS
2N/A * OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not
2N/A * limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF
2N/A * MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF
2N/A * THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY
2N/A * PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.
2N/A *
2N/A * The name of the Massachusetts Institute of Technology or M.I.T. may NOT
2N/A * be used in advertising or publicity pertaining to distribution of the
2N/A * software. Title to copyright in this software and any associated
2N/A * documentation shall at all times remain with M.I.T., and USER agrees to
2N/A * preserve same.
2N/A *
2N/A * Furthermore if you modify this software you must label
2N/A * your software as modified software and not distribute it in such a
2N/A * fashion that it might be confused with the original M.I.T. software.
2N/A */
2N/A/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
2N/A *
2N/A * Copyright 1998-2008 The OpenLDAP Foundation.
2N/A * All rights reserved.
2N/A *
2N/A * Redistribution and use in source and binary forms, with or without
2N/A * modification, are permitted only as authorized by the OpenLDAP
2N/A * Public License.
2N/A *
2N/A * A copy of this license is available in file LICENSE in the
2N/A * top-level directory of the distribution or, alternatively, at
2N/A * <http://www.OpenLDAP.org/license.html>.
2N/A */
2N/A/* This notice applies to changes, created by or for Novell, Inc.,
2N/A * to preexisting works for which notices appear elsewhere in this file.
2N/A *
2N/A * Copyright (C) 2000 Novell, Inc. All Rights Reserved.
2N/A *
2N/A * THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.
2N/A * USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION
2N/A * 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT
2N/A * HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE
2N/A * TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS
2N/A * WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC
2N/A * LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE
2N/A * PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
2N/A */
2N/A
2N/A#ifndef K5_UTF8_H
2N/A#define K5_UTF8_H
2N/A
2N/A#include "autoconf.h"
2N/A
2N/A#ifdef HAVE_SYS_TYPES_H
2N/A#include <sys/types.h>
2N/A#endif
2N/A
2N/A#ifdef HAVE_UNISTD_H
2N/A#include <unistd.h>
2N/A#endif
2N/A
2N/A#ifdef HAVE_STDLIB_H
2N/A#include <stdlib.h>
2N/A#endif
2N/A
2N/A#if INT_MAX == 0x7fff
2N/Atypedef unsigned int krb5_ucs2;
2N/A#elif SHRT_MAX == 0x7fff
2N/Atypedef unsigned short krb5_ucs2;
2N/A#else
2N/A#error undefined 16 bit type
2N/A#endif
2N/A
2N/A#if INT_MAX == 0x7fffffffL
2N/Atypedef int krb5_ucs4;
2N/A#elif LONG_MAX == 0x7fffffffL
2N/Atypedef long krb5_ucs4;
2N/A#elif SHRT_MAX == 0x7fffffffL
2N/Atypedef short krb5_ucs4;
2N/A#else
2N/A#error: undefined 32 bit type
2N/A#endif
2N/A
2N/A#define KRB5_MAX_UTF8_LEN (sizeof(krb5_ucs2) * 3/2)
2N/A
2N/Aint krb5int_utf8_to_ucs2(const char *p, krb5_ucs2 *out);
2N/Asize_t krb5int_ucs2_to_utf8(krb5_ucs2 c, char *buf);
2N/A
2N/Aint krb5int_utf8_to_ucs4(const char *p, krb5_ucs4 *out);
2N/Asize_t krb5int_ucs4_to_utf8(krb5_ucs4 c, char *buf);
2N/A
2N/Aint
2N/Akrb5int_ucs2s_to_utf8s(const krb5_ucs2 *ucs2s,
2N/A char **utf8s,
2N/A size_t *utf8slen);
2N/A
2N/Aint
2N/Akrb5int_ucs2cs_to_utf8s(const krb5_ucs2 *ucs2s,
2N/A size_t ucs2slen,
2N/A char **utf8s,
2N/A size_t *utf8slen);
2N/A
2N/Aint
2N/Akrb5int_ucs2les_to_utf8s(const unsigned char *ucs2les,
2N/A char **utf8s,
2N/A size_t *utf8slen);
2N/A
2N/Aint
2N/Akrb5int_ucs2lecs_to_utf8s(const unsigned char *ucs2les,
2N/A size_t ucs2leslen,
2N/A char **utf8s,
2N/A size_t *utf8slen);
2N/A
2N/Aint
2N/Akrb5int_utf8s_to_ucs2s(const char *utf8s,
2N/A krb5_ucs2 **ucs2s,
2N/A size_t *ucs2chars);
2N/A
2N/Aint
2N/Akrb5int_utf8cs_to_ucs2s(const char *utf8s,
2N/A size_t utf8slen,
2N/A krb5_ucs2 **ucs2s,
2N/A size_t *ucs2chars);
2N/A
2N/Aint
2N/Akrb5int_utf8s_to_ucs2les(const char *utf8s,
2N/A unsigned char **ucs2les,
2N/A size_t *ucs2leslen);
2N/A
2N/Aint
2N/Akrb5int_utf8cs_to_ucs2les(const char *utf8s,
2N/A size_t utf8slen,
2N/A unsigned char **ucs2les,
2N/A size_t *ucs2leslen);
2N/A
2N/A/* returns the number of bytes in the UTF-8 string */
2N/Asize_t krb5int_utf8_bytes(const char *);
2N/A/* returns the number of UTF-8 characters in the string */
2N/Asize_t krb5int_utf8_chars(const char *);
2N/A/* returns the number of UTF-8 characters in the counted string */
2N/Asize_t krb5int_utf8c_chars(const char *, size_t);
2N/A/* returns the length (in bytes) of the UTF-8 character */
2N/Aint krb5int_utf8_offset(const char *);
2N/A/* returns the length (in bytes) indicated by the UTF-8 character */
2N/Aint krb5int_utf8_charlen(const char *);
2N/A
2N/A/* returns the length (in bytes) indicated by the UTF-8 character
2N/A * also checks that shortest possible encoding was used
2N/A */
2N/Aint krb5int_utf8_charlen2(const char *);
2N/A
2N/A/* copies a UTF-8 character and returning number of bytes copied */
2N/Aint krb5int_utf8_copy(char *, const char *);
2N/A
2N/A/* returns pointer of next UTF-8 character in string */
2N/Achar *krb5int_utf8_next( const char *);
2N/A/* returns pointer of previous UTF-8 character in string */
2N/Achar *krb5int_utf8_prev( const char *);
2N/A
2N/A/* primitive ctype routines -- not aware of non-ascii characters */
2N/Aint krb5int_utf8_isascii( const char *);
2N/Aint krb5int_utf8_isalpha( const char *);
2N/Aint krb5int_utf8_isalnum( const char *);
2N/Aint krb5int_utf8_isdigit( const char *);
2N/Aint krb5int_utf8_isxdigit( const char *);
2N/Aint krb5int_utf8_isspace( const char *);
2N/A
2N/A/* span characters not in set, return bytes spanned */
2N/Asize_t krb5int_utf8_strcspn( const char* str, const char *set);
2N/A/* span characters in set, return bytes spanned */
2N/Asize_t krb5int_utf8_strspn( const char* str, const char *set);
2N/A/* return first occurance of character in string */
2N/Achar *krb5int_utf8_strchr( const char* str, const char *chr);
2N/A/* return first character of set in string */
2N/Achar *krb5int_utf8_strpbrk( const char* str, const char *set);
2N/A/* reentrant tokenizer */
2N/Achar *krb5int_utf8_strtok( char* sp, const char* sep, char **last);
2N/A
2N/A/* Optimizations */
2N/Aextern const char krb5int_utf8_lentab[128];
2N/Aextern const char krb5int_utf8_mintab[32];
2N/A
2N/A#define KRB5_UTF8_BV(p) (*(const unsigned char *)(p))
2N/A#define KRB5_UTF8_ISASCII(p) (!(KRB5_UTF8_BV(p) & 0x80))
2N/A#define KRB5_UTF8_CHARLEN(p) (KRB5_UTF8_ISASCII(p) ? 1 : \
2N/A krb5int_utf8_lentab[KRB5_UTF8_BV(p) ^ 0x80])
2N/A
2N/A/* This is like CHARLEN but additionally validates to make sure
2N/A * the char used the shortest possible encoding.
2N/A * 'l' is used to temporarily hold the result of CHARLEN.
2N/A */
2N/A#define KRB5_UTF8_CHARLEN2(p, l) ( \
2N/A ((l = KRB5_UTF8_CHARLEN(p)) < 3 || \
2N/A (krb5int_utf8_mintab[KRB5_UTF8_BV(p) & 0x1f] & (p)[1])) ? \
2N/A l : 0)
2N/A
2N/A#define KRB5_UTF8_OFFSET(p) (KRB5_UTF8_ISASCII(p) \
2N/A ? 1 : krb5int_utf8_offset((p)) )
2N/A
2N/A#define KRB5_UTF8_COPY(d,s) (KRB5_UTF8_ISASCII(s) \
2N/A ? (*(d) = *(s), 1) : krb5int_utf8_copy((d),(s)))
2N/A
2N/A#define KRB5_UTF8_NEXT(p) (KRB5_UTF8_ISASCII(p) \
2N/A ? (char *)(p)+1 : krb5int_utf8_next((p)))
2N/A
2N/A#define KRB5_UTF8_INCR(p) ((p) = KRB5_UTF8_NEXT(p))
2N/A
2N/A/* For symmetry */
2N/A#define KRB5_UTF8_PREV(p) (krb5int_utf8_prev((p)))
2N/A#define KRB5_UTF8_DECR(p) ((p)=KRB5_UTF8_PREV((p)))
2N/A
2N/A/*
2N/A * these macros assume 'x' is an ASCII x
2N/A * and assume the "C" locale
2N/A */
2N/A#define KRB5_ASCII(c) (!((c) & 0x80))
2N/A#define KRB5_SPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\n')
2N/A#define KRB5_DIGIT(c) ((c) >= '0' && (c) <= '9')
2N/A#define KRB5_LOWER(c) ((c) >= 'a' && (c) <= 'z')
2N/A#define KRB5_UPPER(c) ((c) >= 'A' && (c) <= 'Z')
2N/A#define KRB5_ALPHA(c) (KRB5_LOWER(c) || KRB5_UPPER(c))
2N/A#define KRB5_ALNUM(c) (KRB5_ALPHA(c) || KRB5_DIGIT(c))
2N/A
2N/A#define KRB5_LDH(c) (KRB5_ALNUM(c) || (c) == '-')
2N/A
2N/A#define KRB5_HEXLOWER(c) ((c) >= 'a' && (c) <= 'f')
2N/A#define KRB5_HEXUPPER(c) ((c) >= 'A' && (c) <= 'F')
2N/A#define KRB5_HEX(c) (KRB5_DIGIT(c) || \
2N/A KRB5_HEXLOWER(c) || KRB5_HEXUPPER(c))
2N/A
2N/A#endif /* K5_UTF8_H */