networking · intermediate · ~25 min
Strict chunked-encoding parser; refuse permissive variants.
HTTP/1.1 can send a body in "chunked" transfer encoding: a series of size-prefixed pieces. Decode such a body into one flat buffer, parsing strictly so that smuggling-style variants are rejected.
Implement int decode_chunked(const char *in, int in_len, char *out, int cap, int *out_len) that decodes the chunked body in in into out.
The wire format is a repetition of HEX_SIZE CRLF DATA CRLF, terminated by a zero-size chunk 0 CRLF CRLF. HEX_SIZE is the chunk's data length in hexadecimal.
in, in_len: the chunked body bytes and their length.out, cap: destination buffer and its capacity in bytes.out_len: set to the total number of decoded bytes on success.Returns 1 on success (with *out_len set to the decoded length), or 0 on any parse error.
"4\r\nWiki\r\n5\r\npedia\r\n0\r\n\r\n" -> 1, out="Wikipedia", *out_len=9
"0\r\n\r\n" -> 1, *out_len=0
0\r\n\r\n) decodes to length 0.4abc).CRLF after a chunk's data.cap.The chunked-encoding parser is the smuggling-vector surface. Writing one correctly — strict, bounded — is the defensive baseline.
Chunked body bytes (in) and length (in_len), an output buffer (out) with capacity (cap), and out_len for the decoded length.
Returns 1 on success with *out_len set, or 0 on any parse error.
Parse strictly: refuse non-hex size chars, surrounding whitespace, missing CRLFs, and any decode that would overflow cap.
#include <stddef.h>
int decode_chunked(const char *in, int in_len, char *out, int cap, int *out_len) { /* TODO */ (void)in; (void)in_len; (void)out; (void)cap; (void)out_len; return 0; }
Accepting whitespace before/after the size. Accepting non-hex chars.
Empty body (just 0\r\n\r\n). Single chunk. Multiple chunks. Chunk size with leading 0x.
O(in_len).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.