Switch from optional flags to optional elements
authorJonathan Dieter <jdieter@gmail.com>
Tue, 13 Nov 2018 13:32:38 +0000 (13:32 +0000)
committerJonathan Dieter <jdieter@gmail.com>
Tue, 13 Nov 2018 13:53:27 +0000 (13:53 +0000)
Signed-off-by: Jonathan Dieter <jdieter@gmail.com>
src/lib/header.c
src/lib/zck_private.h

index cc402f2679ac7a54c89b8e851703d785ae86194e..38b587b28adeeba19dc20847e978e2cbb49b41a3 100644 (file)
@@ -41,8 +41,8 @@ static bool check_flags(zckCtx *zck, size_t flags) {
                         "This version of zchunk doesn't support streams");
         return false;
     }
-    zck->has_optional_flags = flags & 2;
-    if(zck->has_optional_flags)
+    zck->has_optional_elems = flags & 2;
+    if(zck->has_optional_elems)
         flags -= 2;
     flags = flags & (SIZE_MAX - 1);
     if(flags != 0) {
@@ -52,10 +52,9 @@ static bool check_flags(zckCtx *zck, size_t flags) {
     return true;
 }
 
-static bool check_optional_flags(zckCtx *zck, size_t flags) {
-    flags = flags & (SIZE_MAX - 1);
-    if(flags != 0)
-        zck_log(ZCK_LOG_WARNING, "Unknown optional flags %i set", flags);
+static bool read_optional_element(zckCtx *zck, size_t id, size_t data_size,
+                                  char *data) {
+    zck_log(ZCK_LOG_WARNING, "Unknown optional element id %i set", id);
     return true;
 }
 
@@ -140,19 +139,23 @@ static bool read_preface(zckCtx *zck) {
         return false;
 
     /* Read optional flags */
-    if(zck->has_optional_flags) {
-        size_t opt_flags = 0;
-        if(!compint_to_size(zck, &opt_flags, header+length, &length,
-                            max_length))
-            return false;
-        if(!check_optional_flags(zck, opt_flags))
-            return false;
-        size_t opt_flag_data_size = 0;
-        if(!compint_to_size(zck, &opt_flag_data_size, header+length, &length,
+    if(zck->has_optional_elems) {
+        size_t opt_count = 0;
+        if(!compint_to_size(zck, &opt_count, header+length, &length,
                             max_length))
             return false;
-        if(opt_flag_data_size > 0)
-            length += opt_flag_data_size;
+        for(size_t i=0; i<opt_count; i++) {
+            size_t id = 0;
+            size_t data_size = 0;
+            if(!compint_to_size(zck, &id, header+length, &length, max_length))
+                return false;
+            if(!compint_to_size(zck, &data_size, header+length, &length,
+                                max_length))
+                return false;
+            if(!read_optional_element(zck, id, data_size, header+length))
+                return false;
+            length += data_size;
+        }
     }
 
     /* Read and initialize index size */
index 5c40ccafc8731f23377fa248687219eac279a02e..0e4cbc5e7843bf46816cc41f039a4dab16bf651b 100644 (file)
@@ -260,7 +260,7 @@ typedef struct zckCtx {
     zckHash work_index_hash;
     size_t stream;
     int has_streams;
-    int has_optional_flags;
+    int has_optional_elems;
 
     char *read_buf;
     size_t read_buf_size;