libpayload: UTF-16LE to ASCII conversion
This patch adds a simple function to convert a string in UTF-16LE to ASCII. TEST=Ran against a string found in a GPT with the intended outcome BRANCH=none BUG=none Change-Id: I94ec0a32f5712259d3d0caec2233c992330228e3 Signed-off-by: Patrick Georgi <pgeorgi@chromium.org> Original-Commit-Id: 1104db8328a197c7ccf6959a238277f416a2113a Original-Signed-off-by: Dan Ehrenberg <dehrenberg@chromium.org> Original-Change-Id: I50ca5bfdfbef9e084321b2beb1b8d4194ca5af9c Original-Reviewed-on: https://chromium-review.googlesource.com/231456 Original-Reviewed-by: Julius Werner <jwerner@chromium.org> Reviewed-on: http://review.coreboot.org/8733 Tested-by: build bot (Jenkins) Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
This commit is contained in:
parent
8bbd04ea8d
commit
7aebf3269a
|
@ -68,6 +68,13 @@ char *strtok_r(char *str, const char *delim, char **ptr);
|
|||
char *strerror(int errnum);
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup string Unicode functions
|
||||
* @{
|
||||
*/
|
||||
char *utf16le_to_ascii(uint16_t *utf16_string, int maxlen);
|
||||
/** @} */
|
||||
|
||||
/**
|
||||
* @defgroup string OpenBSD based safe string functions
|
||||
* @{
|
||||
|
|
|
@ -653,3 +653,22 @@ char *strerror(int errnum)
|
|||
snprintf(errstr, sizeof(errstr), "Unknown error %d", errnum);
|
||||
return errstr;
|
||||
}
|
||||
|
||||
/*
|
||||
* Simple routine to convert UTF-16 to ASCII, giving up with ? if too high.
|
||||
* A single code point may convert to ?? if not in the BMP.
|
||||
* @param utf16_string A string encoded in UTF-16LE
|
||||
* @param maxlen Maximum possible length of the string in code points
|
||||
* @return Newly allocated ASCII string
|
||||
*/
|
||||
char *utf16le_to_ascii(uint16_t *utf16_string, int maxlen)
|
||||
{
|
||||
char *ascii_string = xmalloc(maxlen + 1); /* +1 for trailing \0 */
|
||||
ascii_string[maxlen] = '\0';
|
||||
int i;
|
||||
for (i = 0; i < maxlen; i++) {
|
||||
uint16_t wchar = utf16_string[i];
|
||||
ascii_string[i] = wchar > 0x7f ? '?' : (char)wchar;
|
||||
}
|
||||
return ascii_string;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue