Common Lisp Package: BENCODE

README:

FUNCTION

Private

DICTIONARY->ALIST (DICTIONARY)

Returns an alist representation of the dictionary.

MAKE-DICTIONARY (LIST)

Makes a dictionary from a plist or alist. Keys must be strings.

MAKE-FLEXI-STREAM (STREAM &REST ARGS &KEY (EXTERNAL-FORMAT (MAKE-EXTERNAL-FORMAT ISO-8859-1)) ELEMENT-TYPE COLUMN POSITION BOUND)

Creates and returns a new flexi stream. STREAM must be an open binary or `bivalent' stream, i.e. it must be capable of reading/writing octets with READ-SEQUENCE and/or WRITE-SEQUENCE. The resulting flexi stream is an input stream if and only if STREAM is an input stream. Likewise, it's an output stream if and only if STREAM is an output stream. The default for ELEMENT-TYPE is LW:SIMPLE-CHAR on LispWorks and CHARACTER on other Lisps. EXTERNAL-FORMAT must be an EXTERNAL-FORMAT object or a symbol or a list denoting such an object. COLUMN is the initial column of the stream which is either a non-negative integer or NIL. The COLUMN argument must only be used for output streams. POSITION (only used for input streams) should be an integer and it denotes the position the stream is in - it will be increased by one for each octet read. BOUND (only used for input streams) should be NIL or an integer. If BOUND is not NIL and POSITION has gone beyond BOUND, then the stream will behave as if no more input is available.

OCTET-LENGTH (STRING &KEY (EXTERNAL-FORMAT LATIN1) (START 0) (END (LENGTH STRING)))

Returns the length of the substring of STRING from START to END in octets if encoded using the external format EXTERNAL-FORMAT. In spite of the name, STRING can be any sequence of characters, but the function is optimized for strings.

OCTETS-TO-STRING (SEQUENCE &KEY (EXTERNAL-FORMAT LATIN1) (START 0) (END (LENGTH SEQUENCE)))

Converts the Lisp sequence SEQUENCE of octets from START to END to a string using the external format designated by EXTERNAL-FORMAT. This function is optimized for the case of SEQUENCE being a vector. Don't use lists if you're in a hurry.

STRING-TO-OCTETS (STRING &KEY (EXTERNAL-FORMAT LATIN1) (START 0) (END (LENGTH STRING)))

Converts the Lisp string STRING from START to END to an array of octets corresponding to the external format designated by EXTERNAL-FORMAT. In spite of the name, STRING can be any sequence of characters, but the function is optimized for strings.

Undocumented

BINARY-DICTIONARY-KEY-P (KEY)

BINARY-SUB-KEYS (KEY)

CHAR-INTEGER-P (CHAR)

DECODE-BINARY-STRING (STREAM)

DECODE-DICTIONARY (STREAM)

DECODE-INTEGER (STREAM)

DECODE-LIST (STREAM)

DECODE-STRING (STREAM)

GET-DICTIONARY (KEY DICTIONARY)

MAYBE-READ-CHAR (STREAM CHAR)

MUST-READ-CHAR (STREAM CHAR)

MUST-READ-OCTETS (STREAM LENGTH)

READ-EXTERNAL-FORMAT

READ-INTEGERS (STREAM)

RENDER-INTEGER (INTEGER)

STRING-HEADER (LENGTH)

MACRO

Private

WITH-INPUT-FROM-SEQUENCE ((VAR SEQUENCE &KEY START END TRANSFORMER) &BODY BODY)

Creates an IN-MEMORY input stream from SEQUENCE using the parameters START and END, binds VAR to this stream and then executes the code in BODY. A function TRANSFORMER may optionally be specified to transform the returned octets. The stream is automatically closed on exit from WITH-INPUT-FROM-SEQUENCE, no matter whether the exit is normal or abnormal. The return value of this macro is the return value of BODY.

WITH-OUTPUT-TO-SEQUENCE ((VAR &KEY AS-LIST (ELEMENT-TYPE ''OCTET) TRANSFORMER) &BODY BODY)

Creates an IN-MEMORY output stream, binds VAR to this stream and then executes the code in BODY. The stream stores data of type ELEMENT-TYPE (a subtype of OCTET) which is (optionally) transformed by the function TRANSFORMER prior to storage. The stream is automatically closed on exit from WITH-OUTPUT-TO-SEQUENCE, no matter whether the exit is normal or abnormal. The return value of this macro is a vector (or a list if AS-LIST is true) containing the octets that were sent to the stream within BODY.

Undocumented

RESTART-CASE-LOOP (FORM &BODY CLAUSES)

GENERIC-FUNCTION

Public

DECODE (INPUT &KEY EXTERNAL-FORMAT (EXTERNAL-FORMAT UTF-8) &ALLOW-OTHER-KEYS)

Decode a bencode object from a stream or sequence. If input is a flexi-stream, its external-format will be used when decoding strings. If input is a string, all characters must have char-codes that fit in an (unsigned-byte 8). Otherwise, the value of the external-format parameter is used to create a flexi-stream for decoding. The default is UTF-8.

ENCODE (OBJECT STREAM &KEY EXTERNAL-FORMAT (EXTERNAL-FORMAT UTF-8) &ALLOW-OTHER-KEYS)

Encode object and write it to stream or, if stream is nil, use an in-memory stream and return the resulting sequence. The external-format is used when encoding strings. UTF-8 is the default.

Private

Undocumented

ACTUAL-OCTET (CONDITION)

EXPECTED-OCTET (CONDITION)

KEY (CONDITION)

OCTET (CONDITION)

PEEK-BYTE (FLEXI-INPUT-STREAM &OPTIONAL PEEK-TYPE (EOF-ERROR-P) EOF-VALUE)

SLOT-ACCESSOR

Private

FLEXI-STREAM-EXTERNAL-FORMAT (OBJECT)

The encoding currently used by this stream. Can be changed on the fly.

SETFFLEXI-STREAM-EXTERNAL-FORMAT (NEW-VALUE OBJECT)

Converts the new value to an EXTERNAL-FORMAT object if necessary.

VARIABLE

Public

*BINARY-DICTIONARY-KEYS*

A list of strings and dotted lists of strings. Strings denote dictionary keys whose values will not be decoded, returning a byte vector rather than a string. Dotted lists are used to specify nested dictionaries. When decoding the value of a key matching the car of a dotted list, the cdr will be passed on as a new binding of *binary-dictionary-keys*. Decoding a list will pass on the cdr of lists with a car of nil.

Private

Undocumented

*ASCII*

CLASS

Private

FLEXI-STREAM

A FLEXI-STREAM object is a stream that's `layered' atop an existing binary/bivalent stream in order to allow for multi-octet external formats. FLEXI-STREAM itself is a mixin and should not be instantiated.

CONDITION

Private

Undocumented

INVALID-VALUE-TYPE

NONSTRING-DICTIONARY-KEY

UNEXPECTED-OCTET