Text Protocol Lines

pcapkit.utilities.lines reads and writes what the line-based text protocols exchange: CRLF-terminated lines, a reply that runs over several lines, a block of dot-stuffed lines ended by a line holding a single period, and the text a line carries. FTP and SMTP share it, so it lives beside neither (Choosing Between utilities and corekit).

Added in version 1.10.0.

pcapkit.utilities.lines.split_lines(data)[source]

Cut data into its CRLF-terminated lines.

Only a CRLF ends a line, so a CR or LF outside one stays in the line holding it (RFC 5321 Section 2.3.8), for the caller to accept or refuse.

Parameters:

data (bytes) – Octets to cut.

Return type:

tuple[bytes, ...] | None

Returns:

Each line without its CRLF, or None unless data is one or more whole lines, i.e. ends with CRLF.

pcapkit.utilities.lines.join_lines(lines)[source]

Write each of lines followed by its CRLF, as split_lines() reads them.

Parameters:

lines (Iterable[bytes]) – Lines without their CRLF.

Return type:

bytes

Returns:

The lines, each terminated with CRLF.

pcapkit.utilities.lines.closes_reply(code, lines, *, marked)[source]

Whether lines end a multi-line reply that opened with code.

A multi-line reply opens with code and a hyphen, and ends at the first line opening with code and SP (RFC 959 Section 4.2). With marked, as in SMTP, every line before that one opens with code and a hyphen too, and the closing line may be code alone, which a client must accept (RFC 5321 Section 4.2.1). Without it, as in FTP, those lines are free text, which may open with code and a hyphen as well.

Parameters:
  • code (bytes) – Reply code of the opening line.

  • lines (Sequence[bytes]) – Lines after the opening one, CRLF excluded.

  • marked (bool) – Whether every line opens with code.

Return type:

bool

Returns:

Whether each line is bytes holding no LF, and the last line, and only the last, closes the reply.

pcapkit.utilities.lines.block_end(lines)[source]

Index of the line that ends a block of lines.

A block ends at a line holding a single period, as SMTP’s mail data does (RFC 5321 Section 4.1.1.4), and a line of the block opening with a period is sent with one more (stuff()), so none holds one alone.

Parameters:

lines (Sequence[bytes]) – Lines without their CRLF.

Return type:

int | None

Returns:

Index of the first line holding a single period, or None if no line does.

pcapkit.utilities.lines.stuff(line)[source]

Dot-stuff line, a line of a block, for sending.

A line opening with a period is sent with one more (RFC 5321 Section 4.5.2).

Parameters:

line (bytes) – Line of text, CRLF excluded.

Return type:

bytes

Returns:

The line as sent.

pcapkit.utilities.lines.unstuff(line)[source]

Undo the dot-stuffing of line, a line of a block as sent.

A line opening with a period and holding more loses that period (RFC 5321 Section 4.5.2), so stuff() and then this give back any line. A line holding a single period ends the block (block_end()), so it comes back as is.

Parameters:

line (bytes) – Line as sent, CRLF excluded.

Return type:

bytes

Returns:

The line of text.

pcapkit.utilities.lines.decode_text(text)[source]

Decode the text a line carries, recording how to encode it back.

The text is decoded with the charset chardet detects, or as 'unicode_escape' with errors replaced if that fails. Where the result does not encode back to text as UTF-8, the detected charset is returned with it, or 'latin-1', decoding again, if that does not reproduce text either, so the octets survive a rebuild.

Parameters:

text (bytes) – Octets of the text.

Return type:

tuple[str, str | None]

Returns:

The decoded text and the charset to encode it with, the latter None for UTF-8.