pcre_fullinfo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 the external function pcre_fullinfo(), which returns
  33. information about a compiled pattern. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "config.h"
  36. #endif
  37. #include "pcre_internal.h"
  38. /*************************************************
  39. * Return info about compiled pattern *
  40. *************************************************/
  41. /* This is a newer "info" function which has an extensible interface so
  42. that additional items can be added compatibly.
  43. Arguments:
  44. argument_re points to compiled code
  45. extra_data points extra data, or NULL
  46. what what information is required
  47. where where to put the information
  48. Returns: 0 if data returned, negative on error
  49. */
  50. PCRE_EXP_DEFN int
  51. pcre_fullinfo(const pcre *argument_re, const pcre_extra *extra_data, int what,
  52. void *where)
  53. {
  54. real_pcre internal_re;
  55. pcre_study_data internal_study;
  56. const real_pcre *re = (const real_pcre *)argument_re;
  57. const pcre_study_data *study = NULL;
  58. if (re == NULL || where == NULL) return PCRE_ERROR_NULL;
  59. if (extra_data != NULL && (extra_data->flags & PCRE_EXTRA_STUDY_DATA) != 0)
  60. study = (const pcre_study_data *)extra_data->study_data;
  61. if (re->magic_number != MAGIC_NUMBER)
  62. {
  63. re = _pcre_try_flipped(re, &internal_re, study, &internal_study);
  64. if (re == NULL) return PCRE_ERROR_BADMAGIC;
  65. if (study != NULL) study = &internal_study;
  66. }
  67. switch (what)
  68. {
  69. case PCRE_INFO_OPTIONS:
  70. *((unsigned long int *)where) = re->options & PUBLIC_OPTIONS;
  71. break;
  72. case PCRE_INFO_SIZE:
  73. *((size_t *)where) = re->size;
  74. break;
  75. case PCRE_INFO_STUDYSIZE:
  76. *((size_t *)where) = (study == NULL)? 0 : study->size;
  77. break;
  78. case PCRE_INFO_CAPTURECOUNT:
  79. *((int *)where) = re->top_bracket;
  80. break;
  81. case PCRE_INFO_BACKREFMAX:
  82. *((int *)where) = re->top_backref;
  83. break;
  84. case PCRE_INFO_FIRSTBYTE:
  85. *((int *)where) =
  86. ((re->flags & PCRE_FIRSTSET) != 0)? re->first_byte :
  87. ((re->flags & PCRE_STARTLINE) != 0)? -1 : -2;
  88. break;
  89. /* Make sure we pass back the pointer to the bit vector in the external
  90. block, not the internal copy (with flipped integer fields). */
  91. case PCRE_INFO_FIRSTTABLE:
  92. *((const uschar **)where) =
  93. (study != NULL && (study->options & PCRE_STUDY_MAPPED) != 0)?
  94. ((const pcre_study_data *)extra_data->study_data)->start_bits : NULL;
  95. break;
  96. case PCRE_INFO_LASTLITERAL:
  97. *((int *)where) =
  98. ((re->flags & PCRE_REQCHSET) != 0)? re->req_byte : -1;
  99. break;
  100. case PCRE_INFO_NAMEENTRYSIZE:
  101. *((int *)where) = re->name_entry_size;
  102. break;
  103. case PCRE_INFO_NAMECOUNT:
  104. *((int *)where) = re->name_count;
  105. break;
  106. case PCRE_INFO_NAMETABLE:
  107. *((const uschar **)where) = (const uschar *)re + re->name_table_offset;
  108. break;
  109. case PCRE_INFO_DEFAULT_TABLES:
  110. *((const uschar **)where) = (const uschar *)(_pcre_default_tables);
  111. break;
  112. case PCRE_INFO_OKPARTIAL:
  113. *((int *)where) = (re->flags & PCRE_NOPARTIAL) == 0;
  114. break;
  115. case PCRE_INFO_JCHANGED:
  116. *((int *)where) = (re->flags & PCRE_JCHANGED) != 0;
  117. break;
  118. case PCRE_INFO_HASCRORLF:
  119. *((int *)where) = (re->flags & PCRE_HASCRORLF) != 0;
  120. break;
  121. default: return PCRE_ERROR_BADOPTION;
  122. }
  123. return 0;
  124. }
  125. /* End of pcre_fullinfo.c */