Blame gio/gconverter.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2009 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
#include "gconverter.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * SECTION:gconverter
Packit ae235b
 * @short_description: Data conversion interface
Packit ae235b
 * @include: gio/gio.h
Packit ae235b
 * @see_also: #GInputStream, #GOutputStream
Packit ae235b
 *
Packit ae235b
 * #GConverter is implemented by objects that convert
Packit ae235b
 * binary data in various ways. The conversion can be
Packit ae235b
 * stateful and may fail at any place.
Packit ae235b
 *
Packit ae235b
 * Some example conversions are: character set conversion,
Packit ae235b
 * compression, decompression and regular expression
Packit ae235b
 * replace.
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
Packit ae235b
Packit ae235b
typedef GConverterIface GConverterInterface;
Packit ae235b
G_DEFINE_INTERFACE (GConverter, g_converter, G_TYPE_OBJECT)
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_converter_default_init (GConverterInterface *iface)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_converter_convert:
Packit ae235b
 * @converter: a #GConverter.
Packit ae235b
 * @inbuf: (array length=inbuf_size) (element-type guint8): the buffer
Packit ae235b
 *         containing the data to convert.
Packit ae235b
 * @inbuf_size: the number of bytes in @inbuf
Packit ae235b
 * @outbuf: (element-type guint8) (array length=outbuf_size): a buffer to write
Packit ae235b
 *    converted data in.
Packit ae235b
 * @outbuf_size: the number of bytes in @outbuf, must be at least one
Packit ae235b
 * @flags: a #GConverterFlags controlling the conversion details
Packit ae235b
 * @bytes_read: (out): will be set to the number of bytes read from @inbuf on success
Packit ae235b
 * @bytes_written: (out): will be set to the number of bytes written to @outbuf on success
Packit ae235b
 * @error: location to store the error occurring, or %NULL to ignore
Packit ae235b
 *
Packit ae235b
 * This is the main operation used when converting data. It is to be called
Packit ae235b
 * multiple times in a loop, and each time it will do some work, i.e.
Packit ae235b
 * producing some output (in @outbuf) or consuming some input (from @inbuf) or
Packit ae235b
 * both. If its not possible to do any work an error is returned.
Packit ae235b
 *
Packit ae235b
 * Note that a single call may not consume all input (or any input at all).
Packit ae235b
 * Also a call may produce output even if given no input, due to state stored
Packit ae235b
 * in the converter producing output.
Packit ae235b
 *
Packit ae235b
 * If any data was either produced or consumed, and then an error happens, then
Packit ae235b
 * only the successful conversion is reported and the error is returned on the
Packit ae235b
 * next call.
Packit ae235b
 *
Packit ae235b
 * A full conversion loop involves calling this method repeatedly, each time
Packit ae235b
 * giving it new input and space output space. When there is no more input
Packit ae235b
 * data after the data in @inbuf, the flag %G_CONVERTER_INPUT_AT_END must be set.
Packit ae235b
 * The loop will be (unless some error happens) returning %G_CONVERTER_CONVERTED
Packit ae235b
 * each time until all data is consumed and all output is produced, then
Packit ae235b
 * %G_CONVERTER_FINISHED is returned instead. Note, that %G_CONVERTER_FINISHED
Packit ae235b
 * may be returned even if %G_CONVERTER_INPUT_AT_END is not set, for instance
Packit ae235b
 * in a decompression converter where the end of data is detectable from the
Packit ae235b
 * data (and there might even be other data after the end of the compressed data).
Packit ae235b
 *
Packit ae235b
 * When some data has successfully been converted @bytes_read and is set to
Packit ae235b
 * the number of bytes read from @inbuf, and @bytes_written is set to indicate
Packit ae235b
 * how many bytes was written to @outbuf. If there are more data to output
Packit ae235b
 * or consume (i.e. unless the %G_CONVERTER_INPUT_AT_END is specified) then
Packit ae235b
 * %G_CONVERTER_CONVERTED is returned, and if no more data is to be output
Packit ae235b
 * then %G_CONVERTER_FINISHED is returned.
Packit ae235b
 *
Packit ae235b
 * On error %G_CONVERTER_ERROR is returned and @error is set accordingly.
Packit ae235b
 * Some errors need special handling:
Packit ae235b
 *
Packit ae235b
 * %G_IO_ERROR_NO_SPACE is returned if there is not enough space
Packit ae235b
 * to write the resulting converted data, the application should
Packit ae235b
 * call the function again with a larger @outbuf to continue.
Packit ae235b
 *
Packit ae235b
 * %G_IO_ERROR_PARTIAL_INPUT is returned if there is not enough
Packit ae235b
 * input to fully determine what the conversion should produce,
Packit ae235b
 * and the %G_CONVERTER_INPUT_AT_END flag is not set. This happens for
Packit ae235b
 * example with an incomplete multibyte sequence when converting text,
Packit ae235b
 * or when a regexp matches up to the end of the input (and may match
Packit ae235b
 * further input). It may also happen when @inbuf_size is zero and
Packit ae235b
 * there is no more data to produce.
Packit ae235b
 *
Packit ae235b
 * When this happens the application should read more input and then
Packit ae235b
 * call the function again. If further input shows that there is no
Packit ae235b
 * more data call the function again with the same data but with
Packit ae235b
 * the %G_CONVERTER_INPUT_AT_END flag set. This may cause the conversion
Packit ae235b
 * to finish as e.g. in the regexp match case (or, to fail again with
Packit ae235b
 * %G_IO_ERROR_PARTIAL_INPUT in e.g. a charset conversion where the
Packit ae235b
 * input is actually partial).
Packit ae235b
 *
Packit ae235b
 * After g_converter_convert() has returned %G_CONVERTER_FINISHED the
Packit ae235b
 * converter object is in an invalid state where its not allowed
Packit ae235b
 * to call g_converter_convert() anymore. At this time you can only
Packit ae235b
 * free the object or call g_converter_reset() to reset it to the
Packit ae235b
 * initial state.
Packit ae235b
 *
Packit ae235b
 * If the flag %G_CONVERTER_FLUSH is set then conversion is modified
Packit ae235b
 * to try to write out all internal state to the output. The application
Packit ae235b
 * has to call the function multiple times with the flag set, and when
Packit ae235b
 * the available input has been consumed and all internal state has
Packit ae235b
 * been produced then %G_CONVERTER_FLUSHED (or %G_CONVERTER_FINISHED if
Packit ae235b
 * really at the end) is returned instead of %G_CONVERTER_CONVERTED.
Packit ae235b
 * This is somewhat similar to what happens at the end of the input stream,
Packit ae235b
 * but done in the middle of the data.
Packit ae235b
 *
Packit ae235b
 * This has different meanings for different conversions. For instance
Packit ae235b
 * in a compression converter it would mean that we flush all the
Packit ae235b
 * compression state into output such that if you uncompress the
Packit ae235b
 * compressed data you get back all the input data. Doing this may
Packit ae235b
 * make the final file larger due to padding though. Another example
Packit ae235b
 * is a regexp conversion, where if you at the end of the flushed data
Packit ae235b
 * have a match, but there is also a potential longer match. In the
Packit ae235b
 * non-flushed case we would ask for more input, but when flushing we
Packit ae235b
 * treat this as the end of input and do the match.
Packit ae235b
 *
Packit ae235b
 * Flushing is not always possible (like if a charset converter flushes
Packit ae235b
 * at a partial multibyte sequence). Converters are supposed to try
Packit ae235b
 * to produce as much output as possible and then return an error
Packit ae235b
 * (typically %G_IO_ERROR_PARTIAL_INPUT).
Packit ae235b
 *
Packit ae235b
 * Returns: a #GConverterResult, %G_CONVERTER_ERROR on error.
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
GConverterResult
Packit ae235b
g_converter_convert (GConverter *converter,
Packit ae235b
		     const void *inbuf,
Packit ae235b
		     gsize       inbuf_size,
Packit ae235b
		     void       *outbuf,
Packit ae235b
		     gsize       outbuf_size,
Packit ae235b
		     GConverterFlags flags,
Packit ae235b
		     gsize      *bytes_read,
Packit ae235b
		     gsize      *bytes_written,
Packit ae235b
		     GError    **error)
Packit ae235b
{
Packit ae235b
  GConverterIface *iface;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (G_IS_CONVERTER (converter), G_CONVERTER_ERROR);
Packit ae235b
  g_return_val_if_fail (outbuf_size > 0, G_CONVERTER_ERROR);
Packit ae235b
Packit ae235b
  *bytes_read = 0;
Packit ae235b
  *bytes_written = 0;
Packit ae235b
Packit ae235b
  iface = G_CONVERTER_GET_IFACE (converter);
Packit ae235b
Packit ae235b
  return (* iface->convert) (converter,
Packit ae235b
			     inbuf, inbuf_size,
Packit ae235b
			     outbuf, outbuf_size,
Packit ae235b
			     flags,
Packit ae235b
			     bytes_read, bytes_written, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_converter_reset:
Packit ae235b
 * @converter: a #GConverter.
Packit ae235b
 *
Packit ae235b
 * Resets all internal state in the converter, making it behave
Packit ae235b
 * as if it was just created. If the converter has any internal
Packit ae235b
 * state that would produce output then that output is lost.
Packit ae235b
 *
Packit ae235b
 * Since: 2.24
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_converter_reset (GConverter *converter)
Packit ae235b
{
Packit ae235b
  GConverterIface *iface;
Packit ae235b
Packit ae235b
  g_return_if_fail (G_IS_CONVERTER (converter));
Packit ae235b
Packit ae235b
  iface = G_CONVERTER_GET_IFACE (converter);
Packit ae235b
Packit ae235b
  (* iface->reset) (converter);
Packit ae235b
}