Some additional configuration.
[gedcom-parse.git] / iconv / glibc / skeleton.c
1 /* This file is taken from the glibc sources (release 2.2.4).
2    Only this initial comment has been added.  The next comment
3    gives the original copyright notice.
4 */
5
6 /* Skeleton for a conversion module.
7    Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
8    This file is part of the GNU C Library.
9    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
10
11    The GNU C Library is free software; you can redistribute it and/or
12    modify it under the terms of the GNU Lesser General Public
13    License as published by the Free Software Foundation; either
14    version 2.1 of the License, or (at your option) any later version.
15
16    The GNU C Library is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19    Lesser General Public License for more details.
20
21    You should have received a copy of the GNU Lesser General Public
22    License along with the GNU C Library; if not, write to the Free
23    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
24    02111-1307 USA.  */
25
26 /* This file can be included to provide definitions of several things
27    many modules have in common.  It can be customized using the following
28    macros:
29
30      DEFINE_INIT        define the default initializer.  This requires the
31                         following symbol to be defined.
32
33      CHARSET_NAME       string with official name of the coded character
34                         set (in all-caps)
35
36      DEFINE_FINI        define the default destructor function.
37
38      MIN_NEEDED_FROM    minimal number of bytes needed for the from-charset.
39      MIN_NEEDED_TO      likewise for the to-charset.
40
41      MAX_NEEDED_FROM    maximal number of bytes needed for the from-charset.
42                         This macro is optional, it defaults to MIN_NEEDED_FROM.
43      MAX_NEEDED_TO      likewise for the to-charset.
44
45      DEFINE_DIRECTION_OBJECTS
46                         two objects will be defined to be used when the
47                         `gconv' function must only distinguish two
48                         directions.  This is implied by DEFINE_INIT.
49                         If this macro is not defined the following
50                         macro must be available.
51
52      FROM_DIRECTION     this macro is supposed to return a value != 0
53                         if we convert from the current character set,
54                         otherwise it return 0.
55
56      EMIT_SHIFT_TO_INIT this symbol is optional.  If it is defined it
57                         defines some code which writes out a sequence
58                         of characters which bring the current state into
59                         the initial state.
60
61      FROM_LOOP          name of the function implementing the conversion
62                         from the current characters.
63      TO_LOOP            likewise for the other direction
64
65      ONE_DIRECTION      optional.  If defined to 1, only one conversion
66                         direction is defined instead of two.  In this
67                         case, FROM_DIRECTION should be defined to 1, and
68                         FROM_LOOP and TO_LOOP should have the same value.
69
70      SAVE_RESET_STATE   in case of an error we must reset the state for
71                         the rerun so this macro must be defined for
72                         stateful encodings.  It takes an argument which
73                         is nonzero when saving.
74
75      RESET_INPUT_BUFFER If the input character sets allow this the macro
76                         can be defined to reset the input buffer pointers
77                         to cover only those characters up to the error.
78
79      FUNCTION_NAME      if not set the conversion function is named `gconv'.
80
81      PREPARE_LOOP       optional code preparing the conversion loop.  Can
82                         contain variable definitions.
83      END_LOOP           also optional, may be used to store information
84
85      EXTRA_LOOP_ARGS    optional macro specifying extra arguments passed
86                         to loop function.
87  */
88
89 #include <assert.h>
90 #include <gconv.h>
91 #include <string.h>
92 #define __need_size_t
93 #define __need_NULL
94 #include <stddef.h>
95
96 #ifndef STATIC_GCONV
97 # include <dlfcn.h>
98 #endif
99
100 #ifndef DL_CALL_FCT
101 # define DL_CALL_FCT(fct, args) fct args
102 #endif
103
104 /* The direction objects.  */
105 #if DEFINE_DIRECTION_OBJECTS || DEFINE_INIT
106 static int from_object;
107 static int to_object;
108
109 # ifndef FROM_DIRECTION
110 #  define FROM_DIRECTION (step->__data == &from_object)
111 # endif
112 #else
113 # ifndef FROM_DIRECTION
114 #  error "FROM_DIRECTION must be provided if direction objects are not used"
115 # endif
116 #endif
117
118
119 /* How many bytes are needed at most for the from-charset.  */
120 #ifndef MAX_NEEDED_FROM
121 # define MAX_NEEDED_FROM        MIN_NEEDED_FROM
122 #endif
123
124 /* Same for the to-charset.  */
125 #ifndef MAX_NEEDED_TO
126 # define MAX_NEEDED_TO          MIN_NEEDED_TO
127 #endif
128
129
130 /* Define macros which can access unaligned buffers.  These macros are
131    supposed to be used only in code outside the inner loops.  For the inner
132    loops we have other definitions which allow optimized access.  */
133 #ifdef _STRING_ARCH_unaligned
134 /* We can handle unaligned memory access.  */
135 # define get16u(addr) *((__const uint16_t *) (addr))
136 # define get32u(addr) *((__const uint32_t *) (addr))
137
138 /* We need no special support for writing values either.  */
139 # define put16u(addr, val) *((uint16_t *) (addr)) = (val)
140 # define put32u(addr, val) *((uint32_t *) (addr)) = (val)
141 #else
142 /* Distinguish between big endian and little endian.  */
143 # if __BYTE_ORDER == __LITTLE_ENDIAN
144 #  define get16u(addr) \
145      (((__const unsigned char *) (addr))[1] << 8                              \
146       | ((__const unsigned char *) (addr))[0])
147 #  define get32u(addr) \
148      (((((__const unsigned char *) (addr))[3] << 8                            \
149         | ((__const unsigned char *) (addr))[2]) << 8                         \
150        | ((__const unsigned char *) (addr))[1]) << 8                          \
151       | ((__const unsigned char *) (addr))[0])
152
153 #  define put16u(addr, val) \
154      ({ uint16_t __val = (val);                                               \
155         ((unsigned char *) (addr))[0] = __val;                                \
156         ((unsigned char *) (addr))[1] = __val >> 8;                           \
157         (void) 0; })
158 #  define put32u(addr, val) \
159      ({ uint32_t __val = (val);                                               \
160         ((unsigned char *) (addr))[0] = __val;                                \
161         __val >>= 8;                                                          \
162         ((unsigned char *) (addr))[1] = __val;                                \
163         __val >>= 8;                                                          \
164         ((unsigned char *) (addr))[2] = __val;                                \
165         __val >>= 8;                                                          \
166         ((unsigned char *) (addr))[3] = __val;                                \
167         (void) 0; })
168 # else
169 #  define get16u(addr) \
170      (((__const unsigned char *) (addr))[0] << 8                              \
171       | ((__const unsigned char *) (addr))[1])
172 #  define get32u(addr) \
173      (((((__const unsigned char *) (addr))[0] << 8                            \
174         | ((__const unsigned char *) (addr))[1]) << 8                         \
175        | ((__const unsigned char *) (addr))[2]) << 8                          \
176       | ((__const unsigned char *) (addr))[3])
177
178 #  define put16u(addr, val) \
179      ({ uint16_t __val = (val);                                               \
180         ((unsigned char *) (addr))[1] = __val;                                \
181         ((unsigned char *) (addr))[0] = __val >> 8;                           \
182         (void) 0; })
183 #  define put32u(addr, val) \
184      ({ uint32_t __val = (val);                                               \
185         ((unsigned char *) (addr))[3] = __val;                                \
186         __val >>= 8;                                                          \
187         ((unsigned char *) (addr))[2] = __val;                                \
188         __val >>= 8;                                                          \
189         ((unsigned char *) (addr))[1] = __val;                                \
190         __val >>= 8;                                                          \
191         ((unsigned char *) (addr))[0] = __val;                                \
192         (void) 0; })
193 # endif
194 #endif
195
196
197 /* For conversions from a fixed width character set to another fixed width
198    character set we can define RESET_INPUT_BUFFER in a very fast way.  */
199 #if !defined RESET_INPUT_BUFFER && !defined SAVE_RESET_STATE
200 # if MIN_NEEDED_FROM == MAX_NEEDED_FROM && MIN_NEEDED_TO == MAX_NEEDED_TO
201 /* We have to use these `if's here since the compiler cannot know that
202    (outbuf - outerr) is always divisible by MIN_NEEDED_TO.  */
203 #  define RESET_INPUT_BUFFER \
204   if (MIN_NEEDED_FROM % MIN_NEEDED_TO == 0)                                   \
205     *inptrp -= (outbuf - outerr) * (MIN_NEEDED_FROM / MIN_NEEDED_TO);         \
206   else if (MIN_NEEDED_TO % MIN_NEEDED_FROM == 0)                              \
207     *inptrp -= (outbuf - outerr) / (MIN_NEEDED_TO / MIN_NEEDED_FROM);         \
208   else                                                                        \
209     *inptrp -= ((outbuf - outerr) / MIN_NEEDED_TO) * MIN_NEEDED_FROM
210 # endif
211 #endif
212
213
214 /* The default init function.  It simply matches the name and initializes
215    the step data to point to one of the objects above.  */
216 #if DEFINE_INIT
217 # ifndef CHARSET_NAME
218 #  error "CHARSET_NAME not defined"
219 # endif
220
221 extern int gconv_init (struct __gconv_step *step);
222 int
223 gconv_init (struct __gconv_step *step)
224 {
225   /* Determine which direction.  */
226   if (strcmp (step->__from_name, CHARSET_NAME) == 0)
227     {
228       step->__data = &from_object;
229
230       step->__min_needed_from = MIN_NEEDED_FROM;
231       step->__max_needed_from = MAX_NEEDED_FROM;
232       step->__min_needed_to = MIN_NEEDED_TO;
233       step->__max_needed_to = MAX_NEEDED_TO;
234     }
235   else if (__builtin_expect (strcmp (step->__to_name, CHARSET_NAME), 0) == 0)
236     {
237       step->__data = &to_object;
238
239       step->__min_needed_from = MIN_NEEDED_TO;
240       step->__max_needed_from = MAX_NEEDED_TO;
241       step->__min_needed_to = MIN_NEEDED_FROM;
242       step->__max_needed_to = MAX_NEEDED_FROM;
243     }
244   else
245     return __GCONV_NOCONV;
246
247 #ifdef SAVE_RESET_STATE
248   step->__stateful = 1;
249 #else
250   step->__stateful = 0;
251 #endif
252
253   return __GCONV_OK;
254 }
255 #endif
256
257
258 /* The default destructor function does nothing in the moment and so
259    we don't define it at all.  But we still provide the macro just in
260    case we need it some day.  */
261 #if DEFINE_FINI
262 #endif
263
264
265 /* If no arguments have to passed to the loop function define the macro
266    as empty.  */
267 #ifndef EXTRA_LOOP_ARGS
268 # define EXTRA_LOOP_ARGS
269 #endif
270
271
272 /* This is the actual conversion function.  */
273 #ifndef FUNCTION_NAME
274 # define FUNCTION_NAME  gconv
275 #endif
276
277 /* The macros are used to access the function to convert single characters.  */
278 #define SINGLE(fct) SINGLE2 (fct)
279 #define SINGLE2(fct) fct##_single
280
281
282 extern int FUNCTION_NAME (struct __gconv_step *step,
283                           struct __gconv_step_data *data,
284                           const unsigned char **inptrp,
285                           const unsigned char *inend,
286                           unsigned char **outbufstart, size_t *irreversible,
287                           int do_flush, int consume_incomplete);
288 int
289 FUNCTION_NAME (struct __gconv_step *step, struct __gconv_step_data *data,
290                const unsigned char **inptrp, const unsigned char *inend,
291                unsigned char **outbufstart, size_t *irreversible, int do_flush,
292                int consume_incomplete)
293 {
294   struct __gconv_step *next_step = step + 1;
295   struct __gconv_step_data *next_data = data + 1;
296   __gconv_fct fct;
297   int status;
298
299   fct = (data->__flags & __GCONV_IS_LAST) ? NULL : next_step->__fct;
300
301   /* If the function is called with no input this means we have to reset
302      to the initial state.  The possibly partly converted input is
303      dropped.  */
304   if (__builtin_expect (do_flush, 0))
305     {
306       /* This should never happen during error handling.  */
307       assert (outbufstart == NULL);
308
309       status = __GCONV_OK;
310
311 #ifdef EMIT_SHIFT_TO_INIT
312       if (do_flush == 1)
313         {
314           /* We preserve the initial values of the pointer variables.  */
315           unsigned char *outbuf = data->__outbuf;
316           unsigned char *outstart = outbuf;
317           unsigned char *outend = data->__outbufend;
318
319 # ifdef PREPARE_LOOP
320           PREPARE_LOOP
321 # endif
322
323 # ifdef SAVE_RESET_STATE
324           SAVE_RESET_STATE (1);
325 # endif
326
327           /* Emit the escape sequence to reset the state.  */
328           EMIT_SHIFT_TO_INIT;
329
330           /* Call the steps down the chain if there are any but only if we
331              successfully emitted the escape sequence.  This should only
332              fail if the output buffer is full.  If the input is invalid
333              it should be discarded since the user wants to start from a
334              clean state.  */
335           if (status == __GCONV_OK)
336             {
337               if (data->__flags & __GCONV_IS_LAST)
338                 /* Store information about how many bytes are available.  */
339                 data->__outbuf = outbuf;
340               else
341                 {
342                   /* Write out all output which was produced.  */
343                   if (outbuf > outstart)
344                     {
345                       const unsigned char *outerr = outstart;
346                       int result;
347
348                       result = DL_CALL_FCT (fct, (next_step, next_data,
349                                                   &outerr, outbuf, NULL,
350                                                   irreversible, 0,
351                                                   consume_incomplete));
352
353                       if (result != __GCONV_EMPTY_INPUT)
354                         {
355                           if (__builtin_expect (outerr != outbuf, 0))
356                             {
357                               /* We have a problem.  Undo the conversion.  */
358                               outbuf = outstart;
359
360                               /* Restore the state.  */
361 # ifdef SAVE_RESET_STATE
362                               SAVE_RESET_STATE (0);
363 # endif
364                             }
365
366                           /* Change the status.  */
367                           status = result;
368                         }
369                     }
370
371                   if (status == __GCONV_OK)
372                     /* Now flush the remaining steps.  */
373                     status = DL_CALL_FCT (fct, (next_step, next_data, NULL,
374                                                 NULL, NULL, irreversible, 1,
375                                                 consume_incomplete));
376                 }
377             }
378         }
379       else
380 #endif
381         {
382           /* Clear the state object.  There might be bytes in there from
383              previous calls with CONSUME_INCOMPLETE == 1.  But don't emit
384              escape sequences.  */
385           memset (data->__statep, '\0', sizeof (*data->__statep));
386
387           if (! (data->__flags & __GCONV_IS_LAST))
388             /* Now flush the remaining steps.  */
389             status = DL_CALL_FCT (fct, (next_step, next_data, NULL, NULL,
390                                         NULL, irreversible, do_flush,
391                                         consume_incomplete));
392         }
393     }
394   else
395     {
396       /* We preserve the initial values of the pointer variables.  */
397       const unsigned char *inptr = *inptrp;
398       unsigned char *outbuf = (__builtin_expect (outbufstart == NULL, 1)
399                                ? data->__outbuf : *outbufstart);
400       unsigned char *outend = data->__outbufend;
401       unsigned char *outstart;
402       /* This variable is used to count the number of characters we
403          actually converted.  */
404       size_t lirreversible = 0;
405       size_t *lirreversiblep = irreversible ? &lirreversible : NULL;
406 #if defined _STRING_ARCH_unaligned \
407     || MIN_NEEDED_FROM == 1 || MAX_NEEDED_FROM % MIN_NEEDED_FROM != 0 \
408     || MIN_NEEDED_TO == 1 || MAX_NEEDED_TO % MIN_NEEDED_TO != 0
409 # define unaligned 0
410 #else
411       int unaligned;
412 # define GEN_unaligned(name) GEN_unaligned2 (name)
413 # define GEN_unaligned2(name) name##_unaligned
414 #endif
415
416 #ifdef PREPARE_LOOP
417       PREPARE_LOOP
418 #endif
419
420 #if MAX_NEEDED_FROM > 1 || MAX_NEEDED_TO > 1
421       /* If the function is used to implement the mb*towc*() or wc*tomb*()
422          functions we must test whether any bytes from the last call are
423          stored in the `state' object.  */
424       if (((MAX_NEEDED_FROM > 1 && MAX_NEEDED_TO > 1)
425            || (MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
426            || (MAX_NEEDED_TO > 1 && !FROM_DIRECTION))
427           && consume_incomplete && (data->__statep->__count & 7) != 0)
428         {
429           /* Yep, we have some bytes left over.  Process them now.
430              But this must not happen while we are called from an
431              error handler.  */
432           assert (outbufstart == NULL);
433
434 # if MAX_NEEDED_FROM > 1
435           if (MAX_NEEDED_TO == 1 || FROM_DIRECTION)
436             status = SINGLE(FROM_LOOP) (step, data, inptrp, inend, &outbuf,
437                                         outend, lirreversiblep
438                                         EXTRA_LOOP_ARGS);
439 # endif
440 # if MAX_NEEDED_FROM > 1 && MAX_NEEDED_TO > 1 && !ONE_DIRECTION
441           else
442 # endif
443 # if MAX_NEEDED_TO > 1 && !ONE_DIRECTION
444             status = SINGLE(TO_LOOP) (step, data, inptrp, inend, &outbuf,
445                                       outend, lirreversiblep EXTRA_LOOP_ARGS);
446 # endif
447
448           if (__builtin_expect (status, __GCONV_OK) != __GCONV_OK)
449             return status;
450         }
451 #endif
452
453 #if !defined _STRING_ARCH_unaligned \
454     && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
455     && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
456       /* The following assumes that encodings, which have a variable length
457          what might unalign a buffer even though it is a aligned in the
458          beginning, either don't have the minimal number of bytes as a divisor
459          of the maximum length or have a minimum length of 1.  This is true
460          for all known and supported encodings.  */
461       unaligned = ((FROM_DIRECTION
462                     && ((uintptr_t) inptr % MIN_NEEDED_FROM != 0
463                         || ((data->__flags & __GCONV_IS_LAST)
464                             && (uintptr_t) outbuf % MIN_NEEDED_TO != 0)))
465                    || (!FROM_DIRECTION
466                        && (((data->__flags & __GCONV_IS_LAST)
467                             && (uintptr_t) outbuf % MIN_NEEDED_FROM != 0)
468                            || (uintptr_t) inptr % MIN_NEEDED_TO != 0)));
469 #endif
470
471       while (1)
472         {
473           struct __gconv_trans_data *trans;
474
475           /* Remember the start value for this round.  */
476           inptr = *inptrp;
477           /* The outbuf buffer is empty.  */
478           outstart = outbuf;
479
480 #ifdef SAVE_RESET_STATE
481           SAVE_RESET_STATE (1);
482 #endif
483
484           if (__builtin_expect (!unaligned, 1))
485             {
486               if (FROM_DIRECTION)
487                 /* Run the conversion loop.  */
488                 status = FROM_LOOP (step, data, inptrp, inend, &outbuf, outend,
489                                     lirreversiblep EXTRA_LOOP_ARGS);
490               else
491                 /* Run the conversion loop.  */
492                 status = TO_LOOP (step, data, inptrp, inend, &outbuf, outend,
493                                   lirreversiblep EXTRA_LOOP_ARGS);
494             }
495 #if !defined _STRING_ARCH_unaligned \
496     && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
497     && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
498           else
499             {
500               if (FROM_DIRECTION)
501                 /* Run the conversion loop.  */
502                 status = GEN_unaligned (FROM_LOOP) (step, data, inptrp, inend,
503                                                     &outbuf, outend,
504                                                     lirreversiblep
505                                                     EXTRA_LOOP_ARGS);
506               else
507                 /* Run the conversion loop.  */
508                 status = GEN_unaligned (TO_LOOP) (step, data, inptrp, inend,
509                                                   &outbuf, outend,
510                                                   lirreversiblep
511                                                   EXTRA_LOOP_ARGS);
512             }
513 #endif
514
515           /* If we were called as part of an error handling module we
516              don't do anything else here.  */
517           if (__builtin_expect (outbufstart != NULL, 0))
518             {
519               *outbufstart = outbuf;
520               return status;
521             }
522
523           /* Give the transliteration module the chance to store the
524              original text and the result in case it needs a context.  */
525           for (trans = data->__trans; trans != NULL; trans = trans->__next)
526             if (trans->__trans_context_fct != NULL)
527               DL_CALL_FCT (trans->__trans_context_fct,
528                            (trans->__data, inptr, *inptrp, outstart, outbuf));
529
530           /* We finished one use of the loops.  */
531           ++data->__invocation_counter;
532
533           /* If this is the last step leave the loop, there is nothing
534              we can do.  */
535           if (__builtin_expect (data->__flags & __GCONV_IS_LAST, 0))
536             {
537               /* Store information about how many bytes are available.  */
538               data->__outbuf = outbuf;
539
540               /* Remember how many non-identical characters we
541                  converted in a irreversible way.  */
542               *irreversible += lirreversible;
543
544               break;
545             }
546
547           /* Write out all output which was produced.  */
548           if (__builtin_expect (outbuf > outstart, 1))
549             {
550               const unsigned char *outerr = data->__outbuf;
551               int result;
552
553               result = DL_CALL_FCT (fct, (next_step, next_data, &outerr,
554                                           outbuf, NULL, irreversible, 0,
555                                           consume_incomplete));
556
557               if (result != __GCONV_EMPTY_INPUT)
558                 {
559                   if (__builtin_expect (outerr != outbuf, 0))
560                     {
561 #ifdef RESET_INPUT_BUFFER
562                       RESET_INPUT_BUFFER;
563 #else
564                       /* We have a problem with the in on of the functions
565                          below.  Undo the conversion upto the error point.  */
566                       size_t nstatus;
567
568                       /* Reload the pointers.  */
569                       *inptrp = inptr;
570                       outbuf = outstart;
571
572                       /* Restore the state.  */
573 # ifdef SAVE_RESET_STATE
574                       SAVE_RESET_STATE (0);
575 # endif
576
577                       if (__builtin_expect (!unaligned, 1))
578                         {
579                           if (FROM_DIRECTION)
580                             /* Run the conversion loop.  */
581                             nstatus = FROM_LOOP (step, data, inptrp, inend,
582                                                  &outbuf, outerr,
583                                                  lirreversiblep
584                                                  EXTRA_LOOP_ARGS);
585                           else
586                             /* Run the conversion loop.  */
587                             nstatus = TO_LOOP (step, data, inptrp, inend,
588                                                &outbuf, outerr,
589                                                lirreversiblep
590                                                EXTRA_LOOP_ARGS);
591                         }
592 # if !defined _STRING_ARCH_unaligned \
593      && MIN_NEEDED_FROM != 1 && MAX_NEEDED_FROM % MIN_NEEDED_FROM == 0 \
594      && MIN_NEEDED_TO != 1 && MAX_NEEDED_TO % MIN_NEEDED_TO == 0
595                       else
596                         {
597                           if (FROM_DIRECTION)
598                             /* Run the conversion loop.  */
599                             nstatus = GEN_unaligned (FROM_LOOP) (step, data,
600                                                                  inptrp, inend,
601                                                                  &outbuf,
602                                                                  outerr,
603                                                                  lirreversiblep
604                                                                  EXTRA_LOOP_ARGS);
605                           else
606                             /* Run the conversion loop.  */
607                             nstatus = GEN_unaligned (TO_LOOP) (step, data,
608                                                                inptrp, inend,
609                                                                &outbuf, outerr,
610                                                                lirreversiblep
611                                                                EXTRA_LOOP_ARGS);
612                         }
613 # endif
614
615                       /* We must run out of output buffer space in this
616                          rerun.  */
617                       assert (outbuf == outerr);
618                       assert (nstatus == __GCONV_FULL_OUTPUT);
619
620                       /* If we haven't consumed a single byte decrement
621                          the invocation counter.  */
622                       if (__builtin_expect (outbuf == outstart, 0))
623                         --data->__invocation_counter;
624 #endif  /* reset input buffer */
625                     }
626
627                   /* Change the status.  */
628                   status = result;
629                 }
630               else
631                 /* All the output is consumed, we can make another run
632                    if everything was ok.  */
633                 if (status == __GCONV_FULL_OUTPUT)
634                   {
635                     status = __GCONV_OK;
636                     outbuf = data->__outbuf;
637                   }
638             }
639
640           if (status != __GCONV_OK)
641             break;
642
643           /* Reset the output buffer pointer for the next round.  */
644           outbuf = data->__outbuf;
645         }
646
647 #ifdef END_LOOP
648       END_LOOP
649 #endif
650
651       /* If we are supposed to consume all character store now all of the
652          remaining characters in the `state' object.  */
653 #if MAX_NEEDED_FROM > 1 || MAX_NEEDED_TO > 1
654       if (((MAX_NEEDED_FROM > 1 && MAX_NEEDED_TO > 1)
655            || (MAX_NEEDED_FROM > 1 && FROM_DIRECTION)
656            || (MAX_NEEDED_TO > 1 && !FROM_DIRECTION))
657           && __builtin_expect (consume_incomplete, 0)
658           && status == __GCONV_INCOMPLETE_INPUT)
659         {
660 # ifdef STORE_REST
661           mbstate_t *state = data->__statep;
662
663           STORE_REST
664 # else
665           size_t cnt;
666
667           /* Make sure the remaining bytes fit into the state objects
668              buffer.  */
669           assert (inend - *inptrp < 4);
670
671           for (cnt = 0; *inptrp < inend; ++cnt)
672             data->__statep->__value.__wchb[cnt] = *(*inptrp)++;
673           data->__statep->__count &= ~7;
674           data->__statep->__count |= cnt;
675 # endif
676         }
677 #endif
678     }
679
680   return status;
681 }
682
683 #undef DEFINE_INIT
684 #undef CHARSET_NAME
685 #undef DEFINE_FINI
686 #undef MIN_NEEDED_FROM
687 #undef MIN_NEEDED_TO
688 #undef MAX_NEEDED_FROM
689 #undef MAX_NEEDED_TO
690 #undef DEFINE_DIRECTION_OBJECTS
691 #undef FROM_DIRECTION
692 #undef EMIT_SHIFT_TO_INIT
693 #undef FROM_LOOP
694 #undef TO_LOOP
695 #undef SAVE_RESET_STATE
696 #undef RESET_INPUT_BUFFER
697 #undef FUNCTION_NAME
698 #undef PREPARE_LOOP
699 #undef END_LOOP
700 #undef ONE_DIRECTION
701 #undef STORE_REST