[gnome-keyring/wip/header-order] egg: Write Proc-Type header before DEK-Info



commit a72bd7bd41bc1d69f04d619898c4f0421cd7cf29
Author: Iain Lane <iainl gnome org>
Date:   Mon Jan 28 14:38:40 2019 +0000

    egg: Write Proc-Type header before DEK-Info
    
    These headers (at least for OpenSSL) must come in this order. We
    shouldn't assume that `g_hash_table_foreach` is going to give us this
    one out.

 egg/egg-armor.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)
---
diff --git a/egg/egg-armor.c b/egg/egg-armor.c
index 267cc87b..d6f72956 100644
--- a/egg/egg-armor.c
+++ b/egg/egg-armor.c
@@ -59,6 +59,8 @@ EGG_SECURE_DECLARE (armor);
 #define ARMOR_PREF_END      "-----END "
 #define ARMOR_PREF_END_L    9
 
+static const gchar * const ORDERED_HEADERS[] = { "Proc-Type", "DEK-Info", NULL };
+
 static void
 parse_header_lines (const gchar *hbeg,
                     const gchar *hend,
@@ -336,10 +338,13 @@ egg_armor_parse (GBytes *data,
 }
 
 static void
-append_each_header (gpointer key, gpointer value, gpointer user_data)
+append_each_header (gconstpointer key, gconstpointer value, gpointer user_data)
 {
        GString *string = (GString*)user_data;
 
+       if (g_strv_contains (ORDERED_HEADERS, (gchar*) key))
+               return;
+
        g_string_append (string, (gchar*)key);
        g_string_append (string, ": ");
        g_string_append (string, (gchar*)value);
@@ -357,6 +362,7 @@ egg_armor_write (const guchar *data,
        gint state, save;
        gsize i, length;
        gsize n_prefix, estimate;
+       gchar *value;
 
        g_return_val_if_fail (data || !n_data, NULL);
        g_return_val_if_fail (type, NULL);
@@ -370,9 +376,19 @@ egg_armor_write (const guchar *data,
        g_string_append_len (string, ARMOR_SUFF, ARMOR_SUFF_L);
        g_string_append_c (string, '\n');
 
-       /* The headers */
+       /* The headers. Some must come in a specific order. */
+       for (i = 0; ORDERED_HEADERS[i] != NULL; i++) {
+               value = g_hash_table_lookup (headers, ORDERED_HEADERS[i]);
+               if (value != NULL)
+                       g_string_append_printf (string,
+                                               "%s: %s\n",
+                                               ORDERED_HEADERS[i],
+                                               value);
+       }
+
+       /* And the rest we output in any arbitrary order. */
        if (headers && g_hash_table_size (headers) > 0) {
-               g_hash_table_foreach (headers, append_each_header, string);
+               g_hash_table_foreach (headers, (GHFunc) append_each_header, string);
                g_string_append_c (string, '\n');
        }
 


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