]>
Commit | Line | Data |
---|---|---|
6973fc01 UD |
1 | /* Convert characters in input buffer using conversion descriptor to |
2 | output buffer. | |
b168057a | 3 | Copyright (C) 1997-2015 Free Software Foundation, Inc. |
6973fc01 UD |
4 | This file is part of the GNU C Library. |
5 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
6 | ||
7 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
8 | modify it under the terms of the GNU Lesser General Public |
9 | License as published by the Free Software Foundation; either | |
10 | version 2.1 of the License, or (at your option) any later version. | |
6973fc01 UD |
11 | |
12 | The GNU C Library is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 15 | Lesser General Public License for more details. |
6973fc01 | 16 | |
41bdb6e2 | 17 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
18 | License along with the GNU C Library; if not, see |
19 | <http://www.gnu.org/licenses/>. */ | |
6973fc01 | 20 | |
8619129f | 21 | #include <assert.h> |
b3fc5f84 | 22 | #include <dlfcn.h> |
b2b28911 | 23 | #include <stddef.h> |
915a6c51 UD |
24 | #include <sys/param.h> |
25 | ||
26 | #include <gconv_int.h> | |
27 | #include <sysdep.h> | |
28 | ||
6973fc01 UD |
29 | |
30 | int | |
0d9f6793 | 31 | internal_function |
d64b6ad0 UD |
32 | __gconv (__gconv_t cd, const unsigned char **inbuf, |
33 | const unsigned char *inbufend, unsigned char **outbuf, | |
38677ace | 34 | unsigned char *outbufend, size_t *irreversible) |
6973fc01 | 35 | { |
eacde9d0 | 36 | size_t last_step; |
6973fc01 UD |
37 | int result; |
38 | ||
d64b6ad0 UD |
39 | if (cd == (__gconv_t) -1L) |
40 | return __GCONV_ILLEGAL_DESCRIPTOR; | |
e34b0f29 | 41 | |
eacde9d0 UD |
42 | last_step = cd->__nsteps - 1; |
43 | ||
38677ace UD |
44 | assert (irreversible != NULL); |
45 | *irreversible = 0; | |
6973fc01 | 46 | |
c63598bf | 47 | cd->__data[last_step].__outbuf = outbuf != NULL ? *outbuf : NULL; |
4a422921 UD |
48 | cd->__data[last_step].__outbufend = outbufend; |
49 | ||
915a6c51 UD |
50 | __gconv_fct fct = cd->__steps->__fct; |
51 | #ifdef PTR_DEMANGLE | |
52 | if (cd->__steps->__shlib_handle != NULL) | |
53 | PTR_DEMANGLE (fct); | |
54 | #endif | |
55 | ||
8619129f | 56 | if (inbuf == NULL || *inbuf == NULL) |
55ea8790 UD |
57 | { |
58 | /* We just flush. */ | |
59 | result = DL_CALL_FCT (fct, | |
60 | (cd->__steps, cd->__data, NULL, NULL, NULL, | |
61 | irreversible, | |
62 | cd->__data[last_step].__outbuf == NULL ? 2 : 1, | |
63 | 0)); | |
64 | ||
65 | /* If the flush was successful clear the rest of the state. */ | |
66 | if (result == __GCONV_OK) | |
67 | for (size_t cnt = 0; cnt <= last_step; ++cnt) | |
68 | cd->__data[cnt].__invocation_counter = 0; | |
69 | } | |
8619129f UD |
70 | else |
71 | { | |
e573146a | 72 | const unsigned char *last_start; |
6973fc01 | 73 | |
8619129f | 74 | assert (outbuf != NULL && *outbuf != NULL); |
6973fc01 | 75 | |
8619129f UD |
76 | do |
77 | { | |
8619129f | 78 | last_start = *inbuf; |
915a6c51 | 79 | result = DL_CALL_FCT (fct, |
55985355 | 80 | (cd->__steps, cd->__data, inbuf, inbufend, |
f1d5c60d | 81 | NULL, irreversible, 0, 0)); |
8619129f | 82 | } |
d64b6ad0 UD |
83 | while (result == __GCONV_EMPTY_INPUT && last_start != *inbuf |
84 | && *inbuf + cd->__steps->__min_needed_from <= inbufend); | |
4bca4c17 | 85 | } |
6973fc01 | 86 | |
8619129f | 87 | if (outbuf != NULL && *outbuf != NULL) |
d64b6ad0 | 88 | *outbuf = cd->__data[last_step].__outbuf; |
8619129f | 89 | |
6973fc01 UD |
90 | return result; |
91 | } |