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/...
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); if (len == 0)
return null_buffer_singleton; /* special shared representation for 0: */ void buffer_free(void *buf)
{
if (buf != null_buffer_singleton)
free(buf);
}But frankly, having a 1-byte buffer, pointer to which can serve as a sentinel value à la NULL (but dereferenceable!), and which you can pass to free() without it being deallocated is indeed rather useful.
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.
In other words, values beyond that are not forbidden by the spec, but are outside of the conformance requirements. It is a quality of implementation issue whether a given implementation handles more than required.
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...
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.
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.
s:size:value;
For strings, which is pretty similar. Size is in bytes.Dollar, NUL, and other terminated strings, by contrast, are string length O(N).