// Basic string routines. Not hardware optimized, but not shabby.
-#ifdef __SHARC__
-#pragma nosharc
-#endif
-
#include <stdio.h>
#include <string.h>
#include <ros/memlayout.h>
ret = dst;
for (i = 0; i < size; i++) {
- // TODO: ivy bitches about this
*dst++ = *src;
// If strlen(src) < size, null-pad 'dst' out to 'size' chars
if (*src != '\0')
size_t
strlcpy(char *dst, const char *src, size_t size)
{
- char *dst_in;
-
- dst_in = dst;
if (size > 0) {
while (--size > 0 && *src != '\0')
*dst++ = *src++;
*dst = '\0';
}
- return dst - dst_in;
+
+ return strlen(src);
+}
+
+size_t
+strlcat(char *dst, const char *src, size_t size)
+{
+ size_t rem; /* Buffer space remaining after null in dst. */
+
+ /* We must find the terminating NUL byte in dst, but abort the
+ * search if we go past 'size' bytes. At the end of this loop,
+ * 'dst' will point to either the NUL byte in the original
+ * destination or to one byte beyond the end of the buffer.
+ *
+ * 'rem' will be the amount of 'size' remaining beyond the NUL byte;
+ * potentially zero. This implies that 'size - rem' is equal to the
+ * distance from the beginning of the destination buffer to 'dst'.
+ *
+ * The return value of strlcat is the sum of the length of the
+ * original destination buffer (size - rem) plus the size of the
+ * src string (the return value of strlcpy). */
+ rem = size;
+ while ((rem > 0) && (*dst != '\0')) {
+ rem--;
+ dst++;
+ }
+
+ return (size - rem) + strlcpy(dst, src, rem);
}
int
return 0;
}
-void *
-memchr(void* mem, int chr, int len)
+// Return a pointer to the last occurrence of 'c' in 's',
+// or a null pointer if the string has no 'c'.
+char *
+strrchr(const char *s, char c)
{
- char* s = (char*)mem;
- for(int i = 0; i < len; i++)
- if(s[i] == (char)chr)
- return s+i;
+ char *lastc = NULL;
+ for (; *s; s++)
+ if (*s == c){
+ lastc = (char*)s;
+ }
+ return lastc;
+}
+
+void *memchr(const void *mem, int chr, int len)
+{
+ char *s = (char*) mem;
+
+ for (int i = 0; i < len; i++)
+ if (s[i] == (char) chr)
+ return s + i;
return NULL;
}
} while(0)
void *
-memset(void *COUNT(_n) v, int c, size_t _n)
+memset(void *v, int c, size_t _n)
{
- char *BND(v,v+_n) p;
+ char *p;
size_t n0;
size_t n = _n;
}
void *
-memmove(void *COUNT(_n) dst, const void *COUNT(_n) src, size_t _n)
+memmove(void *dst, const void *src, size_t _n)
{
- const char *BND(src,src+_n) s;
- char *BND(dst,dst+_n) d;
+#ifdef CONFIG_X86
+ bcopy(src, dst, _n);
+ return dst;
+#else
+ const char *s;
+ char *d;
size_t n = _n;
-
+
s = src;
d = dst;
if (s < d && s + n > d) {
*d++ = *s++;
return dst;
+#endif
}
int
-memcmp(const void *COUNT(n) v1, const void *COUNT(n) v2, size_t n)
+memcmp(const void *v1, const void *v2, size_t n)
{
- const uint8_t *BND(v1,v1+n) s1 = (const uint8_t *) v1;
- const uint8_t *BND(v2,v2+n) s2 = (const uint8_t *) v2;
+ const uint8_t *s1 = (const uint8_t *) v1;
+ const uint8_t *s2 = (const uint8_t *) v2;
while (n-- > 0) {
if (*s1 != *s2)
}
void *
-memfind(const void *COUNT(n) _s, int c, size_t n)
+memfind(const void *_s, int c, size_t n)
{
- const void *SNT ends = (const char *) _s + n;
- const void *BND(_s,_s + n) s = _s;
+ const void *ends = (const char *) _s + n;
+ const void *s = _s;
for (; s < ends; s++)
if (*(const unsigned char *) s == (unsigned char) c)
break;
- return (void *BND(_s,_s+n)) s;
+ return (void *)s;
}
long
return (neg ? -val : val);
}
-
-int
-atoi(const char* s)
+int atoi(const char *s)
{
+ if (!s)
+ return 0;
+ if (s[0] == '0' && s[1] == 'x')
+ warn("atoi() used on a hex string!");
// no overflow detection
return (int)strtol(s,NULL,10);
}
+
+int sigchecksum(void *address, int length)
+{
+ uint8_t *p, sum;
+
+ sum = 0;
+ for (p = address; length-- > 0; p++)
+ sum += *p;
+
+ return sum;
+}
+
+void *sigscan(uint8_t *address, int length, char *signature)
+{
+ uint8_t *e, *p;
+ int siglength;
+
+ e = address + length;
+ siglength = strlen(signature);
+ for (p = address; p + siglength < e; p += 16) {
+ if (memcmp(p, signature, siglength))
+ continue;
+ return p;
+ }
+
+ return NULL;
+}