cleanup
[platform/upstream/glib.git] / gio / gconverter.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2009 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: Alexander Larsson <alexl@redhat.com>
19  */
20
21 #include "config.h"
22 #include "gconverter.h"
23 #include "glibintl.h"
24
25
26 /**
27  * SECTION:gconverter
28  * @short_description: Data conversion interface
29  * @include: gio/gio.h
30  * @see_also: #GInputStream, #GOutputStream
31  *
32  * #GConverter is implemented by objects that convert
33  * binary data in various ways. The conversion can be
34  * stateful and may fail at any place.
35  *
36  * Some example conversions are: character set conversion,
37  * compression, decompression and regular expression
38  * replace.
39  *
40  * Since: 2.24
41  **/
42
43
44 typedef GConverterIface GConverterInterface;
45 G_DEFINE_INTERFACE (GConverter, g_converter, G_TYPE_OBJECT)
46
47 static void
48 g_converter_default_init (GConverterInterface *iface)
49 {
50 }
51
52 /**
53  * g_converter_convert:
54  * @converter: a #GConverter.
55  * @inbuf: (array length=inbuf_size) (element-type guint8): the buffer
56  *         containing the data to convert.
57  * @inbuf_size: the number of bytes in @inbuf
58  * @outbuf: a buffer to write converted data in.
59  * @outbuf_size: the number of bytes in @outbuf, must be at least one
60  * @flags: a #GConverterFlags controlling the conversion details
61  * @bytes_read: (out): will be set to the number of bytes read from @inbuf on success
62  * @bytes_written: (out): will be set to the number of bytes written to @outbuf on success
63  * @error: location to store the error occurring, or %NULL to ignore
64  *
65  * This is the main operation used when converting data. It is to be called
66  * multiple times in a loop, and each time it will do some work, i.e.
67  * producing some output (in @outbuf) or consuming some input (from @inbuf) or
68  * both. If its not possible to do any work an error is returned.
69  *
70  * Note that a single call may not consume all input (or any input at all).
71  * Also a call may produce output even if given no input, due to state stored
72  * in the converter producing output.
73  *
74  * If any data was either produced or consumed, and then an error happens, then
75  * only the successful conversion is reported and the error is returned on the
76  * next call.
77  *
78  * A full conversion loop involves calling this method repeatedly, each time
79  * giving it new input and space output space. When there is no more input
80  * data after the data in @inbuf, the flag %G_CONVERTER_INPUT_AT_END must be set.
81  * The loop will be (unless some error happens) returning %G_CONVERTER_CONVERTED
82  * each time until all data is consumed and all output is produced, then
83  * %G_CONVERTER_FINISHED is returned instead. Note, that %G_CONVERTER_FINISHED
84  * may be returned even if %G_CONVERTER_INPUT_AT_END is not set, for instance
85  * in a decompression converter where the end of data is detectable from the
86  * data (and there might even be other data after the end of the compressed data).
87  *
88  * When some data has successfully been converted @bytes_read and is set to
89  * the number of bytes read from @inbuf, and @bytes_written is set to indicate
90  * how many bytes was written to @outbuf. If there are more data to output
91  * or consume (i.e. unless the %G_CONVERTER_INPUT_AT_END is specified) then
92  * %G_CONVERTER_CONVERTED is returned, and if no more data is to be output
93  * then %G_CONVERTER_FINISHED is returned.
94  *
95  * On error %G_CONVERTER_ERROR is returned and @error is set accordingly.
96  * Some errors need special handling:
97  *
98  * %G_IO_ERROR_NO_SPACE is returned if there is not enough space
99  * to write the resulting converted data, the application should
100  * call the function again with a larger @outbuf to continue.
101  *
102  * %G_IO_ERROR_PARTIAL_INPUT is returned if there is not enough
103  * input to fully determine what the conversion should produce,
104  * and the %G_CONVERTER_INPUT_AT_END flag is not set. This happens for
105  * example with an incomplete multibyte sequence when converting text,
106  * or when a regexp matches up to the end of the input (and may match
107  * further input). It may also happen when @inbuf_size is zero and
108  * there is no more data to produce.
109  *
110  * When this happens the application should read more input and then
111  * call the function again. If further input shows that there is no
112  * more data call the function again with the same data but with
113  * the %G_CONVERTER_INPUT_AT_END flag set. This may cause the conversion
114  * to finish as e.g. in the regexp match case (or, to fail again with
115  * %G_IO_ERROR_PARTIAL_INPUT in e.g. a charset conversion where the
116  * input is actually partial).
117  *
118  * After g_converter_convert() has returned %G_CONVERTER_FINISHED the
119  * converter object is in an invalid state where its not allowed
120  * to call g_converter_convert() anymore. At this time you can only
121  * free the object or call g_converter_reset() to reset it to the
122  * initial state.
123  *
124  * If the flag %G_CONVERTER_FLUSH is set then conversion is modified
125  * to try to write out all internal state to the output. The application
126  * has to call the function multiple times with the flag set, and when
127  * the available input has been consumed and all internal state has
128  * been produced then %G_CONVERTER_FLUSHED (or %G_CONVERTER_FINISHED if
129  * really at the end) is returned instead of %G_CONVERTER_CONVERTED.
130  * This is somewhat similar to what happens at the end of the input stream,
131  * but done in the middle of the data.
132  *
133  * This has different meanings for different conversions. For instance
134  * in a compression converter it would mean that we flush all the
135  * compression state into output such that if you uncompress the
136  * compressed data you get back all the input data. Doing this may
137  * make the final file larger due to padding though. Another example
138  * is a regexp conversion, where if you at the end of the flushed data
139  * have a match, but there is also a potential longer match. In the
140  * non-flushed case we would ask for more input, but when flushing we
141  * treat this as the end of input and do the match.
142  *
143  * Flushing is not always possible (like if a charset converter flushes
144  * at a partial multibyte sequence). Converters are supposed to try
145  * to produce as much output as possible and then return an error
146  * (typically %G_IO_ERROR_PARTIAL_INPUT).
147  *
148  * Returns: a #GConverterResult, %G_CONVERTER_ERROR on error.
149  *
150  * Since: 2.24
151  **/
152 GConverterResult
153 g_converter_convert (GConverter *converter,
154                      const void *inbuf,
155                      gsize       inbuf_size,
156                      void       *outbuf,
157                      gsize       outbuf_size,
158                      GConverterFlags flags,
159                      gsize      *bytes_read,
160                      gsize      *bytes_written,
161                      GError    **error)
162 {
163   GConverterIface *iface;
164
165   g_return_val_if_fail (G_IS_CONVERTER (converter), G_CONVERTER_ERROR);
166   g_return_val_if_fail (outbuf_size > 0, G_CONVERTER_ERROR);
167
168   *bytes_read = 0;
169   *bytes_written = 0;
170
171   iface = G_CONVERTER_GET_IFACE (converter);
172
173   return (* iface->convert) (converter,
174                              inbuf, inbuf_size,
175                              outbuf, outbuf_size,
176                              flags,
177                              bytes_read, bytes_written, error);
178 }
179
180 /**
181  * g_converter_reset:
182  * @converter: a #GConverter.
183  *
184  * Resets all internal state in the converter, making it behave
185  * as if it was just created. If the converter has any internal
186  * state that would produce output then that output is lost.
187  *
188  * Since: 2.24
189  **/
190 void
191 g_converter_reset (GConverter *converter)
192 {
193   GConverterIface *iface;
194
195   g_return_if_fail (G_IS_CONVERTER (converter));
196
197   iface = G_CONVERTER_GET_IFACE (converter);
198
199   (* iface->reset) (converter);
200 }