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
This commit is contained in:
Colin Walters authored and Atomic Bot committed 2017-03-20 18:32:40 +00:00
1 parent 0b214566a2
commit c2f5a999bf
5 files changed
+206 -13

No files matched your search

+129
View File
@@ -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));
}
+64
View File
@@ -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
+1
View File
@@ -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;