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 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图