[gcr/ecc: 7/16] asn1: Add egg_asn1x_get_string_as_usg function



commit 7c0f90f3a5c9dc781d5b1c33ee05bac282df4ca1
Author: Stef Walter <stefw redhat com>
Date:   Thu Apr 17 07:09:34 2014 +0200

    asn1: Add egg_asn1x_get_string_as_usg function
    
    Gets a string as an unsigned integer, removing leading zeros, and
    combining strings of indefinite length.

 egg/egg-asn1x.c |   50 ++++++++++++++++++++++++++++++++++++++++++++------
 egg/egg-asn1x.h |    3 +++
 2 files changed, 47 insertions(+), 6 deletions(-)
---
diff --git a/egg/egg-asn1x.c b/egg/egg-asn1x.c
index 490b46a..efa5107 100644
--- a/egg/egg-asn1x.c
+++ b/egg/egg-asn1x.c
@@ -2924,6 +2924,47 @@ egg_asn1x_get_integer_as_raw (GNode *node)
        return raw;
 }
 
+typedef struct {
+       EggAllocator allocator;
+       gpointer data;
+} AllocatedData;
+
+static void
+allocator_free (gpointer data)
+{
+       AllocatedData *alloc = data;
+       (alloc->allocator) (alloc->data, 0);
+       g_free (data);
+}
+
+GBytes *
+egg_asn1x_get_string_as_usg (GNode *node,
+                             EggAllocator allocator)
+{
+       AllocatedData *alloc;
+       guchar *raw;
+       const guchar *p;
+       gsize len;
+
+       g_return_val_if_fail (node != NULL, FALSE);
+
+       p = raw = egg_asn1x_get_string_as_raw (node, allocator, &len);
+       if (raw == NULL)
+               return NULL;
+
+       /* Strip off the extra zero bytes */
+       while (p[0] == 0 && len > 1) {
+               p++;
+               len--;
+       }
+
+       alloc = g_new0 (AllocatedData, 1);
+       alloc->allocator = allocator ? allocator : g_realloc;
+       alloc->data = raw;
+
+       return g_bytes_new_with_free_func (p, len, allocator_free, alloc);
+}
+
 GBytes *
 egg_asn1x_get_integer_as_usg (GNode *node)
 {
@@ -2949,12 +2990,9 @@ egg_asn1x_get_integer_as_usg (GNode *node)
                }
 
                /* Strip off the extra zero byte that was preventing it from being negative */
-               if (p[0] == 0 && len > 1) {
-                       sign = !!(p[1] & 0x80);
-                       if (sign) {
-                               p++;
-                               len--;
-                       }
+               while (p[0] == 0 && len > 1) {
+                       p++;
+                       len--;
                }
        }
 
diff --git a/egg/egg-asn1x.h b/egg/egg-asn1x.h
index 0cda783..e379da5 100644
--- a/egg/egg-asn1x.h
+++ b/egg/egg-asn1x.h
@@ -201,6 +201,9 @@ GBytes *            egg_asn1x_get_string_as_bytes    (GNode *node);
 void                egg_asn1x_set_string_as_bytes    (GNode *node,
                                                       GBytes *bytes);
 
+GBytes *            egg_asn1x_get_string_as_usg      (GNode *node,
+                                                     EggAllocator allocator);
+
 GBytes *            egg_asn1x_get_bits_as_raw        (GNode *node,
                                                       guint *n_bits);
 


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