/* $OpenLDAP$ */
-/*
- * Copyright 2000-2002 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2007 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 the file LICENSE in the
+ * top-level directory of the distribution or, alternatively, at
+ * <http://www.OpenLDAP.org/license.html>.
*/
-
-/* $Novell: /ldap/src/cldap/libraries/libldap/utfconv.c,v 1.3 2000/12/11 19:35:37 dsteck Exp $ */
-/******************************************************************************
- * Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
+/* Portions Copyright (C) 1999, 2000 Novell, Inc. All Rights Reserved.
*
* THIS WORK IS SUBJECT TO U.S. AND INTERNATIONAL COPYRIGHT LAWS AND
* TREATIES. USE, MODIFICATION, AND REDISTRIBUTION OF THIS WORK IS SUBJECT
* OF THIS WORK OTHER THAN AS AUTHORIZED IN VERSION 2.0.1 OF THE OPENLDAP
* PUBLIC LICENSE, OR OTHER PRIOR WRITTEN CONSENT FROM NOVELL, COULD SUBJECT
* THE PERPETRATOR TO CRIMINAL AND CIVIL LIABILITY.
- ******************************************************************************/
+ *---
+ * Note: A verbatim copy of version 2.0.1 of the OpenLDAP Public License
+ * can be found in the file "build/LICENSE-2.0.1" in this distribution
+ * of OpenLDAP Software.
+ */
/*
* UTF-8 Conversion Routines
#include "portable.h"
+#if SIZEOF_WCHAR_T >= 4
+/* These routines assume ( sizeof(wchar_t) >= 4 ) */
+
#include <stdio.h>
#include <ac/stdlib.h> /* For wctomb, wcstombs, mbtowc, mbstowcs */
#include <ac/string.h>
Unicode address space (0 - 0x10FFFF) 21 bits
ISO-10646 address space (0 - 0x7FFFFFFF) 31 bits
-Note: This code does not prevent UTF-8 sequences which are longer than
- necessary from being decoded.
+Note: This code does not prevent UTF-8 sequences which are longer than
+ necessary from being decoded.
*/
/*-----------------------------------------------------------------------------
int utflen, i;
wchar_t ch;
- /* If input ptr is NULL, treat it as empty string. */
- if (utf8char == NULL)
- utf8char = "";
+ if (utf8char == NULL) return -1;
/* 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 */
+ if( utflen==0 || utflen > (int)LDAP_MAX_UTF8_LEN ) return -1;
/* First byte minus length tag */
ch = (wchar_t)(utf8char[0] & mask[utflen]);
- for(i=1; i < utflen; i++)
- {
+ for(i=1; i < utflen; i++) {
/* Subsequent bytes must start with 10 */
- if ((utf8char[i] & 0xc0) != 0x80)
- return -1;
+ if ((utf8char[i] & 0xc0) != 0x80) return -1;
ch <<= 6; /* 6 bits of data in each subsequent byte */
ch |= (wchar_t)(utf8char[i] & 0x3f);
}
- if (wchar)
- *wchar = ch;
+ if (wchar) *wchar = ch;
return utflen;
}
wchar_t ch;
- /* If input ptr is NULL, treat it as empty string. */
- if (utf8str == NULL)
- utf8str = "";
+ /* If input ptr is NULL or empty... */
+ if (utf8str == NULL || !*utf8str) {
+ if ( wcstr )
+ *wcstr = 0;
+ return 0;
+ }
/* Examine next UTF-8 character. If output buffer is NULL, ignore count */
- while ( *utf8str && (wcstr==NULL || wclen<count) )
- {
+ 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 */
+ if( utflen==0 || utflen > (int)LDAP_MAX_UTF8_LEN ) return -1;
/* First byte minus length tag */
ch = (wchar_t)(utf8str[0] & mask[utflen]);
- for(i=1; i < utflen; i++)
- {
+ for(i=1; i < utflen; i++) {
/* Subsequent bytes must start with 10 */
- if ((utf8str[i] & 0xc0) != 0x80)
- return -1;
+ if ((utf8str[i] & 0xc0) != 0x80) return -1;
ch <<= 6; /* 6 bits of data in each subsequent byte */
ch |= (wchar_t)(utf8str[i] & 0x3f);
}
- if (wcstr)
- wcstr[wclen] = ch;
+ if (wcstr) wcstr[wclen] = ch;
- utf8str += utflen; /* Move to next UTF-8 character */
- wclen++; /* Count number of wide chars stored/required */
+ utf8str += utflen; /* Move to next UTF-8 character */
+ wclen++; /* Count number of wide chars stored/required */
}
/* Add null terminator if there's room in the buffer. */
- if (wcstr && wclen < count)
- wcstr[wclen] = 0;
+ if (wcstr && wclen < count) wcstr[wclen] = 0;
return wclen;
}
return n;
}
+
+#endif