-1

can you help me to solve a problem with ZSTD library compression.

The thing is, when I tried to compress less than or equal to 4 bytes, the ZSTD_compress function returned me an exception

but if i change it from 4 bytes to 5 it will be fine

Please advise how to make ZSTD_compress to pack bytes less than 4 bytes and return the compressed size

i used zstd version 1.5.4

here's an example of what i did

#include <iostream>
#include <windows.h>
#include <assert.h>

#include "zstd.h"
#pragma comment(lib, "libzstd_static.lib")

int main() {
   
    const size_t srcSize = 4;
    char src[srcSize] = {0x0 ,0x0 ,0x0 ,0x0 };
    

    size_t dstSize = srcSize + 12 + max(1, (srcSize + 12) / 1000);
    char* dst = new char[dstSize];
    
    size_t const countSize = ZSTD_compress((void*)dst, dstSize, (const void*)src, srcSize, 3);
    if (ZSTD_isError(countSize)) {
        assert(!ZSTD_isError(countSize));
    }
}

I wanted ZSTD_compress to return me 17 bytes if src equals 4 bytes

how do I do this?

Pavel12398
  • 47
  • 6
  • 1
    You should probably just use `dstSize = ZSTD_compressBound(srcSize);` and if you get an error, you should print its value and `ZSTD_getErrorName(rc)` and check what does the error code mean. – dewaffled Mar 04 '23 at 11:35
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 05 '23 at 03:33

1 Answers1

1

If you had looked to see what the error was with ZSTD_getErrorName(countSize), you would get "error Destination buffer is too small". How did you come up with that formula? In any case, you don't need a formula. There's already a function to tell you how big to make the output buffer, ZSTD_compressBound(srcSize). That returns 67 for a source length of 4, much more than the 17 you attempted.

Mark Adler
  • 101,978
  • 13
  • 118
  • 158