forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
127 lines (105 loc) · 2.38 KB
/
Copy pathutils.c
File metadata and controls
127 lines (105 loc) · 2.38 KB
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
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <ryadbenadjila@gmail.com>
* Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
* Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
*
* Contributors:
* Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
* Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
*
* 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 if first 'len' bytes of both buffers a and b are equal. It
* returns 0 otherwise. The test is done in constant time.
*/
u8 are_equal(const void *a, const void *b, u32 len)
{
const u8 *la = a, *lb = b;
u8 ret = 1;
u32 i;
for (i = 0; i < len; i++) {
ret &= (*la == *lb);
la++;
lb++;
}
return ret;
}
/* This function is a simple (non-optimized) reimplementation of memcpy() */
void local_memcpy(void *dst, const void *src, u32 n)
{
const u8 *lsrc = src;
u8 *ldst = dst;
u32 i;
for (i = 0; i < n; i++) {
*ldst = *lsrc;
ldst++;
lsrc++;
}
}
/* This function is a simple (non-optimized) reimplementation of memset() */
void local_memset(void *v, u8 c, u32 n)
{
volatile u8 *p = v;
u32 i;
for (i = 0; i < n; i++) {
*p = c;
p++;
}
}
/* This function returns 1 if strings are equal and 0 otherise */
u8 are_str_equal(const char *s1, const char *s2)
{
const char *ls1 = s1, *ls2 = s2;
while (*ls1 && (*ls1 == *ls2)) {
ls1++;
ls2++;
}
return *ls1 == *ls2;
}
/* This function is a simple (non-optimized) reimplementation of strlen() */
u32 local_strlen(const char *s)
{
u32 i = 0;
while (s[i]) {
i++;
}
return i;
}
/* This function is a simple (non-optimized) reimplementation of strnlen() */
u32 local_strnlen(const char *s, u32 maxlen)
{
u32 i = 0;
while ((i < maxlen) && s[i]) {
i++;
}
return i;
}
/* This functin is a simple (non-optimized) reimplementation of strncpy() */
char *local_strncpy(char *dst, const char *src, u32 n)
{
u32 i;
for (i = 0; i < n && src[i]; i++) {
dst[i] = src[i];
}
for (; i < n; i++) {
dst[i] = 0;
}
return dst;
}
/* This functin is a simple (non-optimized) reimplementation of strncat() */
char *local_strncat(char *dst, const char *src, u32 n)
{
u32 dst_len, i;
dst_len = local_strlen(dst);
for (i = 0; i < n && src[i]; i++) {
dst[dst_len + i] = src[i];
}
dst[dst_len + i] = 0;
return dst;
}