summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPeter Jones <pjones@redhat.com>2021-03-09 11:54:58 -0500
committerPeter Jones <pjones@redhat.com>2021-03-10 15:54:20 -0500
commit766aac4d5cfbe76026be5ce718b0883ee211f323 (patch)
tree56b15cac9a128b4285d58bc1e2a4b12cff5ed881
parent78809820b5a3f79a0bfbec00e630e40011acf4ec (diff)
downloadefi-boot-shim-766aac4d5cfbe76026be5ce718b0883ee211f323.tar.gz
efi-boot-shim-766aac4d5cfbe76026be5ce718b0883ee211f323.zip
Consolidate most of our standard lib functions to lib
Signed-off-by: Peter Jones <pjones@redhat.com>
-rw-r--r--Cryptlib/Include/OpenSslSupport.h23
-rw-r--r--Cryptlib/Makefile3
-rw-r--r--Cryptlib/SysCall/BaseStrings.c34
-rw-r--r--Cryptlib/SysCall/CrtWrapper.c82
-rw-r--r--Cryptlib/SysCall/memset.c40
-rw-r--r--Makefile4
-rw-r--r--csv.c4
m---------gnu-efi0
-rw-r--r--httpboot.c14
-rw-r--r--include/compiler.h5
-rw-r--r--include/endian.h19
-rw-r--r--include/hexdump.h2
-rw-r--r--include/str.h118
-rw-r--r--include/system/alloca.h6
-rw-r--r--include/system/builtins_begin_.h94
-rw-r--r--include/system/builtins_end_.h28
-rw-r--r--include/system/ctype.h73
-rw-r--r--include/system/stdlib.h12
-rw-r--r--include/system/string.h51
-rw-r--r--include/system/strings.h9
-rw-r--r--lib/string.c245
-rw-r--r--mok.c2
-rw-r--r--netboot.c8
-rw-r--r--sbat.c4
-rw-r--r--shim.h2
25 files changed, 565 insertions, 317 deletions
diff --git a/Cryptlib/Include/OpenSslSupport.h b/Cryptlib/Include/OpenSslSupport.h
index 7af9650f..6bb7ba64 100644
--- a/Cryptlib/Include/OpenSslSupport.h
+++ b/Cryptlib/Include/OpenSslSupport.h
@@ -288,34 +288,21 @@ extern int errno;
void *malloc (size_t);
void *realloc (void *, size_t);
void free (void *);
-int isdigit (int);
-int isspace (int);
-int tolower (int);
-int isupper (int);
-int isxdigit (int);
-int isalnum (int);
void *memcpy (void *, const void *, size_t);
void *memset (void *, int, size_t);
void *memchr (const void *, int, size_t);
int memcmp (const void *, const void *, size_t);
void *memmove (void *, const void *, size_t);
-int strcmp (const char *, const char *);
-int strncmp (const char *, const char *, size_t);
char *strcpy (char *, const char *);
char *strncpy (char *, const char *, size_t);
-size_t strlen (const char *);
char *strcat (char *, const char *);
char *strchr (const char *, int);
int strcasecmp (const char *, const char *);
int strncasecmp (const char *, const char *, size_t);
char *strncpy (char *, const char *, size_t);
-int strncmp (const char *, const char *, size_t);
-char *strrchr (const char *, int);
unsigned long strtoul (const char *, char **, int);
long strtol (const char *, char **, int);
char *strerror (int);
-size_t strspn (const char *, const char *);
-size_t strcspn (const char *, const char *);
int printf (const char *, ...);
int sscanf (const char *, const char *, ...);
int open (const char *, int, ...);
@@ -351,7 +338,6 @@ gid_t getegid (void);
void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
char *getenv (const char *);
void exit (int);
-void abort (void);
__sighandler_t *signal (int, __sighandler_t *);
//
@@ -361,7 +347,7 @@ extern FILE *stderr;
extern FILE *stdin;
extern FILE *stdout;
-#define AsciiStrLen(x) strlena(x)
+#define AsciiStrLen(x) strlen(x)
#define AsciiStrnCmp(s1, s2, len) strncmpa((CHAR8 *)s1, (CHAR8 *)s2, len)
//
@@ -372,17 +358,10 @@ extern FILE *stdout;
#define memchr(buf,ch,count) ScanMem8((CHAR8 *)buf,(UINTN)(count),ch)
#define memcmp(buf1,buf2,count) (int)(CompareMem(buf1,buf2,(UINTN)(count)))
#define memmove(dest,source,count) CopyMem(dest,source,(UINTN)(count))
-#define strlen(str) (size_t)(AsciiStrLen((CHAR8 *)str))
-#define strcpy(strDest,strSource) AsciiStrCpy((CHAR8 *)strDest,(const CHAR8 *)strSource)
-#define strncpy(strDest,strSource,count) AsciiStrnCpy((CHAR8 *)strDest,(const CHAR8 *)strSource,(UINTN)count)
-#define strcat(strDest,strSource) AsciiStrCat((CHAR8 *)strDest,(const CHAR8 *)strSource)
-#define strchr(str,ch) (char *)(ScanMem8((CHAR8 *)str,AsciiStrSize((CHAR8 *)str),ch))
-#define strncmp(string1,string2,count) (int)(AsciiStrnCmp((const CHAR8 *)string1, (const CHAR8 *)string2,(UINTN)(count)))
#define localtime(timer) NULL
#define assert(expression)
#define atoi(nptr) AsciiStrDecimalToUintn((const CHAR8 *)nptr)
#define gettimeofday(tvp,tz) do { (tvp)->tv_sec = time(NULL); (tvp)->tv_usec = 0; } while (0)
#define gmtime_r(timer,result) (result = NULL)
-#define abort()
#endif
diff --git a/Cryptlib/Makefile b/Cryptlib/Makefile
index 65a3918c..27614618 100644
--- a/Cryptlib/Makefile
+++ b/Cryptlib/Makefile
@@ -63,8 +63,7 @@ OBJS = Hash/CryptMd4Null.o \
SysCall/CrtWrapper.o \
SysCall/TimerWrapper.o \
SysCall/BaseMemAllocation.o \
- SysCall/BaseStrings.o \
- SysCall/memset.o
+ SysCall/BaseStrings.o
all: $(TARGET)
diff --git a/Cryptlib/SysCall/BaseStrings.c b/Cryptlib/SysCall/BaseStrings.c
index c4b3e18e..29a16100 100644
--- a/Cryptlib/SysCall/BaseStrings.c
+++ b/Cryptlib/SysCall/BaseStrings.c
@@ -3,7 +3,7 @@
CHAR8 *
AsciiStrCat(CHAR8 *Destination, const CHAR8 *Source)
{
- UINTN dest_len = strlena((CHAR8 *)Destination);
+ UINTN dest_len = strlen((CHAR8 *)Destination);
UINTN i;
for (i = 0; Source[i] != '\0'; i++)
@@ -61,37 +61,7 @@ WriteUnaligned32(UINT32 *Buffer, UINT32 Value)
UINTN
AsciiStrSize(const CHAR8 *string)
{
- return strlena(string) + 1;
-}
-
-int
-strcmp (const char *str1, const char *str2)
-{
- return strcmpa((CHAR8 *)str1,(CHAR8 *)str2);
-}
-
-inline static char
-toupper (char c)
-{
- return ((c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c);
-}
-
-/* Based on AsciiStriCmp() in edk2 MdePkg/Library/BaseLib/String.c */
-int
-strcasecmp (const char *str1, const char *str2)
-{
- char c1, c2;
-
- c1 = toupper (*str1);
- c2 = toupper (*str2);
- while ((*str1 != '\0') && (c1 == c2)) {
- str1++;
- str2++;
- c1 = toupper (*str1);
- c2 = toupper (*str2);
- }
-
- return c1 - c2;
+ return strlen(string) + 1;
}
/* Based on AsciiStrDecimalToUintnS() in edk2
diff --git a/Cryptlib/SysCall/CrtWrapper.c b/Cryptlib/SysCall/CrtWrapper.c
index 698e1eef..4bdaede9 100644
--- a/Cryptlib/SysCall/CrtWrapper.c
+++ b/Cryptlib/SysCall/CrtWrapper.c
@@ -121,21 +121,6 @@ QuickSortWorker (
// -- String Manipulation Routines --
//
-/* Scan a string for the last occurrence of a character */
-char *strrchr (const char *str, int c)
-{
- char * save;
-
- for (save = NULL; ; ++str) {
- if (*str == c) {
- save = (char *)str;
- }
- if (*str == 0) {
- return (save);
- }
- }
-}
-
/* Read formatted data from a string */
int sscanf (const char *buffer, const char *format, ...)
{
@@ -147,59 +132,6 @@ int sscanf (const char *buffer, const char *format, ...)
}
//
-// -- Character Classification Routines --
-//
-
-/* Determines if a particular character is a decimal-digit character */
-int isdigit (int c)
-{
- //
- // <digit> ::= [0-9]
- //
- return (('0' <= (c)) && ((c) <= '9'));
-}
-
-/* Determine if an integer represents character that is a hex digit */
-int isxdigit (int c)
-{
- //
- // <hexdigit> ::= [0-9] | [a-f] | [A-F]
- //
- return ((('0' <= (c)) && ((c) <= '9')) ||
- (('a' <= (c)) && ((c) <= 'f')) ||
- (('A' <= (c)) && ((c) <= 'F')));
-}
-
-/* Determines if a particular character represents a space character */
-int isspace (int c)
-{
- //
- // <space> ::= [ ]
- //
- return ((c) == ' ');
-}
-
-/* Determine if a particular character is an alphanumeric character */
-int isalnum (int c)
-{
- //
- // <alnum> ::= [0-9] | [a-z] | [A-Z]
- //
- return ((('0' <= (c)) && ((c) <= '9')) ||
- (('a' <= (c)) && ((c) <= 'z')) ||
- (('A' <= (c)) && ((c) <= 'Z')));
-}
-
-/* Determines if a particular character is in upper case */
-int isupper (int c)
-{
- //
- // <uppercase letter> := [A-Z]
- //
- return (('A' <= (c)) && ((c) <= 'Z'));
-}
-
-//
// -- Data Conversion Routines --
//
@@ -223,15 +155,6 @@ unsigned long strtoul (const char *nptr, char **endptr, int base)
return 0;
}
-/* Convert character to lowercase */
-int tolower (int c)
-{
- if (('A' <= (c)) && ((c) <= 'Z')) {
- return (c - ('A' - 'a'));
- }
- return (c);
-}
-
//
// -- Searching and Sorting Routines --
//
@@ -424,11 +347,6 @@ int stat (const char *c, struct stat *s)
return -1;
}
-int strncasecmp (const char *c, const char *s, size_t l)
-{
- return 0;
-}
-
void syslog (int a, const char *c, ...)
{
diff --git a/Cryptlib/SysCall/memset.c b/Cryptlib/SysCall/memset.c
deleted file mode 100644
index 76deed68..00000000
--- a/Cryptlib/SysCall/memset.c
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Copyright 2016 SUSE LINUX GmbH <glin@suse.com>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the
- * distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
- * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
- * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
- * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
- * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
- * OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include <efi.h>
-#include <efilib.h>
-
-typedef UINTN size_t;
-
-void *
-memset (void *dest, int ch, size_t count)
-{
- SetMem(dest, count, (UINT8)(ch));
- return dest;
-}
diff --git a/Makefile b/Makefile
index 6a62e00a..7c0fbb11 100644
--- a/Makefile
+++ b/Makefile
@@ -116,12 +116,12 @@ LIBS = Cryptlib/libcryptlib.a \
gnu-efi/$(ARCH_GNUEFI)/gnuefi/libgnuefi.a
$(SHIMSONAME): $(OBJS) $(LIBS)
- $(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS)
+ $(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS) lib/lib.a
fallback.o: $(FALLBACK_SRCS)
$(FBSONAME): $(FALLBACK_OBJS) $(LIBS)
- $(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS)
+ $(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS) lib/lib.a
MokManager.o: $(MOK_SOURCES)
diff --git a/csv.c b/csv.c
index 3c821414..d141f035 100644
--- a/csv.c
+++ b/csv.c
@@ -21,8 +21,8 @@ parse_csv_line(char * line, size_t max, size_t *n_columns, const char *columns[]
valid = strntoken(next, max, delims, &token, &state);
}
if (valid) {
- next += strlena(token) + 1;
- max -= strlena(token) + 1;
+ next += strlen(token) + 1;
+ max -= strlen(token) + 1;
columns[n] = token;
new_n = n + 1;
} else {
diff --git a/gnu-efi b/gnu-efi
-Subproject 9aa86c7526a4adc363afe6847bc3a4c8efe6df8
+Subproject d7e183a351d1a81efb2402e5148a4a69fc17036
diff --git a/httpboot.c b/httpboot.c
index fe08f3f7..93d88931 100644
--- a/httpboot.c
+++ b/httpboot.c
@@ -130,7 +130,7 @@ find_httpboot (EFI_HANDLE device)
/* Save the current URI */
UriNode = (URI_DEVICE_PATH *)Node;
- uri_size = strlena(UriNode->Uri);
+ uri_size = strlen(UriNode->Uri);
uri = AllocatePool(uri_size + 1);
if (!uri) {
perror(L"Failed to allocate uri\n");
@@ -156,10 +156,10 @@ generate_next_uri (CONST CHAR8 *current_uri, CONST CHAR8 *next_loader,
UINTN path_len = 0;
UINTN count = 0;
- if (strncmpa(current_uri, (CHAR8 *)"http://", 7) == 0) {
+ if (strncmp(current_uri, (CHAR8 *)"http://", 7) == 0) {
ptr = current_uri + 7;
count += 7;
- } else if (strncmpa(current_uri, (CHAR8 *)"https://", 8) == 0) {
+ } else if (strncmp(current_uri, (CHAR8 *)"https://", 8) == 0) {
ptr = current_uri + 8;
count += 8;
} else {
@@ -167,7 +167,7 @@ generate_next_uri (CONST CHAR8 *current_uri, CONST CHAR8 *next_loader,
}
/* Extract the path */
- next_len = strlena(next_loader);
+ next_len = strlen(next_loader);
while (*ptr != '\0') {
count++;
if (*ptr == '/')
@@ -192,9 +192,9 @@ extract_hostname (CONST CHAR8 *url, CHAR8 **hostname)
CONST CHAR8 *ptr, *start;
UINTN host_len = 0;
- if (strncmpa(url, (CHAR8 *)"http://", 7) == 0)
+ if (strncmp(url, (CHAR8 *)"http://", 7) == 0)
start = url + 7;
- else if (strncmpa(url, (CHAR8 *)"https://", 8) == 0)
+ else if (strncmp(url, (CHAR8 *)"https://", 8) == 0)
start = url + 8;
else
return EFI_INVALID_PARAMETER;
@@ -571,7 +571,7 @@ receive_http_response(EFI_HTTP_PROTOCOL *http, VOID **buffer, UINT64 *buf_size)
/* Check the length of the file */
for (i = 0; i < rx_message.HeaderCount; i++) {
- if (!strcmpa(rx_message.Headers[i].FieldName, (CHAR8 *)"Content-Length")) {
+ if (!strcmp(rx_message.Headers[i].FieldName, (CHAR8 *)"Content-Length")) {
*buf_size = ascii_to_int(rx_message.Headers[i].FieldValue);
}
}
diff --git a/include/compiler.h b/include/compiler.h
index 3cabd09c..40358a11 100644
--- a/include/compiler.h
+++ b/include/compiler.h
@@ -179,5 +179,10 @@
#define MIN(a, b) ({(a) < (b) ? (a) : (b);})
#define MAX(a, b) ({(a) <= (b) ? (b) : (a);})
+/**
+ * Builtins that don't go in string.h
+ */
+#define unreachable() __builtin_unreachable()
+
#endif /* !COMPILER_H_ */
// vim:fenc=utf-8:tw=75:et
diff --git a/include/endian.h b/include/endian.h
new file mode 100644
index 00000000..267fbf00
--- /dev/null
+++ b/include/endian.h
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+/*
+ * endian.h - bswap decls that can't go in compiler.h
+ * Copyright Peter Jones <pjones@redhat.com>
+ */
+
+#ifndef ENDIAN_H_
+#define ENDIAN_H_
+
+#include <stdint.h>
+
+#include "system/builtins_begin_.h"
+mkbi1_(uint16_t, bswap16, uint16_t, x)
+mkbi1_(uint32_t, bswap32, uint32_t, x)
+mkbi1_(uint64_t, bswap64, uint64_t, x)
+#include "system/builtins_end_.h"
+
+#endif /* !ENDIAN_H_ */
+// vim:fenc=utf-8:tw=75:noet
diff --git a/include/hexdump.h b/include/hexdump.h
index 36d77ec4..a6aa2bfa 100644
--- a/include/hexdump.h
+++ b/include/hexdump.h
@@ -48,8 +48,6 @@ prepare_hex(const void *data, size_t size, char *buf, unsigned int position)
return ret;
}
-#define isprint(c) ((c) >= 0x20 && (c) <= 0x7e)
-
static inline void UNUSED
prepare_text(const void *data, size_t size, char *buf, unsigned int position)
{
diff --git a/include/str.h b/include/str.h
index c4d12113..189aceff 100644
--- a/include/str.h
+++ b/include/str.h
@@ -9,122 +9,8 @@
#pragma GCC diagnostic ignored "-Wnonnull-compare"
#endif
-static inline UNUSED NONNULL(1) unsigned long
-strnlena(const CHAR8 *s, unsigned long n)
-{
- unsigned long i;
- for (i = 0; i < n; i++)
- if (s[i] == '\0')
- break;
- return i;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1, 2) CHAR8 *
-strncpya(CHAR8 *dest, const CHAR8 *src, unsigned long n)
-{
- unsigned long i;
-
- for (i = 0; i < n && src[i] != '\0'; i++)
- dest[i] = src[i];
- for (; i < n; i++)
- dest[i] = '\0';
-
- return dest;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1, 2) CHAR8 *
-strcata(CHAR8 *dest, const CHAR8 *src)
-{
- unsigned long dest_len = strlena(dest);
- unsigned long i;
-
- for (i = 0; src[i] != '\0'; i++)
- dest[dest_len + i] = src[i];
- dest[dest_len + i] = '\0';
-
- return dest;
-}
-
-static inline UNUSED NONNULL(1) CHAR8 *
-strdup(const CHAR8 * const src)
-{
- UINTN len;
- CHAR8 *news = NULL;
-
- len = strlena(src);
- news = AllocateZeroPool(len + 1);
- if (news)
- strncpya(news, src, len);
- return news;
-}
-
-static inline UNUSED NONNULL(1) CHAR8 *
-strndupa(const CHAR8 * const src, const UINTN srcmax)
-{
- UINTN len;
- CHAR8 *news = NULL;
-
- len = strnlena(src, srcmax);
- news = AllocateZeroPool(len + 1);
- if (news)
- strncpya(news, src, len);
- return news;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1, 2) char *
-stpcpy(char *dest, const char * const src)
-{
- size_t i = 0;
- for (i = 0; src[i]; i++)
- dest[i] = src[i];
- dest[i] = '\000';
- return &dest[i];
-}
-
-static inline UNUSED CHAR8 *
-translate_slashes(CHAR8 *out, const char *str)
-{
- int i;
- int j;
- if (str == NULL || out == NULL)
- return NULL;
-
- for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
- if (str[i] == '\\') {
- out[j] = '/';
- if (str[i+1] == '\\')
- i++;
- } else
- out[j] = str[i];
- }
- out[j] = '\0';
- return out;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1) CHAR8 *
-strchrnula(const CHAR8 *s, int c)
-{
- unsigned int i;
-
- for (i = 0; s[i] != '\000' && s[i] != c; i++)
- ;
-
- return (CHAR8 *)&s[i];
-}
-
-static inline UNUSED NONNULL(1) CHAR8 *
-strchra(const CHAR8 *s, int c)
-{
- const CHAR8 *s1;
-
- s1 = strchrnula(s, c);
- if (!s1 || s1[0] == '\000')
- return NULL;
-
- return (CHAR8 *)s1;
-}
-
-static inline UNUSED RETURNS_NONNULL NONNULL(1) char *
+static inline UNUSED RETURNS_NONNULL NONNULL(1)
+char *
strnchrnul(const char *s, size_t max, int c)
{
unsigned int i;
diff --git a/include/system/alloca.h b/include/system/alloca.h
index dc11b60d..a9d1aab1 100644
--- a/include/system/alloca.h
+++ b/include/system/alloca.h
@@ -5,6 +5,12 @@
#ifndef _ALLOCA_H
#define _ALLOCA_H
+#include <builtins_begin_.h>
+mkbi1_(void *, alloca, size_t, size)
+#define alloca_with_align(size, alignment) __builtin_alloca_with_align(size, alignment)
+#define alloca_with_align_and_max(size, alignment, max) __builtin_alloca_with_align_and_max(size, alignment, max)
+#include <builtins_end_.h>
+
#endif /* !_ALLOCA_H */
#endif
// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/builtins_begin_.h b/include/system/builtins_begin_.h
new file mode 100644
index 00000000..92ea5e3a
--- /dev/null
+++ b/include/system/builtins_begin_.h
@@ -0,0 +1,94 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+/**
+ * macros to build builtin wrappers
+ */
+#ifndef mkbi_cat_
+#define mkbi_cat_(a, b) a##b
+#endif
+#ifdef SHIM_STRING_C_
+
+#ifndef mkbi1_
+#define mkbi1_(rtype, x, typea, a) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+#ifndef mkbi2_
+#define mkbi2_(rtype, x, typea, a, typeb, b) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#ifndef mkbi3_
+#define mkbi3_(rtype, x, typea, a, typeb, b, typec, c) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#ifndef mkdepbi1_
+#define mkdepbi1_(rtype, x, typea, a) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#ifndef mkdepbi2_
+#define mkdepbi2_(rtype, x, typea, a, typeb, b) __typeof__(mkbi_cat_(__builtin_, x)) x;
+#endif
+
+#else /* ! SHIM_STRING_C_ */
+
+#ifndef mkbi1_
+#define mkbi1_(rtype, x, typea, a) \
+ static inline __attribute__((__unused__)) \
+ rtype \
+ x(typea a) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a); \
+ }
+#endif
+
+#ifndef mkbi2_
+#define mkbi2_(rtype, x, typea, a, typeb, b) \
+ static inline __attribute__((__unused__)) \
+ rtype \
+ x(typea a, typeb b) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a, b); \
+ }
+#endif
+
+#ifndef mkbi3_
+#define mkbi3_(rtype, x, typea, a, typeb, b, typec, c) \
+ static inline __attribute__((__unused__)) \
+ rtype \
+ x(typea a, typeb b, typec c) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a, b,c); \
+ }
+#endif
+
+#ifdef SHIM_DEPRECATE_STRLEN
+#ifndef mkdepbi_dep_
+#define mkdepbi_dep_ __attribute__((__deprecated__))
+#endif
+#else /* !SHIM_DEPRECATE_STRLEN */
+#ifndef mkdepbi_dep_
+#define mkdepbi_dep_
+#endif
+#endif /* SHIM_DEPRECATE_STRLEN */
+
+#ifndef mkdepbi1_
+#define mkdepbi1_(rtype, x, typea, a) \
+ static inline __attribute__((__unused__)) \
+ mkdepbi_dep_ \
+ rtype \
+ x(typea a) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a); \
+ }
+#endif
+
+#ifndef mkdepbi2_
+#define mkdepbi2_(rtype, x, typea, a, typeb, b) \
+ static inline __attribute__((__unused__)) \
+ mkdepbi_dep_ \
+ rtype \
+ x(typea a, typeb b) \
+ { \
+ return mkbi_cat_(__builtin_, x)(a, b); \
+ }
+#endif
+#endif /* SHIM_STRING_C_ */
+
+// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/builtins_end_.h b/include/system/builtins_end_.h
new file mode 100644
index 00000000..0a6ad60a
--- /dev/null
+++ b/include/system/builtins_end_.h
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+
+#ifdef mkbi1_
+#undef mkbi1_
+#endif
+#ifdef mkbi2_
+#undef mkbi2_
+#endif
+#ifdef mkbi3_
+#undef mkbi3_
+#endif
+#ifdef mkdepbi1_
+#undef mkdepbi1_
+#endif
+#ifdef mkdepbi2_
+#undef mkdepbi2_
+#endif
+#ifdef mkdepbi_dep_
+#undef mkdepbi_dep_
+#endif
+#ifdef mkbi_cat_
+#undef mkbi_cat_
+#endif
+
+#ifdef _BUILTINS_BEGIN__H
+#undef _BUILTINS_BEGIN__H
+#endif
+// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/ctype.h b/include/system/ctype.h
index c771bb69..65e7348f 100644
--- a/include/system/ctype.h
+++ b/include/system/ctype.h
@@ -3,11 +3,82 @@
* ctype.h - standard ctype functions
*/
#ifdef SHIM_UNIT_TEST
-#include_next <string.h>
+#include_next <ctype.h>
#else
#ifndef _CTYPE_H
#define _CTYPE_H
+#define isprint(c) ((c) >= 0x20 && (c) <= 0x7e)
+
+/* Determines if a particular character is a decimal-digit character */
+static inline __attribute__((__unused__)) int
+isdigit(int c)
+{
+ //
+ // <digit> ::= [0-9]
+ //
+ return (('0' <= (c)) && ((c) <= '9'));
+}
+
+/* Determine if an integer represents character that is a hex digit */
+static inline __attribute__((__unused__)) int
+isxdigit(int c)
+{
+ //
+ // <hexdigit> ::= [0-9] | [a-f] | [A-F]
+ //
+ return ((('0' <= (c)) && ((c) <= '9')) ||
+ (('a' <= (c)) && ((c) <= 'f')) ||
+ (('A' <= (c)) && ((c) <= 'F')));
+}
+
+/* Determines if a particular character represents a space character */
+static inline __attribute__((__unused__)) int
+isspace(int c)
+{
+ //
+ // <space> ::= [ ]
+ //
+ return ((c) == ' ');
+}
+
+/* Determine if a particular character is an alphanumeric character */
+static inline __attribute__((__unused__)) int
+isalnum(int c)
+{
+ //
+ // <alnum> ::= [0-9] | [a-z] | [A-Z]
+ //
+ return ((('0' <= (c)) && ((c) <= '9')) ||
+ (('a' <= (c)) && ((c) <= 'z')) ||
+ (('A' <= (c)) && ((c) <= 'Z')));
+}
+
+/* Determines if a particular character is in upper case */
+static inline __attribute__((__unused__)) int
+isupper(int c)
+{
+ //
+ // <uppercase letter> := [A-Z]
+ //
+ return (('A' <= (c)) && ((c) <= 'Z'));
+}
+
+/* Convert character to lowercase */
+static inline __attribute__((__unused__)) int
+tolower(int c)
+{
+ if (('A' <= (c)) && ((c) <= 'Z')) {
+ return (c - ('A' - 'a'));
+ }
+ return (c);
+}
+
+static inline __attribute__((__unused__)) int
+toupper(int c)
+{
+ return ((c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c);
+}
#endif /* !_CTYPE_H */
#endif /* !SHIM_UNIT_TEST */
diff --git a/include/system/stdlib.h b/include/system/stdlib.h
index f2660f63..da7d3af9 100644
--- a/include/system/stdlib.h
+++ b/include/system/stdlib.h
@@ -11,6 +11,18 @@
*/
#include <stddef.h>
+static inline void abort(void) { }
+
+#include <builtins_begin_.h>
+mkbi1_(int, abs, int, j)
+mkbi1_(long int, labs, long int, j)
+mkbi1_(long long int, llabs, long long int, j)
+
+#ifdef _INTTYPES_H
+mkbi1_(intmax_t, imaxabs, intmax_t, j)
+#endif /* _INTTYPES_H */
+#include <builtins_end_.h>
+
#endif /* !_STDLIB_H */
#endif
// vim:fenc=utf-8:tw=75:noet
diff --git a/include/system/string.h b/include/system/string.h
index 21e46c1d..822e28fa 100644
--- a/include/system/string.h
+++ b/include/system/string.h
@@ -7,8 +7,55 @@
#include <stddef.h>
-__typeof__(__builtin_memset) memset;
-__typeof__(__builtin_memcpy) memcpy;
+#include <builtins_begin_.h>
+
+mkbi1_(long int, ffsl, long int, x)
+mkbi1_(long int, clzl, long int, x)
+mkbi1_(long int, ctzl, long int, x)
+mkbi1_(long int, clrsbl, long int, x)
+mkbi1_(long int, popcountl, long int, x)
+mkbi1_(long int, parityl, long int, x)
+mkbi1_(long long int, ffsll, long long int, x)
+mkbi1_(long long int, clzll, long long int, x)
+mkbi1_(long long int, ctzll, long long int, x)
+mkbi1_(long long int, clrsbll, long long int, x)
+mkbi1_(long long int, popcountll, long long int, x)
+mkbi1_(long long int, parityll, long long int, x)
+
+mkbi3_(int, bcmp, const void *, s1, const void *, s2, size_t, n)
+mkbi3_(void, bcopy, const void *, src, void *, dest, size_t, n)
+mkbi2_(void, bzero, void *, s, size_t, n)
+mkdepbi2_(char *, index, const char *, s, int, c)
+mkbi3_(void *, memchr, const void *, s, int, c, size_t, n)
+mkbi3_(int, memcmp, const void *, s1, const void *, s2, size_t, n)
+mkbi3_(void *, memcpy, void *, dest, const void *, src, size_t, n)
+mkbi3_(void *, memmove, void *, dest, const void *, src, size_t, n)
+mkbi3_(void *, mempcpy, void *, dest, const void *, src, size_t, n)
+mkdepbi2_(char *, rindex, const char *, s, int, c)
+mkdepbi2_(char *, stpcpy, char *, dest, const char *, src)
+mkbi3_(char *, stpncpy, char *, dest, const char *, src, size_t, n)
+mkdepbi2_(int, strcasecmp, const char *, s1, const char *, s2)
+mkdepbi2_(char *, strcat, char *, dest, const char *, src)
+mkdepbi2_(char *, strchr, const char *, s, int, c)
+mkdepbi2_(int, strcmp, const char *, s1, const char *, s2)
+mkdepbi2_(char *, strcpy, char *, dest, const char *, src)
+mkdepbi2_(size_t, strcspn, const char *, s, const char *, reject)
+mkdepbi1_(char *, strdup, const char *, s)
+mkbi2_(char *, strndup, const char *, s, size_t, n)
+mkdepbi1_(size_t, strlen, const char *, s)
+mkbi3_(int, strncasecmp, const char *, s1, const char *, s2, size_t, n)
+mkbi3_(char *, strncat, char *, dest, const char *, src, size_t, n)
+mkbi3_(int, strncmp, const char *, s1, const char *, s2, size_t, n)
+mkbi3_(char *, strncpy, char *, dest, const char *, src, size_t, n)
+mkbi2_(int, strnlen, const char *, s1, size_t, n)
+mkdepbi2_(char *, strpbrk, const char *, s, const char *, accept)
+mkdepbi2_(char *, strrchr, const char *, s, int, c)
+mkdepbi2_(size_t, strspn, const char *, s, const char *, accept)
+mkdepbi2_(char *, strstr, const char *, haystack, const char *, needle)
+
+mkbi3_(void *, memset, void *, s, int, c, size_t, n);
+
+#include <builtins_end_.h>
#endif /* _STRING_H */
#endif
diff --git a/include/system/strings.h b/include/system/strings.h
index c82bd917..99bc05f2 100644
--- a/include/system/strings.h
+++ b/include/system/strings.h
@@ -5,6 +5,15 @@
#ifndef _STRINGS_H
#define _STRINGS_H
+#include <builtins_begin_.h>
+mkbi1_(int, ffs, int, x)
+mkbi1_(int, clz, int, x)
+mkbi1_(int, ctz, int, x)
+mkbi1_(int, clrsb, int, x)
+mkbi1_(int, popcount, int, x)
+mkbi1_(int, parity, int, x)
+#include <builtins_end_.h>
+
#endif /* !_STRINGS_H */
#endif
// vim:fenc=utf-8:tw=75:noet
diff --git a/lib/string.c b/lib/string.c
new file mode 100644
index 00000000..1f58f3ec
--- /dev/null
+++ b/lib/string.c
@@ -0,0 +1,245 @@
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+/*
+ * string.c - implementations we need for string finctions
+ */
+#define SHIM_STRING_C_
+#include "shim.h"
+
+size_t
+strlen(const char *s1)
+{
+ size_t len;
+
+ for (len = 0; *s1; s1 += 1, len += 1)
+ ;
+ return len;
+}
+
+int
+strcmp(const char *s1p, const char *s2p)
+{
+ const uint8_t *s1 = (const uint8_t *)s1p;
+ const uint8_t *s2 = (const uint8_t *)s2p;
+
+ while (*s1) {
+ if (*s1 != *s2) {
+ break;
+ }
+
+ s1 += 1;
+ s2 += 1;
+ }
+
+ return *s1 - *s2;
+}
+
+int
+strncmp(const char *s1p, const char *s2p, size_t len)
+{
+ const uint8_t *s1 = (const uint8_t *)s1p;
+ const uint8_t *s2 = (const uint8_t *)s2p;
+
+ while (*s1 && len) {
+ if (*s1 != *s2) {
+ break;
+ }
+
+ s1 += 1;
+ s2 += 1;
+ len -= 1;
+ }
+
+ return len ? *s1 - *s2 : 0;
+}
+
+/* Based on AsciiStriCmp() in edk2 MdePkg/Library/BaseLib/String.c */
+int
+strncasecmp(const char *s1p, const char *s2p, size_t n)
+{
+ const uint8_t *s1 = (const uint8_t *)s1p;
+ const uint8_t *s2 = (const uint8_t *)s2p;
+
+ while (*s1 && n) {
+ if (toupper(*s1) != toupper(*s2)) {
+ break;
+ }
+
+ s1 += 1;
+ s2 += 1;
+ n -= 1;
+ }
+
+ return n ? *s1 - *s2 : 0;
+}
+
+/* Based on AsciiStriCmp() in edk2 MdePkg/Library/BaseLib/String.c */
+int
+strcasecmp(const char *str1, const char *str2)
+{
+ char c1, c2;
+
+ c1 = toupper(*str1);
+ c2 = toupper(*str2);
+ while ((*str1 != '\0') && (c1 == c2)) {
+ str1++;
+ str2++;
+ c1 = toupper(*str1);
+ c2 = toupper(*str2);
+ }
+
+ return c1 - c2;
+}
+
+/* Scan a string for the last occurrence of a character */
+char *
+strrchr(const char *str, int c)
+{
+ char *save;
+
+ for (save = NULL;; ++str) {
+ if (*str == c) {
+ save = (char *)str;
+ }
+ if (*str == 0) {
+ return (save);
+ }
+ }
+}
+
+NONNULL(1)
+size_t
+strnlen(const char *s, size_t n)
+{
+ size_t i;
+ for (i = 0; i < n; i++)
+ if (s[i] == '\0')
+ break;
+ return i;
+}
+
+RETURNS_NONNULL NONNULL(1, 2)
+char *
+strcpy(char *dest, const char *src)
+{
+ size_t i;
+
+ for (i = 0; src[i] != '\0'; i++)
+ dest[i] = src[i];
+
+ dest[i] = '\0';
+ return dest;
+}
+
+RETURNS_NONNULL NONNULL(1, 2)
+char *
+strncpy(char *dest, const char *src, size_t n)
+{
+ size_t i;
+
+ for (i = 0; i < n && src[i] != '\0'; i++)
+ dest[i] = src[i];
+ for (; i < n; i++)
+ dest[i] = '\0';
+
+ return dest;
+}
+
+RETURNS_NONNULL NONNULL(1, 2)
+char *
+strcat(char *dest, const char *src)
+{
+ size_t dest_len = strlen(dest);
+ size_t i;
+
+ for (i = 0; src[i] != '\0'; i++)
+ dest[dest_len + i] = src[i];
+ dest[dest_len + i] = '\0';
+
+ return dest;
+}
+
+NONNULL(1)
+char *
+strdup(const char *const src)
+{
+ size_t len;
+ char *news = NULL;
+
+ len = strlen(src);
+ news = AllocateZeroPool(len + 1);
+ if (news)
+ strncpy(news, src, len);
+ return news;
+}
+
+NONNULL(1)
+char *
+strndup(const char *const src, const size_t srcmax)
+{
+ size_t len;
+ char *news = NULL;
+
+ len = strnlen(src, srcmax);
+ news = AllocateZeroPool(len + 1);
+ if (news)
+ strncpy(news, src, len);
+ return news;
+}
+
+RETURNS_NONNULL NONNULL(1, 2)
+char *
+stpcpy(char *dest, const char *const src)
+{
+ size_t i = 0;
+ for (i = 0; src[i]; i++)
+ dest[i] = src[i];
+ dest[i] = '\000';
+ return &dest[i];
+}
+
+RETURNS_NONNULL NONNULL(1)
+char *
+strchrnul(const char *s, int c)
+{
+ unsigned int i;
+
+ for (i = 0; s[i] != '\000' && s[i] != c; i++)
+ ;
+
+ return (char *)&s[i];
+}
+
+NONNULL(1)
+char *
+strchr(const char *s, int c)
+{
+ const char *s1;
+
+ s1 = strchrnul(s, c);
+ if (!s1 || s1[0] == '\000')
+ return NULL;
+
+ return (char *)s1;
+}
+
+char *
+translate_slashes(char *out, const char *str)
+{
+ int i;
+ int j;
+ if (str == NULL || out == NULL)
+ return NULL;
+
+ for (i = 0, j = 0; str[i] != '\0'; i++, j++) {
+ if (str[i] == '\\') {
+ out[j] = '/';
+ if (str[i + 1] == '\\')
+ i++;
+ } else
+ out[j] = str[i];
+ }
+ out[j] = '\0';
+ return out;
+}
+
+// vim:fenc=utf-8:tw=75:noet
diff --git a/mok.c b/mok.c
index 6bd506be..be477c48 100644
--- a/mok.c
+++ b/mok.c
@@ -1013,7 +1013,7 @@ EFI_STATUS import_mok_state(EFI_HANDLE image_handle)
struct mok_state_variable *v = &mok_state_variables[i];
ZeroMem(&config_template, sizeof(config_template));
- strncpya(config_template.name, (CHAR8 *)v->rtname8, 255);
+ strncpy(config_template.name, (CHAR8 *)v->rtname8, 255);
config_template.name[255] = '\0';
config_template.data_size = v->data_size;
diff --git a/netboot.c b/netboot.c
index 450e9def..3f5c5198 100644
--- a/netboot.c
+++ b/netboot.c
@@ -172,7 +172,7 @@ static BOOLEAN extract_tftp_info(CHAR8 *url)
// to check against str2ip6() errors
memset(ip6inv, 0, sizeof(ip6inv));
- if (strncmp((UINT8 *)url, (UINT8 *)"tftp://", 7)) {
+ if (strncmp((const char *)url, (const char *)"tftp://", 7)) {
console_print(L"URLS MUST START WITH tftp://\n");
return FALSE;
}
@@ -258,7 +258,7 @@ static EFI_STATUS parseDhcp4()
pkt_v4 = &pxe->Mode->PxeReply.Dhcpv4;
}
- INTN dir_len = strnlena((CHAR8 *)pkt_v4->BootpBootFile, 127);
+ INTN dir_len = strnlen((CHAR8 *)pkt_v4->BootpBootFile, 127);
INTN i;
UINT8 *dir = pkt_v4->BootpBootFile;
@@ -274,13 +274,13 @@ static EFI_STATUS parseDhcp4()
return EFI_OUT_OF_RESOURCES;
if (dir_len > 0) {
- strncpya(full_path, (CHAR8 *)dir, dir_len);
+ strncpy(full_path, (CHAR8 *)dir, dir_len);
if (full_path[dir_len-1] == '/' && template[0] == '/')
full_path[dir_len-1] = '\0';
}
if (dir_len == 0 && dir[0] != '/' && template[0] == '/')
template_ofs++;
- strcata(full_path, template + template_ofs);
+ strcat(full_path, template + template_ofs);
memcpy(&tftp_addr.v4, pkt_v4->BootpSiAddr, 4);
return EFI_SUCCESS;
diff --git a/sbat.c b/sbat.c
index d8750962..fec22a73 100644
--- a/sbat.c
+++ b/sbat.c
@@ -48,7 +48,7 @@ parse_sbat_section(char *section_base, size_t section_size,
efi_status = EFI_INVALID_PARAMETER;
goto err;
}
- allocsz += strlena(row->columns[i]) + 1;
+ allocsz += strlen(row->columns[i]) + 1;
}
n++;
}
@@ -226,7 +226,7 @@ parse_sbat_var_data(list_t *entry_list, UINT8 *data, UINTN datasize)
efi_status = EFI_INVALID_PARAMETER;
goto err;
}
- allocsz += strlena(row->columns[i]) + 1;
+ allocsz += strlen(row->columns[i]) + 1;
}
n++;
}
diff --git a/shim.h b/shim.h
index 61dafa82..3d2ac2d4 100644
--- a/shim.h
+++ b/shim.h
@@ -259,4 +259,6 @@ verify_buffer (char *data, int datasize,
#define LogError(fmt, ...)
#endif
+char *translate_slashes(char *out, const char *str);
+
#endif /* SHIM_H_ */