memcpy_s.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /*
  2. * Copyright (c) 2013-2023, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2023, Intel Corporation. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <errno.h>
  8. #include <stddef.h>
  9. #include <string.h>
  10. int memcpy_s(void *dst, size_t dsize, void *src, size_t ssize)
  11. {
  12. unsigned int *s = (unsigned int *)src;
  13. unsigned int *d = (unsigned int *)dst;
  14. /*
  15. * Check source and destination size is NULL
  16. */
  17. if ((dst == NULL) || (src == NULL)) {
  18. return -ENOMEM;
  19. }
  20. /*
  21. * Check source and destination size validity
  22. */
  23. if ((dsize == 0) || (ssize == 0)) {
  24. return -ERANGE;
  25. }
  26. /*
  27. * Check both source and destination size range
  28. */
  29. if ((ssize > dsize) || (dsize > ssize)) {
  30. return -EINVAL;
  31. }
  32. /*
  33. * Check both source and destination address overlapping
  34. * When (s > d < s + ssize)
  35. * Or (d > s < d + dsize)
  36. */
  37. if (d > s) {
  38. if ((d) < (s + ssize)) {
  39. return -EOPNOTSUPP;
  40. }
  41. }
  42. if (s > d) {
  43. if ((s) < (d + dsize)) {
  44. return -EOPNOTSUPP;
  45. }
  46. }
  47. /*
  48. * Start copy process when there is no error
  49. */
  50. while (ssize--) {
  51. d[ssize] = s[ssize];
  52. }
  53. return 0;
  54. }