Add support for optional flags
authorJonathan Dieter <jdieter@gmail.com>
Mon, 8 Oct 2018 12:57:42 +0000 (13:57 +0100)
committerJonathan Dieter <jdieter@gmail.com>
Mon, 8 Oct 2018 12:57:42 +0000 (13:57 +0100)
Signed-off-by: Jonathan Dieter <jdieter@gmail.com>
src/lib/header.c
src/lib/zck_private.h

index edefd64e44b90ca44ea36f841d3b22503f7e6b8f..cc402f2679ac7a54c89b8e851703d785ae86194e 100644 (file)
 static bool check_flags(zckCtx *zck, size_t flags) {
     zck->has_streams = flags & 1;
     if(zck->has_streams) {
+        flags -= 1;
         set_fatal_error(zck,
                         "This version of zchunk doesn't support streams");
         return false;
     }
+    zck->has_optional_flags = flags & 2;
+    if(zck->has_optional_flags)
+        flags -= 2;
     flags = flags & (SIZE_MAX - 1);
     if(flags != 0) {
         set_fatal_error(zck, "Unknown flags(s) set");
@@ -48,6 +52,13 @@ 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);
+    return true;
+}
+
 static bool read_header_from_file(zckCtx *zck) {
     /* Allocate header and store any extra bytes at beginning of header */
     zck->header = zrealloc(zck->header, zck->lead_size + zck->header_length);
@@ -128,6 +139,22 @@ static bool read_preface(zckCtx *zck) {
     if(!comp_init(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,
+                            max_length))
+            return false;
+        if(opt_flag_data_size > 0)
+            length += opt_flag_data_size;
+    }
+
     /* Read and initialize index size */
     if(!compint_to_int(zck, &tmp, header+length, &length, max_length))
         return false;
index 32b4413d2aad19bc4fa58dfc9752a9351a5cef00..5c40ccafc8731f23377fa248687219eac279a02e 100644 (file)
@@ -260,6 +260,7 @@ typedef struct zckCtx {
     zckHash work_index_hash;
     size_t stream;
     int has_streams;
+    int has_optional_flags;
 
     char *read_buf;
     size_t read_buf_size;