Section 4.30

Length-prefix framing

Definition

Length-prefix framing is a way to mark where each message ends in a continuous stream of bytes: every message, called a frame, starts with a fixed-size header holding a length field that states how many bytes follow. After reading the header, the receiver knows how many more bytes belong to the frame and where the next frame begins. The format states the width and byte order §4.21 of the length field, what the length counts (the body only, or header and body), and the largest length a receiver accepts.

The idea

TCP (Transmission Control Protocol), the Internet’s reliable transport, and a serial line both deliver a stream: the bytes arrive complete and in order, but the boundaries between the sender’s messages are lost. One read can return half a message, exactly one message, or the end of one message and the start of the next. The receiver needs a rule that finds the boundaries again. Three rules are common. Fixed-size messages waste space or limit the content. A delimiter, a special byte that ends each message, forces the sender to escape that byte wherever it appears in the data and the receiver to inspect every byte. A length prefix tells the receiver the size of each message before the message arrives, so the receiver can wait for exactly that many bytes.

The length field is a claim by the sender, not a fact about the bytes. A parcel label that says “contains 7 items” does not put 7 items in the parcel, and a label that says “contains 65,535 items” should not make the receiver clear space for 65,535 items. The receiver therefore compares the declared length with two numbers it knows for itself: the largest frame it is willing to accept, and the number of bytes that have actually arrived.

How it works

Let hh be the header size in bytes, LmaxL_{\max} the largest body length the receiver accepts, and CC the capacity of the receive buffer in bytes. The designer chooses LmaxL_{\max} so that h+Lmax≤Ch + L_{\max} \le C. The receiver keeps aa, the number of bytes accumulated in the buffer and not yet consumed, and repeats these steps.

  1. Read newly arrived bytes into the free part of the buffer, at most C−aC - a bytes, and add their number to aa.

  2. If a<ha < h, the header is incomplete; wait for more bytes.

  3. Decode the declared body length LL from the header, using the format’s byte order.

  4. If L>LmaxL > L_{\max}, reject the frame and close the connection, before reserving memory or waiting for the body. After a bad length the receiver no longer knows where the next frame starts, so it cannot continue on this stream.

  5. Compute the frame size T=h+LT = h + L. This cannot overflow, because L≤LmaxL \le L_{\max} and h+Lmax≤Ch + L_{\max} \le C.

  6. If a<Ta < T, the body is incomplete; wait for the missing T−aT - a bytes, for at most a fixed time.

  7. Pass the body, the LL bytes after the header, to the next stage, which deserializes it into values. Remove the first TT bytes from the buffer, keep the remaining a−Ta - T bytes in order as the start of the next frame, set aa to a−Ta - T, and go to step 2, because the remaining bytes may already hold a complete frame.

When the length counts the whole frame, header included, the receiver must also check L≥hL \ge h; otherwise the body length L−hL - h is negative, which unsigned arithmetic turns into a huge number. A body of length zero is legal in many formats, and its frame still consumes the hh header bytes. A receiver also limits how long a frame may take to complete and how many connections may hold partial frames at once. A sender that declares a permitted length and then sends one byte per minute ties up memory without breaking any other rule, a simple denial of service §1.28.

TLS (Transport Layer Security), the protocol that protects web traffic, frames its data this way. Each record has a 5-byte header: 1 byte of content type, 2 bytes of version and a 2-byte big-endian length that counts only the bytes after the header. The field can state up to 65,535, but TLS 1.3 forbids a declared length above 214+256=16,6402^{14} + 256 = 16{,}640 bytes for an encrypted record, and a receiver must end the connection when a record declares more. The largest value a field can hold is not the limit a receiver accepts.

Worked example

Take a 2-byte big-endian header that counts only the body, so h=2h = 2, with Lmax=1024L_{\max} = 1024 and a buffer of C=1026C = 1026 bytes. A sender writes two frames, with the 3-byte body "abc" and the 4-byte body "defg". In hexadecimal, where 61 is the character code of a, the stream is 00 03 61 62 63 00 04 64 65 66 67, 11 bytes in all. It arrives in three reads of 3, 5 and 3 bytes.

  1. First read, 00 03 61: a=3a = 3. The header is complete, because 3≥23 \ge 2. L=0×256+3=3L = 0 \times 256 + 3 = 3, and 3≤10243 \le 1024. T=2+3=5T = 2 + 3 = 5, and a=3<5a = 3 < 5, so the receiver waits for 5−3=25 - 3 = 2 more bytes. It must not read the body yet: only 1 of its 3 bytes is there.

  2. Second read, 62 63 00 04 64: a=3+5=8a = 3 + 5 = 8, and 8≥58 \ge 5. The receiver passes the body 61 62 63, the text "abc", removes 5 bytes and keeps 8−5=38 - 5 = 3 bytes, 00 04 64. Back at step 2: L=4L = 4, T=2+4=6T = 2 + 4 = 6, and a=3<6a = 3 < 6, so it waits for 3 more bytes.

  3. Third read, 65 66 67: a=3+3=6a = 3 + 3 = 6, and 6≥66 \ge 6. The receiver passes 64 65 66 67, the text "defg", and aa becomes 6−6=06 - 6 = 0.

The two frames used (2+3)+(2+4)=5+6=11(2 + 3) + (2 + 4) = 5 + 6 = 11 bytes, exactly the stream.

An attacker sends the header FF FF. Then L=255×256+255=65,535L = 255 \times 256 + 255 = 65{,}535, which exceeds 1024, so the receiver closes the connection at once, without reserving 65,535 bytes and without waiting for them. A receiver that skipped the comparison with aa after the first read would have read 2 body bytes that had not arrived: leftover data from an earlier frame, or memory beyond the buffer if the buffer had been sized to the bytes received.

Stated precisely

Let hh be the header size, LL the declared body length, LmaxL_{\max} the largest accepted body length, CC the buffer capacity and aa the number of bytes accumulated and not yet consumed, all in bytes, with the design condition h+Lmax≤Ch + L_{\max} \le C, and let LL count only the body. Then:

Two invariants hold throughout: a≤Ca \le C, and no byte at a position at or beyond aa is read. For a length that counts the whole frame, the receiver first requires L≥hL \ge h, and L−hL - h then takes the place of LL above.

Length-prefixed frames form a prefix-free encoding §15.38: the header fixes the frame’s total size, so no frame is a proper beginning of a different frame, and a stream of complete frames splits in exactly one way.

Limits and common mistakes

The length is unauthenticated when it is first used. The receiver must frame the bytes before it can check a message authentication code §5.7, so the framing steps must be safe for every possible length. The authenticated data should still include the header, so that a changed length is detected; TLS 1.3 includes the record header in the data that its authenticated encryption §5.16 protects.

One bad length loses the stream. A corrupted length shifts every later boundary, and a pure length-prefixed stream has no marker on which to resynchronize. Close the connection after a framing error. A delimiter-based format can instead resume at the next delimiter.

A length inside a body needs its own check. A length field inside the body must be checked against that body, not against the stream or the buffer; the type-length-value §4.31 entry treats such nested lengths.

Datagrams keep boundaries but not trust. A datagram transport, which delivers each message as a separate unit, needs no framing, yet a length field inside a datagram must still be checked against the datagram’s real size.

Representable is not acceptable. LmaxL_{\max} comes from the receiver’s memory and the application’s needs, not from the width of the field.

Know what the length counts. Confusing “body only” with “whole frame” misplaces every frame boundary by hh bytes.

Questions answered

How is declared length checked against accumulated bytes and maximum size?

The declared length is checked twice, in this order, and nothing is read or reserved before both checks allow it. First, as soon as the hh header bytes are present, the declared body length LL is compared with the receiver’s maximum LmaxL_{\max}, which was chosen so that h+Lmaxh + L_{\max} fits in the receive buffer; if L>LmaxL > L_{\max}, the receiver closes the connection without reserving memory or waiting for the body. Second, the frame size h+Lh + L is compared with aa, the number of bytes accumulated: while a<h+La < h + L, the receiver waits, for at most a fixed time, for the missing h+L−ah + L - a bytes and reads no body byte; when a≥h+La \ge h + L, it consumes exactly h+Lh + L bytes and keeps the remaining a−(h+L)a - (h + L) bytes as the start of the next frame. Because L≤LmaxL \le L_{\max} is checked first, the sum h+Lh + L cannot overflow. For example, with a 2-byte header, L=3L = 3 and a=3a = 3, the frame needs 2+3=52 + 3 = 5 bytes, so 2 are still missing; a declared length of 65,535 against a maximum of 1024 is refused at once.

Related entries: Type-length-value (TLV) (§4.31), Serialization (§4.28), Deserialization (§4.29), Authentication coverage (§5.41), Denial of service (DoS) (§1.28).