loop.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /* Conversion loop frame work.
  2. Copyright (C) 1998-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library 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 GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. /* This file provides a frame for the reader loop in all conversion modules.
  16. The actual code must (of course) be provided in the actual module source
  17. code but certain actions can be written down generically, with some
  18. customization options which are these:
  19. MIN_NEEDED_INPUT minimal number of input bytes needed for the next
  20. conversion.
  21. MIN_NEEDED_OUTPUT minimal number of bytes produced by the next round
  22. of conversion.
  23. MAX_NEEDED_INPUT you guess it, this is the maximal number of input
  24. bytes needed. It defaults to MIN_NEEDED_INPUT
  25. MAX_NEEDED_OUTPUT likewise for output bytes.
  26. LOOPFCT name of the function created. If not specified
  27. the name is `loop' but this prevents the use
  28. of multiple functions in the same file.
  29. BODY this is supposed to expand to the body of the loop.
  30. The user must provide this.
  31. EXTRA_LOOP_DECLS extra arguments passed from conversion loop call.
  32. INIT_PARAMS code to define and initialize variables from params.
  33. UPDATE_PARAMS code to store result in params.
  34. ONEBYTE_BODY body of the specialized conversion function for a
  35. single byte from the current character set to INTERNAL.
  36. */
  37. #include <assert.h>
  38. #include <endian.h>
  39. #include <iconv/gconv_int.h>
  40. #include <stdint.h>
  41. #include <string.h>
  42. #include <wchar.h>
  43. #include <sys/param.h> /* For MIN. */
  44. #define __need_size_t
  45. #include <stddef.h>
  46. #include <libc-diag.h>
  47. #undef FCTNAME2
  48. #define FCTNAME(name) name
  49. /* We need at least one byte for the next round. */
  50. #ifndef MIN_NEEDED_INPUT
  51. # error "MIN_NEEDED_INPUT definition missing"
  52. #elif MIN_NEEDED_INPUT < 1
  53. # error "MIN_NEEDED_INPUT must be >= 1"
  54. #endif
  55. /* Let's see how many bytes we produce. */
  56. #ifndef MAX_NEEDED_INPUT
  57. # define MAX_NEEDED_INPUT MIN_NEEDED_INPUT
  58. #endif
  59. /* We produce at least one byte in the next round. */
  60. #ifndef MIN_NEEDED_OUTPUT
  61. # error "MIN_NEEDED_OUTPUT definition missing"
  62. #elif MIN_NEEDED_OUTPUT < 1
  63. # error "MIN_NEEDED_OUTPUT must be >= 1"
  64. #endif
  65. /* Let's see how many bytes we produce. */
  66. #ifndef MAX_NEEDED_OUTPUT
  67. # define MAX_NEEDED_OUTPUT MIN_NEEDED_OUTPUT
  68. #endif
  69. /* Default name for the function. */
  70. #ifndef LOOPFCT
  71. # define LOOPFCT loop
  72. #endif
  73. /* Make sure we have a loop body. */
  74. #ifndef BODY
  75. # error "Definition of BODY missing for function" LOOPFCT
  76. #endif
  77. /* If no arguments have to passed to the loop function define the macro
  78. as empty. */
  79. #ifndef EXTRA_LOOP_DECLS
  80. # define EXTRA_LOOP_DECLS
  81. #endif
  82. /* Allow using UPDATE_PARAMS in macros where #ifdef UPDATE_PARAMS test
  83. isn't possible. */
  84. #ifndef UPDATE_PARAMS
  85. # define UPDATE_PARAMS do { } while (0)
  86. #endif
  87. #ifndef REINIT_PARAMS
  88. # define REINIT_PARAMS do { } while (0)
  89. #endif
  90. /* To make it easier for the writers of the modules, we define a macro
  91. to test whether we have to ignore errors. */
  92. #define ignore_errors_p() \
  93. (irreversible != NULL && (flags & __GCONV_IGNORE_ERRORS))
  94. /* Error handling for the FROM_LOOP direction, with ignoring of errors.
  95. Note that we cannot use the do while (0) trick since `break' and
  96. `continue' must reach certain points. */
  97. #define STANDARD_FROM_LOOP_ERR_HANDLER(Incr) \
  98. { \
  99. result = __gconv_mark_illegal_input (step_data); \
  100. if (! ignore_errors_p ()) \
  101. break; \
  102. \
  103. /* We ignore the invalid input byte sequence. */ \
  104. inptr += (Incr); \
  105. ++*irreversible; \
  106. /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
  107. that "iconv -c" must give the same exitcode as "iconv". */ \
  108. continue; \
  109. }
  110. /* Error handling for the TO_LOOP direction, with use of transliteration/
  111. transcription functions and ignoring of errors. Note that we cannot use
  112. the do while (0) trick since `break' and `continue' must reach certain
  113. points. */
  114. #define STANDARD_TO_LOOP_ERR_HANDLER(Incr) \
  115. { \
  116. if (irreversible == NULL) \
  117. { \
  118. /* This means we are in call from __gconv_transliterate. In this \
  119. case we are not doing any error recovery ourselves. */ \
  120. result = __gconv_mark_illegal_input (step_data); \
  121. break; \
  122. } \
  123. \
  124. /* If needed, flush any conversion state, so that __gconv_transliterate \
  125. starts with current shift state. */ \
  126. UPDATE_PARAMS; \
  127. \
  128. /* First try the transliteration methods. */ \
  129. if ((step_data->__flags & __GCONV_TRANSLIT) != 0) \
  130. result = __gconv_transliterate \
  131. (step, step_data, *inptrp, \
  132. &inptr, inend, &outptr, irreversible); \
  133. else \
  134. result = __gconv_mark_illegal_input (step_data); \
  135. \
  136. REINIT_PARAMS; \
  137. \
  138. /* If any of them recognized the input continue with the loop. */ \
  139. if (result != __GCONV_ILLEGAL_INPUT) \
  140. { \
  141. if (__glibc_unlikely (result == __GCONV_FULL_OUTPUT)) \
  142. break; \
  143. \
  144. continue; \
  145. } \
  146. \
  147. /* Next see whether we have to ignore the error. If not, stop. */ \
  148. if (! ignore_errors_p ()) \
  149. break; \
  150. \
  151. /* When we come here it means we ignore the character. */ \
  152. ++*irreversible; \
  153. inptr += Incr; \
  154. /* But we keep result == __GCONV_ILLEGAL_INPUT, because of the constraint \
  155. that "iconv -c" must give the same exitcode as "iconv". */ \
  156. continue; \
  157. }
  158. /* With GCC 7 when compiling with -Os for 32-bit s390 the compiler
  159. warns that the variable 'ch', in the definition of BODY in
  160. sysdeps/s390/multiarch/8bit-generic.c, may be used uninitialized in
  161. the call to UNICODE_TAG_HANDLER in that macro. This variable is
  162. actually always initialized before use, in the prior loop if INDEX
  163. is nonzero and in the following 'if' if INDEX is zero. That code
  164. has a comment referencing this diagnostic disabling; updates in one
  165. place may require updates in the other. */
  166. DIAG_PUSH_NEEDS_COMMENT;
  167. DIAG_IGNORE_Os_NEEDS_COMMENT_GCC (7, "-Wmaybe-uninitialized");
  168. /* Handling of Unicode 3.1 TAG characters. Unicode recommends
  169. "If language codes are not relevant to the particular processing
  170. operation, then they should be ignored." This macro is usually
  171. called right before STANDARD_TO_LOOP_ERR_HANDLER (Incr). */
  172. #define UNICODE_TAG_HANDLER(Character, Incr) \
  173. { \
  174. /* TAG characters are those in the range U+E0000..U+E007F. */ \
  175. if (((Character) >> 7) == (0xe0000 >> 7)) \
  176. { \
  177. inptr += Incr; \
  178. continue; \
  179. } \
  180. }
  181. DIAG_POP_NEEDS_COMMENT;
  182. /* The function returns the status, as defined in gconv.h. */
  183. static inline int
  184. __attribute ((always_inline))
  185. FCTNAME (LOOPFCT) (struct __gconv_step *step,
  186. struct __gconv_step_data *step_data,
  187. const unsigned char **inptrp, const unsigned char *inend,
  188. unsigned char **outptrp, const unsigned char *outend,
  189. size_t *irreversible EXTRA_LOOP_DECLS)
  190. {
  191. #ifdef LOOP_NEED_STATE
  192. mbstate_t *state = step_data->__statep;
  193. #endif
  194. #ifdef LOOP_NEED_FLAGS
  195. int flags = step_data->__flags;
  196. #endif
  197. #ifdef LOOP_NEED_DATA
  198. void *data = step->__data;
  199. #endif
  200. int result = __GCONV_EMPTY_INPUT;
  201. const unsigned char *inptr = *inptrp;
  202. unsigned char *outptr = *outptrp;
  203. #ifdef INIT_PARAMS
  204. INIT_PARAMS;
  205. #endif
  206. while (inptr != inend)
  207. {
  208. /* `if' cases for MIN_NEEDED_OUTPUT ==/!= 1 is made to help the
  209. compiler generating better code. They will be optimized away
  210. since MIN_NEEDED_OUTPUT is always a constant. */
  211. if (MIN_NEEDED_INPUT > 1
  212. && __builtin_expect (inptr + MIN_NEEDED_INPUT > inend, 0))
  213. {
  214. /* We don't have enough input for another complete input
  215. character. */
  216. result = __GCONV_INCOMPLETE_INPUT;
  217. break;
  218. }
  219. if ((MIN_NEEDED_OUTPUT != 1
  220. && __builtin_expect (outptr + MIN_NEEDED_OUTPUT > outend, 0))
  221. || (MIN_NEEDED_OUTPUT == 1
  222. && __builtin_expect (outptr >= outend, 0)))
  223. {
  224. /* Overflow in the output buffer. */
  225. result = __GCONV_FULL_OUTPUT;
  226. break;
  227. }
  228. /* Here comes the body the user provides. It can stop with
  229. RESULT set to GCONV_INCOMPLETE_INPUT (if the size of the
  230. input characters vary in size), GCONV_ILLEGAL_INPUT, or
  231. GCONV_FULL_OUTPUT (if the output characters vary in size). */
  232. BODY
  233. }
  234. /* Update the pointers pointed to by the parameters. */
  235. *inptrp = inptr;
  236. *outptrp = outptr;
  237. UPDATE_PARAMS;
  238. return result;
  239. }
  240. #if MAX_NEEDED_INPUT > 1
  241. # define SINGLE(fct) SINGLE2 (fct)
  242. # define SINGLE2(fct) fct##_single
  243. static inline int
  244. __attribute ((always_inline))
  245. SINGLE(LOOPFCT) (struct __gconv_step *step,
  246. struct __gconv_step_data *step_data,
  247. const unsigned char **inptrp, const unsigned char *inend,
  248. unsigned char **outptrp, unsigned char *outend,
  249. size_t *irreversible EXTRA_LOOP_DECLS)
  250. {
  251. mbstate_t *state = step_data->__statep;
  252. # ifdef LOOP_NEED_FLAGS
  253. int flags = step_data->__flags;
  254. # endif
  255. # ifdef LOOP_NEED_DATA
  256. void *data = step->__data;
  257. # endif
  258. int result = __GCONV_OK;
  259. unsigned char bytebuf[MAX_NEEDED_INPUT];
  260. const unsigned char *inptr = *inptrp;
  261. unsigned char *outptr = *outptrp;
  262. size_t inlen;
  263. # ifdef INIT_PARAMS
  264. INIT_PARAMS;
  265. # endif
  266. # ifdef UNPACK_BYTES
  267. UNPACK_BYTES
  268. # else
  269. /* Add the bytes from the state to the input buffer. */
  270. assert ((state->__count & 7) <= sizeof (state->__value));
  271. for (inlen = 0; inlen < (size_t) (state->__count & 7); ++inlen)
  272. bytebuf[inlen] = state->__value.__wchb[inlen];
  273. # endif
  274. /* Are there enough bytes in the input buffer? */
  275. if (MIN_NEEDED_INPUT > 1
  276. && __builtin_expect (inptr + (MIN_NEEDED_INPUT - inlen) > inend, 0))
  277. {
  278. *inptrp = inend;
  279. # ifdef STORE_REST
  280. /* Building with -O3 GCC emits a `array subscript is above array
  281. bounds' warning. GCC BZ #64739 has been opened for this. */
  282. DIAG_PUSH_NEEDS_COMMENT;
  283. DIAG_IGNORE_NEEDS_COMMENT (4.9, "-Warray-bounds");
  284. while (inptr < inend)
  285. bytebuf[inlen++] = *inptr++;
  286. DIAG_POP_NEEDS_COMMENT;
  287. inptr = bytebuf;
  288. inptrp = &inptr;
  289. inend = &bytebuf[inlen];
  290. STORE_REST
  291. # else
  292. /* We don't have enough input for another complete input
  293. character. */
  294. size_t inlen_after = inlen + (inend - inptr);
  295. assert (inlen_after <= sizeof (state->__value.__wchb));
  296. for (; inlen < inlen_after; inlen++)
  297. state->__value.__wchb[inlen] = *inptr++;
  298. # endif
  299. return __GCONV_INCOMPLETE_INPUT;
  300. }
  301. /* Enough space in output buffer. */
  302. if ((MIN_NEEDED_OUTPUT != 1 && outptr + MIN_NEEDED_OUTPUT > outend)
  303. || (MIN_NEEDED_OUTPUT == 1 && outptr >= outend))
  304. /* Overflow in the output buffer. */
  305. return __GCONV_FULL_OUTPUT;
  306. /* Now add characters from the normal input buffer. */
  307. if (inlen >= MAX_NEEDED_INPUT || inptr >= inend)
  308. /* Avoid a -Wstringop-overflow= warning when this loop is
  309. unrolled. The compiler cannot otherwise see that this is
  310. unreachable because it depends on (state->__count & 7) not
  311. being too large after a previous conversion step.
  312. Starting with GCC 12, we also have mark the inptr >= inend
  313. case as unreachable to omit the warning. Note that this SINGLE
  314. function is only used to implement the mb*towc*() or wc*tomb*()
  315. functions. Those functions use inptr and inend pointing to a
  316. variable on stack, compute the inend pointer or explicitly check
  317. the arguments which always leads to inptr < inend. */
  318. __builtin_unreachable ();
  319. do
  320. bytebuf[inlen++] = *inptr++;
  321. while (inlen < MAX_NEEDED_INPUT && inptr < inend);
  322. inptr = bytebuf;
  323. inend = &bytebuf[inlen];
  324. do
  325. {
  326. BODY
  327. }
  328. while (0);
  329. /* Now we either have produced an output character and consumed all the
  330. bytes from the state and at least one more, or the character is still
  331. incomplete, or we have some other error (like illegal input character,
  332. no space in output buffer). */
  333. if (__glibc_likely (inptr != bytebuf))
  334. {
  335. /* We found a new character. */
  336. assert (inptr - bytebuf > (state->__count & 7));
  337. *inptrp += inptr - bytebuf - (state->__count & 7);
  338. *outptrp = outptr;
  339. result = __GCONV_OK;
  340. /* Clear the state buffer. */
  341. # ifdef CLEAR_STATE
  342. CLEAR_STATE;
  343. # else
  344. state->__count &= ~7;
  345. # endif
  346. }
  347. else if (result == __GCONV_INCOMPLETE_INPUT)
  348. {
  349. /* This can only happen if we have less than MAX_NEEDED_INPUT bytes
  350. available. */
  351. assert (inend != &bytebuf[MAX_NEEDED_INPUT]);
  352. *inptrp += inend - bytebuf - (state->__count & 7);
  353. # ifdef STORE_REST
  354. inptrp = &inptr;
  355. STORE_REST
  356. # else
  357. /* We don't have enough input for another complete input
  358. character. */
  359. assert (inend - inptr > (state->__count & ~7));
  360. assert (inend - inptr <= sizeof (state->__value.__wchb));
  361. state->__count = (state->__count & ~7) | (inend - inptr);
  362. for (inlen = 0; inlen < inend - inptr; inlen++)
  363. state->__value.__wchb[inlen] = inptr[inlen];
  364. inptr = inend;
  365. # endif
  366. }
  367. return result;
  368. }
  369. # undef SINGLE
  370. # undef SINGLE2
  371. # ifdef ONEBYTE_BODY
  372. /* Define the shortcut function for btowc. */
  373. static wint_t
  374. gconv_btowc (struct __gconv_step *step, unsigned char c)
  375. ONEBYTE_BODY
  376. # define FROM_ONEBYTE gconv_btowc
  377. # endif
  378. #endif
  379. /* We remove the macro definitions so that we can include this file again
  380. for the definition of another function. */
  381. #undef MIN_NEEDED_INPUT
  382. #undef MAX_NEEDED_INPUT
  383. #undef MIN_NEEDED_OUTPUT
  384. #undef MAX_NEEDED_OUTPUT
  385. #undef LOOPFCT
  386. #undef BODY
  387. #undef LOOPFCT
  388. #undef EXTRA_LOOP_DECLS
  389. #undef INIT_PARAMS
  390. #undef UPDATE_PARAMS
  391. #undef REINIT_PARAMS
  392. #undef ONEBYTE_BODY
  393. #undef UNPACK_BYTES
  394. #undef CLEAR_STATE
  395. #undef LOOP_NEED_STATE
  396. #undef LOOP_NEED_FLAGS
  397. #undef LOOP_NEED_DATA