pcre2_match_data.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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) 2016-2019 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. #ifdef HAVE_CONFIG_H
  34. #include "config.h"
  35. #endif
  36. #include "pcre2_internal.h"
  37. /*************************************************
  38. * Create a match data block given ovector size *
  39. *************************************************/
  40. /* A minimum of 1 is imposed on the number of ovector pairs. */
  41. PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
  42. pcre2_match_data_create(uint32_t oveccount, pcre2_general_context *gcontext)
  43. {
  44. pcre2_match_data *yield;
  45. if (oveccount < 1) oveccount = 1;
  46. yield = PRIV(memctl_malloc)(
  47. offsetof(pcre2_match_data, ovector) + 2*oveccount*sizeof(PCRE2_SIZE),
  48. (pcre2_memctl *)gcontext);
  49. if (yield == NULL) return NULL;
  50. yield->oveccount = oveccount;
  51. yield->flags = 0;
  52. return yield;
  53. }
  54. /*************************************************
  55. * Create a match data block using pattern data *
  56. *************************************************/
  57. /* If no context is supplied, use the memory allocator from the code. */
  58. PCRE2_EXP_DEFN pcre2_match_data * PCRE2_CALL_CONVENTION
  59. pcre2_match_data_create_from_pattern(const pcre2_code *code,
  60. pcre2_general_context *gcontext)
  61. {
  62. if (gcontext == NULL) gcontext = (pcre2_general_context *)code;
  63. return pcre2_match_data_create(((pcre2_real_code *)code)->top_bracket + 1,
  64. gcontext);
  65. }
  66. /*************************************************
  67. * Free a match data block *
  68. *************************************************/
  69. PCRE2_EXP_DEFN void PCRE2_CALL_CONVENTION
  70. pcre2_match_data_free(pcre2_match_data *match_data)
  71. {
  72. if (match_data != NULL)
  73. {
  74. if ((match_data->flags & PCRE2_MD_COPIED_SUBJECT) != 0)
  75. match_data->memctl.free((void *)match_data->subject,
  76. match_data->memctl.memory_data);
  77. match_data->memctl.free(match_data, match_data->memctl.memory_data);
  78. }
  79. }
  80. /*************************************************
  81. * Get last mark in match *
  82. *************************************************/
  83. PCRE2_EXP_DEFN PCRE2_SPTR PCRE2_CALL_CONVENTION
  84. pcre2_get_mark(pcre2_match_data *match_data)
  85. {
  86. return match_data->mark;
  87. }
  88. /*************************************************
  89. * Get pointer to ovector *
  90. *************************************************/
  91. PCRE2_EXP_DEFN PCRE2_SIZE * PCRE2_CALL_CONVENTION
  92. pcre2_get_ovector_pointer(pcre2_match_data *match_data)
  93. {
  94. return match_data->ovector;
  95. }
  96. /*************************************************
  97. * Get number of ovector slots *
  98. *************************************************/
  99. PCRE2_EXP_DEFN uint32_t PCRE2_CALL_CONVENTION
  100. pcre2_get_ovector_count(pcre2_match_data *match_data)
  101. {
  102. return match_data->oveccount;
  103. }
  104. /*************************************************
  105. * Get starting code unit in match *
  106. *************************************************/
  107. PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION
  108. pcre2_get_startchar(pcre2_match_data *match_data)
  109. {
  110. return match_data->startchar;
  111. }
  112. /*************************************************
  113. * Get size of match data block *
  114. *************************************************/
  115. PCRE2_EXP_DEFN PCRE2_SIZE PCRE2_CALL_CONVENTION
  116. pcre2_get_match_data_size(pcre2_match_data *match_data)
  117. {
  118. return offsetof(pcre2_match_data, ovector) +
  119. 2 * (match_data->oveccount) * sizeof(PCRE2_SIZE);
  120. }
  121. /* End of pcre2_match_data.c */