Skip to content

Commit

Permalink
Consolidate most of our standard lib functions to lib
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Jones <pjones@redhat.com>
  • Loading branch information
vathpela committed Mar 10, 2021
1 parent 7880982 commit 766aac4
Show file tree
Hide file tree
Showing 25 changed files with 566 additions and 318 deletions.
23 changes: 1 addition & 22 deletions Cryptlib/Include/OpenSslSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...);
Expand Down Expand Up @@ -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 *);

//
Expand All @@ -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)

//
Expand All @@ -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
3 changes: 1 addition & 2 deletions Cryptlib/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
34 changes: 2 additions & 32 deletions Cryptlib/SysCall/BaseStrings.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
Expand Down Expand Up @@ -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
Expand Down
82 changes: 0 additions & 82 deletions Cryptlib/SysCall/CrtWrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, ...)
{
Expand All @@ -146,59 +131,6 @@ int sscanf (const char *buffer, const char *format, ...)
return 0;
}

//
// -- 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 --
//
Expand All @@ -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 --
//
Expand Down Expand Up @@ -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, ...)
{

Expand Down
40 changes: 0 additions & 40 deletions Cryptlib/SysCall/memset.c

This file was deleted.

4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
4 changes: 2 additions & 2 deletions csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion gnu-efi
Submodule gnu-efi updated 4 files
+0 −18 inc/efilib.h
+3 −1 lib/init.c
+2 −1 lib/misc.c
+0 −60 lib/str.c
14 changes: 7 additions & 7 deletions httpboot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -156,18 +156,18 @@ 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 {
return EFI_INVALID_PARAMETER;
}

/* Extract the path */
next_len = strlena(next_loader);
next_len = strlen(next_loader);
while (*ptr != '\0') {
count++;
if (*ptr == '/')
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
5 changes: 5 additions & 0 deletions include/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions include/endian.h
Original file line number Diff line number Diff line change
@@ -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
2 changes: 0 additions & 2 deletions include/hexdump.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
Loading

0 comments on commit 766aac4

Please sign in to comment.