]> Joshua Wise's Git repositories - netwatch.git/blame - lib/minilib.c
Add support for the power button SMI.
[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
db9fad13
JAW
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
36ce375d 88void memmove(void *dest, void *src, int bytes)
9b8c947b 89{
36ce375d
JP
90 char * cdest = dest;
91 char * csrc = src;
92 if ((cdest > csrc) && (cdest <= (csrc + bytes)))
9b8c947b
JW
93 {
94 /* do it backwards! */
36ce375d
JP
95 cdest += bytes;
96 csrc += bytes;
9b8c947b 97 while (bytes--)
36ce375d 98 *(--cdest) = *(--csrc);
9b8c947b 99 } else
90122e52 100 memcpy(dest, src, bytes);
9b8c947b
JW
101}
102
36ce375d 103int memcmp (const char *a2, const char *a1, int bytes) {
d56898ee 104 while (bytes--)
105 {
106 if (*(a2++) != *(a1++))
107 return 1;
108 }
109 return 0;
110}
111
36ce375d 112int strcmp (const char *a2, const char *a1) {
d56898ee 113 while (1) {
114 if (*a2 != *a1) return 1;
115 if (*a2 == 0) return 0;
116 a1++;
117 a2++;
118 }
119}
120
6f9272bd
JW
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
f2da2cd3 131int strlen(const char *c)
9b8c947b
JW
132{
133 int l = 0;
134 while (*(c++))
135 l++;
136 return l;
137}
138
36ce375d 139void strcpy(char *a2, const char *a1)
31be35cd
JW
140{
141 do {
142 *(a2++) = *a1;
143 } while (*(a1++));
144}
145
9343e933
JW
146void strcat(char *dest, char *src)
147{
148 while (*dest)
149 dest++;
150 while (*src)
151 *(dest++) = *(src++);
152 *(dest++) = *(src++);
153}
154
f2da2cd3 155void puts(const char *c)
9b8c947b
JW
156{
157 putbytes(c, strlen(c));
158}
159
160static char hexarr[] = "0123456789ABCDEF";
36ce375d 161void tohex(char *s, unsigned long l)
9b8c947b
JW
162{
163 int i;
164 for (i = 0; i < 8; i++)
165 {
31be35cd 166 s[i] = hexarr[l >> 28];
9b8c947b
JW
167 l <<= 4;
168 }
169}
31be35cd 170
db9fad13
JAW
171void btohex(char *s, unsigned char c)
172{
173 s[0] = hexarr[c >> 4];
174 s[1] = hexarr[c & 0xF];
175}
176
31be35cd
JW
177void puthex(unsigned long l)
178{
36ce375d 179 char d[9];
31be35cd
JW
180 d[8] = 0;
181 tohex(d, l);
182 puts(d);
183}
644af6b4
JP
184
185unsigned short htons(unsigned short in)
186{
187 return (in >> 8) | (in << 8);
188}
6e6d4a8b
JP
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.038947 seconds and 4 git commands to generate.