Commit 9723ca95 authored by Nikos Mavrogiannopoulos's avatar Nikos Mavrogiannopoulos Committed by David Woodhouse

Introduced buf_append_hex()

That is being used by openconnect_bin2hex() for hex-encoding.

[dwmw2: Clean up buf error handling in openconnect_bin2hex()]
Signed-off-by: default avatarNikos Mavrogiannopoulos <>
Signed-off-by: default avatarDavid Woodhouse <>
parent 8d86a176
......@@ -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;
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,
......@@ -68,17 +68,20 @@
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;
return p;
static int connect_dtls_socket(struct openconnect_info *vpninfo)
......@@ -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)
......@@ -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) {
......@@ -502,9 +501,7 @@ static char *regen_hotp_secret(struct openconnect_info *vpninfo)
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);
......@@ -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);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment