Skip to content

Commit 81a58e5

Browse files
authored
README on natural order: first write then read
1 parent 28fb7f0 commit 81a58e5

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

README.md

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
Netstring parsing
2-
=================
1+
Netstring for C
2+
===============
33

44
A [netstring](http://en.wikipedia.org/wiki/Netstring) is a way of encoding a sequence of bytes for transmission over a network, or for serialization. They're very easy to work with. They encode the data's length, and can be concatenated trivially. The format was [defined by D. J. Bernstein](http://cr.yp.to/proto/netstrings.txt) and is used in various software. Examples of netstrings:
55

@@ -18,7 +18,37 @@ Basic API
1818

1919
All the code is in `netstring.c` and `netstring.h`, and these have no external dependencies. To use them, just include them in your application. Include `netstring.h` and link with the C code.
2020

21-
### Parsing netstrings
21+
Creating netstrings
22+
-------------------
23+
24+
You can create your netstrings manually like in this example:
25+
26+
sprintf(buf, "%lu:%s,", strlen(str), str);
27+
28+
This code provides a convenience function for creating netstrings:
29+
30+
size_t netstring_add(char **netstring, char *data);
31+
32+
Here is how to use it:
33+
34+
char *netstring=0; /* we must initialize it to zero */
35+
36+
netstring_add(&netstring, "first");
37+
netstring_add(&netstring, "second");
38+
netstring_add(&netstring, "third");
39+
40+
do_something(netstring);
41+
42+
free(netstring); /* we must free after using it */
43+
44+
The extended version `netstring_add_ex` accepts a string length as the last argument:
45+
46+
size_t netstring_add_ex(char **netstring, char *data, size_t len);
47+
48+
This allocates and creates a netstring containing the first `len` bytes of `data`. If `len` is 0 then no data will be read from `data`, and it may be null.
49+
50+
Parsing netstrings
51+
------------------
2252

2353
To parse a netstring, use `netstring_read()`.
2454

@@ -62,34 +92,6 @@ netstring. This restriction is in place partially to protect from
6292
malicious or erroneous input, and partly to be compatible with
6393
D. J. Bernstein's reference implementation.
6494

65-
### Creating netstrings
66-
67-
You can create your netstrings manually like in this example:
68-
69-
sprintf(buf, "%lu:%s,", strlen(str), str);
70-
71-
This code provides a convenience function for creating netstrings:
72-
73-
size_t netstring_add(char **netstring, char *data);
74-
75-
Here is how to use it:
76-
77-
char *netstring=0; /* we must initialize it to zero */
78-
79-
netstring_add(&netstring, "first");
80-
netstring_add(&netstring, "second");
81-
netstring_add(&netstring, "third");
82-
83-
do_something(netstring);
84-
85-
free(netstring); /* we must free after using it */
86-
87-
The extended version `netstring_add_ex` accepts a string length as the last argument:
88-
89-
size_t netstring_add_ex(char **netstring, char *data, size_t len);
90-
91-
This allocates and creates a netstring containing the first `len` bytes of `data`. If `len` is 0 then no data will be read from `data`, and it may be null.
92-
9395
Message Framing on stream-based connections (sockets, pipes...)
9496
---------------------------------------------------------------
9597

0 commit comments

Comments
 (0)