[PATCH 1 of 2] Core: add function to decode hexadecimal strings

Nate Karstens nate.karstens at garmin.com
Fri Jul 28 18:45:36 UTC 2017


# HG changeset patch
# User Nate Karstens <nate.karstens at garmin.com>
# Date 1501265787 18000
#      Fri Jul 28 13:16:27 2017 -0500
# Node ID 7e10e1dc1fae0f538a0a74fd6783f9b33a905933
# Parent  72d3aefc2993a639cc8cddc94aefc7c4390ee252
Core: add function to decode hexadecimal strings.

Adds functionality to convert a hexadecimal string into binary data.
This will be used to decode PSKs stored in hexadecimal representation.

Signed-off-by: Nate Karstens <nate.karstens at garmin.com>

diff -r 72d3aefc2993 -r 7e10e1dc1fae src/core/ngx_string.c
--- a/src/core/ngx_string.c     Wed Jul 26 13:13:51 2017 +0300
+++ b/src/core/ngx_string.c     Fri Jul 28 13:16:27 2017 -0500
@@ -1104,6 +1104,54 @@ ngx_hextoi(u_char *line, size_t n)
 }


+ngx_int_t
+ngx_hex_decode(u_char *dst, size_t dlen, u_char *src, size_t slen)
+{
+    u_char     c, ch;
+    ngx_int_t  len;
+
+    if ((slen & 1) || (dlen < (slen / 2))) {
+        return NGX_ERROR;
+    }
+
+    len = slen / 2;
+
+    while (slen > 0) {
+        ch = *src;
+        c = (u_char) (ch | 0x20);
+
+        if (ch >= '0' && ch <= '9') {
+            *dst = ch - '0';
+        } else if (c >= 'a' && c <= 'f') {
+            *dst = c - 'a' + 10;
+        } else {
+            return NGX_ERROR;
+        }
+
+        *dst <<= 4;
+        src++;
+
+        ch = *src;
+        c = (u_char) (ch | 0x20);
+
+        if (ch >= '0' && ch <= '9') {
+            *dst |= ch - '0';
+        } else if (c >= 'a' && c <= 'f') {
+            *dst |= c - 'a' + 10;
+        } else {
+            return NGX_ERROR;
+        }
+
+        dst++;
+        src++;
+
+        slen -= 2;
+    }
+
+    return len;
+}
+
+
 u_char *
 ngx_hex_dump(u_char *dst, u_char *src, size_t len)
 {
diff -r 72d3aefc2993 -r 7e10e1dc1fae src/core/ngx_string.h
--- a/src/core/ngx_string.h     Wed Jul 26 13:13:51 2017 +0300
+++ b/src/core/ngx_string.h     Fri Jul 28 13:16:27 2017 -0500
@@ -176,6 +176,7 @@ off_t ngx_atoof(u_char *line, size_t n);
 time_t ngx_atotm(u_char *line, size_t n);
 ngx_int_t ngx_hextoi(u_char *line, size_t n);

+ngx_int_t ngx_hex_decode(u_char *dst, size_t dlen, u_char *src, size_t slen);
 u_char *ngx_hex_dump(u_char *dst, u_char *src, size_t len);



________________________________

CONFIDENTIALITY NOTICE: This email and any attachments are for the sole use of the intended recipient(s) and contain information that may be Garmin confidential and/or Garmin legally privileged. If you have received this email in error, please notify the sender by reply email and delete the message. Any disclosure, copying, distribution or use of this communication (including attachments) by someone other than the intended recipient is prohibited. Thank you.


More information about the nginx-devel mailing list