[libc] Rewrite string functions

Some of the C library string functions have an unknown provenance.
Reimplement all such functions to avoid potential licensing
uncertainty.

Remove the inline-assembler versions of strlen(), memswap(), and
strncmp(); these save a minimal amount of space (around 40 bytes in
total) and are not performance-critical.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
This commit is contained in:
Michael Brown
2015-02-16 17:59:11 +00:00
parent b54167b8b6
commit 8ee39f7432
6 changed files with 400 additions and 432 deletions

View File

@@ -1,12 +1,23 @@
#ifndef _STRINGS_H
#define _STRINGS_H
/** @file
*
* String functions
*
*/
FILE_LICENCE ( GPL2_OR_LATER );
#include <limits.h>
#include <string.h>
#include <bits/strings.h>
/**
* Find last (i.e. most significant) set bit
*
* @v x Value
* @ret msb Most significant bit set in value (LSB=1), or zero
*/
static inline __attribute__ (( always_inline )) int
__constant_flsll ( unsigned long long x ) {
int r = 0;
@@ -41,6 +52,12 @@ __constant_flsll ( unsigned long long x ) {
return r;
}
/**
* Find last (i.e. most significant) set bit
*
* @v x Value
* @ret msb Most significant bit set in value (LSB=1), or zero
*/
static inline __attribute__ (( always_inline )) int
__constant_flsl ( unsigned long x ) {
return __constant_flsll ( x );
@@ -49,24 +66,55 @@ __constant_flsl ( unsigned long x ) {
int __flsll ( long long x );
int __flsl ( long x );
/**
* Find last (i.e. most significant) set bit
*
* @v x Value
* @ret msb Most significant bit set in value (LSB=1), or zero
*/
#define flsll( x ) \
( __builtin_constant_p ( x ) ? __constant_flsll ( x ) : __flsll ( x ) )
/**
* Find last (i.e. most significant) set bit
*
* @v x Value
* @ret msb Most significant bit set in value (LSB=1), or zero
*/
#define flsl( x ) \
( __builtin_constant_p ( x ) ? __constant_flsl ( x ) : __flsl ( x ) )
/**
* Find last (i.e. most significant) set bit
*
* @v x Value
* @ret msb Most significant bit set in value (LSB=1), or zero
*/
#define fls( x ) flsl ( x )
extern int strcasecmp ( const char *s1, const char *s2 );
/**
* Copy memory
*
* @v src Source
* @v dest Destination
* @v len Length
*/
static inline __attribute__ (( always_inline )) void
bcopy ( const void *src, void *dest, size_t n ) {
memmove ( dest, src, n );
bcopy ( const void *src, void *dest, size_t len ) {
memmove ( dest, src, len );
}
/**
* Zero memory
*
* @v dest Destination
* @v len Length
*/
static inline __attribute__ (( always_inline )) void
bzero ( void *s, size_t n ) {
memset ( s, 0, n );
bzero ( void *dest, size_t len ) {
memset ( dest, 0, len );
}
int __pure strcasecmp ( const char *first, const char *second ) __nonnull;
#endif /* _STRINGS_H */