-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathesif_ccb_string_lin_user.h
238 lines (216 loc) · 6.14 KB
/
esif_ccb_string_lin_user.h
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
/******************************************************************************
** Copyright (c) 2013-2023 Intel Corporation All Rights Reserved
**
** Licensed under the Apache License, Version 2.0 (the "License"); you may not
** use this file except in compliance with the License.
**
** You may obtain a copy of the License at
** http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
** WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
**
** See the License for the specific language governing permissions and
** limitations under the License.
**
******************************************************************************/
#pragma once
#include "esif_ccb_memory.h"
#include <ctype.h>
#include <stdarg.h>
#include <string.h>
/*
* Kernel/User Agnostic
*/
#if defined(__GNUC__) && (__GNUC__ >= 11)
#if defined(__STDC_LIB_EXT1__)
#define esif_ccb_strnlen(str, siz) strnlen_s(str, siz)
#else
/* Compiler-Safe strnlen that only checks within given buffer length and allows NULL strings */
static ESIF_INLINE size_t esif_ccb_strnlen(
const char *str,
size_t siz
)
{
size_t len = 0;
if (str) {
while (len < siz && str[len]) {
len++;
}
}
return len;
}
#endif
#else
#define esif_ccb_strnlen(str, siz) strnlen(str, siz)
#endif
/* Safe strcpy that always null terminates target and truncates src if necessary */
static ESIF_INLINE char *esif_ccb_strcpy(
char *dst,
const char *src,
size_t siz
)
{
if (siz) {
size_t len = esif_ccb_strnlen(src, siz);
if (len >= siz) {
len = siz - 1;
}
esif_ccb_memmove(dst, src, len);
dst[len] = 0;
}
return dst;
}
/* Safe strcat that always null terminates target and truncates src if necessary */
static ESIF_INLINE char *esif_ccb_strcat(
char *dst,
const char *src,
size_t siz
)
{
if (siz) {
size_t len = esif_ccb_strnlen(dst, siz);
if (len >= siz) {
dst[siz - 1] = 0;
}
else {
strncat(dst, src, siz - len - 1);
}
}
return dst;
}
/* Safe strlen that only checks within given buffer length and allows NULL strings */
static ESIF_INLINE size_t esif_ccb_strlen(
const char *str,
size_t siz
)
{
return (str ? esif_ccb_strnlen(str, siz) : 0);
}
/* Return sprintf result string length not including null terminator, possibly truncated */
static ESIF_INLINE int esif_ccb_sprintf_len(size_t siz, int len)
{
return ((siz) && (len) >= (int)(siz) ? (int)(siz)-1 : (len));
}
/* Safe sprintf that avoids buffer overruns and always null-terminates target */
static ESIF_INLINE int esif_ccb_sprintf(
size_t siz, /* total size of str buffer, including existing string and null terminator */
char *str, /* null terminated string */
const char *fmt, /* format string */
...)
{
int rc = 0;
if (siz) {
va_list args;
va_start(args, fmt);
rc = esif_ccb_sprintf_len(siz, vsnprintf(str, siz, fmt, args));
va_end(args);
}
return rc;
}
#define esif_ccb_snprintf(str, siz, fmt, ...) esif_ccb_sprintf(siz, str, fmt, ##__VA_ARGS__)
#define esif_ccb_snprintf_concat(str, siz, fmt, ...) esif_ccb_sprintf_concat(siz, str, fmt, ##__VA_ARGS__)
#define esif_ccb_strpbrk(str, set) strpbrk(str, set)
#define esif_ccb_strcspn(str, set) strcspn(str, set)
/*
* User Mode Only
*
* NOTE: SCANFBUF is mandatory when using esif_ccb_sscanf or esif_ccb_vsscanf to scan into strings:
* esif_ccb_sscanf(str, "%s=%d", SCANFBUF(name, sizeof(name)), &value);
*/
#ifdef ESIF_ATTR_DEBUG
#define SCANFBUF(str, siz) (char *)esif_ccb_memset(str, 0xFE, siz)
#else
#define SCANFBUF(str, siz) str
#endif
#define esif_ccb_vsprintf(siz, str, fmt, arg) esif_ccb_sprintf_len(siz, vsnprintf(str, siz, fmt, arg))
#define esif_ccb_vsnprintf(str, siz, fmt, arg) esif_ccb_vsprintf(siz, str, fmt, arg)
#define esif_ccb_vsscanf(str, fmt, args) vsscanf(str, fmt, args)
#define esif_ccb_sscanf(str, fmt, ...) sscanf(str, fmt, ##__VA_ARGS__)
#define esif_ccb_strtok(str, sep, ctxt) strtok_r(str, sep, ctxt)
#define esif_ccb_strcmp(s1, s2) strcmp(s1, s2)
#define esif_ccb_stricmp(s1, s2) strcasecmp(s1, s2)
#define esif_ccb_strncmp(s1, s2, count) strncmp(s1, s2, count)
#define esif_ccb_strnicmp(s1, s2, cnt) strncasecmp(s1, s2, cnt)
#define esif_ccb_strstr(str, sub) strstr(str, sub)
#define esif_ccb_strchr(str, chr) strchr(str, chr)
#define esif_ccb_strrchr(str, chr) strrchr(str, chr)
/* Use Memtrace Overrides or Standard Functions? */
#ifdef ESIF_ATTR_MEMTRACE
#include "esif_ccb_memtrace.h"
#else
/* Linux Safe strdup() */
static ESIF_INLINE char *esif_ccb_strdup(const char *str)
{
char *newStr = NULL;
if(str) {
newStr = strdup(str);
}
return newStr;
}
#endif
/* Linux _strupr_s() equlivalent */
static ESIF_INLINE char *esif_ccb_strupr(
char *s,
size_t count
)
{
char *start = s;
for (; *s && count; s++, count--)
*s = toupper(*s);
return start;
}
/* Linux _strlwr_s() equlivalent */
static ESIF_INLINE char *esif_ccb_strlwr(
char *s,
size_t count
)
{
char *start = s;
for (; s && *s && count; s++, count--)
*s = tolower(*s);
return start;
}
/* Linux _vscprintf() equivalient */
static ESIF_INLINE int esif_ccb_vscprintf(
const char *format,
va_list args
)
{
int retval;
va_list argcopy;
va_copy(argcopy, args);
retval = vsnprintf(0, 0, format, argcopy);
va_end(argcopy);
return retval;
}
/* Safe sprintf that concatenates results to the end of a string and always null terminates target */
static ESIF_INLINE int esif_ccb_sprintf_concat(
size_t siz, /* total size of str buffer, including existing string */
char *str, /* null terminated string */
const char *fmt, /* format string */
...)
{
int rc = 0;
size_t len = esif_ccb_strlen(str, siz);
if (siz > len) {
va_list args;
va_start(args, fmt);
rc = (int)len + esif_ccb_sprintf_len(siz - len, esif_ccb_vsprintf(siz - len, str + len, fmt, args));
va_end(args);
}
return rc;
}
/* Safe strncpy that avoids buffer overruns, always null-terminates target, and pads nulls to end of buffer */
static ESIF_INLINE char *esif_ccb_strncpy(
char *dst,
const char *src,
size_t siz)
{
size_t len = esif_ccb_strlen(src, siz);
esif_ccb_strcpy(dst, src, siz);
if (len < siz)
esif_ccb_memset(dst + len, 0, siz - len);
return dst;
}