[gcab/wip/hughsie/meson] trivial: Fix the endian checksum calculation in a better way



commit f7195be9db33d1676c79b1294ca52f28a5b24ebb
Author: Richard Hughes <richard hughsie com>
Date:   Tue Dec 12 13:52:57 2017 +0000

    trivial: Fix the endian checksum calculation in a better way

 libgcab/cabinet.c |   14 +++++++++-----
 1 files changed, 9 insertions(+), 5 deletions(-)
---
diff --git a/libgcab/cabinet.c b/libgcab/cabinet.c
index caf695e..fd99761 100644
--- a/libgcab/cabinet.c
+++ b/libgcab/cabinet.c
@@ -421,8 +421,11 @@ cdata_write (cdata_t *cd, GDataOutputStream *out, int type,
         return FALSE;
 
     guint32 datacsum = compute_checksum(cd->in, cd->ncbytes, 0);
-    guint32 sizecsum = GUINT32_TO_LE(cd->ncbytes << 16 | cd->nubytes);
-    cd->checksum = compute_checksum ((guint8*)&sizecsum, 4, datacsum);
+    guint8 sizecsum[4];
+
+    memcpy (&sizecsum[0], &cd->ncbytes, 2);
+    memcpy (&sizecsum[2], &cd->nubytes, 2);
+    cd->checksum = compute_checksum (sizecsum, sizeof(sizecsum), datacsum);
     GOutputStream *stream = g_filter_output_stream_get_base_stream (G_FILTER_OUTPUT_STREAM (out));
 
     *bytes_written = 0;
@@ -465,7 +468,7 @@ cdata_read (cdata_t *cd, guint8 res_data, gint comptype,
     gint compression = comptype & GCAB_COMPRESSION_MASK;
     guint8 *buf = compression == GCAB_COMPRESSION_NONE ? cd->out : cd->in;
     guint32 datacsum;
-    guint32 sizecsum;
+    guint8 sizecsum[4];
 
     if (compression > GCAB_COMPRESSION_MSZIP &&
         compression != GCAB_COMPRESSION_LZX) {
@@ -482,8 +485,9 @@ cdata_read (cdata_t *cd, guint8 res_data, gint comptype,
     RN (buf, cd->ncbytes);
 
     datacsum = compute_checksum(buf, cd->ncbytes, 0);
-    sizecsum = GUINT32_TO_LE(cd->ncbytes << 16 | cd->nubytes);
-    if (cd->checksum != compute_checksum ((guint8*)&sizecsum, 4, datacsum)) {
+    memcpy (&sizecsum[0], &cd->ncbytes, 2);
+    memcpy (&sizecsum[2], &cd->nubytes, 2);
+    if (cd->checksum != compute_checksum (sizecsum, sizeof(sizecsum), datacsum)) {
         g_set_error_literal (error, GCAB_ERROR, GCAB_ERROR_FAILED,
                              _("incorrect checksum detected"));
         return FALSE;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]