chiark / gitweb /
Description in superblock.
authorDan Sheppard <dan.sheppard.circle@gmail.com>
Tue, 22 Apr 2025 17:38:27 +0000 (18:38 +0100)
committerDan Sheppard <dan.sheppard.circle@gmail.com>
Tue, 22 Apr 2025 17:38:27 +0000 (18:38 +0100)
src/superblock.c
src/superblock.h
src/util.c
src/util.h
testdata/sb1.coquet

index c901a70f0bf5ed5c659f722e0b6186122ace27c5..4f2b690ce495f34711ecaf44ed8f2f13fb1df537 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdint.h>
 #include <string.h>
+#include <stdlib.h>
 #include "coquet.h"
 #include "util.h"
 #include "sha2.h"
@@ -16,8 +17,9 @@
  * | global_iv         |   16        32
  * | sb_hash           |   48        32
  * | sb_serial         |   80         8
+ * | description       |   88       128
  * +-------------------+
- * | unused            |   88       936
+ * | unused            |  216       808
  * +-------------------+
  * | current           | 1024       512
  * +-------------------+
  * +-------------------+
  * | block_size        |   0           1
  * | nursery_size      |   1           1
- * | unused            |   2         126
  * +-------------------+
- * | description       | 128         128
- * +-------------------+
- * | unused            | 256         256
+ * | unused            |   2         510
  * +-------------------+
  */
 
@@ -45,7 +44,8 @@ struct cq_super default_super = {
     .sb_serial = 0,
     .sb_hash = {0,},
     .version = COQUET_VERSION,
-    .from_b = 1
+    .from_b = 1,
+    .desc = {0,},
 };
 
 /* Blindly do a extraction. Note that we may not have any verification
@@ -67,10 +67,11 @@ static int extract_half(uint8_t *data, struct cq_super *super) {
         return 0;
     }
     super->version = be_decode(data+8,8);
-    /* (all versions are fine for now). */
+    /* (all versions are fine for now). TODO */
 
     memcpy(super->global_iv,data+16,GLOBAL_IV_LEN);
     memset(super->sb_hash,0,HASH_LEN);
+    memcpy(super->desc,data+88,DESC_LEN);
     super->sb_serial = be_decode(data+80,8);
     
     extract_config(data+1024,&(super->current));
@@ -95,6 +96,7 @@ static int super_init(coquet_t *cq, struct cq_super *super) {
 
     r = (cq->vfs_funcs.random)(cq->vfs_data,
             super->global_iv, GLOBAL_IV_LEN);
+    memset(super->desc,0,DESC_LEN);
     if(r != COQUET_RET_OK) {
         return r;
     }
@@ -167,6 +169,7 @@ static void intract(uint8_t *data, struct cq_super *super, uint64_t serial) {
     be_encode(data+8,COQUET_VERSION,8);
     memcpy(data+16,super->global_iv,GLOBAL_IV_LEN);
     be_encode(data+80,serial,8);
+    memcpy(data+88,super->desc,DESC_LEN);
     
     intract_config(data+1024,&(super->current));
     intract_config(data+1536,&(super->desired));
@@ -223,6 +226,20 @@ int cq_super_save(coquet_t *cq, struct cq_super *super, bool_t wait) {
     return ret;
 }
 
+void cq_super_set_desc(struct cq_super *super, char *desc) {
+    memset(super->desc,0,DESC_LEN);
+    strncpy_ok(super->desc,desc,DESC_LEN);
+}
+
+char * cq_super_get_desc(struct cq_super *super) {
+    char *out;
+
+    out = malloc(DESC_LEN+1);
+    memcpy(out,super->desc,DESC_LEN);
+    out[DESC_LEN] = '\0';
+    return out;
+}
+
 #ifdef COQUET_TEST
 
 #include <stdio.h>
@@ -231,6 +248,7 @@ void test_superblock() {
     coquet_t cq;
     int i,r;
     struct cq_super super;
+    char *desc;
 
     printf("testing superblock\n");
 
@@ -252,6 +270,7 @@ void test_superblock() {
     test_bail(&cq,r);
 
     super.desired.nursery_size = 11; /* change something for B */
+    cq_super_set_desc(&super,"test file");
     cq_super_save(&cq,&super,1);
     test_bail(&cq,r);
 
@@ -274,6 +293,11 @@ void test_superblock() {
     test_assert(super.desired.block_size == 12,"dbsize");
     test_assert(super.current.nursery_size == 10,"cnurs");
     test_assert(super.desired.nursery_size == 11,"dnurs");
+    
+    desc = cq_super_get_desc(&super);
+    test_assert(!strcmp(desc,"test file"),"desc");
+    free(desc);
+
     for(i=0;i<GLOBAL_IV_LEN;i++)
         test_assert(super.global_iv[i] == 0xA5,"iv");
 
@@ -291,6 +315,7 @@ creation
 corruption
 a/b choice
 magic
+desc truncation
 
 */
 
index a498c8e1ae6551ab5aae0f27575c84d6981c4340..d9c7acf57ee941c829004e16a894cc20ea5fbc81 100644 (file)
@@ -6,6 +6,7 @@
 #define HALF_BYTES  2048
 #define SUPER_BYTES (HALF_BYTES*2)
 #define GLOBAL_IV_LEN 32
+#define DESC_LEN 128
 #define NUM_LOCKS 32
 
 struct cq_super_config {
@@ -14,10 +15,18 @@ struct cq_super_config {
 };
 
 struct cq_super {
+    /* Access these directly */
     struct cq_super_config current, desired;
     uint64_t version;
-    uint64_t sb_serial;
     uint8_t global_iv[GLOBAL_IV_LEN];
+
+    /* DANGER! Don't access directly, use cq_super_*_desc */
+    char desc[DESC_LEN];
+
+    /* superblock internal stuff, you probably don't need, and certainly
+     * shouldn't alter.
+     */
+    uint64_t sb_serial;
     uint8_t sb_hash[32]; /* HMAC SHA512-256 using global_iv */
     int from_b; /* true if loaded from b, false if from a */
 };
@@ -26,6 +35,8 @@ struct cq_super {
 
 int cq_super_save(coquet_t *cq, struct cq_super *super, bool_t wait);
 int cq_super_load(coquet_t *cq, struct cq_super *super, bool_t create);
+void cq_super_set_desc(struct cq_super *super, char *desc);
+char * cq_super_get_desc(struct cq_super *super);
 
 #ifdef COQUET_TEST
 void test_superblock();
index 9fd0254c3de4c584206e051cee1bebd328486c5e..c3c175a250cb5bba8f8bc97652d317d9ee5e2d75 100644 (file)
@@ -56,3 +56,10 @@ void be_encode(uint8_t *where, uint64_t value, int len) {
         value >>= 8;
     }
 }
+
+char *strncpy_ok(char *restrict dst, const char *restrict src, size_t sz) {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wstringop-truncation"
+    return strncpy(dst,src,sz);
+#pragma GCC diagnostic pop
+}
index 6d2f655511bc9262819a4434b136caedf31e055f..0b804bf7159630ad20817f96409ff6c667486204 100644 (file)
@@ -4,5 +4,6 @@
 char * cq_message(const char *fmt, ...);
 uint64_t be_decode(uint8_t *data,int len);
 void be_encode(uint8_t *where, uint64_t value, int len);
+char *strncpy_ok(char *restrict dst, const char *restrict src, size_t sz);
 
 #endif
\ No newline at end of file
index 669f78d9663a5dfbe10db3ac490b9b82d9ed7984..1e7308d871cfead714d7f30e9a4a31739eaa066e 100644 (file)
Binary files a/testdata/sb1.coquet and b/testdata/sb1.coquet differ