summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJo-Philipp Wich <jo@mein.io>2022-05-19 20:15:28 +0200
committerGitHub <noreply@github.com>2022-05-19 20:15:28 +0200
commitf7f6966044a6d705fe60b091c4994931e4c8241c (patch)
treef62000eb23addab8b505fb8bf94ce8dcd6e54926
parent9a724238c27dec032fe2ea75c4975718b0857f98 (diff)
parent8da140fd5548cfab0a2e945091ec78416b1a0d14 (diff)
Merge pull request #76 from jow-/hexcodec
lib: introduce hexenc() and hexdec()
-rw-r--r--README.md29
-rw-r--r--lib.c94
-rw-r--r--tests/custom/03_stdlib/56_hexdec29
-rw-r--r--tests/custom/03_stdlib/57_hexenc24
4 files changed, 176 insertions, 0 deletions
diff --git a/README.md b/README.md
index 633c1b0..dbbd463 100644
--- a/README.md
+++ b/README.md
@@ -1473,3 +1473,32 @@ does not implement this clock type.
clock(); // [ 1647954926, 798269464 ]
clock(true); // [ 474751, 527959975 ]
```
+
+#### 6.70. `hexdec(hexstring[, skipchars])`
+
+The `hexdec()` function decodes the given hexadecimal digit string into
+a byte string, optionally skipping specified characters.
+
+If the characters to skip are not specified, a default of `" \t\n"` is
+used.
+
+Returns null if the input string contains invalid characters or an uneven
+amount of hex digits.
+
+Returns the decoded byte string on success.
+
+```javascript
+hexdec("48656c6c6f20776f726c64210a"); // "Hello world!\n"
+hexdec("44:55:66:77:33:44", ":"); // "DUfw3D"
+```
+
+#### 6.71. `hexenc(val)`
+
+The `hexenc()` function encodes the given byte string into a hexadecimal
+digit string, converting the input value to a string if needed.
+
+Returns the encoded hexadecimal digit string.
+
+```javascript
+hexenc("Hello world!\n"); // "48656c6c6f20776f726c64210a"
+```
diff --git a/lib.c b/lib.c
index 126badc..f0263c2 100644
--- a/lib.c
+++ b/lib.c
@@ -3352,6 +3352,98 @@ uc_clock(uc_vm_t *vm, size_t nargs)
return res;
}
+static uc_value_t *
+uc_hexenc(uc_vm_t *vm, size_t nargs)
+{
+ const char *hex = "0123456789abcdef";
+ uc_value_t *input = uc_fn_arg(0);
+ uc_stringbuf_t *buf;
+ size_t off, len;
+ uint8_t byte;
+
+ if (!input)
+ return NULL;
+
+ buf = ucv_stringbuf_new();
+ off = printbuf_length(buf);
+
+ ucv_to_stringbuf(vm, buf, input, false);
+
+ len = printbuf_length(buf) - off;
+
+ /* memset the last expected output char to grow the output buffer */
+ printbuf_memset(buf, off + len * 2, 0, 1);
+
+ /* translate string into hex back to front to reuse the same buffer */
+ while (len > 0) {
+ byte = buf->buf[--len + off];
+ buf->buf[off + len * 2 + 0] = hex[byte / 16];
+ buf->buf[off + len * 2 + 1] = hex[byte % 16];
+ }
+
+ /* do not include sentinel `\0` in string length */
+ buf->bpos--;
+
+ return ucv_stringbuf_finish(buf);
+}
+
+static inline uint8_t
+hexval(unsigned char c, bool lo)
+{
+ return ((c > '9') ? (c - 'a') + 10 : c - '0') << (lo ? 0 : 4);
+}
+
+static uc_value_t *
+uc_hexdec(uc_vm_t *vm, size_t nargs)
+{
+ uc_value_t *input = uc_fn_arg(0);
+ uc_value_t *skip = uc_fn_arg(1);
+ size_t len, off, n, i;
+ uc_stringbuf_t *buf;
+ unsigned char *p;
+ const char *s;
+
+ if (ucv_type(input) != UC_STRING)
+ return NULL;
+
+ if (skip && ucv_type(skip) != UC_STRING)
+ return NULL;
+
+ p = (unsigned char *)ucv_string_get(input);
+ len = ucv_string_length(input);
+
+ s = skip ? (const char *)ucv_string_get(skip) : " \t\n";
+
+ for (i = 0, n = 0; i < len; i++) {
+ if (isxdigit(p[i]))
+ n++;
+ else if (!s || !strchr(s, p[i]))
+ return NULL;
+ }
+
+ if (n & 1)
+ return NULL;
+
+ buf = ucv_stringbuf_new();
+ off = printbuf_length(buf);
+
+ /* preallocate the output buffer */
+ printbuf_memset(buf, off, 0, n / 2 + 1);
+
+ for (i = 0, n = 0; i < len; i++) {
+ if (!isxdigit(p[i]))
+ continue;
+
+ buf->buf[off + (n >> 1)] |= hexval(p[i] | 32, n & 1);
+ n++;
+ }
+
+ /* do not include sentinel `\0` in string length */
+ buf->bpos--;
+
+ return ucv_stringbuf_finish(buf);
+}
+
const uc_function_list_t uc_stdlib_functions[] = {
{ "chr", uc_chr },
@@ -3417,6 +3509,8 @@ const uc_function_list_t uc_stdlib_functions[] = {
{ "timelocal", uc_timelocal },
{ "timegm", uc_timegm },
{ "clock", uc_clock },
+ { "hexdec", uc_hexdec },
+ { "hexenc", uc_hexenc },
};
diff --git a/tests/custom/03_stdlib/56_hexdec b/tests/custom/03_stdlib/56_hexdec
new file mode 100644
index 0000000..cb842ca
--- /dev/null
+++ b/tests/custom/03_stdlib/56_hexdec
@@ -0,0 +1,29 @@
+The `hexdec()` function decodes the given hexadecimal digit string into
+a byte string, optionally skipping specified characters.
+
+Returns null if the input string contains invalid characters or an uneven
+amount of hex digits.
+
+Returns the decoded byte string on success.
+
+-- Testcase --
+{%
+ printf("%.J\n", [
+ hexdec("44 55 66 77 33 44\n"), // whitespace is skipped by default
+ hexdec("44-55-66:77-33-44", ":-"), // skip specified characters
+ hexdec("abc"), // error; uneven amount of digits
+ hexdec("ab cd !"), // error; non-whitespace, non-hex, non-skipped char
+ hexdec(1234), // error; non-string input
+ ]);
+%}
+-- End --
+
+-- Expect stdout --
+[
+ "DUfw3D",
+ "DUfw3D",
+ null,
+ null,
+ null
+]
+-- End --
diff --git a/tests/custom/03_stdlib/57_hexenc b/tests/custom/03_stdlib/57_hexenc
new file mode 100644
index 0000000..235ad66
--- /dev/null
+++ b/tests/custom/03_stdlib/57_hexenc
@@ -0,0 +1,24 @@
+The `hexenc()` function encodes the given byte string into a hexadecimal
+digit string, converting the input value to a string if needed.
+
+Returns the encoded hexadecimal digit string.
+
+-- Testcase --
+{%
+ printf("%.J\n", [
+ hexenc("Hello world!\n"), // encoding a simple string
+ hexenc(""), // empty input -> empty output
+ hexenc([1, 2, 3]), // implicit stringification
+ hexenc(null), // null input -> null output
+ ]);
+%}
+-- End --
+
+-- Expect stdout --
+[
+ "48656c6c6f20776f726c64210a",
+ "",
+ "5b20312c20322c2033205d",
+ null
+]
+-- End --