pcre2_string_utils.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*************************************************
  2. * Perl-Compatible Regular Expressions *
  3. *************************************************/
  4. /* PCRE is a library of functions to support regular expressions whose syntax
  5. and semantics are as close as possible to those of the Perl 5 language.
  6. Written by Philip Hazel
  7. Original API code Copyright (c) 1997-2012 University of Cambridge
  8. New API code Copyright (c) 2018 University of Cambridge
  9. -----------------------------------------------------------------------------
  10. Redistribution and use in source and binary forms, with or without
  11. modification, are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. * Neither the name of the University of Cambridge nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. POSSIBILITY OF SUCH DAMAGE.
  31. -----------------------------------------------------------------------------
  32. */
  33. /* This module contains internal functions for comparing and finding the length
  34. of strings. These are used instead of strcmp() etc because the standard
  35. functions work only on 8-bit data. */
  36. #ifdef HAVE_CONFIG_H
  37. #include "config.h"
  38. #endif
  39. #include "pcre2_internal.h"
  40. /*************************************************
  41. * Emulated memmove() for systems without it *
  42. *************************************************/
  43. /* This function can make use of bcopy() if it is available. Otherwise do it by
  44. steam, as there some non-Unix environments that lack both memmove() and
  45. bcopy(). */
  46. #if !defined(VPCOMPAT) && !defined(HAVE_MEMMOVE)
  47. void *
  48. PRIV(memmove)(void *d, const void *s, size_t n)
  49. {
  50. #ifdef HAVE_BCOPY
  51. bcopy(s, d, n);
  52. return d;
  53. #else
  54. size_t i;
  55. unsigned char *dest = (unsigned char *)d;
  56. const unsigned char *src = (const unsigned char *)s;
  57. if (dest > src)
  58. {
  59. dest += n;
  60. src += n;
  61. for (i = 0; i < n; ++i) *(--dest) = *(--src);
  62. return (void *)dest;
  63. }
  64. else
  65. {
  66. for (i = 0; i < n; ++i) *dest++ = *src++;
  67. return (void *)(dest - n);
  68. }
  69. #endif /* not HAVE_BCOPY */
  70. }
  71. #endif /* not VPCOMPAT && not HAVE_MEMMOVE */
  72. /*************************************************
  73. * Compare two zero-terminated PCRE2 strings *
  74. *************************************************/
  75. /*
  76. Arguments:
  77. str1 first string
  78. str2 second string
  79. Returns: 0, 1, or -1
  80. */
  81. int
  82. PRIV(strcmp)(PCRE2_SPTR str1, PCRE2_SPTR str2)
  83. {
  84. PCRE2_UCHAR c1, c2;
  85. while (*str1 != '\0' || *str2 != '\0')
  86. {
  87. c1 = *str1++;
  88. c2 = *str2++;
  89. if (c1 != c2) return ((c1 > c2) << 1) - 1;
  90. }
  91. return 0;
  92. }
  93. /*************************************************
  94. * Compare zero-terminated PCRE2 & 8-bit strings *
  95. *************************************************/
  96. /* As the 8-bit string is almost always a literal, its type is specified as
  97. const char *.
  98. Arguments:
  99. str1 first string
  100. str2 second string
  101. Returns: 0, 1, or -1
  102. */
  103. int
  104. PRIV(strcmp_c8)(PCRE2_SPTR str1, const char *str2)
  105. {
  106. PCRE2_UCHAR c1, c2;
  107. while (*str1 != '\0' || *str2 != '\0')
  108. {
  109. c1 = *str1++;
  110. c2 = *str2++;
  111. if (c1 != c2) return ((c1 > c2) << 1) - 1;
  112. }
  113. return 0;
  114. }
  115. /*************************************************
  116. * Compare two PCRE2 strings, given a length *
  117. *************************************************/
  118. /*
  119. Arguments:
  120. str1 first string
  121. str2 second string
  122. len the length
  123. Returns: 0, 1, or -1
  124. */
  125. int
  126. PRIV(strncmp)(PCRE2_SPTR str1, PCRE2_SPTR str2, size_t len)
  127. {
  128. PCRE2_UCHAR c1, c2;
  129. for (; len > 0; len--)
  130. {
  131. c1 = *str1++;
  132. c2 = *str2++;
  133. if (c1 != c2) return ((c1 > c2) << 1) - 1;
  134. }
  135. return 0;
  136. }
  137. /*************************************************
  138. * Compare PCRE2 string to 8-bit string by length *
  139. *************************************************/
  140. /* As the 8-bit string is almost always a literal, its type is specified as
  141. const char *.
  142. Arguments:
  143. str1 first string
  144. str2 second string
  145. len the length
  146. Returns: 0, 1, or -1
  147. */
  148. int
  149. PRIV(strncmp_c8)(PCRE2_SPTR str1, const char *str2, size_t len)
  150. {
  151. PCRE2_UCHAR c1, c2;
  152. for (; len > 0; len--)
  153. {
  154. c1 = *str1++;
  155. c2 = *str2++;
  156. if (c1 != c2) return ((c1 > c2) << 1) - 1;
  157. }
  158. return 0;
  159. }
  160. /*************************************************
  161. * Find the length of a PCRE2 string *
  162. *************************************************/
  163. /*
  164. Argument: the string
  165. Returns: the length
  166. */
  167. PCRE2_SIZE
  168. PRIV(strlen)(PCRE2_SPTR str)
  169. {
  170. PCRE2_SIZE c = 0;
  171. while (*str++ != 0) c++;
  172. return c;
  173. }
  174. /*************************************************
  175. * Copy 8-bit 0-terminated string to PCRE2 string *
  176. *************************************************/
  177. /* Arguments:
  178. str1 buffer to receive the string
  179. str2 8-bit string to be copied
  180. Returns: the number of code units used (excluding trailing zero)
  181. */
  182. PCRE2_SIZE
  183. PRIV(strcpy_c8)(PCRE2_UCHAR *str1, const char *str2)
  184. {
  185. PCRE2_UCHAR *t = str1;
  186. while (*str2 != 0) *t++ = *str2++;
  187. *t = 0;
  188. return t - str1;
  189. }
  190. /* End of pcre2_string_utils.c */