/* $OpenLDAP: pkg/ldap/libraries/liblunicode/ucdata/ucdata.c,v 1.36 2008/01/07 23:20:05 kurt Exp $ */
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
*
* Copyright 1998-2008 The OpenLDAP Foundation.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted only as authorized by the OpenLDAP
* Public License.
*
* A copy of this license is available in file LICENSE in the
* top-level directory of the distribution or, alternatively, at
*/
/* Copyright 2001 Computing Research Labs, New Mexico State University
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE COMPUTING RESEARCH LAB OR NEW MEXICO STATE UNIVERSITY BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* $Id: ucdata.c,v 1.4 2001/01/02 18:46:20 mleisher Exp $" */
#include "k5-int.h"
#include "k5-utf8.h"
#include "k5-unicode.h"
#include "ucdata.h"
#ifndef HARDCODE_DATA
#endif
#if HARDCODE_DATA
#include "uctable.h"
#endif
/**************************************************************************
*
* Miscellaneous types, data, and support functions.
*
**************************************************************************/
typedef struct {
union {
} size;
} _ucheader_t;
/*
* A simple array of 32-bit masks for lookup.
*/
0x00000001UL, 0x00000002UL, 0x00000004UL, 0x00000008UL,
0x00000010UL, 0x00000020UL, 0x00000040UL, 0x00000080UL,
0x00000100UL, 0x00000200UL, 0x00000400UL, 0x00000800UL,
0x00001000UL, 0x00002000UL, 0x00004000UL, 0x00008000UL,
0x00010000UL, 0x00020000UL, 0x00040000UL, 0x00080000UL,
0x00100000UL, 0x00200000UL, 0x00400000UL, 0x00800000UL,
0x01000000UL, 0x02000000UL, 0x04000000UL, 0x08000000UL,
0x10000000UL, 0x20000000UL, 0x40000000UL, 0x80000000UL
};
#if !HARDCODE_DATA
static FILE *
{
FILE *f;
return 0;
*pp++ = *LDAP_DIRSEP;
while (*fp)
*pp = 0;
return f;
if (*dp == ':')
dp++;
}
return 0;
}
#endif
/**************************************************************************
*
* Support for the character properties.
*
**************************************************************************/
#if !HARDCODE_DATA
/*
* Return -1 on error, 0 if okay
*/
static int
{
if (_ucprop_size > 0) {
if (!reload)
/*
* The character properties have already been loaded.
*/
return 0;
/*
* Unload the current character property data in preparation for
* loading a new copy. Only the first array has to be deallocated
* because all the memory for the arrays is allocated as a single
* block.
*/
free((char *) _ucprop_offsets);
_ucprop_size = 0;
}
return -1;
/*
* Load the header.
*/
}
return -1;
}
/*
* Allocate all the storage needed for the lookup table.
*/
/*
* Calculate the offset into the storage for the ranges. The offsets
* array is on a 4-byte boundary and one larger than the value provided in
* the header count field. This means the offset to the ranges must be
* calculated after aligning the count to a 4-byte boundary.
*/
size >>= 1;
/*
* Load the offset array.
*/
/*
* Do an endian swap if necessary. Don't forget there is an extra node on
* the end with the final index.
*/
for (i = 0; i <= _ucprop_size; i++)
}
/*
* Load the ranges. The number of elements is in the last array position
* of the offsets.
*/
/*
* Do an endian swap if necessary.
*/
for (i = 0; i < _ucprop_offsets[_ucprop_size]; i++)
}
return 0;
}
static void
_ucprop_unload(void)
{
if (_ucprop_size == 0)
return;
/*
* Only need to free the offsets because the memory is allocated as a
* single block.
*/
free((char *) _ucprop_offsets);
_ucprop_size = 0;
}
#endif
static int
{
long l, r, m;
if (_ucprop_size == 0)
return 0;
/*
* There is an extra node on the end of the offsets to allow this routine
* to work right. If the index is 0xffff, then there are no nodes for the
* property.
*/
if ((l = _ucprop_offsets[n]) == 0xffff)
return 0;
/*
* Locate the next offset that is not 0xffff. The sentinel at the end of
* the array is the max index value.
*/
for (m = 1;
r = _ucprop_offsets[n + m] - 1;
while (l <= r) {
/*
* Determine a "mid" point and adjust to make sure the mid point is at
* the beginning of a range pair.
*/
m = (l + r) >> 1;
m -= (m & 1);
l = m + 2;
else if (code < _ucprop_ranges[m])
r = m - 2;
return 1;
}
return 0;
}
int
{
krb5_ui_4 i;
return 0;
for (i = 0; mask1 && i < 32; i++) {
return 1;
}
return 1;
}
return 0;
}
/**************************************************************************
*
* Support for case mapping.
*
**************************************************************************/
#if !HARDCODE_DATA
/* These record the number of slots in the map.
* There are 3 words per slot.
*/
/*
* Return -1 on error, 0 if okay
*/
static int
{
krb5_ui_4 i;
if (_uccase_size > 0) {
if (!reload)
/*
* The case mappings have already been loaded.
*/
return 0;
free((char *) _uccase_map);
_uccase_size = 0;
}
return -1;
/*
* Load the header.
*/
}
/*
* Set the node count and lengths of the upper and lower case mapping
* tables.
*/
_uccase_map = (krb5_ui_4 *)
/*
* Load the case mapping table.
*/
/*
* Do an endian swap if necessary.
*/
for (i = 0; i < _uccase_size * 3; i++)
}
return 0;
}
static void
_uccase_unload(void)
{
if (_uccase_size == 0)
return;
free((char *) _uccase_map);
_uccase_size = 0;
}
#endif
static krb5_ui_4
{
long m;
/*
* Do the binary search.
*/
while (l <= r) {
/*
* Determine a "mid" point and adjust to make sure the mid point is at
* the beginning of a case mapping triple.
*/
m = (l + r) >> 1;
l = m + 1;
r = m - 1;
}
return code;
}
{
int field;
long l, r;
return code;
/*
* The character is lower case.
*/
field = 2;
l = _uccase_len[0];
} else {
/*
* The character is title case.
*/
field = 1;
r = _uccase_size - 1;
}
}
{
int field;
long l, r;
return code;
/*
* The character is upper case.
*/
field = 1;
l = 0;
r = _uccase_len[0] - 1;
} else {
/*
* The character is title case.
*/
field = 2;
r = _uccase_size - 1;
}
}
{
int field;
long l, r;
return code;
/*
* The offset will always be the same for converting to title case.
*/
field = 2;
/*
* The character is upper case.
*/
l = 0;
r = _uccase_len[0] - 1;
} else {
/*
* The character is lower case.
*/
l = _uccase_len[0];
}
}
/**************************************************************************
*
* Support for compositions.
*
**************************************************************************/
#if !HARDCODE_DATA
/*
* Return -1 on error, 0 if okay
*/
static int
{
if (_uccomp_size > 0) {
if (!reload)
/*
* The compositions have already been loaded.
*/
return 0;
free((char *) _uccomp_data);
_uccomp_size = 0;
}
return -1;
/*
* Load the header.
*/
}
/*
* Read the composition data in.
*/
/*
* Do an endian swap if necessary.
*/
for (i = 0; i < size; i++)
}
/*
* Assume that the data is ordered on count, so that all compositions
* of length 2 come first. Only handling length 2 for now.
*/
if (_uccomp_data[i] != 2)
break;
_uccomp_size = i - 1;
return 0;
}
static void
_uccomp_unload(void)
{
if (_uccomp_size == 0)
return;
free((char *) _uccomp_data);
_uccomp_size = 0;
}
#endif
int
{
int l, r, m;
l = 0;
r = _uccomp_size - 1;
while (l <= r) {
m = ((r + l) >> 1);
m -= m & 3;
l = m + 4;
r = m - 4;
l = m + 4;
r = m - 4;
else {
*comp = _uccomp_data[m];
return 1;
}
}
return 0;
}
int
{
int i, rlen;
rlen = 1;
for ( i = 1; i < len; i++ ) {
/* check if two current characters are L and V */
/* make syllable of form LV */
continue;
}
}
/* check if two current characters are LV and T */
{
/* make syllable of form LVT */
continue;
}
}
/* if neither case was true, just add the character */
rlen++;
}
return rlen;
}
int
{
stpos = 0;
copos = 1;
for (i = 1; i < len; i++) {
else {
if (cl == 0) {
}
}
}
}
/**************************************************************************
*
* Support for decompositions.
*
**************************************************************************/
#if !HARDCODE_DATA
/*
* Return -1 on error, 0 if okay
*/
static int
{
if (_ucdcmp_size > 0) {
if (!reload)
/*
* The decompositions have already been loaded.
*/
return 0;
free((char *) _ucdcmp_nodes);
_ucdcmp_size = 0;
}
return -1;
/*
* Load the header.
*/
}
/*
* Read the decomposition data in.
*/
/*
* Do an endian swap if necessary.
*/
for (i = 0; i < size; i++)
}
return 0;
}
/*
* Return -1 on error, 0 if okay
*/
static int
{
if (_uckdcmp_size > 0) {
if (!reload)
/*
* The decompositions have already been loaded.
*/
return 0;
free((char *) _uckdcmp_nodes);
_uckdcmp_size = 0;
}
return -1;
/*
* Load the header.
*/
}
/*
* Read the decomposition data in.
*/
/*
* Do an endian swap if necessary.
*/
for (i = 0; i < size; i++)
}
return 0;
}
static void
_ucdcmp_unload(void)
{
if (_ucdcmp_size == 0)
return;
/*
* Only need to free the offsets because the memory is allocated as a
* single block.
*/
free((char *) _ucdcmp_nodes);
_ucdcmp_size = 0;
}
static void
_uckdcmp_unload(void)
{
if (_uckdcmp_size == 0)
return;
/*
* Only need to free the offsets because the memory is allocated as a
* single block.
*/
free((char *) _uckdcmp_nodes);
_uckdcmp_size = 0;
}
#endif
int
{
long l, r, m;
if (code < _ucdcmp_nodes[0]) {
return 0;
}
l = 0;
while (l <= r) {
/*
* Determine a "mid" point and adjust to make sure the mid point is at
* the beginning of a code+offset pair.
*/
m = (l + r) >> 1;
m -= (m & 1);
if (code > _ucdcmp_nodes[m])
l = m + 2;
else if (code < _ucdcmp_nodes[m])
r = m - 2;
else if (code == _ucdcmp_nodes[m]) {
return 1;
}
}
return 0;
}
int
{
long l, r, m;
if (code < _uckdcmp_nodes[0]) {
return 0;
}
l = 0;
while (l <= r) {
/*
* Determine a "mid" point and adjust to make sure the mid point is at
* the beginning of a code+offset pair.
*/
m = (l + r) >> 1;
m -= (m & 1);
if (code > _uckdcmp_nodes[m])
l = m + 2;
else if (code < _uckdcmp_nodes[m])
r = m - 2;
else if (code == _uckdcmp_nodes[m]) {
return 1;
}
}
return 0;
}
int
{
if (!ucishangul(code))
return 0;
code -= 0xac00;
return 1;
}
/* mode == 0 for canonical, mode == 1 for compatibility */
static int
{
int l, size;
unsigned i, j, k;
return *outlen = -1;
i = 0;
for (j = 0; j < (unsigned) inlen; j++) {
return *outlen = -1;
}
for (k = 0; k < num; k++) {
if (class == 0) {
} else {
for (l = i; l > 0; l--)
break;
}
i++;
}
return *outlen = -1;
}
for (k = 0; k < num; k++) {
(*out)[i] = hangdecomp[k];
i++;
}
} else {
if (size - i < 1) {
return *outlen = -1;
}
if (class == 0) {
} else {
for (l = i; l > 0; l--)
break;
}
i++;
}
}
return *outlen = i;
}
int
{
}
int
{
}
/**************************************************************************
*
* Support for combining classes.
*
**************************************************************************/
#if !HARDCODE_DATA
/*
* Return -1 on error, 0 if okay
*/
static int
{
krb5_ui_4 i;
if (_uccmcl_size > 0) {
if (!reload)
/*
* The combining classes have already been loaded.
*/
return 0;
free((char *) _uccmcl_nodes);
_uccmcl_size = 0;
}
return -1;
/*
* Load the header.
*/
}
/*
* Read the combining classes in.
*/
/*
* Do an endian swap if necessary.
*/
for (i = 0; i < _uccmcl_size; i++)
}
return 0;
}
static void
_uccmcl_unload(void)
{
if (_uccmcl_size == 0)
return;
free((char *) _uccmcl_nodes);
_uccmcl_size = 0;
}
#endif
{
long l, r, m;
l = 0;
r = _uccmcl_size - 1;
while (l <= r) {
m = (l + r) >> 1;
m -= (m % 3);
l = m + 3;
else if (code < _uccmcl_nodes[m])
r = m - 3;
return _uccmcl_nodes[m + 2];
}
return 0;
}
/**************************************************************************
*
* Support for numeric values.
*
**************************************************************************/
#if !HARDCODE_DATA
static short *_ucnum_vals;
/*
* Return -1 on error, 0 if okay
*/
static int
{
if (_ucnum_size > 0) {
if (!reload)
/*
* The numbers have already been loaded.
*/
return 0;
free((char *) _ucnum_nodes);
_ucnum_size = 0;
}
return -1;
/*
* Load the header.
*/
}
/*
* Read the combining classes in.
*/
/*
* Do an endian swap if necessary.
*/
for (i = 0; i < _ucnum_size; i++)
/*
* Determine the number of values that have to be adjusted.
*/
sizeof(short);
for (i = 0; i < size; i++)
}
return 0;
}
static void
_ucnumb_unload(void)
{
if (_ucnum_size == 0)
return;
free((char *) _ucnum_nodes);
_ucnum_size = 0;
}
#endif
int
{
long l, r, m;
short *vp;
l = 0;
r = _ucnum_size - 1;
while (l <= r) {
/*
* Determine a "mid" point and adjust to make sure the mid point is at
* the beginning of a code+offset pair.
*/
m = (l + r) >> 1;
m -= (m & 1);
if (code > _ucnum_nodes[m])
l = m + 2;
else if (code < _ucnum_nodes[m])
r = m - 2;
else {
return 1;
}
}
return 0;
}
int
{
long l, r, m;
short *vp;
l = 0;
r = _ucnum_size - 1;
while (l <= r) {
/*
* Determine a "mid" point and adjust to make sure the mid point is at
* the beginning of a code+offset pair.
*/
m = (l + r) >> 1;
m -= (m & 1);
if (code > _ucnum_nodes[m])
l = m + 2;
else if (code < _ucnum_nodes[m])
r = m - 2;
else {
return 1;
}
return 0;
}
}
return 0;
}
struct ucnumber
{
/*
* Initialize with some arbitrary value, because the caller simply cannot
* tell for sure if the code is a number without calling the ucisnumber()
* macro before calling this function.
*/
return num;
}
int
{
int dig;
/*
* Initialize with some arbitrary value, because the caller simply cannot
* tell for sure if the code is a number without calling the ucisdigit()
* macro before calling this function.
*/
dig = -111;
return dig;
}
/**************************************************************************
*
* Setup and cleanup routines.
*
**************************************************************************/
#if HARDCODE_DATA
#else
/*
* Return 0 if okay, negative on error
*/
int
{
int error = 0;
if (masks & UCDATA_CTYPE)
if (masks & UCDATA_CASE)
if (masks & UCDATA_DECOMP)
if (masks & UCDATA_CMBCL)
if (masks & UCDATA_NUM)
if (masks & UCDATA_COMP)
if (masks & UCDATA_KDECOMP)
return -error;
}
void
{
if (masks & UCDATA_CTYPE)
if (masks & UCDATA_CASE)
if (masks & UCDATA_DECOMP)
if (masks & UCDATA_CMBCL)
if (masks & UCDATA_NUM)
if (masks & UCDATA_COMP)
if (masks & UCDATA_KDECOMP)
}
/*
* Return 0 if okay, negative on error
*/
int
{
int error = 0;
if (masks & UCDATA_CTYPE)
if (masks & UCDATA_CASE)
if (masks & UCDATA_DECOMP)
if (masks & UCDATA_CMBCL)
if (masks & UCDATA_NUM)
if (masks & UCDATA_COMP)
if (masks & UCDATA_KDECOMP)
return -error;
}
#endif
#ifdef TEST
void
main(void)
{
int dig;
/* ucdata_setup("."); */
if (ucisweak(0x30))
printf("WEAK\n");
else
printf("NOT WEAK\n");
if (ucisalpha(0x1d5))
printf("ALPHA\n");
else
printf("NOT ALPHA\n");
if (ucisupper(0x1d5)) {
printf("UPPER\n");
} else
printf("NOT UPPER\n");
if (ucistitle(0x1d5))
printf("TITLE\n");
else
printf("NOT TITLE\n");
if (uciscomposite(0x1d5))
printf("COMPOSITE\n");
else
printf("NOT COMPOSITE\n");
for (i = 0; i < lo; i++)
putchar('\n');
}
if (ucisxdigit(0xfeff))
printf("0xFEFF HEX DIGIT\n");
else
printf("0xFEFF NOT HEX DIGIT\n");
if (ucisdefined(0x10000))
printf("0x10000 DEFINED\n");
else
printf("0x10000 NOT DEFINED\n");
else
} else
printf("UCNUMBER: 0x30 NOT A NUMBER\n");
else
} else
printf("UCNUMBER: 0xbc NOT A NUMBER\n");
else
} else
printf("UCNUMBER: 0xff19 NOT A NUMBER\n");
else
} else
printf("UCNUMBER: 0x4e00 NOT A NUMBER\n");
else
printf("UCDIGIT: 0x6f9 NOT A NUMBER\n");
else
else
else
/* ucdata_cleanup(); */
exit(0);
}
#endif /* TEST */