bh_definition.c 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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)
  38. || s1max > RSIZE_MAX) {
  39. return -1;
  40. }
  41. strcpy(s1, s2);
  42. return 0;
  43. }
  44. int fopen_s(FILE ** pFile, const char *filename, const char *mode)
  45. {
  46. if (NULL == pFile || NULL == filename || NULL == mode) {
  47. return -1;
  48. }
  49. *pFile = fopen(filename, mode);
  50. if (NULL == *pFile)
  51. return -1;
  52. return 0;
  53. }