diff --git a/textcode/scan_base64.3 b/textcode/scan_base64.3 index 64e0abc..339ed87 100644 --- a/textcode/scan_base64.3 +++ b/textcode/scan_base64.3 @@ -8,7 +8,7 @@ size_t \fBscan_base64\fP(const char *\fIsrc\fR,char *\fIdest\fR,size_t* \fIdestl .SH DESCRIPTION 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, and return the number of bytes decoded from src. diff --git a/textcode/scan_base64url.3 b/textcode/scan_base64url.3 index c0ccd0a..77867b7 100644 --- a/textcode/scan_base64url.3 +++ b/textcode/scan_base64url.3 @@ -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). 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, and return the number of bytes decoded from src. diff --git a/textcode/scan_hexdump.3 b/textcode/scan_hexdump.3 new file mode 100644 index 0000000..98559fc --- /dev/null +++ b/textcode/scan_hexdump.3 @@ -0,0 +1,28 @@ +.TH scan_hexdump 3 +.SH NAME +scan_hexdump \- decode hexdump data +.SH SYNTAX +.B #include + +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) diff --git a/textcode/scan_hexdump.c b/textcode/scan_hexdump.c index c2bf946..cd8064e 100644 --- a/textcode/scan_hexdump.c +++ b/textcode/scan_hexdump.c @@ -7,14 +7,31 @@ size_t scan_hexdump(const char *src,char *dest,size_t *destlen) { size_t written=0,i; for (i=0; s[i]; ++i) { int j=scan_fromhex(s[i]); + unsigned char k; if (j<0) break; - dest[written]=j<<4; + k=j<<4; j=scan_fromhex(s[i+1]); if (j<0) break; - dest[written]|=j; + if (dest) dest[written]=k|j; ++i; ++written; } - *destlen=written; + if (destlen) *destlen=written; return i; } + +#ifdef UNITTEST +#include +#include +#undef UNITTEST +#include + +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 diff --git a/textcode/scan_uuencoded.3 b/textcode/scan_uuencoded.3 index f28efdb..2101423 100644 --- a/textcode/scan_uuencoded.3 +++ b/textcode/scan_uuencoded.3 @@ -8,7 +8,7 @@ size_t \fBscan_uuencoded\fP(const char *\fIsrc\fR,char *\fIdest\fR,size_t* \fIde .SH DESCRIPTION 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, and return the number of bytes decoded from src.