提交 21b4ce55 authored 作者: Iñaki Baz Castillo's avatar Iñaki Baz Castillo

Add netstring-c dep.

上级 139e5804
...@@ -4,3 +4,6 @@ ...@@ -4,3 +4,6 @@
[submodule "submodules/usrsctp"] [submodule "submodules/usrsctp"]
path = submodules/usrsctp path = submodules/usrsctp
url = https://github.com/sctplab/usrsctp.git url = https://github.com/sctplab/usrsctp.git
[submodule "submodules/netstring-c"]
path = submodules/netstring-c
url = https://github.com/PeterScott/netstring-c.git
NOTE
====
This is a modified version of the [netstring-c](https://github.com/PeterScott/netstring-c.git) project adapted to properly compile in **mediasoup**.
Netstring parsing
=================
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:
"12:hello, world!,"
"3:foo,"
"0:,"
To specify a list of strings, just concatenate the netstrings. The list ["hey", "everyone"] can be encoded as
"3:hey,8:everyone,"
This is some code in C for netstring serialization and deserialization. It checks netstrings for validity when parsing them, and guards against malicious input. It's also fast.
Basic API
---------
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.
**Parsing netstrings**
To parse a netstring, use `netstring_read()`.
int netstring_read(char *buffer, size_t buffer_length,
char **netstring_start, size_t *netstring_length);
Reads a netstring from a `buffer` of length `buffer_length`. Writes to
`netstring_start` a pointer to the beginning of the string in the
buffer, and to `netstring_length` the length of the string. Does not
allocate any memory. If it reads successfully, then it returns 0. If
there is an error, then the return value will be negative. The error
values are:
NETSTRING_ERROR_TOO_LONG More than 999999999 bytes in a field
NETSTRING_ERROR_NO_COLON No colon was found after the number
NETSTRING_ERROR_TOO_SHORT Number of bytes greater than buffer length
NETSTRING_ERROR_NO_COMMA No comma was found at the end
NETSTRING_ERROR_LEADING_ZERO Leading zeros are not allowed
NETSTRING_ERROR_NO_LENGTH Length not given at start of netstring
If you're sending messages with more than 999999999 bytes -- about 2
GB -- then you probably should not be doing so in the form of a single
netstring. This restriction is in place partially to protect from
malicious or erroneous input, and partly to be compatible with
D. J. Bernstein's reference implementation.
**Creating netstrings**
To create a netstring, there are a few ways to do it. You could do something really simple like this example from [the spec](http://cr.yp.to/proto/netstrings.txt):
if (printf("%lu:", len) < 0) barf();
if (fwrite(buf, 1, len, stdout) < len) barf();
if (putchar(',') < 0) barf();
This code provides a convenience function for creating a new netstring:
size_t netstring_encode_new(char **netstring, char *data, size_t len);
This allocates and creates a netstring containing the first `len` bytes of `data`. This must be manually freed by the client. If `len` is 0 then no data will be read from `data`, and it may be null.
Contributing
------------
All this code is Public Domain. If you want to contribute, you can send bug reports, or fork the project on GitHub. Contributions are welcomed with open arms.
CFLAGS = -O2 -std=c++11 -Wall
LDFLAGS= -lm
.PHONY: test clean
all: testsuite
testsuite: testsuite.cpp netstring.o
test: testsuite
./testsuite
clean:
rm -f *.o testsuite
/* Streaming API for netstrings. */
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include "netstring.h"
/* Reads a netstring from a `buffer` of length `buffer_length`. Writes
to `netstring_start` a pointer to the beginning of the string in
the buffer, and to `netstring_length` the length of the
string. Does not allocate any memory. If it reads successfully,
then it returns 0. If there is an error, then the return value will
be negative. The error values are:
NETSTRING_ERROR_TOO_LONG More than 999999999 bytes in a field
NETSTRING_ERROR_NO_COLON No colon was found after the number
NETSTRING_ERROR_TOO_SHORT Number of bytes greater than buffer length
NETSTRING_ERROR_NO_COMMA No comma was found at the end
NETSTRING_ERROR_LEADING_ZERO Leading zeros are not allowed
NETSTRING_ERROR_NO_LENGTH Length not given at start of netstring
If you're sending messages with more than 999999999 bytes -- about
2 GB -- then you probably should not be doing so in the form of a
single netstring. This restriction is in place partially to protect
from malicious or erroneous input, and partly to be compatible with
D. J. Bernstein's reference implementation.
Example:
if (netstring_read("3:foo,", 6, &str, &len) < 0) explode_and_die();
*/
int netstring_read(char *buffer, size_t buffer_length,
char **netstring_start, size_t *netstring_length) {
int i;
size_t len = 0;
/* Write default values for outputs */
*netstring_start = NULL; *netstring_length = 0;
/* Make sure buffer is big enough. Minimum size is 3. */
if (buffer_length < 3) return NETSTRING_ERROR_TOO_SHORT;
/* No leading zeros allowed! */
if (buffer[0] == '0' && isdigit(buffer[1]))
return NETSTRING_ERROR_LEADING_ZERO;
/* The netstring must start with a number */
if (!isdigit(buffer[0])) return NETSTRING_ERROR_NO_LENGTH;
/* Read the number of bytes */
for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) {
/* Error if more than 9 digits */
if (i >= 9) return NETSTRING_ERROR_TOO_LONG;
/* Accumulate each digit, assuming ASCII. */
len = len*10 + (buffer[i] - '0');
}
/* Check buffer length once and for all. Specifically, we make sure
that the buffer is longer than the number we've read, the length
of the string itself, and the colon and comma. */
if (i + len + 1 >= buffer_length) return NETSTRING_ERROR_TOO_SHORT;
/* Read the colon */
if (buffer[i++] != ':') return NETSTRING_ERROR_NO_COLON;
/* Test for the trailing comma, and set the return values */
if (buffer[i + len] != ',') return NETSTRING_ERROR_NO_COMMA;
*netstring_start = &buffer[i]; *netstring_length = len;
return 0;
}
/* Return the length, in ASCII characters, of a netstring containing
`data_length` bytes. */
size_t netstring_buffer_size(size_t data_length) {
if (data_length == 0) return 3;
return (size_t)ceil(log10((double)data_length + 1)) + data_length + 2;
}
/* Allocate and create a netstring containing the first `len` bytes of
`data`. This must be manually freed by the client. If `len` is 0
then no data will be read from `data`, and it may be NULL. */
size_t netstring_encode_new(char **netstring, char *data, size_t len) {
char *ns;
size_t num_len = 1;
if (len == 0) {
ns = (char *)malloc(3);
ns[0] = '0';
ns[1] = ':';
ns[2] = ',';
} else {
num_len = (size_t)ceil(log10((double)len + 1));
ns = (char *)malloc(num_len + len + 2);
sprintf(ns, "%lu:", (unsigned long)len);
memcpy(ns + num_len + 1, data, len);
ns[num_len + len + 1] = ',';
}
*netstring = ns;
return num_len + len + 2;
}
#ifndef __NETSTRING_STREAM_H
#define __NETSTRING_STREAM_H
#include <string.h>
int netstring_read(char *buffer, size_t buffer_length,
char **netstring_start, size_t *netstring_length);
size_t netstring_buffer_size(size_t data_length);
size_t netstring_encode_new(char **netstring, char *data, size_t len);
/* Errors that can occur during netstring parsing */
#define NETSTRING_ERROR_TOO_LONG -1
#define NETSTRING_ERROR_NO_COLON -2
#define NETSTRING_ERROR_TOO_SHORT -3
#define NETSTRING_ERROR_NO_COMMA -4
#define NETSTRING_ERROR_LEADING_ZERO -5
#define NETSTRING_ERROR_NO_LENGTH -6
#endif
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include "netstring.h"
/* Good examples */
char ex1[] = "12:hello world!,";
char ex2[] = "3:foo,0:,3:bar,";
/* Bad examples */
char ex3[] = "12:hello world! "; /* No comma */
char ex4[] = "15:hello world!,"; /* Too short */
char ex5[] = "03:foo,"; /* Leading zeros are forbidden */
char ex6[] = "999999999999999:haha lol,"; /* Too long */
char ex7[] = "3fool,"; /* No colon */
char ex8[] = "what's up"; /* No number or colon */
char ex9[] = ":what's up"; /* No number */
void test_netstring_read(void) {
char *netstring;
size_t netstring_len;
int retval;
/* ex1: hello world */
retval = netstring_read(ex1, strlen(ex1), &netstring, &netstring_len);
assert(netstring_len == 12); assert(strncmp(netstring, "hello world!", 12) == 0);
assert(retval == 0);
/* ex2: three netstrings, concatenated. */
retval = netstring_read(ex2, strlen(ex2), &netstring, &netstring_len);
assert(netstring_len == 3); assert(strncmp(netstring, "foo", 3) == 0);
assert(retval == 0);
retval = netstring_read(netstring+netstring_len+1, 9, &netstring, &netstring_len);
assert(netstring_len == 0); assert(retval == 0);
retval = netstring_read(netstring+netstring_len+1, 6, &netstring, &netstring_len);
assert(netstring_len == 3); assert(strncmp(netstring, "bar", 3) == 0);
assert(retval == 0);
/* ex3: no comma */
retval = netstring_read(ex3, strlen(ex3), &netstring, &netstring_len);
assert(netstring_len == 0); assert(netstring == NULL);
assert(retval == NETSTRING_ERROR_NO_COMMA);
/* ex4: too short */
retval = netstring_read(ex4, strlen(ex4), &netstring, &netstring_len);
assert(netstring_len == 0); assert(netstring == NULL);
assert(retval == NETSTRING_ERROR_TOO_SHORT);
/* ex5: leading zero */
retval = netstring_read(ex5, strlen(ex5), &netstring, &netstring_len);
assert(netstring_len == 0); assert(netstring == NULL);
assert(retval == NETSTRING_ERROR_LEADING_ZERO);
/* ex6: too long */
retval = netstring_read(ex6, strlen(ex6), &netstring, &netstring_len);
assert(netstring_len == 0); assert(netstring == NULL);
assert(retval == NETSTRING_ERROR_TOO_LONG);
/* ex7: no colon */
retval = netstring_read(ex7, strlen(ex7), &netstring, &netstring_len);
assert(netstring_len == 0); assert(netstring == NULL);
assert(retval == NETSTRING_ERROR_NO_COLON);
/* ex8: no number or colon */
retval = netstring_read(ex8, strlen(ex8), &netstring, &netstring_len);
assert(netstring_len == 0); assert(netstring == NULL);
assert(retval == NETSTRING_ERROR_NO_LENGTH);
/* ex9: no number */
retval = netstring_read(ex9, strlen(ex9), &netstring, &netstring_len);
assert(netstring_len == 0); assert(netstring == NULL);
assert(retval == NETSTRING_ERROR_NO_LENGTH);
}
void test_netstring_buffer_size(void) {
assert(netstring_buffer_size(0) == 3);
assert(netstring_buffer_size(1) == 4);
assert(netstring_buffer_size(2) == 5);
assert(netstring_buffer_size(9) == 12);
assert(netstring_buffer_size(10) == 14);
assert(netstring_buffer_size(12345) == 12345 + 5 + 2);
}
void test_netstring_encode_new(void) {
char *ns; size_t bytes;
bytes = netstring_encode_new(&ns, (char *)"foo", 3);
assert(ns != NULL); assert(strncmp(ns, "3:foo,", 6) == 0); assert(bytes == 6);
free(ns);
bytes = netstring_encode_new(&ns, NULL, 0);
assert(ns != NULL); assert(strncmp(ns, "0:,", 3) == 0); assert(bytes == 3);
free(ns);
bytes = netstring_encode_new(&ns, (char *)"hello world!", 12); assert(bytes == 16);
assert(ns != NULL); assert(strncmp(ns, "12:hello world!,", 16) == 0);
free(ns);
}
int main(void) {
printf("Running test suite...\n");
test_netstring_read();
test_netstring_buffer_size();
test_netstring_encode_new();
printf("All tests passed!\n");
return 0;
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论