pcre_refcount.c 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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-2012 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_refcount(), which is an
  33. auxiliary function that can be used to maintain a reference count in a compiled
  34. pattern data block. This might be helpful in applications where the block is
  35. shared by different users. */
  36. #ifdef HAVE_CONFIG_H
  37. #include "pcre_config.h"
  38. #endif
  39. #include "pcre_internal.h"
  40. /*************************************************
  41. * Maintain reference count *
  42. *************************************************/
  43. /* The reference count is a 16-bit field, initialized to zero. It is not
  44. possible to transfer a non-zero count from one host to a different host that
  45. has a different byte order - though I can't see why anyone in their right mind
  46. would ever want to do that!
  47. Arguments:
  48. argument_re points to compiled code
  49. adjust value to add to the count
  50. Returns: the (possibly updated) count value (a non-negative number), or
  51. a negative error number
  52. */
  53. #if defined COMPILE_PCRE8
  54. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  55. pcre_refcount(pcre *argument_re, int adjust)
  56. #elif defined COMPILE_PCRE16
  57. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  58. pcre16_refcount(pcre16 *argument_re, int adjust)
  59. #elif defined COMPILE_PCRE32
  60. PCRE_EXP_DEFN int PCRE_CALL_CONVENTION
  61. pcre32_refcount(pcre32 *argument_re, int adjust)
  62. #endif
  63. {
  64. REAL_PCRE *re = (REAL_PCRE *)argument_re;
  65. if (re == NULL) return PCRE_ERROR_NULL;
  66. if (re->magic_number != MAGIC_NUMBER) return PCRE_ERROR_BADMAGIC;
  67. if ((re->flags & PCRE_MODE) == 0) return PCRE_ERROR_BADMODE;
  68. re->ref_count = (-adjust > re->ref_count)? 0 :
  69. (adjust + re->ref_count > 65535)? 65535 :
  70. re->ref_count + adjust;
  71. return re->ref_count;
  72. }
  73. /* End of pcre_refcount.c */