[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,46 +1,53 @@
/*
* Copyright (C) 1991, 1992 Linus Torvalds
* Copyright (C) 2004 Tobias Lorenz
#ifndef _STRING_H
#define _STRING_H
/** @file
*
* string handling functions
* based on linux/include/linux/ctype.h
* and linux/include/linux/string.h
* String functions
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
FILE_LICENCE ( GPL2_ONLY );
#ifndef ETHERBOOT_STRING_H
#define ETHERBOOT_STRING_H
FILE_LICENCE ( GPL2_OR_LATER );
#include <stddef.h>
#include <bits/string.h>
char * strcpy(char * dest,const char *src) __nonnull;
char * strncpy(char * dest,const char *src,size_t count) __nonnull;
char * strcat(char * dest, const char * src) __nonnull;
int __pure strcmp(const char * cs,const char * ct) __nonnull;
int __pure strncmp(const char * cs,const char * ct,
size_t count) __nonnull;
char * __pure strchr(const char * s, int c) __nonnull;
char * __pure strrchr(const char * s, int c) __nonnull;
size_t __pure strlen(const char * s) __nonnull;
size_t __pure strnlen(const char * s, size_t count) __nonnull;
char * __pure strpbrk(const char * cs,const char * ct) __nonnull;
char * strsep(char **s, const char *ct) __nonnull;
void * memset(void * s,int c,size_t count) __nonnull;
/* Architecture-specific code is expected to provide these functions,
* but may instead explicitly choose to use the generic versions.
*/
void * memset ( void *dest, int character, size_t len ) __nonnull;
void * memcpy ( void *dest, const void *src, size_t len ) __nonnull;
void * memmove(void * dest,const void *src,size_t count) __nonnull;
int __pure memcmp(const void * cs,const void * ct,
size_t count) __nonnull;
char * __pure strstr(const char * s1,const char * s2) __nonnull;
void * __pure memchr(const void *s, int c, size_t n) __nonnull;
char * __malloc strdup(const char *s) __nonnull;
char * __malloc strndup(const char *s, size_t n) __nonnull;
void * memmove ( void *dest, const void *src, size_t len ) __nonnull;
extern void * generic_memset ( void *dest, int character,
size_t len ) __nonnull;
extern void * generic_memcpy ( void *dest, const void *src,
size_t len ) __nonnull;
extern void * generic_memmove ( void *dest, const void *src,
size_t len ) __nonnull;
extern const char * __pure strerror ( int errno );
extern int __pure memcmp ( const void *first, const void *second,
size_t len ) __nonnull;
extern void * __pure memchr ( const void *src, int character,
size_t len ) __nonnull;
extern void * memswap ( void *dest, void *src, size_t len ) __nonnull;
extern int __pure strcmp ( const char *first, const char *second ) __nonnull;
extern int __pure strncmp ( const char *first, const char *second,
size_t max ) __nonnull;
extern size_t __pure strlen ( const char *src ) __nonnull;
extern size_t __pure strnlen ( const char *src, size_t max ) __nonnull;
extern char * __pure strchr ( const char *src, int character ) __nonnull;
extern char * __pure strrchr ( const char *src, int character ) __nonnull;
extern char * __pure strstr ( const char *haystack,
const char *needle ) __nonnull;
extern char * strcpy ( char *dest, const char *src ) __nonnull;
extern char * strncpy ( char *dest, const char *src, size_t max ) __nonnull;
extern char * strcat ( char *dest, const char *src ) __nonnull;
extern char * __malloc strdup ( const char *src ) __nonnull;
extern char * __malloc strndup ( const char *src, size_t max ) __nonnull;
extern char * __pure strpbrk ( const char *string,
const char *delim ) __nonnull;
extern char * strsep ( char **string, const char *delim ) __nonnull;
#endif /* ETHERBOOT_STRING */
extern char * __pure strerror ( int errno );
#endif /* _STRING_H */

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 */