change: decompress the hashed section too

laumann
Aug 22, 2022, 8:54 PM
OBKF6SIIFFHHY7YWKPPX75N23NAVUD5662DKIHXSSIDPKKXQ5ZDQC

Dependencies

  • [2] KDJUAAAL change: prefix function names with change_
  • [3] Q7TKZCJP Add initial support for reading the offsets from a (fixed) change
  • [4] QEFCNNVC change: display offsets for given change file
  • [5] AEMTSEJX change: simplify readout of offsets
  • [6] RIWSVVAS change: decompress the 'contents' with zstd_seekable

Change contents

  • edit in change.c at line 12
    [3.1212]
    [3.1488]
    void
    dump_buf(const char *name, uint8_t *buf, size_t len)
    {
    int i;
  • edit in change.c at line 18
    [3.1489]
    [3.1489]
    if (len == 0) {
    printf("%s = []\n", name);
    return;
    }
    printf("%s = [%02x", name, buf[0]);
    for (i = 1; i < len; i++)
    printf(", %02x", buf[i]);
    printf("]\n");
    }
  • edit in change.c at line 59
    [3.2195][3.67:303]()
    }
    void
    dump_buf(const char *name, uint8_t *buf, size_t len)
    {
    int i;
    if (len == 0) {
    printf("%s = []\n", name);
    return;
    }
    printf("%s = [%u", name, buf[0]);
    for (i = 1; i < len; i++)
    printf(", %u", buf[i]);
    printf("]\n");
  • replacement in change.c at line 62
    [3.4][3.304:608]()
    * Read the contents section of the open change file, decompress it and return
    * the raw bytes.
    *
    * Returns a malloc()'ed byte buffer that the caller is responsible for
    * deallocating. Returns NULL if the full contents could not be read, or the
    * decompression failed, or some other error occurred.
    [3.4]
    [3.608]
    * Read in an expected amount of bytes (or die failing), and decompress with
    * zstd-seekable. Return a malloc()'ed byte buffer that the caller is in charge
    * of free()'ing.
  • replacement in change.c at line 66
    [3.612][3.612:622](),[3.622][2.52:102]()
    uint8_t *
    change_read_contents(int fd, struct offsets *off)
    [3.612]
    [3.665]
    static uint8_t *
    decompress_segment(
    int fd,
    size_t compressed_len,
    uint64_t expected_len
    )
  • edit in change.c at line 73
    [3.667][3.667:813]()
    /**
    * Allocate two buffers: one for compressed data, and one for
    * decompressed data. The decompressed data buffer is the return value.
    */
  • edit in change.c at line 75
    [3.850][3.850:878]()
    size_t compressed_to_read;
  • replacement in change.c at line 79
    [3.941][3.941:1129]()
    compressed_to_read = off->total - off->contents_off;
    printf("expecting to read %lu bytes (compressed)\n", compressed_to_read);
    compressed = calloc(sizeof(uint8_t), compressed_to_read);
    [3.941]
    [3.1129]
    printf("expecting to read %lu bytes (compressed)\n", compressed_len);
    compressed = calloc(sizeof(uint8_t), compressed_len);
  • replacement in change.c at line 85
    [3.1201][3.1201:1252]()
    buf = calloc(sizeof(uint8_t), off->contents_len);
    [3.1201]
    [3.1252]
    buf = calloc(sizeof(uint8_t), expected_len);
  • replacement in change.c at line 92
    [3.1344][3.1344:1451]()
    compressed_read = read(fd, compressed, compressed_to_read);
    if (compressed_read != compressed_to_read) {
    [3.1344]
    [3.1451]
    compressed_read = read(fd, compressed, compressed_len);
    if (compressed_read != compressed_len) {
  • replacement in change.c at line 95
    [3.1517][3.1517:1559]()
    compressed_to_read, compressed_read);
    [3.1517]
    [3.1559]
    compressed_len, compressed_read);
  • replacement in change.c at line 106
    [3.1752][3.1752:1822]()
    result = ZSTD_seekable_initBuff(zs, compressed, compressed_to_read);
    [3.1752]
    [3.1822]
    result = ZSTD_seekable_initBuff(zs, compressed, compressed_len);
  • replacement in change.c at line 113
    [3.1959][3.1959:2026]()
    result = ZSTD_seekable_decompress(zs, buf, off->contents_len, 0);
    [3.1959]
    [3.2026]
    result = ZSTD_seekable_decompress(zs, buf, expected_len, 0);
  • replacement in change.c at line 120
    [3.2213][3.2213:2330]()
    if (result != off->contents_len) {
    fprintf(stderr, "decoded %lu bytes, wanted %lu\n", result, off->contents_len);
    [3.2213]
    [3.2330]
    if (result != expected_len) {
    fprintf(stderr, "decoded %lu bytes, wanted %lu\n", result, expected_len);
  • edit in change.c at line 136
    [3.2591]
    [3.2591]
    }
    /**
    *
    */
    int
    change_decode_hashed(int fd, size_t comp_hashed_len, size_t hashed_len)
    {
    uint8_t *buf;
  • edit in change.c at line 146
    [3.2592]
    [3.2592]
    buf = decompress_segment(fd, comp_hashed_len, hashed_len);
    if (buf == NULL) {
    fprintf(stderr, "error: failed to decompress hashed segment\n");
    return 1;
    }
    dump_buf("hashed", buf, hashed_len);
    free(buf);
    return 0;
    }
    /**
    * Read the contents section of the open change file, decompress it and return
    * the raw bytes.
    *
    * Returns a malloc()'ed byte buffer that the caller is responsible for
    * deallocating. Returns NULL if the full contents could not be read, or the
    * decompression failed, or some other error occurred.
    */
    uint8_t *
    change_read_contents(int fd, struct offsets *off)
    {
    return decompress_segment(fd, off->total - off->contents_off, off->contents_len);
  • edit in change.c at line 205
    [3.2572]
    [3.2572]
    goto out;
    }
    err = change_decode_hashed(fd, off.unhashed_off - OFFSETS_SIZE, off.hashed_len);
    if (err != 0) {
    printf("error: failed to decode hashed\n");
  • edit in change.c at line 217
    [3.2730][3.2730:2778]()
    *
    * seek to off.contents_off - OFFSETS_SIZE