pcre16_utf16_utils.c 4.8 KB

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