/libyara/strutils.c

https://github.com/lucamassarelli/yarasafe · C · 252 lines · 165 code · 49 blank · 38 comment · 54 complexity · c21eb84bbf9f43ce760b615dd649c4d8 MD5 · raw file

  1. /*
  2. Copyright (c) 2007-2014. The YARA Authors. All Rights Reserved.
  3. Redistribution and use in source and binary forms, with or without modification,
  4. are permitted provided that the following conditions are met:
  5. 1. Redistributions of source code must retain the above copyright notice, this
  6. list of conditions and the following disclaimer.
  7. 2. Redistributions in binary form must reproduce the above copyright notice,
  8. this list of conditions and the following disclaimer in the documentation and/or
  9. other materials provided with the distribution.
  10. 3. Neither the name of the copyright holder nor the names of its contributors
  11. may be used to endorse or promote products derived from this software without
  12. specific prior written permission.
  13. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  14. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  17. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  18. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  19. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  20. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  21. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  22. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  23. */
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <yara/strutils.h>
  27. uint64_t xtoi(
  28. const char* hexstr)
  29. {
  30. size_t i;
  31. size_t l = strlen(hexstr);
  32. uint64_t r = 0;
  33. for (i = 0; i < l; i++)
  34. {
  35. switch (hexstr[i])
  36. {
  37. case '0':
  38. case '1':
  39. case '2':
  40. case '3':
  41. case '4':
  42. case '5':
  43. case '6':
  44. case '7':
  45. case '8':
  46. case '9':
  47. r |= ((uint64_t)(hexstr[i] - '0')) << ((l - i - 1) * 4);
  48. break;
  49. case 'a':
  50. case 'b':
  51. case 'c':
  52. case 'd':
  53. case 'e':
  54. case 'f':
  55. r |= ((uint64_t)(hexstr[i] - 'a' + 10)) << ((l - i - 1) * 4);
  56. break;
  57. case 'A':
  58. case 'B':
  59. case 'C':
  60. case 'D':
  61. case 'E':
  62. case 'F':
  63. r |= ((uint64_t)(hexstr[i] - 'A' + 10)) << ((l - i - 1) * 4);
  64. break;
  65. default:
  66. i = l; // force loop exit
  67. }
  68. }
  69. return r;
  70. }
  71. /*
  72. strlcpy and strlcat are defined in FreeBSD and OpenBSD,
  73. the following implementations were taken from OpenBSD.
  74. */
  75. #if !HAVE_STRLCPY && !defined(strlcpy)
  76. size_t strlcpy(
  77. char* dst,
  78. const char* src,
  79. size_t size)
  80. {
  81. register char* d = dst;
  82. register const char* s = src;
  83. register size_t n = size;
  84. // Copy as many bytes as will fit
  85. if (n != 0 && --n != 0)
  86. {
  87. do
  88. {
  89. if ((*d++ = *s++) == 0)
  90. break;
  91. } while (--n != 0);
  92. }
  93. // Not enough room in dst, add NUL and traverse rest of src
  94. if (n == 0)
  95. {
  96. if (size != 0)
  97. *d = '\0'; // NULL-terminate dst
  98. while (*s++);
  99. }
  100. return (s - src - 1); // count does not include NULL
  101. }
  102. #endif
  103. #if !HAVE_STRLCAT && !defined(strlcat)
  104. size_t strlcat(
  105. char* dst,
  106. const char* src,
  107. size_t size)
  108. {
  109. register char* d = dst;
  110. register const char* s = src;
  111. register size_t n = size;
  112. size_t dlen;
  113. // Find the end of dst and adjust bytes left but don't go past end
  114. while (n-- != 0 && *d != '\0') d++;
  115. dlen = d - dst;
  116. n = size - dlen;
  117. if (n == 0)
  118. return(dlen + strlen(s));
  119. while (*s != '\0')
  120. {
  121. if (n != 1)
  122. {
  123. *d++ = *s;
  124. n--;
  125. }
  126. s++;
  127. }
  128. *d = '\0';
  129. return (dlen + (s - src)); // count does not include NULL
  130. }
  131. #endif
  132. int strnlen_w(
  133. const char* w_str)
  134. {
  135. int len = 0;
  136. while (w_str[0] || w_str[1])
  137. {
  138. w_str += 2;
  139. len += 1;
  140. }
  141. return len;
  142. }
  143. int strcmp_w(
  144. const char* w_str,
  145. const char* str)
  146. {
  147. while (*str != 0 && w_str[0] == *str && w_str[1] == 0)
  148. {
  149. w_str += 2;
  150. str += 1;
  151. }
  152. // Higher-order byte of wide char non-zero? -> w_str is larger than str
  153. if (w_str[1] != 0)
  154. return 1;
  155. return w_str[0] - *str;
  156. }
  157. size_t strlcpy_w(
  158. char* dst,
  159. const char* w_src,
  160. size_t n)
  161. {
  162. register char* d = dst;
  163. register const char* s = w_src;
  164. while (n > 1 && *s != 0)
  165. {
  166. *d = *s;
  167. d += 1;
  168. n -= 1;
  169. s += 2;
  170. }
  171. while (*s) s += 2;
  172. *d = '\0';
  173. return (s - w_src) / 2;
  174. }
  175. #if !HAVE_MEMMEM && !defined(memmem)
  176. void* memmem(
  177. const void *haystack,
  178. size_t haystack_size,
  179. const void *needle,
  180. size_t needle_size)
  181. {
  182. char *sp = (char *) haystack;
  183. char *pp = (char *) needle;
  184. char *eos = sp + haystack_size - needle_size;
  185. if (haystack == NULL || haystack_size == 0 ||
  186. needle == NULL || needle_size == 0)
  187. return NULL;
  188. while (sp <= eos)
  189. {
  190. if (*sp == *pp && memcmp(sp, pp, needle_size) == 0)
  191. return sp;
  192. sp++;
  193. }
  194. return NULL;
  195. }
  196. #endif