diff options
Diffstat (limited to 'payloads/libpayload/libc')
-rw-r--r-- | payloads/libpayload/libc/console.c | 19 | ||||
-rw-r--r-- | payloads/libpayload/libc/ctype.c | 38 | ||||
-rw-r--r-- | payloads/libpayload/libc/ipchecksum.c | 34 | ||||
-rw-r--r-- | payloads/libpayload/libc/malloc.c | 136 | ||||
-rw-r--r-- | payloads/libpayload/libc/memory.c | 11 | ||||
-rw-r--r-- | payloads/libpayload/libc/string.c | 19 |
6 files changed, 125 insertions, 132 deletions
diff --git a/payloads/libpayload/libc/console.c b/payloads/libpayload/libc/console.c index bb7dec1f94..b2b115b835 100644 --- a/payloads/libpayload/libc/console.c +++ b/payloads/libpayload/libc/console.c @@ -42,7 +42,7 @@ void console_init(void) static void device_putchar(unsigned char c) { #ifdef CONFIG_VGA_CONSOLE - vga_putchar(0x700| c); + vga_putchar(0x700 | c); #endif #ifdef CONFIG_SERIAL_CONSOLE serial_putchar(c); @@ -65,10 +65,10 @@ int puts(const char *s) while (*s) { putchar(*s++); n++; - } - + } + putchar('\n'); - return n+1; + return n + 1; } int havekey(void) @@ -81,13 +81,13 @@ int havekey(void) if (keyboard_havechar()) return 1; #endif - return 0; + return 0; } -/* This returns an ascii value - the two getchar functions - cook the respective input from the device -*/ - +/** + * This returns an ASCII value - the two getchar functions + * cook the respective input from the device. + */ int getchar(void) { while (1) { @@ -101,4 +101,3 @@ int getchar(void) #endif } } - diff --git a/payloads/libpayload/libc/ctype.c b/payloads/libpayload/libc/ctype.c index c2f42a52d2..9853fceb59 100644 --- a/payloads/libpayload/libc/ctype.c +++ b/payloads/libpayload/libc/ctype.c @@ -27,34 +27,36 @@ * SUCH DAMAGE. */ -/* Basic ctype functions */ - #include <libpayload.h> int isspace(int c) { - switch (c) { - case ' ': case '\f': case '\n': - case '\r': case '\t': case '\v': - return 1; - default: - return 0; - } + switch (c) { + case ' ': + case '\f': + case '\n': + case '\r': + case '\t': + case '\v': + return 1; + default: + return 0; + } } int isdigit(int c) { - switch (c) { - case '0'...'9': - return 1; - default: - return 0; - } + switch (c) { + case '0'...'9': + return 1; + default: + return 0; + } } int tolower(int c) { - if (c >= 'A' && c <= 'Z') - return c - 'A' + 'a'; - return c; + if (c >= 'A' && c <= 'Z') + return c - 'A' + 'a'; + return c; } diff --git a/payloads/libpayload/libc/ipchecksum.c b/payloads/libpayload/libc/ipchecksum.c index caba6aaf34..4f712068e5 100644 --- a/payloads/libpayload/libc/ipchecksum.c +++ b/payloads/libpayload/libc/ipchecksum.c @@ -1,5 +1,7 @@ /* - * This file is part of the libpayload project + * This file is part of the libpayload project. + * + * It has orginally been taken from the FreeBSD project. * * Copyright (c) 2001 Charles Mott <cm@linktel.net> * All rights reserved. @@ -30,20 +32,20 @@ unsigned short ipchksum(const unsigned short *ptr, unsigned long nbytes) { - int sum, oddbyte; + int sum, oddbyte; - sum = 0; - while (nbytes > 1) { - sum += *ptr++; - nbytes -= 2; - } - if (nbytes == 1) { - oddbyte = 0; - ((u8 *) &oddbyte)[0] = *(u8 *)ptr; - ((u8 *) &oddbyte)[1] = 0; - sum += oddbyte; - } - sum = (sum >> 16) + (sum & 0xffff); - sum += (sum >> 16); - return (~sum); + sum = 0; + while (nbytes > 1) { + sum += *ptr++; + nbytes -= 2; + } + if (nbytes == 1) { + oddbyte = 0; + ((u8 *) & oddbyte)[0] = *(u8 *) ptr; + ((u8 *) & oddbyte)[1] = 0; + sum += oddbyte; + } + sum = (sum >> 16) + (sum & 0xffff); + sum += (sum >> 16); + return (~sum); } diff --git a/payloads/libpayload/libc/malloc.c b/payloads/libpayload/libc/malloc.c index a4877e67d9..86fb0b13cd 100644 --- a/payloads/libpayload/libc/malloc.c +++ b/payloads/libpayload/libc/malloc.c @@ -27,24 +27,23 @@ * SUCH DAMAGE. */ -/* This is a classically weak malloc() implmentation. - We have a relatively small and static heap, so we take - the easy route with an O(N) loop through the tree for - every malloc() and free(). Obviously, this doesn't scale - past a few hundred K (if that). - - We're also susecptable to the usual buffer overun poisoning, - though the risk is within acceptable ranges for this - implementation (don't overrun your buffers, kids!) -*/ - +/* + * This is a classically weak malloc() implmentation. We have a relatively + * small and static heap, so we take the easy route with an O(N) loop + * through the tree for every malloc() and free(). Obviously, this doesn't + * scale past a few hundred KB (if that). + * + * We're also susecptable to the usual buffer overun poisoning, though the + * risk is within acceptable ranges for this implementation (don't overrun + * your buffers, kids!). + */ + #include <libpayload.h> -/* Defined in the ldscript */ -extern char _heap, _eheap; +extern char _heap, _eheap; /* Defined in the ldscript. */ -static void *hstart = (void *) &_heap; -static void *hend = (void *) &_eheap; +static void *hstart = (void *)&_heap; +static void *hend = (void *)&_eheap; typedef unsigned int hdrtype_t; @@ -66,66 +65,64 @@ typedef unsigned int hdrtype_t; void print_malloc_map(void); -static void setup(void) +static void setup(void) { - int size = (unsigned int) (_heap - _eheap) - HDRSIZE; + int size = (unsigned int)(_heap - _eheap) - HDRSIZE; *((hdrtype_t *) hstart) = FREE_BLOCK(size); } - + static void *alloc(int len) { hdrtype_t header; void *ptr = hstart; - - /* align the size */ + + /* Align the size. */ len = (len + 3) & ~3; - if (!len || len > 0xFFFFFF) - return (void *) NULL; + if (!len || len > 0xffffff) + return (void *)NULL; - /* Make sure the region is setup correctly */ + /* Make sure the region is setup correctly. */ if (!HAS_MAGIC(*((hdrtype_t *) ptr))) setup(); - /* Find some free space */ - + /* Find some free space. */ do { header = *((hdrtype_t *) ptr); int size = SIZE(header); if (header & FLAG_FREE) { - if (len <= size) { void *nptr = ptr + HDRSIZE + len; int nsize = size - (len + 8); - /* Mark the block as used */ + /* Mark the block as used. */ *((hdrtype_t *) ptr) = USED_BLOCK(len); /* If there is still room in this block, - * then mark it as such */ - - if (nsize > 0) + * then mark it as such. + */ + if (nsize > 0) *((hdrtype_t *) nptr) = - FREE_BLOCK(nsize - 4); + FREE_BLOCK(nsize - 4); - return (void *) (ptr + HDRSIZE); + return (void *)(ptr + HDRSIZE); } } ptr += HDRSIZE + size; - } while(ptr < hend); + } while (ptr < hend); - /* Nothing available */ - return (void *) NULL; + /* Nothing available. */ + return (void *)NULL; } static void _consolidate(void) { void *ptr = hstart; - while(ptr < hend) { + while (ptr < hend) { void *nptr; hdrtype_t hdr = *((hdrtype_t *) ptr); unsigned int size = 0; @@ -134,23 +131,23 @@ static void _consolidate(void) ptr += HDRSIZE + SIZE(hdr); continue; } - + size = SIZE(hdr); nptr = ptr + HDRSIZE + SIZE(hdr); while (nptr < hend) { - hdrtype_t nhdr = *((hdrtype_t *) nptr); - + hdrtype_t nhdr = *((hdrtype_t *) nptr); + if (!(IS_FREE(nhdr))) break; - + size += SIZE(nhdr) + HDRSIZE; - *((hdrtype_t *) nptr) = 0; + *((hdrtype_t *) nptr) = 0; nptr += (HDRSIZE + SIZE(nhdr)); } - + *((hdrtype_t *) ptr) = FREE_BLOCK(size); ptr = nptr; } @@ -162,20 +159,20 @@ void free(void *ptr) ptr -= HDRSIZE; - /* Sanity check */ + /* Sanity check. */ if (ptr < hstart || ptr >= hend) return; hdr = *((hdrtype_t *) ptr); - /* Not our header (we're probably poisoned) */ + /* Not our header (we're probably poisoned). */ if (!HAS_MAGIC(hdr)) return; - /* Double free */ + /* Double free. */ if (hdr & FLAG_FREE) return; - + *((hdrtype_t *) ptr) = FREE_BLOCK(SIZE(hdr)); _consolidate(); } @@ -198,8 +195,7 @@ void *calloc(size_t nmemb, size_t size) void *realloc(void *ptr, size_t size) { - void *ret; - void *pptr; + void *ret, *pptr; unsigned int osize; if (ptr == NULL) @@ -209,38 +205,38 @@ void *realloc(void *ptr, size_t size) if (!HAS_MAGIC(*((hdrtype_t *) pptr))) return NULL; - - /* Get the original size of the block */ + + /* Get the original size of the block. */ osize = SIZE(*((hdrtype_t *) pptr)); - - /* Free the memory to update the tables - this - won't touch the actual memory, so we can still - use it for the copy after we have reallocated - the new space - */ - - free(ptr); + + /* + * Free the memory to update the tables - this won't touch the actual + * memory, so we can still use it for the copy after we have + * reallocated the new space. + */ + free(ptr); ret = alloc(size); - /* if ret == NULL, then doh - failure. - if ret == ptr then woo-hoo! no copy needed */ - + /* + * if ret == NULL, then doh - failure. + * if ret == ptr then woo-hoo! no copy needed. + */ if (ret == NULL || ret == ptr) return ret; - - /* Copy the memory to the new location */ + + /* Copy the memory to the new location. */ memcpy(ret, ptr, osize > size ? size : osize); + return ret; } -/* This is for debugging purposes */ +/* This is for debugging purposes. */ #ifdef TEST - void print_malloc_map(void) { void *ptr = hstart; - while(ptr < hend) { + while (ptr < hend) { hdrtype_t hdr = *((hdrtype_t *) ptr); if (!HAS_MAGIC(hdr)) { @@ -248,15 +244,13 @@ void print_malloc_map(void) break; } - /* FIXME: Verify the size of the block */ + /* FIXME: Verify the size of the block. */ printf("%x: %s (%x bytes)\n", - (unsigned int) (ptr - hstart), - hdr & FLAG_FREE ? "FREE" : "USED", - SIZE(hdr)); + (unsigned int)(ptr - hstart), + hdr & FLAG_FREE ? "FREE" : "USED", SIZE(hdr)); ptr += HDRSIZE + SIZE(hdr); } } - #endif diff --git a/payloads/libpayload/libc/memory.c b/payloads/libpayload/libc/memory.c index 41fa76e343..57c31e0bb3 100644 --- a/payloads/libpayload/libc/memory.c +++ b/payloads/libpayload/libc/memory.c @@ -101,12 +101,11 @@ void *memmove(void *dst, const void *src, size_t n) /** * Compare two memory areas. * - * @param s1 Pointer to the first area to compare. - * @param s2 Pointer to the second area to compare. - * @param len Size of the first area in bytes. Both areas must have the same - * length. - * @return If len is 0, return zero. If the areas match, return zero. - * Otherwise return non-zero. + * @param s1 Pointer to the first area to compare. + * @param s2 Pointer to the second area to compare. + * @param len Size of the first area in bytes (both must have the same length). + * @return If len is 0, return zero. If the areas match, return zero. + * Otherwise return non-zero. */ int memcmp(const char *s1, const char *s2, size_t len) { diff --git a/payloads/libpayload/libc/string.c b/payloads/libpayload/libc/string.c index 87f5789b4c..bbe99933f0 100644 --- a/payloads/libpayload/libc/string.c +++ b/payloads/libpayload/libc/string.c @@ -135,8 +135,8 @@ char *strncpy(char *d, const char *s, int n) int max = n > strlen(s) + 1 ? strlen(s) + 1 : n; int i; - for(i = 0; i < max; i++) - d[i] = (char) s[i]; + for (i = 0; i < max; i++) + d[i] = (char)s[i]; return d; } @@ -152,18 +152,18 @@ char *strncat(char *d, const char *s, int n) int max = n > strlen(s) ? strlen(s) : n; int i; - for(i = 0; i < max; i++) + for (i = 0; i < max; i++) p[i] = s[i]; p[i] = '\0'; return d; } -char * strchr(const char *s, int c) +char *strchr(const char *s, int c) { - char *p = (char *) s; + char *p = (char *)s; - for( ; *p != 0; p++) { + for (; *p != 0; p++) { if (*p == c) return p; } @@ -171,7 +171,6 @@ char * strchr(const char *s, int c) return NULL; } - char *strdup(const char *s) { int n = strlen(s); @@ -189,11 +188,9 @@ char *strstr(const char *h, const char *n) int nn = strlen(n); int i; - for(i = 0; i <= hn - nn; i++) + for (i = 0; i <= hn - nn; i++) if (!strcmp(&h[i], n)) - return (char *) &h[i]; + return (char *)&h[i]; return NULL; } - - |