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