]> Joshua Wise's Git repositories - netwatch.git/blame - lib/minilib.c
add a lot of copyright headers
[netwatch.git] / lib / minilib.c
CommitLineData
3c4e084d
JP
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
9b8c947b 11#include "console.h"
36ce375d 12#include <minilib.h>
90122e52 13#include <output.h>
9b8c947b 14
3c4e084d 15
90122e52
JW
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 */
20d44251 20void _memcpy(void *dest, const void *src, int bytes)
9b8c947b 21{
90122e52
JW
22 /* I hate everyone */
23 /* Since we otherwise compile with -O0, we might as well manually speed this up a bit. */
24
ee681ad4
JW
25 char *cdest = dest;
26 const char *csrc = src;
90122e52
JW
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 {
36ce375d 60 *(cdest++) = *(csrc++);
90122e52
JW
61 bytes--;
62 }
63}
64
65void memcpy(void *dest, const void *src, int bytes)
66{
67 _memcpy(dest, src, bytes);
9b8c947b
JW
68}
69
6e6d4a8b
JP
70void memset(void *dest, int data, int bytes)
71{
72 unsigned char *cdest = dest;
73 while (bytes--)
74 *(cdest++) = (unsigned char)data;
75}
76
36ce375d 77void memmove(void *dest, void *src, int bytes)
9b8c947b 78{
36ce375d
JP
79 char * cdest = dest;
80 char * csrc = src;
81 if ((cdest > csrc) && (cdest <= (csrc + bytes)))
9b8c947b
JW
82 {
83 /* do it backwards! */
36ce375d
JP
84 cdest += bytes;
85 csrc += bytes;
9b8c947b 86 while (bytes--)
36ce375d 87 *(--cdest) = *(--csrc);
9b8c947b 88 } else
90122e52 89 memcpy(dest, src, bytes);
9b8c947b
JW
90}
91
36ce375d 92int memcmp (const char *a2, const char *a1, int bytes) {
d56898ee 93 while (bytes--)
94 {
95 if (*(a2++) != *(a1++))
96 return 1;
97 }
98 return 0;
99}
100
36ce375d 101int strcmp (const char *a2, const char *a1) {
d56898ee 102 while (1) {
103 if (*a2 != *a1) return 1;
104 if (*a2 == 0) return 0;
105 a1++;
106 a2++;
107 }
108}
109
6f9272bd
JW
110int 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
f2da2cd3 120int strlen(const char *c)
9b8c947b
JW
121{
122 int l = 0;
123 while (*(c++))
124 l++;
125 return l;
126}
127
36ce375d 128void strcpy(char *a2, const char *a1)
31be35cd
JW
129{
130 do {
131 *(a2++) = *a1;
132 } while (*(a1++));
133}
134
9343e933
JW
135void strcat(char *dest, char *src)
136{
137 while (*dest)
138 dest++;
139 while (*src)
140 *(dest++) = *(src++);
141 *(dest++) = *(src++);
142}
143
f2da2cd3 144void puts(const char *c)
9b8c947b
JW
145{
146 putbytes(c, strlen(c));
147}
148
149static char hexarr[] = "0123456789ABCDEF";
36ce375d 150void tohex(char *s, unsigned long l)
9b8c947b
JW
151{
152 int i;
153 for (i = 0; i < 8; i++)
154 {
31be35cd 155 s[i] = hexarr[l >> 28];
9b8c947b
JW
156 l <<= 4;
157 }
158}
31be35cd
JW
159
160void puthex(unsigned long l)
161{
36ce375d 162 char d[9];
31be35cd
JW
163 d[8] = 0;
164 tohex(d, l);
165 puts(d);
166}
644af6b4
JP
167
168unsigned short htons(unsigned short in)
169{
170 return (in >> 8) | (in << 8);
171}
6e6d4a8b
JP
172
173unsigned 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.040145 seconds and 4 git commands to generate.