pcre32_utf32_utils.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 a function for converting any UTF-32 character
  33. strings to host byte order. */
  34. #ifdef HAVE_CONFIG_H
  35. #include "pcre_config.h"
  36. #endif
  37. /* Generate code with 32 bit character support. */
  38. #define COMPILE_PCRE32
  39. #include "pcre_internal.h"
  40. #ifdef SUPPORT_UTF
  41. static pcre_uint32
  42. swap_uint32(pcre_uint32 value)
  43. {
  44. return ((value & 0x000000ff) << 24) |
  45. ((value & 0x0000ff00) << 8) |
  46. ((value & 0x00ff0000) >> 8) |
  47. (value >> 24);
  48. }
  49. #endif
  50. /*************************************************
  51. * Convert any UTF-32 string to host byte order *
  52. *************************************************/
  53. /* This function takes an UTF-32 string and converts
  54. it to host byte order. The length can be explicitly set,
  55. or automatically detected for zero terminated strings.
  56. BOMs can be kept or discarded during the conversion.
  57. Conversion can be done in place (output == input).
  58. Arguments:
  59. output the output buffer, its size must be greater
  60. or equal than the input string
  61. input any UTF-32 string
  62. length the number of 32-bit units in the input string
  63. can be less than zero for zero terminated strings
  64. host_byte_order
  65. A non-zero value means the input is in host byte
  66. order, which can be dynamically changed by BOMs later.
  67. Initially it contains the starting byte order and returns
  68. with the last byte order so it can be used for stream
  69. processing. It can be NULL, which set the host byte
  70. order mode by default.
  71. keep_boms for a non-zero value, the BOM (0xfeff) characters
  72. are copied as well
  73. Returns: the number of 32-bit units placed into the output buffer,
  74. including the zero-terminator
  75. */
  76. int
  77. pcre32_utf32_to_host_byte_order(PCRE_UCHAR32 *output, PCRE_SPTR32 input,
  78. int length, int *host_byte_order, int keep_boms)
  79. {
  80. #ifdef SUPPORT_UTF
  81. /* This function converts any UTF-32 string to host byte order and optionally
  82. removes any Byte Order Marks (BOMS). Returns with the remainig length. */
  83. int host_bo = host_byte_order != NULL ? *host_byte_order : 1;
  84. pcre_uchar *optr = (pcre_uchar *)output;
  85. const pcre_uchar *iptr = (const pcre_uchar *)input;
  86. const pcre_uchar *end;
  87. /* The c variable must be unsigned. */
  88. register pcre_uchar c;
  89. if (length < 0)
  90. end = iptr + STRLEN_UC(iptr) + 1;
  91. else
  92. end = iptr + length;
  93. while (iptr < end)
  94. {
  95. c = *iptr++;
  96. if (c == 0x0000feffu || c == 0xfffe0000u)
  97. {
  98. /* Detecting the byte order of the machine is unnecessary, it is
  99. enough to know that the UTF-32 string has the same byte order or not. */
  100. host_bo = c == 0x0000feffu;
  101. if (keep_boms != 0)
  102. *optr++ = 0x0000feffu;
  103. }
  104. else
  105. *optr++ = host_bo ? c : swap_uint32(c);
  106. }
  107. if (host_byte_order != NULL)
  108. *host_byte_order = host_bo;
  109. #else /* SUPPORT_UTF */
  110. (void)(output); /* Keep picky compilers happy */
  111. (void)(input);
  112. (void)(keep_boms);
  113. (void)(host_byte_order);
  114. #endif /* SUPPORT_UTF */
  115. return length;
  116. }
  117. /* End of pcre32_utf32_utils.c */