pcre_byte_order.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. Copyright (c) 1997-2014 University of Cambridge
  8. -----------------------------------------------------------------------------
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions are met:
  11. * Redistributions of source code must retain the above copyright notice,
  12. this list of conditions and the following disclaimer.
  13. * Redistributions in binary form must reproduce the above copyright
  14. notice, this list of conditions and the following disclaimer in the
  15. documentation and/or other materials provided with the distribution.
  16. * Neither the name of the University of Cambridge nor the names of its
  17. contributors may be used to endorse or promote products derived from
  18. this software without specific prior written permission.
  19. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  23. LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  24. CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  25. SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  28. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  29. POSSIBILITY OF SUCH DAMAGE.
  30. -----------------------------------------------------------------------------
  31. */
  32. /* This module contains an internal function that tests a compiled pattern to
  33. see if it was compiled with the opposite endianness. If so, it uses an
  34. auxiliary local function to flip the appropriate bytes. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "pcre_config.h"
  37. #endif
  38. #include "pcre_internal.h"
  39. /*************************************************
  40. * Swap byte functions *
  41. *************************************************/
  42. /* The following functions swap the bytes of a pcre_uint16
  43. and pcre_uint32 value.
  44. Arguments:
  45. value any number
  46. Returns: the byte swapped value
  47. */
  48. static pcre_uint32
  49. swap_uint32(pcre_uint32 value)
  50. {
  51. return ((value & 0x000000ff) << 24) |
  52. ((value & 0x0000ff00) << 8) |
  53. ((value & 0x00ff0000) >> 8) |
  54. (value >> 24);
  55. }
  56. static pcre_uint16
  57. swap_uint16(pcre_uint16 value)
  58. {
  59. return (value >> 8) | (value << 8);
  60. }
  61. /*************************************************
  62. * Test for a byte-flipped compiled regex *
  63. *************************************************/
  64. /* This function swaps the bytes of a compiled pattern usually
  65. loaded form the disk. It also sets the tables pointer, which
  66. is likely an invalid pointer after reload.
  67. Arguments:
  68. argument_re points to the compiled expression
  69. extra_data points to extra data or is NULL
  70. tables points to the character tables or NULL
  71. Returns: 0 if the swap is successful, negative on error
  72. */
  73. #if defined COMPILE_PCRE8
  74. PCRE_EXP_DECL int pcre_pattern_to_host_byte_order(pcre *argument_re,
  75. pcre_extra *extra_data, const unsigned char *tables)
  76. #elif defined COMPILE_PCRE16
  77. PCRE_EXP_DECL int pcre16_pattern_to_host_byte_order(pcre16 *argument_re,
  78. pcre16_extra *extra_data, const unsigned char *tables)
  79. #elif defined COMPILE_PCRE32
  80. PCRE_EXP_DECL int pcre32_pattern_to_host_byte_order(pcre32 *argument_re,
  81. pcre32_extra *extra_data, const unsigned char *tables)
  82. #endif
  83. {
  84. REAL_PCRE *re = (REAL_PCRE *)argument_re;
  85. pcre_study_data *study;
  86. #ifndef COMPILE_PCRE8
  87. pcre_uchar *ptr;
  88. int length;
  89. #if defined SUPPORT_UTF && defined COMPILE_PCRE16
  90. BOOL utf;
  91. BOOL utf16_char;
  92. #endif /* SUPPORT_UTF && COMPILE_PCRE16 */
  93. #endif /* !COMPILE_PCRE8 */
  94. if (re == NULL) return PCRE_ERROR_NULL;
  95. if (re->magic_number == MAGIC_NUMBER)
  96. {
  97. if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
  98. re->tables = tables;
  99. return 0;
  100. }
  101. if (re->magic_number != REVERSED_MAGIC_NUMBER) return PCRE_ERROR_BADMAGIC;
  102. if ((swap_uint32(re->flags) & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
  103. re->magic_number = MAGIC_NUMBER;
  104. re->size = swap_uint32(re->size);
  105. re->options = swap_uint32(re->options);
  106. re->flags = swap_uint32(re->flags);
  107. re->limit_match = swap_uint32(re->limit_match);
  108. re->limit_recursion = swap_uint32(re->limit_recursion);
  109. #if defined COMPILE_PCRE8 || defined COMPILE_PCRE16
  110. re->first_char = swap_uint16(re->first_char);
  111. re->req_char = swap_uint16(re->req_char);
  112. #elif defined COMPILE_PCRE32
  113. re->first_char = swap_uint32(re->first_char);
  114. re->req_char = swap_uint32(re->req_char);
  115. #endif
  116. re->max_lookbehind = swap_uint16(re->max_lookbehind);
  117. re->top_bracket = swap_uint16(re->top_bracket);
  118. re->top_backref = swap_uint16(re->top_backref);
  119. re->name_table_offset = swap_uint16(re->name_table_offset);
  120. re->name_entry_size = swap_uint16(re->name_entry_size);
  121. re->name_count = swap_uint16(re->name_count);
  122. re->ref_count = swap_uint16(re->ref_count);
  123. re->tables = tables;
  124. if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_STUDY_DATA) != 0)
  125. {
  126. study = (pcre_study_data *)extra_data->study_data;
  127. study->size = swap_uint32(study->size);
  128. study->flags = swap_uint32(study->flags);
  129. study->minlength = swap_uint32(study->minlength);
  130. }
  131. #ifndef COMPILE_PCRE8
  132. ptr = (pcre_uchar *)re + re->name_table_offset;
  133. length = re->name_count * re->name_entry_size;
  134. #if defined SUPPORT_UTF && defined COMPILE_PCRE16
  135. utf = (re->options & PCRE_UTF16) != 0;
  136. utf16_char = FALSE;
  137. #endif /* SUPPORT_UTF && COMPILE_PCRE16 */
  138. while(TRUE)
  139. {
  140. /* Swap previous characters. */
  141. while (length-- > 0)
  142. {
  143. #if defined COMPILE_PCRE16
  144. *ptr = swap_uint16(*ptr);
  145. #elif defined COMPILE_PCRE32
  146. *ptr = swap_uint32(*ptr);
  147. #endif
  148. ptr++;
  149. }
  150. #if defined SUPPORT_UTF && defined COMPILE_PCRE16
  151. if (utf16_char)
  152. {
  153. if (HAS_EXTRALEN(ptr[-1]))
  154. {
  155. /* We know that there is only one extra character in UTF-16. */
  156. *ptr = swap_uint16(*ptr);
  157. ptr++;
  158. }
  159. }
  160. utf16_char = FALSE;
  161. #endif /* SUPPORT_UTF */
  162. /* Get next opcode. */
  163. length = 0;
  164. #if defined COMPILE_PCRE16
  165. *ptr = swap_uint16(*ptr);
  166. #elif defined COMPILE_PCRE32
  167. *ptr = swap_uint32(*ptr);
  168. #endif
  169. switch (*ptr)
  170. {
  171. case OP_END:
  172. return 0;
  173. #if defined SUPPORT_UTF && defined COMPILE_PCRE16
  174. case OP_CHAR:
  175. case OP_CHARI:
  176. case OP_NOT:
  177. case OP_NOTI:
  178. case OP_STAR:
  179. case OP_MINSTAR:
  180. case OP_PLUS:
  181. case OP_MINPLUS:
  182. case OP_QUERY:
  183. case OP_MINQUERY:
  184. case OP_UPTO:
  185. case OP_MINUPTO:
  186. case OP_EXACT:
  187. case OP_POSSTAR:
  188. case OP_POSPLUS:
  189. case OP_POSQUERY:
  190. case OP_POSUPTO:
  191. case OP_STARI:
  192. case OP_MINSTARI:
  193. case OP_PLUSI:
  194. case OP_MINPLUSI:
  195. case OP_QUERYI:
  196. case OP_MINQUERYI:
  197. case OP_UPTOI:
  198. case OP_MINUPTOI:
  199. case OP_EXACTI:
  200. case OP_POSSTARI:
  201. case OP_POSPLUSI:
  202. case OP_POSQUERYI:
  203. case OP_POSUPTOI:
  204. case OP_NOTSTAR:
  205. case OP_NOTMINSTAR:
  206. case OP_NOTPLUS:
  207. case OP_NOTMINPLUS:
  208. case OP_NOTQUERY:
  209. case OP_NOTMINQUERY:
  210. case OP_NOTUPTO:
  211. case OP_NOTMINUPTO:
  212. case OP_NOTEXACT:
  213. case OP_NOTPOSSTAR:
  214. case OP_NOTPOSPLUS:
  215. case OP_NOTPOSQUERY:
  216. case OP_NOTPOSUPTO:
  217. case OP_NOTSTARI:
  218. case OP_NOTMINSTARI:
  219. case OP_NOTPLUSI:
  220. case OP_NOTMINPLUSI:
  221. case OP_NOTQUERYI:
  222. case OP_NOTMINQUERYI:
  223. case OP_NOTUPTOI:
  224. case OP_NOTMINUPTOI:
  225. case OP_NOTEXACTI:
  226. case OP_NOTPOSSTARI:
  227. case OP_NOTPOSPLUSI:
  228. case OP_NOTPOSQUERYI:
  229. case OP_NOTPOSUPTOI:
  230. if (utf) utf16_char = TRUE;
  231. #endif
  232. /* Fall through. */
  233. default:
  234. length = PRIV(OP_lengths)[*ptr] - 1;
  235. break;
  236. case OP_CLASS:
  237. case OP_NCLASS:
  238. /* Skip the character bit map. */
  239. ptr += 32/sizeof(pcre_uchar);
  240. length = 0;
  241. break;
  242. case OP_XCLASS:
  243. /* Reverse the size of the XCLASS instance. */
  244. ptr++;
  245. #if defined COMPILE_PCRE16
  246. *ptr = swap_uint16(*ptr);
  247. #elif defined COMPILE_PCRE32
  248. *ptr = swap_uint32(*ptr);
  249. #endif
  250. #ifndef COMPILE_PCRE32
  251. if (LINK_SIZE > 1)
  252. {
  253. /* LINK_SIZE can be 1 or 2 in 16 bit mode. */
  254. ptr++;
  255. *ptr = swap_uint16(*ptr);
  256. }
  257. #endif
  258. ptr++;
  259. length = (GET(ptr, -LINK_SIZE)) - (1 + LINK_SIZE + 1);
  260. #if defined COMPILE_PCRE16
  261. *ptr = swap_uint16(*ptr);
  262. #elif defined COMPILE_PCRE32
  263. *ptr = swap_uint32(*ptr);
  264. #endif
  265. if ((*ptr & XCL_MAP) != 0)
  266. {
  267. /* Skip the character bit map. */
  268. ptr += 32/sizeof(pcre_uchar);
  269. length -= 32/sizeof(pcre_uchar);
  270. }
  271. break;
  272. }
  273. ptr++;
  274. }
  275. /* Control should never reach here in 16/32 bit mode. */
  276. #else /* In 8-bit mode, the pattern does not need to be processed. */
  277. return 0;
  278. #endif /* !COMPILE_PCRE8 */
  279. }
  280. /* End of pcre_byte_order.c */