From fc113d64f580c1788a00ec27cae61edf6e4ca9d9 Mon Sep 17 00:00:00 2001 From: Deomid Ryabkov Date: Thu, 9 Feb 2017 10:25:56 +0000 Subject: [PATCH] Add mg_hexdumpf: mg_hexdump that outputs to a file PUBLISHED_FROM=f0fe58c9f01ef0c7b491ed0e5f51b983e4119507 --- docs/c-api/util.h/intro.md | 1 + docs/c-api/util.h/mg_hexdumpf.md | 10 ++++++++++ mongoose.c | 27 ++++++++++++++++++++++----- mongoose.h | 3 +++ 4 files changed, 36 insertions(+), 5 deletions(-) create mode 100644 docs/c-api/util.h/mg_hexdumpf.md diff --git a/docs/c-api/util.h/intro.md b/docs/c-api/util.h/intro.md index 99d4e40750..9b30d68009 100644 --- a/docs/c-api/util.h/intro.md +++ b/docs/c-api/util.h/intro.md @@ -10,6 +10,7 @@ items: - { name: mg_fopen.md } - { name: mg_hexdump.md } - { name: mg_hexdump_connection.md } + - { name: mg_hexdumpf.md } - { name: mg_is_big_endian.md } - { name: mg_match_prefix.md } - { name: mg_mbuf_append_base64.md } diff --git a/docs/c-api/util.h/mg_hexdumpf.md b/docs/c-api/util.h/mg_hexdumpf.md new file mode 100644 index 0000000000..dcb14ece70 --- /dev/null +++ b/docs/c-api/util.h/mg_hexdumpf.md @@ -0,0 +1,10 @@ +--- +title: "mg_hexdumpf()" +decl_name: "mg_hexdumpf" +symbol_kind: "func" +signature: | + void mg_hexdumpf(FILE *fp, const void *buf, int len); +--- + +Same as mg_hexdump, but with output going to file instead of a buffer. + diff --git a/mongoose.c b/mongoose.c index b5f1cbc74c..61d200af8a 100644 --- a/mongoose.c +++ b/mongoose.c @@ -9207,7 +9207,8 @@ void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len, } #if MG_ENABLE_HEXDUMP -int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { +static int mg_hexdump_n(const void *buf, int len, char *dst, int dst_len, + int offset) { const unsigned char *p = (const unsigned char *) buf; char ascii[17] = ""; int i, idx, n = 0; @@ -9216,7 +9217,7 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { idx = i % 16; if (idx == 0) { if (i > 0) n += snprintf(dst + n, MAX(dst_len - n, 0), " %s\n", ascii); - n += snprintf(dst + n, MAX(dst_len - n, 0), "%04x ", i); + n += snprintf(dst + n, MAX(dst_len - n, 0), "%04x ", i + offset); } if (dst_len - n < 0) { return n; @@ -9227,11 +9228,27 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { } while (i++ % 16) n += snprintf(dst + n, MAX(dst_len - n, 0), "%s", " "); - n += snprintf(dst + n, MAX(dst_len - n, 0), " %s\n\n", ascii); + n += snprintf(dst + n, MAX(dst_len - n, 0), " %s\n", ascii); return n; } +int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { + return mg_hexdump_n(buf, len, dst, dst_len, 0); +} + +void mg_hexdumpf(FILE *fp, const void *buf, int len) { + char tmp[80]; + int offset = 0, n; + while (len > 0) { + n = (len < 16 ? len : 16); + mg_hexdump_n(((const char *) buf) + offset, n, tmp, sizeof(tmp), offset); + fputs(tmp, fp); + offset += n; + len -= n; + } +} + void mg_hexdump_connection(struct mg_connection *nc, const char *path, const void *buf, int num_bytes, int ev) { FILE *fp = NULL; @@ -13535,8 +13552,8 @@ struct mg_lwip_conn_state { size_t rx_offset; /* Offset within the first pbuf (if partially consumed) */ /* Last SSL write size, for retries. */ int last_ssl_write_size; - int recv_pending; /* Whether MG_SIG_RECV is already pending for this - connection */ + /* Whether MG_SIG_RECV is already pending for this connection */ + int recv_pending; }; enum mg_sig_type { diff --git a/mongoose.h b/mongoose.h index 9b54a41aef..308b60c9f2 100644 --- a/mongoose.h +++ b/mongoose.h @@ -3938,6 +3938,9 @@ void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len, */ int mg_hexdump(const void *buf, int len, char *dst, int dst_len); +/* Same as mg_hexdump, but with output going to file instead of a buffer. */ +void mg_hexdumpf(FILE *fp, const void *buf, int len); + /* * Generates human-readable hexdump of the data sent or received by the * connection. `path` is a file name where hexdump should be written.