diff options
author | Colin Walters <walters@verbum.org> | 2017-03-13 15:40:24 -0400 |
---|---|---|
committer | Atomic Bot <atomic-devel@projectatomic.io> | 2017-03-20 18:32:40 +0000 |
commit | c2f5a999bfd865696b5456858e3cae984fba46f5 (patch) | |
tree | 4f3ea276df014f62aa8dc00c1d459af5774fe924 /src/libotutil | |
parent | 0b214566a244df2da1fcd2db09b5560a9b3d7280 (diff) | |
download | ostree-c2f5a999bfd865696b5456858e3cae984fba46f5.tar.gz |
lib: Add a private copy of checksum-instream
The current `OstreeChecksumInputStream` is public due to a historical
mistake. I'd like to add an OpenSSL checksum backend, but that's
harder without breaking this API.
Let's ignore it and create a new private version, so it's easier to do the
GLib/OpenSSL abstraction in one place.
Closes: #738
Approved by: jlebon
Diffstat (limited to 'src/libotutil')
-rw-r--r-- | src/libotutil/ot-checksum-instream.c | 129 | ||||
-rw-r--r-- | src/libotutil/ot-checksum-instream.h | 64 | ||||
-rw-r--r-- | src/libotutil/otutil.h | 1 |
3 files changed, 194 insertions, 0 deletions
diff --git a/src/libotutil/ot-checksum-instream.c b/src/libotutil/ot-checksum-instream.c new file mode 100644 index 00000000..14d5b5f0 --- /dev/null +++ b/src/libotutil/ot-checksum-instream.c @@ -0,0 +1,129 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2017 Colin Walters <walters@verbum.org> + * + * 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 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. + */ + +#include "config.h" + +#include "ot-checksum-instream.h" + +G_DEFINE_TYPE (OtChecksumInstream, ot_checksum_instream, G_TYPE_FILTER_INPUT_STREAM) + +struct _OtChecksumInstreamPrivate { + GChecksum *checksum; +}; + +static gssize ot_checksum_instream_read (GInputStream *stream, + void *buffer, + gsize count, + GCancellable *cancellable, + GError **error); + +static void +ot_checksum_instream_finalize (GObject *object) +{ + OtChecksumInstream *self = (OtChecksumInstream*)object; + + g_checksum_free (self->priv->checksum); + + G_OBJECT_CLASS (ot_checksum_instream_parent_class)->finalize (object); +} + +static void +ot_checksum_instream_class_init (OtChecksumInstreamClass *klass) +{ + GObjectClass *object_class = G_OBJECT_CLASS (klass); + GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass); + + g_type_class_add_private (klass, sizeof (OtChecksumInstreamPrivate)); + + object_class->finalize = ot_checksum_instream_finalize; + stream_class->read_fn = ot_checksum_instream_read; +} + +static void +ot_checksum_instream_init (OtChecksumInstream *self) +{ + self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, OT_TYPE_CHECKSUM_INSTREAM, OtChecksumInstreamPrivate); + +} + +OtChecksumInstream * +ot_checksum_instream_new (GInputStream *base, + GChecksumType checksum_type) +{ + OtChecksumInstream *stream; + + g_return_val_if_fail (G_IS_INPUT_STREAM (base), NULL); + + stream = g_object_new (OT_TYPE_CHECKSUM_INSTREAM, + "base-stream", base, + NULL); + stream->priv->checksum = g_checksum_new (checksum_type); + + return (OtChecksumInstream*) (stream); +} + +static gssize +ot_checksum_instream_read (GInputStream *stream, + void *buffer, + gsize count, + GCancellable *cancellable, + GError **error) +{ + OtChecksumInstream *self = (OtChecksumInstream*) stream; + GFilterInputStream *fself = (GFilterInputStream*) self; + gssize res = -1; + + res = g_input_stream_read (fself->base_stream, + buffer, + count, + cancellable, + error); + if (res > 0) + g_checksum_update (self->priv->checksum, buffer, res); + + return res; +} + +void +ot_checksum_instream_get_digest (OtChecksumInstream *stream, + guint8 *buffer, + gsize *digest_len) +{ + g_checksum_get_digest (stream->priv->checksum, buffer, digest_len); +} + +guint8* +ot_checksum_instream_dup_digest (OtChecksumInstream *stream, + gsize *ret_len) +{ + gsize len = 32; + guchar *ret = g_malloc (len); + g_checksum_get_digest (stream->priv->checksum, ret, &len); + g_assert (len == 32); + if (ret_len) + *ret_len = len; + return ret; +} + +char * +ot_checksum_instream_get_string (OtChecksumInstream *stream) +{ + return g_strdup (g_checksum_get_string (stream->priv->checksum)); +} diff --git a/src/libotutil/ot-checksum-instream.h b/src/libotutil/ot-checksum-instream.h new file mode 100644 index 00000000..78a5d44e --- /dev/null +++ b/src/libotutil/ot-checksum-instream.h @@ -0,0 +1,64 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- + * + * Copyright (C) 2011 Colin Walters <walters@verbum.org> + * + * 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 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. + * + */ + +#pragma once + +#include <gio/gio.h> + +G_BEGIN_DECLS + +#define OT_TYPE_CHECKSUM_INSTREAM (ot_checksum_instream_get_type ()) +#define OT_CHECKSUM_INSTREAM(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), OT_TYPE_CHECKSUM_INPUT_STREAM, OtChecksumInstream)) +#define OT_CHECKSUM_INSTREAM_CLASS(k) (G_TYPE_CHECK_CLASS_CAST((k), OT_TYPE_CHECKSUM_INPUT_STREAM, OtChecksumInstreamClass)) +#define OT_IS_CHECKSUM_INSTREAM(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), OT_TYPE_CHECKSUM_INPUT_STREAM)) +#define OT_IS_CHECKSUM_INSTREAM_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), OT_TYPE_CHECKSUM_INPUT_STREAM)) +#define OT_CHECKSUM_INSTREAM_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), OT_TYPE_CHECKSUM_INPUT_STREAM, OtChecksumInstreamClass)) + +typedef struct _OtChecksumInstream OtChecksumInstream; +typedef struct _OtChecksumInstreamClass OtChecksumInstreamClass; +typedef struct _OtChecksumInstreamPrivate OtChecksumInstreamPrivate; + +struct _OtChecksumInstream +{ + GFilterInputStream parent_instance; + + /*< private >*/ + OtChecksumInstreamPrivate *priv; +}; + +struct _OtChecksumInstreamClass +{ + GFilterInputStreamClass parent_class; +}; + +GType ot_checksum_instream_get_type (void) G_GNUC_CONST; + +OtChecksumInstream * ot_checksum_instream_new (GInputStream *stream, GChecksumType checksum); + +void ot_checksum_instream_get_digest (OtChecksumInstream *stream, + guint8 *buffer, + gsize *digest_len); + +guint8* ot_checksum_instream_dup_digest (OtChecksumInstream *stream, + gsize *ret_len); +char * ot_checksum_instream_get_string (OtChecksumInstream *stream); + +G_END_DECLS diff --git a/src/libotutil/otutil.h b/src/libotutil/otutil.h index c66d5634..f6158de8 100644 --- a/src/libotutil/otutil.h +++ b/src/libotutil/otutil.h @@ -53,5 +53,6 @@ #include <ot-checksum-utils.h> #include <ot-gpg-utils.h> #include <ot-log-utils.h> +#include <ot-checksum-instream.h> void ot_ptrarray_add_many (GPtrArray *a, ...) G_GNUC_NULL_TERMINATED; |