xref: /dragonfly/lib/libefivar/libefivar.c (revision 6e5c5008)
1 /*-
2  * Copyright (c) 2010 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD: head/lib/libefivar/libefivar.c 307071 2016-10-11 22:30:41Z imp $");
29 
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <stddef.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sys/efi.h>
37 #include <machine/efi.h>
38 
39 #include "libefivar_int.h"
40 
41 #include <stdio.h>
42 
43 /*
44  * If nm were converted to utf8, what what would strlen
45  * return on the resulting string?
46  */
47 static size_t
48 utf8_len_of_ucs2(const efi_char *nm)
49 {
50 	size_t len;
51 	efi_char c;
52 
53 	len = 0;
54 	while (*nm) {
55 		c = *nm++;
56 		if (c > 0x7ff)
57 			len += 3;
58 		else if (c > 0x7f)
59 			len += 2;
60 		else
61 			len++;
62 	}
63 
64 	return (len);
65 }
66 
67 int
68 libefi_ucs2_to_utf8(const efi_char *nm, char **name)
69 {
70 	size_t len, sz;
71 	efi_char c;
72 	char *cp;
73 	int freeit = *name == NULL;
74 
75 	sz = utf8_len_of_ucs2(nm) + 1;
76 	len = 0;
77 	if (*name != NULL)
78 		cp = *name;
79 	else
80 		cp = *name = malloc(sz);
81 	if (*name == NULL)
82 		return (ENOMEM);
83 
84 	while (*nm) {
85 		c = *nm++;
86 		if (c > 0x7ff) {
87 			if (len++ < sz)
88 				*cp++ = (char)(0xE0 | (c >> 12));
89 			if (len++ < sz)
90 				*cp++ = (char)(0x80 | ((c >> 6) & 0x3f));
91 			if (len++ < sz)
92 				*cp++ = (char)(0x80 | (c & 0x3f));
93 		} else if (c > 0x7f) {
94 			if (len++ < sz)
95 				*cp++ = (char)(0xC0 | ((c >> 6) & 0x1f));
96 			if (len++ < sz)
97 				*cp++ = (char)(0x80 | (c & 0x3f));
98 		} else {
99 			if (len++ < sz)
100 				*cp++ = (char)(c & 0x7f);
101 		}
102 	}
103 
104 	if (len >= sz) {
105 		/* Absent bugs, we'll never return EOVERFLOW */
106 		if (freeit)
107 			free(*name);
108 		return (EOVERFLOW);
109 	}
110 	*cp++ = '\0';
111 
112 	return (0);
113 }
114 
115 int
116 libefi_utf8_to_ucs2(const char *name, efi_char **nmp, size_t *len)
117 {
118 	efi_char *nm;
119 	size_t sz;
120 	uint32_t ucs4;
121 	int c, bytes;
122 	int freeit = *nmp == NULL;
123 
124 	sz = strlen(name) * 2 + 2;
125 	if (*nmp == NULL)
126 		*nmp = malloc(sz);
127 	nm = *nmp;
128 	*len = sz;
129 
130 	ucs4 = 0;
131 	bytes = 0;
132 	while (sz > 1 && *name != '\0') {
133 		c = *name++;
134 		/*
135 		 * Conditionalize on the two major character types:
136 		 * initial and followup characters.
137 		 */
138 		if ((c & 0xc0) != 0x80) {
139 			/* Initial characters. */
140 			if (bytes != 0) {
141 				if (freeit)
142 					free(nm);
143 				return (EILSEQ);
144 			}
145 			if ((c & 0xf8) == 0xf0) {
146 				ucs4 = c & 0x07;
147 				bytes = 3;
148 			} else if ((c & 0xf0) == 0xe0) {
149 				ucs4 = c & 0x0f;
150 				bytes = 2;
151 			} else if ((c & 0xe0) == 0xc0) {
152 				ucs4 = c & 0x1f;
153 				bytes = 1;
154 			} else {
155 				ucs4 = c & 0x7f;
156 				bytes = 0;
157 			}
158 		} else {
159 			/* Followup characters. */
160 			if (bytes > 0) {
161 				ucs4 = (ucs4 << 6) + (c & 0x3f);
162 				bytes--;
163 			} else if (bytes == 0) {
164 				if (freeit)
165 					free(nm);
166 				return (EILSEQ);
167 			}
168 		}
169 		if (bytes == 0) {
170 			if (ucs4 > 0xffff) {
171 				if (freeit)
172 					free(nm);
173 				return (EILSEQ);
174 			}
175 			*nm++ = (efi_char)ucs4;
176 			sz -= 2;
177 		}
178 	}
179 	if (sz < 2) {
180 		if (freeit)
181 			free(nm);
182 		return (EDOOFUS);
183 	}
184 	sz -= 2;
185 	*nm = 0;
186 	*len -= sz;
187 	return (0);
188 }
189