|
Packit |
fd8b60 |
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
|
|
Packit |
fd8b60 |
/* util/support/utf8_conv.c */
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright 2008, 2017 by the Massachusetts Institute of Technology.
|
|
Packit |
fd8b60 |
* All Rights Reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Export of this software from the United States of America may
|
|
Packit |
fd8b60 |
* require a specific license from the United States Government.
|
|
Packit |
fd8b60 |
* It is the responsibility of any person or organization contemplating
|
|
Packit |
fd8b60 |
* export to obtain such a license before exporting.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
|
|
Packit |
fd8b60 |
* distribute this software and its documentation for any purpose and
|
|
Packit |
fd8b60 |
* without fee is hereby granted, provided that the above copyright
|
|
Packit |
fd8b60 |
* notice appear in all copies and that both that copyright notice and
|
|
Packit |
fd8b60 |
* this permission notice appear in supporting documentation, and that
|
|
Packit |
fd8b60 |
* the name of M.I.T. not be used in advertising or publicity pertaining
|
|
Packit |
fd8b60 |
* to distribution of the software without specific, written prior
|
|
Packit |
fd8b60 |
* permission. Furthermore if you modify this software you must label
|
|
Packit |
fd8b60 |
* your software as modified software and not distribute it in such a
|
|
Packit |
fd8b60 |
* fashion that it might be confused with the original M.I.T. software.
|
|
Packit |
fd8b60 |
* M.I.T. makes no representations about the suitability of
|
|
Packit |
fd8b60 |
* this software for any purpose. It is provided "as is" without express
|
|
Packit |
fd8b60 |
* or implied warranty.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* Copyright 1998-2008 The OpenLDAP Foundation.
|
|
Packit |
fd8b60 |
* All rights reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
fd8b60 |
* modification, are permitted only as authorized by the OpenLDAP
|
|
Packit |
fd8b60 |
* Public License.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* A copy of this license is available in the file LICENSE in the
|
|
Packit |
fd8b60 |
* top-level directory of the distribution or, alternatively, at
|
|
Packit |
fd8b60 |
* <https://www.OpenLDAP.org/license.html>.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
/* Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
|
|
Packit |
fd8b60 |
*
|
|
Packit |
fd8b60 |
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
|
|
Packit |
fd8b60 |
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
|
|
Packit |
fd8b60 |
* TO VERSION 2.0.1 OF THE OPENLDAP PUBLIC LICENSE, A COPY OF WHICH IS
|
|
Packit |
fd8b60 |
* AVAILABLE AT HTTP://WWW.OPENLDAP.ORG/LICENSE.HTML OR IN THE FILE "LICENSE"
|
|
Packit |
fd8b60 |
* IN THE TOP-LEVEL DIRECTORY OF THE DISTRIBUTION. ANY USE OR EXPLOITATION
|
|
Packit |
fd8b60 |
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
|
|
Packit |
fd8b60 |
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
|
|
Packit |
fd8b60 |
* THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* This work is based on OpenLDAP Software <https://www.openldap.org/>. */
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/*
|
|
Packit |
fd8b60 |
* These routines convert between UTF-16 and UTF-8. UTF-16 encodes a Unicode
|
|
Packit |
fd8b60 |
* character in either two or four bytes. Characters in the Basic Multilingual
|
|
Packit |
fd8b60 |
* Plane (hex 0..D7FF and E000..FFFF) are encoded as-is in two bytes.
|
|
Packit |
fd8b60 |
* Characters in the Supplementary Planes (10000..10FFFF) are split into a high
|
|
Packit |
fd8b60 |
* surrogate and a low surrogate, each containing ten bits of the character
|
|
Packit |
fd8b60 |
* value, and encoded in four bytes.
|
|
Packit |
fd8b60 |
*/
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
#include "k5-platform.h"
|
|
Packit |
fd8b60 |
#include "k5-utf8.h"
|
|
Packit |
fd8b60 |
#include "k5-buf.h"
|
|
Packit |
fd8b60 |
#include "k5-input.h"
|
|
Packit |
fd8b60 |
#include "supp-int.h"
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
static unsigned char mask[] = { 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* A high surrogate is ten bits masked with 0xD800. */
|
|
Packit |
fd8b60 |
#define IS_HIGH_SURROGATE(c) ((c) >= 0xD800 && (c) <= 0xDBFF)
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* A low surrogate is ten bits masked with 0xDC00. */
|
|
Packit |
fd8b60 |
#define IS_LOW_SURROGATE(c) ((c) >= 0xDC00 && (c) <= 0xDFFF)
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* A valid Unicode code point is in the range 0..10FFFF and is not a surrogate
|
|
Packit |
fd8b60 |
* value. */
|
|
Packit |
fd8b60 |
#define IS_SURROGATE(c) ((c) >= 0xD800 && (c) <= 0xDFFF)
|
|
Packit |
fd8b60 |
#define IS_VALID_UNICODE(c) ((c) <= 0x10FFFF && !IS_SURROGATE(c))
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* A Basic Multilingual Plane character is in the range 0..FFFF and is not a
|
|
Packit |
fd8b60 |
* surrogate value. */
|
|
Packit |
fd8b60 |
#define IS_BMP(c) ((c) <= 0xFFFF && !IS_SURROGATE(c))
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Characters in the Supplementary Planes have a base value subtracted from
|
|
Packit |
fd8b60 |
* their code points to form a 20-bit value; ten bits go in each surrogate. */
|
|
Packit |
fd8b60 |
#define BASE 0x10000
|
|
Packit |
fd8b60 |
#define HIGH_SURROGATE(c) (0xD800 | (((c) - BASE) >> 10))
|
|
Packit |
fd8b60 |
#define LOW_SURROGATE(c) (0xDC00 | (((c) - BASE) & 0x3FF))
|
|
Packit |
fd8b60 |
#define COMPOSE(c1, c2) (BASE + ((((c1) & 0x3FF) << 10) | ((c2) & 0x3FF)))
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
int
|
|
Packit |
fd8b60 |
k5_utf8_to_utf16le(const char *utf8, uint8_t **utf16_out, size_t *nbytes_out)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
struct k5buf buf;
|
|
Packit |
fd8b60 |
krb5_ucs4 ch;
|
|
Packit |
fd8b60 |
size_t chlen, i;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
*utf16_out = NULL;
|
|
Packit |
fd8b60 |
*nbytes_out = 0;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* UTF-16 conversion is used for RC4 string-to-key, so treat this data as
|
|
Packit |
fd8b60 |
* sensitive. */
|
|
Packit |
fd8b60 |
k5_buf_init_dynamic_zap(&buf;;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Examine next UTF-8 character. */
|
|
Packit |
fd8b60 |
while (*utf8 != '\0') {
|
|
Packit |
fd8b60 |
/* Get UTF-8 sequence length from first byte. */
|
|
Packit |
fd8b60 |
chlen = KRB5_UTF8_CHARLEN2(utf8, chlen);
|
|
Packit |
fd8b60 |
if (chlen == 0)
|
|
Packit |
fd8b60 |
goto invalid;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* First byte minus length tag */
|
|
Packit |
fd8b60 |
ch = (krb5_ucs4)(utf8[0] & mask[chlen]);
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
for (i = 1; i < chlen; i++) {
|
|
Packit |
fd8b60 |
/* Subsequent bytes must start with 10. */
|
|
Packit |
fd8b60 |
if ((utf8[i] & 0xc0) != 0x80)
|
|
Packit |
fd8b60 |
goto invalid;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* 6 bits of data in each subsequent byte */
|
|
Packit |
fd8b60 |
ch <<= 6;
|
|
Packit |
fd8b60 |
ch |= (krb5_ucs4)(utf8[i] & 0x3f);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
if (!IS_VALID_UNICODE(ch))
|
|
Packit |
fd8b60 |
goto invalid;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Characters in the basic multilingual plane are encoded using two
|
|
Packit |
fd8b60 |
* bytes; other characters are encoded using four bytes. */
|
|
Packit |
fd8b60 |
if (IS_BMP(ch)) {
|
|
Packit |
fd8b60 |
k5_buf_add_uint16_le(&buf, ch);
|
|
Packit |
fd8b60 |
} else {
|
|
Packit |
fd8b60 |
/* 0x10000 is subtracted from ch; then the high ten bits plus
|
|
Packit |
fd8b60 |
* 0xD800 and the low ten bits plus 0xDC00 are the surrogates. */
|
|
Packit |
fd8b60 |
k5_buf_add_uint16_le(&buf, HIGH_SURROGATE(ch));
|
|
Packit |
fd8b60 |
k5_buf_add_uint16_le(&buf, LOW_SURROGATE(ch));
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
/* Move to next UTF-8 character. */
|
|
Packit |
fd8b60 |
utf8 += chlen;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
*utf16_out = buf.data;
|
|
Packit |
fd8b60 |
*nbytes_out = buf.len;
|
|
Packit |
fd8b60 |
return 0;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
invalid:
|
|
Packit |
fd8b60 |
k5_buf_free(&buf;;
|
|
Packit |
fd8b60 |
return EINVAL;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
int
|
|
Packit |
fd8b60 |
k5_utf16le_to_utf8(const uint8_t *utf16bytes, size_t nbytes, char **utf8_out)
|
|
Packit |
fd8b60 |
{
|
|
Packit |
fd8b60 |
struct k5buf buf;
|
|
Packit |
fd8b60 |
struct k5input in;
|
|
Packit |
fd8b60 |
uint16_t ch1, ch2;
|
|
Packit |
fd8b60 |
krb5_ucs4 ch;
|
|
Packit |
fd8b60 |
size_t chlen;
|
|
Packit |
fd8b60 |
void *p;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
*utf8_out = NULL;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (nbytes % 2 != 0)
|
|
Packit |
fd8b60 |
return EINVAL;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
k5_buf_init_dynamic(&buf;;
|
|
Packit |
fd8b60 |
k5_input_init(&in, utf16bytes, nbytes);
|
|
Packit |
fd8b60 |
while (!in.status && in.len > 0) {
|
|
Packit |
fd8b60 |
/* Get the next character or high surrogate. A low surrogate without a
|
|
Packit |
fd8b60 |
* preceding high surrogate is invalid. */
|
|
Packit |
fd8b60 |
ch1 = k5_input_get_uint16_le(&in);
|
|
Packit |
fd8b60 |
if (IS_LOW_SURROGATE(ch1))
|
|
Packit |
fd8b60 |
goto invalid;
|
|
Packit |
fd8b60 |
if (IS_HIGH_SURROGATE(ch1)) {
|
|
Packit |
fd8b60 |
/* Get the low surrogate and combine the pair. */
|
|
Packit |
fd8b60 |
ch2 = k5_input_get_uint16_le(&in);
|
|
Packit |
fd8b60 |
if (!IS_LOW_SURROGATE(ch2))
|
|
Packit |
fd8b60 |
goto invalid;
|
|
Packit |
fd8b60 |
ch = COMPOSE(ch1, ch2);
|
|
Packit |
fd8b60 |
} else {
|
|
Packit |
fd8b60 |
ch = ch1;
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
chlen = krb5int_ucs4_to_utf8(ch, NULL);
|
|
Packit |
fd8b60 |
p = k5_buf_get_space(&buf, chlen);
|
|
Packit |
fd8b60 |
if (p == NULL)
|
|
Packit |
fd8b60 |
return ENOMEM;
|
|
Packit |
fd8b60 |
(void)krb5int_ucs4_to_utf8(ch, p);
|
|
Packit |
fd8b60 |
}
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
if (in.status)
|
|
Packit |
fd8b60 |
goto invalid;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
*utf8_out = buf.data;
|
|
Packit |
fd8b60 |
return 0;
|
|
Packit |
fd8b60 |
|
|
Packit |
fd8b60 |
invalid:
|
|
Packit |
fd8b60 |
k5_buf_free(&buf;;
|
|
Packit |
fd8b60 |
return EINVAL;
|
|
Packit |
fd8b60 |
}
|