]> Joshua Wise's Git repositories - netwatch.git/blame - lib/minilib.c
Fix width to 8, not 9.
[netwatch.git] / lib / minilib.c
CommitLineData
9b8c947b 1#include "console.h"
36ce375d 2#include <minilib.h>
90122e52 3#include <output.h>
9b8c947b 4
90122e52
JW
5/* We have both _memcpy and memcpy, because gcc might be able to do better in lwip.
6 * For small things, gcc inlines its memcpy, but for large things, we call out
7 * to this memcpy.
8 */
20d44251 9void _memcpy(void *dest, const void *src, int bytes)
9b8c947b 10{
90122e52
JW
11 /* I hate everyone */
12 /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */
13
ee681ad4
JW
14 char *cdest = dest;
15 const char *csrc = src;
90122e52
JW
16 int *idest;
17 const int *isrc;
18 int nwords;
19
20 /* Align to src (picked arbitrarily; might as well align to something) */
21 while (bytes && ((unsigned int)csrc & 3))
22 {
23 *(cdest++) = *(csrc++);
24 bytes--;
25 }
26
27 idest = (int *)cdest;
28 isrc = (const int *)csrc;
29
30 nwords = bytes / 4;
31 bytes -= bytes & ~3;
32 if (nwords != 0)
33 switch(nwords % 8) /* They see me Duff'sin'. They hatin'. */
34 do {
35 case 0: nwords--; *(idest++) = *(isrc++);
36 case 7: nwords--; *(idest++) = *(isrc++);
37 case 6: nwords--; *(idest++) = *(isrc++);
38 case 5: nwords--; *(idest++) = *(isrc++);
39 case 4: nwords--; *(idest++) = *(isrc++);
40 case 3: nwords--; *(idest++) = *(isrc++);
41 case 2: nwords--; *(idest++) = *(isrc++);
42 case 1: nwords--; *(idest++) = *(isrc++);
43 } while (nwords);
44
45 cdest = (char *)idest;
46 csrc = (const char *)isrc;
47 while (bytes) /* Clean up the remainder */
48 {
36ce375d 49 *(cdest++) = *(csrc++);
90122e52
JW
50 bytes--;
51 }
52}
53
54void memcpy(void *dest, const void *src, int bytes)
55{
56 _memcpy(dest, src, bytes);
9b8c947b
JW
57}
58
6e6d4a8b
JP
59void memset(void *dest, int data, int bytes)
60{
61 unsigned char *cdest = dest;
62 while (bytes--)
63 *(cdest++) = (unsigned char)data;
64}
65
36ce375d 66void memmove(void *dest, void *src, int bytes)
9b8c947b 67{
36ce375d
JP
68 char * cdest = dest;
69 char * csrc = src;
70 if ((cdest > csrc) && (cdest <= (csrc + bytes)))
9b8c947b
JW
71 {
72 /* do it backwards! */
36ce375d
JP
73 cdest += bytes;
74 csrc += bytes;
9b8c947b 75 while (bytes--)
36ce375d 76 *(--cdest) = *(--csrc);
9b8c947b 77 } else
90122e52 78 memcpy(dest, src, bytes);
9b8c947b
JW
79}
80
36ce375d 81int memcmp (const char *a2, const char *a1, int bytes) {
d56898ee 82 while (bytes--)
83 {
84 if (*(a2++) != *(a1++))
85 return 1;
86 }
87 return 0;
88}
89
36ce375d 90int strcmp (const char *a2, const char *a1) {
d56898ee 91 while (1) {
92 if (*a2 != *a1) return 1;
93 if (*a2 == 0) return 0;
94 a1++;
95 a2++;
96 }
97}
98
6f9272bd
JW
99int strncmp (const char *a2, const char *a1, int n) {
100 while (n--) {
101 if (*a2 != *a1) return 1;
102 if (*a2 == 0) return 0;
103 a1++;
104 a2++;
105 }
106 return 0;
107}
108
f2da2cd3 109int strlen(const char *c)
9b8c947b
JW
110{
111 int l = 0;
112 while (*(c++))
113 l++;
114 return l;
115}
116
36ce375d 117void strcpy(char *a2, const char *a1)
31be35cd
JW
118{
119 do {
120 *(a2++) = *a1;
121 } while (*(a1++));
122}
123
9343e933
JW
124void strcat(char *dest, char *src)
125{
126 while (*dest)
127 dest++;
128 while (*src)
129 *(dest++) = *(src++);
130 *(dest++) = *(src++);
131}
132
f2da2cd3 133void puts(const char *c)
9b8c947b
JW
134{
135 putbytes(c, strlen(c));
136}
137
138static char hexarr[] = "0123456789ABCDEF";
36ce375d 139void tohex(char *s, unsigned long l)
9b8c947b
JW
140{
141 int i;
142 for (i = 0; i < 8; i++)
143 {
31be35cd 144 s[i] = hexarr[l >> 28];
9b8c947b
JW
145 l <<= 4;
146 }
147}
31be35cd
JW
148
149void puthex(unsigned long l)
150{
36ce375d 151 char d[9];
31be35cd
JW
152 d[8] = 0;
153 tohex(d, l);
154 puts(d);
155}
644af6b4
JP
156
157unsigned short htons(unsigned short in)
158{
159 return (in >> 8) | (in << 8);
160}
6e6d4a8b
JP
161
162unsigned int htonl(unsigned int in)
163{
164 return ((in & 0xff) << 24) |
165 ((in & 0xff00) << 8) |
166 ((in & 0xff0000UL) >> 8) |
167 ((in & 0xff000000UL) >> 24);
168}
This page took 0.043133 seconds and 4 git commands to generate.