s9e/bencode
Composer 安装命令:
composer require s9e/bencode
包简介
Fast and efficient bencode decoder/encoder, designed to handle malformed and malicious input gracefully.
关键字:
README 文档
README
s9e\Bencode is a clean and efficient Bencode encoder/decoder. It is designed to handle malformed and malicious input gracefully.
Installation
composer require s9e/bencode
Usage
Decode a bencoded string
use s9e\Bencode\Bencode; print_r(Bencode::decode('d3:bar4:spam3:fooi42ee'));
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[bar] => spam
[foo] => 42
)
)
Encode a PHP value
use s9e\Bencode\Bencode; print_r(Bencode::encode(['foo' => 42, 'bar' => 'spam']));
d3:bar4:spam3:fooi42ee
Supported types are as follow:
array,int, andstringvalues are encoded natively.floatvalues that can be losslessly converted to integers are coerced toint.boolvalues are coerced toint.- An object that implements
s9e\Bencode\BencodeSerializableis encoded as the value returned by itsbencodeSerialize()method. - The properties of an
stdClassobject are encoded in a dictionary. - An instance of
ArrayObjectis treated as an array.
use s9e\Bencode\Bencode; use s9e\Bencode\BencodeSerializable; $bencodable = new class implements BencodeSerializable { public function bencodeSerialize(): array|int|string { return 42; } }; print_r(Bencode::encode($bencodable));
i42e
Handle exceptions
try { s9e\Bencode\Bencode::decode('i123x'); } catch (s9e\Bencode\Exceptions\DecodingException $e) { var_dump($e->getMessage(), $e->getOffset()); }
string(29) "Illegal character at offset 4"
int(4)
try { s9e\Bencode\Bencode::encode(2.5); } catch (s9e\Bencode\Exceptions\EncodingException $e) { var_dump($e->getMessage(), $e->getValue()); }
string(17) "Unsupported value"
float(2.5)
Salvage non-compliant input
By default, the decoder rejects non-compliant input with a ComplianceError exception, which is a subtype of DecodingException. If you have to handle input produced by a non-compliant encoder, the decodeNonCompliant method may be able to salvage it by replacing illegal values as follow:
- Unordered dictionaries are automatically sorted.
- Duplicate entries in dictionaries overwrite prior entries.
- Integers used as dictionary keys are converted to strings.
- Leading
0s are removed from integers. - Negative zero is converted to
0. - Trailing junk at the end of the input is ignored.
In the following example, we try to load an invalid dictionary normally and upon failure, we retry using the non-compliant decoder.
use s9e\Bencode\Bencode; $input = 'd3:fooi42e3:bar4:spame'; try { $value = Bencode::decode($input); } catch (s9e\Bencode\Exceptions\ComplianceError $e) { echo 'Failed: ', $e->getMessage(), "\nRetry with non-compliant decoder:\n"; $value = Bencode::decodeNonCompliant($input); print_r($value); }
Failed: Out of order dictionary entry 'bar' at offset 10
Retry with non-compliant decoder:
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[bar] => spam
[foo] => 42
)
)
Implementation details
- Rejects invalid bencoded data with meaningful exception messages.
- Uses ArrayObject instances to represent dictionaries. Dictionaries can be created and read using either the array notation or the object notation.
- Integers are limited in range from
PHP_INT_MINtoPHP_INT_MAX. - The encoder accepts booleans but converts them to integers.
- The encoder accepts floats that are equal to their integer value.
s9e/bencode 适用场景与选型建议
s9e/bencode 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 68 次下载、GitHub Stars 达 10, 最近一次更新时间为 2014 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「bencode」 「bittorrent」 「torrent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 s9e/bencode 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 s9e/bencode 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 s9e/bencode 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A set of components that can be used to interact with torrent files (read+write) and classes that can encode/decode to/from the BitTorrent format.
PHP Library for decoding and encoding BitTorrent BEncoded data
Extract media information from torrent-like filename
Composer version of torrent-rw https://github.com/adriengibrat/torrent-rw
A Bencode serializer and deserializer in pure PHP
An easy way to use bencoding in Laravel.
统计信息
- 总下载量: 68
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 11
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2014-12-22