blob: 1403730996d6bd2c294c972315e060612b8cab05 [file] [log] [blame]
Nick Kralevichf73ff172014-09-27 12:41:49 -07001/*************************************************
2* Perl-Compatible Regular Expressions *
3*************************************************/
4
5/* PCRE is a library of functions to support regular expressions whose syntax
6and semantics are as close as possible to those of the Perl 5 language.
7
8 Written by Philip Hazel
Janis Danisevskis112c9cc2016-03-31 13:35:25 +01009 Original API code Copyright (c) 1997-2012 University of Cambridge
10 New API code Copyright (c) 2016 University of Cambridge
Nick Kralevichf73ff172014-09-27 12:41:49 -070011
12-----------------------------------------------------------------------------
13Redistribution and use in source and binary forms, with or without
14modification, are permitted provided that the following conditions are met:
15
16 * Redistributions of source code must retain the above copyright notice,
17 this list of conditions and the following disclaimer.
18
19 * Redistributions in binary form must reproduce the above copyright
20 notice, this list of conditions and the following disclaimer in the
21 documentation and/or other materials provided with the distribution.
22
23 * Neither the name of the University of Cambridge nor the names of its
24 contributors may be used to endorse or promote products derived from
25 this software without specific prior written permission.
26
27THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
28AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
31LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37POSSIBILITY OF SUCH DAMAGE.
38-----------------------------------------------------------------------------
39*/
40
41
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010042/* This file contains a function that converts a Unicode character code point
43into a UTF string. The behaviour is different for each code unit width. */
44
Nick Kralevichf73ff172014-09-27 12:41:49 -070045
46#ifdef HAVE_CONFIG_H
47#include "config.h"
48#endif
49
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010050#include "pcre2_internal.h"
Nick Kralevichf73ff172014-09-27 12:41:49 -070051
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010052
53/* If SUPPORT_UNICODE is not defined, this function will never be called.
54Supply a dummy function because some compilers do not like empty source
55modules. */
56
57#ifndef SUPPORT_UNICODE
58unsigned int
59PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
60{
61(void)(cvalue);
62(void)(buffer);
63return 0;
64}
65#else /* SUPPORT_UNICODE */
66
Nick Kralevichf73ff172014-09-27 12:41:49 -070067
68/*************************************************
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010069* Convert code point to UTF *
Nick Kralevichf73ff172014-09-27 12:41:49 -070070*************************************************/
71
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010072/*
Nick Kralevichf73ff172014-09-27 12:41:49 -070073Arguments:
74 cvalue the character value
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010075 buffer pointer to buffer for result
Nick Kralevichf73ff172014-09-27 12:41:49 -070076
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010077Returns: number of code units placed in the buffer
Nick Kralevichf73ff172014-09-27 12:41:49 -070078*/
79
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010080unsigned int
81PRIV(ord2utf)(uint32_t cvalue, PCRE2_UCHAR *buffer)
Nick Kralevichf73ff172014-09-27 12:41:49 -070082{
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010083/* Convert to UTF-8 */
Nick Kralevichf73ff172014-09-27 12:41:49 -070084
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010085#if PCRE2_CODE_UNIT_WIDTH == 8
Elliott Hughes9bc971b2018-07-27 13:23:14 -070086int i, j;
Nick Kralevichf73ff172014-09-27 12:41:49 -070087for (i = 0; i < PRIV(utf8_table1_size); i++)
88 if ((int)cvalue <= PRIV(utf8_table1)[i]) break;
89buffer += i;
90for (j = i; j > 0; j--)
91 {
92 *buffer-- = 0x80 | (cvalue & 0x3f);
93 cvalue >>= 6;
94 }
95*buffer = PRIV(utf8_table2)[i] | cvalue;
96return i + 1;
97
Janis Danisevskis112c9cc2016-03-31 13:35:25 +010098/* Convert to UTF-16 */
99
100#elif PCRE2_CODE_UNIT_WIDTH == 16
101if (cvalue <= 0xffff)
102 {
103 *buffer = (PCRE2_UCHAR)cvalue;
104 return 1;
105 }
106cvalue -= 0x10000;
107*buffer++ = 0xd800 | (cvalue >> 10);
108*buffer = 0xdc00 | (cvalue & 0x3ff);
109return 2;
110
111/* Convert to UTF-32 */
112
Nick Kralevichf73ff172014-09-27 12:41:49 -0700113#else
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100114*buffer = (PCRE2_UCHAR)cvalue;
115return 1;
Nick Kralevichf73ff172014-09-27 12:41:49 -0700116#endif
117}
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100118#endif /* SUPPORT_UNICODE */
Nick Kralevichf73ff172014-09-27 12:41:49 -0700119
Janis Danisevskis112c9cc2016-03-31 13:35:25 +0100120/* End of pcre_ord2utf.c */