bh_definition.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "bh_definition.h"
  17. #include "bh_platform.h"
  18. int bh_return(int ret)
  19. {
  20. return ret;
  21. }
  22. #define RSIZE_MAX 0x7FFFFFFF
  23. int b_memcpy_s(void * s1, unsigned int s1max, const void * s2, unsigned int n)
  24. {
  25. char *dest = (char*) s1;
  26. char *src = (char*) s2;
  27. if (n == 0) {
  28. return 0;
  29. }
  30. if (s1 == NULL || s1max > RSIZE_MAX) {
  31. return -1;
  32. }
  33. if (s2 == NULL || n > s1max) {
  34. memset(dest, 0, s1max);
  35. return -1;
  36. }
  37. memcpy(dest, src, n);
  38. return 0;
  39. }
  40. int b_strcat_s(char * s1, size_t s1max, const char * s2)
  41. {
  42. if (NULL
  43. == s1|| NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1) || s1max > RSIZE_MAX) {
  44. return -1;
  45. }
  46. strcat(s1, s2);
  47. return 0;
  48. }
  49. int b_strcpy_s(char * s1, size_t s1max, const char * s2)
  50. {
  51. if (NULL
  52. == s1|| NULL == s2 || s1max < (strlen(s2) + 1) || s1max > RSIZE_MAX) {
  53. return -1;
  54. }
  55. strcpy(s1, s2);
  56. return 0;
  57. }
  58. int fopen_s(FILE ** pFile, const char *filename, const char *mode)
  59. {
  60. if (NULL == pFile || NULL == filename || NULL == mode) {
  61. return -1;
  62. }
  63. *pFile = fopen(filename, mode);
  64. if (NULL == *pFile)
  65. return -1;
  66. return 0;
  67. }