/ToolsResources/REVERSE/Tools/yara/libyara/strutils.c

https://github.com/SofianeHamlaoui/Lockdoor-Framework · C · 241 lines · 166 code · 50 blank · 25 comment · 54 complexity · 9f7e0520025a07f60f09b4b25ffb518b MD5 · raw file

  1. /*
  2. Copyright (c) 2007-2014. The YARA Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <yara/strutils.h>
  16. #include "config.h"
  17. uint64_t xtoi(
  18. const char* hexstr)
  19. {
  20. size_t i;
  21. size_t l = strlen(hexstr);
  22. uint64_t r = 0;
  23. for (i = 0; i < l; i++)
  24. {
  25. switch (hexstr[i])
  26. {
  27. case '0':
  28. case '1':
  29. case '2':
  30. case '3':
  31. case '4':
  32. case '5':
  33. case '6':
  34. case '7':
  35. case '8':
  36. case '9':
  37. r |= ((uint64_t)(hexstr[i] - '0')) << ((l - i - 1) * 4);
  38. break;
  39. case 'a':
  40. case 'b':
  41. case 'c':
  42. case 'd':
  43. case 'e':
  44. case 'f':
  45. r |= ((uint64_t)(hexstr[i] - 'a' + 10)) << ((l - i - 1) * 4);
  46. break;
  47. case 'A':
  48. case 'B':
  49. case 'C':
  50. case 'D':
  51. case 'E':
  52. case 'F':
  53. r |= ((uint64_t)(hexstr[i] - 'A' + 10)) << ((l - i - 1) * 4);
  54. break;
  55. default:
  56. i = l; // force loop exit
  57. }
  58. }
  59. return r;
  60. }
  61. /*
  62. strlcpy and strlcat are defined in FreeBSD and OpenBSD,
  63. the following implementations were taken from OpenBSD.
  64. */
  65. #if !HAVE_STRLCPY && !defined(strlcpy)
  66. size_t strlcpy(
  67. char* dst,
  68. const char* src,
  69. size_t size)
  70. {
  71. register char* d = dst;
  72. register const char* s = src;
  73. register size_t n = size;
  74. // Copy as many bytes as will fit
  75. if (n != 0 && --n != 0)
  76. {
  77. do
  78. {
  79. if ((*d++ = *s++) == 0)
  80. break;
  81. } while (--n != 0);
  82. }
  83. // Not enough room in dst, add NUL and traverse rest of src
  84. if (n == 0)
  85. {
  86. if (size != 0)
  87. *d = '\0'; // NULL-terminate dst
  88. while (*s++);
  89. }
  90. return (s - src - 1); // count does not include NULL
  91. }
  92. #endif
  93. #if !HAVE_STRLCAT && !defined(strlcat)
  94. size_t strlcat(
  95. char* dst,
  96. const char* src,
  97. size_t size)
  98. {
  99. register char* d = dst;
  100. register const char* s = src;
  101. register size_t n = size;
  102. size_t dlen;
  103. // Find the end of dst and adjust bytes left but don't go past end
  104. while (n-- != 0 && *d != '\0') d++;
  105. dlen = d - dst;
  106. n = size - dlen;
  107. if (n == 0)
  108. return(dlen + strlen(s));
  109. while (*s != '\0')
  110. {
  111. if (n != 1)
  112. {
  113. *d++ = *s;
  114. n--;
  115. }
  116. s++;
  117. }
  118. *d = '\0';
  119. return (dlen + (s - src)); // count does not include NULL
  120. }
  121. #endif
  122. int strnlen_w(
  123. const char* w_str)
  124. {
  125. int len = 0;
  126. while (w_str[0] || w_str[1])
  127. {
  128. w_str += 2;
  129. len += 1;
  130. }
  131. return len;
  132. }
  133. int strcmp_w(
  134. const char* w_str,
  135. const char* str)
  136. {
  137. while (*str != 0 && w_str[0] == *str && w_str[1] == 0)
  138. {
  139. w_str += 2;
  140. str += 1;
  141. }
  142. // Higher-order byte of wide char non-zero? -> w_str is larger than str
  143. if (w_str[1] != 0)
  144. return 1;
  145. return w_str[0] - *str;
  146. }
  147. size_t strlcpy_w(
  148. char* dst,
  149. const char* w_src,
  150. size_t n)
  151. {
  152. register char* d = dst;
  153. register const char* s = w_src;
  154. while (n > 1 && *s != 0)
  155. {
  156. *d = *s;
  157. d += 1;
  158. n -= 1;
  159. s += 2;
  160. }
  161. while (*s) s += 2;
  162. *d = '\0';
  163. return (s - w_src) / 2;
  164. }
  165. #if !HAVE_MEMMEM && !defined(memmem)
  166. void* memmem(
  167. const void *haystack,
  168. size_t haystack_size,
  169. const void *needle,
  170. size_t needle_size)
  171. {
  172. char *sp = (char *) haystack;
  173. char *pp = (char *) needle;
  174. char *eos = sp + haystack_size - needle_size;
  175. if (haystack == NULL || haystack_size == 0 ||
  176. needle == NULL || needle_size == 0)
  177. return NULL;
  178. while (sp <= eos)
  179. {
  180. if (*sp == *pp && memcmp(sp, pp, needle_size) == 0)
  181. return sp;
  182. sp++;
  183. }
  184. return NULL;
  185. }
  186. #endif