man page and unit tests for scan_hexdump

master
leitner 7 years ago
parent c8156a9841
commit 42a78bb04e

@ -8,7 +8,7 @@ size_t \fBscan_base64\fP(const char *\fIsrc\fR,char *\fIdest\fR,size_t* \fIdestl
.SH DESCRIPTION .SH DESCRIPTION
scan_base64 decodes base64 encoded data from src into dest. scan_base64 decodes base64 encoded data from src into dest.
It will stop when it encountes any non-valid input characters. It will stop when it encounters any non-valid input characters.
It will then write the number of decoded bytes in dest into *destlen, It will then write the number of decoded bytes in dest into *destlen,
and return the number of bytes decoded from src. and return the number of bytes decoded from src.

@ -12,7 +12,7 @@ and +, which can cause problems in URLs, so base64url uses - and _
instead; also base64url does not use = padding at the end). instead; also base64url does not use = padding at the end).
scan_base64url decodes base64url encoded data from src into dest. scan_base64url decodes base64url encoded data from src into dest.
It will stop when it encountes any non-valid input characters. It will stop when it encounters any non-valid input characters.
It will then write the number of decoded bytes in dest into *destlen, It will then write the number of decoded bytes in dest into *destlen,
and return the number of bytes decoded from src. and return the number of bytes decoded from src.

@ -0,0 +1,28 @@
.TH scan_hexdump 3
.SH NAME
scan_hexdump \- decode hexdump data
.SH SYNTAX
.B #include <libowfat/textcode.h>
size_t \fBscan_hexdump\fP(const char *\fIsrc\fR,char *\fIdest\fR,size_t* \fIdestlen\fR);
.SH DESCRIPTION
scan_hexdump decodes hexdump data from src into dest.
It will stop when it encounters any non-valid input characters.
It will then write the number of decoded bytes in dest into *destlen,
and return the number of bytes decoded from src.
Note that real world hexdump data is sometimes permitted to
contain whitespace characters or new lines. This function will not allow
those and return the decoded data until then.
dest can be NULL. destlen can be NULL.
.SH "RETURN VALUE"
scan_hexdump returns the number of bytes successfully scanned and
processed from src.
.SH EXAMPLES
scan_hexdump("302e",buf,&i) -> return 4, i=2, buf="0."
.SH "SEE ALSO"
scan_xlong(3), scan_8long(3), fmt_ulong(3)

@ -7,14 +7,31 @@ size_t scan_hexdump(const char *src,char *dest,size_t *destlen) {
size_t written=0,i; size_t written=0,i;
for (i=0; s[i]; ++i) { for (i=0; s[i]; ++i) {
int j=scan_fromhex(s[i]); int j=scan_fromhex(s[i]);
unsigned char k;
if (j<0) break; if (j<0) break;
dest[written]=j<<4; k=j<<4;
j=scan_fromhex(s[i+1]); j=scan_fromhex(s[i+1]);
if (j<0) break; if (j<0) break;
dest[written]|=j; if (dest) dest[written]=k|j;
++i; ++i;
++written; ++written;
} }
*destlen=written; if (destlen) *destlen=written;
return i; return i;
} }
#ifdef UNITTEST
#include <assert.h>
#include <string.h>
#undef UNITTEST
#include <scan/scan_fromhex.c>
int main() {
char buf[100];
size_t l;
memset(buf,0,sizeof(buf));
assert(scan_hexdump("0123456789abcdef",buf,&l)==16 && l==8 && !memcmp(buf,"\x01\x23\x45\x67\x89\xab\xcd\xef",9));
memset(buf,'?',sizeof(buf));
assert(scan_hexdump("0",buf,&l)==0 && l==0 && buf[0]=='?');
}
#endif

@ -8,7 +8,7 @@ size_t \fBscan_uuencoded\fP(const char *\fIsrc\fR,char *\fIdest\fR,size_t* \fIde
.SH DESCRIPTION .SH DESCRIPTION
scan_uuencoded decodes uuencoded data from src into dest. scan_uuencoded decodes uuencoded data from src into dest.
It will stop when it encountes any non-valid input characters. It will stop when it encounters any non-valid input characters.
It will then write the number of decoded bytes in dest into *destlen, It will then write the number of decoded bytes in dest into *destlen,
and return the number of bytes decoded from src. and return the number of bytes decoded from src.

Loading…
Cancel
Save