0

I am integrating flashzlib into one of my flash project. As mentioned in documentation of project. I have successfully generated libz.a and z.l.bc and pushed libs and headers in appropriate places.

Now, I have written this small wrapper https://gist.github.com/65d3c7ff683b326ecd22. This compiles just fine using:

gcc example1_as3.c -lz -O3 -Wall -swc -o example.swc

This is included into flash project like this (uncompressedData is a byteArray):

private var loader:CLibInit = new CLibInit;
private var lib:Object = loader.init();
logger.info("B : " + lib.returnString(uncompressedData, uncompressedData.length) + "");

But flash file fails at this last line. Am not sure what am i missing. z.l.bc file is 340KB while example.swc is just 80KB.

Nakul
  • 1,574
  • 11
  • 13
  • it logs nothing and no further code is executed. It just gets stuck here. – Nakul Nov 08 '11 at 22:54
  • Quickly trying it myself, the runtime error I'm getting on `example.c:50` is `Undefined sym: _inflateInit_`. – Gunslinger47 Nov 09 '11 at 06:57
  • @The_asMan no, just logger (logging.ILogger) – Nakul Nov 09 '11 at 11:02
  • @Gunslinger47 how do you run it to see this error? I am running flash inside browser which doesn't throw anything at me :( – Nakul Nov 09 '11 at 11:03
  • @Gunslinger47 also any hints how to fix this error? (may be some compile options?) – Nakul Nov 09 '11 at 11:04
  • @Nakul Your example code works for me-- that is, it returns zero at the `// FAILS HERE` line. Does `logger.info("hello world")` show anything? An ILogger doesn't send its output anywhere in particular unless it's told to, for example via [TraceTarget](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/logging/targets/TraceTarget.html) – paleozogt Nov 09 '11 at 19:26
  • @Gunslinger47 you'll get that runtime error if you don't have z.l.bc 'installed' in ALCHEMY_HOME/usr/local/lib. Alternatively you can use -L in the gcc command. – paleozogt Nov 09 '11 at 19:28
  • I'm using Adobe Flash Builder's debugger. To run your SWF directly without the need for a browser, you can go to your project properties under "ActionScript Compiler", then uncheck "Generate HTML wrapper file". – Gunslinger47 Nov 10 '11 at 03:12
  • @paleozogt yes logger works just fine. i do get "hello world" :( but now am getting same error as Gunslinger47: "Undefined sym: _inflateInit_" though i compiled as you suggested :( – Nakul Nov 10 '11 at 09:38
  • @paleozogt I am getting this error when running flash file and not while compiling :( what is size of your example.swc? – Nakul Nov 10 '11 at 09:45
  • adding "-v" option to gcc command above, exits with error "ld: unknown option: -emit-llvm" – Nakul Nov 10 '11 at 10:05
  • Why do you need zlib in Flash? What are you using it for? – Cameron Nov 11 '11 at 05:23
  • @Cameron I am using it for speeding up inflate on ByteArray – Nakul Nov 11 '11 at 07:09
  • @Nakul: Cool, but I thought that inflate was already pretty darn fast (especially compared to deflation)? – Cameron Nov 11 '11 at 08:13
  • @Cameraon inflate means decompressing in actionscript, do you have some numbers? In my application I found decompression a culprit. – Nakul Nov 11 '11 at 08:46
  • @Nakul: No numbers (so if you say it's the bottleneck I believe you), but generally it's compression that's slow, and decompression is fairly fast. LZ77-based compression (like zlib) is extremely fast to decompress in general (of course, there's also the Huffman coding which may be a little slower). The decompression algorithm is fairly straightforward; I'm a little surprised you can beat it with AVM2-based code (especially since I suspect Adobe uses zlib internally as a (compiled) C library). – Cameron Nov 12 '11 at 04:30
  • @Cameron thanks for the info. Yesterday only, I found that Adobe inflate also uses native C code internally :(. Actually, I was comparing to java version of code which takes 1/4th the time in decompressing as compared to actionscript. – Nakul Nov 12 '11 at 06:40

1 Answers1

1

­It's failing for one of two reasons. Either Alchemy isn't finding z.l.bc (and isn't bothering to tell you about it), or you're not catching and reporting your error codes correctly.

As I mentioned in the question comments, I was getting a runtime error on the inflateInit invocation. For my case, it turned out that Alchemy wasn't searching $ALCHEMY_HOME/usr/local/lib for z.l.bc like it was supposed to be. This was solved by moving it to /usr/local/lib instead. After that, your code returned Z_OK as expected.

I notice you have all the CHECK_ERR calls commented out, which means that you'll enter an infinite loop if you comment out the early return on example.c:52 since you're not checking for errors on your inflate call in the following unconditional for block. For me, inflate was returning Z_DATA_ERROR since I was just handing it some UTF bytes for test purposes.

Speaking of testing, I notice that there's a small test suite supplied by flashzlib:

I borrowed this code, renamed main to test_all, commented out the call to test_gzio¹, and replaced all printf calls with fprintf calls to stderr instead². This is the result:

zlib version 1.2.3 = 0x1230, compile flags = 0x2000095

uncompress(): hello, hello!

inflate(): hello, hello!

large_inflate(): OK

after inflateSync(): hello, hello!

inflate with dictionary: hello, hello! 

0

Everything appears to be in order.


footnotes:
¹ No file access in Alchemy, of course.
² stdout is disconnected for me, but stderr appears in my trace console

Gunslinger47
  • 7,001
  • 2
  • 21
  • 29
  • thanks @Gunslinger47. the flashzlib was working totally cool for me too. I tried pushing z.l.bc in /usr/local/lib but thats not working for me :(. will dig more and get back. thanks again! – Nakul Nov 11 '11 at 08:47
  • ok, so am able to integrate it with command from Makefile i.e. gcc -O3 -DNO_vsnprintf -DUSE_MMAP -swc -o example.swc example1_as3.o -L. libz.a Making it work now by passing right values. Thanks! – Nakul Nov 11 '11 at 13:27