[gcab] trivial: Allocate cdata_t on the heap



commit c512f6ff0c82a1139b36db2b28f93edc01c74b4b
Author: Richard Hughes <richard hughsie com>
Date:   Fri Dec 15 09:36:12 2017 +0000

    trivial: Allocate cdata_t on the heap
    
    Using a 91kB stack allocation for one object isn't awesome, and it also allows
    us to use g_autoptr() to simplify gcab_folder_extract()

 libgcab/cabinet.c     |   20 ++++++++------------
 libgcab/cabinet.h     |    3 ++-
 libgcab/gcab-folder.c |   34 ++++++++++++++--------------------
 3 files changed, 24 insertions(+), 33 deletions(-)
---
diff --git a/libgcab/cabinet.c b/libgcab/cabinet.c
index d008b32..96f821d 100644
--- a/libgcab/cabinet.c
+++ b/libgcab/cabinet.c
@@ -496,25 +496,21 @@ cdata_write (cdata_t *cd, GDataOutputStream *out, int type,
 }
 
 G_GNUC_INTERNAL void
-cdata_finish (cdata_t *cd, GError **error)
+cdata_free (cdata_t *cd)
 {
     z_stream *z = &cd->z;
-    int zret;
 
     if (cd->decomp.comptype == GCAB_COMPRESSION_LZX) {
         LZXfdi_clear (&cd->decomp);
-        return;
     }
 
-    if (!z->opaque)
-        return;
-
-    zret = inflateEnd (z);
-    z->opaque = NULL;
-
-    if (zret != Z_OK)
-        g_set_error (error, GCAB_ERROR, GCAB_ERROR_FAILED,
-                     "zlib failed: %s", zError (zret));
+    if (cd->decomp.comptype == GCAB_COMPRESSION_MSZIP) {
+        if (z->opaque) {
+            inflateEnd (z);
+            z->opaque = NULL;
+        }
+    }
+    g_free (cd);
 }
 
 static gint
diff --git a/libgcab/cabinet.h b/libgcab/cabinet.h
index 0a2b873..850ac65 100644
--- a/libgcab/cabinet.h
+++ b/libgcab/cabinet.h
@@ -154,10 +154,11 @@ gboolean     cdata_read                         (cdata_t *cd,
                                                  GDataInputStream *in,
                                                  GCancellable *cancellable,
                                                  GError **error);
-void         cdata_finish                       (cdata_t *cd, GError **error);
+void         cdata_free                         (cdata_t *cd);
 
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(cfolder_t, cfolder_free)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(cfile_t, cfile_free)
 G_DEFINE_AUTOPTR_CLEANUP_FUNC(cheader_t, cheader_free)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(cdata_t, cdata_free)
 
 #endif /* CABINET_H */
diff --git a/libgcab/gcab-folder.c b/libgcab/gcab-folder.c
index 174a5c6..de15ecf 100644
--- a/libgcab/gcab-folder.c
+++ b/libgcab/gcab-folder.c
@@ -391,12 +391,11 @@ gcab_folder_extract (GCabFolder *self,
                      GError **error)
 {
     GError *my_error = NULL;
-    gboolean success = FALSE;
     g_autoptr(GDataInputStream) data = NULL;
     g_autoptr(GFileOutputStream) out = NULL;
     GSList *f = NULL;
     g_autoptr(GSList) files = NULL;
-    cdata_t cdata = { 0, };
+    g_autoptr(cdata_t) cdata = g_new0 (cdata_t, 1);
     guint32 nubytes = 0;
 
     /* never loaded from a stream */
@@ -407,7 +406,7 @@ gcab_folder_extract (GCabFolder *self,
     g_filter_input_stream_set_close_base_stream (G_FILTER_INPUT_STREAM (data), FALSE);
 
     if (!g_seekable_seek (G_SEEKABLE (data), self->cfolder->offsetdata, G_SEEK_SET, cancellable, error))
-        goto end;
+        return FALSE;
 
     files = g_slist_sort (g_slist_copy (self->files), (GCompareFunc)sort_by_offset);
 
@@ -445,14 +444,14 @@ gcab_folder_extract (GCabFolder *self,
                 g_clear_error (&my_error);
             else {
                 g_propagate_error (error, my_error);
-                goto end;
+                return FALSE;
             }
         }
 
         g_autoptr(GFileOutputStream) out2 = NULL;
         out2 = g_file_replace (gfile, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, cancellable, error);
         if (!out2)
-            goto end;
+            return FALSE;
 
         guint32 usize = gcab_file_get_usize (file);
         guint32 uoffset = gcab_file_get_uoffset (file);
@@ -461,36 +460,31 @@ gcab_folder_extract (GCabFolder *self,
         if (uoffset < nubytes) {
             if (!g_seekable_seek (G_SEEKABLE (data), self->cfolder->offsetdata,
                                   G_SEEK_SET, cancellable, error))
-                goto end;
-            bzero(&cdata, sizeof(cdata));
+                return FALSE;
+            bzero(cdata, sizeof(cdata_t));
             nubytes = 0;
         }
 
         while (usize > 0) {
-            if ((nubytes + cdata.nubytes) <= uoffset) {
-                nubytes += cdata.nubytes;
-                if (!cdata_read (&cdata, res_data, self->comptype,
+            if ((nubytes + cdata->nubytes) <= uoffset) {
+                nubytes += cdata->nubytes;
+                if (!cdata_read (cdata, res_data, self->comptype,
                                  data, cancellable, error))
-                    goto end;
+                    return FALSE;
                 continue;
             } else {
                 gsize offset = gcab_file_get_uoffset (file) > nubytes ?
                     gcab_file_get_uoffset (file) - nubytes : 0;
-                const void *p = &cdata.out[offset];
-                gsize count = MIN (usize, cdata.nubytes - offset);
+                const void *p = &cdata->out[offset];
+                gsize count = MIN (usize, cdata->nubytes - offset);
                 if (!g_output_stream_write_all (G_OUTPUT_STREAM (out2), p, count,
                                                 NULL, cancellable, error))
-                    goto end;
+                    return FALSE;
                 usize -= count;
                 uoffset += count;
             }
         }
     }
 
-    success = TRUE;
-
-end:
-    cdata_finish (&cdata, NULL);
-
-    return success;
+    return TRUE;
 }


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