bh_definition.c 1.3 KB

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