2 * Copyright (c) 2010 Marcel Moolenaar
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
41 #include <machine/efi.h>
46 ucs2len(const efi_char *str)
57 * If nm were converted to utf8, what what would strlen
58 * return on the resulting string?
61 utf8_len_of_ucs2(const efi_char *nm)
81 ucs2_to_utf8(const efi_char *nm, char **name)
86 int freeit = *name == NULL;
88 sz = utf8_len_of_ucs2(nm) + 1;
93 cp = *name = malloc(sz);
101 *cp++ = (char)(0xE0 | (c >> 12));
103 *cp++ = (char)(0x80 | ((c >> 6) & 0x3f));
105 *cp++ = (char)(0x80 | (c & 0x3f));
106 } else if (c > 0x7f) {
108 *cp++ = (char)(0xC0 | ((c >> 6) & 0x1f));
110 *cp++ = (char)(0x80 | (c & 0x3f));
113 *cp++ = (char)(c & 0x7f);
118 /* Absent bugs, we'll never return EOVERFLOW */
129 utf8_to_ucs2(const char *name, efi_char **nmp, size_t *len)
135 int freeit = *nmp == NULL;
137 sz = strlen(name) * 2 + 2;
145 while (sz > 1 && *name != '\0') {
148 * Conditionalize on the two major character types:
149 * initial and followup characters.
151 if ((c & 0xc0) != 0x80) {
152 /* Initial characters. */
158 if ((c & 0xf8) == 0xf0) {
161 } else if ((c & 0xf0) == 0xe0) {
164 } else if ((c & 0xe0) == 0xc0) {
172 /* Followup characters. */
174 ucs4 = (ucs4 << 6) + (c & 0x3f);
176 } else if (bytes == 0) {
188 *nm++ = (efi_char)ucs4;