bh_common.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "bh_common.h"
  6. static char *
  7. align_ptr(char *src, unsigned int b)
  8. {
  9. uintptr_t v = (uintptr_t)src;
  10. uintptr_t m = b - 1;
  11. return (char *)((v + m) & ~m);
  12. }
  13. /*
  14. Memory copy, with word alignment
  15. */
  16. int
  17. b_memcpy_wa(void *s1, unsigned int s1max, const void *s2, unsigned int n)
  18. {
  19. char *dest = (char *)s1;
  20. char *src = (char *)s2;
  21. char *pa = align_ptr(src, 4);
  22. char *pb = align_ptr((src + n), 4);
  23. unsigned int buff;
  24. const char *p_byte_read;
  25. unsigned int *p;
  26. char *ps;
  27. if (pa > src) {
  28. pa -= 4;
  29. }
  30. for (p = (unsigned int *)pa; p < (unsigned int *)pb; p++) {
  31. buff = *(p);
  32. p_byte_read = ((char *)&buff);
  33. /* read leading word */
  34. if ((char *)p <= src) {
  35. for (ps = src; ps < ((char *)p + 4); ps++) {
  36. if (ps >= src + n) {
  37. break;
  38. }
  39. p_byte_read = ((char *)&buff) + (ps - (char *)p);
  40. *dest++ = *p_byte_read;
  41. }
  42. }
  43. /* read trailing word */
  44. else if ((char *)p >= pb - 4) {
  45. for (ps = (char *)p; ps < src + n; ps++) {
  46. *dest++ = *p_byte_read++;
  47. }
  48. }
  49. /* read meaning word(s) */
  50. else {
  51. if ((char *)p + 4 >= src + n) {
  52. for (ps = (char *)p; ps < src + n; ps++) {
  53. *dest++ = *p_byte_read++;
  54. }
  55. }
  56. else {
  57. *(unsigned int *)dest = buff;
  58. dest += 4;
  59. }
  60. }
  61. }
  62. return 0;
  63. }
  64. int
  65. b_memcpy_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
  66. {
  67. char *dest = (char *)s1;
  68. char *src = (char *)s2;
  69. if (n == 0) {
  70. return 0;
  71. }
  72. if (s1 == NULL) {
  73. return -1;
  74. }
  75. if (s2 == NULL || n > s1max) {
  76. memset(dest, 0, s1max);
  77. return -1;
  78. }
  79. memcpy(dest, src, n);
  80. return 0;
  81. }
  82. int
  83. b_memmove_s(void *s1, unsigned int s1max, const void *s2, unsigned int n)
  84. {
  85. char *dest = (char *)s1;
  86. char *src = (char *)s2;
  87. if (n == 0) {
  88. return 0;
  89. }
  90. if (s1 == NULL) {
  91. return -1;
  92. }
  93. if (s2 == NULL || n > s1max) {
  94. memset(dest, 0, s1max);
  95. return -1;
  96. }
  97. memmove(dest, src, n);
  98. return 0;
  99. }
  100. int
  101. b_strcat_s(char *s1, unsigned int s1max, const char *s2)
  102. {
  103. if (NULL == s1 || NULL == s2 || s1max < (strlen(s1) + strlen(s2) + 1)) {
  104. return -1;
  105. }
  106. memcpy(s1 + strlen(s1), s2, strlen(s2) + 1);
  107. return 0;
  108. }
  109. int
  110. b_strcpy_s(char *s1, unsigned int s1max, const char *s2)
  111. {
  112. if (NULL == s1 || NULL == s2 || s1max < (strlen(s2) + 1)) {
  113. return -1;
  114. }
  115. memcpy(s1, s2, strlen(s2) + 1);
  116. return 0;
  117. }
  118. char *
  119. bh_strdup(const char *s)
  120. {
  121. uint32 size;
  122. char *s1 = NULL;
  123. if (s) {
  124. size = (uint32)(strlen(s) + 1);
  125. if ((s1 = BH_MALLOC(size)))
  126. bh_memcpy_s(s1, size, s, size);
  127. }
  128. return s1;
  129. }
  130. char *
  131. wa_strdup(const char *s)
  132. {
  133. uint32 size;
  134. char *s1 = NULL;
  135. if (s) {
  136. size = (uint32)(strlen(s) + 1);
  137. if ((s1 = WA_MALLOC(size)))
  138. bh_memcpy_s(s1, size, s, size);
  139. }
  140. return s1;
  141. }