core: Clean up checksumming
Don't expose GChecksum in APIs. Add a new stream class which allows us to pass an input stream somewhere, but gather a checksum as it's read. Move some bits of the internals towards binary csums.
This commit is contained in:
1 parent
fa4cbc8b89
commit
f15c184584
17 files changed
+605
-274
No files matched your search
@@ -0,0 +1,38 @@
|
||||
/* -*- 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.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "otutil.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
guchar *
|
||||
ot_csum_from_gchecksum (GChecksum *checksum)
|
||||
{
|
||||
guchar *ret = g_malloc (32);
|
||||
gsize len = 32;
|
||||
|
||||
g_checksum_get_digest (checksum, ret, &len);
|
||||
g_assert (len == 32);
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
/* -*- 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.
|
||||
*
|
||||
* Author: Colin Walters <walters@verbum.org>
|
||||
*/
|
||||
|
||||
#ifndef __OSTREE_CHECKSUM_UTILS_H__
|
||||
#define __OSTREE_CHECKSUM_UTILS_H__
|
||||
|
||||
#include <gio/gio.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
guchar *ot_csum_from_gchecksum (GChecksum *checksum);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif
|
||||
@@ -229,21 +229,17 @@ ot_gfile_get_basename_cached (GFile *file)
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_gio_splice_and_checksum (GOutputStream *out,
|
||||
GInputStream *in,
|
||||
GChecksum **out_checksum,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
ot_gio_splice_update_checksum (GOutputStream *out,
|
||||
GInputStream *in,
|
||||
GChecksum *checksum,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
gboolean ret = FALSE;
|
||||
GChecksum *ret_checksum = NULL;
|
||||
|
||||
g_return_val_if_fail (out != NULL || out_checksum != NULL, FALSE);
|
||||
g_return_val_if_fail (out != NULL || checksum != NULL, FALSE);
|
||||
|
||||
if (out_checksum)
|
||||
ret_checksum = g_checksum_new (G_CHECKSUM_SHA256);
|
||||
|
||||
if (ret_checksum != NULL)
|
||||
if (checksum != NULL)
|
||||
{
|
||||
gsize bytes_read, bytes_written;
|
||||
char buf[4096];
|
||||
@@ -251,8 +247,7 @@ ot_gio_splice_and_checksum (GOutputStream *out,
|
||||
{
|
||||
if (!g_input_stream_read_all (in, buf, sizeof(buf), &bytes_read, cancellable, error))
|
||||
goto out;
|
||||
if (ret_checksum)
|
||||
g_checksum_update (ret_checksum, (guint8*)buf, bytes_read);
|
||||
g_checksum_update (checksum, (guint8*)buf, bytes_read);
|
||||
if (out)
|
||||
{
|
||||
if (!g_output_stream_write_all (out, buf, bytes_read, &bytes_written, cancellable, error))
|
||||
@@ -268,21 +263,44 @@ ot_gio_splice_and_checksum (GOutputStream *out,
|
||||
}
|
||||
|
||||
ret = TRUE;
|
||||
ot_transfer_out_value(out_checksum, &ret_checksum);
|
||||
out:
|
||||
ot_clear_checksum (&ret_checksum);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_gio_splice_get_checksum (GOutputStream *out,
|
||||
GInputStream *in,
|
||||
guchar **out_csum,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
gboolean ret = FALSE;
|
||||
GChecksum *checksum = NULL;
|
||||
ot_lfree guchar *ret_csum = NULL;
|
||||
|
||||
checksum = g_checksum_new (G_CHECKSUM_SHA256);
|
||||
|
||||
if (!ot_gio_splice_update_checksum (out, in, checksum, cancellable, error))
|
||||
goto out;
|
||||
|
||||
ret_csum = ot_csum_from_gchecksum (checksum);
|
||||
|
||||
ret = TRUE;
|
||||
ot_transfer_out_value (out_csum, &ret_csum);
|
||||
out:
|
||||
ot_clear_checksum (&checksum);
|
||||
return ret;
|
||||
}
|
||||
|
||||
gboolean
|
||||
ot_gio_checksum_stream (GInputStream *in,
|
||||
GChecksum **out_checksum,
|
||||
guchar **out_csum,
|
||||
GCancellable *cancellable,
|
||||
GError **error)
|
||||
{
|
||||
if (!out_checksum)
|
||||
if (!out_csum)
|
||||
return TRUE;
|
||||
return ot_gio_splice_and_checksum (NULL, in, out_checksum, cancellable, error);
|
||||
return ot_gio_splice_get_checksum (NULL, in, out_csum, cancellable, error);
|
||||
}
|
||||
|
||||
gboolean
|
||||
|
||||
@@ -61,14 +61,20 @@ gboolean ot_gfile_load_contents_utf8 (GFile *file,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
gboolean ot_gio_splice_and_checksum (GOutputStream *out,
|
||||
gboolean ot_gio_splice_get_checksum (GOutputStream *out,
|
||||
GInputStream *in,
|
||||
GChecksum **out_checksum,
|
||||
guchar **out_csum,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
gboolean ot_gio_splice_update_checksum (GOutputStream *out,
|
||||
GInputStream *in,
|
||||
GChecksum *checksum,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
gboolean ot_gio_checksum_stream (GInputStream *in,
|
||||
GChecksum **out_checksum,
|
||||
guchar **out_csum,
|
||||
GCancellable *cancellable,
|
||||
GError **error);
|
||||
|
||||
|
||||
@@ -159,9 +159,9 @@ ot_util_variant_from_stream (GInputStream *src,
|
||||
|
||||
data_stream = (GMemoryOutputStream*)g_memory_output_stream_new (NULL, 0, g_realloc, g_free);
|
||||
|
||||
if (!g_output_stream_splice ((GOutputStream*)data_stream, src,
|
||||
G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
|
||||
cancellable, error))
|
||||
if (g_output_stream_splice ((GOutputStream*)data_stream, src,
|
||||
G_OUTPUT_STREAM_SPLICE_CLOSE_SOURCE | G_OUTPUT_STREAM_SPLICE_CLOSE_TARGET,
|
||||
cancellable, error) < 0)
|
||||
goto out;
|
||||
|
||||
ret_variant = g_variant_new_from_data (type, g_memory_output_stream_get_data (data_stream),
|
||||
|
||||
@@ -47,5 +47,6 @@
|
||||
#include <ot-opt-utils.h>
|
||||
#include <ot-unix-utils.h>
|
||||
#include <ot-variant-utils.h>
|
||||
#include <ot-checksum-utils.h>
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user