PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-1.0.1/source/hbpcre/pcretryf.c

#
C | 137 lines | 45 code | 19 blank | 73 comment | 6 complexity | 7b1256f06c8120cae7c35d0274ec8943 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  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-2008 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. #if 2875
  36. #include "_hbconf.h"
  37. #endif
  38. #include "pcreinal.h"
  39. /*************************************************
  40. * Flip bytes in an integer *
  41. *************************************************/
  42. /* This function is called when the magic number in a regex doesn't match, in
  43. order to flip its bytes to see if we are dealing with a pattern that was
  44. compiled on a host of different endianness. If so, this function is used to
  45. flip other byte values.
  46. Arguments:
  47. value the number to flip
  48. n the number of bytes to flip (assumed to be 2 or 4)
  49. Returns: the flipped value
  50. */
  51. static unsigned long int
  52. byteflip(unsigned long int value, int n)
  53. {
  54. if (n == 2) return ((value & 0x00ff) << 8) | ((value & 0xff00) >> 8);
  55. return ((value & 0x000000ff) << 24) |
  56. ((value & 0x0000ff00) << 8) |
  57. ((value & 0x00ff0000) >> 8) |
  58. ((value & 0xff000000) >> 24);
  59. }
  60. /*************************************************
  61. * Test for a byte-flipped compiled regex *
  62. *************************************************/
  63. /* This function is called from pcre_exec(), pcre_dfa_exec(), and also from
  64. pcre_fullinfo(). Its job is to test whether the regex is byte-flipped - that
  65. is, it was compiled on a system of opposite endianness. The function is called
  66. only when the native MAGIC_NUMBER test fails. If the regex is indeed flipped,
  67. we flip all the relevant values into a different data block, and return it.
  68. Arguments:
  69. re points to the regex
  70. study points to study data, or NULL
  71. internal_re points to a new regex block
  72. internal_study points to a new study block
  73. Returns: the new block if is is indeed a byte-flipped regex
  74. NULL if it is not
  75. */
  76. real_pcre *
  77. _pcre_try_flipped(const real_pcre *re, real_pcre *internal_re,
  78. const pcre_study_data *study, pcre_study_data *internal_study)
  79. {
  80. if (byteflip(re->magic_number, sizeof(re->magic_number)) != MAGIC_NUMBER)
  81. return NULL;
  82. *internal_re = *re; /* To copy other fields */
  83. internal_re->size = byteflip(re->size, sizeof(re->size));
  84. internal_re->options = byteflip(re->options, sizeof(re->options));
  85. internal_re->flags = (pcre_uint16)byteflip(re->flags, sizeof(re->flags));
  86. internal_re->top_bracket =
  87. (pcre_uint16)byteflip(re->top_bracket, sizeof(re->top_bracket));
  88. internal_re->top_backref =
  89. (pcre_uint16)byteflip(re->top_backref, sizeof(re->top_backref));
  90. internal_re->first_byte =
  91. (pcre_uint16)byteflip(re->first_byte, sizeof(re->first_byte));
  92. internal_re->req_byte =
  93. (pcre_uint16)byteflip(re->req_byte, sizeof(re->req_byte));
  94. internal_re->name_table_offset =
  95. (pcre_uint16)byteflip(re->name_table_offset, sizeof(re->name_table_offset));
  96. internal_re->name_entry_size =
  97. (pcre_uint16)byteflip(re->name_entry_size, sizeof(re->name_entry_size));
  98. internal_re->name_count =
  99. (pcre_uint16)byteflip(re->name_count, sizeof(re->name_count));
  100. if (study != NULL)
  101. {
  102. *internal_study = *study; /* To copy other fields */
  103. internal_study->size = byteflip(study->size, sizeof(study->size));
  104. internal_study->options = byteflip(study->options, sizeof(study->options));
  105. }
  106. return internal_re;
  107. }
  108. /* End of pcre_tryflipped.c */