duanjing2013 2016-11-18 22:19
浏览 76
已采纳

从表示上下文资源的内部结构获取值

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条回答 默认 最新

  • douji9184 2017-01-28 04:30
    关注

    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.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊