Documentation
¶
Overview ¶
Package v44 is the ITU-T V.44 (11/2000, with Corrigendum 1 of 03/2002) data compression algorithm in pure Go: the packet method (annex B.1, Compress and Decompress), the multi-packet method (annex B.2) and the stream method of the main body (Encoder and Decoder), and the negotiation of annex A and 7.4 (Proposal, MarshalXID, ParseXID, Agree, parameter mode).
Multi-packet and stream (stream.go): the dictionary and history last from packet to packet, or along the stream. Each packet ends with FLUSH and octet alignment; the character after a FLUSH is appended to the string before it (7.13, Table 2), as the appendix II encoder does ahead of that character's arrival. A full node-tree (7.11.3) or history (7.11.4) brings REINIT, mid-packet if need be; N2 defaults to 1024 and N8 to 3 × N2 (Table 10). After a multi-packet packet sent uncompressed both ends start a new dictionary (B.2.1). The stream encoder holds back N7 characters of look-ahead until Flush, so that its codes are those of the whole stream; its decoder takes input in pieces of any size. The stream also has transparent mode (6.5) with the ESCAPE of 7.14, an optional compressibility monitor (7.11.5, Encoder.Auto) and parameter mode (7.4.2). V.44 itself has no test vectors for these methods; they are tested by round trip, the appendix II.1 sequence continued across a FLUSH, and the codes and octets the clauses define.
Negotiation (xid.go): the V.44 parameters of Table 10 as an XID user data subfield (annex A) or a parameter-mode sequence, and the agreement rules of 7.4 and annex B (lesser values, directions, method). The V.42 LAPM procedures that carry the XID frame are not part of this package.
Each call to Compress or Decompress is one packet: the dictionaries start in their initial state (7.5.1, 7.5.2), the encoder ends the packet with a FLUSH control code and zero bits up to the next octet boundary (7.13, B.1.1), and the decoder stops at the FLUSH, treating whatever follows it as padding. RFC 3051 (IPComp with V.44, clauses 2.1, 2.3 and 3.1) reads annex B.1 the same way: reset before every packet, flush after its last byte, whole octets.
What is built, clause by clause:
- encoder dictionary of root array, node-tree and history (6.2.1); decoder string collection and history (6.2.2);
- string matching down the tree-structures, string extension against the history, and the append and extend rules for new string-segments (6.3.1 to 6.3.4, Table 1);
- decoding of ordinals, codewords (including the codeword the decoder has not created yet, 6.4.1 rules 3 and 4) and string-extension lengths, with the string-creation rules of Table 2 (6.4);
- code prefixes (Table 5), control codes and codewords in C2 bits, ordinals in C5 bits (6.6.1), string-extension lengths (Tables 3 and 4), least significant bit first (6.6, Table 6);
- STEPUP of the ordinal size (7.11.1) and of the codeword size (7.11.2); the node-tree full rule of the packet method (B.1.1: no new codewords, matching and extension go on); FLUSH (7.13); the decoder's error conditions (7.15);
- the annex B indicator for a packet sent uncompressed (the ETM control code with its prefix in the first octet, then the original octets).
Parameters are the clause 8 set that matters to the packet method, with the B.1.2 defaults: N2 = 1525 codewords, N7 = 255. N8 (history size) is "not applicable" in the packet method, where the packet itself is the history; setting it bounds the history, and a packet longer than N8 is then coded as runs of N8 characters separated by REINIT control codes, as the multi-packet method does when its history fills (B.2.1, 7.11.4). The decoder also honours REINIT wherever it appears. The initial codeword size (6 bits) and ordinal size (7 bits) are fixed by 7.5 and are not parameters. Transparent mode (6.5) is not part of the packet methods (annex B); the stream method has it.
The decoder never panics on bad input and always ends: every code consumes at least one bit, and the output is bounded by Params.Limit.
Index ¶
Examples ¶
Constants ¶
const ( DefaultCodewords = 1525 DefaultMaxString = 255 DefaultLimit = 1 << 20 )
Defaults of the packet method (annex B.1.2) and of Limit.
const ( ParamSetID = 0x40 // parameter set identifier, "V44" (annex A) ParamCapability = 0x41 // C0: P, M and N bits ParamRequest = 0x42 // P0: directions ParamCodewordsTx = 0x43 // P1T ParamCodewordsRx = 0x44 // P1R ParamMaxStringTx = 0x45 // P2T ParamMaxStringRx = 0x46 // P2R ParamHistoryTx = 0x47 // P3T ParamHistoryRx = 0x48 // P3R )
Identifiers of Table 10.
const (
DefaultStreamCodewords = 1024
)
Defaults of the multi-packet and stream methods (Table 10, B.2.2).
Variables ¶
var ErrCorrupt = errors.New("v44: compressed data is not valid")
ErrCorrupt is wrapped by every decoding error caused by the input.
var ErrLimit = errors.New("v44: decompressed data exceeds the limit")
ErrLimit is wrapped when the output would exceed Params.Limit.
Functions ¶
func Compress ¶
Compress compresses one packet with the packet method: reset, encode, FLUSH, pad to an octet boundary.
Example ¶
Compress one packet with the annex B.1.2 default parameters and decompress it with the same parameters.
p := v44.DefaultParams()
src := bytes.Repeat([]byte("0-0:96.1.0.255 meter "), 20)
packed, err := v44.Compress(p, src)
if err != nil {
panic(err)
}
fmt.Println(len(src), "->", len(packed) < len(src)/4)
back, err := v44.Decompress(p, packed)
fmt.Println(bytes.Equal(back, src), err)
Output: 420 -> true true <nil>
func CompressOrRaw ¶
CompressOrRaw returns the smaller of the compressed packet and the annex B uncompressed form (the ETM control code with its prefix in the first octet, then src), and whether it is compressed.
Types ¶
type Agreement ¶ added in v0.2.0
type Agreement struct {
TxOn, RxOn bool
Tx, Rx Params
// Method is MultiPacket when both ends support it, 0 for the packet
// method (both support only that), Stream for a modem link (neither
// sets P).
Method Method
}
Agreement is the outcome of a negotiation seen from one end: whether and with which parameters its encoder (Tx) and decoder (Rx) run, and the method.
func Agree ¶ added in v0.2.0
Agree applies 7.4 and annex B to this end's proposal and the peer's: each direction runs when the encoder's end asks to send and the decoder's end to receive; each size is the lesser of the two proposals for that direction (Table 10 defaults where one is absent); a value below its minimum is a procedural error.
type Decoder ¶ added in v0.2.0
type Decoder struct {
// OnParameters, when set, receives the parameters of each parameter
// mode sequence (7.4.2).
OnParameters func([]Parameter)
// contains filtered or unexported fields
}
Decoder is a V.44 decoder whose dictionary lives across calls, the peer of an Encoder of the same method and parameters. It is not safe for concurrent use. After an error it refuses further input.
func NewDecoder ¶ added in v0.2.0
NewDecoder starts a decoder of the multi-packet or stream method; p as for NewEncoder. Limit bounds the output of one call.
type Encoder ¶ added in v0.2.0
type Encoder struct {
// Auto switches the stream between compressed and transparent mode
// at each Flush (7.11.5): to transparent when the codes of the
// segment took more bits than its characters, back to compressed
// when a trial compression of a transparent segment saves at least
// an eighth.
Auto bool
// contains filtered or unexported fields
}
Encoder is a V.44 encoder whose dictionary lives across calls, for the multi-packet method (Packet, PacketOrRaw) or the stream method (Write, Flush, SetTransparent, SendParameters). It is not safe for concurrent use.
func NewEncoder ¶ added in v0.2.0
NewEncoder starts an encoder of the multi-packet or stream method. Zero parameters take the Table 10 defaults (N2 1024, N7 255, N8 3 × N2); N8 must exceed the longest packet for the multi-packet method to keep packets whole in one history (B.2.1), though longer ones still work.
Example ¶
The multi-packet method (annex B.2): the second packet, like the first, costs little because the dictionary remembers the first.
enc, _ := v44.NewEncoder(v44.MultiPacket, v44.Params{})
dec, _ := v44.NewDecoder(v44.MultiPacket, v44.Params{})
pkt := []byte(`{"meter":"0-0:96.1.0.255","voltage":230.1,"current":4.2}`)
for i := 0; i < 2; i++ {
c := enc.Packet(pkt)
back, err := dec.Packet(c)
fmt.Println(len(pkt), "->", len(c), bytes.Equal(back, pkt), err)
}
Output: 56 -> 52 true <nil> 56 -> 4 true <nil>
func (*Encoder) Flush ¶ added in v0.2.0
Flush ends the codes of every character written (7.13): FLUSH and zero bits to the octet in compressed mode. The dictionary is kept.
func (*Encoder) Packet ¶ added in v0.2.0
Packet compresses one packet with the multi-packet method: the codes, FLUSH and zero bits to the octet.
func (*Encoder) PacketOrRaw ¶ added in v0.2.0
PacketOrRaw is Packet, or the annex B uncompressed form (ETM with its prefix, in the codeword size the decoder has at the start of the packet, then the octets) when that is not larger; the second result is whether it is compressed. After an uncompressed packet both ends start a new dictionary (B.2.1).
func (*Encoder) SendParameters ¶ added in v0.2.0
SendParameters transfers parameters to the peer decoder in parameter mode (7.4.2): transparent mode if not in it, EPM, the parameters, End. The stream stays in transparent mode; the control function applies the agreed values by starting new encoders and decoders (7.5).
func (*Encoder) SetTransparent ¶ added in v0.2.0
SetTransparent switches the stream to transparent mode (6.5.1: the characters written are encoded, then ETM and zero bits to the octet) or back to compressed mode (6.5.2: ESCAPE and ECM, and both dictionaries start again), and returns the octets that does.
type Method ¶ added in v0.2.0
type Method int
Method is how an Encoder and its Decoder keep their dictionaries.
const ( // MultiPacket keeps the dictionary and history from packet to packet // (annex B.2); every packet ends with FLUSH and octet alignment, and // the link must deliver every packet, in order. MultiPacket Method = iota + 1 // Stream is the primary method of the main body: one continuous // stream of codes, FLUSH on request (7.13), transparent mode (6.5) // and parameter mode (7.4.2). Stream )
Methods of V.44.
type Parameter ¶ added in v0.2.0
Parameter is one negotiation parameter of Table 10: its identifier and value octets.
type Params ¶
type Params struct {
// Codewords is N2, the number of codewords including the four control
// codes (Table 10: 256 to 65535; B.1.2 default 1525).
Codewords int
// MaxString is N7, the maximum string length (Table 10: 32 to 255;
// B.1.2 default 255).
MaxString int
// History is N8, the history size. 0 (the default, B.1.2 "not
// applicable") makes the whole packet the history. Otherwise at least
// 512 (Table 10).
History int
// Limit bounds the decompressed size of one packet (not a V.44
// parameter: a guard against hostile input). Default 1 MiB.
Limit int
}
Params are the V.44 parameters one encoder and its peer decoder share. The zero value of a field means its default.
func DefaultParams ¶
func DefaultParams() Params
DefaultParams are the annex B.1.2 packet-method defaults.
type Proposal ¶ added in v0.2.0
type Proposal struct {
// Packet and MultiPacket are the P and M bits (annex A, annex B):
// the packet-network methods supported. Neither is a modem link.
Packet, MultiPacket bool
// AfterLink is the N bit: parameters negotiated in parameter mode
// after the link is up (7.4.2) rather than in the XID exchange.
AfterLink bool
// Transmit and Receive are P0: compression wanted in the direction
// of this end's encoder and of its decoder.
Transmit, Receive bool
TxCodewords, RxCodewords int // P1T, P1R: 256 to 65535
TxMaxString, RxMaxString int // P2T, P2R: 32 to 255
TxHistory, RxHistory int // P3T, P3R: 512 or more
}
Proposal is what one end proposes: the V.44 capability (C0) and the values of Table 10, in the directions of its own encoder (Tx) and decoder (Rx). Zero sizes are not sent, which leaves the peer the defaults.
func ParseXID ¶ added in v0.2.0
ParseXID reads the V.44 proposal from an XID user data subfield: the parameters after the "V44" parameter set identifier, up to another parameter set or the end.
func ProposalOf ¶ added in v0.2.0
ProposalOf reads a proposal from Table 10 parameters (from parameter mode or an XID). Unknown identifiers are skipped (Table 10 reserves them).
func (Proposal) MarshalXID ¶ added in v0.2.0
MarshalXID is the user data subfield of an XID frame that proposes V.44 (annex A, Table A.1): group identifier FFh, the parameter set identifier "V44", then the parameters. With AfterLink set only C0 and P0 go in it; the rest follows in parameter mode (7.4.1, 7.4.2).
func (Proposal) Parameters ¶ added in v0.2.0
Parameters lists the proposal as Table 10 parameters, C0 and P0 first.