xref: /dflybsd-src/contrib/gcc-8.0/libcpp/charset.c (revision 38fd149817dfbff97799f62fcb70be98c4e32523)
1*38fd1498Szrj /* CPP Library - charsets
2*38fd1498Szrj    Copyright (C) 1998-2018 Free Software Foundation, Inc.
3*38fd1498Szrj 
4*38fd1498Szrj    Broken out of c-lex.c Apr 2003, adding valid C99 UCN ranges.
5*38fd1498Szrj 
6*38fd1498Szrj This program is free software; you can redistribute it and/or modify it
7*38fd1498Szrj under the terms of the GNU General Public License as published by the
8*38fd1498Szrj Free Software Foundation; either version 3, or (at your option) any
9*38fd1498Szrj later version.
10*38fd1498Szrj 
11*38fd1498Szrj This program is distributed in the hope that it will be useful,
12*38fd1498Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*38fd1498Szrj GNU General Public License for more details.
15*38fd1498Szrj 
16*38fd1498Szrj You should have received a copy of the GNU General Public License
17*38fd1498Szrj along with this program; see the file COPYING3.  If not see
18*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
19*38fd1498Szrj 
20*38fd1498Szrj #include "config.h"
21*38fd1498Szrj #include "system.h"
22*38fd1498Szrj #include "cpplib.h"
23*38fd1498Szrj #include "internal.h"
24*38fd1498Szrj 
25*38fd1498Szrj /* Character set handling for C-family languages.
26*38fd1498Szrj 
27*38fd1498Szrj    Terminological note: In what follows, "charset" or "character set"
28*38fd1498Szrj    will be taken to mean both an abstract set of characters and an
29*38fd1498Szrj    encoding for that set.
30*38fd1498Szrj 
31*38fd1498Szrj    The C99 standard discusses two character sets: source and execution.
32*38fd1498Szrj    The source character set is used for internal processing in translation
33*38fd1498Szrj    phases 1 through 4; the execution character set is used thereafter.
34*38fd1498Szrj    Both are required by 5.2.1.2p1 to be multibyte encodings, not wide
35*38fd1498Szrj    character encodings (see 3.7.2, 3.7.3 for the standardese meanings
36*38fd1498Szrj    of these terms).  Furthermore, the "basic character set" (listed in
37*38fd1498Szrj    5.2.1p3) is to be encoded in each with values one byte wide, and is
38*38fd1498Szrj    to appear in the initial shift state.
39*38fd1498Szrj 
40*38fd1498Szrj    It is not explicitly mentioned, but there is also a "wide execution
41*38fd1498Szrj    character set" used to encode wide character constants and wide
42*38fd1498Szrj    string literals; this is supposed to be the result of applying the
43*38fd1498Szrj    standard library function mbstowcs() to an equivalent narrow string
44*38fd1498Szrj    (6.4.5p5).  However, the behavior of hexadecimal and octal
45*38fd1498Szrj    \-escapes is at odds with this; they are supposed to be translated
46*38fd1498Szrj    directly to wchar_t values (6.4.4.4p5,6).
47*38fd1498Szrj 
48*38fd1498Szrj    The source character set is not necessarily the character set used
49*38fd1498Szrj    to encode physical source files on disk; translation phase 1 converts
50*38fd1498Szrj    from whatever that encoding is to the source character set.
51*38fd1498Szrj 
52*38fd1498Szrj    The presence of universal character names in C99 (6.4.3 et seq.)
53*38fd1498Szrj    forces the source character set to be isomorphic to ISO 10646,
54*38fd1498Szrj    that is, Unicode.  There is no such constraint on the execution
55*38fd1498Szrj    character set; note also that the conversion from source to
56*38fd1498Szrj    execution character set does not occur for identifiers (5.1.1.2p1#5).
57*38fd1498Szrj 
58*38fd1498Szrj    For convenience of implementation, the source character set's
59*38fd1498Szrj    encoding of the basic character set should be identical to the
60*38fd1498Szrj    execution character set OF THE HOST SYSTEM's encoding of the basic
61*38fd1498Szrj    character set, and it should not be a state-dependent encoding.
62*38fd1498Szrj 
63*38fd1498Szrj    cpplib uses UTF-8 or UTF-EBCDIC for the source character set,
64*38fd1498Szrj    depending on whether the host is based on ASCII or EBCDIC (see
65*38fd1498Szrj    respectively Unicode section 2.3/ISO10646 Amendment 2, and Unicode
66*38fd1498Szrj    Technical Report #16).  With limited exceptions, it relies on the
67*38fd1498Szrj    system library's iconv() primitive to do charset conversion
68*38fd1498Szrj    (specified in SUSv2).  */
69*38fd1498Szrj 
70*38fd1498Szrj #if !HAVE_ICONV
71*38fd1498Szrj /* Make certain that the uses of iconv(), iconv_open(), iconv_close()
72*38fd1498Szrj    below, which are guarded only by if statements with compile-time
73*38fd1498Szrj    constant conditions, do not cause link errors.  */
74*38fd1498Szrj #define iconv_open(x, y) (errno = EINVAL, (iconv_t)-1)
75*38fd1498Szrj #define iconv(a,b,c,d,e) (errno = EINVAL, (size_t)-1)
76*38fd1498Szrj #define iconv_close(x)   (void)0
77*38fd1498Szrj #define ICONV_CONST
78*38fd1498Szrj #endif
79*38fd1498Szrj 
80*38fd1498Szrj #if HOST_CHARSET == HOST_CHARSET_ASCII
81*38fd1498Szrj #define SOURCE_CHARSET "UTF-8"
82*38fd1498Szrj #define LAST_POSSIBLY_BASIC_SOURCE_CHAR 0x7e
83*38fd1498Szrj #elif HOST_CHARSET == HOST_CHARSET_EBCDIC
84*38fd1498Szrj #define SOURCE_CHARSET "UTF-EBCDIC"
85*38fd1498Szrj #define LAST_POSSIBLY_BASIC_SOURCE_CHAR 0xFF
86*38fd1498Szrj #else
87*38fd1498Szrj #error "Unrecognized basic host character set"
88*38fd1498Szrj #endif
89*38fd1498Szrj 
90*38fd1498Szrj #ifndef EILSEQ
91*38fd1498Szrj #define EILSEQ EINVAL
92*38fd1498Szrj #endif
93*38fd1498Szrj 
94*38fd1498Szrj /* This structure is used for a resizable string buffer throughout.  */
95*38fd1498Szrj /* Don't call it strbuf, as that conflicts with unistd.h on systems
96*38fd1498Szrj    such as DYNIX/ptx where unistd.h includes stropts.h.  */
97*38fd1498Szrj struct _cpp_strbuf
98*38fd1498Szrj {
99*38fd1498Szrj   uchar *text;
100*38fd1498Szrj   size_t asize;
101*38fd1498Szrj   size_t len;
102*38fd1498Szrj };
103*38fd1498Szrj 
104*38fd1498Szrj /* This is enough to hold any string that fits on a single 80-column
105*38fd1498Szrj    line, even if iconv quadruples its size (e.g. conversion from
106*38fd1498Szrj    ASCII to UTF-32) rounded up to a power of two.  */
107*38fd1498Szrj #define OUTBUF_BLOCK_SIZE 256
108*38fd1498Szrj 
109*38fd1498Szrj /* Conversions between UTF-8 and UTF-16/32 are implemented by custom
110*38fd1498Szrj    logic.  This is because a depressing number of systems lack iconv,
111*38fd1498Szrj    or have have iconv libraries that do not do these conversions, so
112*38fd1498Szrj    we need a fallback implementation for them.  To ensure the fallback
113*38fd1498Szrj    doesn't break due to neglect, it is used on all systems.
114*38fd1498Szrj 
115*38fd1498Szrj    UTF-32 encoding is nice and simple: a four-byte binary number,
116*38fd1498Szrj    constrained to the range 00000000-7FFFFFFF to avoid questions of
117*38fd1498Szrj    signedness.  We do have to cope with big- and little-endian
118*38fd1498Szrj    variants.
119*38fd1498Szrj 
120*38fd1498Szrj    UTF-16 encoding uses two-byte binary numbers, again in big- and
121*38fd1498Szrj    little-endian variants, for all values in the 00000000-0000FFFF
122*38fd1498Szrj    range.  Values in the 00010000-0010FFFF range are encoded as pairs
123*38fd1498Szrj    of two-byte numbers, called "surrogate pairs": given a number S in
124*38fd1498Szrj    this range, it is mapped to a pair (H, L) as follows:
125*38fd1498Szrj 
126*38fd1498Szrj      H = (S - 0x10000) / 0x400 + 0xD800
127*38fd1498Szrj      L = (S - 0x10000) % 0x400 + 0xDC00
128*38fd1498Szrj 
129*38fd1498Szrj    Two-byte values in the D800...DFFF range are ill-formed except as a
130*38fd1498Szrj    component of a surrogate pair.  Even if the encoding within a
131*38fd1498Szrj    two-byte value is little-endian, the H member of the surrogate pair
132*38fd1498Szrj    comes first.
133*38fd1498Szrj 
134*38fd1498Szrj    There is no way to encode values in the 00110000-7FFFFFFF range,
135*38fd1498Szrj    which is not currently a problem as there are no assigned code
136*38fd1498Szrj    points in that range; however, the author expects that it will
137*38fd1498Szrj    eventually become necessary to abandon UTF-16 due to this
138*38fd1498Szrj    limitation.  Note also that, because of these pairs, UTF-16 does
139*38fd1498Szrj    not meet the requirements of the C standard for a wide character
140*38fd1498Szrj    encoding (see 3.7.3 and 6.4.4.4p11).
141*38fd1498Szrj 
142*38fd1498Szrj    UTF-8 encoding looks like this:
143*38fd1498Szrj 
144*38fd1498Szrj    value range	       encoded as
145*38fd1498Szrj    00000000-0000007F   0xxxxxxx
146*38fd1498Szrj    00000080-000007FF   110xxxxx 10xxxxxx
147*38fd1498Szrj    00000800-0000FFFF   1110xxxx 10xxxxxx 10xxxxxx
148*38fd1498Szrj    00010000-001FFFFF   11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
149*38fd1498Szrj    00200000-03FFFFFF   111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
150*38fd1498Szrj    04000000-7FFFFFFF   1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
151*38fd1498Szrj 
152*38fd1498Szrj    Values in the 0000D800 ... 0000DFFF range (surrogates) are invalid,
153*38fd1498Szrj    which means that three-byte sequences ED xx yy, with A0 <= xx <= BF,
154*38fd1498Szrj    never occur.  Note also that any value that can be encoded by a
155*38fd1498Szrj    given row of the table can also be encoded by all successive rows,
156*38fd1498Szrj    but this is not done; only the shortest possible encoding for any
157*38fd1498Szrj    given value is valid.  For instance, the character 07C0 could be
158*38fd1498Szrj    encoded as any of DF 80, E0 9F 80, F0 80 9F 80, F8 80 80 9F 80, or
159*38fd1498Szrj    FC 80 80 80 9F 80.  Only the first is valid.
160*38fd1498Szrj 
161*38fd1498Szrj    An implementation note: the transformation from UTF-16 to UTF-8, or
162*38fd1498Szrj    vice versa, is easiest done by using UTF-32 as an intermediary.  */
163*38fd1498Szrj 
164*38fd1498Szrj /* Internal primitives which go from an UTF-8 byte stream to native-endian
165*38fd1498Szrj    UTF-32 in a cppchar_t, or vice versa; this avoids an extra marshal/unmarshal
166*38fd1498Szrj    operation in several places below.  */
167*38fd1498Szrj static inline int
one_utf8_to_cppchar(const uchar ** inbufp,size_t * inbytesleftp,cppchar_t * cp)168*38fd1498Szrj one_utf8_to_cppchar (const uchar **inbufp, size_t *inbytesleftp,
169*38fd1498Szrj 		     cppchar_t *cp)
170*38fd1498Szrj {
171*38fd1498Szrj   static const uchar masks[6] = { 0x7F, 0x1F, 0x0F, 0x07, 0x03, 0x01 };
172*38fd1498Szrj   static const uchar patns[6] = { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
173*38fd1498Szrj 
174*38fd1498Szrj   cppchar_t c;
175*38fd1498Szrj   const uchar *inbuf = *inbufp;
176*38fd1498Szrj   size_t nbytes, i;
177*38fd1498Szrj 
178*38fd1498Szrj   if (*inbytesleftp < 1)
179*38fd1498Szrj     return EINVAL;
180*38fd1498Szrj 
181*38fd1498Szrj   c = *inbuf;
182*38fd1498Szrj   if (c < 0x80)
183*38fd1498Szrj     {
184*38fd1498Szrj       *cp = c;
185*38fd1498Szrj       *inbytesleftp -= 1;
186*38fd1498Szrj       *inbufp += 1;
187*38fd1498Szrj       return 0;
188*38fd1498Szrj     }
189*38fd1498Szrj 
190*38fd1498Szrj   /* The number of leading 1-bits in the first byte indicates how many
191*38fd1498Szrj      bytes follow.  */
192*38fd1498Szrj   for (nbytes = 2; nbytes < 7; nbytes++)
193*38fd1498Szrj     if ((c & ~masks[nbytes-1]) == patns[nbytes-1])
194*38fd1498Szrj       goto found;
195*38fd1498Szrj   return EILSEQ;
196*38fd1498Szrj  found:
197*38fd1498Szrj 
198*38fd1498Szrj   if (*inbytesleftp < nbytes)
199*38fd1498Szrj     return EINVAL;
200*38fd1498Szrj 
201*38fd1498Szrj   c = (c & masks[nbytes-1]);
202*38fd1498Szrj   inbuf++;
203*38fd1498Szrj   for (i = 1; i < nbytes; i++)
204*38fd1498Szrj     {
205*38fd1498Szrj       cppchar_t n = *inbuf++;
206*38fd1498Szrj       if ((n & 0xC0) != 0x80)
207*38fd1498Szrj 	return EILSEQ;
208*38fd1498Szrj       c = ((c << 6) + (n & 0x3F));
209*38fd1498Szrj     }
210*38fd1498Szrj 
211*38fd1498Szrj   /* Make sure the shortest possible encoding was used.  */
212*38fd1498Szrj   if (c <=      0x7F && nbytes > 1) return EILSEQ;
213*38fd1498Szrj   if (c <=     0x7FF && nbytes > 2) return EILSEQ;
214*38fd1498Szrj   if (c <=    0xFFFF && nbytes > 3) return EILSEQ;
215*38fd1498Szrj   if (c <=  0x1FFFFF && nbytes > 4) return EILSEQ;
216*38fd1498Szrj   if (c <= 0x3FFFFFF && nbytes > 5) return EILSEQ;
217*38fd1498Szrj 
218*38fd1498Szrj   /* Make sure the character is valid.  */
219*38fd1498Szrj   if (c > 0x7FFFFFFF || (c >= 0xD800 && c <= 0xDFFF)) return EILSEQ;
220*38fd1498Szrj 
221*38fd1498Szrj   *cp = c;
222*38fd1498Szrj   *inbufp = inbuf;
223*38fd1498Szrj   *inbytesleftp -= nbytes;
224*38fd1498Szrj   return 0;
225*38fd1498Szrj }
226*38fd1498Szrj 
227*38fd1498Szrj static inline int
one_cppchar_to_utf8(cppchar_t c,uchar ** outbufp,size_t * outbytesleftp)228*38fd1498Szrj one_cppchar_to_utf8 (cppchar_t c, uchar **outbufp, size_t *outbytesleftp)
229*38fd1498Szrj {
230*38fd1498Szrj   static const uchar masks[6] =  { 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
231*38fd1498Szrj   static const uchar limits[6] = { 0x80, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE };
232*38fd1498Szrj   size_t nbytes;
233*38fd1498Szrj   uchar buf[6], *p = &buf[6];
234*38fd1498Szrj   uchar *outbuf = *outbufp;
235*38fd1498Szrj 
236*38fd1498Szrj   nbytes = 1;
237*38fd1498Szrj   if (c < 0x80)
238*38fd1498Szrj     *--p = c;
239*38fd1498Szrj   else
240*38fd1498Szrj     {
241*38fd1498Szrj       do
242*38fd1498Szrj 	{
243*38fd1498Szrj 	  *--p = ((c & 0x3F) | 0x80);
244*38fd1498Szrj 	  c >>= 6;
245*38fd1498Szrj 	  nbytes++;
246*38fd1498Szrj 	}
247*38fd1498Szrj       while (c >= 0x3F || (c & limits[nbytes-1]));
248*38fd1498Szrj       *--p = (c | masks[nbytes-1]);
249*38fd1498Szrj     }
250*38fd1498Szrj 
251*38fd1498Szrj   if (*outbytesleftp < nbytes)
252*38fd1498Szrj     return E2BIG;
253*38fd1498Szrj 
254*38fd1498Szrj   while (p < &buf[6])
255*38fd1498Szrj     *outbuf++ = *p++;
256*38fd1498Szrj   *outbytesleftp -= nbytes;
257*38fd1498Szrj   *outbufp = outbuf;
258*38fd1498Szrj   return 0;
259*38fd1498Szrj }
260*38fd1498Szrj 
261*38fd1498Szrj /* The following four functions transform one character between the two
262*38fd1498Szrj    encodings named in the function name.  All have the signature
263*38fd1498Szrj    int (*)(iconv_t bigend, const uchar **inbufp, size_t *inbytesleftp,
264*38fd1498Szrj            uchar **outbufp, size_t *outbytesleftp)
265*38fd1498Szrj 
266*38fd1498Szrj    BIGEND must have the value 0 or 1, coerced to (iconv_t); it is
267*38fd1498Szrj    interpreted as a boolean indicating whether big-endian or
268*38fd1498Szrj    little-endian encoding is to be used for the member of the pair
269*38fd1498Szrj    that is not UTF-8.
270*38fd1498Szrj 
271*38fd1498Szrj    INBUFP, INBYTESLEFTP, OUTBUFP, OUTBYTESLEFTP work exactly as they
272*38fd1498Szrj    do for iconv.
273*38fd1498Szrj 
274*38fd1498Szrj    The return value is either 0 for success, or an errno value for
275*38fd1498Szrj    failure, which may be E2BIG (need more space), EILSEQ (ill-formed
276*38fd1498Szrj    input sequence), ir EINVAL (incomplete input sequence).  */
277*38fd1498Szrj 
278*38fd1498Szrj static inline int
one_utf8_to_utf32(iconv_t bigend,const uchar ** inbufp,size_t * inbytesleftp,uchar ** outbufp,size_t * outbytesleftp)279*38fd1498Szrj one_utf8_to_utf32 (iconv_t bigend, const uchar **inbufp, size_t *inbytesleftp,
280*38fd1498Szrj 		   uchar **outbufp, size_t *outbytesleftp)
281*38fd1498Szrj {
282*38fd1498Szrj   uchar *outbuf;
283*38fd1498Szrj   cppchar_t s = 0;
284*38fd1498Szrj   int rval;
285*38fd1498Szrj 
286*38fd1498Szrj   /* Check for space first, since we know exactly how much we need.  */
287*38fd1498Szrj   if (*outbytesleftp < 4)
288*38fd1498Szrj     return E2BIG;
289*38fd1498Szrj 
290*38fd1498Szrj   rval = one_utf8_to_cppchar (inbufp, inbytesleftp, &s);
291*38fd1498Szrj   if (rval)
292*38fd1498Szrj     return rval;
293*38fd1498Szrj 
294*38fd1498Szrj   outbuf = *outbufp;
295*38fd1498Szrj   outbuf[bigend ? 3 : 0] = (s & 0x000000FF);
296*38fd1498Szrj   outbuf[bigend ? 2 : 1] = (s & 0x0000FF00) >> 8;
297*38fd1498Szrj   outbuf[bigend ? 1 : 2] = (s & 0x00FF0000) >> 16;
298*38fd1498Szrj   outbuf[bigend ? 0 : 3] = (s & 0xFF000000) >> 24;
299*38fd1498Szrj 
300*38fd1498Szrj   *outbufp += 4;
301*38fd1498Szrj   *outbytesleftp -= 4;
302*38fd1498Szrj   return 0;
303*38fd1498Szrj }
304*38fd1498Szrj 
305*38fd1498Szrj static inline int
one_utf32_to_utf8(iconv_t bigend,const uchar ** inbufp,size_t * inbytesleftp,uchar ** outbufp,size_t * outbytesleftp)306*38fd1498Szrj one_utf32_to_utf8 (iconv_t bigend, const uchar **inbufp, size_t *inbytesleftp,
307*38fd1498Szrj 		   uchar **outbufp, size_t *outbytesleftp)
308*38fd1498Szrj {
309*38fd1498Szrj   cppchar_t s;
310*38fd1498Szrj   int rval;
311*38fd1498Szrj   const uchar *inbuf;
312*38fd1498Szrj 
313*38fd1498Szrj   if (*inbytesleftp < 4)
314*38fd1498Szrj     return EINVAL;
315*38fd1498Szrj 
316*38fd1498Szrj   inbuf = *inbufp;
317*38fd1498Szrj 
318*38fd1498Szrj   s  = inbuf[bigend ? 0 : 3] << 24;
319*38fd1498Szrj   s += inbuf[bigend ? 1 : 2] << 16;
320*38fd1498Szrj   s += inbuf[bigend ? 2 : 1] << 8;
321*38fd1498Szrj   s += inbuf[bigend ? 3 : 0];
322*38fd1498Szrj 
323*38fd1498Szrj   if (s >= 0x7FFFFFFF || (s >= 0xD800 && s <= 0xDFFF))
324*38fd1498Szrj     return EILSEQ;
325*38fd1498Szrj 
326*38fd1498Szrj   rval = one_cppchar_to_utf8 (s, outbufp, outbytesleftp);
327*38fd1498Szrj   if (rval)
328*38fd1498Szrj     return rval;
329*38fd1498Szrj 
330*38fd1498Szrj   *inbufp += 4;
331*38fd1498Szrj   *inbytesleftp -= 4;
332*38fd1498Szrj   return 0;
333*38fd1498Szrj }
334*38fd1498Szrj 
335*38fd1498Szrj static inline int
one_utf8_to_utf16(iconv_t bigend,const uchar ** inbufp,size_t * inbytesleftp,uchar ** outbufp,size_t * outbytesleftp)336*38fd1498Szrj one_utf8_to_utf16 (iconv_t bigend, const uchar **inbufp, size_t *inbytesleftp,
337*38fd1498Szrj 		   uchar **outbufp, size_t *outbytesleftp)
338*38fd1498Szrj {
339*38fd1498Szrj   int rval;
340*38fd1498Szrj   cppchar_t s = 0;
341*38fd1498Szrj   const uchar *save_inbuf = *inbufp;
342*38fd1498Szrj   size_t save_inbytesleft = *inbytesleftp;
343*38fd1498Szrj   uchar *outbuf = *outbufp;
344*38fd1498Szrj 
345*38fd1498Szrj   rval = one_utf8_to_cppchar (inbufp, inbytesleftp, &s);
346*38fd1498Szrj   if (rval)
347*38fd1498Szrj     return rval;
348*38fd1498Szrj 
349*38fd1498Szrj   if (s > 0x0010FFFF)
350*38fd1498Szrj     {
351*38fd1498Szrj       *inbufp = save_inbuf;
352*38fd1498Szrj       *inbytesleftp = save_inbytesleft;
353*38fd1498Szrj       return EILSEQ;
354*38fd1498Szrj     }
355*38fd1498Szrj 
356*38fd1498Szrj   if (s <= 0xFFFF)
357*38fd1498Szrj     {
358*38fd1498Szrj       if (*outbytesleftp < 2)
359*38fd1498Szrj 	{
360*38fd1498Szrj 	  *inbufp = save_inbuf;
361*38fd1498Szrj 	  *inbytesleftp = save_inbytesleft;
362*38fd1498Szrj 	  return E2BIG;
363*38fd1498Szrj 	}
364*38fd1498Szrj       outbuf[bigend ? 1 : 0] = (s & 0x00FF);
365*38fd1498Szrj       outbuf[bigend ? 0 : 1] = (s & 0xFF00) >> 8;
366*38fd1498Szrj 
367*38fd1498Szrj       *outbufp += 2;
368*38fd1498Szrj       *outbytesleftp -= 2;
369*38fd1498Szrj       return 0;
370*38fd1498Szrj     }
371*38fd1498Szrj   else
372*38fd1498Szrj     {
373*38fd1498Szrj       cppchar_t hi, lo;
374*38fd1498Szrj 
375*38fd1498Szrj       if (*outbytesleftp < 4)
376*38fd1498Szrj 	{
377*38fd1498Szrj 	  *inbufp = save_inbuf;
378*38fd1498Szrj 	  *inbytesleftp = save_inbytesleft;
379*38fd1498Szrj 	  return E2BIG;
380*38fd1498Szrj 	}
381*38fd1498Szrj 
382*38fd1498Szrj       hi = (s - 0x10000) / 0x400 + 0xD800;
383*38fd1498Szrj       lo = (s - 0x10000) % 0x400 + 0xDC00;
384*38fd1498Szrj 
385*38fd1498Szrj       /* Even if we are little-endian, put the high surrogate first.
386*38fd1498Szrj 	 ??? Matches practice?  */
387*38fd1498Szrj       outbuf[bigend ? 1 : 0] = (hi & 0x00FF);
388*38fd1498Szrj       outbuf[bigend ? 0 : 1] = (hi & 0xFF00) >> 8;
389*38fd1498Szrj       outbuf[bigend ? 3 : 2] = (lo & 0x00FF);
390*38fd1498Szrj       outbuf[bigend ? 2 : 3] = (lo & 0xFF00) >> 8;
391*38fd1498Szrj 
392*38fd1498Szrj       *outbufp += 4;
393*38fd1498Szrj       *outbytesleftp -= 4;
394*38fd1498Szrj       return 0;
395*38fd1498Szrj     }
396*38fd1498Szrj }
397*38fd1498Szrj 
398*38fd1498Szrj static inline int
one_utf16_to_utf8(iconv_t bigend,const uchar ** inbufp,size_t * inbytesleftp,uchar ** outbufp,size_t * outbytesleftp)399*38fd1498Szrj one_utf16_to_utf8 (iconv_t bigend, const uchar **inbufp, size_t *inbytesleftp,
400*38fd1498Szrj 		   uchar **outbufp, size_t *outbytesleftp)
401*38fd1498Szrj {
402*38fd1498Szrj   cppchar_t s;
403*38fd1498Szrj   const uchar *inbuf = *inbufp;
404*38fd1498Szrj   int rval;
405*38fd1498Szrj 
406*38fd1498Szrj   if (*inbytesleftp < 2)
407*38fd1498Szrj     return EINVAL;
408*38fd1498Szrj   s  = inbuf[bigend ? 0 : 1] << 8;
409*38fd1498Szrj   s += inbuf[bigend ? 1 : 0];
410*38fd1498Szrj 
411*38fd1498Szrj   /* Low surrogate without immediately preceding high surrogate is invalid.  */
412*38fd1498Szrj   if (s >= 0xDC00 && s <= 0xDFFF)
413*38fd1498Szrj     return EILSEQ;
414*38fd1498Szrj   /* High surrogate must have a following low surrogate.  */
415*38fd1498Szrj   else if (s >= 0xD800 && s <= 0xDBFF)
416*38fd1498Szrj     {
417*38fd1498Szrj       cppchar_t hi = s, lo;
418*38fd1498Szrj       if (*inbytesleftp < 4)
419*38fd1498Szrj 	return EINVAL;
420*38fd1498Szrj 
421*38fd1498Szrj       lo  = inbuf[bigend ? 2 : 3] << 8;
422*38fd1498Szrj       lo += inbuf[bigend ? 3 : 2];
423*38fd1498Szrj 
424*38fd1498Szrj       if (lo < 0xDC00 || lo > 0xDFFF)
425*38fd1498Szrj 	return EILSEQ;
426*38fd1498Szrj 
427*38fd1498Szrj       s = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000;
428*38fd1498Szrj     }
429*38fd1498Szrj 
430*38fd1498Szrj   rval = one_cppchar_to_utf8 (s, outbufp, outbytesleftp);
431*38fd1498Szrj   if (rval)
432*38fd1498Szrj     return rval;
433*38fd1498Szrj 
434*38fd1498Szrj   /* Success - update the input pointers (one_cppchar_to_utf8 has done
435*38fd1498Szrj      the output pointers for us).  */
436*38fd1498Szrj   if (s <= 0xFFFF)
437*38fd1498Szrj     {
438*38fd1498Szrj       *inbufp += 2;
439*38fd1498Szrj       *inbytesleftp -= 2;
440*38fd1498Szrj     }
441*38fd1498Szrj   else
442*38fd1498Szrj     {
443*38fd1498Szrj       *inbufp += 4;
444*38fd1498Szrj       *inbytesleftp -= 4;
445*38fd1498Szrj     }
446*38fd1498Szrj   return 0;
447*38fd1498Szrj }
448*38fd1498Szrj 
449*38fd1498Szrj /* Helper routine for the next few functions.  The 'const' on
450*38fd1498Szrj    one_conversion means that we promise not to modify what function is
451*38fd1498Szrj    pointed to, which lets the inliner see through it.  */
452*38fd1498Szrj 
453*38fd1498Szrj static inline bool
conversion_loop(int (* const one_conversion)(iconv_t,const uchar **,size_t *,uchar **,size_t *),iconv_t cd,const uchar * from,size_t flen,struct _cpp_strbuf * to)454*38fd1498Szrj conversion_loop (int (*const one_conversion)(iconv_t, const uchar **, size_t *,
455*38fd1498Szrj 					     uchar **, size_t *),
456*38fd1498Szrj 		 iconv_t cd, const uchar *from, size_t flen, struct _cpp_strbuf *to)
457*38fd1498Szrj {
458*38fd1498Szrj   const uchar *inbuf;
459*38fd1498Szrj   uchar *outbuf;
460*38fd1498Szrj   size_t inbytesleft, outbytesleft;
461*38fd1498Szrj   int rval;
462*38fd1498Szrj 
463*38fd1498Szrj   inbuf = from;
464*38fd1498Szrj   inbytesleft = flen;
465*38fd1498Szrj   outbuf = to->text + to->len;
466*38fd1498Szrj   outbytesleft = to->asize - to->len;
467*38fd1498Szrj 
468*38fd1498Szrj   for (;;)
469*38fd1498Szrj     {
470*38fd1498Szrj       do
471*38fd1498Szrj 	rval = one_conversion (cd, &inbuf, &inbytesleft,
472*38fd1498Szrj 			       &outbuf, &outbytesleft);
473*38fd1498Szrj       while (inbytesleft && !rval);
474*38fd1498Szrj 
475*38fd1498Szrj       if (__builtin_expect (inbytesleft == 0, 1))
476*38fd1498Szrj 	{
477*38fd1498Szrj 	  to->len = to->asize - outbytesleft;
478*38fd1498Szrj 	  return true;
479*38fd1498Szrj 	}
480*38fd1498Szrj       if (rval != E2BIG)
481*38fd1498Szrj 	{
482*38fd1498Szrj 	  errno = rval;
483*38fd1498Szrj 	  return false;
484*38fd1498Szrj 	}
485*38fd1498Szrj 
486*38fd1498Szrj       outbytesleft += OUTBUF_BLOCK_SIZE;
487*38fd1498Szrj       to->asize += OUTBUF_BLOCK_SIZE;
488*38fd1498Szrj       to->text = XRESIZEVEC (uchar, to->text, to->asize);
489*38fd1498Szrj       outbuf = to->text + to->asize - outbytesleft;
490*38fd1498Szrj     }
491*38fd1498Szrj }
492*38fd1498Szrj 
493*38fd1498Szrj 
494*38fd1498Szrj /* These functions convert entire strings between character sets.
495*38fd1498Szrj    They all have the signature
496*38fd1498Szrj 
497*38fd1498Szrj    bool (*)(iconv_t cd, const uchar *from, size_t flen, struct _cpp_strbuf *to);
498*38fd1498Szrj 
499*38fd1498Szrj    The input string FROM is converted as specified by the function
500*38fd1498Szrj    name plus the iconv descriptor CD (which may be fake), and the
501*38fd1498Szrj    result appended to TO.  On any error, false is returned, otherwise true.  */
502*38fd1498Szrj 
503*38fd1498Szrj /* These four use the custom conversion code above.  */
504*38fd1498Szrj static bool
convert_utf8_utf16(iconv_t cd,const uchar * from,size_t flen,struct _cpp_strbuf * to)505*38fd1498Szrj convert_utf8_utf16 (iconv_t cd, const uchar *from, size_t flen,
506*38fd1498Szrj 		    struct _cpp_strbuf *to)
507*38fd1498Szrj {
508*38fd1498Szrj   return conversion_loop (one_utf8_to_utf16, cd, from, flen, to);
509*38fd1498Szrj }
510*38fd1498Szrj 
511*38fd1498Szrj static bool
convert_utf8_utf32(iconv_t cd,const uchar * from,size_t flen,struct _cpp_strbuf * to)512*38fd1498Szrj convert_utf8_utf32 (iconv_t cd, const uchar *from, size_t flen,
513*38fd1498Szrj 		    struct _cpp_strbuf *to)
514*38fd1498Szrj {
515*38fd1498Szrj   return conversion_loop (one_utf8_to_utf32, cd, from, flen, to);
516*38fd1498Szrj }
517*38fd1498Szrj 
518*38fd1498Szrj static bool
convert_utf16_utf8(iconv_t cd,const uchar * from,size_t flen,struct _cpp_strbuf * to)519*38fd1498Szrj convert_utf16_utf8 (iconv_t cd, const uchar *from, size_t flen,
520*38fd1498Szrj 		    struct _cpp_strbuf *to)
521*38fd1498Szrj {
522*38fd1498Szrj   return conversion_loop (one_utf16_to_utf8, cd, from, flen, to);
523*38fd1498Szrj }
524*38fd1498Szrj 
525*38fd1498Szrj static bool
convert_utf32_utf8(iconv_t cd,const uchar * from,size_t flen,struct _cpp_strbuf * to)526*38fd1498Szrj convert_utf32_utf8 (iconv_t cd, const uchar *from, size_t flen,
527*38fd1498Szrj 		    struct _cpp_strbuf *to)
528*38fd1498Szrj {
529*38fd1498Szrj   return conversion_loop (one_utf32_to_utf8, cd, from, flen, to);
530*38fd1498Szrj }
531*38fd1498Szrj 
532*38fd1498Szrj /* Identity conversion, used when we have no alternative.  */
533*38fd1498Szrj static bool
convert_no_conversion(iconv_t cd ATTRIBUTE_UNUSED,const uchar * from,size_t flen,struct _cpp_strbuf * to)534*38fd1498Szrj convert_no_conversion (iconv_t cd ATTRIBUTE_UNUSED,
535*38fd1498Szrj 		       const uchar *from, size_t flen, struct _cpp_strbuf *to)
536*38fd1498Szrj {
537*38fd1498Szrj   if (to->len + flen > to->asize)
538*38fd1498Szrj     {
539*38fd1498Szrj       to->asize = to->len + flen;
540*38fd1498Szrj       to->asize += to->asize / 4;
541*38fd1498Szrj       to->text = XRESIZEVEC (uchar, to->text, to->asize);
542*38fd1498Szrj     }
543*38fd1498Szrj   memcpy (to->text + to->len, from, flen);
544*38fd1498Szrj   to->len += flen;
545*38fd1498Szrj   return true;
546*38fd1498Szrj }
547*38fd1498Szrj 
548*38fd1498Szrj /* And this one uses the system iconv primitive.  It's a little
549*38fd1498Szrj    different, since iconv's interface is a little different.  */
550*38fd1498Szrj #if HAVE_ICONV
551*38fd1498Szrj 
552*38fd1498Szrj #define CONVERT_ICONV_GROW_BUFFER \
553*38fd1498Szrj   do { \
554*38fd1498Szrj       outbytesleft += OUTBUF_BLOCK_SIZE; \
555*38fd1498Szrj       to->asize += OUTBUF_BLOCK_SIZE; \
556*38fd1498Szrj       to->text = XRESIZEVEC (uchar, to->text, to->asize); \
557*38fd1498Szrj       outbuf = (char *)to->text + to->asize - outbytesleft; \
558*38fd1498Szrj   } while (0)
559*38fd1498Szrj 
560*38fd1498Szrj static bool
convert_using_iconv(iconv_t cd,const uchar * from,size_t flen,struct _cpp_strbuf * to)561*38fd1498Szrj convert_using_iconv (iconv_t cd, const uchar *from, size_t flen,
562*38fd1498Szrj 		     struct _cpp_strbuf *to)
563*38fd1498Szrj {
564*38fd1498Szrj   ICONV_CONST char *inbuf;
565*38fd1498Szrj   char *outbuf;
566*38fd1498Szrj   size_t inbytesleft, outbytesleft;
567*38fd1498Szrj 
568*38fd1498Szrj   /* Reset conversion descriptor and check that it is valid.  */
569*38fd1498Szrj   if (iconv (cd, 0, 0, 0, 0) == (size_t)-1)
570*38fd1498Szrj     return false;
571*38fd1498Szrj 
572*38fd1498Szrj   inbuf = (ICONV_CONST char *)from;
573*38fd1498Szrj   inbytesleft = flen;
574*38fd1498Szrj   outbuf = (char *)to->text + to->len;
575*38fd1498Szrj   outbytesleft = to->asize - to->len;
576*38fd1498Szrj 
577*38fd1498Szrj   for (;;)
578*38fd1498Szrj     {
579*38fd1498Szrj       iconv (cd, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
580*38fd1498Szrj       if (__builtin_expect (inbytesleft == 0, 1))
581*38fd1498Szrj 	{
582*38fd1498Szrj 	  /* Close out any shift states, returning to the initial state.  */
583*38fd1498Szrj 	  if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t)-1)
584*38fd1498Szrj 	    {
585*38fd1498Szrj 	      if (errno != E2BIG)
586*38fd1498Szrj 		return false;
587*38fd1498Szrj 
588*38fd1498Szrj 	      CONVERT_ICONV_GROW_BUFFER;
589*38fd1498Szrj 	      if (iconv (cd, 0, 0, &outbuf, &outbytesleft) == (size_t)-1)
590*38fd1498Szrj 		return false;
591*38fd1498Szrj 	    }
592*38fd1498Szrj 
593*38fd1498Szrj 	  to->len = to->asize - outbytesleft;
594*38fd1498Szrj 	  return true;
595*38fd1498Szrj 	}
596*38fd1498Szrj       if (errno != E2BIG)
597*38fd1498Szrj 	return false;
598*38fd1498Szrj 
599*38fd1498Szrj       CONVERT_ICONV_GROW_BUFFER;
600*38fd1498Szrj     }
601*38fd1498Szrj }
602*38fd1498Szrj #else
603*38fd1498Szrj #define convert_using_iconv 0 /* prevent undefined symbol error below */
604*38fd1498Szrj #endif
605*38fd1498Szrj 
606*38fd1498Szrj /* Arrange for the above custom conversion logic to be used automatically
607*38fd1498Szrj    when conversion between a suitable pair of character sets is requested.  */
608*38fd1498Szrj 
609*38fd1498Szrj #define APPLY_CONVERSION(CONVERTER, FROM, FLEN, TO) \
610*38fd1498Szrj    CONVERTER.func (CONVERTER.cd, FROM, FLEN, TO)
611*38fd1498Szrj 
612*38fd1498Szrj struct cpp_conversion
613*38fd1498Szrj {
614*38fd1498Szrj   const char *pair;
615*38fd1498Szrj   convert_f func;
616*38fd1498Szrj   iconv_t fake_cd;
617*38fd1498Szrj };
618*38fd1498Szrj static const struct cpp_conversion conversion_tab[] = {
619*38fd1498Szrj   { "UTF-8/UTF-32LE", convert_utf8_utf32, (iconv_t)0 },
620*38fd1498Szrj   { "UTF-8/UTF-32BE", convert_utf8_utf32, (iconv_t)1 },
621*38fd1498Szrj   { "UTF-8/UTF-16LE", convert_utf8_utf16, (iconv_t)0 },
622*38fd1498Szrj   { "UTF-8/UTF-16BE", convert_utf8_utf16, (iconv_t)1 },
623*38fd1498Szrj   { "UTF-32LE/UTF-8", convert_utf32_utf8, (iconv_t)0 },
624*38fd1498Szrj   { "UTF-32BE/UTF-8", convert_utf32_utf8, (iconv_t)1 },
625*38fd1498Szrj   { "UTF-16LE/UTF-8", convert_utf16_utf8, (iconv_t)0 },
626*38fd1498Szrj   { "UTF-16BE/UTF-8", convert_utf16_utf8, (iconv_t)1 },
627*38fd1498Szrj };
628*38fd1498Szrj 
629*38fd1498Szrj /* Subroutine of cpp_init_iconv: initialize and return a
630*38fd1498Szrj    cset_converter structure for conversion from FROM to TO.  If
631*38fd1498Szrj    iconv_open() fails, issue an error and return an identity
632*38fd1498Szrj    converter.  Silently return an identity converter if FROM and TO
633*38fd1498Szrj    are identical.  */
634*38fd1498Szrj static struct cset_converter
init_iconv_desc(cpp_reader * pfile,const char * to,const char * from)635*38fd1498Szrj init_iconv_desc (cpp_reader *pfile, const char *to, const char *from)
636*38fd1498Szrj {
637*38fd1498Szrj   struct cset_converter ret;
638*38fd1498Szrj   char *pair;
639*38fd1498Szrj   size_t i;
640*38fd1498Szrj 
641*38fd1498Szrj   if (!strcasecmp (to, from))
642*38fd1498Szrj     {
643*38fd1498Szrj       ret.func = convert_no_conversion;
644*38fd1498Szrj       ret.cd = (iconv_t) -1;
645*38fd1498Szrj       ret.width = -1;
646*38fd1498Szrj       return ret;
647*38fd1498Szrj     }
648*38fd1498Szrj 
649*38fd1498Szrj   pair = (char *) alloca(strlen(to) + strlen(from) + 2);
650*38fd1498Szrj 
651*38fd1498Szrj   strcpy(pair, from);
652*38fd1498Szrj   strcat(pair, "/");
653*38fd1498Szrj   strcat(pair, to);
654*38fd1498Szrj   for (i = 0; i < ARRAY_SIZE (conversion_tab); i++)
655*38fd1498Szrj     if (!strcasecmp (pair, conversion_tab[i].pair))
656*38fd1498Szrj       {
657*38fd1498Szrj 	ret.func = conversion_tab[i].func;
658*38fd1498Szrj 	ret.cd = conversion_tab[i].fake_cd;
659*38fd1498Szrj 	ret.width = -1;
660*38fd1498Szrj 	return ret;
661*38fd1498Szrj       }
662*38fd1498Szrj 
663*38fd1498Szrj   /* No custom converter - try iconv.  */
664*38fd1498Szrj   if (HAVE_ICONV)
665*38fd1498Szrj     {
666*38fd1498Szrj       ret.func = convert_using_iconv;
667*38fd1498Szrj       ret.cd = iconv_open (to, from);
668*38fd1498Szrj       ret.width = -1;
669*38fd1498Szrj 
670*38fd1498Szrj       if (ret.cd == (iconv_t) -1)
671*38fd1498Szrj 	{
672*38fd1498Szrj 	  if (errno == EINVAL)
673*38fd1498Szrj 	    cpp_error (pfile, CPP_DL_ERROR, /* FIXME should be DL_SORRY */
674*38fd1498Szrj 		       "conversion from %s to %s not supported by iconv",
675*38fd1498Szrj 		       from, to);
676*38fd1498Szrj 	  else
677*38fd1498Szrj 	    cpp_errno (pfile, CPP_DL_ERROR, "iconv_open");
678*38fd1498Szrj 
679*38fd1498Szrj 	  ret.func = convert_no_conversion;
680*38fd1498Szrj 	}
681*38fd1498Szrj     }
682*38fd1498Szrj   else
683*38fd1498Szrj     {
684*38fd1498Szrj       cpp_error (pfile, CPP_DL_ERROR, /* FIXME: should be DL_SORRY */
685*38fd1498Szrj 		 "no iconv implementation, cannot convert from %s to %s",
686*38fd1498Szrj 		 from, to);
687*38fd1498Szrj       ret.func = convert_no_conversion;
688*38fd1498Szrj       ret.cd = (iconv_t) -1;
689*38fd1498Szrj       ret.width = -1;
690*38fd1498Szrj     }
691*38fd1498Szrj   return ret;
692*38fd1498Szrj }
693*38fd1498Szrj 
694*38fd1498Szrj /* If charset conversion is requested, initialize iconv(3) descriptors
695*38fd1498Szrj    for conversion from the source character set to the execution
696*38fd1498Szrj    character sets.  If iconv is not present in the C library, and
697*38fd1498Szrj    conversion is requested, issue an error.  */
698*38fd1498Szrj 
699*38fd1498Szrj void
cpp_init_iconv(cpp_reader * pfile)700*38fd1498Szrj cpp_init_iconv (cpp_reader *pfile)
701*38fd1498Szrj {
702*38fd1498Szrj   const char *ncset = CPP_OPTION (pfile, narrow_charset);
703*38fd1498Szrj   const char *wcset = CPP_OPTION (pfile, wide_charset);
704*38fd1498Szrj   const char *default_wcset;
705*38fd1498Szrj 
706*38fd1498Szrj   bool be = CPP_OPTION (pfile, bytes_big_endian);
707*38fd1498Szrj 
708*38fd1498Szrj   if (CPP_OPTION (pfile, wchar_precision) >= 32)
709*38fd1498Szrj     default_wcset = be ? "UTF-32BE" : "UTF-32LE";
710*38fd1498Szrj   else if (CPP_OPTION (pfile, wchar_precision) >= 16)
711*38fd1498Szrj     default_wcset = be ? "UTF-16BE" : "UTF-16LE";
712*38fd1498Szrj   else
713*38fd1498Szrj     /* This effectively means that wide strings are not supported,
714*38fd1498Szrj        so don't do any conversion at all.  */
715*38fd1498Szrj    default_wcset = SOURCE_CHARSET;
716*38fd1498Szrj 
717*38fd1498Szrj   if (!ncset)
718*38fd1498Szrj     ncset = SOURCE_CHARSET;
719*38fd1498Szrj   if (!wcset)
720*38fd1498Szrj     wcset = default_wcset;
721*38fd1498Szrj 
722*38fd1498Szrj   pfile->narrow_cset_desc = init_iconv_desc (pfile, ncset, SOURCE_CHARSET);
723*38fd1498Szrj   pfile->narrow_cset_desc.width = CPP_OPTION (pfile, char_precision);
724*38fd1498Szrj   pfile->utf8_cset_desc = init_iconv_desc (pfile, "UTF-8", SOURCE_CHARSET);
725*38fd1498Szrj   pfile->utf8_cset_desc.width = CPP_OPTION (pfile, char_precision);
726*38fd1498Szrj   pfile->char16_cset_desc = init_iconv_desc (pfile,
727*38fd1498Szrj 					     be ? "UTF-16BE" : "UTF-16LE",
728*38fd1498Szrj 					     SOURCE_CHARSET);
729*38fd1498Szrj   pfile->char16_cset_desc.width = 16;
730*38fd1498Szrj   pfile->char32_cset_desc = init_iconv_desc (pfile,
731*38fd1498Szrj 					     be ? "UTF-32BE" : "UTF-32LE",
732*38fd1498Szrj 					     SOURCE_CHARSET);
733*38fd1498Szrj   pfile->char32_cset_desc.width = 32;
734*38fd1498Szrj   pfile->wide_cset_desc = init_iconv_desc (pfile, wcset, SOURCE_CHARSET);
735*38fd1498Szrj   pfile->wide_cset_desc.width = CPP_OPTION (pfile, wchar_precision);
736*38fd1498Szrj }
737*38fd1498Szrj 
738*38fd1498Szrj /* Destroy iconv(3) descriptors set up by cpp_init_iconv, if necessary.  */
739*38fd1498Szrj void
_cpp_destroy_iconv(cpp_reader * pfile)740*38fd1498Szrj _cpp_destroy_iconv (cpp_reader *pfile)
741*38fd1498Szrj {
742*38fd1498Szrj   if (HAVE_ICONV)
743*38fd1498Szrj     {
744*38fd1498Szrj       if (pfile->narrow_cset_desc.func == convert_using_iconv)
745*38fd1498Szrj 	iconv_close (pfile->narrow_cset_desc.cd);
746*38fd1498Szrj       if (pfile->utf8_cset_desc.func == convert_using_iconv)
747*38fd1498Szrj 	iconv_close (pfile->utf8_cset_desc.cd);
748*38fd1498Szrj       if (pfile->char16_cset_desc.func == convert_using_iconv)
749*38fd1498Szrj 	iconv_close (pfile->char16_cset_desc.cd);
750*38fd1498Szrj       if (pfile->char32_cset_desc.func == convert_using_iconv)
751*38fd1498Szrj 	iconv_close (pfile->char32_cset_desc.cd);
752*38fd1498Szrj       if (pfile->wide_cset_desc.func == convert_using_iconv)
753*38fd1498Szrj 	iconv_close (pfile->wide_cset_desc.cd);
754*38fd1498Szrj     }
755*38fd1498Szrj }
756*38fd1498Szrj 
757*38fd1498Szrj /* Utility routine for use by a full compiler.  C is a character taken
758*38fd1498Szrj    from the *basic* source character set, encoded in the host's
759*38fd1498Szrj    execution encoding.  Convert it to (the target's) execution
760*38fd1498Szrj    encoding, and return that value.
761*38fd1498Szrj 
762*38fd1498Szrj    Issues an internal error if C's representation in the narrow
763*38fd1498Szrj    execution character set fails to be a single-byte value (C99
764*38fd1498Szrj    5.2.1p3: "The representation of each member of the source and
765*38fd1498Szrj    execution character sets shall fit in a byte.")  May also issue an
766*38fd1498Szrj    internal error if C fails to be a member of the basic source
767*38fd1498Szrj    character set (testing this exactly is too hard, especially when
768*38fd1498Szrj    the host character set is EBCDIC).  */
769*38fd1498Szrj cppchar_t
cpp_host_to_exec_charset(cpp_reader * pfile,cppchar_t c)770*38fd1498Szrj cpp_host_to_exec_charset (cpp_reader *pfile, cppchar_t c)
771*38fd1498Szrj {
772*38fd1498Szrj   uchar sbuf[1];
773*38fd1498Szrj   struct _cpp_strbuf tbuf;
774*38fd1498Szrj 
775*38fd1498Szrj   /* This test is merely an approximation, but it suffices to catch
776*38fd1498Szrj      the most important thing, which is that we don't get handed a
777*38fd1498Szrj      character outside the unibyte range of the host character set.  */
778*38fd1498Szrj   if (c > LAST_POSSIBLY_BASIC_SOURCE_CHAR)
779*38fd1498Szrj     {
780*38fd1498Szrj       cpp_error (pfile, CPP_DL_ICE,
781*38fd1498Szrj 		 "character 0x%lx is not in the basic source character set\n",
782*38fd1498Szrj 		 (unsigned long)c);
783*38fd1498Szrj       return 0;
784*38fd1498Szrj     }
785*38fd1498Szrj 
786*38fd1498Szrj   /* Being a character in the unibyte range of the host character set,
787*38fd1498Szrj      we can safely splat it into a one-byte buffer and trust that that
788*38fd1498Szrj      is a well-formed string.  */
789*38fd1498Szrj   sbuf[0] = c;
790*38fd1498Szrj 
791*38fd1498Szrj   /* This should never need to reallocate, but just in case... */
792*38fd1498Szrj   tbuf.asize = 1;
793*38fd1498Szrj   tbuf.text = XNEWVEC (uchar, tbuf.asize);
794*38fd1498Szrj   tbuf.len = 0;
795*38fd1498Szrj 
796*38fd1498Szrj   if (!APPLY_CONVERSION (pfile->narrow_cset_desc, sbuf, 1, &tbuf))
797*38fd1498Szrj     {
798*38fd1498Szrj       cpp_errno (pfile, CPP_DL_ICE, "converting to execution character set");
799*38fd1498Szrj       return 0;
800*38fd1498Szrj     }
801*38fd1498Szrj   if (tbuf.len != 1)
802*38fd1498Szrj     {
803*38fd1498Szrj       cpp_error (pfile, CPP_DL_ICE,
804*38fd1498Szrj 		 "character 0x%lx is not unibyte in execution character set",
805*38fd1498Szrj 		 (unsigned long)c);
806*38fd1498Szrj       return 0;
807*38fd1498Szrj     }
808*38fd1498Szrj   c = tbuf.text[0];
809*38fd1498Szrj   free(tbuf.text);
810*38fd1498Szrj   return c;
811*38fd1498Szrj }
812*38fd1498Szrj 
813*38fd1498Szrj 
814*38fd1498Szrj 
815*38fd1498Szrj /* cpp_substring_ranges's constructor. */
816*38fd1498Szrj 
cpp_substring_ranges()817*38fd1498Szrj cpp_substring_ranges::cpp_substring_ranges () :
818*38fd1498Szrj   m_ranges (NULL),
819*38fd1498Szrj   m_num_ranges (0),
820*38fd1498Szrj   m_alloc_ranges (8)
821*38fd1498Szrj {
822*38fd1498Szrj   m_ranges = XNEWVEC (source_range, m_alloc_ranges);
823*38fd1498Szrj }
824*38fd1498Szrj 
825*38fd1498Szrj /* cpp_substring_ranges's destructor. */
826*38fd1498Szrj 
~cpp_substring_ranges()827*38fd1498Szrj cpp_substring_ranges::~cpp_substring_ranges ()
828*38fd1498Szrj {
829*38fd1498Szrj   free (m_ranges);
830*38fd1498Szrj }
831*38fd1498Szrj 
832*38fd1498Szrj /* Add RANGE to the vector of source_range information.  */
833*38fd1498Szrj 
834*38fd1498Szrj void
add_range(source_range range)835*38fd1498Szrj cpp_substring_ranges::add_range (source_range range)
836*38fd1498Szrj {
837*38fd1498Szrj   if (m_num_ranges >= m_alloc_ranges)
838*38fd1498Szrj     {
839*38fd1498Szrj       m_alloc_ranges *= 2;
840*38fd1498Szrj       m_ranges
841*38fd1498Szrj 	= (source_range *)xrealloc (m_ranges,
842*38fd1498Szrj 				    sizeof (source_range) * m_alloc_ranges);
843*38fd1498Szrj     }
844*38fd1498Szrj   m_ranges[m_num_ranges++] = range;
845*38fd1498Szrj }
846*38fd1498Szrj 
847*38fd1498Szrj /* Read NUM ranges from LOC_READER, adding them to the vector of source_range
848*38fd1498Szrj    information.  */
849*38fd1498Szrj 
850*38fd1498Szrj void
add_n_ranges(int num,cpp_string_location_reader & loc_reader)851*38fd1498Szrj cpp_substring_ranges::add_n_ranges (int num,
852*38fd1498Szrj 				    cpp_string_location_reader &loc_reader)
853*38fd1498Szrj {
854*38fd1498Szrj   for (int i = 0; i < num; i++)
855*38fd1498Szrj     add_range (loc_reader.get_next ());
856*38fd1498Szrj }
857*38fd1498Szrj 
858*38fd1498Szrj 
859*38fd1498Szrj 
860*38fd1498Szrj /* Utility routine that computes a mask of the form 0000...111... with
861*38fd1498Szrj    WIDTH 1-bits.  */
862*38fd1498Szrj static inline size_t
width_to_mask(size_t width)863*38fd1498Szrj width_to_mask (size_t width)
864*38fd1498Szrj {
865*38fd1498Szrj   width = MIN (width, BITS_PER_CPPCHAR_T);
866*38fd1498Szrj   if (width >= CHAR_BIT * sizeof (size_t))
867*38fd1498Szrj     return ~(size_t) 0;
868*38fd1498Szrj   else
869*38fd1498Szrj     return ((size_t) 1 << width) - 1;
870*38fd1498Szrj }
871*38fd1498Szrj 
872*38fd1498Szrj /* A large table of unicode character information.  */
873*38fd1498Szrj enum {
874*38fd1498Szrj   /* Valid in a C99 identifier?  */
875*38fd1498Szrj   C99 = 1,
876*38fd1498Szrj   /* Valid in a C99 identifier, but not as the first character?  */
877*38fd1498Szrj   N99 = 2,
878*38fd1498Szrj   /* Valid in a C++ identifier?  */
879*38fd1498Szrj   CXX = 4,
880*38fd1498Szrj   /* Valid in a C11/C++11 identifier?  */
881*38fd1498Szrj   C11 = 8,
882*38fd1498Szrj   /* Valid in a C11/C++11 identifier, but not as the first character?  */
883*38fd1498Szrj   N11 = 16,
884*38fd1498Szrj   /* NFC representation is not valid in an identifier?  */
885*38fd1498Szrj   CID = 32,
886*38fd1498Szrj   /* Might be valid NFC form?  */
887*38fd1498Szrj   NFC = 64,
888*38fd1498Szrj   /* Might be valid NFKC form?  */
889*38fd1498Szrj   NKC = 128,
890*38fd1498Szrj   /* Certain preceding characters might make it not valid NFC/NKFC form?  */
891*38fd1498Szrj   CTX = 256
892*38fd1498Szrj };
893*38fd1498Szrj 
894*38fd1498Szrj struct ucnrange {
895*38fd1498Szrj   /* Bitmap of flags above.  */
896*38fd1498Szrj   unsigned short flags;
897*38fd1498Szrj   /* Combining class of the character.  */
898*38fd1498Szrj   unsigned char combine;
899*38fd1498Szrj   /* Last character in the range described by this entry.  */
900*38fd1498Szrj   unsigned int end;
901*38fd1498Szrj };
902*38fd1498Szrj #include "ucnid.h"
903*38fd1498Szrj 
904*38fd1498Szrj /* Returns 1 if C is valid in an identifier, 2 if C is valid except at
905*38fd1498Szrj    the start of an identifier, and 0 if C is not valid in an
906*38fd1498Szrj    identifier.  We assume C has already gone through the checks of
907*38fd1498Szrj    _cpp_valid_ucn.  Also update NST for C if returning nonzero.  The
908*38fd1498Szrj    algorithm is a simple binary search on the table defined in
909*38fd1498Szrj    ucnid.h.  */
910*38fd1498Szrj 
911*38fd1498Szrj static int
ucn_valid_in_identifier(cpp_reader * pfile,cppchar_t c,struct normalize_state * nst)912*38fd1498Szrj ucn_valid_in_identifier (cpp_reader *pfile, cppchar_t c,
913*38fd1498Szrj 			 struct normalize_state *nst)
914*38fd1498Szrj {
915*38fd1498Szrj   int mn, mx, md;
916*38fd1498Szrj   unsigned short valid_flags, invalid_start_flags;
917*38fd1498Szrj 
918*38fd1498Szrj   if (c > 0x10FFFF)
919*38fd1498Szrj     return 0;
920*38fd1498Szrj 
921*38fd1498Szrj   mn = 0;
922*38fd1498Szrj   mx = ARRAY_SIZE (ucnranges) - 1;
923*38fd1498Szrj   while (mx != mn)
924*38fd1498Szrj     {
925*38fd1498Szrj       md = (mn + mx) / 2;
926*38fd1498Szrj       if (c <= ucnranges[md].end)
927*38fd1498Szrj 	mx = md;
928*38fd1498Szrj       else
929*38fd1498Szrj 	mn = md + 1;
930*38fd1498Szrj     }
931*38fd1498Szrj 
932*38fd1498Szrj   /* When -pedantic, we require the character to have been listed by
933*38fd1498Szrj      the standard for the current language.  Otherwise, we accept the
934*38fd1498Szrj      union of the acceptable sets for all supported language versions.  */
935*38fd1498Szrj   valid_flags = C99 | CXX | C11;
936*38fd1498Szrj   if (CPP_PEDANTIC (pfile))
937*38fd1498Szrj     {
938*38fd1498Szrj       if (CPP_OPTION (pfile, c11_identifiers))
939*38fd1498Szrj 	valid_flags = C11;
940*38fd1498Szrj       else if (CPP_OPTION (pfile, c99))
941*38fd1498Szrj 	valid_flags = C99;
942*38fd1498Szrj       else if (CPP_OPTION (pfile, cplusplus))
943*38fd1498Szrj 	valid_flags = CXX;
944*38fd1498Szrj     }
945*38fd1498Szrj   if (! (ucnranges[mn].flags & valid_flags))
946*38fd1498Szrj       return 0;
947*38fd1498Szrj   if (CPP_OPTION (pfile, c11_identifiers))
948*38fd1498Szrj     invalid_start_flags = N11;
949*38fd1498Szrj   else if (CPP_OPTION (pfile, c99))
950*38fd1498Szrj     invalid_start_flags = N99;
951*38fd1498Szrj   else
952*38fd1498Szrj     invalid_start_flags = 0;
953*38fd1498Szrj 
954*38fd1498Szrj   /* Update NST.  */
955*38fd1498Szrj   if (ucnranges[mn].combine != 0 && ucnranges[mn].combine < nst->prev_class)
956*38fd1498Szrj     nst->level = normalized_none;
957*38fd1498Szrj   else if (ucnranges[mn].flags & CTX)
958*38fd1498Szrj     {
959*38fd1498Szrj       bool safe;
960*38fd1498Szrj       cppchar_t p = nst->previous;
961*38fd1498Szrj 
962*38fd1498Szrj       /* For Hangul, characters in the range AC00-D7A3 are NFC/NFKC,
963*38fd1498Szrj 	 and are combined algorithmically from a sequence of the form
964*38fd1498Szrj 	 1100-1112 1161-1175 11A8-11C2
965*38fd1498Szrj 	 (if the third is not present, it is treated as 11A7, which is not
966*38fd1498Szrj 	 really a valid character).
967*38fd1498Szrj 	 Unfortunately, C99 allows (only) the NFC form, but C++ allows
968*38fd1498Szrj 	 only the combining characters.  */
969*38fd1498Szrj       if (c >= 0x1161 && c <= 0x1175)
970*38fd1498Szrj 	safe = p < 0x1100 || p > 0x1112;
971*38fd1498Szrj       else if (c >= 0x11A8 && c <= 0x11C2)
972*38fd1498Szrj 	safe = (p < 0xAC00 || p > 0xD7A3 || (p - 0xAC00) % 28 != 0);
973*38fd1498Szrj       else
974*38fd1498Szrj 	safe = check_nfc (pfile, c, p);
975*38fd1498Szrj       if (!safe)
976*38fd1498Szrj 	{
977*38fd1498Szrj 	  if ((c >= 0x1161 && c <= 0x1175) || (c >= 0x11A8 && c <= 0x11C2))
978*38fd1498Szrj 	    nst->level = MAX (nst->level, normalized_identifier_C);
979*38fd1498Szrj 	  else
980*38fd1498Szrj 	    nst->level = normalized_none;
981*38fd1498Szrj 	}
982*38fd1498Szrj     }
983*38fd1498Szrj   else if (ucnranges[mn].flags & NKC)
984*38fd1498Szrj     ;
985*38fd1498Szrj   else if (ucnranges[mn].flags & NFC)
986*38fd1498Szrj     nst->level = MAX (nst->level, normalized_C);
987*38fd1498Szrj   else if (ucnranges[mn].flags & CID)
988*38fd1498Szrj     nst->level = MAX (nst->level, normalized_identifier_C);
989*38fd1498Szrj   else
990*38fd1498Szrj     nst->level = normalized_none;
991*38fd1498Szrj   if (ucnranges[mn].combine == 0)
992*38fd1498Szrj     nst->previous = c;
993*38fd1498Szrj   nst->prev_class = ucnranges[mn].combine;
994*38fd1498Szrj 
995*38fd1498Szrj   /* In C99, UCN digits may not begin identifiers.  In C11 and C++11,
996*38fd1498Szrj      UCN combining characters may not begin identifiers.  */
997*38fd1498Szrj   if (ucnranges[mn].flags & invalid_start_flags)
998*38fd1498Szrj     return 2;
999*38fd1498Szrj 
1000*38fd1498Szrj   return 1;
1001*38fd1498Szrj }
1002*38fd1498Szrj 
1003*38fd1498Szrj /* [lex.charset]: The character designated by the universal character
1004*38fd1498Szrj    name \UNNNNNNNN is that character whose character short name in
1005*38fd1498Szrj    ISO/IEC 10646 is NNNNNNNN; the character designated by the
1006*38fd1498Szrj    universal character name \uNNNN is that character whose character
1007*38fd1498Szrj    short name in ISO/IEC 10646 is 0000NNNN.  If the hexadecimal value
1008*38fd1498Szrj    for a universal character name corresponds to a surrogate code point
1009*38fd1498Szrj    (in the range 0xD800-0xDFFF, inclusive), the program is ill-formed.
1010*38fd1498Szrj    Additionally, if the hexadecimal value for a universal-character-name
1011*38fd1498Szrj    outside a character or string literal corresponds to a control character
1012*38fd1498Szrj    (in either of the ranges 0x00-0x1F or 0x7F-0x9F, both inclusive) or to a
1013*38fd1498Szrj    character in the basic source character set, the program is ill-formed.
1014*38fd1498Szrj 
1015*38fd1498Szrj    C99 6.4.3: A universal character name shall not specify a character
1016*38fd1498Szrj    whose short identifier is less than 00A0 other than 0024 ($), 0040 (@),
1017*38fd1498Szrj    or 0060 (`), nor one in the range D800 through DFFF inclusive.
1018*38fd1498Szrj 
1019*38fd1498Szrj    *PSTR must be preceded by "\u" or "\U"; it is assumed that the
1020*38fd1498Szrj    buffer end is delimited by a non-hex digit.  Returns false if the
1021*38fd1498Szrj    UCN has not been consumed, true otherwise.
1022*38fd1498Szrj 
1023*38fd1498Szrj    The value of the UCN, whether valid or invalid, is returned in *CP.
1024*38fd1498Szrj    Diagnostics are emitted for invalid values.  PSTR is updated to point
1025*38fd1498Szrj    one beyond the UCN, or to the syntactically invalid character.
1026*38fd1498Szrj 
1027*38fd1498Szrj    IDENTIFIER_POS is 0 when not in an identifier, 1 for the start of
1028*38fd1498Szrj    an identifier, or 2 otherwise.
1029*38fd1498Szrj 
1030*38fd1498Szrj    If LOC_READER is non-NULL, then position information is
1031*38fd1498Szrj    read from *LOC_READER and CHAR_RANGE->m_finish is updated accordingly.  */
1032*38fd1498Szrj 
1033*38fd1498Szrj bool
_cpp_valid_ucn(cpp_reader * pfile,const uchar ** pstr,const uchar * limit,int identifier_pos,struct normalize_state * nst,cppchar_t * cp,source_range * char_range,cpp_string_location_reader * loc_reader)1034*38fd1498Szrj _cpp_valid_ucn (cpp_reader *pfile, const uchar **pstr,
1035*38fd1498Szrj 		const uchar *limit, int identifier_pos,
1036*38fd1498Szrj 		struct normalize_state *nst, cppchar_t *cp,
1037*38fd1498Szrj 		source_range *char_range,
1038*38fd1498Szrj 		cpp_string_location_reader *loc_reader)
1039*38fd1498Szrj {
1040*38fd1498Szrj   cppchar_t result, c;
1041*38fd1498Szrj   unsigned int length;
1042*38fd1498Szrj   const uchar *str = *pstr;
1043*38fd1498Szrj   const uchar *base = str - 2;
1044*38fd1498Szrj 
1045*38fd1498Szrj   if (!CPP_OPTION (pfile, cplusplus) && !CPP_OPTION (pfile, c99))
1046*38fd1498Szrj     cpp_error (pfile, CPP_DL_WARNING,
1047*38fd1498Szrj 	       "universal character names are only valid in C++ and C99");
1048*38fd1498Szrj   else if (CPP_OPTION (pfile, cpp_warn_c90_c99_compat) > 0
1049*38fd1498Szrj 	   && !CPP_OPTION (pfile, cplusplus))
1050*38fd1498Szrj     cpp_error (pfile, CPP_DL_WARNING,
1051*38fd1498Szrj 	       "C99's universal character names are incompatible with C90");
1052*38fd1498Szrj   else if (CPP_WTRADITIONAL (pfile) && identifier_pos == 0)
1053*38fd1498Szrj     cpp_warning (pfile, CPP_W_TRADITIONAL,
1054*38fd1498Szrj 	         "the meaning of '\\%c' is different in traditional C",
1055*38fd1498Szrj 	         (int) str[-1]);
1056*38fd1498Szrj 
1057*38fd1498Szrj   if (str[-1] == 'u')
1058*38fd1498Szrj     length = 4;
1059*38fd1498Szrj   else if (str[-1] == 'U')
1060*38fd1498Szrj     length = 8;
1061*38fd1498Szrj   else
1062*38fd1498Szrj     {
1063*38fd1498Szrj       cpp_error (pfile, CPP_DL_ICE, "In _cpp_valid_ucn but not a UCN");
1064*38fd1498Szrj       length = 4;
1065*38fd1498Szrj     }
1066*38fd1498Szrj 
1067*38fd1498Szrj   result = 0;
1068*38fd1498Szrj   do
1069*38fd1498Szrj     {
1070*38fd1498Szrj       c = *str;
1071*38fd1498Szrj       if (!ISXDIGIT (c))
1072*38fd1498Szrj 	break;
1073*38fd1498Szrj       str++;
1074*38fd1498Szrj       if (loc_reader)
1075*38fd1498Szrj 	{
1076*38fd1498Szrj 	  gcc_assert (char_range);
1077*38fd1498Szrj 	  char_range->m_finish = loc_reader->get_next ().m_finish;
1078*38fd1498Szrj 	}
1079*38fd1498Szrj       result = (result << 4) + hex_value (c);
1080*38fd1498Szrj     }
1081*38fd1498Szrj   while (--length && str < limit);
1082*38fd1498Szrj 
1083*38fd1498Szrj   /* Partial UCNs are not valid in strings, but decompose into
1084*38fd1498Szrj      multiple tokens in identifiers, so we can't give a helpful
1085*38fd1498Szrj      error message in that case.  */
1086*38fd1498Szrj   if (length && identifier_pos)
1087*38fd1498Szrj     {
1088*38fd1498Szrj       *cp = 0;
1089*38fd1498Szrj       return false;
1090*38fd1498Szrj     }
1091*38fd1498Szrj 
1092*38fd1498Szrj   *pstr = str;
1093*38fd1498Szrj   if (length)
1094*38fd1498Szrj     {
1095*38fd1498Szrj       cpp_error (pfile, CPP_DL_ERROR,
1096*38fd1498Szrj 		 "incomplete universal character name %.*s",
1097*38fd1498Szrj 		 (int) (str - base), base);
1098*38fd1498Szrj       result = 1;
1099*38fd1498Szrj     }
1100*38fd1498Szrj   /* The C99 standard permits $, @ and ` to be specified as UCNs.  We use
1101*38fd1498Szrj      hex escapes so that this also works with EBCDIC hosts.
1102*38fd1498Szrj      C++0x permits everything below 0xa0 within literals;
1103*38fd1498Szrj      ucn_valid_in_identifier will complain about identifiers.  */
1104*38fd1498Szrj   else if ((result < 0xa0
1105*38fd1498Szrj 	    && !CPP_OPTION (pfile, cplusplus)
1106*38fd1498Szrj 	    && (result != 0x24 && result != 0x40 && result != 0x60))
1107*38fd1498Szrj 	   || (result & 0x80000000)
1108*38fd1498Szrj 	   || (result >= 0xD800 && result <= 0xDFFF))
1109*38fd1498Szrj     {
1110*38fd1498Szrj       cpp_error (pfile, CPP_DL_ERROR,
1111*38fd1498Szrj 		 "%.*s is not a valid universal character",
1112*38fd1498Szrj 		 (int) (str - base), base);
1113*38fd1498Szrj       result = 1;
1114*38fd1498Szrj     }
1115*38fd1498Szrj   else if (identifier_pos && result == 0x24
1116*38fd1498Szrj 	   && CPP_OPTION (pfile, dollars_in_ident))
1117*38fd1498Szrj     {
1118*38fd1498Szrj       if (CPP_OPTION (pfile, warn_dollars) && !pfile->state.skipping)
1119*38fd1498Szrj 	{
1120*38fd1498Szrj 	  CPP_OPTION (pfile, warn_dollars) = 0;
1121*38fd1498Szrj 	  cpp_error (pfile, CPP_DL_PEDWARN, "'$' in identifier or number");
1122*38fd1498Szrj 	}
1123*38fd1498Szrj       NORMALIZE_STATE_UPDATE_IDNUM (nst, result);
1124*38fd1498Szrj     }
1125*38fd1498Szrj   else if (identifier_pos)
1126*38fd1498Szrj     {
1127*38fd1498Szrj       int validity = ucn_valid_in_identifier (pfile, result, nst);
1128*38fd1498Szrj 
1129*38fd1498Szrj       if (validity == 0)
1130*38fd1498Szrj 	cpp_error (pfile, CPP_DL_ERROR,
1131*38fd1498Szrj 		   "universal character %.*s is not valid in an identifier",
1132*38fd1498Szrj 		   (int) (str - base), base);
1133*38fd1498Szrj       else if (validity == 2 && identifier_pos == 1)
1134*38fd1498Szrj 	cpp_error (pfile, CPP_DL_ERROR,
1135*38fd1498Szrj    "universal character %.*s is not valid at the start of an identifier",
1136*38fd1498Szrj 		   (int) (str - base), base);
1137*38fd1498Szrj     }
1138*38fd1498Szrj 
1139*38fd1498Szrj   *cp = result;
1140*38fd1498Szrj   return true;
1141*38fd1498Szrj }
1142*38fd1498Szrj 
1143*38fd1498Szrj /* Convert an UCN, pointed to by FROM, to UTF-8 encoding, then translate
1144*38fd1498Szrj    it to the execution character set and write the result into TBUF,
1145*38fd1498Szrj    if TBUF is non-NULL.
1146*38fd1498Szrj    An advanced pointer is returned.  Issues all relevant diagnostics.
1147*38fd1498Szrj    If LOC_READER is non-NULL, then RANGES must be non-NULL and CHAR_RANGE
1148*38fd1498Szrj    contains the location of the character so far: location information
1149*38fd1498Szrj    is read from *LOC_READER, and *RANGES is updated accordingly.  */
1150*38fd1498Szrj static const uchar *
convert_ucn(cpp_reader * pfile,const uchar * from,const uchar * limit,struct _cpp_strbuf * tbuf,struct cset_converter cvt,source_range char_range,cpp_string_location_reader * loc_reader,cpp_substring_ranges * ranges)1151*38fd1498Szrj convert_ucn (cpp_reader *pfile, const uchar *from, const uchar *limit,
1152*38fd1498Szrj 	     struct _cpp_strbuf *tbuf, struct cset_converter cvt,
1153*38fd1498Szrj 	     source_range char_range,
1154*38fd1498Szrj 	     cpp_string_location_reader *loc_reader,
1155*38fd1498Szrj 	     cpp_substring_ranges *ranges)
1156*38fd1498Szrj {
1157*38fd1498Szrj   cppchar_t ucn;
1158*38fd1498Szrj   uchar buf[6];
1159*38fd1498Szrj   uchar *bufp = buf;
1160*38fd1498Szrj   size_t bytesleft = 6;
1161*38fd1498Szrj   int rval;
1162*38fd1498Szrj   struct normalize_state nst = INITIAL_NORMALIZE_STATE;
1163*38fd1498Szrj 
1164*38fd1498Szrj   /* loc_reader and ranges must either be both NULL, or both be non-NULL.  */
1165*38fd1498Szrj   gcc_assert ((loc_reader != NULL) == (ranges != NULL));
1166*38fd1498Szrj 
1167*38fd1498Szrj   from++;  /* Skip u/U.  */
1168*38fd1498Szrj 
1169*38fd1498Szrj   if (loc_reader)
1170*38fd1498Szrj     /* The u/U is part of the spelling of this character.  */
1171*38fd1498Szrj     char_range.m_finish = loc_reader->get_next ().m_finish;
1172*38fd1498Szrj 
1173*38fd1498Szrj   _cpp_valid_ucn (pfile, &from, limit, 0, &nst,
1174*38fd1498Szrj 		  &ucn, &char_range, loc_reader);
1175*38fd1498Szrj 
1176*38fd1498Szrj   rval = one_cppchar_to_utf8 (ucn, &bufp, &bytesleft);
1177*38fd1498Szrj   if (rval)
1178*38fd1498Szrj     {
1179*38fd1498Szrj       errno = rval;
1180*38fd1498Szrj       cpp_errno (pfile, CPP_DL_ERROR,
1181*38fd1498Szrj 		 "converting UCN to source character set");
1182*38fd1498Szrj     }
1183*38fd1498Szrj   else
1184*38fd1498Szrj     {
1185*38fd1498Szrj       if (tbuf)
1186*38fd1498Szrj 	if (!APPLY_CONVERSION (cvt, buf, 6 - bytesleft, tbuf))
1187*38fd1498Szrj 	  cpp_errno (pfile, CPP_DL_ERROR,
1188*38fd1498Szrj 		     "converting UCN to execution character set");
1189*38fd1498Szrj 
1190*38fd1498Szrj       if (loc_reader)
1191*38fd1498Szrj 	{
1192*38fd1498Szrj 	  int num_encoded_bytes = 6 - bytesleft;
1193*38fd1498Szrj 	  for (int i = 0; i < num_encoded_bytes; i++)
1194*38fd1498Szrj 	    ranges->add_range (char_range);
1195*38fd1498Szrj 	}
1196*38fd1498Szrj     }
1197*38fd1498Szrj 
1198*38fd1498Szrj   return from;
1199*38fd1498Szrj }
1200*38fd1498Szrj 
1201*38fd1498Szrj /* Subroutine of convert_hex and convert_oct.  N is the representation
1202*38fd1498Szrj    in the execution character set of a numeric escape; write it into the
1203*38fd1498Szrj    string buffer TBUF and update the end-of-string pointer therein.  WIDE
1204*38fd1498Szrj    is true if it's a wide string that's being assembled in TBUF.  This
1205*38fd1498Szrj    function issues no diagnostics and never fails.  */
1206*38fd1498Szrj static void
emit_numeric_escape(cpp_reader * pfile,cppchar_t n,struct _cpp_strbuf * tbuf,struct cset_converter cvt)1207*38fd1498Szrj emit_numeric_escape (cpp_reader *pfile, cppchar_t n,
1208*38fd1498Szrj 		     struct _cpp_strbuf *tbuf, struct cset_converter cvt)
1209*38fd1498Szrj {
1210*38fd1498Szrj   size_t width = cvt.width;
1211*38fd1498Szrj 
1212*38fd1498Szrj   if (width != CPP_OPTION (pfile, char_precision))
1213*38fd1498Szrj     {
1214*38fd1498Szrj       /* We have to render this into the target byte order, which may not
1215*38fd1498Szrj 	 be our byte order.  */
1216*38fd1498Szrj       bool bigend = CPP_OPTION (pfile, bytes_big_endian);
1217*38fd1498Szrj       size_t cwidth = CPP_OPTION (pfile, char_precision);
1218*38fd1498Szrj       size_t cmask = width_to_mask (cwidth);
1219*38fd1498Szrj       size_t nbwc = width / cwidth;
1220*38fd1498Szrj       size_t i;
1221*38fd1498Szrj       size_t off = tbuf->len;
1222*38fd1498Szrj       cppchar_t c;
1223*38fd1498Szrj 
1224*38fd1498Szrj       if (tbuf->len + nbwc > tbuf->asize)
1225*38fd1498Szrj 	{
1226*38fd1498Szrj 	  tbuf->asize += OUTBUF_BLOCK_SIZE;
1227*38fd1498Szrj 	  tbuf->text = XRESIZEVEC (uchar, tbuf->text, tbuf->asize);
1228*38fd1498Szrj 	}
1229*38fd1498Szrj 
1230*38fd1498Szrj       for (i = 0; i < nbwc; i++)
1231*38fd1498Szrj 	{
1232*38fd1498Szrj 	  c = n & cmask;
1233*38fd1498Szrj 	  n >>= cwidth;
1234*38fd1498Szrj 	  tbuf->text[off + (bigend ? nbwc - i - 1 : i)] = c;
1235*38fd1498Szrj 	}
1236*38fd1498Szrj       tbuf->len += nbwc;
1237*38fd1498Szrj     }
1238*38fd1498Szrj   else
1239*38fd1498Szrj     {
1240*38fd1498Szrj       /* Note: this code does not handle the case where the target
1241*38fd1498Szrj 	 and host have a different number of bits in a byte.  */
1242*38fd1498Szrj       if (tbuf->len + 1 > tbuf->asize)
1243*38fd1498Szrj 	{
1244*38fd1498Szrj 	  tbuf->asize += OUTBUF_BLOCK_SIZE;
1245*38fd1498Szrj 	  tbuf->text = XRESIZEVEC (uchar, tbuf->text, tbuf->asize);
1246*38fd1498Szrj 	}
1247*38fd1498Szrj       tbuf->text[tbuf->len++] = n;
1248*38fd1498Szrj     }
1249*38fd1498Szrj }
1250*38fd1498Szrj 
1251*38fd1498Szrj /* Convert a hexadecimal escape, pointed to by FROM, to the execution
1252*38fd1498Szrj    character set and write it into the string buffer TBUF (if non-NULL).
1253*38fd1498Szrj    Returns an advanced pointer, and issues diagnostics as necessary.
1254*38fd1498Szrj    No character set translation occurs; this routine always produces the
1255*38fd1498Szrj    execution-set character with numeric value equal to the given hex
1256*38fd1498Szrj    number.  You can, e.g. generate surrogate pairs this way.
1257*38fd1498Szrj    If LOC_READER is non-NULL, then RANGES must be non-NULL and CHAR_RANGE
1258*38fd1498Szrj    contains the location of the character so far: location information
1259*38fd1498Szrj    is read from *LOC_READER, and *RANGES is updated accordingly.  */
1260*38fd1498Szrj static const uchar *
convert_hex(cpp_reader * pfile,const uchar * from,const uchar * limit,struct _cpp_strbuf * tbuf,struct cset_converter cvt,source_range char_range,cpp_string_location_reader * loc_reader,cpp_substring_ranges * ranges)1261*38fd1498Szrj convert_hex (cpp_reader *pfile, const uchar *from, const uchar *limit,
1262*38fd1498Szrj 	     struct _cpp_strbuf *tbuf, struct cset_converter cvt,
1263*38fd1498Szrj 	     source_range char_range,
1264*38fd1498Szrj 	     cpp_string_location_reader *loc_reader,
1265*38fd1498Szrj 	     cpp_substring_ranges *ranges)
1266*38fd1498Szrj {
1267*38fd1498Szrj   cppchar_t c, n = 0, overflow = 0;
1268*38fd1498Szrj   int digits_found = 0;
1269*38fd1498Szrj   size_t width = cvt.width;
1270*38fd1498Szrj   size_t mask = width_to_mask (width);
1271*38fd1498Szrj 
1272*38fd1498Szrj   /* loc_reader and ranges must either be both NULL, or both be non-NULL.  */
1273*38fd1498Szrj   gcc_assert ((loc_reader != NULL) == (ranges != NULL));
1274*38fd1498Szrj 
1275*38fd1498Szrj   if (CPP_WTRADITIONAL (pfile))
1276*38fd1498Szrj     cpp_warning (pfile, CPP_W_TRADITIONAL,
1277*38fd1498Szrj 	         "the meaning of '\\x' is different in traditional C");
1278*38fd1498Szrj 
1279*38fd1498Szrj   /* Skip 'x'.  */
1280*38fd1498Szrj   from++;
1281*38fd1498Szrj 
1282*38fd1498Szrj   /* The 'x' is part of the spelling of this character.  */
1283*38fd1498Szrj   if (loc_reader)
1284*38fd1498Szrj     char_range.m_finish = loc_reader->get_next ().m_finish;
1285*38fd1498Szrj 
1286*38fd1498Szrj   while (from < limit)
1287*38fd1498Szrj     {
1288*38fd1498Szrj       c = *from;
1289*38fd1498Szrj       if (! hex_p (c))
1290*38fd1498Szrj 	break;
1291*38fd1498Szrj       from++;
1292*38fd1498Szrj       if (loc_reader)
1293*38fd1498Szrj 	char_range.m_finish = loc_reader->get_next ().m_finish;
1294*38fd1498Szrj       overflow |= n ^ (n << 4 >> 4);
1295*38fd1498Szrj       n = (n << 4) + hex_value (c);
1296*38fd1498Szrj       digits_found = 1;
1297*38fd1498Szrj     }
1298*38fd1498Szrj 
1299*38fd1498Szrj   if (!digits_found)
1300*38fd1498Szrj     {
1301*38fd1498Szrj       cpp_error (pfile, CPP_DL_ERROR,
1302*38fd1498Szrj 		 "\\x used with no following hex digits");
1303*38fd1498Szrj       return from;
1304*38fd1498Szrj     }
1305*38fd1498Szrj 
1306*38fd1498Szrj   if (overflow | (n != (n & mask)))
1307*38fd1498Szrj     {
1308*38fd1498Szrj       cpp_error (pfile, CPP_DL_PEDWARN,
1309*38fd1498Szrj 		 "hex escape sequence out of range");
1310*38fd1498Szrj       n &= mask;
1311*38fd1498Szrj     }
1312*38fd1498Szrj 
1313*38fd1498Szrj   if (tbuf)
1314*38fd1498Szrj     emit_numeric_escape (pfile, n, tbuf, cvt);
1315*38fd1498Szrj   if (ranges)
1316*38fd1498Szrj     ranges->add_range (char_range);
1317*38fd1498Szrj 
1318*38fd1498Szrj   return from;
1319*38fd1498Szrj }
1320*38fd1498Szrj 
1321*38fd1498Szrj /* Convert an octal escape, pointed to by FROM, to the execution
1322*38fd1498Szrj    character set and write it into the string buffer TBUF.  Returns an
1323*38fd1498Szrj    advanced pointer, and issues diagnostics as necessary.
1324*38fd1498Szrj    No character set translation occurs; this routine always produces the
1325*38fd1498Szrj    execution-set character with numeric value equal to the given octal
1326*38fd1498Szrj    number.
1327*38fd1498Szrj    If LOC_READER is non-NULL, then RANGES must be non-NULL and CHAR_RANGE
1328*38fd1498Szrj    contains the location of the character so far: location information
1329*38fd1498Szrj    is read from *LOC_READER, and *RANGES is updated accordingly.  */
1330*38fd1498Szrj static const uchar *
convert_oct(cpp_reader * pfile,const uchar * from,const uchar * limit,struct _cpp_strbuf * tbuf,struct cset_converter cvt,source_range char_range,cpp_string_location_reader * loc_reader,cpp_substring_ranges * ranges)1331*38fd1498Szrj convert_oct (cpp_reader *pfile, const uchar *from, const uchar *limit,
1332*38fd1498Szrj 	     struct _cpp_strbuf *tbuf, struct cset_converter cvt,
1333*38fd1498Szrj 	     source_range char_range,
1334*38fd1498Szrj 	     cpp_string_location_reader *loc_reader,
1335*38fd1498Szrj 	     cpp_substring_ranges *ranges)
1336*38fd1498Szrj {
1337*38fd1498Szrj   size_t count = 0;
1338*38fd1498Szrj   cppchar_t c, n = 0;
1339*38fd1498Szrj   size_t width = cvt.width;
1340*38fd1498Szrj   size_t mask = width_to_mask (width);
1341*38fd1498Szrj   bool overflow = false;
1342*38fd1498Szrj 
1343*38fd1498Szrj   /* loc_reader and ranges must either be both NULL, or both be non-NULL.  */
1344*38fd1498Szrj   gcc_assert ((loc_reader != NULL) == (ranges != NULL));
1345*38fd1498Szrj 
1346*38fd1498Szrj   while (from < limit && count++ < 3)
1347*38fd1498Szrj     {
1348*38fd1498Szrj       c = *from;
1349*38fd1498Szrj       if (c < '0' || c > '7')
1350*38fd1498Szrj 	break;
1351*38fd1498Szrj       from++;
1352*38fd1498Szrj       if (loc_reader)
1353*38fd1498Szrj 	char_range.m_finish = loc_reader->get_next ().m_finish;
1354*38fd1498Szrj       overflow |= n ^ (n << 3 >> 3);
1355*38fd1498Szrj       n = (n << 3) + c - '0';
1356*38fd1498Szrj     }
1357*38fd1498Szrj 
1358*38fd1498Szrj   if (n != (n & mask))
1359*38fd1498Szrj     {
1360*38fd1498Szrj       cpp_error (pfile, CPP_DL_PEDWARN,
1361*38fd1498Szrj 		 "octal escape sequence out of range");
1362*38fd1498Szrj       n &= mask;
1363*38fd1498Szrj     }
1364*38fd1498Szrj 
1365*38fd1498Szrj   if (tbuf)
1366*38fd1498Szrj     emit_numeric_escape (pfile, n, tbuf, cvt);
1367*38fd1498Szrj   if (ranges)
1368*38fd1498Szrj     ranges->add_range (char_range);
1369*38fd1498Szrj 
1370*38fd1498Szrj   return from;
1371*38fd1498Szrj }
1372*38fd1498Szrj 
1373*38fd1498Szrj /* Convert an escape sequence (pointed to by FROM) to its value on
1374*38fd1498Szrj    the target, and to the execution character set.  Do not scan past
1375*38fd1498Szrj    LIMIT.  Write the converted value into TBUF, if TBUF is non-NULL.
1376*38fd1498Szrj    Returns an advanced pointer.  Handles all relevant diagnostics.
1377*38fd1498Szrj    If LOC_READER is non-NULL, then RANGES must be non-NULL: location
1378*38fd1498Szrj    information is read from *LOC_READER, and *RANGES is updated
1379*38fd1498Szrj    accordingly.  */
1380*38fd1498Szrj static const uchar *
convert_escape(cpp_reader * pfile,const uchar * from,const uchar * limit,struct _cpp_strbuf * tbuf,struct cset_converter cvt,cpp_string_location_reader * loc_reader,cpp_substring_ranges * ranges)1381*38fd1498Szrj convert_escape (cpp_reader *pfile, const uchar *from, const uchar *limit,
1382*38fd1498Szrj 		struct _cpp_strbuf *tbuf, struct cset_converter cvt,
1383*38fd1498Szrj 		cpp_string_location_reader *loc_reader,
1384*38fd1498Szrj 		cpp_substring_ranges *ranges)
1385*38fd1498Szrj {
1386*38fd1498Szrj   /* Values of \a \b \e \f \n \r \t \v respectively.  */
1387*38fd1498Szrj #if HOST_CHARSET == HOST_CHARSET_ASCII
1388*38fd1498Szrj   static const uchar charconsts[] = {  7,  8, 27, 12, 10, 13,  9, 11 };
1389*38fd1498Szrj #elif HOST_CHARSET == HOST_CHARSET_EBCDIC
1390*38fd1498Szrj   static const uchar charconsts[] = { 47, 22, 39, 12, 21, 13,  5, 11 };
1391*38fd1498Szrj #else
1392*38fd1498Szrj #error "unknown host character set"
1393*38fd1498Szrj #endif
1394*38fd1498Szrj 
1395*38fd1498Szrj   uchar c;
1396*38fd1498Szrj 
1397*38fd1498Szrj   /* Record the location of the backslash.  */
1398*38fd1498Szrj   source_range char_range;
1399*38fd1498Szrj   if (loc_reader)
1400*38fd1498Szrj     char_range = loc_reader->get_next ();
1401*38fd1498Szrj 
1402*38fd1498Szrj   c = *from;
1403*38fd1498Szrj   switch (c)
1404*38fd1498Szrj     {
1405*38fd1498Szrj       /* UCNs, hex escapes, and octal escapes are processed separately.  */
1406*38fd1498Szrj     case 'u': case 'U':
1407*38fd1498Szrj       return convert_ucn (pfile, from, limit, tbuf, cvt,
1408*38fd1498Szrj 			  char_range, loc_reader, ranges);
1409*38fd1498Szrj 
1410*38fd1498Szrj     case 'x':
1411*38fd1498Szrj       return convert_hex (pfile, from, limit, tbuf, cvt,
1412*38fd1498Szrj 			  char_range, loc_reader, ranges);
1413*38fd1498Szrj       break;
1414*38fd1498Szrj 
1415*38fd1498Szrj     case '0':  case '1':  case '2':  case '3':
1416*38fd1498Szrj     case '4':  case '5':  case '6':  case '7':
1417*38fd1498Szrj       return convert_oct (pfile, from, limit, tbuf, cvt,
1418*38fd1498Szrj 			  char_range, loc_reader, ranges);
1419*38fd1498Szrj 
1420*38fd1498Szrj       /* Various letter escapes.  Get the appropriate host-charset
1421*38fd1498Szrj 	 value into C.  */
1422*38fd1498Szrj     case '\\': case '\'': case '"': case '?': break;
1423*38fd1498Szrj 
1424*38fd1498Szrj     case '(': case '{': case '[': case '%':
1425*38fd1498Szrj       /* '\(', etc, can be used at the beginning of a line in a long
1426*38fd1498Szrj 	 string split onto multiple lines with \-newline, to prevent
1427*38fd1498Szrj 	 Emacs or other text editors from getting confused.  '\%' can
1428*38fd1498Szrj 	 be used to prevent SCCS from mangling printf format strings.  */
1429*38fd1498Szrj       if (CPP_PEDANTIC (pfile))
1430*38fd1498Szrj 	goto unknown;
1431*38fd1498Szrj       break;
1432*38fd1498Szrj 
1433*38fd1498Szrj     case 'b': c = charconsts[1];  break;
1434*38fd1498Szrj     case 'f': c = charconsts[3];  break;
1435*38fd1498Szrj     case 'n': c = charconsts[4];  break;
1436*38fd1498Szrj     case 'r': c = charconsts[5];  break;
1437*38fd1498Szrj     case 't': c = charconsts[6];  break;
1438*38fd1498Szrj     case 'v': c = charconsts[7];  break;
1439*38fd1498Szrj 
1440*38fd1498Szrj     case 'a':
1441*38fd1498Szrj       if (CPP_WTRADITIONAL (pfile))
1442*38fd1498Szrj 	cpp_warning (pfile, CPP_W_TRADITIONAL,
1443*38fd1498Szrj 		     "the meaning of '\\a' is different in traditional C");
1444*38fd1498Szrj       c = charconsts[0];
1445*38fd1498Szrj       break;
1446*38fd1498Szrj 
1447*38fd1498Szrj     case 'e': case 'E':
1448*38fd1498Szrj       if (CPP_PEDANTIC (pfile))
1449*38fd1498Szrj 	cpp_error (pfile, CPP_DL_PEDWARN,
1450*38fd1498Szrj 		   "non-ISO-standard escape sequence, '\\%c'", (int) c);
1451*38fd1498Szrj       c = charconsts[2];
1452*38fd1498Szrj       break;
1453*38fd1498Szrj 
1454*38fd1498Szrj     default:
1455*38fd1498Szrj     unknown:
1456*38fd1498Szrj       if (ISGRAPH (c))
1457*38fd1498Szrj 	cpp_error (pfile, CPP_DL_PEDWARN,
1458*38fd1498Szrj 		   "unknown escape sequence: '\\%c'", (int) c);
1459*38fd1498Szrj       else
1460*38fd1498Szrj 	{
1461*38fd1498Szrj 	  /* diagnostic.c does not support "%03o".  When it does, this
1462*38fd1498Szrj 	     code can use %03o directly in the diagnostic again.  */
1463*38fd1498Szrj 	  char buf[32];
1464*38fd1498Szrj 	  sprintf(buf, "%03o", (int) c);
1465*38fd1498Szrj 	  cpp_error (pfile, CPP_DL_PEDWARN,
1466*38fd1498Szrj 		     "unknown escape sequence: '\\%s'", buf);
1467*38fd1498Szrj 	}
1468*38fd1498Szrj     }
1469*38fd1498Szrj 
1470*38fd1498Szrj   if (tbuf)
1471*38fd1498Szrj     /* Now convert what we have to the execution character set.  */
1472*38fd1498Szrj     if (!APPLY_CONVERSION (cvt, &c, 1, tbuf))
1473*38fd1498Szrj       cpp_errno (pfile, CPP_DL_ERROR,
1474*38fd1498Szrj 		 "converting escape sequence to execution character set");
1475*38fd1498Szrj 
1476*38fd1498Szrj   if (loc_reader)
1477*38fd1498Szrj     {
1478*38fd1498Szrj       char_range.m_finish = loc_reader->get_next ().m_finish;
1479*38fd1498Szrj       ranges->add_range (char_range);
1480*38fd1498Szrj     }
1481*38fd1498Szrj 
1482*38fd1498Szrj   return from + 1;
1483*38fd1498Szrj }
1484*38fd1498Szrj 
1485*38fd1498Szrj /* TYPE is a token type.  The return value is the conversion needed to
1486*38fd1498Szrj    convert from source to execution character set for the given type. */
1487*38fd1498Szrj static struct cset_converter
converter_for_type(cpp_reader * pfile,enum cpp_ttype type)1488*38fd1498Szrj converter_for_type (cpp_reader *pfile, enum cpp_ttype type)
1489*38fd1498Szrj {
1490*38fd1498Szrj   switch (type)
1491*38fd1498Szrj     {
1492*38fd1498Szrj     default:
1493*38fd1498Szrj 	return pfile->narrow_cset_desc;
1494*38fd1498Szrj     case CPP_UTF8CHAR:
1495*38fd1498Szrj     case CPP_UTF8STRING:
1496*38fd1498Szrj 	return pfile->utf8_cset_desc;
1497*38fd1498Szrj     case CPP_CHAR16:
1498*38fd1498Szrj     case CPP_STRING16:
1499*38fd1498Szrj 	return pfile->char16_cset_desc;
1500*38fd1498Szrj     case CPP_CHAR32:
1501*38fd1498Szrj     case CPP_STRING32:
1502*38fd1498Szrj 	return pfile->char32_cset_desc;
1503*38fd1498Szrj     case CPP_WCHAR:
1504*38fd1498Szrj     case CPP_WSTRING:
1505*38fd1498Szrj 	return pfile->wide_cset_desc;
1506*38fd1498Szrj     }
1507*38fd1498Szrj }
1508*38fd1498Szrj 
1509*38fd1498Szrj /* FROM is an array of cpp_string structures of length COUNT.  These
1510*38fd1498Szrj    are to be converted from the source to the execution character set,
1511*38fd1498Szrj    escape sequences translated, and finally all are to be
1512*38fd1498Szrj    concatenated.  WIDE indicates whether or not to produce a wide
1513*38fd1498Szrj    string.  If TO is non-NULL, the result is written into TO.
1514*38fd1498Szrj    If LOC_READERS and OUT are non-NULL, then location information
1515*38fd1498Szrj    is read from LOC_READERS (which must be an array of length COUNT),
1516*38fd1498Szrj    and location information is written to *RANGES.
1517*38fd1498Szrj 
1518*38fd1498Szrj    Returns true for success, false for failure.  */
1519*38fd1498Szrj 
1520*38fd1498Szrj static bool
cpp_interpret_string_1(cpp_reader * pfile,const cpp_string * from,size_t count,cpp_string * to,enum cpp_ttype type,cpp_string_location_reader * loc_readers,cpp_substring_ranges * out)1521*38fd1498Szrj cpp_interpret_string_1 (cpp_reader *pfile, const cpp_string *from, size_t count,
1522*38fd1498Szrj 			cpp_string *to,  enum cpp_ttype type,
1523*38fd1498Szrj 			cpp_string_location_reader *loc_readers,
1524*38fd1498Szrj 			cpp_substring_ranges *out)
1525*38fd1498Szrj {
1526*38fd1498Szrj   struct _cpp_strbuf tbuf;
1527*38fd1498Szrj   const uchar *p, *base, *limit;
1528*38fd1498Szrj   size_t i;
1529*38fd1498Szrj   struct cset_converter cvt = converter_for_type (pfile, type);
1530*38fd1498Szrj 
1531*38fd1498Szrj   /* loc_readers and out must either be both NULL, or both be non-NULL.  */
1532*38fd1498Szrj   gcc_assert ((loc_readers != NULL) == (out != NULL));
1533*38fd1498Szrj 
1534*38fd1498Szrj   if (to)
1535*38fd1498Szrj     {
1536*38fd1498Szrj       tbuf.asize = MAX (OUTBUF_BLOCK_SIZE, from->len);
1537*38fd1498Szrj       tbuf.text = XNEWVEC (uchar, tbuf.asize);
1538*38fd1498Szrj       tbuf.len = 0;
1539*38fd1498Szrj     }
1540*38fd1498Szrj 
1541*38fd1498Szrj   cpp_string_location_reader *loc_reader = NULL;
1542*38fd1498Szrj   for (i = 0; i < count; i++)
1543*38fd1498Szrj     {
1544*38fd1498Szrj       if (loc_readers)
1545*38fd1498Szrj 	loc_reader = &loc_readers[i];
1546*38fd1498Szrj 
1547*38fd1498Szrj       p = from[i].text;
1548*38fd1498Szrj       if (*p == 'u')
1549*38fd1498Szrj 	{
1550*38fd1498Szrj 	  p++;
1551*38fd1498Szrj 	  if (loc_reader)
1552*38fd1498Szrj 	    loc_reader->get_next ();
1553*38fd1498Szrj 	  if (*p == '8')
1554*38fd1498Szrj 	    {
1555*38fd1498Szrj 	      p++;
1556*38fd1498Szrj 	      if (loc_reader)
1557*38fd1498Szrj 		loc_reader->get_next ();
1558*38fd1498Szrj 	    }
1559*38fd1498Szrj 	}
1560*38fd1498Szrj       else if (*p == 'L' || *p == 'U') p++;
1561*38fd1498Szrj       if (*p == 'R')
1562*38fd1498Szrj 	{
1563*38fd1498Szrj 	  const uchar *prefix;
1564*38fd1498Szrj 
1565*38fd1498Szrj 	  /* Skip over 'R"'.  */
1566*38fd1498Szrj 	  p += 2;
1567*38fd1498Szrj 	  if (loc_reader)
1568*38fd1498Szrj 	    {
1569*38fd1498Szrj 	      loc_reader->get_next ();
1570*38fd1498Szrj 	      loc_reader->get_next ();
1571*38fd1498Szrj 	    }
1572*38fd1498Szrj 	  prefix = p;
1573*38fd1498Szrj 	  while (*p != '(')
1574*38fd1498Szrj 	    {
1575*38fd1498Szrj 	      p++;
1576*38fd1498Szrj 	      if (loc_reader)
1577*38fd1498Szrj 		loc_reader->get_next ();
1578*38fd1498Szrj 	    }
1579*38fd1498Szrj 	  p++;
1580*38fd1498Szrj 	  if (loc_reader)
1581*38fd1498Szrj 	    loc_reader->get_next ();
1582*38fd1498Szrj 	  limit = from[i].text + from[i].len;
1583*38fd1498Szrj 	  if (limit >= p + (p - prefix) + 1)
1584*38fd1498Szrj 	    limit -= (p - prefix) + 1;
1585*38fd1498Szrj 
1586*38fd1498Szrj 	  /* Raw strings are all normal characters; these can be fed
1587*38fd1498Szrj 	     directly to convert_cset.  */
1588*38fd1498Szrj 	  if (to)
1589*38fd1498Szrj 	    if (!APPLY_CONVERSION (cvt, p, limit - p, &tbuf))
1590*38fd1498Szrj 	      goto fail;
1591*38fd1498Szrj 
1592*38fd1498Szrj 	  if (loc_reader)
1593*38fd1498Szrj 	    {
1594*38fd1498Szrj 	      /* If generating source ranges, assume we have a 1:1
1595*38fd1498Szrj 		 correspondence between bytes in the source encoding and bytes
1596*38fd1498Szrj 		 in the execution encoding (e.g. if we have a UTF-8 to UTF-8
1597*38fd1498Szrj 		 conversion), so that this run of bytes in the source file
1598*38fd1498Szrj 		 corresponds to a run of bytes in the execution string.
1599*38fd1498Szrj 		 This requirement is guaranteed by an early-reject in
1600*38fd1498Szrj 		 cpp_interpret_string_ranges.  */
1601*38fd1498Szrj 	      gcc_assert (cvt.func == convert_no_conversion);
1602*38fd1498Szrj 	      out->add_n_ranges (limit - p, *loc_reader);
1603*38fd1498Szrj 	    }
1604*38fd1498Szrj 
1605*38fd1498Szrj 	  continue;
1606*38fd1498Szrj 	}
1607*38fd1498Szrj 
1608*38fd1498Szrj       /* If we don't now have a leading quote, something has gone wrong.
1609*38fd1498Szrj 	 This can occur if cpp_interpret_string_ranges is handling a
1610*38fd1498Szrj 	 stringified macro argument, but should not be possible otherwise.  */
1611*38fd1498Szrj       if (*p != '"' && *p != '\'')
1612*38fd1498Szrj 	{
1613*38fd1498Szrj 	  gcc_assert (out != NULL);
1614*38fd1498Szrj 	  cpp_error (pfile, CPP_DL_ERROR, "missing open quote");
1615*38fd1498Szrj 	  if (to)
1616*38fd1498Szrj 	    free (tbuf.text);
1617*38fd1498Szrj 	  return false;
1618*38fd1498Szrj 	}
1619*38fd1498Szrj 
1620*38fd1498Szrj       /* Skip leading quote.  */
1621*38fd1498Szrj       p++;
1622*38fd1498Szrj       if (loc_reader)
1623*38fd1498Szrj 	loc_reader->get_next ();
1624*38fd1498Szrj 
1625*38fd1498Szrj       limit = from[i].text + from[i].len - 1; /* Skip trailing quote.  */
1626*38fd1498Szrj 
1627*38fd1498Szrj       for (;;)
1628*38fd1498Szrj 	{
1629*38fd1498Szrj 	  base = p;
1630*38fd1498Szrj 	  while (p < limit && *p != '\\')
1631*38fd1498Szrj 	    p++;
1632*38fd1498Szrj 	  if (p > base)
1633*38fd1498Szrj 	    {
1634*38fd1498Szrj 	      /* We have a run of normal characters; these can be fed
1635*38fd1498Szrj 		 directly to convert_cset.  */
1636*38fd1498Szrj 	      if (to)
1637*38fd1498Szrj 		if (!APPLY_CONVERSION (cvt, base, p - base, &tbuf))
1638*38fd1498Szrj 		  goto fail;
1639*38fd1498Szrj 	    /* Similar to above: assumes we have a 1:1 correspondence
1640*38fd1498Szrj 	       between bytes in the source encoding and bytes in the
1641*38fd1498Szrj 	       execution encoding.  */
1642*38fd1498Szrj 	      if (loc_reader)
1643*38fd1498Szrj 		{
1644*38fd1498Szrj 		  gcc_assert (cvt.func == convert_no_conversion);
1645*38fd1498Szrj 		  out->add_n_ranges (p - base, *loc_reader);
1646*38fd1498Szrj 		}
1647*38fd1498Szrj 	    }
1648*38fd1498Szrj 	  if (p >= limit)
1649*38fd1498Szrj 	    break;
1650*38fd1498Szrj 
1651*38fd1498Szrj 	  struct _cpp_strbuf *tbuf_ptr = to ? &tbuf : NULL;
1652*38fd1498Szrj 	  p = convert_escape (pfile, p + 1, limit, tbuf_ptr, cvt,
1653*38fd1498Szrj 			      loc_reader, out);
1654*38fd1498Szrj 	}
1655*38fd1498Szrj     }
1656*38fd1498Szrj 
1657*38fd1498Szrj   if (to)
1658*38fd1498Szrj     {
1659*38fd1498Szrj       /* NUL-terminate the 'to' buffer and translate it to a cpp_string
1660*38fd1498Szrj 	 structure.  */
1661*38fd1498Szrj       emit_numeric_escape (pfile, 0, &tbuf, cvt);
1662*38fd1498Szrj       tbuf.text = XRESIZEVEC (uchar, tbuf.text, tbuf.len);
1663*38fd1498Szrj       to->text = tbuf.text;
1664*38fd1498Szrj       to->len = tbuf.len;
1665*38fd1498Szrj     }
1666*38fd1498Szrj   /* Use the location of the trailing quote as the location of the
1667*38fd1498Szrj      NUL-terminator.  */
1668*38fd1498Szrj   if (loc_reader)
1669*38fd1498Szrj     {
1670*38fd1498Szrj       source_range range = loc_reader->get_next ();
1671*38fd1498Szrj       out->add_range (range);
1672*38fd1498Szrj     }
1673*38fd1498Szrj 
1674*38fd1498Szrj   return true;
1675*38fd1498Szrj 
1676*38fd1498Szrj  fail:
1677*38fd1498Szrj   cpp_errno (pfile, CPP_DL_ERROR, "converting to execution character set");
1678*38fd1498Szrj   if (to)
1679*38fd1498Szrj     free (tbuf.text);
1680*38fd1498Szrj   return false;
1681*38fd1498Szrj }
1682*38fd1498Szrj 
1683*38fd1498Szrj /* FROM is an array of cpp_string structures of length COUNT.  These
1684*38fd1498Szrj    are to be converted from the source to the execution character set,
1685*38fd1498Szrj    escape sequences translated, and finally all are to be
1686*38fd1498Szrj    concatenated.  WIDE indicates whether or not to produce a wide
1687*38fd1498Szrj    string.  The result is written into TO.  Returns true for success,
1688*38fd1498Szrj    false for failure.  */
1689*38fd1498Szrj bool
cpp_interpret_string(cpp_reader * pfile,const cpp_string * from,size_t count,cpp_string * to,enum cpp_ttype type)1690*38fd1498Szrj cpp_interpret_string (cpp_reader *pfile, const cpp_string *from, size_t count,
1691*38fd1498Szrj 		      cpp_string *to,  enum cpp_ttype type)
1692*38fd1498Szrj {
1693*38fd1498Szrj   return cpp_interpret_string_1 (pfile, from, count, to, type, NULL, NULL);
1694*38fd1498Szrj }
1695*38fd1498Szrj 
1696*38fd1498Szrj /* A "do nothing" error-handling callback for use by
1697*38fd1498Szrj    cpp_interpret_string_ranges, so that it can temporarily suppress
1698*38fd1498Szrj    error-handling.  */
1699*38fd1498Szrj 
1700*38fd1498Szrj static bool
noop_error_cb(cpp_reader *,int,int,rich_location *,const char *,va_list *)1701*38fd1498Szrj noop_error_cb (cpp_reader *, int, int, rich_location *,
1702*38fd1498Szrj 	       const char *, va_list *)
1703*38fd1498Szrj {
1704*38fd1498Szrj   /* no-op.  */
1705*38fd1498Szrj   return true;
1706*38fd1498Szrj }
1707*38fd1498Szrj 
1708*38fd1498Szrj /* This function mimics the behavior of cpp_interpret_string, but
1709*38fd1498Szrj    rather than generating a string in the execution character set,
1710*38fd1498Szrj    *OUT is written to with the source code ranges of the characters
1711*38fd1498Szrj    in such a string.
1712*38fd1498Szrj    FROM and LOC_READERS should both be arrays of length COUNT.
1713*38fd1498Szrj    Returns NULL for success, or an error message for failure.  */
1714*38fd1498Szrj 
1715*38fd1498Szrj const char *
cpp_interpret_string_ranges(cpp_reader * pfile,const cpp_string * from,cpp_string_location_reader * loc_readers,size_t count,cpp_substring_ranges * out,enum cpp_ttype type)1716*38fd1498Szrj cpp_interpret_string_ranges (cpp_reader *pfile, const cpp_string *from,
1717*38fd1498Szrj 			     cpp_string_location_reader *loc_readers,
1718*38fd1498Szrj 			     size_t count,
1719*38fd1498Szrj 			     cpp_substring_ranges *out,
1720*38fd1498Szrj 			     enum cpp_ttype type)
1721*38fd1498Szrj {
1722*38fd1498Szrj   /* There are a couple of cases in the range-handling in
1723*38fd1498Szrj      cpp_interpret_string_1 that rely on there being a 1:1 correspondence
1724*38fd1498Szrj      between bytes in the source encoding and bytes in the execution
1725*38fd1498Szrj      encoding, so that each byte in the execution string can correspond
1726*38fd1498Szrj      to the location of a byte in the source string.
1727*38fd1498Szrj 
1728*38fd1498Szrj      This holds for the typical case of a UTF-8 to UTF-8 conversion.
1729*38fd1498Szrj      Enforce this requirement by only attempting to track substring
1730*38fd1498Szrj      locations if we have source encoding == execution encoding.
1731*38fd1498Szrj 
1732*38fd1498Szrj      This is a stronger condition than we need, since we could e.g.
1733*38fd1498Szrj      have ASCII to EBCDIC (with 1 byte per character before and after),
1734*38fd1498Szrj      but it seems to be a reasonable restriction.  */
1735*38fd1498Szrj   struct cset_converter cvt = converter_for_type (pfile, type);
1736*38fd1498Szrj   if (cvt.func != convert_no_conversion)
1737*38fd1498Szrj     return "execution character set != source character set";
1738*38fd1498Szrj 
1739*38fd1498Szrj   /* For on-demand strings we have already lexed the strings, so there
1740*38fd1498Szrj      should be no errors.  However, if we have bogus source location
1741*38fd1498Szrj      data (or stringified macro arguments), the attempt to lex the
1742*38fd1498Szrj      strings could fail with an error.  Temporarily install an
1743*38fd1498Szrj      error-handler to catch the error, so that it can lead to this call
1744*38fd1498Szrj      failing, rather than being emitted as a user-visible diagnostic.
1745*38fd1498Szrj      If an error does occur, we should see it via the return value of
1746*38fd1498Szrj      cpp_interpret_string_1.  */
1747*38fd1498Szrj   bool (*saved_error_handler) (cpp_reader *, int, int, rich_location *,
1748*38fd1498Szrj 			       const char *, va_list *)
1749*38fd1498Szrj     ATTRIBUTE_FPTR_PRINTF(5,0);
1750*38fd1498Szrj 
1751*38fd1498Szrj   saved_error_handler = pfile->cb.error;
1752*38fd1498Szrj   pfile->cb.error = noop_error_cb;
1753*38fd1498Szrj 
1754*38fd1498Szrj   bool result = cpp_interpret_string_1 (pfile, from, count, NULL, type,
1755*38fd1498Szrj 					loc_readers, out);
1756*38fd1498Szrj 
1757*38fd1498Szrj   /* Restore the saved error-handler.  */
1758*38fd1498Szrj   pfile->cb.error = saved_error_handler;
1759*38fd1498Szrj 
1760*38fd1498Szrj   if (!result)
1761*38fd1498Szrj     return "cpp_interpret_string_1 failed";
1762*38fd1498Szrj 
1763*38fd1498Szrj   /* Success.  */
1764*38fd1498Szrj   return NULL;
1765*38fd1498Szrj }
1766*38fd1498Szrj 
1767*38fd1498Szrj /* Subroutine of do_line and do_linemarker.  Convert escape sequences
1768*38fd1498Szrj    in a string, but do not perform character set conversion.  */
1769*38fd1498Szrj bool
cpp_interpret_string_notranslate(cpp_reader * pfile,const cpp_string * from,size_t count,cpp_string * to,enum cpp_ttype type ATTRIBUTE_UNUSED)1770*38fd1498Szrj cpp_interpret_string_notranslate (cpp_reader *pfile, const cpp_string *from,
1771*38fd1498Szrj 				  size_t count,	cpp_string *to,
1772*38fd1498Szrj 				  enum cpp_ttype type ATTRIBUTE_UNUSED)
1773*38fd1498Szrj {
1774*38fd1498Szrj   struct cset_converter save_narrow_cset_desc = pfile->narrow_cset_desc;
1775*38fd1498Szrj   bool retval;
1776*38fd1498Szrj 
1777*38fd1498Szrj   pfile->narrow_cset_desc.func = convert_no_conversion;
1778*38fd1498Szrj   pfile->narrow_cset_desc.cd = (iconv_t) -1;
1779*38fd1498Szrj   pfile->narrow_cset_desc.width = CPP_OPTION (pfile, char_precision);
1780*38fd1498Szrj 
1781*38fd1498Szrj   retval = cpp_interpret_string (pfile, from, count, to, CPP_STRING);
1782*38fd1498Szrj 
1783*38fd1498Szrj   pfile->narrow_cset_desc = save_narrow_cset_desc;
1784*38fd1498Szrj   return retval;
1785*38fd1498Szrj }
1786*38fd1498Szrj 
1787*38fd1498Szrj 
1788*38fd1498Szrj /* Subroutine of cpp_interpret_charconst which performs the conversion
1789*38fd1498Szrj    to a number, for narrow strings.  STR is the string structure returned
1790*38fd1498Szrj    by cpp_interpret_string.  PCHARS_SEEN and UNSIGNEDP are as for
1791*38fd1498Szrj    cpp_interpret_charconst.  */
1792*38fd1498Szrj static cppchar_t
narrow_str_to_charconst(cpp_reader * pfile,cpp_string str,unsigned int * pchars_seen,int * unsignedp)1793*38fd1498Szrj narrow_str_to_charconst (cpp_reader *pfile, cpp_string str,
1794*38fd1498Szrj 			 unsigned int *pchars_seen, int *unsignedp)
1795*38fd1498Szrj {
1796*38fd1498Szrj   size_t width = CPP_OPTION (pfile, char_precision);
1797*38fd1498Szrj   size_t max_chars = CPP_OPTION (pfile, int_precision) / width;
1798*38fd1498Szrj   size_t mask = width_to_mask (width);
1799*38fd1498Szrj   size_t i;
1800*38fd1498Szrj   cppchar_t result, c;
1801*38fd1498Szrj   bool unsigned_p;
1802*38fd1498Szrj 
1803*38fd1498Szrj   /* The value of a multi-character character constant, or a
1804*38fd1498Szrj      single-character character constant whose representation in the
1805*38fd1498Szrj      execution character set is more than one byte long, is
1806*38fd1498Szrj      implementation defined.  This implementation defines it to be the
1807*38fd1498Szrj      number formed by interpreting the byte sequence in memory as a
1808*38fd1498Szrj      big-endian binary number.  If overflow occurs, the high bytes are
1809*38fd1498Szrj      lost, and a warning is issued.
1810*38fd1498Szrj 
1811*38fd1498Szrj      We don't want to process the NUL terminator handed back by
1812*38fd1498Szrj      cpp_interpret_string.  */
1813*38fd1498Szrj   result = 0;
1814*38fd1498Szrj   for (i = 0; i < str.len - 1; i++)
1815*38fd1498Szrj     {
1816*38fd1498Szrj       c = str.text[i] & mask;
1817*38fd1498Szrj       if (width < BITS_PER_CPPCHAR_T)
1818*38fd1498Szrj 	result = (result << width) | c;
1819*38fd1498Szrj       else
1820*38fd1498Szrj 	result = c;
1821*38fd1498Szrj     }
1822*38fd1498Szrj 
1823*38fd1498Szrj   if (i > max_chars)
1824*38fd1498Szrj     {
1825*38fd1498Szrj       i = max_chars;
1826*38fd1498Szrj       cpp_error (pfile, CPP_DL_WARNING,
1827*38fd1498Szrj 		 "character constant too long for its type");
1828*38fd1498Szrj     }
1829*38fd1498Szrj   else if (i > 1 && CPP_OPTION (pfile, warn_multichar))
1830*38fd1498Szrj     cpp_warning (pfile, CPP_W_MULTICHAR, "multi-character character constant");
1831*38fd1498Szrj 
1832*38fd1498Szrj   /* Multichar constants are of type int and therefore signed.  */
1833*38fd1498Szrj   if (i > 1)
1834*38fd1498Szrj     unsigned_p = 0;
1835*38fd1498Szrj   else
1836*38fd1498Szrj     unsigned_p = CPP_OPTION (pfile, unsigned_char);
1837*38fd1498Szrj 
1838*38fd1498Szrj   /* Truncate the constant to its natural width, and simultaneously
1839*38fd1498Szrj      sign- or zero-extend to the full width of cppchar_t.
1840*38fd1498Szrj      For single-character constants, the value is WIDTH bits wide.
1841*38fd1498Szrj      For multi-character constants, the value is INT_PRECISION bits wide.  */
1842*38fd1498Szrj   if (i > 1)
1843*38fd1498Szrj     width = CPP_OPTION (pfile, int_precision);
1844*38fd1498Szrj   if (width < BITS_PER_CPPCHAR_T)
1845*38fd1498Szrj     {
1846*38fd1498Szrj       mask = ((cppchar_t) 1 << width) - 1;
1847*38fd1498Szrj       if (unsigned_p || !(result & (1 << (width - 1))))
1848*38fd1498Szrj 	result &= mask;
1849*38fd1498Szrj       else
1850*38fd1498Szrj 	result |= ~mask;
1851*38fd1498Szrj     }
1852*38fd1498Szrj   *pchars_seen = i;
1853*38fd1498Szrj   *unsignedp = unsigned_p;
1854*38fd1498Szrj   return result;
1855*38fd1498Szrj }
1856*38fd1498Szrj 
1857*38fd1498Szrj /* Subroutine of cpp_interpret_charconst which performs the conversion
1858*38fd1498Szrj    to a number, for wide strings.  STR is the string structure returned
1859*38fd1498Szrj    by cpp_interpret_string.  PCHARS_SEEN and UNSIGNEDP are as for
1860*38fd1498Szrj    cpp_interpret_charconst.  TYPE is the token type.  */
1861*38fd1498Szrj static cppchar_t
wide_str_to_charconst(cpp_reader * pfile,cpp_string str,unsigned int * pchars_seen,int * unsignedp,enum cpp_ttype type)1862*38fd1498Szrj wide_str_to_charconst (cpp_reader *pfile, cpp_string str,
1863*38fd1498Szrj 		       unsigned int *pchars_seen, int *unsignedp,
1864*38fd1498Szrj 		       enum cpp_ttype type)
1865*38fd1498Szrj {
1866*38fd1498Szrj   bool bigend = CPP_OPTION (pfile, bytes_big_endian);
1867*38fd1498Szrj   size_t width = converter_for_type (pfile, type).width;
1868*38fd1498Szrj   size_t cwidth = CPP_OPTION (pfile, char_precision);
1869*38fd1498Szrj   size_t mask = width_to_mask (width);
1870*38fd1498Szrj   size_t cmask = width_to_mask (cwidth);
1871*38fd1498Szrj   size_t nbwc = width / cwidth;
1872*38fd1498Szrj   size_t off, i;
1873*38fd1498Szrj   cppchar_t result = 0, c;
1874*38fd1498Szrj 
1875*38fd1498Szrj   /* This is finicky because the string is in the target's byte order,
1876*38fd1498Szrj      which may not be our byte order.  Only the last character, ignoring
1877*38fd1498Szrj      the NUL terminator, is relevant.  */
1878*38fd1498Szrj   off = str.len - (nbwc * 2);
1879*38fd1498Szrj   result = 0;
1880*38fd1498Szrj   for (i = 0; i < nbwc; i++)
1881*38fd1498Szrj     {
1882*38fd1498Szrj       c = bigend ? str.text[off + i] : str.text[off + nbwc - i - 1];
1883*38fd1498Szrj       result = (result << cwidth) | (c & cmask);
1884*38fd1498Szrj     }
1885*38fd1498Szrj 
1886*38fd1498Szrj   /* Wide character constants have type wchar_t, and a single
1887*38fd1498Szrj      character exactly fills a wchar_t, so a multi-character wide
1888*38fd1498Szrj      character constant is guaranteed to overflow.  */
1889*38fd1498Szrj   if (str.len > nbwc * 2)
1890*38fd1498Szrj     cpp_error (pfile, CPP_DL_WARNING,
1891*38fd1498Szrj 	       "character constant too long for its type");
1892*38fd1498Szrj 
1893*38fd1498Szrj   /* Truncate the constant to its natural width, and simultaneously
1894*38fd1498Szrj      sign- or zero-extend to the full width of cppchar_t.  */
1895*38fd1498Szrj   if (width < BITS_PER_CPPCHAR_T)
1896*38fd1498Szrj     {
1897*38fd1498Szrj       if (type == CPP_CHAR16 || type == CPP_CHAR32
1898*38fd1498Szrj 	  || CPP_OPTION (pfile, unsigned_wchar)
1899*38fd1498Szrj 	  || !(result & (1 << (width - 1))))
1900*38fd1498Szrj 	result &= mask;
1901*38fd1498Szrj       else
1902*38fd1498Szrj 	result |= ~mask;
1903*38fd1498Szrj     }
1904*38fd1498Szrj 
1905*38fd1498Szrj   if (type == CPP_CHAR16 || type == CPP_CHAR32
1906*38fd1498Szrj       || CPP_OPTION (pfile, unsigned_wchar))
1907*38fd1498Szrj     *unsignedp = 1;
1908*38fd1498Szrj   else
1909*38fd1498Szrj     *unsignedp = 0;
1910*38fd1498Szrj 
1911*38fd1498Szrj   *pchars_seen = 1;
1912*38fd1498Szrj   return result;
1913*38fd1498Szrj }
1914*38fd1498Szrj 
1915*38fd1498Szrj /* Interpret a (possibly wide) character constant in TOKEN.
1916*38fd1498Szrj    PCHARS_SEEN points to a variable that is filled in with the number
1917*38fd1498Szrj    of characters seen, and UNSIGNEDP to a variable that indicates
1918*38fd1498Szrj    whether the result has signed type.  */
1919*38fd1498Szrj cppchar_t
cpp_interpret_charconst(cpp_reader * pfile,const cpp_token * token,unsigned int * pchars_seen,int * unsignedp)1920*38fd1498Szrj cpp_interpret_charconst (cpp_reader *pfile, const cpp_token *token,
1921*38fd1498Szrj 			 unsigned int *pchars_seen, int *unsignedp)
1922*38fd1498Szrj {
1923*38fd1498Szrj   cpp_string str = { 0, 0 };
1924*38fd1498Szrj   bool wide = (token->type != CPP_CHAR && token->type != CPP_UTF8CHAR);
1925*38fd1498Szrj   int u8 = 2 * int(token->type == CPP_UTF8CHAR);
1926*38fd1498Szrj   cppchar_t result;
1927*38fd1498Szrj 
1928*38fd1498Szrj   /* An empty constant will appear as L'', u'', U'', u8'', or '' */
1929*38fd1498Szrj   if (token->val.str.len == (size_t) (2 + wide + u8))
1930*38fd1498Szrj     {
1931*38fd1498Szrj       cpp_error (pfile, CPP_DL_ERROR, "empty character constant");
1932*38fd1498Szrj       *pchars_seen = 0;
1933*38fd1498Szrj       *unsignedp = 0;
1934*38fd1498Szrj       return 0;
1935*38fd1498Szrj     }
1936*38fd1498Szrj   else if (!cpp_interpret_string (pfile, &token->val.str, 1, &str,
1937*38fd1498Szrj 				  token->type))
1938*38fd1498Szrj     {
1939*38fd1498Szrj       *pchars_seen = 0;
1940*38fd1498Szrj       *unsignedp = 0;
1941*38fd1498Szrj       return 0;
1942*38fd1498Szrj     }
1943*38fd1498Szrj 
1944*38fd1498Szrj   if (wide)
1945*38fd1498Szrj     result = wide_str_to_charconst (pfile, str, pchars_seen, unsignedp,
1946*38fd1498Szrj 				    token->type);
1947*38fd1498Szrj   else
1948*38fd1498Szrj     result = narrow_str_to_charconst (pfile, str, pchars_seen, unsignedp);
1949*38fd1498Szrj 
1950*38fd1498Szrj   if (str.text != token->val.str.text)
1951*38fd1498Szrj     free ((void *)str.text);
1952*38fd1498Szrj 
1953*38fd1498Szrj   return result;
1954*38fd1498Szrj }
1955*38fd1498Szrj 
1956*38fd1498Szrj /* Convert an identifier denoted by ID and LEN, which might contain
1957*38fd1498Szrj    UCN escapes, to the source character set, either UTF-8 or
1958*38fd1498Szrj    UTF-EBCDIC.  Assumes that the identifier is actually a valid identifier.  */
1959*38fd1498Szrj cpp_hashnode *
_cpp_interpret_identifier(cpp_reader * pfile,const uchar * id,size_t len)1960*38fd1498Szrj _cpp_interpret_identifier (cpp_reader *pfile, const uchar *id, size_t len)
1961*38fd1498Szrj {
1962*38fd1498Szrj   /* It turns out that a UCN escape always turns into fewer characters
1963*38fd1498Szrj      than the escape itself, so we can allocate a temporary in advance.  */
1964*38fd1498Szrj   uchar * buf = (uchar *) alloca (len + 1);
1965*38fd1498Szrj   uchar * bufp = buf;
1966*38fd1498Szrj   size_t idp;
1967*38fd1498Szrj 
1968*38fd1498Szrj   for (idp = 0; idp < len; idp++)
1969*38fd1498Szrj     if (id[idp] != '\\')
1970*38fd1498Szrj       *bufp++ = id[idp];
1971*38fd1498Szrj     else
1972*38fd1498Szrj       {
1973*38fd1498Szrj 	unsigned length = id[idp+1] == 'u' ? 4 : 8;
1974*38fd1498Szrj 	cppchar_t value = 0;
1975*38fd1498Szrj 	size_t bufleft = len - (bufp - buf);
1976*38fd1498Szrj 	int rval;
1977*38fd1498Szrj 
1978*38fd1498Szrj 	idp += 2;
1979*38fd1498Szrj 	while (length && idp < len && ISXDIGIT (id[idp]))
1980*38fd1498Szrj 	  {
1981*38fd1498Szrj 	    value = (value << 4) + hex_value (id[idp]);
1982*38fd1498Szrj 	    idp++;
1983*38fd1498Szrj 	    length--;
1984*38fd1498Szrj 	  }
1985*38fd1498Szrj 	idp--;
1986*38fd1498Szrj 
1987*38fd1498Szrj 	/* Special case for EBCDIC: if the identifier contains
1988*38fd1498Szrj 	   a '$' specified using a UCN, translate it to EBCDIC.  */
1989*38fd1498Szrj 	if (value == 0x24)
1990*38fd1498Szrj 	  {
1991*38fd1498Szrj 	    *bufp++ = '$';
1992*38fd1498Szrj 	    continue;
1993*38fd1498Szrj 	  }
1994*38fd1498Szrj 
1995*38fd1498Szrj 	rval = one_cppchar_to_utf8 (value, &bufp, &bufleft);
1996*38fd1498Szrj 	if (rval)
1997*38fd1498Szrj 	  {
1998*38fd1498Szrj 	    errno = rval;
1999*38fd1498Szrj 	    cpp_errno (pfile, CPP_DL_ERROR,
2000*38fd1498Szrj 		       "converting UCN to source character set");
2001*38fd1498Szrj 	    break;
2002*38fd1498Szrj 	  }
2003*38fd1498Szrj       }
2004*38fd1498Szrj 
2005*38fd1498Szrj   return CPP_HASHNODE (ht_lookup (pfile->hash_table,
2006*38fd1498Szrj 				  buf, bufp - buf, HT_ALLOC));
2007*38fd1498Szrj }
2008*38fd1498Szrj 
2009*38fd1498Szrj /* Convert an input buffer (containing the complete contents of one
2010*38fd1498Szrj    source file) from INPUT_CHARSET to the source character set.  INPUT
2011*38fd1498Szrj    points to the input buffer, SIZE is its allocated size, and LEN is
2012*38fd1498Szrj    the length of the meaningful data within the buffer.  The
2013*38fd1498Szrj    translated buffer is returned, *ST_SIZE is set to the length of
2014*38fd1498Szrj    the meaningful data within the translated buffer, and *BUFFER_START
2015*38fd1498Szrj    is set to the start of the returned buffer.  *BUFFER_START may
2016*38fd1498Szrj    differ from the return value in the case of a BOM or other ignored
2017*38fd1498Szrj    marker information.
2018*38fd1498Szrj 
2019*38fd1498Szrj    INPUT is expected to have been allocated with xmalloc.  This
2020*38fd1498Szrj    function will either set *BUFFER_START to INPUT, or free it and set
2021*38fd1498Szrj    *BUFFER_START to a pointer to another xmalloc-allocated block of
2022*38fd1498Szrj    memory.  */
2023*38fd1498Szrj uchar *
_cpp_convert_input(cpp_reader * pfile,const char * input_charset,uchar * input,size_t size,size_t len,const unsigned char ** buffer_start,off_t * st_size)2024*38fd1498Szrj _cpp_convert_input (cpp_reader *pfile, const char *input_charset,
2025*38fd1498Szrj 		    uchar *input, size_t size, size_t len,
2026*38fd1498Szrj 		    const unsigned char **buffer_start, off_t *st_size)
2027*38fd1498Szrj {
2028*38fd1498Szrj   struct cset_converter input_cset;
2029*38fd1498Szrj   struct _cpp_strbuf to;
2030*38fd1498Szrj   unsigned char *buffer;
2031*38fd1498Szrj 
2032*38fd1498Szrj   input_cset = init_iconv_desc (pfile, SOURCE_CHARSET, input_charset);
2033*38fd1498Szrj   if (input_cset.func == convert_no_conversion)
2034*38fd1498Szrj     {
2035*38fd1498Szrj       to.text = input;
2036*38fd1498Szrj       to.asize = size;
2037*38fd1498Szrj       to.len = len;
2038*38fd1498Szrj     }
2039*38fd1498Szrj   else
2040*38fd1498Szrj     {
2041*38fd1498Szrj       to.asize = MAX (65536, len);
2042*38fd1498Szrj       to.text = XNEWVEC (uchar, to.asize);
2043*38fd1498Szrj       to.len = 0;
2044*38fd1498Szrj 
2045*38fd1498Szrj       if (!APPLY_CONVERSION (input_cset, input, len, &to))
2046*38fd1498Szrj 	cpp_error (pfile, CPP_DL_ERROR,
2047*38fd1498Szrj 		   "failure to convert %s to %s",
2048*38fd1498Szrj 		   CPP_OPTION (pfile, input_charset), SOURCE_CHARSET);
2049*38fd1498Szrj 
2050*38fd1498Szrj       free (input);
2051*38fd1498Szrj     }
2052*38fd1498Szrj 
2053*38fd1498Szrj   /* Clean up the mess.  */
2054*38fd1498Szrj   if (input_cset.func == convert_using_iconv)
2055*38fd1498Szrj     iconv_close (input_cset.cd);
2056*38fd1498Szrj 
2057*38fd1498Szrj   /* Resize buffer if we allocated substantially too much, or if we
2058*38fd1498Szrj      haven't enough space for the \n-terminator or following
2059*38fd1498Szrj      15 bytes of padding (used to quiet warnings from valgrind or
2060*38fd1498Szrj      Address Sanitizer, when the optimized lexer accesses aligned
2061*38fd1498Szrj      16-byte memory chunks, including the bytes after the malloced,
2062*38fd1498Szrj      area, and stops lexing on '\n').  */
2063*38fd1498Szrj   if (to.len + 4096 < to.asize || to.len + 16 > to.asize)
2064*38fd1498Szrj     to.text = XRESIZEVEC (uchar, to.text, to.len + 16);
2065*38fd1498Szrj 
2066*38fd1498Szrj   memset (to.text + to.len, '\0', 16);
2067*38fd1498Szrj 
2068*38fd1498Szrj   /* If the file is using old-school Mac line endings (\r only),
2069*38fd1498Szrj      terminate with another \r, not an \n, so that we do not mistake
2070*38fd1498Szrj      the \r\n sequence for a single DOS line ending and erroneously
2071*38fd1498Szrj      issue the "No newline at end of file" diagnostic.  */
2072*38fd1498Szrj   if (to.len && to.text[to.len - 1] == '\r')
2073*38fd1498Szrj     to.text[to.len] = '\r';
2074*38fd1498Szrj   else
2075*38fd1498Szrj     to.text[to.len] = '\n';
2076*38fd1498Szrj 
2077*38fd1498Szrj   buffer = to.text;
2078*38fd1498Szrj   *st_size = to.len;
2079*38fd1498Szrj #if HOST_CHARSET == HOST_CHARSET_ASCII
2080*38fd1498Szrj   /* The HOST_CHARSET test just above ensures that the source charset
2081*38fd1498Szrj      is UTF-8.  So, ignore a UTF-8 BOM if we see one.  Note that
2082*38fd1498Szrj      glib'c UTF-8 iconv() provider (as of glibc 2.7) does not ignore a
2083*38fd1498Szrj      BOM -- however, even if it did, we would still need this code due
2084*38fd1498Szrj      to the 'convert_no_conversion' case.  */
2085*38fd1498Szrj   if (to.len >= 3 && to.text[0] == 0xef && to.text[1] == 0xbb
2086*38fd1498Szrj       && to.text[2] == 0xbf)
2087*38fd1498Szrj     {
2088*38fd1498Szrj       *st_size -= 3;
2089*38fd1498Szrj       buffer += 3;
2090*38fd1498Szrj     }
2091*38fd1498Szrj #endif
2092*38fd1498Szrj 
2093*38fd1498Szrj   *buffer_start = to.text;
2094*38fd1498Szrj   return buffer;
2095*38fd1498Szrj }
2096*38fd1498Szrj 
2097*38fd1498Szrj /* Decide on the default encoding to assume for input files.  */
2098*38fd1498Szrj const char *
_cpp_default_encoding(void)2099*38fd1498Szrj _cpp_default_encoding (void)
2100*38fd1498Szrj {
2101*38fd1498Szrj   const char *current_encoding = NULL;
2102*38fd1498Szrj 
2103*38fd1498Szrj   /* We disable this because the default codeset is 7-bit ASCII on
2104*38fd1498Szrj      most platforms, and this causes conversion failures on every
2105*38fd1498Szrj      file in GCC that happens to have one of the upper 128 characters
2106*38fd1498Szrj      in it -- most likely, as part of the name of a contributor.
2107*38fd1498Szrj      We should definitely recognize in-band markers of file encoding,
2108*38fd1498Szrj      like:
2109*38fd1498Szrj      - the appropriate Unicode byte-order mark (FE FF) to recognize
2110*38fd1498Szrj        UTF16 and UCS4 (in both big-endian and little-endian flavors)
2111*38fd1498Szrj        and UTF8
2112*38fd1498Szrj      - a "#i", "#d", "/ *", "//", " #p" or "#p" (for #pragma) to
2113*38fd1498Szrj        distinguish ASCII and EBCDIC.
2114*38fd1498Szrj      - now we can parse something like "#pragma GCC encoding <xyz>
2115*38fd1498Szrj        on the first line, or even Emacs/VIM's mode line tags (there's
2116*38fd1498Szrj        a problem here in that VIM uses the last line, and Emacs has
2117*38fd1498Szrj        its more elaborate "local variables" convention).
2118*38fd1498Szrj      - investigate whether Java has another common convention, which
2119*38fd1498Szrj        would be friendly to support.
2120*38fd1498Szrj      (Zack Weinberg and Paolo Bonzini, May 20th 2004)  */
2121*38fd1498Szrj #if defined (HAVE_LOCALE_H) && defined (HAVE_LANGINFO_CODESET) && 0
2122*38fd1498Szrj   setlocale (LC_CTYPE, "");
2123*38fd1498Szrj   current_encoding = nl_langinfo (CODESET);
2124*38fd1498Szrj #endif
2125*38fd1498Szrj   if (current_encoding == NULL || *current_encoding == '\0')
2126*38fd1498Szrj     current_encoding = SOURCE_CHARSET;
2127*38fd1498Szrj 
2128*38fd1498Szrj   return current_encoding;
2129*38fd1498Szrj }
2130*38fd1498Szrj 
2131*38fd1498Szrj /* Implementation of class cpp_string_location_reader.  */
2132*38fd1498Szrj 
2133*38fd1498Szrj /* Constructor for cpp_string_location_reader.  */
2134*38fd1498Szrj 
2135*38fd1498Szrj cpp_string_location_reader::
cpp_string_location_reader(source_location src_loc,line_maps * line_table)2136*38fd1498Szrj cpp_string_location_reader (source_location src_loc,
2137*38fd1498Szrj 			    line_maps *line_table)
2138*38fd1498Szrj : m_line_table (line_table)
2139*38fd1498Szrj {
2140*38fd1498Szrj   src_loc = get_range_from_loc (line_table, src_loc).m_start;
2141*38fd1498Szrj 
2142*38fd1498Szrj   /* SRC_LOC might be a macro location.  It only makes sense to do
2143*38fd1498Szrj      column-by-column calculations on ordinary maps, so get the
2144*38fd1498Szrj      corresponding location in an ordinary map.  */
2145*38fd1498Szrj   m_loc
2146*38fd1498Szrj     = linemap_resolve_location (line_table, src_loc,
2147*38fd1498Szrj 				LRK_SPELLING_LOCATION, NULL);
2148*38fd1498Szrj 
2149*38fd1498Szrj   const line_map_ordinary *map
2150*38fd1498Szrj     = linemap_check_ordinary (linemap_lookup (line_table, m_loc));
2151*38fd1498Szrj   m_offset_per_column = (1 << map->m_range_bits);
2152*38fd1498Szrj }
2153*38fd1498Szrj 
2154*38fd1498Szrj /* Get the range of the next source byte.  */
2155*38fd1498Szrj 
2156*38fd1498Szrj source_range
get_next()2157*38fd1498Szrj cpp_string_location_reader::get_next ()
2158*38fd1498Szrj {
2159*38fd1498Szrj   source_range result;
2160*38fd1498Szrj   result.m_start = m_loc;
2161*38fd1498Szrj   result.m_finish = m_loc;
2162*38fd1498Szrj   if (m_loc <= LINE_MAP_MAX_LOCATION_WITH_COLS)
2163*38fd1498Szrj     m_loc += m_offset_per_column;
2164*38fd1498Szrj   return result;
2165*38fd1498Szrj }
2166