This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathutils.c
233 lines (187 loc) · 4.17 KB
/
utils.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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#include "utils.h"
/*
* Return 1 in 'check' if first 'len' bytes of both buffers a and b are equal, 0 otherwise.
* It returns 0 if success, -1 on error. 'check' is only relevant on success.
*
* The test is done in constant time.
*/
int are_equal(const void *a, const void *b, u32 len, int *check)
{
const u8 *la = (const u8*)a, *lb = (const u8*)b;
int ret;
u32 i;
MUST_HAVE((a != NULL) && (b != NULL) && (check != NULL), ret, err);
*check = 1;
for (i = 0; i < len; i++) {
(*check) &= (*la == *lb);
la++;
lb++;
}
ret = 0;
err:
return ret;
}
/*
* This function is a simple (non-optimized) reimplementation of memcpy()
* Returns 0 on success, -1 on error.
*/
int local_memcpy(void *dst, const void *src, u32 n)
{
const u8 *lsrc = (const u8*)src;
u8 *ldst = (u8*)dst;
u32 i;
int ret;
MUST_HAVE((dst != NULL) && (src != NULL), ret, err);
for (i = 0; i < n; i++) {
*ldst = *lsrc;
ldst++;
lsrc++;
}
ret = 0;
err:
return ret;
}
/*
* This function is a simple (non-optimized) reimplementation of memset()
* Returns 0 on success, -1 on error.
*/
int local_memset(void *v, u8 c, u32 n)
{
volatile u8 *p = (volatile u8*)v;
u32 i;
int ret;
MUST_HAVE((v != NULL), ret, err);
for (i = 0; i < n; i++) {
*p = c;
p++;
}
ret = 0;
err:
return ret;
}
/*
* Return 1 in 'check' if strings are equal, 0 otherwise.
* It returns 0 if success, -1 on error. 'check' is only relevant on success.
*
*/
int are_str_equal(const char *s1, const char *s2, int *check)
{
const char *ls1 = s1, *ls2 = s2;
int ret;
MUST_HAVE((s1 != NULL) && (s2 != NULL) && (check != NULL), ret, err);
while (*ls1 && (*ls1 == *ls2)) {
ls1++;
ls2++;
}
(*check) = (*ls1 == *ls2);
ret = 0;
err:
return ret;
}
/*
* Return 1 in 'check' if strings are equal up to maxlen, 0 otherwise.
* It returns 0 if success, -1 on error. 'check' is only relevant on success.
*
*/
int are_str_equal_nlen(const char *s1, const char *s2, u32 maxlen, int *check)
{
const char *ls1 = s1, *ls2 = s2;
u32 i = 0;
int ret;
MUST_HAVE((s1 != NULL) && (s2 != NULL) && (check != NULL), ret, err);
while (*ls1 && (*ls1 == *ls2) && (i < maxlen)) {
ls1++;
ls2++;
i++;
}
(*check) = (*ls1 == *ls2);
ret = 0;
err:
return ret;
}
/*
* This function is a simple (non-optimized) reimplementation of strlen()
* Returns the lenth in 'len'.
* It returns 0 if success, -1 on error. 'len' is only relevant on success.
*/
int local_strlen(const char *s, u32 *len)
{
u32 i = 0;
int ret;
MUST_HAVE((s != NULL) && (len != NULL), ret, err);
while (s[i]) {
i++;
}
(*len) = i;
ret = 0;
err:
return ret;
}
/*
* This function is a simple (non-optimized) reimplementation of strnlen()
* Returns the lenth in 'len'.
* It returns 0 if success, -1 on error. 'len' is only relevant on success.
*/
int local_strnlen(const char *s, u32 maxlen, u32 *len)
{
u32 i = 0;
int ret;
MUST_HAVE((s != NULL) && (len != NULL), ret, err);
while ((i < maxlen) && s[i]) {
i++;
}
(*len) = i;
ret = 0;
err:
return ret;
}
/*
* This functin is a simple (non-optimized) reimplementation of strncpy()
*/
int local_strncpy(char *dst, const char *src, u32 n)
{
u32 i;
int ret;
MUST_HAVE((dst != NULL) && (src != NULL), ret, err);
for (i = 0; (i < n) && src[i]; i++) {
dst[i] = src[i];
}
for (; i < n; i++) {
dst[i] = 0;
}
ret = 0;
err:
return ret;
}
/*
* This functin is a simple (non-optimized) reimplementation of strncat()
*/
int local_strncat(char *dst, const char *src, u32 n)
{
u32 dst_len, i;
int ret;
MUST_HAVE((dst != NULL) && (src != NULL), ret, err);
ret = local_strlen(dst, &dst_len); EG(ret, err);
for (i = 0; (i < n) && src[i]; i++) {
dst[dst_len + i] = src[i];
}
dst[dst_len + i] = 0;
ret = 0;
err:
return ret;
}