charset.c 55 KB

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