hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/* SPDX-License-Identifier: GPL-2.0 */
/*
 * Broadcom Secure Standard Library.
 *
 * Copyright (C) 1999-2019, Broadcom.
 *
 *      Unless you and Broadcom execute a separate written software license
 * agreement governing use of this software, this software is licensed to you
 * under the terms of the GNU General Public License version 2 (the "GPL"),
 * available at http://www.broadcom.com/licenses/GPLv2.php, with the
 * following added to such license:
 *
 *      As a special exception, the copyright holders of this software give you
 * permission to link this software with independent modules, and to copy and
 * distribute the resulting executable under terms of your choice, provided that
 * you also meet, for each linked independent module, the terms and conditions of
 * the license of that module.  An independent module is a module which is not
 * derived from this software.  The special exception does not apply to any
 * modifications of the software.
 *
 *      Notwithstanding the above, under no circumstances may you combine this
 * software in any way with any other Broadcom software provided under a license
 * other than the GPL, without Broadcom's express prior written consent.
 *
 *
 * <<Broadcom-WL-IPTag/Open:>>
 *
 * $Id $
 */
 
#include <bcm_cfg.h>
#include <typedefs.h>
#include <bcmdefs.h>
#ifdef BCMDRIVER
#include <osl.h>
#else /* BCMDRIVER */
#include <stddef.h>
#include <string.h>
#endif /* else BCMDRIVER */
 
#include <bcmstdlib_s.h>
#include <bcmutils.h>
 
/*
 * __SIZE_MAX__ value is depending on platform:
 * Firmware Dongle: RAMSIZE (Dongle Specific Limit).
 * LINUX NIC/Windows/MACOSX/Application: OS Native or
 * 0xFFFFFFFFu if not defined.
 */
#ifndef SIZE_MAX
#ifndef __SIZE_MAX__
#define __SIZE_MAX__ 0xFFFFFFFFu
#endif /* __SIZE_MAX__ */
#define SIZE_MAX __SIZE_MAX__
#endif /* SIZE_MAX */
#define RSIZE_MAX (SIZE_MAX >> 1u)
 
#if !defined(__STDC_WANT_SECURE_LIB__) && !(defined(__STDC_LIB_EXT1__) && \
   defined(__STDC_WANT_LIB_EXT1__))
/*
 * memmove_s - secure memmove
 * dest : pointer to the object to copy to
 * destsz : size of the destination buffer
 * src : pointer to the object to copy from
 * n : number of bytes to copy
 * Return Value : zero on success and non-zero on error
 * Also on error, if dest is not a null pointer and destsz not greater
 * than RSIZE_MAX, writes destsz zero bytes into the dest object.
 */
int
memmove_s(void *dest, size_t destsz, const void *src, size_t n)
{
   int err = BCME_OK;
 
   if ((!dest) || (((char *)dest + destsz) < (char *)dest)) {
       err = BCME_BADARG;
       goto exit;
   }
 
   if (destsz > RSIZE_MAX) {
       err = BCME_BADLEN;
       goto exit;
   }
 
   if (destsz < n) {
       memset(dest, 0, destsz);
       err = BCME_BADLEN;
       goto exit;
   }
 
   if ((!src) || (((const char *)src + n) < (const char *)src)) {
       memset(dest, 0, destsz);
       err = BCME_BADARG;
       goto exit;
   }
 
   memmove(dest, src, n);
exit:
   return err;
}
 
/*
 * memcpy_s - secure memcpy
 * dest : pointer to the object to copy to
 * destsz : size of the destination buffer
 * src : pointer to the object to copy from
 * n : number of bytes to copy
 * Return Value : zero on success and non-zero on error
 * Also on error, if dest is not a null pointer and destsz not greater
 * than RSIZE_MAX, writes destsz zero bytes into the dest object.
 */
int
memcpy_s(void *dest, size_t destsz, const void *src, size_t n)
{
   int err = BCME_OK;
   char *d = dest;
   const char *s = src;
 
   if ((!d) || ((d + destsz) < d)) {
       err = BCME_BADARG;
       goto exit;
   }
 
   if (destsz > RSIZE_MAX) {
       err = BCME_BADLEN;
       goto exit;
   }
 
   if (destsz < n) {
       memset(dest, 0, destsz);
       err = BCME_BADLEN;
       goto exit;
   }
 
   if ((!s) || ((s + n) < s)) {
       memset(dest, 0, destsz);
       err = BCME_BADARG;
       goto exit;
   }
 
   /* overlap checking between dest and src */
   if (!(((d + destsz) <= s) || (d >= (s + n)))) {
       memset(dest, 0, destsz);
       err = BCME_BADARG;
       goto exit;
   }
 
   (void)memcpy(dest, src, n);
exit:
   return err;
}
 
/*
 * memset_s - secure memset
 * dest : pointer to the object to be set
 * destsz : size of the destination buffer
 * c : byte value
 * n : number of bytes to be set
 * Return Value : zero on success and non-zero on error
 * Also on error, if dest is not a null pointer and destsz not greater
 * than RSIZE_MAX, writes destsz bytes with value c into the dest object.
 */
int
memset_s(void *dest, size_t destsz, int c, size_t n)
{
   int err = BCME_OK;
   if ((!dest) || (((char *)dest + destsz) < (char *)dest)) {
       err = BCME_BADARG;
       goto exit;
   }
 
   if (destsz > RSIZE_MAX) {
       err = BCME_BADLEN;
       goto exit;
   }
 
   if (destsz < n) {
       (void)memset(dest, c, destsz);
       err = BCME_BADLEN;
       goto exit;
   }
 
   (void)memset(dest, c, n);
exit:
   return err;
}
#endif /* !__STDC_WANT_SECURE_LIB__ && !(__STDC_LIB_EXT1__ && __STDC_WANT_LIB_EXT1__) */
 
#if 0
/**
 * strlcpy - Copy a %NUL terminated string into a sized buffer
 * @dest: Where to copy the string to
 * @src: Where to copy the string from
 * @size: size of destination buffer 0 if input parameters are NOK
 * return: string leng of src (assume src is NUL terminated)
 *
 * Compatible with *BSD: the result is always a valid
 * NUL-terminated string that fits in the buffer (unless,
 * of course, the buffer size is zero). It does not pad
 * out the result like strncpy() does.
 */
size_t strlcpy(char *dest, const char *src, size_t size)
{
   const char *s = src;
   size_t n;
 
   if (dest == NULL) {
       return 0;
   }
 
   /* terminate dest if src is NULL and return 0 as only NULL was added */
   if (s == NULL) {
       *dest = '\0';
       return 0;
   }
 
   /* allows us to handle size 0 */
   if (size == 0) {
       n = 0;
   } else {
       n = size - 1u;
   }
 
   /* perform copy */
   while (*s && n != 0) {
       *dest++ = *s++;
       n--;
   }
 
   *dest = '\0';
 
   /* count to end of s or compensate for NULL */
   if (n == 0) {
       while (*s++)
           ;
   } else {
       s++;
   }
 
   /* return bytes copied not accounting NUL */
   return (s - src - 1u);
}
#endif // endif
 
/**
 * strlcat_s - Concatenate a %NUL terminated string with a sized buffer
 * @dest: Where to concatenate the string to
 * @src: Where to copy the string from
 * @size: size of destination buffer
 * return: string length of created string (i.e. the initial length of dest plus the length of src)
 *         not including the NUL char, up until size
 *
 * Unlike strncat(), strlcat() take the full size of the buffer (not just the number of bytes to
 * copy) and guarantee to NUL-terminate the result (even when there's nothing to concat).
 * If the length of dest string concatinated with the src string >= size, truncation occurs.
 *
 * Compatible with *BSD: the result is always a valid NUL-terminated string that fits in the buffer
 * (unless, of course, the buffer size is zero).
 *
 * If either src or dest is not NUL-terminated, dest[size-1] will be set to NUL.
 * If size < strlen(dest) + strlen(src), dest[size-1] will be set to NUL.
 * If size == 0, dest[0] will be set to NUL.
 */
size_t
strlcat_s(char *dest, const char *src, size_t size)
{
   char *d = dest;
   const char *s = src;    /* point to the start of the src string */
   size_t n = size;
   size_t dlen;
   size_t bytes_to_copy = 0;
 
   if (dest == NULL) {
       return 0;
   }
 
   /* set d to point to the end of dest string (up to size) */
   while (n != 0 && *d != '\0') {
       d++;
       n--;
   }
   dlen = (size_t)(d - dest);
 
   if (s != NULL) {
       size_t slen = 0;
 
       /* calculate src len in case it's not null-terminated */
       n = size;
       while (n-- != 0 && *(s + slen) != '\0') {
           ++slen;
       }
 
       n = size - dlen;    /* maximum num of chars to copy */
       if (n != 0) {
           /* copy relevant chars (until end of src buf or given size is reached) */
           bytes_to_copy = MIN(slen - (size_t)(s - src), n - 1);
           (void)memcpy(d, s, bytes_to_copy);
           d += bytes_to_copy;
       }
   }
   if (n == 0 && dlen != 0) {
       --d;    /* nothing to copy, but NUL-terminate dest anyway */
   }
   *d = '\0';    /* NUL-terminate dest */
 
   return (dlen + bytes_to_copy);
}