From d690f8d591806d13c0c0c96ad338dff942191d94 Mon Sep 17 00:00:00 2001 From: Neale Pickett Date: Thu, 12 Jun 2014 12:52:37 -0600 Subject: [PATCH] optimize pcat --- Makefile | 4 ++-- pcat.c | 24 +++++++++++++++++++++--- pmerge.c | 2 +- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 2fe1142..90b775f 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ -CFLAGS = -Wall -Werror -Wno-unused-variable -TARGETS = pmerge puniq p4split pcat +CFLAGS += -Wall -Werror -Wno-unused-variable -O2 +TARGETS += pmerge puniq p4split pcat TARGETS += hd pyesc printfesc xor unhex all: $(TARGETS) diff --git a/pcat.c b/pcat.c index c0afc54..dcd32e1 100644 --- a/pcat.c +++ b/pcat.c @@ -20,11 +20,29 @@ ip4_addr(char *addr_s, uint32_t addr) snprintf(addr_s, 16, "%u.%u.%u.%u", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr >> 0) & 0xff); } +void +print_nybble(uint8_t nybble) +{ + if (nybble < 10) { + putchar(nybble + '0'); + } else { + putchar(nybble - 10 + 'a'); + } +} + +/* About 3x faster than printf("%02x", octet); */ +void +printx(uint8_t octet) +{ + print_nybble(octet >> 4); + print_nybble(octet & 0xf); +} + void print_payload(struct stream *s) { while (s->len) { - printf("%02x", read_uint8(s)); + printx(read_uint8(s)); } } @@ -163,13 +181,13 @@ void pcat(FILE * f) { struct pcap_file p; + struct pcap_pkthdr hdr; + char frame[MAXFRAME]; if (-1 == pcap_open_in(&p, f)) return; for (;;) { - struct pcap_pkthdr hdr; - char frame[MAXFRAME]; if (-1 == pcap_read_pkthdr(&p, &hdr)) { break; diff --git a/pmerge.c b/pmerge.c index d991774..5699237 100644 --- a/pmerge.c +++ b/pmerge.c @@ -102,7 +102,7 @@ main(int argc, char *argv[]) * Make sure it'll fit */ if (cur->next.caplen > sizeof(frame)) { - fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int) len); + fprintf(stderr, "error: huge frame (size %u)\n", (unsigned int) cur->next.caplen); return EX_SOFTWARE; }