kripken/emscripten — Issue #124: zlib unable to inflate gzip?
Author: eminence
Created: 2011-11-25 04:54:40+00:00
State: open

I am trying to use zlib to decompress some gzip data. My code when compiled to native code works as expected. But I get a zlib runtime error when I try to run with emscripten. I see that there is a zlib test as part of the test suite (which runs and passes on my machine), but it seems that this only tests zlib data, not gzip data.

Test file:

```c
#include
#include

#include

#define OUTPUT_BUF_SIZE 1024

int main(int argc, char** argv) {
unsigned char output_buffer[1024];

const unsigned char compressed_data[] = {
0x1f, 0x8b, 0x08, 0x00, 0x2e, 0x15, 0xcf, 0x4e, 0x00, 0x03, 0xcb, 0x48,
0xcd, 0xc9, 0xc9, 0xd7, 0x51, 0xc8, 0xce, 0x2c, 0x29, 0xc9, 0x4c, 0x2d,
0xe6, 0x02, 0x00, 0x82, 0xe4, 0x0c, 0x1c, 0x0f, 0x00, 0x00, 0x00};

z_stream strm;

strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 35;
strm.next_in = compressed_data;

int ret = inflateInit2(&strm, 15+32);
if (ret != Z_OK) {
printf("inflateInit2 failed\n");
return 1;
}
printf("inflateInit2 is Z_OK\n");

strm.avail_out = OUTPUT_BUF_SIZE;
strm.next_out = output_buffer;
ret = inflate(&strm, Z_NO_FLUSH);
if (ret != Z_STREAM_END) {
printf("inflate returned an error: %d\n", ret);
printf("Error: %s\n", strm.msg);
return 1;
}
printf("Decompressed the following data:\n");
printf("-->%s<--\n", output_buffer);

inflateEnd(&strm);

return 0;
}
```

When built to native code:

```

$ clang test.c ../zlib-1.2.5/libz.a -o test.exe
test.c:22:18: warning: assigning to 'Bytef *' (aka 'unsigned char *') from 'const unsigned char [35]' discards qualifiers
[-Wincompatible-pointer-types]
strm.next_in = compressed_data;
^ ~~~~~~~~~~~~~~~
1 warning generated.

$ ./test.exe
inflateInit2 is Z_OK
Decompressed the following data:
-->hello, kitties
<--
```

When build with emscripten:

```
$ ./tools/emmaken.py test.c -o test.bc
emmaken.py: ./tools/emmaken.py test.c -o test.bc
Running: /home/achin/llvm_install/bin/clang test.c -o test.bc -m32 -U__i386__ -U__x86_64__ -U__i386 -U__x86_64 -U__SSE__ -U__SSE2__ -U__MMX__ -UX87_DOUBLE_ROUNDING -UHAVE_GCC_ASM_FOR_X87 -DEMSCRIPTEN -U__STRICT_ANSI__ -nostdinc -I/home/achin/devel/emscripten/system/include -I/home/achin/devel/emscripten/system/include/bsd -I/home/achin/devel/emscripten/system/include/libc -I/home/achin/devel/emscripten/system/include/libcxx -I/home/achin/devel/emscripten/system/include/gfx -I/home/achin/devel/emscripten/system/include/net -I/home/achin/devel/emscripten/system/include/SDL -U__APPLE__ -g -emit-llvm -c
test.c:22:18: warning: assigning to 'Bytef *' (aka 'unsigned char *') from 'const unsigned char [35]' discards qualifiers
[-Wincompatible-pointer-types]
strm.next_in = compressed_data;
^ ~~~~~~~~~~~~~~~
1 warning generated.

$ llvm-link test.bc ../zlib-1.2.5/libz.a.bc -o test.exe.bc

$ ./emscripten.py -m test.exe.bc > test.exe.js
clang: warning: treating 'c' input as 'c++' when in C++ mode, this behavior is deprecated

$ ../v8-read-only/d8 test.exe.js
inflateInit2 is Z_OK
inflate returned an error: -3
Error: incorrect data check
```

I can't say for sure this is emscripten's fault (I may be messing up the build), but I'm at a loss to explain this. Any thoughts?

Emscripten version is: 753437a56d63666d0761b66ead1b0d491c4bcb2b
clang/llvm version is: clang version 3.1 (trunk 145096)
V8 version is SVN 10046
Platform is 32-bit Linux