bcmstdlib_s.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * Broadcom Secure Standard Library.
  3. *
  4. * Portions of this code are copyright (c) 2020 Cypress Semiconductor Corporation
  5. *
  6. * Copyright (C) 1999-2020, Broadcom Corporation
  7. *
  8. * Unless you and Broadcom execute a separate written software license
  9. * agreement governing use of this software, this software is licensed to you
  10. * under the terms of the GNU General Public License version 2 (the "GPL"),
  11. * available at http://www.broadcom.com/licenses/GPLv2.php, with the
  12. * following added to such license:
  13. *
  14. * As a special exception, the copyright holders of this software give you
  15. * permission to link this software with independent modules, and to copy and
  16. * distribute the resulting executable under terms of your choice, provided that
  17. * you also meet, for each linked independent module, the terms and conditions of
  18. * the license of that module. An independent module is a module which is not
  19. * derived from this software. The special exception does not apply to any
  20. * modifications of the software.
  21. *
  22. * Notwithstanding the above, under no circumstances may you combine this
  23. * software in any way with any other Broadcom software provided under a license
  24. * other than the GPL, without Broadcom's express prior written consent.
  25. *
  26. *
  27. * <<Broadcom-WL-IPTag/Open:>>
  28. *
  29. * $Id $
  30. */
  31. #include <bcm_cfg.h>
  32. #include <typedefs.h>
  33. #include <bcmdefs.h>
  34. #ifdef BCMDRIVER
  35. #include <osl.h>
  36. #else /* BCMDRIVER */
  37. #include <stddef.h>
  38. #include <string.h>
  39. #endif /* else BCMDRIVER */
  40. #include <bcmstdlib_s.h>
  41. #include <bcmutils.h>
  42. /*
  43. * __SIZE_MAX__ value is depending on platform:
  44. * Firmware Dongle: RAMSIZE (Dongle Specific Limit).
  45. * LINUX NIC/Windows/MACOSX/Application: OS Native or
  46. * 0xFFFFFFFFu if not defined.
  47. */
  48. #ifndef SIZE_MAX
  49. #ifndef __SIZE_MAX__
  50. #define __SIZE_MAX__ 0xFFFFFFFFu
  51. #endif /* __SIZE_MAX__ */
  52. #define SIZE_MAX __SIZE_MAX__
  53. #endif /* SIZE_MAX */
  54. #define RSIZE_MAX (SIZE_MAX >> 1u)
  55. #if !defined(__STDC_WANT_SECURE_LIB__) && !(defined(__STDC_LIB_EXT1__) && \
  56. defined(__STDC_WANT_LIB_EXT1__))
  57. /*
  58. * memmove_s - secure memmove
  59. * dest : pointer to the object to copy to
  60. * destsz : size of the destination buffer
  61. * src : pointer to the object to copy from
  62. * n : number of bytes to copy
  63. * Return Value : zero on success and non-zero on error
  64. * Also on error, if dest is not a null pointer and destsz not greater
  65. * than RSIZE_MAX, writes destsz zero bytes into the dest object.
  66. */
  67. int
  68. memmove_s(void *dest, size_t destsz, const void *src, size_t n)
  69. {
  70. int err = BCME_OK;
  71. if ((!dest) || (((char *)dest + destsz) < (char *)dest)) {
  72. err = BCME_BADARG;
  73. goto exit;
  74. }
  75. if (destsz > RSIZE_MAX) {
  76. err = BCME_BADLEN;
  77. goto exit;
  78. }
  79. if (destsz < n) {
  80. memset(dest, 0, destsz);
  81. err = BCME_BADLEN;
  82. goto exit;
  83. }
  84. if ((!src) || (((const char *)src + n) < (const char *)src)) {
  85. memset(dest, 0, destsz);
  86. err = BCME_BADARG;
  87. goto exit;
  88. }
  89. memmove(dest, src, n);
  90. exit:
  91. return err;
  92. }
  93. /*
  94. * memcpy_s - secure memcpy
  95. * dest : pointer to the object to copy to
  96. * destsz : size of the destination buffer
  97. * src : pointer to the object to copy from
  98. * n : number of bytes to copy
  99. * Return Value : zero on success and non-zero on error
  100. * Also on error, if dest is not a null pointer and destsz not greater
  101. * than RSIZE_MAX, writes destsz zero bytes into the dest object.
  102. */
  103. int
  104. memcpy_s(void *dest, size_t destsz, const void *src, size_t n)
  105. {
  106. int err = BCME_OK;
  107. char *d = dest;
  108. const char *s = src;
  109. if ((!d) || ((d + destsz) < d)) {
  110. err = BCME_BADARG;
  111. goto exit;
  112. }
  113. if (destsz > RSIZE_MAX) {
  114. err = BCME_BADLEN;
  115. goto exit;
  116. }
  117. if (destsz < n) {
  118. memset(dest, 0, destsz);
  119. err = BCME_BADLEN;
  120. goto exit;
  121. }
  122. if ((!s) || ((s + n) < s)) {
  123. memset(dest, 0, destsz);
  124. err = BCME_BADARG;
  125. goto exit;
  126. }
  127. /* overlap checking between dest and src */
  128. if (!(((d + destsz) <= s) || (d >= (s + n)))) {
  129. memset(dest, 0, destsz);
  130. err = BCME_BADARG;
  131. goto exit;
  132. }
  133. (void)memcpy(dest, src, n);
  134. exit:
  135. return err;
  136. }
  137. /*
  138. * memset_s - secure memset
  139. * dest : pointer to the object to be set
  140. * destsz : size of the destination buffer
  141. * c : byte value
  142. * n : number of bytes to be set
  143. * Return Value : zero on success and non-zero on error
  144. * Also on error, if dest is not a null pointer and destsz not greater
  145. * than RSIZE_MAX, writes destsz bytes with value c into the dest object.
  146. */
  147. int
  148. memset_s(void *dest, size_t destsz, int c, size_t n)
  149. {
  150. int err = BCME_OK;
  151. if ((!dest) || (((char *)dest + destsz) < (char *)dest)) {
  152. err = BCME_BADARG;
  153. goto exit;
  154. }
  155. if (destsz > RSIZE_MAX) {
  156. err = BCME_BADLEN;
  157. goto exit;
  158. }
  159. if (destsz < n) {
  160. (void)memset(dest, c, destsz);
  161. err = BCME_BADLEN;
  162. goto exit;
  163. }
  164. (void)memset(dest, c, n);
  165. exit:
  166. return err;
  167. }
  168. #endif /* !__STDC_WANT_SECURE_LIB__ && !(__STDC_LIB_EXT1__ && __STDC_WANT_LIB_EXT1__) */
  169. #if !defined(FREEBSD) && !defined(BCM_USE_PLATFORM_STRLCPY)
  170. /**
  171. * strlcpy - Copy a %NUL terminated string into a sized buffer
  172. * @dest: Where to copy the string to
  173. * @src: Where to copy the string from
  174. * @size: size of destination buffer 0 if input parameters are NOK
  175. * return: string leng of src (assume src is NUL terminated)
  176. *
  177. * Compatible with *BSD: the result is always a valid
  178. * NUL-terminated string that fits in the buffer (unless,
  179. * of course, the buffer size is zero). It does not pad
  180. * out the result like strncpy() does.
  181. */
  182. size_t strlcpy(char *dest, const char *src, size_t size)
  183. {
  184. const char *s = src;
  185. size_t n;
  186. if (dest == NULL) {
  187. return 0;
  188. }
  189. /* terminate dest if src is NULL and return 0 as only NULL was added */
  190. if (s == NULL) {
  191. *dest = '\0';
  192. return 0;
  193. }
  194. /* allows us to handle size 0 */
  195. if (size == 0) {
  196. n = 0;
  197. } else {
  198. n = size - 1u;
  199. }
  200. /* perform copy */
  201. while (*s && n != 0) {
  202. *dest++ = *s++;
  203. n--;
  204. }
  205. *dest = '\0';
  206. /* count to end of s or compensate for NULL */
  207. if (n == 0) {
  208. while (*s++)
  209. ;
  210. } else {
  211. s++;
  212. }
  213. /* return bytes copied not accounting NUL */
  214. return (s - src - 1u);
  215. }
  216. #endif // endif
  217. /**
  218. * strlcat_s - Concatenate a %NUL terminated string with a sized buffer
  219. * @dest: Where to concatenate the string to
  220. * @src: Where to copy the string from
  221. * @size: size of destination buffer
  222. * return: string length of created string (i.e. the initial length of dest plus the length of src)
  223. * not including the NUL char, up until size
  224. *
  225. * Unlike strncat(), strlcat() take the full size of the buffer (not just the number of bytes to
  226. * copy) and guarantee to NUL-terminate the result (even when there's nothing to concat).
  227. * If the length of dest string concatinated with the src string >= size, truncation occurs.
  228. *
  229. * Compatible with *BSD: the result is always a valid NUL-terminated string that fits in the buffer
  230. * (unless, of course, the buffer size is zero).
  231. *
  232. * If either src or dest is not NUL-terminated, dest[size-1] will be set to NUL.
  233. * If size < strlen(dest) + strlen(src), dest[size-1] will be set to NUL.
  234. * If size == 0, dest[0] will be set to NUL.
  235. */
  236. size_t
  237. strlcat_s(char *dest, const char *src, size_t size)
  238. {
  239. char *d = dest;
  240. const char *s = src; /* point to the start of the src string */
  241. size_t n = size;
  242. size_t dlen;
  243. size_t bytes_to_copy = 0;
  244. if (dest == NULL) {
  245. return 0;
  246. }
  247. /* set d to point to the end of dest string (up to size) */
  248. while (n != 0 && *d != '\0') {
  249. d++;
  250. n--;
  251. }
  252. dlen = (size_t)(d - dest);
  253. if (s != NULL) {
  254. size_t slen = 0;
  255. /* calculate src len in case it's not null-terminated */
  256. n = size;
  257. while (n-- != 0 && *(s + slen) != '\0') {
  258. ++slen;
  259. }
  260. n = size - dlen; /* maximum num of chars to copy */
  261. if (n != 0) {
  262. /* copy relevant chars (until end of src buf or given size is reached) */
  263. bytes_to_copy = MIN(slen - (size_t)(s - src), n - 1);
  264. (void)memcpy(d, s, bytes_to_copy);
  265. d += bytes_to_copy;
  266. }
  267. }
  268. if (n == 0 && dlen != 0) {
  269. --d; /* nothing to copy, but NUL-terminate dest anyway */
  270. }
  271. *d = '\0'; /* NUL-terminate dest */
  272. return (dlen + bytes_to_copy);
  273. }