1 |
/*************************************************
|
2 |
* Perl-Compatible Regular Expressions *
|
3 |
*************************************************/
|
4 |
|
5 |
/* PCRE is a library of functions to support regular expressions whose syntax
|
6 |
and semantics are as close as possible to those of the Perl 5 language.
|
7 |
|
8 |
Written by Philip Hazel
|
9 |
Copyright (c) 1997-2009 University of Cambridge
|
10 |
|
11 |
-----------------------------------------------------------------------------
|
12 |
Redistribution and use in source and binary forms, with or without
|
13 |
modification, are permitted provided that the following conditions are met:
|
14 |
|
15 |
* Redistributions of source code must retain the above copyright notice,
|
16 |
this list of conditions and the following disclaimer.
|
17 |
|
18 |
* Redistributions in binary form must reproduce the above copyright
|
19 |
notice, this list of conditions and the following disclaimer in the
|
20 |
documentation and/or other materials provided with the distribution.
|
21 |
|
22 |
* Neither the name of the University of Cambridge nor the names of its
|
23 |
contributors may be used to endorse or promote products derived from
|
24 |
this software without specific prior written permission.
|
25 |
|
26 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
27 |
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
28 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
29 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
30 |
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
31 |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
32 |
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
33 |
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
34 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
35 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
36 |
POSSIBILITY OF SUCH DAMAGE.
|
37 |
-----------------------------------------------------------------------------
|
38 |
*/
|
39 |
|
40 |
|
41 |
/* This module contains a function for converting any UTF-16 character
|
42 |
strings to host byte order. */
|
43 |
|
44 |
|
45 |
#ifdef HAVE_CONFIG_H
|
46 |
#include "config.h"
|
47 |
#endif
|
48 |
|
49 |
/* Generate code with 16 bit character support. */
|
50 |
#define COMPILE_PCRE16
|
51 |
|
52 |
#include "pcre_internal.h"
|
53 |
|
54 |
/*************************************************
|
55 |
* Convert any UTF-16 string to host byte order *
|
56 |
*************************************************/
|
57 |
|
58 |
/* This function takes an UTF-16 string and converts
|
59 |
it to host byte order. The length can be explicitly set,
|
60 |
or autmatically detected for zero terminated strings.
|
61 |
BOMs can be kept or discarded during the conversion.
|
62 |
Conversion can be done in place (output == input).
|
63 |
|
64 |
Arguments:
|
65 |
output the output buffer, its size must be greater
|
66 |
or equal than the input string
|
67 |
input any UTF-16 string
|
68 |
length the number of characters in the input string
|
69 |
can be less than zero for zero terminated strings
|
70 |
keep_boms for a non-zero value, the BOM (0xfeff) characters
|
71 |
are copied as well
|
72 |
|
73 |
Returns: the number of characters placed into the output buffer,
|
74 |
including the zero-terminator
|
75 |
*/
|
76 |
|
77 |
int
|
78 |
pcre16_utf16_to_host_byte_order(PCRE_SCHAR16 *output, PCRE_SPTR16 input, int length, int keep_boms)
|
79 |
{
|
80 |
#ifdef SUPPORT_UTF16
|
81 |
/* This function converts any UTF-16 string to host byte order and optionally removes
|
82 |
any Byte Order Marks (BOMS). Returns with the remainig length. */
|
83 |
BOOL same_bo = TRUE;
|
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 |
|
90 |
if (length < 0)
|
91 |
length = STRLEN_UC(iptr) + 1;
|
92 |
end = iptr + length;
|
93 |
|
94 |
while (iptr < end)
|
95 |
{
|
96 |
c = *iptr++;
|
97 |
if (c == 0xfeff || c == 0xfffe)
|
98 |
{
|
99 |
/* Detecting the byte order of the machine is unnecessary, it is
|
100 |
enough to know that the UTF-16 string has the same byte order or not. */
|
101 |
same_bo = c == 0xfeff;
|
102 |
if (keep_boms != 0)
|
103 |
*optr++ = 0xfeff;
|
104 |
else
|
105 |
length--;
|
106 |
}
|
107 |
else
|
108 |
*optr++ = same_bo ? c : ((c >> 8) | (c << 8)); /* Flip bytes if needed. */
|
109 |
}
|
110 |
|
111 |
#else
|
112 |
(void)(output); /* Keep picky compilers happy */
|
113 |
(void)(input);
|
114 |
(void)(keep_boms);
|
115 |
#endif
|
116 |
return length;
|
117 |
}
|
118 |
|
119 |
/* End of pcre16_utf16_utils.c */
|