Skip to content

Commit

Permalink
Add mg_hexdumpf: mg_hexdump that outputs to a file
Browse files Browse the repository at this point in the history
PUBLISHED_FROM=f0fe58c9f01ef0c7b491ed0e5f51b983e4119507
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Feb 9, 2017
1 parent 6f6b12b commit fc113d6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/c-api/util.h/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
10 changes: 10 additions & 0 deletions docs/c-api/util.h/mg_hexdumpf.md
Original file line number Diff line number Diff line change
@@ -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.

27 changes: 22 additions & 5 deletions mongoose.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions mongoose.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit fc113d6

Please sign in to comment.