2

So I have been trying to convert some data from C to be compressed and sent to a library which is in golang where it will be decompressed. only issue is, they seem to be slightly different implementations from each other.

What i did was to use "zlib.h" library from C to compress the string "hello" which resulted in bytes of

[120 156 203 72 205 201 201 103 32 5]

while in golang for the same string "hello" results in an array of

[120 156 202 72 205 201 201 7 4 0 0 255 255 6 44 2 21]

My question is if there is a way to make these outputs similar , what is it? or at the very least can a compressed data in C be decompressed in zlib?

I've also looked at How can I use zlib in golang to cooperate with zlib in c? but i'm looking for something more specific like an example.

dante2702
  • 25
  • 6
  • Isn't the answer in the question you linked to ? i.e. no you can't guarentee that the compressed data is similar - there's differences in the implementation details (though the data compressed in one of them can still be decompressed with the other) – nos Oct 24 '18 at 23:08
  • the decompression in golang for something compressed in C doesn't seem to work, it exits saying it encountered an EOF – dante2702 Oct 24 '18 at 23:10
  • the pigz command can't decompress your first array without an error, neither can gzip if I prepend a gzip header, there is something wrong with the output from your C program. (Note, some programs will decompress it, e.g. `openssl zlib` or the `zlib-flate` command, but they have a rather loose handling of errors from zlib). – nos Oct 24 '18 at 23:43
  • This is a duplicate of [Compressed output differs from Go to Ruby Implementation](https://stackoverflow.com/questions/52767214/compressed-output-differs-from-go-to-ruby-implementation) - ruby uses the same zlib library you use in C. – Steffen Ullrich Oct 25 '18 at 04:31

1 Answers1

3

Comparing compressed data tells you nothing. Different compressors, or different versions of the same compressor, or the same version used with different settings, can all give different compressed output for the same input. What actually matters for a lossless compressor is whether you can decompress to the original data.

The problem with your first example is that it is not complete. (The second example is complete and correct.) The first example ends in the middle of a deflate block. There is an error in your usage of zlib, either in managing the resulting data or not properly requesting the completion of the compression.

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