ccompressioninflate

how to use zpipe inflate on a binary unsigned char


I have a compressed and base64 encoded string that I want to decompress zpipe.

I did the tutorial here and it worked great. I b64 decoded the string first, saved it to a file and then used the inf() function to decompress it.

int ret;
char *b64_string = (char *)read_b64_string();
size_t my_string_len;
unsigned char *my_string = b64_decode_ex(my_string, strlen(my_string), &my_string_len);
free(b64_string);

write_decoded_b64_to_file(my_string, my_string_len);
free(my_string);

ret = inf();

and then I changed the inf() function to hardcoded files:

int inf()
{
    FILE *source;
    FILE *dest;
    source = fopen("/path/to/my/b64decoded_file/", "r");
    dest = fopen("/path/to/my/decompressed_file/", "w");

Now I want to change the inf() function to make it work when the binary is passed as an argument.

int ret;
size_t my_string_len;
unsigned char *my_string = b64_decode_ex(my_string, strlen(my_string), &my_string_len);
ret = inf(my_string);

I think I identified this line

strm.avail_in = fread(in, 1, CHUNK, source);

as the one where I have to read in the binary. fread is only for files though. How can I read this binary file in without a file?


Solution

  • Just use fmemopen() to open my_string as a file.

    source = fmemopen(my_string, my_string_len, "rb");
    

    (I put in the b in "rb" by habit. Never hurts. Can help.)