bh_definition.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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. #ifdef RSIZE_MAX
  7. #undef RSIZE_MAX
  8. #endif
  9. #define RSIZE_MAX 0x7FFFFFFF
  10. int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
  11. {
  12. char *dest = (char*) s1;
  13. char *src = (char*) s2;
  14. if (n == 0) {
  15. return 0;
  16. }
  17. if (s1 == NULL || s1max > RSIZE_MAX) {
  18. return -1;
  19. }
  20. if (s2 == NULL || n > s1max) {
  21. memset(dest, 0, s1max);
  22. return -1;
  23. }
  24. memcpy(dest, src, n);
  25. return 0;
  26. }
  27. int b_strcat_s(char * s1, size_t s1max, const char * s2)
  28. {
  29. if (NULL == s1 || NULL == s2
  30. || s1max < (strlen(s1) + strlen(s2) + 1)
  31. || s1max > RSIZE_MAX) {
  32. return -1;
  33. }
  34. strcat(s1, s2);
  35. return 0;
  36. }
  37. int b_strcpy_s(char * s1, size_t s1max, const char * s2)
  38. {
  39. if (NULL == s1 || NULL == s2
  40. || s1max < (strlen(s2) + 1)
  41. || 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. }