From e1f9e3c41c315b01b977a7caaa47afd0e4994d61 Mon Sep 17 00:00:00 2001 From: Jon Bergli Heier Date: Tue, 17 Aug 2010 18:19:05 +0200 Subject: Added encoder and decoder stream converters. --- decode_converter.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ decode_converter.h | 31 +++++++++++++++++++ encode_converter.c | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ encode_converter.h | 31 +++++++++++++++++++ 4 files changed, 236 insertions(+) create mode 100644 decode_converter.c create mode 100644 decode_converter.h create mode 100644 encode_converter.c create mode 100644 encode_converter.h diff --git a/decode_converter.c b/decode_converter.c new file mode 100644 index 0000000..102d059 --- /dev/null +++ b/decode_converter.c @@ -0,0 +1,87 @@ +#include "decode_converter.h" + +#include + +static void decode_converter_interface_init(GConverterIface *iface); +static void decode_converter_finalize(GObject *object); + +static GConverterResult decoder_converter_convert(GConverter *converter, + const void *inbuf, gsize inbuf_size, void *outbuf, gsize outbuf_size, + GConverterFlags flags, gsize *bytes_read, gsize *bytes_written, + GError **error); +static void decode_converter_reset(GConverter *converter); + +static GObjectClass *parent_class = NULL; + +G_DEFINE_TYPE_WITH_CODE(DecodeConverter, decode_converter, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE(G_TYPE_CONVERTER, + decode_converter_interface_init)); + +static void decode_converter_class_init(DecodeConverterClass *klass) { + GObjectClass *object_class; + + parent_class = (GObjectClass*)g_type_class_peek_parent(klass); + object_class = (GObjectClass*)klass; + object_class->finalize = decode_converter_finalize; +} + +static void decode_converter_interface_init(GConverterIface *iface) { + iface->convert = decoder_converter_convert; + iface->reset = decode_converter_reset; +} + +static void decode_converter_init(DecodeConverter *self) { +} + +static void decode_converter_finalize(GObject *object) { + DecodeConverter *dc; + + g_assert(CONVERTER_IS_DECODE(object)); + + dc = DECODE_CONVERTER(object); + + dc->decoder.decoder->close(dc->decoder.data); +} + +static GConverterResult decoder_converter_convert(GConverter *converter, + const void *inbuf, gsize inbuf_size, void *outbuf, gsize outbuf_size, + GConverterFlags flags, gsize *bytes_read, gsize *bytes_written, + GError **error) { + DecodeConverter *dc; + + dc = (DecodeConverter*)converter; + + gssize size = dc->decoder.decoder->decode(dc->decoder.data, inbuf, inbuf_size, outbuf, outbuf_size); + + if(size < 0) { + *error = g_error_new(g_quark_from_string("DecodeConverter"), + size, "decode() returned %ld", size); + return G_CONVERTER_ERROR; + } else if(size == 0) { + return G_CONVERTER_FINISHED; + } + *bytes_read = inbuf_size; + *bytes_written = size; + return G_CONVERTER_CONVERTED; +} + +static void decode_converter_reset(GConverter *converter) { + DecodeConverter *dc; + + dc = (DecodeConverter*)converter; + + dc->decoder.decoder->close(&dc->decoder.data); +} + +DecodeConverter *decode_converter_new(const struct decoder_plugin *decoder_plugin) { + DecodeConverter *dc; + + dc = (DecodeConverter*)g_object_new(CONVERTER_TYPE_DECODE, NULL); + + g_assert(dc != NULL); + + dc->decoder.decoder = decoder_plugin; + dc->decoder.decoder->init(&dc->decoder.data); + + return dc; +} diff --git a/decode_converter.h b/decode_converter.h new file mode 100644 index 0000000..47106c9 --- /dev/null +++ b/decode_converter.h @@ -0,0 +1,31 @@ +#ifndef _DECODE_CONVERTER_H_ +#define _DECODE_CONVERTER_H_ + +#include "decoder.h" + +#include + +#define CONVERTER_TYPE_DECODE (decode_converter_get_type()) +#define DECODE_CONVERTER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), CONVERTER_TYPE_DECODE, DecodeConverter)) +#define CONVERTER_IS_DECODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), CONVERTER_TYPE_DECODE)) +#define DECODE_CONVERTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), CONVERTER_TYPE_DECODE, DecodeConverterClass)) +#define CONVERTER_IS_DECODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), CONVERTER_TYPE_DECODE)) +#define DECODE_CONVERTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), CONVERTER_TYPE_DECODE, DecodeConverterClass)) + +typedef struct _DecodeConverter DecodeConverter; +typedef struct _DecodeConverterClass DecodeConverterClass; + +struct _DecodeConverter { + GObject parent_instance; + + struct decoder decoder; +}; + +struct _DecodeConverterClass { + GObjectClass parent_class; +}; + +GType decode_converter_get_type(); +DecodeConverter *decode_converter_new(const struct decoder_plugin *decoder_plugin); + +#endif diff --git a/encode_converter.c b/encode_converter.c new file mode 100644 index 0000000..e113351 --- /dev/null +++ b/encode_converter.c @@ -0,0 +1,87 @@ +#include "encode_converter.h" + +#include + +static void encode_converter_interface_init(GConverterIface *iface); +static void encode_converter_finalize(GObject *object); + +static GConverterResult encoder_converter_convert(GConverter *converter, + const void *inbuf, gsize inbuf_size, void *outbuf, gsize outbuf_size, + GConverterFlags flags, gsize *bytes_read, gsize *bytes_written, + GError **error); +static void encode_converter_reset(GConverter *converter); + +static GObjectClass *parent_class = NULL; + +G_DEFINE_TYPE_WITH_CODE(EncodeConverter, encode_converter, G_TYPE_OBJECT, + G_IMPLEMENT_INTERFACE(G_TYPE_CONVERTER, + encode_converter_interface_init)); + +static void encode_converter_class_init(EncodeConverterClass *klass) { + GObjectClass *object_class; + + parent_class = (GObjectClass*)g_type_class_peek_parent(klass); + object_class = (GObjectClass*)klass; + object_class->finalize = encode_converter_finalize; +} + +static void encode_converter_interface_init(GConverterIface *iface) { + iface->convert = encoder_converter_convert; + iface->reset = encode_converter_reset; +} + +static void encode_converter_init(EncodeConverter *self) { +} + +static void encode_converter_finalize(GObject *object) { + EncodeConverter *dc; + + g_assert(CONVERTER_IS_ENCODE(object)); + + dc = ENCODE_CONVERTER(object); + + dc->encoder.encoder->close(dc->encoder.data); +} + +static GConverterResult encoder_converter_convert(GConverter *converter, + const void *inbuf, gsize inbuf_size, void *outbuf, gsize outbuf_size, + GConverterFlags flags, gsize *bytes_read, gsize *bytes_written, + GError **error) { + EncodeConverter *dc; + + dc = (EncodeConverter*)converter; + + gssize size = dc->encoder.encoder->encode(dc->encoder.data, inbuf, inbuf_size, outbuf, outbuf_size); + + if(size < 0) { + *error = g_error_new(g_quark_from_string("EncodeConverter"), + size, "encode() returned %ld", size); + return G_CONVERTER_ERROR; + } else if(size == 0) { + return G_CONVERTER_FINISHED; + } + *bytes_read = inbuf_size; + *bytes_written = size; + return G_CONVERTER_CONVERTED; +} + +static void encode_converter_reset(GConverter *converter) { + EncodeConverter *ec; + + ec = (EncodeConverter*)converter; + + ec->encoder.encoder->close(&ec->encoder.data); +} + +EncodeConverter *encode_converter_new(const struct encoder_plugin *encoder_plugin) { + EncodeConverter *dc; + + dc = (EncodeConverter*)g_object_new(CONVERTER_TYPE_ENCODE, NULL); + + g_assert(dc != NULL); + + dc->encoder.encoder = encoder_plugin; + dc->encoder.encoder->init(&dc->encoder.data); + + return dc; +} diff --git a/encode_converter.h b/encode_converter.h new file mode 100644 index 0000000..739f593 --- /dev/null +++ b/encode_converter.h @@ -0,0 +1,31 @@ +#ifndef _ENCODE_CONVERTER_H_ +#define _ENCODE_CONVERTER_H_ + +#include "encoder.h" + +#include + +#define CONVERTER_TYPE_ENCODE (encode_converter_get_type()) +#define ENCODE_CONVERTER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), CONVERTER_TYPE_ENCODE, EncodeConverter)) +#define CONVERTER_IS_ENCODE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), CONVERTER_TYPE_ENCODE)) +#define ENCODE_CONVERTER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), CONVERTER_TYPE_ENCODE, EncodeConverterClass)) +#define CONVERTER_IS_ENCODE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), CONVERTER_TYPE_ENCODE)) +#define ENCODE_CONVERTER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), CONVERTER_TYPE_ENCODE, EncodeConverterClass)) + +typedef struct _EncodeConverter EncodeConverter; +typedef struct _EncodeConverterClass EncodeConverterClass; + +struct _EncodeConverter { + GObject parent_instance; + + struct encoder encoder; +}; + +struct _EncodeConverterClass { + GObjectClass parent_class; +}; + +GType encode_converter_get_type(); +EncodeConverter *encode_converter_new(const struct encoder_plugin *encoder_plugin); + +#endif -- cgit v1.2.3