summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix von Leitner <felix-libowfat@fefe.de>2017-07-30 13:50:12 +0000
committerFelix von Leitner <felix-libowfat@fefe.de>2017-07-30 13:50:12 +0000
commita0f4bc85207ee98cc951cbf7ba133b56047a6024 (patch)
tree6948146ad986ecc8aaea6c78c06707920aa306df
parentefafd510b85cff9ad2db303ec4baf1241825f941 (diff)
downloadlibowfat-a0f4bc85207ee98cc951cbf7ba133b56047a6024.tar.gz
libowfat-a0f4bc85207ee98cc951cbf7ba133b56047a6024.zip
man page and unit tests for scan_hexdump
-rw-r--r--textcode/scan_base64.32
-rw-r--r--textcode/scan_base64url.32
-rw-r--r--textcode/scan_hexdump.328
-rw-r--r--textcode/scan_hexdump.c23
-rw-r--r--textcode/scan_uuencoded.32
5 files changed, 51 insertions, 6 deletions
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 <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)
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 <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
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.