Skip to content

Commit 9ead2b8

Browse files
committed
removed netstring_encode_new()
1 parent 81a58e5 commit 9ead2b8

File tree

3 files changed

+11
-55
lines changed

3 files changed

+11
-55
lines changed

netstring.c

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
D. J. Bernstein's reference implementation.
2828
2929
Example:
30-
if (netstring_read("3:foo,", 6, &str, &len) < 0) explode_and_die();
30+
if (netstring_read(&buf, &buflen, &str, &len) < 0) failed();
3131
*/
3232
int netstring_read(char **pbuffer, size_t *pbuffer_length,
3333
char **netstring_start, size_t *netstring_length) {
@@ -93,52 +93,28 @@ size_t netstring_buffer_size(size_t data_length) {
9393
return (size_t)numdigits(data_length) + data_length + 2;
9494
}
9595

96-
/* Allocate and create a netstring containing the first `len` bytes of
97-
`data`. This must be manually freed by the client. If `len` is 0
98-
then no data will be read from `data`, and it may be NULL. */
99-
size_t netstring_encode_new(char **netstring, char *data, size_t len) {
100-
char *ns;
101-
size_t num_len = 1;
102-
103-
if (len == 0) {
104-
ns = malloc(3);
105-
ns[0] = '0';
106-
ns[1] = ':';
107-
ns[2] = ',';
108-
} else {
109-
num_len = numdigits(len);
110-
ns = malloc(num_len + len + 2);
111-
sprintf(ns, "%lu:", (unsigned long)len);
112-
memcpy(ns + num_len + 1, data, len);
113-
ns[num_len + len + 1] = ',';
114-
}
115-
116-
*netstring = ns;
117-
return num_len + len + 2;
118-
}
119-
12096
/* Allocate and create a netstring containing the first `len` bytes of
12197
`data`. This must be manually freed by the client. If `len` is 0
12298
then no data will be read from `data`, and it may be NULL.
12399
Returns the netstring size not including the null terminator */
124-
size_t netstring_add_ex(char **list, char *str, size_t len) {
100+
size_t netstring_add_ex(char **netstring, char *data, size_t len) {
125101
size_t num_len, size_prev=0, size_next;
126102
char *ptr;
127103

128-
if (list == 0 || (len > 0 && str == 0)) return 0;
104+
if (netstring == 0 || (len > 0 && data == 0)) return 0;
129105

130106
num_len = numdigits(len);
131107
size_next = num_len + len + 2;
132108

133-
if (*list == 0) {
109+
if (*netstring == 0) {
134110
ptr = malloc(size_next + 1);
135111
if (ptr == 0) return 0;
136-
*list = ptr;
112+
*netstring = ptr;
137113
} else {
138-
size_prev = strlen(*list);
139-
ptr = realloc(*list, size_prev + size_next + 1);
114+
size_prev = strlen(*netstring);
115+
ptr = realloc(*netstring, size_prev + size_next + 1);
140116
if (ptr == 0) return 0;
141-
*list = ptr;
117+
*netstring = ptr;
142118
ptr += size_prev;
143119
}
144120

@@ -147,13 +123,13 @@ size_t netstring_add_ex(char **list, char *str, size_t len) {
147123
} else {
148124
sprintf(ptr, "%lu:", (unsigned long)len);
149125
ptr += num_len + 1;
150-
memcpy(ptr, str, len);
126+
memcpy(ptr, data, len);
151127
ptr += len; *ptr = ',';
152128
ptr++; *ptr = 0;
153129
}
154130
return size_prev + size_next;
155131
}
156132

157-
size_t netstring_add(char **list, char *str) {
158-
return netstring_add_ex(list, str, strlen(str));
133+
size_t netstring_add(char **netstring, char *data) {
134+
return netstring_add_ex(netstring, data, strlen(data));
159135
}

netstring.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
size_t netstring_add(char **netstring, char *data);
77
size_t netstring_add_ex(char **netstring, char *data, size_t len);
88

9-
size_t netstring_encode_new(char **netstring, char *data, size_t len);
10-
119
int netstring_read(char **buffer_start, size_t *buffer_length,
1210
char **netstring_start, size_t *netstring_length);
1311

testsuite.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,6 @@ void test_netstring_buffer_size(void) {
103103
assert(netstring_buffer_size(12345) == 12345 + 5 + 2);
104104
}
105105

106-
void test_netstring_encode_new(void) {
107-
char *ns; size_t bytes;
108-
109-
bytes = netstring_encode_new(&ns, "foo", 3);
110-
assert(ns != NULL); assert(strncmp(ns, "3:foo,", 6) == 0); assert(bytes == 6);
111-
free(ns);
112-
113-
bytes = netstring_encode_new(&ns, NULL, 0);
114-
assert(ns != NULL); assert(strncmp(ns, "0:,", 3) == 0); assert(bytes == 3);
115-
free(ns);
116-
117-
bytes = netstring_encode_new(&ns, "hello world!", 12); assert(bytes == 16);
118-
assert(ns != NULL); assert(strncmp(ns, "12:hello world!,", 16) == 0);
119-
free(ns);
120-
}
121-
122106
void test_netstring_add_ex(void) {
123107
char *ns=0; size_t bytes;
124108

@@ -135,7 +119,6 @@ void test_netstring_add_ex(void) {
135119
free(ns); ns = 0;
136120

137121
bytes = netstring_add_ex(&ns, "hello world!", 5); assert(bytes == 8);
138-
puts(ns);
139122
assert(ns != NULL); assert(strncmp(ns, "5:hello,", 8) == 0);
140123
free(ns); ns = 0;
141124
}
@@ -156,7 +139,6 @@ int main(void) {
156139
printf("Running test suite...\n");
157140
test_netstring_read();
158141
test_netstring_buffer_size();
159-
test_netstring_encode_new();
160142
test_netstring_add_ex();
161143
test_netstring_add();
162144
printf("All tests passed!\n");

0 commit comments

Comments
 (0)