mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-24 14:36:13 +01:00
Add GBase64Encoder and Decoder
These are GConverter implementations that convert to/from base64 encoding. Originally written by Christian Persch. Fixes: #305
This commit is contained in:
parent
928b66898c
commit
b2c11516e2
135
gio/gbase64decoder.c
Normal file
135
gio/gbase64decoder.c
Normal file
@ -0,0 +1,135 @@
|
||||
/* GIO - GLib Input, Output and Streaming Library
|
||||
*
|
||||
* Copyright © 2009 Red Hat, Inc.
|
||||
* Copyright © 2010 Christian Persch
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General
|
||||
* Public License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Alexander Larsson <alexl@redhat.com>
|
||||
* Christian Persch <chpe@gnome.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gbase64decoder.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "gioerror.h"
|
||||
#include "glibintl.h"
|
||||
#include "gioenums.h"
|
||||
#include "gioenumtypes.h"
|
||||
|
||||
/**
|
||||
* GBase64Decoder:
|
||||
*
|
||||
* `GBase64Decoder` is an implementation of `GConverter` that
|
||||
* converts data from base64 encoding.
|
||||
*/
|
||||
|
||||
struct _GBase64Decoder
|
||||
{
|
||||
GObject parent_instance;
|
||||
|
||||
int state;
|
||||
guint save;
|
||||
};
|
||||
|
||||
struct _GBase64DecoderClass
|
||||
{
|
||||
GObjectClass parent_class;
|
||||
};
|
||||
|
||||
static void
|
||||
g_base64_decoder_reset (GConverter *converter)
|
||||
{
|
||||
GBase64Decoder *decoder = G_BASE64_DECODER (converter);
|
||||
|
||||
decoder->state = 0;
|
||||
decoder->save = 0;
|
||||
}
|
||||
|
||||
static GConverterResult
|
||||
g_base64_decoder_convert (GConverter *converter,
|
||||
const void *inbuf,
|
||||
gsize inbuf_size,
|
||||
void *outbuf,
|
||||
gsize outbuf_size,
|
||||
GConverterFlags flags,
|
||||
gsize *bytes_read,
|
||||
gsize *bytes_written,
|
||||
GError **error)
|
||||
{
|
||||
GBase64Decoder *decoder = G_BASE64_DECODER (converter);
|
||||
|
||||
if (outbuf_size < ((inbuf_size / 4) * 3 + 3))
|
||||
{
|
||||
g_set_error (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
|
||||
"Not enough space in dest");
|
||||
return G_CONVERTER_ERROR;
|
||||
}
|
||||
|
||||
*bytes_read = inbuf_size;
|
||||
*bytes_written = g_base64_decode_step (inbuf, inbuf_size, outbuf,
|
||||
&decoder->state, &decoder->save);
|
||||
|
||||
if (flags & G_CONVERTER_FLUSH)
|
||||
return G_CONVERTER_FLUSHED;
|
||||
if (*bytes_read == inbuf_size && (flags & G_CONVERTER_INPUT_AT_END))
|
||||
return G_CONVERTER_FINISHED;
|
||||
|
||||
return G_CONVERTER_CONVERTED;
|
||||
}
|
||||
|
||||
static void
|
||||
g_base64_decoder_iface_init (GConverterIface *iface)
|
||||
{
|
||||
iface->convert = g_base64_decoder_convert;
|
||||
iface->reset = g_base64_decoder_reset;
|
||||
}
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE (GBase64Decoder, g_base64_decoder, G_TYPE_OBJECT,
|
||||
G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
|
||||
g_base64_decoder_iface_init))
|
||||
|
||||
static void
|
||||
g_base64_decoder_init (GBase64Decoder *decoder)
|
||||
{
|
||||
decoder->state = 0;
|
||||
decoder->save = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
g_base64_decoder_class_init (GBase64DecoderClass *klass)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* g_base64_decoder_new:
|
||||
*
|
||||
* Creates a new `GBase64Decoder`.
|
||||
*
|
||||
* Returns: a new `GBase64Decoder`
|
||||
*
|
||||
* Since: 2.82
|
||||
*/
|
||||
GConverter *
|
||||
g_base64_decoder_new (void)
|
||||
{
|
||||
return g_object_new (G_TYPE_BASE64_DECODER, NULL);
|
||||
}
|
42
gio/gbase64decoder.h
Normal file
42
gio/gbase64decoder.h
Normal file
@ -0,0 +1,42 @@
|
||||
/* GIO - GLib Input, Output and Streaming Library
|
||||
*
|
||||
* Copyright (C) 2009 Red Hat, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General
|
||||
* Public License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Alexander Larsson <alexl@redhat.com>
|
||||
* Christian Persch <chpe@gnome.org>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
|
||||
#error "Only <gio/gio.h> can be included directly."
|
||||
#endif
|
||||
|
||||
#include <gio/gconverter.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define G_TYPE_BASE64_DECODER (g_base64_decoder_get_type ())
|
||||
|
||||
GIO_AVAILABLE_IN_2_82
|
||||
G_DECLARE_FINAL_TYPE (GBase64Decoder, g_base64_decoder, G, BASE64_DECODER, GObject)
|
||||
|
||||
GIO_AVAILABLE_IN_2_82
|
||||
GConverter *g_base64_decoder_new (void);
|
||||
|
||||
G_END_DECLS
|
232
gio/gbase64encoder.c
Normal file
232
gio/gbase64encoder.c
Normal file
@ -0,0 +1,232 @@
|
||||
/* GIO - GLib Input, Output and Streaming Library
|
||||
*
|
||||
* Copyright © 2009 Red Hat, Inc.
|
||||
* Copyright © 2010 Christian Persch
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General
|
||||
* Public License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Alexander Larsson <alexl@redhat.com>
|
||||
* Christian Persch <chpe@gnome.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "gbase64encoder.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "glib.h"
|
||||
#include "gioerror.h"
|
||||
#include "glibintl.h"
|
||||
#include "gioenums.h"
|
||||
#include "gioenumtypes.h"
|
||||
|
||||
|
||||
#define BASE64_ENCODING_OUTPUT_SIZE(len, break_lines) \
|
||||
(((len) / 3 + 1) * 4 + 4 + ((break_lines) ? ((((len) / 3 + 1) * 4 + 4) / 72 + 1) : 0))
|
||||
|
||||
/**
|
||||
* GBase64Encoder:
|
||||
*
|
||||
* GBase64Encoder is an implementation of `GConverter` that
|
||||
* converts data to base64 encoding.
|
||||
*/
|
||||
|
||||
struct _GBase64Encoder
|
||||
{
|
||||
GObject parent_instance;
|
||||
|
||||
gboolean break_lines;
|
||||
int state[2];
|
||||
};
|
||||
|
||||
enum {
|
||||
PROP_0,
|
||||
PROP_BREAK_LINES
|
||||
};
|
||||
|
||||
static void
|
||||
g_base64_encoder_reset (GConverter *converter)
|
||||
{
|
||||
GBase64Encoder *encoder = G_BASE64_ENCODER (converter);
|
||||
|
||||
encoder->state[0] = encoder->state[1] = 0;
|
||||
}
|
||||
|
||||
static GConverterResult
|
||||
g_base64_encoder_convert (GConverter *converter,
|
||||
const void *inbuf,
|
||||
gsize inbuf_size,
|
||||
void *outbuf,
|
||||
gsize outbuf_size,
|
||||
GConverterFlags flags,
|
||||
gsize *bytes_read,
|
||||
gsize *bytes_written,
|
||||
GError **error)
|
||||
{
|
||||
GBase64Encoder *encoder = G_BASE64_ENCODER (converter);
|
||||
|
||||
if (inbuf_size == 0 || (flags & G_CONVERTER_FLUSH))
|
||||
{
|
||||
if (outbuf_size < (encoder->break_lines ? 5 : 4))
|
||||
{
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
|
||||
"Need more output space");
|
||||
return G_CONVERTER_ERROR;
|
||||
}
|
||||
|
||||
*bytes_read = 0;
|
||||
*bytes_written = g_base64_encode_close (encoder->break_lines, outbuf,
|
||||
&encoder->state[0],
|
||||
&encoder->state[1]);
|
||||
|
||||
if (flags & G_CONVERTER_FLUSH)
|
||||
return G_CONVERTER_FLUSHED;
|
||||
if (flags & G_CONVERTER_INPUT_AT_END)
|
||||
return G_CONVERTER_FINISHED;
|
||||
|
||||
return G_CONVERTER_CONVERTED;
|
||||
}
|
||||
|
||||
if (outbuf_size < BASE64_ENCODING_OUTPUT_SIZE (inbuf_size, encoder->break_lines))
|
||||
{
|
||||
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_NO_SPACE,
|
||||
"Need more output space");
|
||||
return G_CONVERTER_ERROR;
|
||||
}
|
||||
|
||||
*bytes_read = inbuf_size;
|
||||
*bytes_written = g_base64_encode_step (inbuf, inbuf_size,
|
||||
encoder->break_lines,
|
||||
outbuf,
|
||||
&encoder->state[0],
|
||||
&encoder->state[1]);
|
||||
if (flags & G_CONVERTER_INPUT_AT_END)
|
||||
return G_CONVERTER_FINISHED;
|
||||
|
||||
return G_CONVERTER_CONVERTED;
|
||||
}
|
||||
|
||||
static void
|
||||
g_base64_encoder_iface_init (GConverterIface *iface)
|
||||
{
|
||||
iface->convert = g_base64_encoder_convert;
|
||||
iface->reset = g_base64_encoder_reset;
|
||||
}
|
||||
|
||||
G_DEFINE_TYPE_WITH_CODE (GBase64Encoder, g_base64_encoder, G_TYPE_OBJECT,
|
||||
G_IMPLEMENT_INTERFACE (G_TYPE_CONVERTER,
|
||||
g_base64_encoder_iface_init))
|
||||
|
||||
static void
|
||||
g_base64_encoder_init (GBase64Encoder *encoder)
|
||||
{
|
||||
encoder->break_lines = FALSE;
|
||||
encoder->state[0] = encoder->state[1] = 0;
|
||||
}
|
||||
|
||||
static void
|
||||
g_base64_encoder_set_property (GObject *object,
|
||||
guint prop_id,
|
||||
const GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
GBase64Encoder *encoder = G_BASE64_ENCODER (object);
|
||||
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_BREAK_LINES:
|
||||
encoder->break_lines = g_value_get_boolean (value);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
g_base64_encoder_get_property (GObject *object,
|
||||
guint prop_id,
|
||||
GValue *value,
|
||||
GParamSpec *pspec)
|
||||
{
|
||||
GBase64Encoder *encoder = G_BASE64_ENCODER (object);
|
||||
|
||||
switch (prop_id)
|
||||
{
|
||||
case PROP_BREAK_LINES:
|
||||
g_value_set_boolean (value, encoder->break_lines);
|
||||
break;
|
||||
|
||||
default:
|
||||
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
g_base64_encoder_class_init (GBase64EncoderClass *klass)
|
||||
{
|
||||
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
gobject_class->get_property = g_base64_encoder_get_property;
|
||||
gobject_class->set_property = g_base64_encoder_set_property;
|
||||
|
||||
/**
|
||||
* GBase64Encoder:break-lines:
|
||||
*
|
||||
* Whether to break lines.
|
||||
*
|
||||
* This is typically used when putting base64-encoded data in emails.
|
||||
* It breaks the lines at 72 columns instead of putting all of the text on
|
||||
* the same line. This avoids problems with long lines in the email system.
|
||||
*/
|
||||
g_object_class_install_property (gobject_class,
|
||||
PROP_BREAK_LINES,
|
||||
g_param_spec_boolean ("break-lines",
|
||||
P_("Break lines"),
|
||||
P_("Break lines"),
|
||||
FALSE,
|
||||
G_PARAM_READWRITE |
|
||||
G_PARAM_CONSTRUCT_ONLY |
|
||||
G_PARAM_STATIC_STRINGS));
|
||||
}
|
||||
|
||||
/**
|
||||
* g_base64_encoder_new:
|
||||
* @break_lines: whether to break long lines
|
||||
*
|
||||
* Creates a new `GBase64Encoder`.
|
||||
*
|
||||
* Setting @break_lines to `TRUE` is typically used when putting
|
||||
* base64-encoded data in emails. It breaks the lines at 72 columns
|
||||
* instead of putting all of the text on the same line. This avoids
|
||||
* problems with long lines in the email system.
|
||||
*
|
||||
* Returns: a new `GBase64Encoder`
|
||||
*
|
||||
* Since: 2.82
|
||||
*/
|
||||
GConverter *
|
||||
g_base64_encoder_new (gboolean break_lines)
|
||||
{
|
||||
return g_object_new (G_TYPE_BASE64_ENCODER,
|
||||
"break-lines", break_lines,
|
||||
NULL);
|
||||
}
|
42
gio/gbase64encoder.h
Normal file
42
gio/gbase64encoder.h
Normal file
@ -0,0 +1,42 @@
|
||||
/* GIO - GLib Input, Output and Streaming Library
|
||||
*
|
||||
* Copyright (C) 2009 Red Hat, Inc.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General
|
||||
* Public License along with this library; if not, write to the
|
||||
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
|
||||
* Boston, MA 02111-1307, USA.
|
||||
*
|
||||
* Author: Alexander Larsson <alexl@redhat.com>
|
||||
* Christian Persch <chpe@gnome.org>
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#if !defined (__GIO_GIO_H_INSIDE__) && !defined (GIO_COMPILATION)
|
||||
#error "Only <gio/gio.h> can be included directly."
|
||||
#endif
|
||||
|
||||
#include <gio/gconverter.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define G_TYPE_BASE64_ENCODER (g_base64_encoder_get_type ())
|
||||
|
||||
GIO_AVAILABLE_IN_2_82
|
||||
G_DECLARE_FINAL_TYPE (GBase64Encoder, g_base64_encoder, G, BASE64_ENCODER, GObject)
|
||||
|
||||
GIO_AVAILABLE_IN_2_82
|
||||
GConverter *g_base64_encoder_new (gboolean break_lines);
|
||||
|
||||
G_END_DECLS
|
@ -36,6 +36,8 @@
|
||||
#include <gio/gapplicationcommandline.h>
|
||||
#include <gio/gasyncinitable.h>
|
||||
#include <gio/gasyncresult.h>
|
||||
#include <gio/gbase64decoder.h>
|
||||
#include <gio/gbase64encoder.h>
|
||||
#include <gio/gbufferedinputstream.h>
|
||||
#include <gio/gbufferedoutputstream.h>
|
||||
#include <gio/gbytesicon.h>
|
||||
|
@ -35,6 +35,8 @@ typedef struct _GAppLaunchContext GAppLaunchContext;
|
||||
typedef struct _GAppInfo GAppInfo; /* Dummy typedef */
|
||||
typedef struct _GAsyncResult GAsyncResult; /* Dummy typedef */
|
||||
typedef struct _GAsyncInitable GAsyncInitable;
|
||||
typedef struct _GBase64Decoder GBase64Decoder;
|
||||
typedef struct _GBase64Encoder GBase64Encoder;
|
||||
typedef struct _GBufferedInputStream GBufferedInputStream;
|
||||
typedef struct _GBufferedOutputStream GBufferedOutputStream;
|
||||
typedef struct _GCancellable GCancellable;
|
||||
|
@ -480,6 +480,8 @@ gio_base_sources = files(
|
||||
'gasynchelper.c',
|
||||
'gasyncinitable.c',
|
||||
'gasyncresult.c',
|
||||
'gbase64decoder.c',
|
||||
'gbase64encoder.c',
|
||||
'gbufferedinputstream.c',
|
||||
'gbufferedoutputstream.c',
|
||||
'gbytesicon.c',
|
||||
|
@ -47,6 +47,9 @@ static gboolean decompress = FALSE;
|
||||
static gboolean compress = FALSE;
|
||||
static gboolean gzip = FALSE;
|
||||
static gboolean fallback = FALSE;
|
||||
static gboolean base64_decode = FALSE;
|
||||
static gboolean base64_encode = FALSE;
|
||||
static gboolean base64_break_lines = FALSE;
|
||||
|
||||
static GOptionEntry entries[] = {
|
||||
{"decompress", 0, 0, G_OPTION_ARG_NONE, &decompress, "decompress", NULL},
|
||||
@ -55,6 +58,9 @@ static GOptionEntry entries[] = {
|
||||
{"from-charset", 0, 0, G_OPTION_ARG_STRING, &from_charset, "from charset", NULL},
|
||||
{"to-charset", 0, 0, G_OPTION_ARG_STRING, &to_charset, "to charset", NULL},
|
||||
{"fallback", 0, 0, G_OPTION_ARG_NONE, &fallback, "use fallback", NULL},
|
||||
{"from-base64", 0, 0, G_OPTION_ARG_NONE, &base64_decode, "decode from Base-64", NULL},
|
||||
{"to-base64", 0, 0, G_OPTION_ARG_NONE, &base64_encode, "encode to Base-64", NULL},
|
||||
{"break-lines", 0, 0, G_OPTION_ARG_NONE, &base64_break_lines, "break lines in Base-64", NULL},
|
||||
{G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &locations, "locations", NULL},
|
||||
G_OPTION_ENTRY_NULL
|
||||
};
|
||||
@ -100,6 +106,16 @@ cat (GFile * file)
|
||||
return;
|
||||
}
|
||||
|
||||
if (base64_decode)
|
||||
{
|
||||
GInputStream *old;
|
||||
conv = (GConverter *)g_base64_decoder_new();
|
||||
old = in;
|
||||
in = (GInputStream *) g_converter_input_stream_new (in, conv);
|
||||
g_object_unref (conv);
|
||||
g_object_unref (old);
|
||||
}
|
||||
|
||||
if (decompress)
|
||||
{
|
||||
GInputStream *old;
|
||||
@ -161,6 +177,16 @@ cat (GFile * file)
|
||||
g_object_unref (in_file_info);
|
||||
}
|
||||
|
||||
if (base64_encode)
|
||||
{
|
||||
GInputStream *old;
|
||||
conv = (GConverter *)g_base64_encoder_new(base64_break_lines);
|
||||
old = in;
|
||||
in = (GInputStream *) g_converter_input_stream_new (in, conv);
|
||||
g_object_unref (conv);
|
||||
g_object_unref (old);
|
||||
}
|
||||
|
||||
while (1)
|
||||
{
|
||||
res =
|
||||
|
Loading…
Reference in New Issue
Block a user