]> Joshua Wise's Git repositories - netwatch.git/blame_incremental - lib/minilib.c
More ICH2-specific code diked out.
[netwatch.git] / lib / minilib.c
... / ...
CommitLineData
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 */
20void _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
65void memcpy(void *dest, const void *src, int bytes)
66{
67 _memcpy(dest, src, bytes);
68}
69
70void memset(void *dest, int data, int bytes)
71{
72 unsigned char *cdest = dest;
73 while (bytes--)
74 *(cdest++) = (unsigned char)data;
75}
76
77void *memchr(const void *buf, char c, int maxlen)
78{
79 const char * cbuf = buf;
80 while (maxlen--)
81 {
82 if (*cbuf == c) return (void *)cbuf;
83 cbuf++;
84 }
85 return 0;
86}
87
88void memmove(void *dest, void *src, int bytes)
89{
90 char * cdest = dest;
91 char * csrc = src;
92 if ((cdest > csrc) && (cdest <= (csrc + bytes)))
93 {
94 /* do it backwards! */
95 cdest += bytes;
96 csrc += bytes;
97 while (bytes--)
98 *(--cdest) = *(--csrc);
99 } else
100 memcpy(dest, src, bytes);
101}
102
103int memcmp (const char *a2, const char *a1, int bytes) {
104 while (bytes--)
105 {
106 if (*(a2++) != *(a1++))
107 return 1;
108 }
109 return 0;
110}
111
112int strcmp (const char *a2, const char *a1) {
113 while (1) {
114 if (*a2 != *a1) return 1;
115 if (*a2 == 0) return 0;
116 a1++;
117 a2++;
118 }
119}
120
121int strncmp (const char *a2, const char *a1, int n) {
122 while (n--) {
123 if (*a2 != *a1) return 1;
124 if (*a2 == 0) return 0;
125 a1++;
126 a2++;
127 }
128 return 0;
129}
130
131int strlen(const char *c)
132{
133 int l = 0;
134 while (*(c++))
135 l++;
136 return l;
137}
138
139void strcpy(char *a2, const char *a1)
140{
141 do {
142 *(a2++) = *a1;
143 } while (*(a1++));
144}
145
146void strcat(char *dest, char *src)
147{
148 while (*dest)
149 dest++;
150 while (*src)
151 *(dest++) = *(src++);
152 *(dest++) = *(src++);
153}
154
155void puts(const char *c)
156{
157 putbytes(c, strlen(c));
158}
159
160static char hexarr[] = "0123456789ABCDEF";
161void tohex(char *s, unsigned long l)
162{
163 int i;
164 for (i = 0; i < 8; i++)
165 {
166 s[i] = hexarr[l >> 28];
167 l <<= 4;
168 }
169}
170
171void btohex(char *s, unsigned char c)
172{
173 s[0] = hexarr[c >> 4];
174 s[1] = hexarr[c & 0xF];
175}
176
177void puthex(unsigned long l)
178{
179 char d[9];
180 d[8] = 0;
181 tohex(d, l);
182 puts(d);
183}
184
185unsigned short htons(unsigned short in)
186{
187 return (in >> 8) | (in << 8);
188}
189
190unsigned int htonl(unsigned int in)
191{
192 return ((in & 0xff) << 24) |
193 ((in & 0xff00) << 8) |
194 ((in & 0xff0000UL) >> 8) |
195 ((in & 0xff000000UL) >> 24);
196}
This page took 0.024512 seconds and 5 git commands to generate.