Netstrings (1997)

signa11 31 points 23 comments July 16, 2026
cr.yp.to · View on Hacker News

Discussion Highlights (10 comments)

gnabgib

(1997) -DJB

ocrow

Seems like a coherent, sensible proposal, as one might expect from djb. Any notable protocols use them?

regularfry

Tagged Netstrings (tnetstrings) was a related proposal from 15 years ago or so. It replaces the comma with a single-character type definition so you can do JSON-like objects with a couple of recursive types: you had ',', '#', '^', '!', and '~' for strings, integers, floats, booleans, and nulls, then ']' and '}' for lists and dictionaries. Most of the links have bitrotted and I don't think it ever got much traction, but I did always like how simple it was. There's a copy someone grabbed of the original spec here: https://raw.githubusercontent.com/ged/tnetstrings.info/refs/...

Joker_vD

if (scanf("%9lu",&len) < 1) barf(); /* >999999999 bytes is bad */ if (getchar() != ':') barf(); buf = malloc(len + 1); /* malloc(0) is not portable */ if (!buf) barf(); if (fread(buf,1,len,stdin) < len) barf(); if (getchar() != ',') barf(); Ah, the wonders of error-handling in C. Also, I wonder what's wrong with buf = malloc(len ? len : 1);

weinzierl

Making the thing that describes the bounds of an arbitrary length thing itself arbitrary length sound like an unnecessarily risky complication to me. Especially since it only grows with the log of the thing it bounds. So, we could easily have s fixed length length field that covers all ever possible length values.

account42

I don't like formats that look like text buy may actually contain binary data - that's only going to tempt implementations that will choke when the string actually contains arbitrary data. Would be safer to encode the length and/or separator as something more obviously binary, which will also make the thing easier to parse in low-level implementations.

dayjah

I’ve near infinite respect for DJB, so I’m assuming I’ve missed something obvious here. Why is ‘,’ being used as a termination byte. Is it just a backstop? If buf[len+1] != ‘,’ then there’s a line error? I’ve done plenty of wire protocol work, and length prefixed strings are great to work with. I’ve also been lucky that those strings were typically contained within a broader payload. To that end, I’ve not had to think about the case of many strings one after another.

kazinator

This is very similar to a notation occuring in Ronald Rivest's S-Expression proposal from 1996. https://people.csail.mit.edu/rivest/pubs/RL96.ver-1.0.pdf He has it as a hexadecimal length preceded by a pound sign (#), a colon, and the raw octet data.

burnt-resistor

Another string length O(1) encoding format like Pascal strings that were len (unsigned byte) + data. Limited to 255 characters however. Dollar, NUL, and other terminated strings, by contrast, are string length O(N).

johnea

So much of the terse, utilitarian, DJB Way was overpowered by bloated corporate crap 8-/ One still functional example exists in voidlinux's init system. There was never any need for systemd 8-/ Except leveraging RedHat/IBM's domination of the linux user space ecosystem...

Semantic search powered by Rivestack pgvector
14,015 stories · 131,331 chunks indexed