diff options
author | Stef Walter <stefw@redhat.com> | 2014-04-17 07:09:34 +0200 |
---|---|---|
committer | Stef Walter <stefw@redhat.com> | 2014-04-19 22:29:49 +0200 |
commit | a96a6c7c5b4244e01c07918dccbfe02d780f2a5a (patch) | |
tree | a36c630f5dfc6fdd5e4d6db80070d7d5402a8be3 /egg | |
parent | 81ede79d157e06a6143c41608b56d9bd6c71a963 (diff) | |
download | gcr-a96a6c7c5b4244e01c07918dccbfe02d780f2a5a.tar.gz |
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.
Diffstat (limited to 'egg')
-rw-r--r-- | egg/egg-asn1x.c | 50 | ||||
-rw-r--r-- | 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 0daf2ef..566a47a 100644 --- a/egg/egg-asn1x.c +++ b/egg/egg-asn1x.c @@ -2909,6 +2909,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) { @@ -2934,12 +2975,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); |