]> www.infradead.org Git - users/dwmw2/openconnect.git/commitdiff
Introduced buf_append_hex()
authorNikos Mavrogiannopoulos <nmav@redhat.com>
Tue, 22 Nov 2016 12:40:57 +0000 (13:40 +0100)
committerDavid Woodhouse <dwmw2@infradead.org>
Tue, 13 Dec 2016 10:42:11 +0000 (10:42 +0000)
That is being used by openconnect_bin2hex() for hex-encoding.

[dwmw2: Clean up buf error handling in openconnect_bin2hex()]

Signed-off-by: Nikos Mavrogiannopoulos <nmav@redhat.com>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
digest.c
dtls.c
http.c
oath.c
openconnect-internal.h

index b2064b0eb8f73a72d78fbd93be0cf189aa16bc75..06dca75b8507580858592d4a7d549a2ff4307bf2 100644 (file)
--- a/digest.c
+++ b/digest.c
@@ -67,15 +67,13 @@ static void buf_append_unq(struct oc_text_buf *buf, const char *str)
 static void buf_append_md5(struct oc_text_buf *buf, void *data, int len)
 {
        unsigned char md5[16];
-       int i;
 
        if (openconnect_md5(md5, data, len)) {
                buf->error = -EIO;
                return;
        }
 
-       for (i = 0; i < 16; i++)
-               buf_append(buf, "%02x", md5[i]);
+       buf_append_hex(buf, md5, 16);
 }
 
 int digest_authorization(struct openconnect_info *vpninfo, int proxy,
diff --git a/dtls.c b/dtls.c
index 6f464fa78bfa07b1c607eacfa41fc85def294eeb..df104ed583a9b90d9e26e3020e5f8d31837779b5 100644 (file)
--- a/dtls.c
+++ b/dtls.c
 
 char *openconnect_bin2hex(const char *prefix, const uint8_t *data, unsigned len)
 {
-       char *v;
-       unsigned plen = strlen(prefix);
-       unsigned i;
-
-       v = malloc(len*2+plen+1);
-       if (v) {
-               snprintf(v, plen+1, "%s", prefix);
-               for (i = 0; i < len; i++)
-                       sprintf(&v[i*2 + plen], "%02x", data[i]);
+       struct oc_text_buf *buf;
+       char *p = NULL;
+
+       buf = buf_alloc();
+       buf_append(buf, "%s", prefix);
+       buf_append_hex(buf, data, len);
+
+       if (!buf_error(buf)) {
+               p = buf->data;
+               buf->data = NULL;
        }
-       return v;
+       buf_free(buf);
+
+       return p;
 }
 
 static int connect_dtls_socket(struct openconnect_info *vpninfo)
diff --git a/http.c b/http.c
index 18d7da2bc8273f8970e9eb559f00407875123d92..cde4b4045fcc2a6c63b31754f75d3d7049e325f1 100644 (file)
--- a/http.c
+++ b/http.c
@@ -54,6 +54,15 @@ void buf_append_urlencoded(struct oc_text_buf *buf, char *str)
        }
 }
 
+void buf_append_hex(struct oc_text_buf *buf, const void *str, unsigned len)
+{
+       const unsigned char *data = str;
+       unsigned i;
+
+       for (i = 0; i < len; i++)
+               buf_append(buf, "%02x", (unsigned)data[i]);
+}
+
 void buf_truncate(struct oc_text_buf *buf)
 {
        if (!buf)
diff --git a/oath.c b/oath.c
index 1914c3a0042123699b561ccac12beb599498523c..8d772abb232e2b7207492641b189944cc9b4a2dc 100644 (file)
--- a/oath.c
+++ b/oath.c
@@ -489,7 +489,6 @@ static char *regen_hotp_secret(struct openconnect_info *vpninfo)
 {
        char *new_secret = NULL;
        struct oc_text_buf *buf;
-       int i;
 
        switch (vpninfo->hotp_secret_format) {
        case HOTP_SECRET_BASE32:
@@ -502,9 +501,7 @@ static char *regen_hotp_secret(struct openconnect_info *vpninfo)
        case HOTP_SECRET_HEX:
                buf = buf_alloc();
                buf_append(buf, "0x");
-               for (i=0; i < vpninfo->oath_secret_len; i++)
-                       buf_append(buf, "%02x",
-                                  (unsigned char)vpninfo->oath_secret[i]);
+               buf_append_hex(buf, vpninfo->oath_secret, vpninfo->oath_secret_len);
                break;
 
        case HOTP_SECRET_RAW:
index ae901e01fba464a89d83f9f61f3b465f9912de6f..f68c2d9175575678fefb778c6a0bb9923adf4fd3 100644 (file)
@@ -991,6 +991,7 @@ int buf_ensure_space(struct oc_text_buf *buf, int len);
 void  __attribute__ ((format (printf, 2, 3)))
        buf_append(struct oc_text_buf *buf, const char *fmt, ...);
 void buf_append_bytes(struct oc_text_buf *buf, const void *bytes, int len);
+void buf_append_hex(struct oc_text_buf *buf, const void *str, unsigned len);
 int buf_append_utf16le(struct oc_text_buf *buf, const char *utf8);
 int get_utf8char(const char **utf8);
 void buf_append_from_utf16le(struct oc_text_buf *buf, const void *utf16);