xref: /illumos-gate/usr/src/boot/sys/boot/efi/libefi/wchar.c (revision eee59048)
1*eee59048SToomas Soome /*-
2*eee59048SToomas Soome  * Copyright 2016 Netflix, Inc. All Rights Reserved.
3*eee59048SToomas Soome  *
4*eee59048SToomas Soome  * Redistribution and use in source and binary forms, with or without
5*eee59048SToomas Soome  * modification, are permitted provided that the following conditions
6*eee59048SToomas Soome  * are met:
7*eee59048SToomas Soome  * 1. Redistributions of source code must retain the above copyright
8*eee59048SToomas Soome  *    notice, this list of conditions and the following disclaimer.
9*eee59048SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
10*eee59048SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
11*eee59048SToomas Soome  *    documentation and/or other materials provided with the distribution.
12*eee59048SToomas Soome  *
13*eee59048SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14*eee59048SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15*eee59048SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16*eee59048SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17*eee59048SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18*eee59048SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19*eee59048SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20*eee59048SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21*eee59048SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22*eee59048SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23*eee59048SToomas Soome  * SUCH DAMAGE.
24*eee59048SToomas Soome  */
25*eee59048SToomas Soome 
26*eee59048SToomas Soome #include <sys/cdefs.h>
27*eee59048SToomas Soome 
28*eee59048SToomas Soome #include <efi.h>
29*eee59048SToomas Soome #include <efilib.h>
30*eee59048SToomas Soome 
31*eee59048SToomas Soome /*
32*eee59048SToomas Soome  * CHAR16 related functions moved from loader.
33*eee59048SToomas Soome  * Perhaps we should move those to libstand afterall, but they are
34*eee59048SToomas Soome  * needed only by UEFI.
35*eee59048SToomas Soome  */
36*eee59048SToomas Soome 
37*eee59048SToomas Soome int
wcscmp(CHAR16 * a,CHAR16 * b)38*eee59048SToomas Soome wcscmp(CHAR16 *a, CHAR16 *b)
39*eee59048SToomas Soome {
40*eee59048SToomas Soome 
41*eee59048SToomas Soome 	while (*a && *b && *a == *b) {
42*eee59048SToomas Soome 		a++;
43*eee59048SToomas Soome 		b++;
44*eee59048SToomas Soome 	}
45*eee59048SToomas Soome 	return *a - *b;
46*eee59048SToomas Soome }
47*eee59048SToomas Soome 
48*eee59048SToomas Soome /*
49*eee59048SToomas Soome  * cpy8to16 copies a traditional C string into a CHAR16 string and
50*eee59048SToomas Soome  * 0 terminates it. len is the size of *dst in bytes.
51*eee59048SToomas Soome  */
52*eee59048SToomas Soome void
cpy8to16(const char * src,CHAR16 * dst,size_t len)53*eee59048SToomas Soome cpy8to16(const char *src, CHAR16 *dst, size_t len)
54*eee59048SToomas Soome {
55*eee59048SToomas Soome 	len <<= 1;		/* Assume CHAR16 is 2 bytes */
56*eee59048SToomas Soome 	while (len > 0 && *src) {
57*eee59048SToomas Soome 		*dst++ = *src++;
58*eee59048SToomas Soome 		len--;
59*eee59048SToomas Soome 	}
60*eee59048SToomas Soome 	*dst++ = (CHAR16)0;
61*eee59048SToomas Soome }
62*eee59048SToomas Soome 
63*eee59048SToomas Soome void
cpy16to8(const CHAR16 * src,char * dst,size_t len)64*eee59048SToomas Soome cpy16to8(const CHAR16 *src, char *dst, size_t len)
65*eee59048SToomas Soome {
66*eee59048SToomas Soome 	size_t i;
67*eee59048SToomas Soome 
68*eee59048SToomas Soome 	for (i = 0; i < len && src[i]; i++)
69*eee59048SToomas Soome 		dst[i] = (char)src[i];
70*eee59048SToomas Soome 	if (i < len)
71*eee59048SToomas Soome 		dst[i] = '\0';
72*eee59048SToomas Soome }
73