Netstrings (1997)

(cr.yp.to)

18 points | by signa11 4 hours ago

4 comments

  • regularfry 3 hours ago
    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 1 hour ago

        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);
  • ocrow 4 hours ago
    Seems like a coherent, sensible proposal, as one might expect from djb. Any notable protocols use them?
    • Scaevolus 3 hours ago
      BitTorrent's bencoding format, used in .torrent files, effectively uses netstrings-- but without the trailing commas, so it uses "5:hello" to represent filenames and similar.
    • Asmod4n 9 minutes ago
      zurl and mongrel2 are using it.
    • asalahli 2 hours ago
      Not sure if it counts as notable, but SCGI uses it too: https://python.ca/scgi/protocol.txt
    • toast0 3 hours ago
      Php serialized uses

         s:size:value;
      
      For strings, which is pretty similar. Size is in bytes.
  • gnabgib 4 hours ago
    (1997) -DJB