da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER START
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The contents of this file are subject to the terms of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Common Development and Distribution License (the "License").
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You may not use this file except in compliance with the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * See the License for the specific language governing permissions
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and limitations under the License.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * When distributing Covered Code, include this CDDL HEADER in each
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If applicable, add the following below this CDDL HEADER, with the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fields enclosed by brackets "[]" replaced with your own identifying
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * information: Portions Copyright [yyyy] [name of copyright owner]
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * CDDL HEADER END
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Use is subject to license terms.
b819cea2f73f98c5662230cc9affc8cc84f77fcfGordon Ross * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Multibyte/wide-char conversion routines. Wide-char encoding provides
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * a fixed size character encoding that maps to the Unicode 16-bit
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * (UCS-2) character set standard. Multibyte or UCS transformation
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * format (UTF) encoding is a variable length character encoding scheme
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * that s compatible with existing ASCII characters and guarantees that
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the resultant strings do not contain embedded null characters. Both
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * types of encoding provide a null terminator: single byte for UTF-8
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * and a wide-char null for Unicode. See RFC 2044.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The table below illustrates the UTF-8 encoding scheme. The letter x
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * indicates bits available for encoding the character value.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UCS-2 UTF-8 octet sequence (binary)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 0x0000-0x007F 0xxxxxxx
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 0x0080-0x07FF 110xxxxx 10xxxxxx
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 0x0800-0xFFFF 1110xxxx 10xxxxxx 10xxxxxx
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * RFC 2044
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UTF-8,a transformation format of UNICODE and ISO 10646
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * F. Yergeau
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Alis Technologies
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * October 1996
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * mbstowcs
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The mbstowcs() function converts a multibyte character string
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * mbstring into a wide character string wcstring. No more than
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * nwchars wide characters are stored. A terminating null wide
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * character is appended if there is room.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the number of wide characters converted, not counting
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * any terminating null wide character. Returns -1 if an invalid
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * multibyte character is encountered.
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brownsmb_mbstowcs(smb_wchar_t *wcstring, const char *mbstring, size_t nwchars)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (nwchars--) {
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown len = smb_mbtowc(wcstring, mbstring, MTS_MB_CHAR_MAX);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw if (len < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The mbtowc() function converts a multibyte character mbchar into
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * a wide character and stores the result in the object pointed to
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * by wcharp. Up to nbytes bytes are examined.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If mbchar is NULL, mbtowc() returns zero to indicate that shift
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * states are not supported. Shift states are used to switch between
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * representation modes using reserved bytes to signal shifting
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * without them being interpreted as characters. If mbchar is null
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * mbtowc should return non-zero if the current locale requires shift
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * states. Otherwise it should be return 0.
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * If mbchar is non-null, returns the number of bytes processed in
55bf511df53aad0fdb7eb3fa349f0308cc05234cas * mbchar. If mbchar is invalid, returns -1.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amwint /*ARGSUSED*/
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brownsmb_mbtowc(smb_wchar_t *wcharp, const char *mbchar, size_t nbytes)
55bf511df53aad0fdb7eb3fa349f0308cc05234cas return (0); /* no shift states */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /* 0xxxxxxx -> 1 byte ASCII encoding */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /* 10xxxxxx -> invalid first byte */
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The wctomb() function converts a wide character wchar into a multibyte
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * character and stores the result in mbchar. The object pointed to by
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * mbchar must be large enough to accommodate the multibyte character.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the numberof bytes written to mbchar.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (2);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (3);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * wcstombs
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * The wcstombs() function converts a wide character string wcstring
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * into a multibyte character string mbstring. Up to nbytes bytes are
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * stored in mbstring. Partial multibyte characters at the end of the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * string are not stored. The multibyte character string is null
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * terminated if there is room.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the number of bytes converted, not counting the terminating
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * null byte.
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brownsmb_wcstombs(char *mbstring, const smb_wchar_t *wcstring, size_t nbytes)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (0);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*LINTED E_PTRDIFF_OVERFLOW*/
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown if ((len = smb_wctomb(buf, wide_char)) > nbytes) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*LINTED E_PTRDIFF_OVERFLOW*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the number of bytes that would be written if the multi-
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * byte string mbs was converted to a wide character string, not
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * counting the terminating null wide character.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (*mbs) {
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown bytes = smb_mbtowc(&wide_char, mbs, MTS_MB_CHAR_MAX);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the number of bytes that would be written if the multi-
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * byte string mbs was converted to a single byte character string,
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * not counting the terminating null character.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw while (*mbs) {
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown nbytes = smb_mbtowc(&wide_char, mbs, MTS_MB_CHAR_MAX);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Convert a regular null terminated string 'string' to a UTF-8 encoded
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * null terminated multi-byte string 'mbstring'. Only full converted
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * UTF-8 characters will be written 'mbstring'. If a character will not
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * fit within the remaining buffer space or 'mbstring' will overflow
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * max_mblen, the conversion process will be terminated and 'mbstring'
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * will be null terminated.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Returns the number of bytes written to 'mbstring', excluding the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * terminating null character.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If either mbstring or string is a null pointer, -1 is returned.
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brownsmb_stombs(char *mbstring, char *string, int max_mblen)
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw unsigned char *p = (unsigned char *)string;
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*LINTED E_PTRDIFF_OVERFLOW*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Convert a null terminated multi-byte string 'mbstring' to a regular
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * null terminated string 'string'. A 1-byte character in 'mbstring'
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * maps to a 1-byte character in 'string'. A 2-byte character in
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * 'mbstring' will be mapped to 2-bytes, if the upper byte is non-null.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * Otherwise the upper byte null will be discarded to ensure that the
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * output stream does not contain embedded null characters.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If the input stream contains invalid multi-byte characters, a value
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * of -1 will be returned. Otherwise the length of 'string', excluding
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * the terminating null character, is returned.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw * If either mbstring or string is a null pointer, -1 is returned.
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
bbf6f00c25b6a2bed23c35eac6d62998ecdb338cJordan Brown if ((len = smb_mbtowc(&wc, mbstring, MTS_MB_CHAR_MAX)) < 0) {
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw return (-1);
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*LINTED E_BAD_PTR_CAST_ALIGN*/
da6c28aaf62fa55f0fdb8004aa40f88f23bf53f0amw /*LINTED E_PTRDIFF_OVERFLOW*/