mirror of /home/gitosis/repositories/libowfat.git
fix scan_netstring to actually correctly return the length of the transported string add documentationmaster
parent
3ccdc6055a
commit
ec32c40bab
7 changed files with 127 additions and 12 deletions
@ -0,0 +1,29 @@ |
||||
.TH fmt_netstring 3 |
||||
.SH NAME |
||||
fmt_netstring \- convert a memory buffer into a netstring |
||||
.SH SYNTAX |
||||
.B #include <fmt.h> |
||||
|
||||
size_t \fBfmt_netstring\fP(char *\fIdest\fR,const char *\fIsource\fR, |
||||
size_t \fIlen\fR); |
||||
.SH DESCRIPTION |
||||
fmt_netstring creates a netstring from a raw memory buffer and returns |
||||
the length. |
||||
|
||||
fmt_netstring does not append \\0. |
||||
|
||||
If \fIdest\fR equals FMT_LEN (i.e. is zero), fmt_netstring returns the number |
||||
of bytes it would have written. |
||||
.SH "RETURN VALUE" |
||||
fmt_netstring returns the number of bytes written (or that would have |
||||
been written, had the destination pointer not pointed to NULL). |
||||
|
||||
If the input buffer is implausibly large, fmt_netstring returns 0 |
||||
instead. |
||||
.SH EXAMPLE |
||||
The raw memory buffer "foo" would become the netstring "3:foo," |
||||
.SH SPEC |
||||
http://cr.yp.to/proto/netstrings.txt |
||||
|
||||
.SH "SEE ALSO" |
||||
scan_netstring(3) |
@ -0,0 +1,15 @@ |
||||
#include "fmt.h" |
||||
#include "rangecheck.h" |
||||
#include <string.h> |
||||
|
||||
size_t fmt_netstring(char* dest,const char* src,size_t len) { |
||||
size_t n=fmt_ulong(NULL,len); |
||||
size_t m; |
||||
if (!range_validbuf(src,len) || add_of(m,len,n+2)) return 0; /* n came from fmt_ulong, so it is a very small number, below 50 */ |
||||
if (!dest) return m; |
||||
fmt_ulong(dest,len); dest+=n; |
||||
*dest++=':'; |
||||
memcpy(dest,src,len); |
||||
dest[len]=','; |
||||
return m; |
||||
} |
@ -0,0 +1,27 @@ |
||||
.TH scan_netstring 3 |
||||
.SH NAME |
||||
scan_netstring \- parse a netstring |
||||
.SH SYNTAX |
||||
.B #include <scan.h> |
||||
|
||||
size_t \fBscan_netstring\fP(const char *\fIin\fR,size_t len,char** \fIdest\fR, size_t *\fIslen\fR); |
||||
.SH DESCRIPTION |
||||
scan_netstring attempts to parse the netstring in the input buffer |
||||
(in,len). If the buffer contains a valid netstring, then (*dest,*slen) |
||||
is set to the start and length of the transported string. |
||||
|
||||
Note that this string is not zero terminated. No copy is made. |
||||
scan_netstring points it inside the input buffer. |
||||
.SH "RETURN VALUE" |
||||
scan_netstring returns the number of bytes in the netstring (the outer |
||||
representation, not the transported inner string) if parsing worked, or |
||||
0 if the input buffer did not contain a valid (or full) netstring. |
||||
|
||||
.SH EXAMPLE |
||||
The raw memory buffer "foo" would become the netstring "3:foo," |
||||
.SH SPEC |
||||
http://cr.yp.to/proto/netstrings.txt |
||||
|
||||
.SH "SEE ALSO" |
||||
fmt_netstring(3) |
||||
|
@ -0,0 +1,20 @@ |
||||
#include "fmt.h" |
||||
#include "scan.h" |
||||
#include <assert.h> |
||||
#include <stdio.h> |
||||
|
||||
int main() { |
||||
char buf[100]; |
||||
char* s; |
||||
size_t l; |
||||
const char* orig; |
||||
assert(fmt_netstring(buf,"hello world!",12)==16 && !memcmp(buf,"12:hello world!,",16)); |
||||
assert(scan_netstring(buf,16,&s,&l)==16 && s==buf+3 && l==12); |
||||
|
||||
orig="3:foo,"; assert(scan_netstring(orig,6,&s,&l)==6 && s==orig+2 && l==3); |
||||
orig="4294967295:foo,"; assert(scan_netstring(orig,15,&s,&l)==0); |
||||
orig="18446744073709551615:foo,"; assert(scan_netstring(orig,25,&s,&l)==0); |
||||
|
||||
assert(fmt_netstring(buf,orig,(size_t)-1)==0); |
||||
assert(fmt_netstring(buf,NULL,(size_t)-1)==0); |
||||
} |
@ -1,11 +0,0 @@ |
||||
#include "scan.h" |
||||
#include <assert.h> |
||||
|
||||
int main() { |
||||
char* s; |
||||
size_t l; |
||||
const char* orig; |
||||
orig="3:foo,"; assert(scan_netstring(orig,6,&s,&l)==6); assert(s==orig+2); |
||||
orig="4294967295:foo,"; assert(scan_netstring(orig,15,&s,&l)==0); |
||||
orig="18446744073709551615:foo,"; assert(scan_netstring(orig,25,&s,&l)==0); |
||||
} |
Loading…
Reference in new issue