Bdecode

object Bdecode

Bencode decoder, the port of libtorrent's bdecode() (src/bdecode.cpp). Parses a bencoded buffer into a BdecodeNode tree whose nodes reference back into the original ByteArray (no copies of the payload are made until you ask for them).

Faithful to libtorrent's limits and error semantics: a depth limit (default 100) caps nesting, a token limit (default 2,000,000) caps total nodes, integers are overflow-checked, and a dictionary key must be a string. libtorrent uses an explicit stack to stay iterative; this port uses bounded recursion (≤ depthLimit frames), which is simpler and equivalent for every real input.

Properties

Link copied to clipboard
const val DEFAULT_DEPTH_LIMIT: Int = 100
Link copied to clipboard

Functions

Link copied to clipboard
fun decode(buffer: ByteArray, depthLimit: Int = DEFAULT_DEPTH_LIMIT, tokenLimit: Int = DEFAULT_TOKEN_LIMIT): BdecodeNode

Decode buffer; throws BdecodeException on malformed input.

Link copied to clipboard
fun decodeOrNull(buffer: ByteArray, depthLimit: Int = DEFAULT_DEPTH_LIMIT, tokenLimit: Int = DEFAULT_TOKEN_LIMIT): BdecodeNode?

Decode buffer; returns null instead of throwing on malformed input.