Skip to content

Commit 99a41ec

Browse files
committed
No <string.h> functions used at all if MSPACK_NO_DEFAULT_SYSTEM set
1 parent 546e3a6 commit 99a41ec

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

libmspack/ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
* macros.h: new header for the D(), LD/LU and EndGet???() macros.
44
Use this instead of system.h.
55

6+
* system.h: if MSPACK_NO_DEFAULT_SYSTEM is defined, define
7+
inline versions of the only standard C functions used in
8+
mspack (strlen, memcmp, memset), so that no standard C library
9+
functions are needed at all.
10+
611
2020-01-08 Stuart Caie <[email protected]>
712

813
* lzxd_decompress(): do not apply the E8 transformation on the

libmspack/Makefile.am

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ TESTS = $(check_PROGRAMS)
55

66
ACLOCAL_AMFLAGS = -I m4
77
AM_CFLAGS =
8-
# add "-DMSPACK_NO_DEFAULT_SYSTEM" to remove default mspack_system
8+
# add "-DMSPACK_NO_DEFAULT_SYSTEM" to remove default mspack_system.
9+
# however, note that many of the tests and examples provided DO rely on the
10+
# default mspack_system and will fail without it -- any program with a call
11+
# like "mspack_create_...(NULL)" expects a default mspack_system.
912
if GCC
1013
AM_CFLAGS += -Wall -Wextra -Wno-unused-parameter -Wno-unused-result
1114
endif

libmspack/mspack/system.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,25 @@ extern "C" {
2222
#include <macros.h>
2323

2424
/* assume <string.h> exists */
25-
#include <string.h>
25+
#ifndef MSPACK_NO_DEFAULT_SYSTEM
26+
# include <string.h>
27+
#else
28+
/* but if no default system wanted, avoid using <string.h> entirely,
29+
* to avoid linking to even these standard C library functions */
30+
static inline int memcmp(const void *s1, const void *s2, size_t n) {
31+
const unsigned char *a = s1, *b = s2;
32+
while (n--) if (*a++ != *b++) return a[-1] - b[-1];
33+
return 0;
34+
}
35+
static inline void *memset(void *s, int c, size_t n) {
36+
unsigned char *s2 = s, c2 = (unsigned char) c;
37+
while (n--) *s2++ = c2;
38+
return s;
39+
}
40+
static inline size_t strlen(const char *s) {
41+
size_t c = 0; while (*s++) c++; return c;
42+
}
43+
#endif
2644

2745
/* fix for problem with GCC 4 and glibc (thanks to Ville Skytta)
2846
* http://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=150429

0 commit comments

Comments
 (0)