bh_definition.c 1022 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_platform.h"
  6. #define RSIZE_MAX 0x7FFFFFFF
  7. int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
  8. {
  9. char *dest = (char*) s1;
  10. char *src = (char*) s2;
  11. if (n == 0) {
  12. return 0;
  13. }
  14. if (s1 == NULL || s1max > RSIZE_MAX) {
  15. return -1;
  16. }
  17. if (s2 == NULL || n > s1max) {
  18. memset(dest, 0, s1max);
  19. return -1;
  20. }
  21. memcpy(dest, src, n);
  22. return 0;
  23. }
  24. int b_strcat_s(char * s1, size_t s1max, const char * s2)
  25. {
  26. if (NULL == s1 || NULL == s2
  27. || s1max < (strlen(s1) + strlen(s2) + 1)
  28. || s1max > RSIZE_MAX) {
  29. return -1;
  30. }
  31. strcat(s1, s2);
  32. return 0;
  33. }
  34. int b_strcpy_s(char * s1, size_t s1max, const char * s2)
  35. {
  36. if (NULL == s1|| NULL == s2
  37. || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
  38. return -1;
  39. }
  40. strcpy(s1, s2);
  41. return 0;
  42. }