]> Joshua Wise's Git repositories - netwatch.git/blob - lib/minilib.c
392eae7abcaf146e8f09ce8a4b4f3cec32457b6e
[netwatch.git] / lib / minilib.c
1 /* minilib.c
2  * General-purpose C library routines.
3  * NetWatch system management mode administration console
4  *
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.
8  *
9  */
10
11 #include "console.h"
12 #include <minilib.h>
13 #include <output.h>
14
15
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
18  * to this memcpy.
19  */
20 void _memcpy(void *dest, const void *src, int bytes)
21 {
22         /* I hate everyone */
23         /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */
24         
25         char *cdest = dest;
26         const char *csrc = src;
27         int *idest;
28         const int *isrc;
29         int nwords;
30         
31         /* Align to src (picked arbitrarily; might as well align to something) */
32         while (bytes && ((unsigned int)csrc & 3))
33         {
34                 *(cdest++) = *(csrc++);
35                 bytes--;
36         }
37         
38         idest = (int *)cdest;
39         isrc = (const int *)csrc;
40         
41         nwords = bytes / 4;
42         bytes -= bytes & ~3;
43         if (nwords != 0)
44                 switch(nwords % 8)      /* They see me Duff'sin'.  They hatin'. */
45                         do {
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++);
54                         } while (nwords);
55         
56         cdest = (char *)idest;
57         csrc = (const char *)isrc;
58         while (bytes)   /* Clean up the remainder */
59         {
60                 *(cdest++) = *(csrc++);
61                 bytes--;
62         }
63 }
64
65 void memcpy(void *dest, const void *src, int bytes)
66 {
67         _memcpy(dest, src, bytes);
68 }
69
70 void memset(void *dest, int data, int bytes)
71 {
72         unsigned char *cdest = dest;
73         while (bytes--)
74                 *(cdest++) = (unsigned char)data;
75 }
76
77 void memmove(void *dest, void *src, int bytes)
78 {
79         char * cdest = dest;
80         char * csrc = src;
81         if ((cdest > csrc) && (cdest <= (csrc + bytes)))
82         {
83                 /* do it backwards! */
84                 cdest += bytes;
85                 csrc += bytes;
86                 while (bytes--)
87                         *(--cdest) = *(--csrc);
88         } else
89                 memcpy(dest, src, bytes);
90 }
91
92 int memcmp (const char *a2, const char *a1, int bytes) {
93         while (bytes--)
94         {
95                 if (*(a2++) != *(a1++))
96                         return 1;
97         }
98         return 0;
99 }
100
101 int strcmp (const char *a2, const char *a1) {
102         while (1) {
103                 if (*a2 != *a1) return 1;
104                 if (*a2 == 0) return 0;
105                 a1++;
106                 a2++;
107         }
108 }
109
110 int strncmp (const char *a2, const char *a1, int n) {
111         while (n--) {
112                 if (*a2 != *a1) return 1;
113                 if (*a2 == 0) return 0;
114                 a1++;
115                 a2++;
116         }
117         return 0;
118 }
119
120 int strlen(const char *c)
121 {
122         int l = 0;
123         while (*(c++))
124                 l++;
125         return l;
126 }
127
128 void strcpy(char *a2, const char *a1)
129 {
130         do {
131                 *(a2++) = *a1;
132         } while (*(a1++));
133 }
134
135 void strcat(char *dest, char *src)
136 {
137         while (*dest)
138                 dest++;
139         while (*src)
140                 *(dest++) = *(src++);
141         *(dest++) = *(src++);
142 }
143
144 void puts(const char *c)
145 {
146         putbytes(c, strlen(c));
147 }
148
149 static char hexarr[] = "0123456789ABCDEF";
150 void tohex(char *s, unsigned long l)
151 {
152         int i;
153         for (i = 0; i < 8; i++)
154         {
155                 s[i] = hexarr[l >> 28];
156                 l <<= 4;
157         }
158 }
159
160 void puthex(unsigned long l)
161 {
162         char d[9];
163         d[8] = 0;
164         tohex(d, l);
165         puts(d);
166 }
167
168 unsigned short htons(unsigned short in)
169 {
170         return (in >> 8) | (in << 8);
171 }
172
173 unsigned int htonl(unsigned int in)
174 {
175         return ((in & 0xff) << 24) |
176                ((in & 0xff00) << 8) |
177                ((in & 0xff0000UL) >> 8) |
178                ((in & 0xff000000UL) >> 24);
179 }
This page took 0.023749 seconds and 2 git commands to generate.