|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2022 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "shared-module/usb/utf16le.h" |
| 28 | + |
| 29 | +STATIC void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) { |
| 30 | + // TODO: Check for runover. |
| 31 | + (void)utf8_len; |
| 32 | + |
| 33 | + for (size_t i = 0; i < utf16_len; i++) { |
| 34 | + uint16_t chr = utf16[i]; |
| 35 | + if (chr < 0x80) { |
| 36 | + *utf8++ = chr & 0xff; |
| 37 | + } else if (chr < 0x800) { |
| 38 | + *utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F)); |
| 39 | + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
| 40 | + } else if (chr < 0x10000) { |
| 41 | + // TODO: Verify surrogate. |
| 42 | + *utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F)); |
| 43 | + *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F)); |
| 44 | + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
| 45 | + } else { |
| 46 | + // TODO: Handle UTF-16 code points that take two entries. |
| 47 | + uint32_t hc = ((chr & 0xFFFF0000) - 0xD8000000) >> 6; /* Get high 10 bits */ |
| 48 | + chr = (chr & 0xFFFF) - 0xDC00; /* Get low 10 bits */ |
| 49 | + chr = (hc | chr) + 0x10000; |
| 50 | + *utf8++ = (uint8_t)(0xF0 | (chr >> 18 & 0x07)); |
| 51 | + *utf8++ = (uint8_t)(0x80 | (chr >> 12 & 0x3F)); |
| 52 | + *utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F)); |
| 53 | + *utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F)); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +// Count how many bytes a utf-16-le encoded string will take in utf-8. |
| 59 | +STATIC mp_int_t _count_utf8_bytes(const uint16_t *buf, size_t len) { |
| 60 | + size_t total_bytes = 0; |
| 61 | + for (size_t i = 0; i < len; i++) { |
| 62 | + uint16_t chr = buf[i]; |
| 63 | + if (chr < 0x80) { |
| 64 | + total_bytes += 1; |
| 65 | + } else if (chr < 0x800) { |
| 66 | + total_bytes += 2; |
| 67 | + } else if (chr < 0x10000) { |
| 68 | + total_bytes += 3; |
| 69 | + } else { |
| 70 | + total_bytes += 4; |
| 71 | + } |
| 72 | + } |
| 73 | + return total_bytes; |
| 74 | +} |
| 75 | + |
| 76 | +mp_obj_t utf16le_to_string(const uint16_t *buf, size_t utf16_len) { |
| 77 | + size_t size = _count_utf8_bytes(buf, utf16_len); |
| 78 | + vstr_t vstr; |
| 79 | + vstr_init_len(&vstr, size + 1); |
| 80 | + byte *p = (byte *)vstr.buf; |
| 81 | + // Null terminate. |
| 82 | + p[size] = '\0'; |
| 83 | + _convert_utf16le_to_utf8(buf, utf16_len, p, size); |
| 84 | + return mp_obj_new_str_from_vstr(&mp_type_str, &vstr); |
| 85 | +} |
0 commit comments