/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */1/*2* Copyright (C) 2008 by the Massachusetts Institute of Technology,3* Cambridge, MA, USA. All Rights Reserved.4*5* This software is being provided to you, the LICENSEE, by the6* Massachusetts Institute of Technology (M.I.T.) under the following7* license. By obtaining, using and/or copying this software, you agree8* that you have read, understood, and will comply with these terms and9* conditions:10*11* Export of this software from the United States of America may12* require a specific license from the United States Government.13* It is the responsibility of any person or organization contemplating14* export to obtain such a license before exporting.15*16* WITHIN THAT CONSTRAINT, permission to use, copy, modify and distribute17* this software and its documentation for any purpose and without fee or18* royalty is hereby granted, provided that you agree to comply with the19* following copyright notice and statements, including the disclaimer, and20* that the same appear on ALL copies of the software and documentation,21* including modifications that you make for internal use or for22* distribution:23*24* THIS SOFTWARE IS PROVIDED "AS IS", AND M.I.T. MAKES NO REPRESENTATIONS25* OR WARRANTIES, EXPRESS OR IMPLIED. By way of example, but not26* limitation, M.I.T. MAKES NO REPRESENTATIONS OR WARRANTIES OF27* MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE OF28* THE LICENSED SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY29* PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS.30*31* The name of the Massachusetts Institute of Technology or M.I.T. may NOT32* be used in advertising or publicity pertaining to distribution of the33* software. Title to copyright in this software and any associated34* documentation shall at all times remain with M.I.T., and USER agrees to35* preserve same.36*37* Furthermore if you modify this software you must label38* your software as modified software and not distribute it in such a39* fashion that it might be confused with the original M.I.T. software.40*/41/*42* Copyright 1998-2008 The OpenLDAP Foundation.43* All rights reserved.44*45* Redistribution and use in source and binary forms, with or without46* modification, are permitted only as authorized by the OpenLDAP47* Public License.48*49* A copy of this license is available in file LICENSE in the50* top-level directory of the distribution or, alternatively, at51* <https://www.OpenLDAP.org/license.html>.52*/53/*54* Copyright (C) 2000 Novell, Inc. All Rights Reserved.55*56* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND TREATIES.57* USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT TO VERSION58* 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS AVAILABLE AT59* HTTPS://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE" IN THE60* TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION OF THIS61* WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP PUBLIC62* LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT THE63* PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.64*/65/* This work is part of OpenLDAP Software <https://www.openldap.org/>. */6667#ifndef K5_UTF8_H68#define K5_UTF8_H6970#include "k5-platform.h"7172typedef uint16_t krb5_ucs2;73typedef uint32_t krb5_ucs4;7475int krb5int_utf8_to_ucs4(const char *p, krb5_ucs4 *out);76size_t krb5int_ucs4_to_utf8(krb5_ucs4 c, char *buf);7778/*79* Convert a little-endian UTF-16 string to an allocated null-terminated UTF-880* string. nbytes is the length of ucs2bytes in bytes, and must be an even81* number. Return EINVAL on invalid input, ENOMEM on out of memory, or 0 on82* success.83*/84int k5_utf16le_to_utf8(const uint8_t *utf16bytes, size_t nbytes,85char **utf8_out);8687/*88* Convert a UTF-8 string to an allocated little-endian UTF-16 string. The89* resulting length is in bytes and will always be even. Return EINVAL on90* invalid input, ENOMEM on out of memory, or 0 on success.91*/92int k5_utf8_to_utf16le(const char *utf8, uint8_t **utf16_out,93size_t *nbytes_out);9495/* Optimizations */96extern const char krb5int_utf8_lentab[128];97extern const char krb5int_utf8_mintab[32];9899#define KRB5_UTF8_BV(p) (*(const unsigned char *)(p))100#define KRB5_UTF8_ISASCII(p) (!(KRB5_UTF8_BV(p) & 0x80))101#define KRB5_UTF8_CHARLEN(p) (KRB5_UTF8_ISASCII(p) ? 1 : \102krb5int_utf8_lentab[KRB5_UTF8_BV(p) ^ 0x80])103104/* This is like CHARLEN but additionally validates to make sure105* the char used the shortest possible encoding.106* 'l' is used to temporarily hold the result of CHARLEN.107*/108#define KRB5_UTF8_CHARLEN2(p, l) ( \109((l = KRB5_UTF8_CHARLEN(p)) < 3 || \110(krb5int_utf8_mintab[KRB5_UTF8_BV(p) & 0x1f] & (p)[1])) ? \111l : 0)112113/*114* these macros assume 'x' is an ASCII x115* and assume the "C" locale116*/117#define KRB5_UPPER(c) ((c) >= 'A' && (c) <= 'Z')118119#endif /* K5_UTF8_H */120121122