skeleton.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. /* Skeleton for a conversion module.
  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 can be included to provide definitions of several things
  16. many modules have in common. It can be customized using the following
  17. macros:
  18. DEFINE_INIT define the default initializer. This requires the
  19. following symbol to be defined.
  20. CHARSET_NAME string with official name of the coded character
  21. set (in all-caps)
  22. DEFINE_FINI define the default destructor function.
  23. MIN_NEEDED_FROM minimal number of bytes needed for the from-charset.
  24. MIN_NEEDED_TO likewise for the to-charset.
  25. MAX_NEEDED_FROM maximal number of bytes needed for the from-charset.
  26. This macro is optional, it defaults to MIN_NEEDED_FROM.
  27. MAX_NEEDED_TO likewise for the to-charset.
  28. FROM_LOOP_MIN_NEEDED_FROM
  29. FROM_LOOP_MAX_NEEDED_FROM
  30. minimal/maximal number of bytes needed on input
  31. of one round through the FROM_LOOP. Defaults
  32. to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
  33. FROM_LOOP_MIN_NEEDED_TO
  34. FROM_LOOP_MAX_NEEDED_TO
  35. minimal/maximal number of bytes needed on output
  36. of one round through the FROM_LOOP. Defaults
  37. to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
  38. TO_LOOP_MIN_NEEDED_FROM
  39. TO_LOOP_MAX_NEEDED_FROM
  40. minimal/maximal number of bytes needed on input
  41. of one round through the TO_LOOP. Defaults
  42. to MIN_NEEDED_TO and MAX_NEEDED_TO, respectively.
  43. TO_LOOP_MIN_NEEDED_TO
  44. TO_LOOP_MAX_NEEDED_TO
  45. minimal/maximal number of bytes needed on output
  46. of one round through the TO_LOOP. Defaults
  47. to MIN_NEEDED_FROM and MAX_NEEDED_FROM, respectively.
  48. FROM_DIRECTION this macro is supposed to return a value != 0
  49. if we convert from the current character set,
  50. otherwise it return 0.
  51. EMIT_SHIFT_TO_INIT this symbol is optional. If it is defined it
  52. defines some code which writes out a sequence
  53. of bytes which bring the current state into
  54. the initial state.
  55. FROM_LOOP name of the function implementing the conversion
  56. from the current character set.
  57. TO_LOOP likewise for the other direction
  58. ONE_DIRECTION optional. If defined to 1, only one conversion
  59. direction is defined instead of two. In this
  60. case, FROM_DIRECTION should be defined to 1, and
  61. FROM_LOOP and TO_LOOP should have the same value.
  62. SAVE_RESET_STATE in case of an error we must reset the state for
  63. the rerun so this macro must be defined for
  64. stateful encodings. It takes an argument which
  65. is nonzero when saving.
  66. RESET_INPUT_BUFFER If the input character sets allow this the macro
  67. can be defined to reset the input buffer pointers
  68. to cover only those characters up to the error.
  69. Note that if the conversion has skipped over
  70. irreversible characters (due to
  71. __GCONV_IGNORE_ERRORS) there is no longer a direct
  72. correspondence between input and output pointers,
  73. and this macro is not called.
  74. FUNCTION_NAME if not set the conversion function is named `gconv'.
  75. PREPARE_LOOP optional code preparing the conversion loop. Can
  76. contain variable definitions.
  77. END_LOOP also optional, may be used to store information
  78. EXTRA_LOOP_ARGS optional macro specifying extra arguments passed
  79. to loop function.
  80. STORE_REST optional, needed only when MAX_NEEDED_FROM > 4.
  81. This macro stores the seen but unconverted input bytes
  82. in the state.
  83. FROM_ONEBYTE optional. If defined, should be the name of a
  84. specialized conversion function for a single byte
  85. from the current character set to INTERNAL. This
  86. function has prototype
  87. wint_t
  88. FROM_ONEBYTE (struct __gconv_step *, unsigned char);
  89. and does a special conversion:
  90. - The input is a single byte.
  91. - The output is a single uint32_t.
  92. - The state before the conversion is the initial state;
  93. the state after the conversion is irrelevant.
  94. - No transliteration.
  95. - __invocation_counter = 0.
  96. - __internal_use = 1.
  97. - do_flush = 0.
  98. Modules can use mbstate_t to store conversion state as follows:
  99. * Bits 2..0 of '__count' contain the number of lookahead input bytes
  100. stored in __value.__wchb. Always zero if the converter never
  101. returns __GCONV_INCOMPLETE_INPUT.
  102. * Bits 31..3 of '__count' are module dependent shift state.
  103. * __value: When STORE_REST/UNPACK_BYTES aren't defined and when the
  104. converter has returned __GCONV_INCOMPLETE_INPUT, this contains
  105. at most 4 lookahead bytes. Converters with an mb_cur_max > 4
  106. (currently only UTF-8) must find a way to store their state
  107. in __value.__wch and define STORE_REST/UNPACK_BYTES appropriately.
  108. When __value contains lookahead, __count must not be zero, because
  109. the converter is not in the initial state then, and mbsinit() --
  110. defined as a (__count == 0) test -- must reflect this.
  111. */
  112. #include <assert.h>
  113. #include <iconv/gconv_int.h>
  114. #include <string.h>
  115. #define __need_size_t
  116. #define __need_NULL
  117. #include <stddef.h>
  118. #ifndef STATIC_GCONV
  119. # include <dlfcn.h>
  120. #endif
  121. #include <pointer_guard.h>
  122. #include <stdint.h>
  123. #ifndef DL_CALL_FCT
  124. # define DL_CALL_FCT(fct, args) fct args
  125. #endif
  126. /* The direction objects. */
  127. #if DEFINE_INIT
  128. # ifndef FROM_DIRECTION
  129. # define FROM_DIRECTION_VAL NULL
  130. # define TO_DIRECTION_VAL ((void *) ~((uintptr_t) 0))
  131. # define FROM_DIRECTION (step->__data == FROM_DIRECTION_VAL)
  132. # endif
  133. #else
  134. # ifndef FROM_DIRECTION
  135. # error "FROM_DIRECTION must be provided if non-default init is used"
  136. # endif
  137. #endif
  138. /* How many bytes are needed at most for the from-charset. */
  139. #ifndef MAX_NEEDED_FROM
  140. # define MAX_NEEDED_FROM MIN_NEEDED_FROM
  141. #endif
  142. /* Same for the to-charset. */
  143. #ifndef MAX_NEEDED_TO
  144. # define MAX_NEEDED_TO MIN_NEEDED_TO
  145. #endif
  146. /* Defaults for the per-direction min/max constants. */
  147. #ifndef FROM_LOOP_MIN_NEEDED_FROM
  148. # define FROM_LOOP_MIN_NEEDED_FROM MIN_NEEDED_FROM
  149. #endif
  150. #ifndef FROM_LOOP_MAX_NEEDED_FROM
  151. # define FROM_LOOP_MAX_NEEDED_FROM MAX_NEEDED_FROM
  152. #endif
  153. #ifndef FROM_LOOP_MIN_NEEDED_TO
  154. # define FROM_LOOP_MIN_NEEDED_TO MIN_NEEDED_TO
  155. #endif
  156. #ifndef FROM_LOOP_MAX_NEEDED_TO
  157. # define FROM_LOOP_MAX_NEEDED_TO MAX_NEEDED_TO
  158. #endif
  159. #ifndef TO_LOOP_MIN_NEEDED_FROM
  160. # define TO_LOOP_MIN_NEEDED_FROM MIN_NEEDED_TO
  161. #endif
  162. #ifndef TO_LOOP_MAX_NEEDED_FROM
  163. # define TO_LOOP_MAX_NEEDED_FROM MAX_NEEDED_TO
  164. #endif
  165. #ifndef TO_LOOP_MIN_NEEDED_TO
  166. # define TO_LOOP_MIN_NEEDED_TO MIN_NEEDED_FROM
  167. #endif
  168. #ifndef TO_LOOP_MAX_NEEDED_TO
  169. # define TO_LOOP_MAX_NEEDED_TO MAX_NEEDED_FROM
  170. #endif
  171. /* For conversions from a fixed width character set to another fixed width
  172. character set we can define RESET_INPUT_BUFFER in a very fast way. */
  173. #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
  174. # if FROM_LOOP_MIN_NEEDED_FROM == FROM_LOOP_MAX_NEEDED_FROM \
  175. && FROM_LOOP_MIN_NEEDED_TO == FROM_LOOP_MAX_NEEDED_TO \
  176. && TO_LOOP_MIN_NEEDED_FROM == TO_LOOP_MAX_NEEDED_FROM \
  177. && TO_LOOP_MIN_NEEDED_TO == TO_LOOP_MAX_NEEDED_TO
  178. /* We have to use these `if's here since the compiler cannot know that
  179. (outbuf - outerr) is always divisible by FROM/TO_LOOP_MIN_NEEDED_TO.
  180. The ?:1 avoids division by zero warnings that gcc 3.2 emits even for
  181. obviously unreachable code. */
  182. # define RESET_INPUT_BUFFER \
  183. if (FROM_DIRECTION) \
  184. { \
  185. if (FROM_LOOP_MIN_NEEDED_FROM % FROM_LOOP_MIN_NEEDED_TO == 0) \
  186. *inptrp -= (outbuf - outerr) \
  187. * (FROM_LOOP_MIN_NEEDED_FROM / FROM_LOOP_MIN_NEEDED_TO); \
  188. else if (FROM_LOOP_MIN_NEEDED_TO % FROM_LOOP_MIN_NEEDED_FROM == 0) \
  189. *inptrp -= (outbuf - outerr) \
  190. / (FROM_LOOP_MIN_NEEDED_TO / FROM_LOOP_MIN_NEEDED_FROM \
  191. ? : 1); \
  192. else \
  193. *inptrp -= ((outbuf - outerr) / FROM_LOOP_MIN_NEEDED_TO) \
  194. * FROM_LOOP_MIN_NEEDED_FROM; \
  195. } \
  196. else \
  197. { \
  198. if (TO_LOOP_MIN_NEEDED_FROM % TO_LOOP_MIN_NEEDED_TO == 0) \
  199. *inptrp -= (outbuf - outerr) \
  200. * (TO_LOOP_MIN_NEEDED_FROM / TO_LOOP_MIN_NEEDED_TO); \
  201. else if (TO_LOOP_MIN_NEEDED_TO % TO_LOOP_MIN_NEEDED_FROM == 0) \
  202. *inptrp -= (outbuf - outerr) \
  203. / (TO_LOOP_MIN_NEEDED_TO / TO_LOOP_MIN_NEEDED_FROM ? : 1); \
  204. else \
  205. *inptrp -= ((outbuf - outerr) / TO_LOOP_MIN_NEEDED_TO) \
  206. * TO_LOOP_MIN_NEEDED_FROM; \
  207. }
  208. # endif
  209. #endif
  210. /* The default init function. It simply matches the name and initializes
  211. the step data to point to one of the objects above. */
  212. #if DEFINE_INIT
  213. # ifndef CHARSET_NAME
  214. # error "CHARSET_NAME not defined"
  215. # endif
  216. extern int gconv_init (struct __gconv_step *step);
  217. int
  218. gconv_init (struct __gconv_step *step)
  219. {
  220. /* Determine which direction. */
  221. if (strcmp (step->__from_name, CHARSET_NAME) == 0)
  222. {
  223. step->__data = FROM_DIRECTION_VAL;
  224. step->__min_needed_from = FROM_LOOP_MIN_NEEDED_FROM;
  225. step->__max_needed_from = FROM_LOOP_MAX_NEEDED_FROM;
  226. step->__min_needed_to = FROM_LOOP_MIN_NEEDED_TO;
  227. step->__max_needed_to = FROM_LOOP_MAX_NEEDED_TO;
  228. #ifdef FROM_ONEBYTE
  229. step->__btowc_fct = FROM_ONEBYTE;
  230. #endif
  231. }
  232. else if (__builtin_expect (strcmp (step->__to_name, CHARSET_NAME), 0) == 0)
  233. {
  234. step->__data = TO_DIRECTION_VAL;
  235. step->__min_needed_from = TO_LOOP_MIN_NEEDED_FROM;
  236. step->__max_needed_from = TO_LOOP_MAX_NEEDED_FROM;
  237. step->__min_needed_to = TO_LOOP_MIN_NEEDED_TO;
  238. step->__max_needed_to = TO_LOOP_MAX_NEEDED_TO;
  239. }
  240. else
  241. return __GCONV_NOCONV;
  242. #ifdef SAVE_RESET_STATE
  243. step->__stateful = 1;
  244. #else
  245. step->__stateful = 0;
  246. #endif
  247. return __GCONV_OK;
  248. }
  249. #endif
  250. /* The default destructor function does nothing in the moment and so
  251. we don't define it at all. But we still provide the macro just in
  252. case we need it some day. */
  253. #if DEFINE_FINI
  254. #endif
  255. /* If no arguments have to passed to the loop function define the macro
  256. as empty. */
  257. #ifndef EXTRA_LOOP_ARGS
  258. # define EXTRA_LOOP_ARGS
  259. #endif
  260. /* This is the actual conversion function. */
  261. #ifndef FUNCTION_NAME
  262. # define FUNCTION_NAME gconv
  263. #endif
  264. /* The macros are used to access the function to convert single characters. */
  265. #define SINGLE(fct) SINGLE2 (fct)
  266. #define SINGLE2(fct) fct##_single
  267. extern int FUNCTION_NAME (struct __gconv_step *step,
  268. struct __gconv_step_data *data,
  269. const unsigned char **inptrp,
  270. const unsigned char *inend,
  271. unsigned char **outbufstart, size_t *irreversible,
  272. int do_flush, int consume_incomplete);
  273. int
  274. FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data,
  275. const unsigned char **inptrp, const unsigned char *inend,
  276. unsigned char **outbufstart, size_t *irreversible, int do_flush,
  277. int consume_incomplete)
  278. {
  279. struct __gconv_step *next_step = step + 1;
  280. struct __gconv_step_data *next_data = data + 1;
  281. __gconv_fct fct = NULL;
  282. int status;
  283. if ((data->__flags & __GCONV_IS_LAST) == 0)
  284. {
  285. fct = next_step->__fct;
  286. if (next_step->__shlib_handle != NULL)
  287. PTR_DEMANGLE (fct);
  288. }
  289. /* If the function is called with no input this means we have to reset
  290. to the initial state. The possibly partly converted input is
  291. dropped. */
  292. if (__glibc_unlikely (do_flush))
  293. {
  294. /* This should never happen during error handling. */
  295. assert (outbufstart == NULL);
  296. status = __GCONV_OK;
  297. #ifdef EMIT_SHIFT_TO_INIT
  298. if (do_flush == 1)
  299. {
  300. /* We preserve the initial values of the pointer variables. */
  301. unsigned char *outbuf = data->__outbuf;
  302. unsigned char *outstart = outbuf;
  303. unsigned char *outend = data->__outbufend;
  304. # ifdef PREPARE_LOOP
  305. PREPARE_LOOP
  306. # endif
  307. # ifdef SAVE_RESET_STATE
  308. SAVE_RESET_STATE (1);
  309. # endif
  310. /* Emit the escape sequence to reset the state. */
  311. EMIT_SHIFT_TO_INIT;
  312. /* Call the steps down the chain if there are any but only if we
  313. successfully emitted the escape sequence. This should only
  314. fail if the output buffer is full. If the input is invalid
  315. it should be discarded since the user wants to start from a
  316. clean state. */
  317. if (status == __GCONV_OK)
  318. {
  319. if (data->__flags & __GCONV_IS_LAST)
  320. /* Store information about how many bytes are available. */
  321. data->__outbuf = outbuf;
  322. else
  323. {
  324. /* Write out all output which was produced. */
  325. if (outbuf > outstart)
  326. {
  327. const unsigned char *outerr = outstart;
  328. int result;
  329. result = DL_CALL_FCT (fct, (next_step, next_data,
  330. &outerr, outbuf, NULL,
  331. irreversible, 0,
  332. consume_incomplete));
  333. if (result != __GCONV_EMPTY_INPUT)
  334. {
  335. if (__glibc_unlikely (outerr != outbuf))
  336. {
  337. /* We have a problem. Undo the conversion. */
  338. outbuf = outstart;
  339. /* Restore the state. */
  340. # ifdef SAVE_RESET_STATE
  341. SAVE_RESET_STATE (0);
  342. # endif
  343. }
  344. /* Change the status. */
  345. status = result;
  346. }
  347. }
  348. if (status == __GCONV_OK)
  349. /* Now flush the remaining steps. */
  350. status = DL_CALL_FCT (fct, (next_step, next_data, NULL,
  351. NULL, NULL, irreversible, 1,
  352. consume_incomplete));
  353. }
  354. }
  355. }
  356. else
  357. #endif
  358. {
  359. /* Clear the state object. There might be bytes in there from
  360. previous calls with CONSUME_INCOMPLETE == 1. But don't emit
  361. escape sequences. */
  362. memset (data->__statep, '\0', sizeof (*data->__statep));
  363. if (! (data->__flags & __GCONV_IS_LAST))
  364. /* Now flush the remaining steps. */
  365. status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
  366. NULL, irreversible, do_flush,
  367. consume_incomplete));
  368. }
  369. }
  370. else
  371. {
  372. /* We preserve the initial values of the pointer variables,
  373. but only some conversion modules need it. */
  374. const unsigned char *inptr __attribute__ ((__unused__)) = *inptrp;
  375. unsigned char *outbuf = (__builtin_expect (outbufstart == NULL, 1)
  376. ? data->__outbuf : *outbufstart);
  377. unsigned char *outend = data->__outbufend;
  378. unsigned char *outstart;
  379. /* This variable is used to count the number of characters we
  380. actually converted. */
  381. size_t lirreversible = 0;
  382. size_t *lirreversiblep = irreversible ? &lirreversible : NULL;
  383. #ifdef PREPARE_LOOP
  384. PREPARE_LOOP
  385. #endif
  386. #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
  387. /* If the function is used to implement the mb*towc*() or wc*tomb*()
  388. functions we must test whether any bytes from the last call are
  389. stored in the `state' object. */
  390. if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1)
  391. || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
  392. || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION))
  393. && consume_incomplete && (data->__statep->__count & 7) != 0)
  394. {
  395. /* Yep, we have some bytes left over. Process them now.
  396. But this must not happen while we are called from an
  397. error handler. */
  398. assert (outbufstart == NULL);
  399. # if FROM_LOOP_MAX_NEEDED_FROM > 1
  400. if (TO_LOOP_MAX_NEEDED_FROM == 1 || FROM_DIRECTION)
  401. status = SINGLE(FROM_LOOP) (step, data, inptrp, inend, &outbuf,
  402. outend, lirreversiblep
  403. EXTRA_LOOP_ARGS);
  404. # endif
  405. # if !ONE_DIRECTION
  406. # if FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1
  407. else
  408. # endif
  409. # if TO_LOOP_MAX_NEEDED_FROM > 1
  410. status = SINGLE(TO_LOOP) (step, data, inptrp, inend, &outbuf,
  411. outend, lirreversiblep EXTRA_LOOP_ARGS);
  412. # endif
  413. # endif
  414. if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
  415. return status;
  416. }
  417. #endif
  418. while (1)
  419. {
  420. /* Remember the start value for this round. */
  421. inptr = *inptrp;
  422. /* The outbuf buffer is empty. */
  423. outstart = outbuf;
  424. #ifdef RESET_INPUT_BUFFER
  425. /* Remember how many irreversible characters were skipped before
  426. this round. */
  427. size_t loop_irreversible
  428. = lirreversible + (irreversible ? *irreversible : 0);
  429. #endif
  430. #ifdef SAVE_RESET_STATE
  431. SAVE_RESET_STATE (1);
  432. #endif
  433. if (FROM_DIRECTION)
  434. /* Run the conversion loop. */
  435. status = FROM_LOOP (step, data, inptrp, inend, &outbuf, outend,
  436. lirreversiblep EXTRA_LOOP_ARGS);
  437. else
  438. /* Run the conversion loop. */
  439. status = TO_LOOP (step, data, inptrp, inend, &outbuf, outend,
  440. lirreversiblep EXTRA_LOOP_ARGS);
  441. /* If we were called as part of an error handling module we
  442. don't do anything else here. */
  443. if (__glibc_unlikely (outbufstart != NULL))
  444. {
  445. *outbufstart = outbuf;
  446. return status;
  447. }
  448. /* We finished one use of the loops. */
  449. ++data->__invocation_counter;
  450. /* If this is the last step leave the loop, there is nothing
  451. we can do. */
  452. if (__glibc_unlikely (data->__flags & __GCONV_IS_LAST))
  453. {
  454. /* Store information about how many bytes are available. */
  455. data->__outbuf = outbuf;
  456. /* Remember how many non-identical characters we
  457. converted in an irreversible way. */
  458. *irreversible += lirreversible;
  459. break;
  460. }
  461. /* Write out all output which was produced. */
  462. if (__glibc_likely (outbuf > outstart))
  463. {
  464. const unsigned char *outerr = data->__outbuf;
  465. int result;
  466. result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
  467. outbuf, NULL, irreversible, 0,
  468. consume_incomplete));
  469. if (result != __GCONV_EMPTY_INPUT)
  470. {
  471. if (__glibc_unlikely (outerr != outbuf))
  472. {
  473. #ifdef RESET_INPUT_BUFFER
  474. /* RESET_INPUT_BUFFER can only work when there were
  475. no new irreversible characters skipped during
  476. this round. */
  477. if (loop_irreversible
  478. == lirreversible + (irreversible ? *irreversible : 0))
  479. {
  480. RESET_INPUT_BUFFER;
  481. goto done_reset;
  482. }
  483. #endif
  484. /* We have a problem in one of the functions below.
  485. Undo the conversion upto the error point. */
  486. size_t nstatus __attribute__ ((unused));
  487. /* Reload the pointers. */
  488. *inptrp = inptr;
  489. outbuf = outstart;
  490. /* Restore the state. */
  491. #ifdef SAVE_RESET_STATE
  492. SAVE_RESET_STATE (0);
  493. #endif
  494. if (FROM_DIRECTION)
  495. /* Run the conversion loop. */
  496. nstatus = FROM_LOOP (step, data, inptrp, inend,
  497. &outbuf, outerr,
  498. lirreversiblep
  499. EXTRA_LOOP_ARGS);
  500. else
  501. /* Run the conversion loop. */
  502. nstatus = TO_LOOP (step, data, inptrp, inend,
  503. &outbuf, outerr,
  504. lirreversiblep
  505. EXTRA_LOOP_ARGS);
  506. /* We must run out of output buffer space in this
  507. rerun. */
  508. assert (outbuf == outerr);
  509. assert (nstatus == __GCONV_FULL_OUTPUT);
  510. /* If we haven't consumed a single byte decrement
  511. the invocation counter. */
  512. if (__glibc_unlikely (outbuf == outstart))
  513. --data->__invocation_counter;
  514. }
  515. #ifdef RESET_INPUT_BUFFER
  516. done_reset:
  517. #endif
  518. /* Change the status. */
  519. status = result;
  520. }
  521. else
  522. /* All the output is consumed, we can make another run
  523. if everything was ok. */
  524. if (status == __GCONV_FULL_OUTPUT)
  525. {
  526. status = __GCONV_OK;
  527. outbuf = data->__outbuf;
  528. }
  529. }
  530. if (status != __GCONV_OK)
  531. break;
  532. /* Reset the output buffer pointer for the next round. */
  533. outbuf = data->__outbuf;
  534. }
  535. #ifdef END_LOOP
  536. END_LOOP
  537. #endif
  538. /* If we are supposed to consume all character store now all of the
  539. remaining characters in the `state' object. */
  540. #if FROM_LOOP_MAX_NEEDED_FROM > 1 || TO_LOOP_MAX_NEEDED_FROM > 1
  541. if (((FROM_LOOP_MAX_NEEDED_FROM > 1 && TO_LOOP_MAX_NEEDED_FROM > 1)
  542. || (FROM_LOOP_MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
  543. || (TO_LOOP_MAX_NEEDED_FROM > 1 && !FROM_DIRECTION))
  544. && __builtin_expect (consume_incomplete, 0)
  545. && status == __GCONV_INCOMPLETE_INPUT)
  546. {
  547. # ifdef STORE_REST
  548. mbstate_t *state = data->__statep;
  549. STORE_REST
  550. # else
  551. /* Make sure the remaining bytes fit into the state objects
  552. buffer. */
  553. size_t cnt_after = inend - *inptrp;
  554. assert (cnt_after <= sizeof (data->__statep->__value.__wchb));
  555. size_t cnt;
  556. for (cnt = 0; cnt < cnt_after; ++cnt)
  557. data->__statep->__value.__wchb[cnt] = (*inptrp)[cnt];
  558. *inptrp = inend;
  559. data->__statep->__count &= ~7;
  560. data->__statep->__count |= cnt;
  561. # endif
  562. }
  563. #endif
  564. #undef unaligned
  565. #undef POSSIBLY_UNALIGNED
  566. }
  567. return status;
  568. }
  569. #undef DEFINE_INIT
  570. #undef CHARSET_NAME
  571. #undef DEFINE_FINI
  572. #undef MIN_NEEDED_FROM
  573. #undef MIN_NEEDED_TO
  574. #undef MAX_NEEDED_FROM
  575. #undef MAX_NEEDED_TO
  576. #undef FROM_LOOP_MIN_NEEDED_FROM
  577. #undef FROM_LOOP_MAX_NEEDED_FROM
  578. #undef FROM_LOOP_MIN_NEEDED_TO
  579. #undef FROM_LOOP_MAX_NEEDED_TO
  580. #undef TO_LOOP_MIN_NEEDED_FROM
  581. #undef TO_LOOP_MAX_NEEDED_FROM
  582. #undef TO_LOOP_MIN_NEEDED_TO
  583. #undef TO_LOOP_MAX_NEEDED_TO
  584. #undef FROM_DIRECTION
  585. #undef EMIT_SHIFT_TO_INIT
  586. #undef FROM_LOOP
  587. #undef TO_LOOP
  588. #undef ONE_DIRECTION
  589. #undef SAVE_RESET_STATE
  590. #undef RESET_INPUT_BUFFER
  591. #undef FUNCTION_NAME
  592. #undef PREPARE_LOOP
  593. #undef END_LOOP
  594. #undef EXTRA_LOOP_ARGS
  595. #undef STORE_REST
  596. #undef FROM_ONEBYTE