fatfs/src/ff.c
static BYTE put_utf (	/* Returns number of encoding units written (0:buffer overflow or wrong encoding) */
	DWORD chr,	/* UTF-16 encoded character (Double encoding unit char if >=0x10000) */
	TCHAR* buf,	/* Output buffer */
	UINT szb	/* Size of the buffer */
)
{
#if FF_LFN_UNICODE == 1	/* UTF-16 output */
	WCHAR hs, wc;

	hs = (WCHAR)(chr >> 16);
	wc = (WCHAR)chr;
	if (hs == 0) {	/* Single encoding unit? */
		if (szb < 1 || IsSurrogate(wc)) return 0;	/* Buffer overflow or wrong code? */
		*buf = wc;
		return 1;
	}
	if (szb < 2 || !IsSurrogateH(hs) || !IsSurrogateL(wc)) return 0;	/* Buffer overflow or wrong surrogate? */
	*buf++ = hs;
	*buf++ = wc;
	return 2;

#elif FF_LFN_UNICODE == 2	/* UTF-8 output */
	DWORD hc;

	if (chr < 0x80) {	/* Single byte code? */
		if (szb < 1) return 0;	/* Buffer overflow? */
		*buf = (TCHAR)chr;
		return 1;
	}
	if (chr < 0x800) {	/* 2-byte sequence? */
		if (szb < 2) return 0;	/* Buffer overflow? */
		*buf++ = (TCHAR)(0xC0 | (chr >> 6 & 0x1F));
		*buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
		return 2;
	}
	if (chr < 0x10000) {	/* 3-byte sequence? */
		if (szb < 3 || IsSurrogate(chr)) return 0;	/* Buffer overflow or wrong code? */
		*buf++ = (TCHAR)(0xE0 | (chr >> 12 & 0x0F));
		*buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));
		*buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
		return 3;
	}
	/* 4-byte sequence */
	if (szb < 4) return 0;	/* Buffer overflow? */
	hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6;	/* Get high 10 bits */
	chr = (chr & 0xFFFF) - 0xDC00;					/* Get low 10 bits */
	if (hc >= 0x100000 || chr >= 0x400) return 0;	/* Wrong surrogate? */
	chr = (hc | chr) + 0x10000;
	*buf++ = (TCHAR)(0xF0 | (chr >> 18 & 0x07));
	*buf++ = (TCHAR)(0x80 | (chr >> 12 & 0x3F));
	*buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));
	*buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
	return 4;

#elif FF_LFN_UNICODE == 3	/* UTF-32 output */
	DWORD hc;

	if (szb < 1) return 0;	/* Buffer overflow? */
	if (chr >= 0x10000) {	/* Out of BMP? */
		hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6;	/* Get high 10 bits */
		chr = (chr & 0xFFFF) - 0xDC00;					/* Get low 10 bits */
		if (hc >= 0x100000 || chr >= 0x400) return 0;	/* Wrong surrogate? */
		chr = (hc | chr) + 0x10000;
	}
	*buf++ = (TCHAR)chr;
	return 1;

#else						/* ANSI/OEM output */
	WCHAR wc;

	wc = ff_uni2oem(chr, CODEPAGE);
	if (wc >= 0x100) {	/* Is this a DBC? */
		if (szb < 2) return 0;
		*buf++ = (char)(wc >> 8);	/* Store DBC 1st byte */
		*buf++ = (TCHAR)wc;			/* Store DBC 2nd byte */
		return 2;
	}
	if (wc == 0 || szb < 1) return 0;	/* Invalid char or buffer overflow? */
	*buf++ = (TCHAR)wc;					/* Store the character */
	return 1;
#endif
}
/* Output a TCHAR string in defined API encoding */
Output a TCHAR string in defined API encoding
Output a TCHAR string in defined API encoding
static BYTE put_utf (	
	DWORD chr,	
	TCHAR* buf,	
	UINT szb	
)
{
#if FF_LFN_UNICODE == 1	
	WCHAR hs, wc;
	hs = (WCHAR)(chr >> 16);
	wc = (WCHAR)chr;
	if (hs == 0) {	
		if (szb < 1 || IsSurrogate(wc)) return 0;	
		*buf = wc;
		return 1;
	}
	if (szb < 2 || !IsSurrogateH(hs) || !IsSurrogateL(wc)) return 0;	
	*buf++ = hs;
	*buf++ = wc;
	return 2;
#elif FF_LFN_UNICODE == 2	
	DWORD hc;
	if (chr < 0x80) {	
		if (szb < 1) return 0;	
		*buf = (TCHAR)chr;
		return 1;
	}
	if (chr < 0x800) {	
		if (szb < 2) return 0;	
		*buf++ = (TCHAR)(0xC0 | (chr >> 6 & 0x1F));
		*buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
		return 2;
	}
	if (chr < 0x10000) {	
		if (szb < 3 || IsSurrogate(chr)) return 0;	
		*buf++ = (TCHAR)(0xE0 | (chr >> 12 & 0x0F));
		*buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));
		*buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
		return 3;
	}
	if (szb < 4) return 0;	
	hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6;	
	chr = (chr & 0xFFFF) - 0xDC00;					
	if (hc >= 0x100000 || chr >= 0x400) return 0;	
	chr = (hc | chr) + 0x10000;
	*buf++ = (TCHAR)(0xF0 | (chr >> 18 & 0x07));
	*buf++ = (TCHAR)(0x80 | (chr >> 12 & 0x3F));
	*buf++ = (TCHAR)(0x80 | (chr >> 6 & 0x3F));
	*buf++ = (TCHAR)(0x80 | (chr >> 0 & 0x3F));
	return 4;
#elif FF_LFN_UNICODE == 3	
	DWORD hc;
	if (szb < 1) return 0;	
	if (chr >= 0x10000) {	
		hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6;	
		chr = (chr & 0xFFFF) - 0xDC00;					
		if (hc >= 0x100000 || chr >= 0x400) return 0;	
		chr = (hc | chr) + 0x10000;
	}
	*buf++ = (TCHAR)chr;
	return 1;
#else						
	WCHAR wc;
	wc = ff_uni2oem(chr, CODEPAGE);
	if (wc >= 0x100) {	
		if (szb < 2) return 0;
		*buf++ = (char)(wc >> 8);	
		*buf++ = (TCHAR)wc;			
		return 2;
	}
	if (wc == 0 || szb < 1) return 0;	
	*buf++ = (TCHAR)wc;					
	return 1;
#endif
}
static BYTE put_utf ( DWORD chr , TCHAR * buf , UINT szb ) { #if FF_LFN_UNICODE == 1 WCHAR hs , wc ; hs = ( WCHAR ) ( chr >> 16 ) ; wc = ( WCHAR ) chr ; if ( hs == 0 ) { if ( szb < 1 || IsSurrogate ( wc ) ) return 0 ; * buf = wc ; return 1 ; } if ( szb < 2 || ! IsSurrogateH ( hs ) || ! IsSurrogateL ( wc ) ) return 0 ; * buf ++ = hs ; * buf ++ = wc ; return 2 ; #elif FF_LFN_UNICODE == 2 DWORD hc ; if ( chr < 0x80 ) { if ( szb < 1 ) return 0 ; * buf = ( TCHAR ) chr ; return 1 ; } if ( chr < 0x800 ) { if ( szb < 2 ) return 0 ; * buf ++ = ( TCHAR ) ( 0xC0 | ( chr >> 6 & 0x1F ) ) ; * buf ++ = ( TCHAR ) ( 0x80 | ( chr >> 0 & 0x3F ) ) ; return 2 ; } if ( chr < 0x10000 ) { if ( szb < 3 || IsSurrogate ( chr ) ) return 0 ; * buf ++ = ( TCHAR ) ( 0xE0 | ( chr >> 12 & 0x0F ) ) ; * buf ++ = ( TCHAR ) ( 0x80 | ( chr >> 6 & 0x3F ) ) ; * buf ++ = ( TCHAR ) ( 0x80 | ( chr >> 0 & 0x3F ) ) ; return 3 ; } if ( szb < 4 ) return 0 ; hc = ( ( chr & 0xFFFF0000 ) - 0xD8000000 ) >> 6 ; chr = ( chr & 0xFFFF ) - 0xDC00 ; if ( hc >= 0x100000 || chr >= 0x400 ) return 0 ; chr = ( hc | chr ) + 0x10000 ; * buf ++ = ( TCHAR ) ( 0xF0 | ( chr >> 18 & 0x07 ) ) ; * buf ++ = ( TCHAR ) ( 0x80 | ( chr >> 12 & 0x3F ) ) ; * buf ++ = ( TCHAR ) ( 0x80 | ( chr >> 6 & 0x3F ) ) ; * buf ++ = ( TCHAR ) ( 0x80 | ( chr >> 0 & 0x3F ) ) ; return 4 ; #elif FF_LFN_UNICODE == 3 DWORD hc ; if ( szb < 1 ) return 0 ; if ( chr >= 0x10000 ) { hc = ( ( chr & 0xFFFF0000 ) - 0xD8000000 ) >> 6 ; chr = ( chr & 0xFFFF ) - 0xDC00 ; if ( hc >= 0x100000 || chr >= 0x400 ) return 0 ; chr = ( hc | chr ) + 0x10000 ; } * buf ++ = ( TCHAR ) chr ; return 1 ; #else WCHAR wc ; wc = ff_uni2oem ( chr , CODEPAGE ) ; if ( wc >= 0x100 ) { if ( szb < 2 ) return 0 ; * buf ++ = ( char ) ( wc >> 8 ) ; * buf ++ = ( TCHAR ) wc ; return 2 ; } if ( wc == 0 || szb < 1 ) return 0 ; * buf ++ = ( TCHAR ) wc ; return 1 ; #endif }
Output a TCHAR string in defined API encoding
Output a TCHAR string in defined API encoding
/* Returns number of encoding units written (0:buffer overflow or wrong encoding) */ /* UTF-16 encoded character (Double encoding unit char if >=0x10000) */ /* Output buffer */ /* Size of the buffer */ /* UTF-16 output */ /* Single encoding unit? */ /* Buffer overflow or wrong code? */ /* Buffer overflow or wrong surrogate? */ /* UTF-8 output */ /* Single byte code? */ /* Buffer overflow? */ /* 2-byte sequence? */ /* Buffer overflow? */ /* 3-byte sequence? */ /* Buffer overflow or wrong code? */ /* 4-byte sequence */ /* Buffer overflow? */ /* Get high 10 bits */ /* Get low 10 bits */ /* Wrong surrogate? */ /* UTF-32 output */ /* Buffer overflow? */ /* Out of BMP? */ /* Get high 10 bits */ /* Get low 10 bits */ /* Wrong surrogate? */ /* ANSI/OEM output */ /* Is this a DBC? */ /* Store DBC 1st byte */ /* Store DBC 2nd byte */ /* Invalid char or buffer overflow? */ /* Store the character */