Download the PHP package s9e/bencode without Composer
On this page you can find all versions of the php package s9e/bencode. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package bencode
Short Description Fast and efficient bencode decoder/encoder, designed to handle malformed and malicious input gracefully.
License MIT
Homepage https://github.com/s9e/Bencode/
Informations about the package bencode
s9e\Bencode is a clean and efficient Bencode encoder/decoder. It is designed to handle malformed and malicious input gracefully.
Installation
Usage
Decode a bencoded string
Encode a PHP value
Supported types are as follow:
array
,int
, andstring
values are encoded natively.float
values that can be losslessly converted to integers are coerced toint
.bool
values are coerced toint
.- An object that implements
s9e\Bencode\BencodeSerializable
is encoded as the value returned by itsbencodeSerialize()
method. - The properties of an
stdClass
object are encoded in a dictionary. - An instance of
ArrayObject
is treated as an array.
Handle exceptions
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
0
s 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.
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_MIN
toPHP_INT_MAX
. - The encoder accepts booleans but converts them to integers.
- The encoder accepts floats that are equal to their integer value.