Skip to content

Commit 766aac4

Browse files
committed
Consolidate most of our standard lib functions to lib
Signed-off-by: Peter Jones <[email protected]>
1 parent 7880982 commit 766aac4

25 files changed

+566
-318
lines changed

Cryptlib/Include/OpenSslSupport.h

+1-22
Original file line numberDiff line numberDiff line change
@@ -288,34 +288,21 @@ extern int errno;
288288
void *malloc (size_t);
289289
void *realloc (void *, size_t);
290290
void free (void *);
291-
int isdigit (int);
292-
int isspace (int);
293-
int tolower (int);
294-
int isupper (int);
295-
int isxdigit (int);
296-
int isalnum (int);
297291
void *memcpy (void *, const void *, size_t);
298292
void *memset (void *, int, size_t);
299293
void *memchr (const void *, int, size_t);
300294
int memcmp (const void *, const void *, size_t);
301295
void *memmove (void *, const void *, size_t);
302-
int strcmp (const char *, const char *);
303-
int strncmp (const char *, const char *, size_t);
304296
char *strcpy (char *, const char *);
305297
char *strncpy (char *, const char *, size_t);
306-
size_t strlen (const char *);
307298
char *strcat (char *, const char *);
308299
char *strchr (const char *, int);
309300
int strcasecmp (const char *, const char *);
310301
int strncasecmp (const char *, const char *, size_t);
311302
char *strncpy (char *, const char *, size_t);
312-
int strncmp (const char *, const char *, size_t);
313-
char *strrchr (const char *, int);
314303
unsigned long strtoul (const char *, char **, int);
315304
long strtol (const char *, char **, int);
316305
char *strerror (int);
317-
size_t strspn (const char *, const char *);
318-
size_t strcspn (const char *, const char *);
319306
int printf (const char *, ...);
320307
int sscanf (const char *, const char *, ...);
321308
int open (const char *, int, ...);
@@ -351,7 +338,6 @@ gid_t getegid (void);
351338
void qsort (void *, size_t, size_t, int (*)(const void *, const void *));
352339
char *getenv (const char *);
353340
void exit (int);
354-
void abort (void);
355341
__sighandler_t *signal (int, __sighandler_t *);
356342

357343
//
@@ -361,7 +347,7 @@ extern FILE *stderr;
361347
extern FILE *stdin;
362348
extern FILE *stdout;
363349

364-
#define AsciiStrLen(x) strlena(x)
350+
#define AsciiStrLen(x) strlen(x)
365351
#define AsciiStrnCmp(s1, s2, len) strncmpa((CHAR8 *)s1, (CHAR8 *)s2, len)
366352

367353
//
@@ -372,17 +358,10 @@ extern FILE *stdout;
372358
#define memchr(buf,ch,count) ScanMem8((CHAR8 *)buf,(UINTN)(count),ch)
373359
#define memcmp(buf1,buf2,count) (int)(CompareMem(buf1,buf2,(UINTN)(count)))
374360
#define memmove(dest,source,count) CopyMem(dest,source,(UINTN)(count))
375-
#define strlen(str) (size_t)(AsciiStrLen((CHAR8 *)str))
376-
#define strcpy(strDest,strSource) AsciiStrCpy((CHAR8 *)strDest,(const CHAR8 *)strSource)
377-
#define strncpy(strDest,strSource,count) AsciiStrnCpy((CHAR8 *)strDest,(const CHAR8 *)strSource,(UINTN)count)
378-
#define strcat(strDest,strSource) AsciiStrCat((CHAR8 *)strDest,(const CHAR8 *)strSource)
379-
#define strchr(str,ch) (char *)(ScanMem8((CHAR8 *)str,AsciiStrSize((CHAR8 *)str),ch))
380-
#define strncmp(string1,string2,count) (int)(AsciiStrnCmp((const CHAR8 *)string1, (const CHAR8 *)string2,(UINTN)(count)))
381361
#define localtime(timer) NULL
382362
#define assert(expression)
383363
#define atoi(nptr) AsciiStrDecimalToUintn((const CHAR8 *)nptr)
384364
#define gettimeofday(tvp,tz) do { (tvp)->tv_sec = time(NULL); (tvp)->tv_usec = 0; } while (0)
385365
#define gmtime_r(timer,result) (result = NULL)
386-
#define abort()
387366

388367
#endif

Cryptlib/Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,7 @@ OBJS = Hash/CryptMd4Null.o \
6363
SysCall/CrtWrapper.o \
6464
SysCall/TimerWrapper.o \
6565
SysCall/BaseMemAllocation.o \
66-
SysCall/BaseStrings.o \
67-
SysCall/memset.o
66+
SysCall/BaseStrings.o
6867

6968
all: $(TARGET)
7069

Cryptlib/SysCall/BaseStrings.c

+2-32
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
CHAR8 *
44
AsciiStrCat(CHAR8 *Destination, const CHAR8 *Source)
55
{
6-
UINTN dest_len = strlena((CHAR8 *)Destination);
6+
UINTN dest_len = strlen((CHAR8 *)Destination);
77
UINTN i;
88

99
for (i = 0; Source[i] != '\0'; i++)
@@ -61,37 +61,7 @@ WriteUnaligned32(UINT32 *Buffer, UINT32 Value)
6161
UINTN
6262
AsciiStrSize(const CHAR8 *string)
6363
{
64-
return strlena(string) + 1;
65-
}
66-
67-
int
68-
strcmp (const char *str1, const char *str2)
69-
{
70-
return strcmpa((CHAR8 *)str1,(CHAR8 *)str2);
71-
}
72-
73-
inline static char
74-
toupper (char c)
75-
{
76-
return ((c >= 'a' && c <= 'z') ? c - ('a' - 'A') : c);
77-
}
78-
79-
/* Based on AsciiStriCmp() in edk2 MdePkg/Library/BaseLib/String.c */
80-
int
81-
strcasecmp (const char *str1, const char *str2)
82-
{
83-
char c1, c2;
84-
85-
c1 = toupper (*str1);
86-
c2 = toupper (*str2);
87-
while ((*str1 != '\0') && (c1 == c2)) {
88-
str1++;
89-
str2++;
90-
c1 = toupper (*str1);
91-
c2 = toupper (*str2);
92-
}
93-
94-
return c1 - c2;
64+
return strlen(string) + 1;
9565
}
9666

9767
/* Based on AsciiStrDecimalToUintnS() in edk2

Cryptlib/SysCall/CrtWrapper.c

-82
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,6 @@ QuickSortWorker (
121121
// -- String Manipulation Routines --
122122
//
123123

124-
/* Scan a string for the last occurrence of a character */
125-
char *strrchr (const char *str, int c)
126-
{
127-
char * save;
128-
129-
for (save = NULL; ; ++str) {
130-
if (*str == c) {
131-
save = (char *)str;
132-
}
133-
if (*str == 0) {
134-
return (save);
135-
}
136-
}
137-
}
138-
139124
/* Read formatted data from a string */
140125
int sscanf (const char *buffer, const char *format, ...)
141126
{
@@ -146,59 +131,6 @@ int sscanf (const char *buffer, const char *format, ...)
146131
return 0;
147132
}
148133

149-
//
150-
// -- Character Classification Routines --
151-
//
152-
153-
/* Determines if a particular character is a decimal-digit character */
154-
int isdigit (int c)
155-
{
156-
//
157-
// <digit> ::= [0-9]
158-
//
159-
return (('0' <= (c)) && ((c) <= '9'));
160-
}
161-
162-
/* Determine if an integer represents character that is a hex digit */
163-
int isxdigit (int c)
164-
{
165-
//
166-
// <hexdigit> ::= [0-9] | [a-f] | [A-F]
167-
//
168-
return ((('0' <= (c)) && ((c) <= '9')) ||
169-
(('a' <= (c)) && ((c) <= 'f')) ||
170-
(('A' <= (c)) && ((c) <= 'F')));
171-
}
172-
173-
/* Determines if a particular character represents a space character */
174-
int isspace (int c)
175-
{
176-
//
177-
// <space> ::= [ ]
178-
//
179-
return ((c) == ' ');
180-
}
181-
182-
/* Determine if a particular character is an alphanumeric character */
183-
int isalnum (int c)
184-
{
185-
//
186-
// <alnum> ::= [0-9] | [a-z] | [A-Z]
187-
//
188-
return ((('0' <= (c)) && ((c) <= '9')) ||
189-
(('a' <= (c)) && ((c) <= 'z')) ||
190-
(('A' <= (c)) && ((c) <= 'Z')));
191-
}
192-
193-
/* Determines if a particular character is in upper case */
194-
int isupper (int c)
195-
{
196-
//
197-
// <uppercase letter> := [A-Z]
198-
//
199-
return (('A' <= (c)) && ((c) <= 'Z'));
200-
}
201-
202134
//
203135
// -- Data Conversion Routines --
204136
//
@@ -223,15 +155,6 @@ unsigned long strtoul (const char *nptr, char **endptr, int base)
223155
return 0;
224156
}
225157

226-
/* Convert character to lowercase */
227-
int tolower (int c)
228-
{
229-
if (('A' <= (c)) && ((c) <= 'Z')) {
230-
return (c - ('A' - 'a'));
231-
}
232-
return (c);
233-
}
234-
235158
//
236159
// -- Searching and Sorting Routines --
237160
//
@@ -424,11 +347,6 @@ int stat (const char *c, struct stat *s)
424347
return -1;
425348
}
426349

427-
int strncasecmp (const char *c, const char *s, size_t l)
428-
{
429-
return 0;
430-
}
431-
432350
void syslog (int a, const char *c, ...)
433351
{
434352

Cryptlib/SysCall/memset.c

-40
This file was deleted.

Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@ LIBS = Cryptlib/libcryptlib.a \
116116
gnu-efi/$(ARCH_GNUEFI)/gnuefi/libgnuefi.a
117117

118118
$(SHIMSONAME): $(OBJS) $(LIBS)
119-
$(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS)
119+
$(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS) lib/lib.a
120120

121121
fallback.o: $(FALLBACK_SRCS)
122122

123123
$(FBSONAME): $(FALLBACK_OBJS) $(LIBS)
124-
$(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS)
124+
$(LD) -o $@ $(LDFLAGS) $^ $(EFI_LIBS) lib/lib.a
125125

126126
MokManager.o: $(MOK_SOURCES)
127127

csv.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ parse_csv_line(char * line, size_t max, size_t *n_columns, const char *columns[]
2121
valid = strntoken(next, max, delims, &token, &state);
2222
}
2323
if (valid) {
24-
next += strlena(token) + 1;
25-
max -= strlena(token) + 1;
24+
next += strlen(token) + 1;
25+
max -= strlen(token) + 1;
2626
columns[n] = token;
2727
new_n = n + 1;
2828
} else {

httpboot.c

+7-7
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ find_httpboot (EFI_HANDLE device)
130130

131131
/* Save the current URI */
132132
UriNode = (URI_DEVICE_PATH *)Node;
133-
uri_size = strlena(UriNode->Uri);
133+
uri_size = strlen(UriNode->Uri);
134134
uri = AllocatePool(uri_size + 1);
135135
if (!uri) {
136136
perror(L"Failed to allocate uri\n");
@@ -156,18 +156,18 @@ generate_next_uri (CONST CHAR8 *current_uri, CONST CHAR8 *next_loader,
156156
UINTN path_len = 0;
157157
UINTN count = 0;
158158

159-
if (strncmpa(current_uri, (CHAR8 *)"http://", 7) == 0) {
159+
if (strncmp(current_uri, (CHAR8 *)"http://", 7) == 0) {
160160
ptr = current_uri + 7;
161161
count += 7;
162-
} else if (strncmpa(current_uri, (CHAR8 *)"https://", 8) == 0) {
162+
} else if (strncmp(current_uri, (CHAR8 *)"https://", 8) == 0) {
163163
ptr = current_uri + 8;
164164
count += 8;
165165
} else {
166166
return EFI_INVALID_PARAMETER;
167167
}
168168

169169
/* Extract the path */
170-
next_len = strlena(next_loader);
170+
next_len = strlen(next_loader);
171171
while (*ptr != '\0') {
172172
count++;
173173
if (*ptr == '/')
@@ -192,9 +192,9 @@ extract_hostname (CONST CHAR8 *url, CHAR8 **hostname)
192192
CONST CHAR8 *ptr, *start;
193193
UINTN host_len = 0;
194194

195-
if (strncmpa(url, (CHAR8 *)"http://", 7) == 0)
195+
if (strncmp(url, (CHAR8 *)"http://", 7) == 0)
196196
start = url + 7;
197-
else if (strncmpa(url, (CHAR8 *)"https://", 8) == 0)
197+
else if (strncmp(url, (CHAR8 *)"https://", 8) == 0)
198198
start = url + 8;
199199
else
200200
return EFI_INVALID_PARAMETER;
@@ -571,7 +571,7 @@ receive_http_response(EFI_HTTP_PROTOCOL *http, VOID **buffer, UINT64 *buf_size)
571571

572572
/* Check the length of the file */
573573
for (i = 0; i < rx_message.HeaderCount; i++) {
574-
if (!strcmpa(rx_message.Headers[i].FieldName, (CHAR8 *)"Content-Length")) {
574+
if (!strcmp(rx_message.Headers[i].FieldName, (CHAR8 *)"Content-Length")) {
575575
*buf_size = ascii_to_int(rx_message.Headers[i].FieldValue);
576576
}
577577
}

include/compiler.h

+5
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,10 @@
179179
#define MIN(a, b) ({(a) < (b) ? (a) : (b);})
180180
#define MAX(a, b) ({(a) <= (b) ? (b) : (a);})
181181

182+
/**
183+
* Builtins that don't go in string.h
184+
*/
185+
#define unreachable() __builtin_unreachable()
186+
182187
#endif /* !COMPILER_H_ */
183188
// vim:fenc=utf-8:tw=75:et

include/endian.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// SPDX-License-Identifier: BSD-2-Clause-Patent
2+
/*
3+
* endian.h - bswap decls that can't go in compiler.h
4+
* Copyright Peter Jones <[email protected]>
5+
*/
6+
7+
#ifndef ENDIAN_H_
8+
#define ENDIAN_H_
9+
10+
#include <stdint.h>
11+
12+
#include "system/builtins_begin_.h"
13+
mkbi1_(uint16_t, bswap16, uint16_t, x)
14+
mkbi1_(uint32_t, bswap32, uint32_t, x)
15+
mkbi1_(uint64_t, bswap64, uint64_t, x)
16+
#include "system/builtins_end_.h"
17+
18+
#endif /* !ENDIAN_H_ */
19+
// vim:fenc=utf-8:tw=75:noet

include/hexdump.h

-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ prepare_hex(const void *data, size_t size, char *buf, unsigned int position)
4848
return ret;
4949
}
5050

51-
#define isprint(c) ((c) >= 0x20 && (c) <= 0x7e)
52-
5351
static inline void UNUSED
5452
prepare_text(const void *data, size_t size, char *buf, unsigned int position)
5553
{

0 commit comments

Comments
 (0)