Exemplo n.º 1
0
const char *test_zlib_vmbuf() {
    ssize_t data_size;
    for (data_size = DATA_SIZE - 5; data_size < DATA_SIZE + 5; ++data_size) {
        int data[data_size];
        int i;
        for (i = 0; i < data_size; ++i) {
            data[i] = i;
        }
        size_t expected_size = compressBound(sizeof(data));
        uint8_t *expected = malloc(expected_size);
        compress(expected, &expected_size, (uint8_t *)data, sizeof(data));

        struct vmbuf compressed = VMBUF_INITIALIZER;
        vmbuf_init(&compressed, 128);
        if (0 > vmbuf_deflate_ptr(data, sizeof(data), &compressed))
            mu_fail("vmbuf_deflate_ptr() failed");
        // printf("%zu ==> %zu  (%zu)\n", sizeof(data), vmbuf_wlocpos(&compressed), expected_size);
        mu_assert_eqi(expected_size + 12 /* gzip header and trailer */, vmbuf_wlocpos(&compressed));
        struct vmbuf decompressed = VMBUF_INITIALIZER;
        vmbuf_init(&decompressed, 128);
        vmbuf_inflate2(&compressed, &decompressed);
        mu_assert_eqi(vmbuf_wlocpos(&decompressed), sizeof(data));
        typeof(data[0]) *decompressed_buf = vmbuf_mem(&decompressed);
        for (i = 0; i < data_size; ++i)
            mu_assert_eqi_idx(i, data[i], decompressed_buf[i]);
        vmbuf_free(&compressed);
        vmbuf_free(&decompressed);
        free(expected);
    }
    return NULL;
}
Exemplo n.º 2
0
int vmbuf_inflate(struct vmbuf *buf) {
    static struct vmbuf outbuf = VMBUF_INITIALIZER;
    vmbuf_init(&outbuf, vmbuf_ravail(buf));
    if (0 > vmbuf_inflate2(buf, &outbuf))
        return -1;
    vmbuf_swap(&outbuf, buf);
    return 0;
}