<commit_before>#include "checksum.h"

#define RAUC_DEFAULT_CHECKSUM G_CHECKSUM_SHA256

G_DEFINE_QUARK(r-checksum-error-quark, r_checksum_error)

gboolean compute_checksum(RaucChecksum *checksum, const gchar *filename, GError **error)
{
	GError *ierror = NULL;
	g_autoptr(GMappedFile) file = NULL;
	g_autoptr(GBytes) content = NULL;
	gboolean res = FALSE;

	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

	file = g_mapped_file_new(filename, FALSE, &ierror);
	if (file == NULL) {
		g_propagate_error(error, ierror);
		goto out;
	}
	content = g_mapped_file_get_bytes(file);

	if (checksum->digest == NULL)
		checksum->type = RAUC_DEFAULT_CHECKSUM;
	g_clear_pointer(&checksum->digest, g_free);
	checksum->digest = g_compute_checksum_for_bytes(checksum->type, content);
	checksum->size = g_bytes_get_size(content);

	res = TRUE;
out:
	if (!res) {
		g_clear_pointer(&checksum->digest, g_free);
		checksum->size = 0;
	}
	return res;
}

gboolean verify_checksum(const RaucChecksum *checksum, const gchar *filename, GError **error)
{
	GError *ierror = NULL;
	g_autoptr(GMappedFile) file = NULL;
	g_autoptr(GBytes) content = NULL;
	g_autofree gchar *digest = NULL;
	gboolean res = FALSE;<commit_msg>checksum: check for ->type being 0, not ->digest being NULL<commit_after>#include "checksum.h"

#define RAUC_DEFAULT_CHECKSUM G_CHECKSUM_SHA256
/*
 * G_CHECKSUM_MD5 is 0. We will never allow use of such a weak hash
 * for anything. Hence checking for !checksum->type below to mean "use
 * the default" is ok.
 */
G_STATIC_ASSERT(G_CHECKSUM_MD5 == 0);
G_STATIC_ASSERT(RAUC_DEFAULT_CHECKSUM != 0);

G_DEFINE_QUARK(r-checksum-error-quark, r_checksum_error)

gboolean compute_checksum(RaucChecksum *checksum, const gchar *filename, GError **error)
{
	GError *ierror = NULL;
	g_autoptr(GMappedFile) file = NULL;
	g_autoptr(GBytes) content = NULL;
	gboolean res = FALSE;

	g_return_val_if_fail(error == NULL || *error == NULL, FALSE);

	file = g_mapped_file_new(filename, FALSE, &ierror);
	if (file == NULL) {
		g_propagate_error(error, ierror);
		goto out;
	}
	content = g_mapped_file_get_bytes(file);

	if (!checksum->type)
		checksum->type = RAUC_DEFAULT_CHECKSUM;
	g_clear_pointer(&checksum->digest, g_free);
	checksum->digest = g_compute_checksum_for_bytes(checksum->type, content);
	checksum->size = g_bytes_get_size(content);

	res = TRUE;
out:
	if (!res) {
		g_clear_pointer(&checksum->digest, g_free);
		checksum->size = 0;
	}
	return res;
}

gboolean verify_checksum(const RaucChecksum *checksum, const gchar *filename, GError **error)
{
	GError *ierror = NULL;
	g_autoptr(GMappedFile) file = NULL;
	g_autoptr(GBytes) content = NULL;
	g_autofree gchar *digest = NULL;
	gboolean res = FALSE;
{'fraction_non_alphanumeric': 0.12004446091144869, 'fraction_numerical': 0.005187106335679882, 'mean_word_length': 5.7164179104477615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4389800', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1023, 'n_words': 224}