1 /*
2  * Copyright (c) 2011 - 2022, Intel Corporation.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  *    * Redistributions of source code must retain the above copyright
9  *      notice, this list of conditions and the following disclaimer.
10  *    * Redistributions in binary form must reproduce the above copyright
11  *      notice, this list of conditions and the following disclaimer
12  *      in the documentation and/or other materials provided with the
13  *      distribution.
14  *    * Neither the name of Intel Corporation nor the names of its
15  *      contributors may be used to endorse or promote products
16  *      derived from this software without specific prior written
17  *      permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30  * OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * This file contains some wrappers around the gnu-efi functions. As
33  * we're not going through uefi_call_wrapper() directly, this allows
34  * us to get some type-safety for function call arguments and for the
35  * compiler to check that the number of function call arguments is
36  * correct.
37  *
38  * It's also a good place to document the EFI interface.
39  */
40 
41 
42 
43 #ifndef __STDLIB_H__
44 #define __STDLIB_H__
45 
46 #include <efi.h>
47 #include <efilib.h>
48 
memset(void * dstv,char ch,UINTN size)49 static inline void memset(void *dstv, char ch, UINTN size)
50 {
51 	char *dst = dstv;
52 	int32_t i;
53 
54 	for (i = 0; i < size; i++)
55 		dst[i] = ch;
56 }
57 
memcpy(char * dst,const char * src,UINTN size)58 static inline void memcpy(char *dst, const char *src, UINTN size)
59 {
60 	int32_t i;
61 
62 	for (i = 0; i < size; i++)
63 		*dst++ = *src++;
64 }
65 
strlen(const char * str)66 static inline int32_t strlen(const char *str)
67 {
68 	int32_t len;
69 
70 	len = 0;
71 	while (*str++)
72 		len++;
73 
74 	return len;
75 }
76 
strstr_16(CHAR16 * haystack,CHAR16 * needle,UINTN len)77 static inline CHAR16 *strstr_16(CHAR16 *haystack, CHAR16 *needle, UINTN len)
78 {
79 	CHAR16 *p;
80 	CHAR16 *word = NULL;
81 
82 	if (!len)
83 		return NULL;
84 
85 	p = haystack;
86 	while (*p) {
87 		if (!StrnCmp(p, needle, len)) {
88 			word = p;
89 			break;
90 		}
91 		p++;
92 	}
93 
94 	return (CHAR16*)word;
95 }
96 
ch16_2_ch8(CHAR16 * str16,UINTN len)97 static inline char *ch16_2_ch8(CHAR16 *str16, UINTN len)
98 {
99 	UINTN i;
100 	char *str8;
101 
102 	str8 = AllocatePool((len + 1) * sizeof(char));
103 
104 	for (i = 0; i < len; i++)
105 		str8[i] = str16[i];
106 
107 	str8[len] = 0;
108 
109 	return str8;
110 }
111 
ch8_2_ch16(char * str8,UINTN len)112 static inline CHAR16 *ch8_2_ch16(char *str8, UINTN len)
113 {
114 	UINTN i;
115 	CHAR16 *str16;
116 
117 	str16 = AllocatePool((len + 1) * sizeof(CHAR16));
118 
119 	for (i = 0; i < len; i++)
120 		str16[i] = str8[i];
121 
122 	str16[len] = 0;
123 
124 	return str16;
125 }
126 
127 #endif /* __STDLIB_H__ */
128