[PATCH libcacard 06/45] simpletlv: Add a way to skip unused members

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

 



Signed-off-by: Jakub Jelen <jjelen@xxxxxxxxxx>
Reviewed-by: Robert Relyea <rrelyea@xxxxxxxxxx>
---
 src/simpletlv.c   | 11 ++++++++-
 src/simpletlv.h   |  5 ++--
 tests/simpletlv.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 73 insertions(+), 3 deletions(-)

diff --git a/src/simpletlv.c b/src/simpletlv.c
index 0a20056..5cea04b 100644
--- a/src/simpletlv.c
+++ b/src/simpletlv.c
@@ -42,10 +42,14 @@ simpletlv_get_length(struct simpletlv_member *tlv, size_t tlv_len,
     int child_length;
 
     for (i = 0; i < tlv_len; i++) {
+        /* This TLV is skipped */
+        if (tlv[i].type == SIMPLETLV_TYPE_NONE)
+            continue;
+
         /* We can not unambiguously split the buffers
          * for recursive structures
          */
-        if (tlv[i].type != SIMPLETLV_TYPE_LEAF
+        if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND
             && buffer_type != SIMPLETLV_BOTH)
             return -1;
 
@@ -100,6 +104,11 @@ simpletlv_encode_internal(struct simpletlv_member *tlv, size_t tlv_len,
     p_len = tmp_len;
     for (i = 0; i < tlv_len; i++) {
         size_t child_length = tlv[i].length;
+
+        /* This TLV is skipped */
+        if (tlv[i].type == SIMPLETLV_TYPE_NONE)
+            continue;
+
         if (tlv[i].type == SIMPLETLV_TYPE_COMPOUND) {
             child_length = simpletlv_get_length(tlv[i].value.child,
                 tlv[i].length, SIMPLETLV_BOTH);
diff --git a/src/simpletlv.h b/src/simpletlv.h
index 6d0e229..dcb795b 100644
--- a/src/simpletlv.h
+++ b/src/simpletlv.h
@@ -25,8 +25,9 @@
 #define _SIMPLETLV_H
 
 enum simpletlv_type {
-    SIMPLETLV_TYPE_LEAF = 0,
-    SIMPLETLV_TYPE_COMPOUND = 1
+    SIMPLETLV_TYPE_NONE = 0,
+    SIMPLETLV_TYPE_LEAF = 1,
+    SIMPLETLV_TYPE_COMPOUND = 2
 };
 
 enum simpletlv_buffer_type {
diff --git a/tests/simpletlv.c b/tests/simpletlv.c
index 0d4a66b..3bd22ae 100644
--- a/tests/simpletlv.c
+++ b/tests/simpletlv.c
@@ -73,6 +73,31 @@ static void test_length_nested(void)
     g_assert_cmpint(length, ==, -1);
 }
 
+static void test_length_skipped(void)
+{
+    size_t length = 0;
+    unsigned char simple_value[] = "\x12\x14";
+    unsigned char simple_value2[] = "\x16\x18";
+    static struct simpletlv_member simple[2] = {
+      {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF},
+      {0x30, 2, {/*.value = simple_value2*/}, SIMPLETLV_TYPE_NONE}
+    };
+    simple[0].value.value = simple_value;
+    simple[1].value.value = simple_value2;
+
+    /* Simple short value to TLV */
+    length = simpletlv_get_length(simple, 2, SIMPLETLV_BOTH);
+    g_assert_cmpint(length, ==, 4);
+
+    /* Simple short value to TL */
+    length = simpletlv_get_length(simple, 2, SIMPLETLV_TL);
+    g_assert_cmpint(length, ==, 2);
+
+    /* Simple short value to V */
+    length = simpletlv_get_length(simple, 2, SIMPLETLV_VALUE);
+    g_assert_cmpint(length, ==, 2);
+}
+
 /* Test that we can encode arbitrary data into Simple TLV */
 static void test_encode_simple(void)
 {
@@ -184,6 +209,39 @@ static void test_encode_nested(void)
     g_assert_cmpint(result_len, ==, -1);
 }
 
+static void test_encode_skipped(void)
+{
+    unsigned char *result = NULL;
+    size_t result_len = 0;
+    unsigned char simple_value[] = "\x12\x14";
+    unsigned char simple_value2[] = "\x16\x18";
+    static struct simpletlv_member simple[2] = {
+      {0x25, 2, {/*.value = simple_value*/}, SIMPLETLV_TYPE_LEAF},
+      {0x30, 2, {/*.value = simple_value2*/}, SIMPLETLV_TYPE_NONE}
+    };
+    unsigned char encoded[] = "\x25\x02\x12\x14";
+    simple[0].value.value = simple_value;
+    simple[1].value.value = simple_value2;
+
+    /* Simple short value to TLV */
+    result = NULL;
+    result_len = simpletlv_encode(simple, 2, &result, 0, NULL);
+    g_assert_cmpmem(result, result_len, encoded, 4);
+    g_free(result);
+
+    /* Simple short value to TL */
+    result = NULL;
+    result_len = simpletlv_encode_tl(simple, 2, &result, 0, NULL);
+    g_assert_cmpmem(result, result_len, "\x25\x02", 2);
+    g_free(result);
+
+    /* Simple short value to V */
+    result = NULL;
+    result_len = simpletlv_encode_val(simple, 2, &result, 0, NULL);
+    g_assert_cmpmem(result, result_len, "\x12\x14", 2);
+    g_free(result);
+}
+
 int main(int argc, char *argv[])
 {
     int ret;
@@ -194,8 +252,10 @@ int main(int argc, char *argv[])
 
     g_test_add_func("/simpletlv/length/simple", test_length_simple);
     g_test_add_func("/simpletlv/length/nested", test_length_nested);
+    g_test_add_func("/simpletlv/length/skipped", test_length_skipped);
     g_test_add_func("/simpletlv/encode/simple", test_encode_simple);
     g_test_add_func("/simpletlv/encode/nested", test_encode_nested);
+    g_test_add_func("/simpletlv/encode/skipped", test_encode_skipped);
 
     ret = g_test_run();
 
-- 
2.17.1

_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux Virtualization]     [Linux Virtualization]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]