Commit fc113d64 authored by Deomid Ryabkov's avatar Deomid Ryabkov Committed by Cesanta Bot

Add mg_hexdumpf: mg_hexdump that outputs to a file

PUBLISHED_FROM=f0fe58c9f01ef0c7b491ed0e5f51b983e4119507
parent 6f6b12be
...@@ -10,6 +10,7 @@ items: ...@@ -10,6 +10,7 @@ items:
- { name: mg_fopen.md } - { name: mg_fopen.md }
- { name: mg_hexdump.md } - { name: mg_hexdump.md }
- { name: mg_hexdump_connection.md } - { name: mg_hexdump_connection.md }
- { name: mg_hexdumpf.md }
- { name: mg_is_big_endian.md } - { name: mg_is_big_endian.md }
- { name: mg_match_prefix.md } - { name: mg_match_prefix.md }
- { name: mg_mbuf_append_base64.md } - { name: mg_mbuf_append_base64.md }
......
---
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.
...@@ -9207,7 +9207,8 @@ void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len, ...@@ -9207,7 +9207,8 @@ void mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len,
} }
#if MG_ENABLE_HEXDUMP #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; const unsigned char *p = (const unsigned char *) buf;
char ascii[17] = ""; char ascii[17] = "";
int i, idx, n = 0; int i, idx, n = 0;
...@@ -9216,7 +9217,7 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { ...@@ -9216,7 +9217,7 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len) {
idx = i % 16; idx = i % 16;
if (idx == 0) { if (idx == 0) {
if (i > 0) n += snprintf(dst + n, MAX(dst_len - n, 0), " %s\n", ascii); 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) { if (dst_len - n < 0) {
return n; return n;
...@@ -9227,11 +9228,27 @@ int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { ...@@ -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", " "); 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; 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, void mg_hexdump_connection(struct mg_connection *nc, const char *path,
const void *buf, int num_bytes, int ev) { const void *buf, int num_bytes, int ev) {
FILE *fp = NULL; FILE *fp = NULL;
...@@ -13535,8 +13552,8 @@ struct mg_lwip_conn_state { ...@@ -13535,8 +13552,8 @@ struct mg_lwip_conn_state {
size_t rx_offset; /* Offset within the first pbuf (if partially consumed) */ size_t rx_offset; /* Offset within the first pbuf (if partially consumed) */
/* Last SSL write size, for retries. */ /* Last SSL write size, for retries. */
int last_ssl_write_size; int last_ssl_write_size;
int recv_pending; /* Whether MG_SIG_RECV is already pending for this /* Whether MG_SIG_RECV is already pending for this connection */
connection */ int recv_pending;
}; };
enum mg_sig_type { enum mg_sig_type {
......
...@@ -3938,6 +3938,9 @@ void mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len, ...@@ -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); 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 * Generates human-readable hexdump of the data sent or received by the
* connection. `path` is a file name where hexdump should be written. * connection. `path` is a file name where hexdump should be written.
......
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