pcre2_ord2utf.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 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. /* This file contains a function that converts a Unicode character code point
  34. into a UTF string. The behaviour is different for each code unit width. */
  35. #ifdef HAVE_CONFIG_H
  36. #include "config.h"
  37. #endif
  38. #include "pcre2_internal.h"
  39. /* If SUPPORT_UNICODE is not defined, this function will never be called.
  40. Supply a dummy function because some compilers do not like empty source
  41. modules. */
  42. #ifndef SUPPORT_UNICODE
  43. unsigned int
  44. PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
  45. {
  46. (void)(cvalue);
  47. (void)(buffer);
  48. return 0;
  49. }
  50. #else /* SUPPORT_UNICODE */
  51. /*************************************************
  52. * Convert code point to UTF *
  53. *************************************************/
  54. /*
  55. Arguments:
  56. cvalue the character value
  57. buffer pointer to buffer for result
  58. Returns: number of code units placed in the buffer
  59. */
  60. unsigned int
  61. PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
  62. {
  63. /* Convert to UTF-8 */
  64. #if PCRE2_CODE_UNIT_WIDTH == 8
  65. int i, j;
  66. for (i = 0; i < PRIV(utf8_table1_size); i++)
  67. if ((int)cvalue <= PRIV(utf8_table1)[i]) break;
  68. buffer += i;
  69. for (j = i; j > 0; j--)
  70. {
  71. *buffer-- = 0x80 | (cvalue & 0x3f);
  72. cvalue >>= 6;
  73. }
  74. *buffer = PRIV(utf8_table2)[i] | cvalue;
  75. return i + 1;
  76. /* Convert to UTF-16 */
  77. #elif PCRE2_CODE_UNIT_WIDTH == 16
  78. if (cvalue <= 0xffff)
  79. {
  80. *buffer = (PCRE2_UCHAR)cvalue;
  81. return 1;
  82. }
  83. cvalue -= 0x10000;
  84. *buffer++ = 0xd800 | (cvalue >> 10);
  85. *buffer = 0xdc00 | (cvalue & 0x3ff);
  86. return 2;
  87. /* Convert to UTF-32 */
  88. #else
  89. *buffer = (PCRE2_UCHAR)cvalue;
  90. return 1;
  91. #endif
  92. }
  93. #endif /* SUPPORT_UNICODE */
  94. /* End of pcre_ord2utf.c */