commit 2633d4d5b0aa0fb58add270b6ded57904b8de6b1
parent 9f0a76556ac7ee316996d439e3c2881135231dce
Author: Stephen Gregoratto <dev@sgregoratto.me>
Date: Wed, 16 Sep 2020 20:53:59 +1000
Make dump_(unwrapped|byline) one function
Instead of splitting on functions, split on the char comparison.
Now we can just loop through the data and fwrite it instead of
putchar()ing and highlight checking.
While I'm here, reset the char_cmp func if the user is evil.
Diffstat:
M | ngrep.c | | | 66 | ++++++++++++++++++++++++++++-------------------------------------- |
M | ngrep.h | | | 5 | +++-- |
2 files changed, 31 insertions(+), 40 deletions(-)
diff --git a/ngrep.c b/ngrep.c
@@ -88,6 +88,7 @@ size_t match_len = 0;
match_func matcher = blank_match_func;
bool dump_single = false;
+char_cmp_func char_cmp = &isprint;
dump_func dumper = &dump_formatted;
/* BPF/Network */
@@ -245,14 +246,18 @@ main(int argc, char **argv)
re_match_word = true;
break;
case 'W':
- if (!strcasecmp(optarg, "normal"))
+ if (!strcasecmp(optarg, "normal")) {
dumper = &dump_formatted;
- else if (!strcasecmp(optarg, "byline"))
- dumper = &dump_byline;
- else if (!strcasecmp(optarg, "none"))
- dumper = &dump_unwrapped;
- else if (!strcasecmp(optarg, "single")) {
- dumper = &dump_unwrapped;
+ char_cmp = &isprint;
+ } else if (!strcasecmp(optarg, "byline")) {
+ dumper = &dump_unformatted;
+ char_cmp = &byline_cmp;
+ } else if (!strcasecmp(optarg, "none")) {
+ dumper = &dump_unformatted;
+ char_cmp = &isprint;
+ } else if (!strcasecmp(optarg, "single")) {
+ dumper = &dump_unformatted;
+ char_cmp = &isprint;
dump_single = true;
} else {
warnx("invalid wrap method: %s", optarg);
@@ -944,42 +949,27 @@ blank_match_func(UNUSED uint8_t *data, UNUSED uint32_t len, size_t *mindex,
return true;
}
-void
-dump_byline(uint8_t *data, uint32_t len, size_t mindex, size_t msize)
+inline int
+byline_cmp(int c)
{
- bool should_hl = (highlight && msize);
- uint8_t *hl_start = data + mindex;
- uint8_t *hl_end = hl_start + msize;
-
- for (uint8_t *s = data; s < data + len; s++) {
- if (should_hl) {
- if (s == hl_start)
- fputs(RED, stdout);
- else if (s == hl_end)
- fputs(RESET, stdout);
- }
- putchar((*s == '\n' || isprint(*s)) ? *s : nonprint_char);
- }
- putchar('\n');
+ return c == '\n' || isprint(c);
}
void
-dump_unwrapped(uint8_t *data, uint32_t len, size_t mindex, size_t msize)
+dump_unformatted(uint8_t *data, uint32_t len, size_t mindex, size_t msize)
{
- bool should_hl = (highlight && msize);
- uint8_t *hl_start = data + mindex;
- uint8_t *hl_end = hl_start + msize;
-
- for (uint8_t *s = data; s < data + len; s++) {
- if (should_hl) {
- if (s == hl_start)
- fputs(RED, stdout);
- else if (s == hl_end)
- fputs(RESET, stdout);
- }
- putchar(isprint(*s) ? *s : nonprint_char);
+ for (uint8_t *s = data; s < data + len; s++)
+ *s = char_cmp(*s) ? *s : nonprint_char;
+
+ if (highlight && msize) {
+ fwrite(data, 1, mindex, stdout);
+ fputs(RED, stdout);
+ fwrite(data + mindex, 1, msize, stdout);
+ fputs(RESET, stdout);
+ fwrite(data + mindex + msize, 1, len - mindex - msize, stdout);
+ } else {
+ fwrite(data, 1, len, stdout);
}
- putchar('\n');
}
void
@@ -1027,7 +1017,7 @@ dump_formatted(uint8_t *data, uint32_t len, size_t mindex, size_t msize)
}
if (i + j < len)
- putchar(isprint(str[j]) ? str[j] : nonprint_char);
+ putchar(char_cmp(str[j]) ? str[j] : nonprint_char);
else
printf(" ");
diff --git a/ngrep.h b/ngrep.h
@@ -61,6 +61,7 @@
#define RESET "\33[00m"
typedef void (*dump_func)(uint8_t *, uint32_t, size_t, size_t);
+typedef int (*char_cmp_func)(int);
typedef bool (*match_func)(uint8_t *, uint32_t, size_t *, size_t *);
typedef void (*delay_func)(struct pcap_pkthdr *);
typedef void (*ts_func)(struct pcap_pkthdr *);
@@ -82,9 +83,9 @@ void dump_packet(struct pcap_pkthdr *, uint8_t *, uint8_t, uint8_t *, uint32_t,
const char *, const char *, uint16_t, uint16_t, uint8_t,
uint16_t, bool, uint16_t, uint32_t);
-void dump_unwrapped(uint8_t *, uint32_t, size_t, size_t);
+int byline_cmp(int);
+void dump_unformatted(uint8_t *, uint32_t, size_t, size_t);
void dump_formatted(uint8_t *, uint32_t, size_t, size_t);
-void dump_byline(uint8_t *, uint32_t, size_t, size_t);
void dump_delay_proc_init(struct pcap_pkthdr *);
void dump_delay_proc(struct pcap_pkthdr *);