source/AccessorySDK/Support/utfconv.c
size_t
utf8_encodelen(const uint16_t * ucsp, size_t ucslen, uint16_t altslash, int flags)
{
	uint16_t ucs_ch;
	uint16_t * chp = NULL;
	uint16_t sequence[8];
	int extra = 0;
	int charcnt;
	int swapbytes = (flags & UTF_REVERSE_ENDIAN);
	int decompose = (flags & UTF_DECOMPOSED);
	size_t len;

	charcnt = (int)(ucslen / 2);
	len = 0;

	while (charcnt-- > 0) {
		if (extra > 0) {
			--extra;
			ucs_ch = *chp++; // Note: chp is from the local, aligned "sequence" array.
		} else {
			if (swapbytes)	ucs_ch = ReadSwap16(ucsp);
			else			ucs_ch = ReadHost16(ucsp);
			++ucsp;
			if (ucs_ch == '/') {
				ucs_ch = (uint16_t)(altslash ? altslash : '_');
			} else if (ucs_ch == '\0') {
				ucs_ch = UCS_ALT_NULL;
			} else if (decompose && unicode_decomposeable(ucs_ch)) {
				extra = unicode_decompose(ucs_ch, sequence) - 1;
				charcnt += extra;
				ucs_ch = sequence[0];
				chp = &sequence[1];
			}
		}
		len += UNICODE_TO_UTF8_LEN(ucs_ch);
	}

	return (len);
}
/*
 * utf8_encodelen - Calculate the UTF-8 encoding length
 *
 * This function takes a Unicode input string, ucsp, of ucslen bytes
 * and calculates the size of the UTF-8 output in bytes (not including
 * a NULL termination byte). The string must reside in kernel memory.
 *
 * If '/' chars are possible in the Unicode input then an alternate
 * (replacement) char should be provided in altslash.
 *
 * FLAGS
 *    UTF_REVERSE_ENDIAN:  Unicode byte order is opposite current runtime
 *
 *    UTF_BIG_ENDIAN:  Unicode byte order is always big endian
 *
 *    UTF_LITTLE_ENDIAN:  Unicode byte order is always little endian
 *
 *    UTF_DECOMPOSED:  generate fully decomposed output
 *
 *    UTF_PRECOMPOSED is ignored since utf8_encodestr doesn't support it
 *
 * ERRORS
 *    None
 */
Calculate the UTF-8 encoding length
This function takes a Unicode input string, ucsp, of ucslen bytes
and calculates the size of the UTF-8 output in bytes (not including
a NULL termination byte). The string must reside in kernel memory.

If '/' chars are possible in the Unicode input then an alternate
(replacement) char should be provided in altslash.

FLAGS
UTF_REVERSE_ENDIAN:  Unicode byte order is opposite current runtime

Unicode byte order is always big endian

Unicode byte order is always little endian

generate fully decomposed output

UTF_PRECOMPOSED is ignored since utf8_encodestr doesn't support it

ERRORS
None
Calculate the UTF - 8 encoding length This function takes a Unicode input string ucsp of ucslen bytes and calculates the size of the UTF - 8 output in bytes ( not including a NULL termination byte ) . The string must reside in kernel memory . If ' / ' chars are possible in the Unicode input then an alternate ( replacement ) char should be provided in altslash . FLAGS UTF_REVERSE_ENDIAN : Unicode byte order is opposite current runtime Unicode byte order is always big endian Unicode byte order is always little endian generate fully decomposed output UTF_PRECOMPOSED is ignored since utf8_encodestr doesn ' t support it ERRORS None
size_t
utf8_encodelen(const uint16_t * ucsp, size_t ucslen, uint16_t altslash, int flags)
{
	uint16_t ucs_ch;
	uint16_t * chp = NULL;
	uint16_t sequence[8];
	int extra = 0;
	int charcnt;
	int swapbytes = (flags & UTF_REVERSE_ENDIAN);
	int decompose = (flags & UTF_DECOMPOSED);
	size_t len;
	charcnt = (int)(ucslen / 2);
	len = 0;
	while (charcnt-- > 0) {
		if (extra > 0) {
			--extra;
			ucs_ch = *chp++; 
		} else {
			if (swapbytes)	ucs_ch = ReadSwap16(ucsp);
			else			ucs_ch = ReadHost16(ucsp);
			++ucsp;
			if (ucs_ch == '/') {
				ucs_ch = (uint16_t)(altslash ? altslash : '_');
			} else if (ucs_ch == '\0') {
				ucs_ch = UCS_ALT_NULL;
			} else if (decompose && unicode_decomposeable(ucs_ch)) {
				extra = unicode_decompose(ucs_ch, sequence) - 1;
				charcnt += extra;
				ucs_ch = sequence[0];
				chp = &sequence[1];
			}
		}
		len += UNICODE_TO_UTF8_LEN(ucs_ch);
	}
	return (len);
}
size_t utf8_encodelen ( const uint16_t * ucsp , size_t ucslen , uint16_t altslash , int flags ) { uint16_t ucs_ch ; uint16_t * chp = NULL ; uint16_t sequence [ 8 ] ; int extra = 0 ; int charcnt ; int swapbytes = ( flags & UTF_REVERSE_ENDIAN ) ; int decompose = ( flags & UTF_DECOMPOSED ) ; size_t len ; charcnt = ( int ) ( ucslen / 2 ) ; len = 0 ; while ( charcnt -- > 0 ) { if ( extra > 0 ) { -- extra ; ucs_ch = * chp ++ ; } else { if ( swapbytes ) ucs_ch = ReadSwap16 ( ucsp ) ; else ucs_ch = ReadHost16 ( ucsp ) ; ++ ucsp ; if ( ucs_ch == ' ' ) { ucs_ch = ( uint16_t ) ( altslash ? altslash : ' ' ) ; } else if ( ucs_ch == ' \0 ' ) { ucs_ch = UCS_ALT_NULL ; } else if ( decompose && unicode_decomposeable ( ucs_ch ) ) { extra = unicode_decompose ( ucs_ch , sequence ) - 1 ; charcnt += extra ; ucs_ch = sequence [ 0 ] ; chp = & sequence [ 1 ] ; } } len += UNICODE_TO_UTF8_LEN ( ucs_ch ) ; } return ( len ) ; }
utf8_encodelen - Calculate the UTF-8 encoding length
 This function takes a Unicode input string, ucsp, of ucslen bytes
 and calculates the size of the UTF-8 output in bytes (not including
 a NULL termination byte).
utf8_encodelen - Calculate the UTF - 8 encoding length This function takes a Unicode input string ucsp of ucslen bytes and calculates the size of the UTF - 8 output in bytes ( not including a NULL termination byte ) .
// Note: chp is from the local, aligned "sequence" array.