Documentation
¶
Index ¶
- Constants
- func Decode(reader io.Reader) (data any, err error)
- func DecodeWithOptions(reader io.Reader, opts Options) (data any, err error)
- func Marshal(w io.Writer, val any) error
- func Unmarshal(r io.Reader, val any) (err error)
- func UnmarshalWithOptions(r io.Reader, val any, opts Options) error
- type Decoder
- func (d *Decoder) Buffered() io.Reader
- func (d *Decoder) Decode() (data any, err error)
- func (d *Decoder) SetMaxDepth(depth int) *Decoder
- func (d *Decoder) SetMaxElements(elements int64) *Decoder
- func (d *Decoder) SetMaxStringLength(limit int64) *Decoder
- func (d *Decoder) SetStrict(strict bool) *Decoder
- func (d *Decoder) Unmarshal(val any) error
- type MarshalError
- type Options
- type RawMessage
- type Readerdeprecated
Constants ¶
const ( // DefaultMaxStringLength is the default maximum string length (64 MiB). DefaultMaxStringLength int64 = 64 * 1024 * 1024 // DefaultMaxDepth is the default maximum nesting depth for lists and dictionaries. DefaultMaxDepth int = 100 // DefaultMaxElements is the default maximum number of bencode values parsed. DefaultMaxElements int64 = 3_000_000 )
Default limits for decoding bencode data.
Variables ¶
This section is empty.
Functions ¶
func Decode ¶
Decode parses the stream r and returns the generic bencode object representation. The object representation is a tree of Go data types: string, int64, uint64, []any, or map[string]any.
func DecodeWithOptions ¶ added in v1.1.0
DecodeWithOptions parses the stream r with custom Options and returns the generic bencode representation.
func Marshal ¶
Marshal writes the bencode encoding of val to w.
Marshal traverses the value v recursively.
Marshal uses the following type-dependent encodings:
Floating point, integer, and Number values encode as bencode numbers.
String values encode as bencode strings.
Array and slice values encode as bencode arrays.
Struct values encode as bencode maps. Each exported struct field becomes a member of the object. The object's default key string is the struct field name but can be specified in the struct field's tag value. The text of the struct field's tag value is the key name. Examples:
// Field appears in bencode as key "Field". Field int // Field appears in bencode as key "myName". Field int "myName"
Anonymous struct fields are ignored.
Map values encode as bencode objects. The map's key type must be string; the object keys are used directly as map keys.
Boolean, Pointer, Interface, Channel, complex, and function values cannot be encoded in bencode. Attempting to encode such a value causes Marshal to return a MarshalError.
Bencode cannot represent cyclic data structures and Marshal does not handle them. Passing cyclic structures to Marshal will result in an infinite recursion.
Types ¶
type Decoder ¶ added in v1.1.0
type Decoder struct {
// contains filtered or unexported fields
}
Decoder reads and decodes bencode values from an input stream.
func NewDecoder ¶ added in v1.1.0
NewDecoder returns a new Decoder reading from r with DefaultOptions.
func NewDecoderWithOptions ¶ added in v1.1.0
NewDecoderWithOptions returns a new Decoder reading from r with the given Options.
func (*Decoder) Buffered ¶ added in v1.2.0
Buffered returns a reader of the data remaining in the Decoder's buffer. The reader is valid until the next call to Decode or Unmarshal.
func (*Decoder) Decode ¶ added in v1.1.0
Decode reads the next bencode value from the stream and returns its generic Go representation.
func (*Decoder) SetMaxDepth ¶ added in v1.1.0
SetMaxDepth sets the maximum nesting depth and returns the Decoder.
func (*Decoder) SetMaxElements ¶ added in v1.1.0
SetMaxElements sets the maximum total elements limit and returns the Decoder.
func (*Decoder) SetMaxStringLength ¶ added in v1.1.0
SetMaxStringLength sets the maximum string length in bytes and returns the Decoder.
type MarshalError ¶
func (*MarshalError) Error ¶
func (e *MarshalError) Error() string
type Options ¶ added in v1.1.0
type Options struct {
// MaxStringLength limits the maximum length in bytes of any bencode string.
// Default is DefaultMaxStringLength (64 MiB). Set to -1 for unlimited.
MaxStringLength int64
// MaxDepth limits the maximum nesting depth for nested lists and dictionaries.
// Default is DefaultMaxDepth (100). Set to -1 for unlimited.
MaxDepth int
// MaxElements limits the total number of bencode values (integers, strings, lists, dicts) parsed.
// Default is DefaultMaxElements (3,000,000). Set to -1 for unlimited.
MaxElements int64
// Strict enables strict adherence to the BitTorrent BEP 3 bencode specification:
// - Rejects integer representations with leading zeros (e.g. "i03e" is rejected; "i0e" is allowed).
// - Rejects negative zero ("i-0e").
// - Rejects explicit positive sign ("i+5e").
// - Rejects non-integer numbers (such as floats or exponential notation).
// - Enforces lexicographically sorted dictionary keys without duplicates.
// - Rejects string lengths with leading zeros (e.g. "03:abc").
Strict bool
}
Options specifies configuration and resource limits for decoding bencode data.
func DefaultOptions ¶ added in v1.1.0
func DefaultOptions() Options
DefaultOptions returns a new Options struct initialized with standard limits.
type RawMessage ¶ added in v1.2.0
type RawMessage []byte
RawMessage is a raw encoded bencode value. It can be used to delay bencode decoding or to access the original bytes of a bencoded value (such as the "info" dictionary in a BitTorrent metainfo file).
type Reader
deprecated
type Reader interface {
io.Reader
io.ByteScanner
}
Deprecated: This type is currently unused. It is exposed for backwards compatability. The public API that previously used this type,
Unmarshal(r Reader, val any) (err error)
is now
Unmarshal(r io.Reader, val any) (err error)
Which is compatible, since any Reader is also an io.Reader. Clients should drop their use of this type. It may be removed in the future.