diff --git a/native/src/crt0/Android.mk b/native/src/crt0/Android.mk index 3aeccda0a..0a458757e 100644 --- a/native/src/crt0/Android.mk +++ b/native/src/crt0/Android.mk @@ -10,6 +10,8 @@ LOCAL_EXPORT_LDFLAGS := -static -nostartfiles -nodefaultlibs $(LOCAL_compiler_rt LOCAL_SRC_FILES := \ malloc.c \ + mem.c \ + misc.c \ nolibc.c \ stdio.c \ syscall.c \ diff --git a/native/src/crt0/mem.c b/native/src/crt0/mem.c new file mode 100644 index 000000000..31a66d1c2 --- /dev/null +++ b/native/src/crt0/mem.c @@ -0,0 +1,314 @@ +#include +#include + +void *memcpy(void *dst, const void *src, size_t size) { + return __builtin_memcpy(dst, src, size); +} + +int memcmp(const void *lhs, const void *rhs, size_t n) { + return __builtin_memcmp(lhs, rhs, n); +} + +void *memchr(const void *ptr, int ch, size_t count) { + return __builtin_memchr(ptr, ch, count); +} + +char *strchr(const char *str, int ch) { + return __builtin_strchr(str, ch); +} + +int strcmp(const char *lhs, const char *rhs) { + return __builtin_strcmp(lhs, rhs); +} + +size_t strlen(const char *str) { + return __builtin_strlen(str); +} + +char *strcpy(char *restrict dest, const char *restrict src) { + return __builtin_strcpy(dest, src); +} + +char *strdup(const char *str) { + size_t siz; + char *copy; + siz = strlen(str) + 1; + if ((copy = malloc(siz)) == NULL) + return NULL; + memcpy(copy, str, siz); + return copy; +} + +// memmem source: bionic/libc/upstream-openbsd/lib/libc/string/memmem.c + +static char *twobyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) { + uint16_t nw = n[0]<<8 | n[1], hw = h[0]<<8 | h[1]; + for (h+=2, k-=2; k; k--, hw = hw<<8 | *h++) + if (hw == nw) return (char *)h-2; + return hw == nw ? (char *)h-2 : 0; +} + +static char *threebyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) { + uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8; + uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8; + for (h+=3, k-=3; k; k--, hw = (hw|*h++)<<8) + if (hw == nw) return (char *)h-3; + return hw == nw ? (char *)h-3 : 0; +} + +static char *fourbyte_memmem(const unsigned char *h, size_t k, const unsigned char *n) { + uint32_t nw = n[0]<<24 | n[1]<<16 | n[2]<<8 | n[3]; + uint32_t hw = h[0]<<24 | h[1]<<16 | h[2]<<8 | h[3]; + for (h+=4, k-=4; k; k--, hw = hw<<8 | *h++) + if (hw == nw) return (char *)h-4; + return hw == nw ? (char *)h-4 : 0; +} + +#define MAX(a,b) ((a)>(b)?(a):(b)) +#define MIN(a,b) ((a)<(b)?(a):(b)) + +#define BITOP(a,b,op) \ + ((a)[(size_t)(b)/(8*sizeof *(a))] op (size_t)1<<((size_t)(b)%(8*sizeof *(a)))) + +/* + * Maxime Crochemore and Dominique Perrin, Two-way string-matching, + * Journal of the ACM, 38(3):651-675, July 1991. + * + */ +static char *twoway_memmem( + const unsigned char *h, const unsigned char *z, const unsigned char *n, size_t l) { + size_t i, ip, jp, k, p, ms, p0, mem, mem0; + size_t byteset[32 / sizeof(size_t)] = { 0 }; + size_t shift[256]; + + /* Computing length of needle and fill shift table */ + for (i=0; i n[jp+k]) { + jp += k; + k = 1; + p = jp - ip; + } else { + ip = jp++; + k = p = 1; + } + } + ms = ip; + p0 = p; + + /* And with the opposite comparison */ + ip = -1; jp = 0; k = p = 1; + while (jp+k ms+1) ms = ip; + else p = p0; + + /* Periodic needle? */ + if (memcmp(n, n+p, ms+1)) { + mem0 = 0; + p = MAX(ms, l-ms-1) + 1; + } else mem0 = l-p; + mem = 0; + + /* Search loop */ + for (;;) { + /* If remainder of haystack is shorter than needle, done */ + if (z-h < l) return 0; + + /* Check last byte first; advance by shift on mismatch */ + if (BITOP(byteset, h[l-1], &)) { + k = l-shift[h[l-1]]; + if (k) { + if (k < mem) k = mem; + h += k; + mem = 0; + continue; + } + } else { + h += l; + mem = 0; + continue; + } + + /* Compare right half */ + for (k=MAX(ms+1,mem); kmem && n[k-1] == h[k-1]; k--); + if (k <= mem) return (char *)h; + h += p; + mem = mem0; + } +} + +void *memmem(const void *h0, size_t k, const void *n0, size_t l) { + const unsigned char *h = h0, *n = n0; + + /* Return immediately on empty needle */ + if (!l) return (void *)h; + + /* Return immediately when needle is longer than haystack */ + if (k +#include +#include +#include + +// Source: bionic/libc/bionic/libgen.cpp +static int __basename_r(const char *path, char* buffer, size_t buffer_size) { + const char *startp = NULL; + const char *endp = NULL; + int len; + int result; + + // Empty or NULL string gets treated as ".". + if (path == NULL || *path == '\0') { + startp = "."; + len = 1; + goto Exit; + } + + // Strip trailing slashes. + endp = path + strlen(path) - 1; + while (endp > path && *endp == '/') { + endp--; + } + + // All slashes becomes "/". + if (endp == path && *endp == '/') { + startp = "/"; + len = 1; + goto Exit; + } + + // Find the start of the base. + startp = endp; + while (startp > path && *(startp - 1) != '/') { + startp--; + } + + len = endp - startp +1; + +Exit: + result = len; + if (buffer == NULL) { + return result; + } + if (len > (int) buffer_size - 1) { + len = buffer_size - 1; + result = -1; + errno = ERANGE; + } + + if (len >= 0) { + memcpy(buffer, startp, len); + buffer[len] = 0; + } + return result; +} + +char *basename(const char *path) { + static char buf[4069]; + int rc = __basename_r(path, buf, sizeof(buf)); + return (rc < 0) ? NULL : buf; +}