[njs] Added hex encoding for byte strings.

Dmitry Volyntsev xeioex at nginx.com
Fri Mar 30 12:47:10 UTC 2018


details:   http://hg.nginx.org/njs/rev/c685137c4e57
branches:  
changeset: 473:c685137c4e57
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Fri Mar 30 15:46:38 2018 +0300
description:
Added hex encoding for byte strings.

diffstat:

 njs/njs_string.c |  31 +++++++++++++++++++++++++++++++
 njs/njs_string.h |   2 ++
 2 files changed, 33 insertions(+), 0 deletions(-)

diffs (53 lines):

diff -r 36ef67f13052 -r c685137c4e57 njs/njs_string.c
--- a/njs/njs_string.c	Thu Mar 29 16:38:02 2018 +0300
+++ b/njs/njs_string.c	Fri Mar 30 15:46:38 2018 +0300
@@ -244,6 +244,37 @@ njs_string_alloc(njs_vm_t *vm, njs_value
 }
 
 
+nxt_noinline njs_ret_t
+njs_string_hex(njs_vm_t *vm, njs_value_t *value, const nxt_str_t *src)
+{
+    u_char        *p, c;
+    size_t        len;
+    nxt_uint_t    i;
+    const u_char  *start;
+
+    static const u_char  hex[16] = "0123456789abcdef";
+
+    len = src->length;
+    start = src->start;
+
+    p = njs_string_alloc(vm, value, len * 2, len * 2);
+
+    if (nxt_fast_path(p != NULL)) {
+        for (i = 0; i < len; i++) {
+            c = start[i];
+            *p++ = hex[c >> 4];
+            *p++ = hex[c & 0x0f];
+        }
+
+        return NXT_OK;
+    }
+
+    njs_memory_error(vm);
+
+    return NXT_ERROR;
+}
+
+
 void
 njs_string_copy(njs_value_t *dst, njs_value_t *src)
 {
diff -r 36ef67f13052 -r c685137c4e57 njs/njs_string.h
--- a/njs/njs_string.h	Thu Mar 29 16:38:02 2018 +0300
+++ b/njs/njs_string.h	Fri Mar 30 15:46:38 2018 +0300
@@ -122,6 +122,8 @@ njs_string_length(njs_utf8_t utf8, u_cha
 
 njs_ret_t njs_string_new(njs_vm_t *vm, njs_value_t *value, const u_char *start,
     uint32_t size, uint32_t length);
+njs_ret_t njs_string_hex(njs_vm_t *vm, njs_value_t *value,
+    const nxt_str_t *src);
 void njs_string_copy(njs_value_t *dst, njs_value_t *src);
 njs_ret_t njs_string_validate(njs_vm_t *vm, njs_string_prop_t *string,
     njs_value_t *value);


More information about the nginx-devel mailing list