ongrep

A cleaned up fork of ngrep for OpenBSD
git clone git://git.sgregoratto.me/ongrep
Log | Files | Refs | README | LICENSE

commit c0dabd9a708d14109bf09c852f7d5e39a6ef2aa1
parent adbad1fc4007a72b179d2f6f9bf3bccfdbb993f6
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date:   Sun, 14 Jun 2020 21:43:57 +1000

move len checks from the dump_funcs to the caller

Diffstat:
Mngrep.c | 141++++++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 67 insertions(+), 74 deletions(-)

diff --git a/ngrep.c b/ngrep.c @@ -737,7 +737,7 @@ dump_packet(struct pcap_pkthdr *h, u_char *p, uint8_t proto, else printf(" #%u\n", seen_frames); - if (quiet < 3) + if (quiet < 3 && len > 0) dump_func(data, len, match_index, match_size); if (pd_dump) @@ -816,116 +816,109 @@ blank_match_func(unsigned char *data, uint32_t len, uint16_t *mindex, void dump_byline(unsigned char *data, uint32_t len, uint16_t mindex, uint16_t msize) { - if (len > 0) { - const unsigned char *s = data; - uint8_t should_hilite = (msize && enable_hilite); - unsigned char *hilite_start = data + mindex; - unsigned char *hilite_end = hilite_start + msize; - - while (s < data + len) { - if (should_hilite && s == hilite_start) - printf("%s", ANSI_hilite); + const unsigned char *s = data; + uint8_t should_hilite = (msize && enable_hilite); + unsigned char *hilite_start = data + mindex; + unsigned char *hilite_end = hilite_start + msize; - printf("%c", - (*s == '\n' || isprint(*s)) ? *s : nonprint_char); - s++; + while (s < data + len) { + if (should_hilite && s == hilite_start) + printf("%s", ANSI_hilite); - if (should_hilite && s == hilite_end) - printf("%s", ANSI_off); - } + printf("%c", + (*s == '\n' || isprint(*s)) ? *s : nonprint_char); + s++; - printf("\n"); + if (should_hilite && s == hilite_end) + printf("%s", ANSI_off); } + + printf("\n"); } void dump_unwrapped(unsigned char *data, uint32_t len, uint16_t mindex, uint16_t msize) { - if (len > 0) { - const unsigned char *s = data; - uint8_t should_hilite = (msize && enable_hilite); - unsigned char * hilite_start = data + mindex; - unsigned char * hilite_end = hilite_start + msize; - - while (s < data + len) { - if (should_hilite && s == hilite_start) - printf("%s", ANSI_hilite); + const unsigned char *s = data; + uint8_t should_hilite = (msize && enable_hilite); + unsigned char * hilite_start = data + mindex; + unsigned char * hilite_end = hilite_start + msize; - printf("%c", isprint(*s) ? *s : nonprint_char); - s++; + while (s < data + len) { + if (should_hilite && s == hilite_start) + printf("%s", ANSI_hilite); - if (should_hilite && s == hilite_end) - printf("%s", ANSI_off); - } + printf("%c", isprint(*s) ? *s : nonprint_char); + s++; - printf("\n"); + if (should_hilite && s == hilite_end) + printf("%s", ANSI_off); } + + printf("\n"); } void dump_formatted(unsigned char *data, uint32_t len, uint16_t mindex, uint16_t msize) { - if (len > 0) { - uint8_t should_hilite = (msize && enable_hilite); - unsigned char *str = data; - uint8_t hiliting = 0; - uint8_t width = show_hex ? 16 : (ws_col - 5); - uint32_t i = 0, j = 0; - - while (i < len) { - printf(" "); - - if (show_hex) { - for (j = 0; j < width; j++) { - if (should_hilite - && (mindex <= (i + j) - && (i + j) < mindex + msize)) { - hiliting = 1; - printf("%s", ANSI_hilite); - } - - if (i + j < len) - printf("%02x ", str[j]); - else - printf(" "); - - if ((j + 1) % (width / 2) == 0) - printf(" "); - - if (hiliting) { - hiliting = 0; - printf("%s", ANSI_off); - } - } - } + uint8_t should_hilite = (msize && enable_hilite); + unsigned char *str = data; + uint8_t hiliting = 0; + uint8_t width = show_hex ? 16 : (ws_col - 5); + uint32_t i = 0, j = 0; + + while (i < len) { + printf(" "); + if (show_hex) { for (j = 0; j < width; j++) { - if (should_hilite && mindex <= (i + j) - && (i + j) < mindex + msize) { + if (should_hilite && mindex <= i + j && + i + j < mindex + msize) { hiliting = 1; printf("%s", ANSI_hilite); } if (i + j < len) - printf("%c", isprint(str[j]) - ? str[j] - : nonprint_char); + printf("%02x ", str[j]); else - printf(" "); + printf(" "); + + if ((j + 1) % (width / 2) == 0) + printf(" "); if (hiliting) { hiliting = 0; printf("%s", ANSI_off); } } + } - str += width; - i += j; + for (j = 0; j < width; j++) { + if (should_hilite && mindex <= i + j && + i + j < mindex + msize) { + hiliting = 1; + printf("%s", ANSI_hilite); + } - printf("\n"); + if (i + j < len) + printf("%c", isprint(str[j]) + ? str[j] + : nonprint_char); + else + printf(" "); + + if (hiliting) { + hiliting = 0; + printf("%s", ANSI_off); + } } + + str += width; + i += j; + + printf("\n"); } }