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

Nate Karstens nate.karstens at garmin.com
Thu Aug 24 02:19:39 UTC 2017


# HG changeset patch
# User Nate Karstens <nate.karstens at garmin.com>
# Date 1503540018 18000
#      Wed Aug 23 21:00:18 2017 -0500
# Node ID 17c038b56051f922ec440d40e23e8d1b23d835df
# Parent  c7d4017c8876af6d8570e400320537d7d39e9578
[PATCH 1 of 4] 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 c7d4017c8876 -r 17c038b56051 src/core/ngx_string.c
--- a/src/core/ngx_string.c     Tue Aug 22 21:22:59 2017 +0300
+++ b/src/core/ngx_string.c     Wed Aug 23 21:00:18 2017 -0500
@@ -1118,6 +1118,57 @@ ngx_hex_dump(u_char *dst, u_char *src, s
 }


+ngx_int_t
+ngx_hex_decode(u_char *dst, u_char *src, size_t len)
+{
+    u_char     ch, decoded;
+
+    if (len & 1) {
+        return NGX_ERROR;
+    }
+
+    while (len > 0) {
+        ch = *src++;
+        len--;
+
+        if (len & 1) {
+
+            if (ch >= '0' && ch <= '9') {
+                decoded = ch - '0';
+                continue;
+            }
+
+            ch |= 0x20;
+
+            if (ch >= 'a' && ch <= 'f') {
+                decoded = ch - 'a' + 10;
+                continue;
+            }
+
+            return NGX_ERROR;
+
+        } else {
+
+            if (ch >= '0' && ch <= '9') {
+                *dst++ = (u_char) ((decoded << 4) + ch - '0');
+                continue;
+            }
+
+            ch |= 0x20;
+
+            if (ch >= 'a' && ch <= 'f') {
+                *dst++ = (u_char) ((decoded << 4) + ch - 'a' + 10);
+                continue;
+            }
+
+            return NGX_ERROR;
+        }
+    }
+
+    return NGX_OK;
+}
+
+
 void
 ngx_encode_base64(ngx_str_t *dst, ngx_str_t *src)
 {
diff -r c7d4017c8876 -r 17c038b56051 src/core/ngx_string.h
--- a/src/core/ngx_string.h     Tue Aug 22 21:22:59 2017 +0300
+++ b/src/core/ngx_string.h     Wed Aug 23 21:00:18 2017 -0500
@@ -177,6 +177,7 @@ time_t ngx_atotm(u_char *line, size_t n)
 ngx_int_t ngx_hextoi(u_char *line, size_t n);

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


 #define ngx_base64_encoded_length(len)  (((len + 2) / 3) * 4)

________________________________

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