0

I want to parse a Git packfile in PHP with zlib. (Details on the format here and here). The problem, as mentioned in those articles, is that there's no length specified in the packfile for the compressed data, meaning you have to throw it into zlib and use zlib to figure out where data starts and stops. While zlib's native C API provides the Z_STREAM_END constant which is returned when the end of the compressed data is reached, PHP's zlib extension offers no such option. My only option seems to be to add one byte to the input stream at a time and check whether total_in (part of the zlib stream struct) is 0, which, according to the PHP and zlib source code, is reset to zero whenever inflate() returns Z_STREAM_END. EDIT: PHP resets the total_in value, not zlib. This is changed in the pull request linked in my answer below.

The problem is, how do I get the value of total_in? PHP's inflate_init() function returns a "context resource" which from my perusal of the source code represents the struct zlib uses for decompressing.

Is it possible, in pure PHP, to get the the value of total_in from the internal struct using the context resource?

Thanks.

EDIT: Basically, I want to be able to use stream_context_get_options() on the zlib.inflate context, but I can't because I get Invalid stream/context parameter.

1 Answer 1

0

After some research, here are the results (TL;DR: it's not possible without a PHP patch.):

  • First, resources are not just structs. They can represent any C variable registered with the Zend Engine. Some properties might be exposed to userspace (PHP code) with things like stream_context_get_params(). However this is separate from the actual content that the resource represents.
    • The values resources represent are not directly available to userspace.
    • I've added some functions to expose the information I wanted. They are waiting in this php-src pull request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.