X-Git-Url: http://git.joshuawise.com/netwatch.git/blobdiff_plain/6f9272bdabcda81425ec1aaecf26a160535d4749..refs/heads/master:/lib/minilib.c diff --git a/lib/minilib.c b/lib/minilib.c index e90f7af..935d0c4 100644 --- a/lib/minilib.c +++ b/lib/minilib.c @@ -1,12 +1,70 @@ +/* minilib.c + * General-purpose C library routines. + * NetWatch system management mode administration console + * + * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved. + * This program is free software; you can redistribute and/or modify it under + * the terms found in the file LICENSE in the root of this source tree. + * + */ + #include "console.h" #include +#include -void memcpy(void *dest, const void *src, int bytes) + +/* We have both _memcpy and memcpy, because gcc might be able to do better in lwip. + * For small things, gcc inlines its memcpy, but for large things, we call out + * to this memcpy. + */ +void _memcpy(void *dest, const void *src, int bytes) { + /* I hate everyone */ + /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */ + char *cdest = dest; const char *csrc = src; - while (bytes--) + int *idest; + const int *isrc; + int nwords; + + /* Align to src (picked arbitrarily; might as well align to something) */ + while (bytes && ((unsigned int)csrc & 3)) + { *(cdest++) = *(csrc++); + bytes--; + } + + idest = (int *)cdest; + isrc = (const int *)csrc; + + nwords = bytes / 4; + bytes -= bytes & ~3; + if (nwords != 0) + switch(nwords % 8) /* They see me Duff'sin'. They hatin'. */ + do { + case 0: nwords--; *(idest++) = *(isrc++); + case 7: nwords--; *(idest++) = *(isrc++); + case 6: nwords--; *(idest++) = *(isrc++); + case 5: nwords--; *(idest++) = *(isrc++); + case 4: nwords--; *(idest++) = *(isrc++); + case 3: nwords--; *(idest++) = *(isrc++); + case 2: nwords--; *(idest++) = *(isrc++); + case 1: nwords--; *(idest++) = *(isrc++); + } while (nwords); + + cdest = (char *)idest; + csrc = (const char *)isrc; + while (bytes) /* Clean up the remainder */ + { + *(cdest++) = *(csrc++); + bytes--; + } +} + +void memcpy(void *dest, const void *src, int bytes) +{ + _memcpy(dest, src, bytes); } void memset(void *dest, int data, int bytes) @@ -16,6 +74,17 @@ void memset(void *dest, int data, int bytes) *(cdest++) = (unsigned char)data; } +void *memchr(const void *buf, char c, int maxlen) +{ + const char * cbuf = buf; + while (maxlen--) + { + if (*cbuf == c) return (void *)cbuf; + cbuf++; + } + return 0; +} + void memmove(void *dest, void *src, int bytes) { char * cdest = dest; @@ -28,8 +97,7 @@ void memmove(void *dest, void *src, int bytes) while (bytes--) *(--cdest) = *(--csrc); } else - while (bytes--) - *(cdest++) = *(csrc++); + memcpy(dest, src, bytes); } int memcmp (const char *a2, const char *a1, int bytes) { @@ -75,6 +143,15 @@ void strcpy(char *a2, const char *a1) } while (*(a1++)); } +void strcat(char *dest, char *src) +{ + while (*dest) + dest++; + while (*src) + *(dest++) = *(src++); + *(dest++) = *(src++); +} + void puts(const char *c) { putbytes(c, strlen(c)); @@ -91,6 +168,12 @@ void tohex(char *s, unsigned long l) } } +void btohex(char *s, unsigned char c) +{ + s[0] = hexarr[c >> 4]; + s[1] = hexarr[c & 0xF]; +} + void puthex(unsigned long l) { char d[9];