change: decode message, description and timestamp

laumann
Sep 1, 2022, 9:15 PM
Y26WT3ZFN7KSVXOZ26B5Y2OR4M4VQYQLPMAHPC4O5VIT3ENBISXAC

Dependencies

  • [2] WMFNGOYT change: reduce printed noise, rework some code
  • [3] Q7TKZCJP Add initial support for reading the offsets from a (fixed) change
  • [4] 3NA345CN Add zstd_seekable + many headers
  • [5] B3XLVPNC Add ani.c and initial Makefile
  • [6] FB67XX5E add argument parsing setup
  • [7] X36ICMJN Initial import for blake3
  • [8] VKLGQREY change: add base32 decode, initial deconstruction of hashed
  • [9] 3OHR6ZPH make: prettify output
  • [*] PEUS54XQ
  • [*] RIWSVVAS change: decompress the 'contents' with zstd_seekable
  • [*] OBKF6SII change: decompress the hashed section too

Change contents

  • file addition: scaffold.h (----------)
    [11.1]
    #ifndef SCAFFOLD_H
    #define SCAFFOLD_H
    /* Various scaffolding functions around stdlib functions */
    void *xmalloc(size_t size);
    #endif
  • file addition: scaffold.c (----------)
    [11.1]
    #include <stdio.h>
    #include <stdlib.h>
    #include "scaffold.h"
    /**
    * Expand to support varargs like a regular printf
    */
    void die(const char *msg)
    {
    fprintf(stderr, msg);
    exit(57);
    }
    void *
    xmalloc(size_t size)
    {
    void *ret;
    ret = malloc(size);
    if (!ret)
    die("Unable to allocate memory");
    return ret;
    }
  • replacement in change.h at line 39
    [3.173][3.173:209]()
    void *authors; /* something... */
    [3.173]
    [3.209]
    void *authors; /* a "key"="value" collection */
  • edit in change.c at line 10
    [3.1192]
    [12.39]
    #include "scaffold.h"
  • edit in change.c at line 15
    [3.711]
    [13.0]
    #include "bincode.h"
  • edit in change.c at line 165
    [13.1172]
    [3.872]
    uint8_t *p;
  • edit in change.c at line 171
    [2.362]
    [12.2591]
    uint64_t len;
  • replacement in change.c at line 197
    [3.1399][2.792:832]()
    /* TODO Decode into 'struct hashed' */
    [3.1399]
    [3.1399]
    p = buf;
    dump_buf("hashed", p, hashed_len);
    hashed.version = bincode_getu64(p);
    printf("version = %lu\n", hashed.version);
    p += sizeof(uint64_t);
    len = bincode_getu64(p);
    p += sizeof(uint64_t);
    hashed.header.message = bincode_getstr(p, len);
    p += len;
    printf("message = '%s'\n", hashed.header.message);
    /**
    * Description! This is like the commit "body", and is optional, so we
    * have a byte indicating its presence, '0' = None, '1' =
    * Some(description)
    */
    if (*p++) {
    len = bincode_getu64(p);
    p += sizeof(uint64_t);
    hashed.header.description = bincode_getstr(p, len);
    p += len;
    printf("description = '%s'\n", hashed.header.description);
    } else
    printf("description = (none)\n");
    len = bincode_getu64(p);
    p += sizeof(uint64_t);
  • edit in change.c at line 230
    [3.1400]
    [13.1373]
    if (len != 30)
    printf("warning: timestamp field has unexpected length %lu (expected 30)\n", len);
    hashed.header.timestamp = bincode_getstr(p, len);
    p += len;
    printf("timestamp = '%s'\n", hashed.header.timestamp);
  • file addition: bincode.h (----------)
    [11.1]
    #ifndef BINCODE_H
    #define BINCODE_H
    /** Requires: <stdint.h> */
    /**
    * Definitions for the bincode encode/decode routines.
    *
    * Generally, the idea here is that you open a file, get a file descriptor and
    * use the encode/decode routines to get components read from the fd and and
    * decoded as the type you want.
    */
    /**
    * Some notes regarding the bincode format:
    *
    * Enum types: The order of the variants matter, as the encoding indicates the
    * variant with a u32.
    */
    uint16_t bincode_getu16(uint8_t *);
    uint32_t bincode_getu32(uint8_t *);
    uint64_t bincode_getu64(uint8_t *);
    /**
    * Allocate and return a string of the given length.
    *
    * The memory allocated shall be the given length plus one, to hold the
    * terminating zero.
    *
    * Returns the allocated string. The caller is responsible for deallocating.
    */
    char *bincode_getstr(uint8_t *, size_t);
    /* void bincode_putu16(uint8_t *, uint16_t); */
    /* void bincode_putu32(uint8_t *, uint32_t); */
    /* void bincode_putu64(uint8_t *, uint64_t); */
    #endif
  • file addition: bincode.c (----------)
    [11.1]
    #include <stdint.h>
    #include <stddef.h>
    #include <stdlib.h>
    #include <string.h>
    #include "scaffold.h"
    #include "bincode.h"
    uint16_t
    bincode_getu16(uint8_t *b)
    {
    return (uint16_t)b[1] << 8 | (uint16_t)b[0];
    }
    uint32_t
    bincode_getu32(uint8_t *b)
    {
    return (uint32_t)b[3] << 24
    | (uint32_t)b[2] << 16
    | (uint32_t)b[1] << 8
    | (uint32_t)b[0];
    }
    uint64_t
    bincode_getu64(uint8_t *b)
    {
    return (uint64_t)b[7] << 56
    | (uint64_t)b[6] << 48
    | (uint64_t)b[5] << 40
    | (uint64_t)b[4] << 32
    | (uint64_t)b[3] << 24
    | (uint64_t)b[2] << 16
    | (uint64_t)b[1] << 8
    | (uint64_t)b[0];
    }
    char *
    bincode_getstr(uint8_t *b, size_t len)
    {
    char *str;
    str = xmalloc(len + 1);
    memcpy(str, b, len);
    str[len] = '\0';
    return str;
    }
  • edit in Makefile at line 24
    [3.3742]
    [3.53469]
    OBJS += bincode.o
    OBJS += scaffold.o
  • replacement in Makefile at line 33
    [3.634][3.95:118]()
    @printf "LD\t%s\n" $@
    [3.634]
    [3.53606]
    @printf " LD\t%s\n" $@