2 * General-purpose C library routines.
3 * NetWatch system management mode administration console
5 * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved.
6 * This program is free software; you can redistribute and/or modify it under
7 * the terms found in the file LICENSE in the root of this source tree.
16 /* We have both _memcpy and memcpy, because gcc might be able to do better in lwip.
17 * For small things, gcc inlines its memcpy, but for large things, we call out
20 void _memcpy(void *dest, const void *src, int bytes)
23 /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */
26 const char *csrc = src;
31 /* Align to src (picked arbitrarily; might as well align to something) */
32 while (bytes && ((unsigned int)csrc & 3))
34 *(cdest++) = *(csrc++);
39 isrc = (const int *)csrc;
44 switch(nwords % 8) /* They see me Duff'sin'. They hatin'. */
46 case 0: nwords--; *(idest++) = *(isrc++);
47 case 7: nwords--; *(idest++) = *(isrc++);
48 case 6: nwords--; *(idest++) = *(isrc++);
49 case 5: nwords--; *(idest++) = *(isrc++);
50 case 4: nwords--; *(idest++) = *(isrc++);
51 case 3: nwords--; *(idest++) = *(isrc++);
52 case 2: nwords--; *(idest++) = *(isrc++);
53 case 1: nwords--; *(idest++) = *(isrc++);
56 cdest = (char *)idest;
57 csrc = (const char *)isrc;
58 while (bytes) /* Clean up the remainder */
60 *(cdest++) = *(csrc++);
65 void memcpy(void *dest, const void *src, int bytes)
67 _memcpy(dest, src, bytes);
70 void memset(void *dest, int data, int bytes)
72 unsigned char *cdest = dest;
74 *(cdest++) = (unsigned char)data;
77 void *memchr(const void *buf, char c, int maxlen)
79 const char * cbuf = buf;
82 if (*cbuf == c) return (void *)cbuf;
88 void memmove(void *dest, void *src, int bytes)
92 if ((cdest > csrc) && (cdest <= (csrc + bytes)))
94 /* do it backwards! */
98 *(--cdest) = *(--csrc);
100 memcpy(dest, src, bytes);
103 int memcmp (const char *a2, const char *a1, int bytes) {
106 if (*(a2++) != *(a1++))
112 int strcmp (const char *a2, const char *a1) {
114 if (*a2 != *a1) return 1;
115 if (*a2 == 0) return 0;
121 int strncmp (const char *a2, const char *a1, int n) {
123 if (*a2 != *a1) return 1;
124 if (*a2 == 0) return 0;
131 int strlen(const char *c)
139 void strcpy(char *a2, const char *a1)
146 void strcat(char *dest, char *src)
151 *(dest++) = *(src++);
152 *(dest++) = *(src++);
155 void puts(const char *c)
157 putbytes(c, strlen(c));
160 static char hexarr[] = "0123456789ABCDEF";
161 void tohex(char *s, unsigned long l)
164 for (i = 0; i < 8; i++)
166 s[i] = hexarr[l >> 28];
171 void btohex(char *s, unsigned char c)
173 s[0] = hexarr[c >> 4];
174 s[1] = hexarr[c & 0xF];
177 void puthex(unsigned long l)
185 unsigned short htons(unsigned short in)
187 return (in >> 8) | (in << 8);
190 unsigned int htonl(unsigned int in)
192 return ((in & 0xff) << 24) |
193 ((in & 0xff00) << 8) |
194 ((in & 0xff0000UL) >> 8) |
195 ((in & 0xff000000UL) >> 24);