summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix von Leitner <felix-libowfat@fefe.de>2017-07-30 12:42:07 +0000
committerFelix von Leitner <felix-libowfat@fefe.de>2017-07-30 12:42:07 +0000
commitfdb61c53015c07995ebcef6be01eed1b4bce581c (patch)
tree4b32d0e8bbc571a1f9fef76a3b9a8a87529df685
parent4ba71a754011dad0990cec867f6eac6dac2f8a81 (diff)
downloadlibowfat-fdb61c53015c07995ebcef6be01eed1b4bce581c.tar.gz
libowfat-fdb61c53015c07995ebcef6be01eed1b4bce581c.zip
add unit tests and man page for scan_ldapescape
-rw-r--r--textcode/scan_ldapescape.333
-rw-r--r--textcode/scan_ldapescape.c24
2 files changed, 53 insertions, 4 deletions
diff --git a/textcode/scan_ldapescape.3 b/textcode/scan_ldapescape.3
new file mode 100644
index 0000000..3aa38d5
--- /dev/null
+++ b/textcode/scan_ldapescape.3
@@ -0,0 +1,33 @@
+.TH scan_ldapescape 3
+.SH NAME
+scan_ldapescape \- parse LDIF escaped string
+.SH SYNTAX
+.B #include <libowfat/textcode.h>
+
+size_t \fBscan_ldapescape\fP(const char *\fIsrc\fR,char *\fIdest\fR,size_t* \fIdestlen\fR);
+
+.SH DESCRIPTION
+scan_ldapescape parses LDIF escaped text from src into dest.
+Parsing stops at the 0 terminator or invalid input characters.
+
+This function will turn backslash escaped characters into their binary
+equivalent, i.e. \0a to new-line and \5c to backslash.
+
+scan_cescape will then write the number of bytes in dest into *destlen,
+and return the number of bytes decoded from src.
+
+dest can be NULL. destlen can be NULL.
+
+To make sure dest is large enough, either allocate strlen(src)+1 bytes
+or call scan_ldapescape twice, the first time with dest == NULL (*destlen
+will still be written).
+
+.SH "RETURN VALUE"
+scan_ldapescape returns the number of bytes successfully parsed
+from src.
+
+.SH EXAMPLES
+scan_ldapescape("test\5C\\");",buf,&i) -> return 7, i=5, buf="test\\" (C notation).
+
+.SH "SEE ALSO"
+fmt_ldapescape(3), scan_cescape(3), scan_ldapescape(3)
diff --git a/textcode/scan_ldapescape.c b/textcode/scan_ldapescape.c
index 6dacaa3..464abf7 100644
--- a/textcode/scan_ldapescape.c
+++ b/textcode/scan_ldapescape.c
@@ -9,16 +9,32 @@ size_t scan_ldapescape(const char *src,char *dest,size_t *destlen) {
if (s[i]=='\\') {
int j=scan_fromhex(s[i+1]);
if (j<0) break;
- dest[written]=j<<4;
+ if (dest) dest[written]=j<<4;
j=scan_fromhex(s[i+2]);
if (j<0) break;
- dest[written]|=j;
+ if (dest) dest[written]|=j;
i+=2;
} else {
- dest[written]=s[i];
+ if (dest) dest[written]=s[i];
}
++written;
}
- *destlen=written;
+ if (destlen) *destlen=written;
return i;
}
+
+#ifdef UNITTEST
+#include <assert.h>
+#undef UNITTEST
+#include <scan/scan_fromhex.c>
+
+int main() {
+ size_t dl;
+ char buf[100];
+ assert(scan_ldapescape("test\\n\");",buf,&dl)==4 && dl==4 && !memcmp(buf,"test",4));
+ /* check hex escaping */
+ assert(scan_ldapescape("test\\0afoo",buf,&dl)==10 && dl==8 && !memcmp(buf,"test\nfoo",8));
+ /* check that short sequences are rejected */
+ assert(scan_ldapescape("test\\ax",buf,&dl)==4 && dl==4 && !memcmp(buf,"test",4));
+}
+#endif