LDAP_F (int) ldap_utf8_offset( const char * );
/* returns the length (in bytes) indicated by the UTF-8 character */
LDAP_F (int) ldap_utf8_charlen( const char * );
+
+/* returns the length (in bytes) indicated by the UTF-8 character
+ * also checks that shortest possible encoding was used
+ */
+LDAP_F (int) ldap_utf8_charlen2( const char * );
+
/* copies a UTF-8 character and returning number of bytes copied */
LDAP_F (int) ldap_utf8_copy( char *, const char *);
/* Optimizations */
LDAP_V (const char) ldap_utf8_lentab[128];
+LDAP_V (const char) ldap_utf8_mintab[32];
#define LDAP_UTF8_ISASCII(p) ( !(*(unsigned char *)(p) & 0x80 ) )
#define LDAP_UTF8_CHARLEN(p) ( LDAP_UTF8_ISASCII(p) \
? 1 : ldap_utf8_lentab[*(unsigned char *)(p) ^ 0x80] )
+
+/* This is like CHARLEN but additionally validates to make sure
+ * the char used the shortest possible encoding.
+ * 'l' is used to temporarily hold the result of CHARLEN.
+ */
+#define LDAP_UTF8_CHARLEN2(p, l) ( ( ( l = LDAP_UTF8_CHARLEN( p )) < 3 || \
+ ( ldap_utf8_mintab[*(unsigned char *)(p) & 0x1f] & (p)[1] ) ) ? \
+ l : 0 )
+
#define LDAP_UTF8_OFFSET(p) ( LDAP_UTF8_ISASCII(p) \
? 1 : ldap_utf8_offset((p)) )
continue;
}
- cl = LDAP_UTF8_CHARLEN( p );
+ cl = LDAP_UTF8_CHARLEN2( p, cl );
if ( cl == 0 ) {
/* illegal utf-8 char! */
return( -1 );
ber_len_t cnt;
for ( cnt = 1; cnt < cl; cnt++ ) {
- if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
+ if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
return( -1 );
}
}
utf8char = "";
/* Get UTF-8 sequence length from 1st byte */
- utflen = LDAP_UTF8_CHARLEN(utf8char);
+ utflen = LDAP_UTF8_CHARLEN2(utf8char, utflen);
if( utflen==0 || utflen > LDAP_MAX_UTF8_LEN )
return -1; /* Invalid input */
while ( *utf8str && (wcstr==NULL || wclen<count) )
{
/* Get UTF-8 sequence length from 1st byte */
- utflen = LDAP_UTF8_CHARLEN(utf8str);
+ utflen = LDAP_UTF8_CHARLEN2(utf8str, utflen);
if( utflen==0 || utflen > LDAP_MAX_UTF8_LEN )
return -1; /* Invalid input */
return ldap_utf8_lentab[*(unsigned char *)p ^ 0x80];
}
+/*
+ * Make sure the UTF-8 char used the shortest possible encoding
+ * returns charlen if valid, 0 if not.
+ */
+
+/* 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;
+}
+
/* conv UTF-8 to UCS-4, useful for comparisons */
ldap_ucs4_t ldap_x_utf8_to_ucs4( const char * p )
{
static unsigned char mask[] = {
0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 };
- len = LDAP_UTF8_CHARLEN(p);
+ len = LDAP_UTF8_CHARLEN2(p, len);
if( len == 0 ) return LDAP_UCS4_INVALID;