usb.core.Device: fix trailing NUL on e.g., product string
.. and simplify the code to use vstr_add_char.
This commit is contained in:
parent
843ea86978
commit
6174e585af
@ -26,60 +26,49 @@
|
|||||||
|
|
||||||
#include "shared-module/usb/utf16le.h"
|
#include "shared-module/usb/utf16le.h"
|
||||||
|
|
||||||
STATIC void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) {
|
typedef struct {
|
||||||
// TODO: Check for runover.
|
const uint16_t *buf;
|
||||||
(void)utf8_len;
|
size_t len;
|
||||||
|
} utf16_str;
|
||||||
|
|
||||||
for (size_t i = 0; i < utf16_len; i++) {
|
STATIC uint32_t utf16str_peek_unit(utf16_str *utf) {
|
||||||
uint16_t chr = utf16[i];
|
if (!utf->len) {
|
||||||
if (chr < 0x80) {
|
return 0;
|
||||||
*utf8++ = chr & 0xff;
|
|
||||||
} else if (chr < 0x800) {
|
|
||||||
*utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F));
|
|
||||||
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
|
|
||||||
} else if (chr < 0x10000) {
|
|
||||||
// TODO: Verify surrogate.
|
|
||||||
*utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F));
|
|
||||||
*utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F));
|
|
||||||
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
|
|
||||||
} else {
|
|
||||||
// TODO: Handle UTF-16 code points that take two entries.
|
|
||||||
uint32_t hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */
|
|
||||||
chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */
|
|
||||||
chr = (hc | chr) + 0x10000;
|
|
||||||
*utf8++ = (uint8_t)(0xF0 | (chr >> 18 & 0x07));
|
|
||||||
*utf8++ = (uint8_t)(0x80 | (chr >> 12 & 0x3F));
|
|
||||||
*utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F));
|
|
||||||
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
return *utf->buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Count how many bytes a utf-16-le encoded string will take in utf-8.
|
STATIC uint32_t utf16str_next_unit(utf16_str *utf) {
|
||||||
STATIC mp_int_t _count_utf8_bytes(const uint16_t *buf, size_t len) {
|
uint32_t result = utf16str_peek_unit(utf);
|
||||||
size_t total_bytes = 0;
|
if (utf->len) {
|
||||||
for (size_t i = 0; i < len; i++) {
|
utf->len--;
|
||||||
uint16_t chr = buf[i];
|
utf->buf++;
|
||||||
if (chr < 0x80) {
|
}
|
||||||
total_bytes += 1;
|
return result;
|
||||||
} else if (chr < 0x800) {
|
}
|
||||||
total_bytes += 2;
|
STATIC uint32_t utf16str_next_codepoint(utf16_str *utf) {
|
||||||
} else if (chr < 0x10000) {
|
uint32_t unichr = utf16str_next_unit(utf);
|
||||||
total_bytes += 3;
|
if (unichr >= 0xd800 && unichr < 0xdc00) {
|
||||||
} else {
|
uint32_t low_surrogate = utf16str_peek_unit(utf);
|
||||||
total_bytes += 4;
|
if (low_surrogate >= 0xdc00 && low_surrogate < 0xe000) {
|
||||||
|
(void)utf16str_next_unit(utf);
|
||||||
|
unichr = (unichr - 0xd800) * 0x400 + low_surrogate - 0xdc00 + 0x10000;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return total_bytes;
|
return unichr;
|
||||||
|
}
|
||||||
|
|
||||||
|
STATIC void _convert_utf16le_to_utf8(vstr_t *vstr, utf16_str *utf) {
|
||||||
|
while (utf->len) {
|
||||||
|
vstr_add_char(vstr, utf16str_next_codepoint(utf));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mp_obj_t utf16le_to_string(const uint16_t *buf, size_t utf16_len) {
|
mp_obj_t utf16le_to_string(const uint16_t *buf, size_t utf16_len) {
|
||||||
size_t size = _count_utf8_bytes(buf, utf16_len);
|
// will grow if necessary, but will never grow for an all-ASCII descriptor
|
||||||
vstr_t vstr;
|
vstr_t vstr;
|
||||||
vstr_init_len(&vstr, size + 1);
|
vstr_init(&vstr, utf16_len);
|
||||||
byte *p = (byte *)vstr.buf;
|
utf16_str utf = {buf, utf16_len};
|
||||||
// Null terminate.
|
_convert_utf16le_to_utf8(&vstr, &utf);
|
||||||
p[size] = '\0';
|
|
||||||
_convert_utf16le_to_utf8(buf, utf16_len, p, size);
|
|
||||||
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user