-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCKLib.c
285 lines (251 loc) · 7.53 KB
/
CKLib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*****************************************************************************
* Project Name: CKOS
* Authors: Casey Artner & Kenny Deardorff
* Class: CST 352 - Operating Systems
* File: CKLib.c - CKOS Library
*****************************************************************************/
#include "CKLib.h"
//Error Vars
char CKLIB_ERROR[255] = { "" };
BOOL CKLIB_EFLAG = FALSE;
/*****************************************************************************
* Function Name: Memcpy
* Params:
* void * m_From - Address to copy from
* void * m_To - Address to copy to
* int Length - Length in bytes to copy
* Returns:
* void
* Discription:
* Copies a piece of memory from [m_From] to [m_To].
*****************************************************************************/
void Memcpy (unsigned char * m_From, unsigned char * m_To, int Length)
{
int i = 0;
for (i = 0; i < Length; i++)
{
m_To[i] = m_From[i];
}
}
/*****************************************************************************
* Function Name: Strcpy
* Params:
* void * m_From - Address to copy from
* void * m_To - Address to copy to
* Returns:
* void
* Discription:
* Copies a null terminated string from [m_From] to [m_To].
*****************************************************************************/
void Strcpy (unsigned char * m_From, unsigned char * m_To)
{
int i = 0;
while ( m_From[i] != 0 )
{
m_To[i] = m_From[i];
++i;
}
}
/*****************************************************************************
* Function Name: Strcmp
* Params:
* char * string1 - String 1
* char * string2 - String 2
* Returns:
* int - 1 means string 1 comes before string 2
* - -1 means string 2 comes before string 1
* - 0 means string 1 is the same as string 2
* Discription:
* Compairs two strings to see if they are the same. Returns the
* coresponding integer.
*****************************************************************************/
int Strcmp(unsigned char * string1, unsigned char * string2)
{
while(1)
{
if ((*string1 == 0) && (*string2 == 0))
return 0;
if (*string1 == 0)
return 2;
if (*string2 == 0)
return -2;
if (*string1 > *string2)
return -1;
if (*string1 < *string2)
return 1;
++string1;
++string2;
}
}
/*****************************************************************************
* Function Name: SStrcmp
* Discription:
* Overload for Strcmp
*****************************************************************************/
int SStrcmp(unsigned char * string1, char * string2)
{
return Strcmp(string1, (unsigned char *)string2);
}
//Version by Casey
//int Strcmp(PTR dest, PTR src)
//{
// int i = 0;
// while(dest[i] || src[i])
// {
// if(dest[i] > src[i])
// return -1;
// if(dest[i] < src[i])
// return 1;
// i++;
// }
// return 0;
//}
/*****************************************************************************
* Function Name: AtoH
* Params:
* unsigned char * string - ASCII string
* unsigned char * hex - Hex string
* Returns:
* void
* Discription:
* Converts an ASCII null-terminated string into a hex string. [hex] should
* be 1/2 as long or greater than the ASCII string.
*****************************************************************************/
void AtoH (unsigned char * string, unsigned char * hex)
{
int i = 0; //index
char b = 0; //buffer
while (string[i] != 0)
{
if ((string[i] >= '0') &&
(string[i] <= '9'))
{
b = string[i] - '0'; //convert ASCII to hex
}
else if ((string[i] >= 'A') &&
(string[i] <= 'F'))
{
b = string[i] - 'A' + 10; //convert ASCII to hex
}
b <<= 4; //shift hex value over
++i;
if ((string[i] >= '0') &&
(string[i] <= '9'))
{
b = b | (string[i] - '0'); //convert ASCII to hex and OR the two values together
}
else if ((string[i] >= 'A') &&
(string[i] <= 'F'))
{
b = b | (string[i] - 'A' + 10); //convert ASCII to hex and OR the two values together
}
hex[(i - 1) / 2] = b; //add buffer to string
++i;
}
hex[(i + 1) / 2] = 0; //null terminate string
}
/*****************************************************************************
* Function Name: PtoX
* Params:
* str - Char array for string storage
* ptr - Pointer to be converted
* Returns:
* void
* Discription:
* Converts [ptr] to a hex string [str] (0xXXXX)
*****************************************************************************/
void PtoX(char * str, const unsigned char * ptr)
{
unsigned int buffer = (unsigned int)ptr;
int pos = 3;
char lookup[] = "0123456789ABCDEF";
Strcpy(CtoUC(str), (unsigned char *)"0000");
while (pos != -1)
{
str[pos--] = lookup[buffer & 0x0000000F];
buffer >>= 4;
}
str[4] = 0;
}
/*****************************************************************************
* Function Name: CtoX
* Params:
* str - Char array for string storage
* c - Character to be converted
* Returns:
* void
* Discription:
* Converts an unsigned char [c] to a hex string [str] (0xXX)
*****************************************************************************/
void CtoX(char * str, const unsigned char c)
{
char lookup[] = { "0123456789ABCDEF" };
unsigned char ch = 0;
ch = (c & 0xF0);
ch = (ch >> 4);
str[0] = lookup[ch];
ch = (c & 0x0F);
str[1] = lookup[ch];
str[2] = 0;
}
/*****************************************************************************
* Function Name: UCtoC
* Params:
* uc - unsigned character pointer
* Returns:
* character pointer
* Discription:
* Easy/Lazy typecasting from unsigned char * to char *
*****************************************************************************/
char * UCtoC(unsigned char * uc)
{
return (char *)uc;
}
/*****************************************************************************
* Function Name: CtoUC
* Params:
* c - character pointer
* Returns:
* unsigned character pointer
* Discription:
* Easy/Lazy typecasting from char * to unsigned char *
*****************************************************************************/
unsigned char * CtoUC(char * c)
{
return (unsigned char *)c;
}
/*****************************************************************************
* Function Name: SetError
* Params:
* err - String containing error message
* Returns:
* void
* Discription:
* Sets the current system error and prints it
*****************************************************************************/
void SetError(char * err)
{
CKLIB_EFLAG = TRUE;
Strcpy((unsigned char *)err, (unsigned char *)CKLIB_ERROR);
puts("[System Error] - ");
puts(CKLIB_ERROR);
puts("\n");
}
/*****************************************************************************
* Function Name: GetLastError
* Params:
* void
* Returns:
* char * containing last system error message
* Discription:
* Returns the last system error message set
*****************************************************************************/
char * GetLastError()
{
if(CKLIB_EFLAG == TRUE)
{
CKLIB_EFLAG = FALSE;
return CKLIB_ERROR;
}
return 0;
}