2000-01-22 09:55:34 +08:00
|
|
|
/* $OpenLDAP$ */
|
|
|
|
/*
|
2002-01-05 05:17:25 +08:00
|
|
|
* Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
|
2000-01-22 09:55:34 +08:00
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Basic UTF-8 routines
|
|
|
|
*
|
2000-01-23 10:39:55 +08:00
|
|
|
* These routines are "dumb". Though they understand UTF-8,
|
|
|
|
* they don't grok Unicode. That is, they can push bits,
|
|
|
|
* but don't have a clue what the bits represent. That's
|
|
|
|
* good enough for use with the LDAP Client SDK.
|
|
|
|
*
|
2000-01-22 09:55:34 +08:00
|
|
|
* These routines are not optimized.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
|
|
|
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/time.h>
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
#include "ldap_utf8.h"
|
|
|
|
|
2000-01-22 09:55:34 +08:00
|
|
|
#include "ldap-int.h"
|
|
|
|
#include "ldap_defaults.h"
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
/*
|
|
|
|
* Basic UTF-8 routines
|
|
|
|
*/
|
|
|
|
|
2000-01-23 02:48:37 +08:00
|
|
|
/*
|
|
|
|
* return the number of bytes required to hold the
|
2000-01-24 07:33:01 +08:00
|
|
|
* NULL-terminated UTF-8 string NOT INCLUDING the
|
2000-01-23 02:48:37 +08:00
|
|
|
* termination.
|
|
|
|
*/
|
2000-01-22 09:55:34 +08:00
|
|
|
ber_len_t ldap_utf8_bytes( const char * p )
|
|
|
|
{
|
2000-01-24 07:33:01 +08:00
|
|
|
ber_len_t bytes;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2000-01-24 07:33:01 +08:00
|
|
|
for( bytes=0; p[bytes]; bytes++ ) {
|
2000-01-22 09:55:34 +08:00
|
|
|
/* EMPTY */ ;
|
|
|
|
}
|
|
|
|
|
2000-01-23 04:55:43 +08:00
|
|
|
return bytes;
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ber_len_t ldap_utf8_chars( const char * p )
|
|
|
|
{
|
2000-01-22 11:40:54 +08:00
|
|
|
/* could be optimized and could check for invalid sequences */
|
2000-01-24 02:43:30 +08:00
|
|
|
ber_len_t chars=0;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2000-01-24 07:07:24 +08:00
|
|
|
for( ; *p ; LDAP_UTF8_INCR(p) ) {
|
2000-01-24 02:43:30 +08:00
|
|
|
chars++;
|
2001-12-20 05:23:11 +08:00
|
|
|
}
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2000-01-22 11:40:54 +08:00
|
|
|
return chars;
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
2000-01-24 07:07:24 +08:00
|
|
|
/* return offset to next character */
|
|
|
|
int ldap_utf8_offset( const char * p )
|
|
|
|
{
|
|
|
|
return LDAP_UTF8_NEXT(p) - p;
|
|
|
|
}
|
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
/*
|
|
|
|
* Returns length indicated by first byte.
|
|
|
|
*/
|
2001-12-29 10:57:32 +08:00
|
|
|
const char ldap_utf8_lentab[] = {
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
|
|
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
2002-01-15 11:31:37 +08:00
|
|
|
0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
2001-12-29 10:57:32 +08:00
|
|
|
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
|
|
|
|
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
|
|
|
|
4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 0, 0 };
|
|
|
|
|
2000-01-22 09:55:34 +08:00
|
|
|
int ldap_utf8_charlen( const char * p )
|
|
|
|
{
|
2001-12-29 10:57:32 +08:00
|
|
|
if (!(*p & 0x80))
|
|
|
|
return 1;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2001-12-29 10:57:32 +08:00
|
|
|
return ldap_utf8_lentab[*(unsigned char *)p ^ 0x80];
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
2002-01-15 16:07:46 +08:00
|
|
|
/*
|
|
|
|
* Make sure the UTF-8 char used the shortest possible encoding
|
|
|
|
* returns charlen if valid, 0 if not.
|
2002-01-15 17:01:44 +08:00
|
|
|
*
|
|
|
|
* Here are the valid UTF-8 encodings, taken from RFC 2279 page 4.
|
|
|
|
* The table is slightly modified from that of the RFC.
|
|
|
|
*
|
|
|
|
* UCS-4 range (hex) UTF-8 sequence (binary)
|
|
|
|
* 0000 0000-0000 007F 0.......
|
|
|
|
* 0000 0080-0000 07FF 110++++. 10......
|
|
|
|
* 0000 0800-0000 FFFF 1110++++ 10+..... 10......
|
|
|
|
* 0001 0000-001F FFFF 11110+++ 10++.... 10...... 10......
|
|
|
|
* 0020 0000-03FF FFFF 111110++ 10+++... 10...... 10...... 10......
|
|
|
|
* 0400 0000-7FFF FFFF 1111110+ 10++++.. 10...... 10...... 10...... 10......
|
|
|
|
*
|
|
|
|
* The '.' bits are "don't cares". When validating a UTF-8 sequence,
|
|
|
|
* at least one of the '+' bits must be set, otherwise the character
|
|
|
|
* should have been encoded in fewer octets. Note that in the two-octet
|
|
|
|
* case, only the first octet needs to be validated, and this is done
|
|
|
|
* in the ldap_utf8_lentab[] above.
|
2002-01-15 16:07:46 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* mask of required bits in second octet */
|
|
|
|
const char ldap_utf8_mintab[] = {
|
|
|
|
0x20, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
|
|
|
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
|
|
|
0x30, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
|
|
|
|
0x38, 0x80, 0x80, 0x80, 0x3c, 0x80, 0x00, 0x00 };
|
|
|
|
|
|
|
|
int ldap_utf8_charlen2( const char * p )
|
|
|
|
{
|
|
|
|
int i = LDAP_UTF8_CHARLEN( p );
|
|
|
|
|
|
|
|
if ( i > 2 ) {
|
|
|
|
if ( !( ldap_utf8_mintab[*p & 0x1f] & p[1] ) )
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
/* conv UTF-8 to UCS-4, useful for comparisons */
|
2001-07-22 07:13:04 +08:00
|
|
|
ldap_ucs4_t ldap_x_utf8_to_ucs4( const char * p )
|
2000-01-23 02:48:37 +08:00
|
|
|
{
|
2000-01-24 02:43:30 +08:00
|
|
|
const unsigned char *c = p;
|
2000-01-27 09:04:17 +08:00
|
|
|
ldap_ucs4_t ch;
|
2000-01-23 02:48:37 +08:00
|
|
|
int len, i;
|
2000-01-24 02:43:30 +08:00
|
|
|
static unsigned char mask[] = {
|
2000-01-24 07:07:24 +08:00
|
|
|
0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
|
2000-01-23 02:48:37 +08:00
|
|
|
|
2002-01-15 16:07:46 +08:00
|
|
|
len = LDAP_UTF8_CHARLEN2(p, len);
|
2000-01-23 02:48:37 +08:00
|
|
|
|
2000-09-04 07:48:35 +08:00
|
|
|
if( len == 0 ) return LDAP_UCS4_INVALID;
|
2000-01-23 02:48:37 +08:00
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
ch = c[0] & mask[len];
|
2000-01-23 02:48:37 +08:00
|
|
|
|
|
|
|
for(i=1; i < len; i++) {
|
2000-01-24 02:43:30 +08:00
|
|
|
if ((c[i] & 0xc0) != 0x80) {
|
2000-09-04 07:48:35 +08:00
|
|
|
return LDAP_UCS4_INVALID;
|
2000-01-23 02:48:37 +08:00
|
|
|
}
|
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
ch <<= 6;
|
|
|
|
ch |= c[i] & 0x3f;
|
2000-01-23 02:48:37 +08:00
|
|
|
}
|
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
return ch;
|
2000-01-23 02:48:37 +08:00
|
|
|
}
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
/* conv UCS-4 to UTF-8, not used */
|
2001-07-22 07:13:04 +08:00
|
|
|
int ldap_x_ucs4_to_utf8( ldap_ucs4_t c, char *buf )
|
2000-01-23 02:48:37 +08:00
|
|
|
{
|
|
|
|
int len=0;
|
|
|
|
unsigned char* p = buf;
|
|
|
|
if(buf == NULL) return 0;
|
|
|
|
|
|
|
|
if ( c < 0 ) {
|
|
|
|
/* not a valid Unicode character */
|
|
|
|
|
|
|
|
} else if( c < 0x80 ) {
|
|
|
|
p[len++] = c;
|
|
|
|
|
|
|
|
} else if( c < 0x800 ) {
|
|
|
|
p[len++] = 0xc0 | ( c >> 6 );
|
2000-01-24 07:07:24 +08:00
|
|
|
p[len++] = 0x80 | ( c & 0x3f );
|
2000-01-23 02:48:37 +08:00
|
|
|
|
|
|
|
} else if( c < 0x10000 ) {
|
|
|
|
p[len++] = 0xe0 | ( c >> 12 );
|
2000-01-24 07:07:24 +08:00
|
|
|
p[len++] = 0x80 | ( (c >> 6) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( c & 0x3f );
|
2000-01-23 02:48:37 +08:00
|
|
|
|
|
|
|
} else if( c < 0x200000 ) {
|
|
|
|
p[len++] = 0xf0 | ( c >> 18 );
|
2000-01-24 07:07:24 +08:00
|
|
|
p[len++] = 0x80 | ( (c >> 12) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( (c >> 6) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( c & 0x3f );
|
2000-01-23 02:48:37 +08:00
|
|
|
|
2000-10-28 05:32:46 +08:00
|
|
|
} else if( c < 0x4000000 ) {
|
2000-01-23 02:48:37 +08:00
|
|
|
p[len++] = 0xf8 | ( c >> 24 );
|
2000-01-24 07:07:24 +08:00
|
|
|
p[len++] = 0x80 | ( (c >> 18) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( (c >> 12) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( (c >> 6) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( c & 0x3f );
|
2000-01-23 02:48:37 +08:00
|
|
|
|
|
|
|
} else /* if( c < 0x80000000 ) */ {
|
|
|
|
p[len++] = 0xfc | ( c >> 30 );
|
2000-01-24 07:07:24 +08:00
|
|
|
p[len++] = 0x80 | ( (c >> 24) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( (c >> 18) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( (c >> 12) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( (c >> 6) & 0x3f );
|
|
|
|
p[len++] = 0x80 | ( c & 0x3f );
|
2000-01-23 02:48:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
buf[len] = '\0';
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
/*
|
|
|
|
* Advance to the next UTF-8 character
|
|
|
|
*
|
|
|
|
* Ignores length of multibyte character, instead rely on
|
|
|
|
* continuation markers to find start of next character.
|
|
|
|
* This allows for "resyncing" of when invalid characters
|
|
|
|
* are provided provided the start of the next character
|
|
|
|
* is appears within the 6 bytes examined.
|
|
|
|
*/
|
2000-01-23 02:48:37 +08:00
|
|
|
char* ldap_utf8_next( const char * p )
|
2000-01-22 09:55:34 +08:00
|
|
|
{
|
2000-01-24 02:43:30 +08:00
|
|
|
int i;
|
|
|
|
const unsigned char *u = p;
|
|
|
|
|
|
|
|
if( LDAP_UTF8_ISASCII(u) ) {
|
|
|
|
return (char *) &p[1];
|
|
|
|
}
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
for( i=1; i<6; i++ ) {
|
2000-06-09 14:48:43 +08:00
|
|
|
if ( ( u[i] & 0xc0 ) != 0x80 ) {
|
2000-01-24 02:43:30 +08:00
|
|
|
return (char *) &p[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (char *) &p[i];
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
/*
|
|
|
|
* Advance to the previous UTF-8 character
|
|
|
|
*
|
|
|
|
* Ignores length of multibyte character, instead rely on
|
|
|
|
* continuation markers to find start of next character.
|
|
|
|
* This allows for "resyncing" of when invalid characters
|
|
|
|
* are provided provided the start of the next character
|
|
|
|
* is appears within the 6 bytes examined.
|
|
|
|
*/
|
2000-01-23 02:48:37 +08:00
|
|
|
char* ldap_utf8_prev( const char * p )
|
2000-01-22 09:55:34 +08:00
|
|
|
{
|
|
|
|
int i;
|
2000-01-23 02:48:37 +08:00
|
|
|
const unsigned char *u = p;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
for( i=-1; i>-6 ; i-- ) {
|
2000-06-09 14:48:43 +08:00
|
|
|
if ( ( u[i] & 0xc0 ) != 0x80 ) {
|
2000-01-24 02:43:30 +08:00
|
|
|
return (char *) &p[i];
|
|
|
|
}
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
return (char *) &p[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy one UTF-8 character from src to dst returning
|
|
|
|
* number of bytes copied.
|
|
|
|
*
|
|
|
|
* Ignores length of multibyte character, instead rely on
|
|
|
|
* continuation markers to find start of next character.
|
|
|
|
* This allows for "resyncing" of when invalid characters
|
|
|
|
* are provided provided the start of the next character
|
|
|
|
* is appears within the 6 bytes examined.
|
|
|
|
*/
|
|
|
|
int ldap_utf8_copy( char* dst, const char *src )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const unsigned char *u = src;
|
|
|
|
|
|
|
|
dst[0] = src[0];
|
|
|
|
|
|
|
|
if( LDAP_UTF8_ISASCII(u) ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i=1; i<6; i++ ) {
|
2000-06-09 14:48:43 +08:00
|
|
|
if ( ( u[i] & 0xc0 ) != 0x80 ) {
|
2000-01-24 02:43:30 +08:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
dst[i] = src[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
#ifndef UTF8_ALPHA_CTYPE
|
2000-01-23 10:39:55 +08:00
|
|
|
/*
|
|
|
|
* UTF-8 ctype routines
|
2000-10-24 02:04:45 +08:00
|
|
|
* Only deals with characters < 0x80 (ie: US-ASCII)
|
2000-01-23 10:39:55 +08:00
|
|
|
*/
|
|
|
|
|
2000-01-22 09:55:34 +08:00
|
|
|
int ldap_utf8_isascii( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
2002-01-03 01:06:56 +08:00
|
|
|
return LDAP_ASCII(c);
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ldap_utf8_isdigit( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
if(!LDAP_ASCII(c)) return 0;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
return LDAP_DIGIT( c );
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ldap_utf8_isxdigit( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
if(!LDAP_ASCII(c)) return 0;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
return LDAP_HEX(c);
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
int ldap_utf8_isspace( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
if(!LDAP_ASCII(c)) return 0;
|
2000-01-23 10:39:55 +08:00
|
|
|
|
|
|
|
switch(c) {
|
|
|
|
case ' ':
|
|
|
|
case '\t':
|
|
|
|
case '\n':
|
|
|
|
case '\r':
|
|
|
|
case '\v':
|
|
|
|
case '\f':
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* These are not needed by the C SDK and are
|
|
|
|
* not "good enough" for general use.
|
|
|
|
*/
|
2000-01-22 09:55:34 +08:00
|
|
|
int ldap_utf8_isalpha( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
if(!LDAP_ASCII(c)) return 0;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
return LDAP_ALPHA(c);
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ldap_utf8_isalnum( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
if(!LDAP_ASCII(c)) return 0;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
return LDAP_ALNUM(c);
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ldap_utf8_islower( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
if(!LDAP_ASCII(c)) return 0;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
return LDAP_LOWER(c);
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int ldap_utf8_isupper( const char * p )
|
|
|
|
{
|
|
|
|
unsigned c = * (const unsigned char *) p;
|
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
if(!LDAP_ASCII(c)) return 0;
|
2000-01-22 09:55:34 +08:00
|
|
|
|
2002-01-03 01:06:56 +08:00
|
|
|
return LDAP_UPPER(c);
|
2000-01-22 09:55:34 +08:00
|
|
|
}
|
2000-01-23 13:35:38 +08:00
|
|
|
#endif
|
2000-01-23 04:55:43 +08:00
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* UTF-8 string routines
|
|
|
|
*/
|
|
|
|
|
2000-01-24 07:07:24 +08:00
|
|
|
/* like strchr() */
|
|
|
|
char * (ldap_utf8_strchr)( const char *str, const char *chr )
|
|
|
|
{
|
|
|
|
for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
|
2001-07-22 07:13:04 +08:00
|
|
|
if( ldap_x_utf8_to_ucs4( str ) == ldap_x_utf8_to_ucs4( chr ) ) {
|
2000-01-24 07:07:24 +08:00
|
|
|
return (char *) str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
/* like strcspn() but returns number of bytes, not characters */
|
2000-01-23 04:55:43 +08:00
|
|
|
ber_len_t (ldap_utf8_strcspn)( const char *str, const char *set )
|
|
|
|
{
|
|
|
|
const char *cstr;
|
2000-01-24 07:07:24 +08:00
|
|
|
const char *cset;
|
2000-01-23 04:55:43 +08:00
|
|
|
|
2000-01-24 07:07:24 +08:00
|
|
|
for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
|
|
|
|
for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
|
2001-07-22 07:13:04 +08:00
|
|
|
if( ldap_x_utf8_to_ucs4( cstr ) == ldap_x_utf8_to_ucs4( cset ) ) {
|
2000-01-23 04:55:43 +08:00
|
|
|
return cstr - str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cstr - str;
|
|
|
|
}
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
/* like strspn() but returns number of bytes, not characters */
|
2000-01-23 04:55:43 +08:00
|
|
|
ber_len_t (ldap_utf8_strspn)( const char *str, const char *set )
|
|
|
|
{
|
|
|
|
const char *cstr;
|
2000-01-24 07:07:24 +08:00
|
|
|
const char *cset;
|
2000-01-23 04:55:43 +08:00
|
|
|
|
2000-01-24 02:43:30 +08:00
|
|
|
for( cstr = str; *cstr != '\0'; LDAP_UTF8_INCR(cstr) ) {
|
|
|
|
for( cset = set; ; LDAP_UTF8_INCR(cset) ) {
|
2000-01-23 04:55:43 +08:00
|
|
|
if( *cset == '\0' ) {
|
|
|
|
return cstr - str;
|
|
|
|
}
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
if( ldap_x_utf8_to_ucs4( cstr ) == ldap_x_utf8_to_ucs4( cset ) ) {
|
2000-01-23 04:55:43 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cstr - str;
|
|
|
|
}
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
/* like strpbrk(), replaces strchr() as well */
|
2000-01-23 05:03:21 +08:00
|
|
|
char *(ldap_utf8_strpbrk)( const char *str, const char *set )
|
|
|
|
{
|
2000-01-24 07:07:24 +08:00
|
|
|
for( ; *str != '\0'; LDAP_UTF8_INCR(str) ) {
|
2000-01-23 05:03:21 +08:00
|
|
|
const char *cset;
|
|
|
|
|
2000-01-24 07:07:24 +08:00
|
|
|
for( cset = set; *cset != '\0'; LDAP_UTF8_INCR(cset) ) {
|
2001-07-22 07:13:04 +08:00
|
|
|
if( ldap_x_utf8_to_ucs4( str ) == ldap_x_utf8_to_ucs4( cset ) ) {
|
2000-01-24 07:07:24 +08:00
|
|
|
return (char *) str;
|
2000-01-23 05:03:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-23 10:39:55 +08:00
|
|
|
/* like strtok_r(), not strtok() */
|
2000-01-23 04:55:43 +08:00
|
|
|
char *(ldap_utf8_strtok)(char *str, const char *sep, char **last)
|
|
|
|
{
|
|
|
|
char *begin;
|
|
|
|
char *end;
|
|
|
|
|
|
|
|
if( last == NULL ) return NULL;
|
|
|
|
|
|
|
|
begin = str ? str : *last;
|
|
|
|
|
|
|
|
begin += ldap_utf8_strspn( begin, sep );
|
|
|
|
|
|
|
|
if( *begin == '\0' ) {
|
|
|
|
*last = NULL;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-01-24 07:07:24 +08:00
|
|
|
end = &begin[ ldap_utf8_strcspn( begin, sep ) ];
|
2000-01-23 04:55:43 +08:00
|
|
|
|
|
|
|
if( *end != '\0' ) {
|
2000-01-24 02:43:30 +08:00
|
|
|
char *next = LDAP_UTF8_NEXT( end );
|
2000-01-23 04:55:43 +08:00
|
|
|
*end = '\0';
|
2000-01-24 02:43:30 +08:00
|
|
|
end = next;
|
2000-01-23 04:55:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
*last = end;
|
|
|
|
return begin;
|
|
|
|
}
|