/[pcre]/code/trunk/pcre16_utf16_utils.c
ViewVC logotype

Contents of /code/trunk/pcre16_utf16_utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1033 - (hide annotations) (download)
Mon Sep 10 11:02:48 2012 UTC (8 months, 1 week ago) by ph10
File MIME type: text/plain
File size: 4899 byte(s)
General spring-clean of EBCDIC-related issues in the code, which had decayed 
over time. Also the documentation. Added one test that can be run in an ASCII
world to do a little testing of EBCDIC-related things. 

1 ph10 757 /*************************************************
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 ph10 830 Copyright (c) 1997-2012 University of Cambridge
10 ph10 757
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 zherczeg 782 /* Generate code with 16 bit character support. */
50     #define COMPILE_PCRE16
51    
52 ph10 757 #include "pcre_internal.h"
53    
54 zherczeg 789 /*************************************************
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 ph10 854 or automatically detected for zero terminated strings.
61 zherczeg 789 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 ph10 854 length the number of 16-bit units in the input string
69 zherczeg 789 can be less than zero for zero terminated strings
70 zherczeg 809 host_byte_order
71     A non-zero value means the input is in host byte
72     order, which can be dynamically changed by BOMs later.
73     Initially it contains the starting byte order and returns
74     with the last byte order so it can be used for stream
75     processing. It can be NULL, which set the host byte
76     order mode by default.
77 zherczeg 789 keep_boms for a non-zero value, the BOM (0xfeff) characters
78     are copied as well
79    
80 ph10 854 Returns: the number of 16-bit units placed into the output buffer,
81 zherczeg 789 including the zero-terminator
82     */
83    
84 ph10 757 int
85 zherczeg 860 pcre16_utf16_to_host_byte_order(PCRE_UCHAR16 *output, PCRE_SPTR16 input,
86 zherczeg 809 int length, int *host_byte_order, int keep_boms)
87 ph10 757 {
88 zherczeg 794 #ifdef SUPPORT_UTF
89 zherczeg 809 /* This function converts any UTF-16 string to host byte order and optionally
90     removes any Byte Order Marks (BOMS). Returns with the remainig length. */
91     int host_bo = host_byte_order != NULL ? *host_byte_order : 1;
92 zherczeg 789 pcre_uchar *optr = (pcre_uchar *)output;
93     const pcre_uchar *iptr = (const pcre_uchar *)input;
94     const pcre_uchar *end;
95 ph10 757 /* The c variable must be unsigned. */
96 zherczeg 781 register pcre_uchar c;
97 ph10 757
98 zherczeg 789 if (length < 0)
99     length = STRLEN_UC(iptr) + 1;
100     end = iptr + length;
101    
102     while (iptr < end)
103 ph10 757 {
104 zherczeg 789 c = *iptr++;
105 ph10 757 if (c == 0xfeff || c == 0xfffe)
106     {
107     /* Detecting the byte order of the machine is unnecessary, it is
108     enough to know that the UTF-16 string has the same byte order or not. */
109 zherczeg 809 host_bo = c == 0xfeff;
110 ph10 757 if (keep_boms != 0)
111 zherczeg 789 *optr++ = 0xfeff;
112 ph10 757 else
113     length--;
114     }
115     else
116 zherczeg 809 *optr++ = host_bo ? c : ((c >> 8) | (c << 8)); /* Flip bytes if needed. */
117 ph10 757 }
118 zherczeg 809 if (host_byte_order != NULL)
119     *host_byte_order = host_bo;
120 ph10 757
121 ph10 1033 #else /* Not SUPPORT_UTF */
122 ph10 757 (void)(output); /* Keep picky compilers happy */
123     (void)(input);
124     (void)(keep_boms);
125 ph10 1033 (void)(host_byte_order);
126 zherczeg 794 #endif /* SUPPORT_UTF */
127 ph10 757 return length;
128     }
129    
130 zherczeg 764 /* End of pcre16_utf16_utils.c */

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12