Reindent everything

This commit is contained in:
Neale Pickett 2013-01-29 21:53:17 -07:00
parent 31e9beb71a
commit 9bd3e1bd87
14 changed files with 653 additions and 640 deletions

View File

@ -1,6 +1,6 @@
CFLAGS = -Wall -Werror CFLAGS = -Wall -Werror
TARGETS = pmerge puniq p4split hd TARGETS = pmerge puniq p4split pcat
TARGETS += pyesc printfesc pcat TARGETS += hd pyesc printfesc xor unhex
all: $(TARGETS) all: $(TARGETS)

15
hd.c
View File

@ -2,7 +2,7 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
const char* charset[] = { const char *charset[] = {
"·", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "·", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
"", "", "", "", "", "§", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "§", "", "", "", "", "", "", "", "", "", "",
" ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/", " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*", "+", ",", "-", ".", "/",
@ -23,7 +23,7 @@ const char* charset[] = {
}; };
int int
dump(FILE *f) dump(FILE * f)
{ {
uint64_t p = 0; uint64_t p = 0;
uint8_t buf[32]; uint8_t buf[32];
@ -38,10 +38,11 @@ dump(FILE *f)
offset = 16 - offset; offset = 16 - offset;
len = fread(bytes, 1, 16, f); len = fread(bytes, 1, 16, f);
if (0 == len) break; if (0 == len)
break;
if (p && (0 == memcmp(buf, buf + 16, 16))) { if (p && (0 == memcmp(buf, buf + 16, 16))) {
if (! skipping) { if (!skipping) {
printf("*\n"); printf("*\n");
skipping = 1; skipping = 1;
} }
@ -51,7 +52,7 @@ dump(FILE *f)
skipping = 0; skipping = 0;
} }
printf("%08lx ", (long unsigned int)p); printf("%08lx ", (long unsigned int) p);
for (i = 0; i < 16; i += 1) { for (i = 0; i < 16; i += 1) {
if (i < len) { if (i < len) {
printf("%02x ", bytes[i]); printf("%02x ", bytes[i]);
@ -72,7 +73,7 @@ dump(FILE *f)
} }
p += len; p += len;
} }
printf("%08lx\n", (long unsigned int)p); printf("%08lx\n", (long unsigned int) p);
return 0; return 0;
} }
@ -85,7 +86,7 @@ main(int argc, char *argv[])
} else { } else {
FILE *f = fopen(argv[1], "rb"); FILE *f = fopen(argv[1], "rb");
if (! f) { if (!f) {
perror("open"); perror("open");
return 1; return 1;
} }

View File

@ -5,7 +5,9 @@
#include <strings.h> #include <strings.h>
#include "pcap.h" #include "pcap.h"
/* Gaah so much crap */ /*
* Gaah so much crap
*/
#include <sys/socket.h> #include <sys/socket.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netinet/ether.h> #include <netinet/ether.h>
@ -24,7 +26,7 @@ usage(int ret)
int int
parse_cidr(char *s, uint32_t *addr, uint8_t *bits) parse_cidr(char *s, uint32_t * addr, uint8_t * bits)
{ {
char *slash = index(s, '/'); char *slash = index(s, '/');
struct in_addr inp; struct in_addr inp;
@ -36,7 +38,8 @@ parse_cidr(char *s, uint32_t *addr, uint8_t *bits)
*bits = 0; *bits = 0;
} }
if (0 == inet_aton(s, &inp)) return -1; if (0 == inet_aton(s, &inp))
return -1;
*addr = ntohl(inp.s_addr); *addr = ntohl(inp.s_addr);
return 0; return 0;
@ -55,22 +58,26 @@ main(int argc, char *argv[])
uint8_t shr; uint8_t shr;
int i; int i;
if (argc != 2) return usage(0); if (argc != 2)
if (-1 == parse_cidr(argv[1], &addr, &bits)) return usage(0); return usage(0);
if (bits > 24) return usage(0); if (-1 == parse_cidr(argv[1], &addr, &bits))
return usage(0);
if (bits > 24)
return usage(0);
if (bits % 8) { if (bits % 8) {
fprintf(stderr, "Warning: routing prefix is not a multiple of 8.\n"); fprintf(stderr, "Warning: routing prefix is not a multiple of 8.\n");
} }
mask = ~((1 << (32-bits)) - 1); mask = ~((1 << (32 - bits)) - 1);
addr &= mask; addr &= mask;
shr = (32-bits) - 8; shr = (32 - bits) - 8;
for (i = 0; i < 256; i += 1) { for (i = 0; i < 256; i += 1) {
out[i].f = NULL; out[i].f = NULL;
} }
if (-1 == pcap_open_in(&p, stdin)) return usage(0); if (-1 == pcap_open_in(&p, stdin))
return usage(0);
while (1) { while (1) {
struct pcap_pkthdr hdr; struct pcap_pkthdr hdr;
@ -78,18 +85,22 @@ main(int argc, char *argv[])
char frame[MAXFRAME]; char frame[MAXFRAME];
ok = 1; ok = 1;
if (-1 == pcap_read_pkthdr(&p, &hdr)) break; if (-1 == pcap_read_pkthdr(&p, &hdr))
if (1 != fread(frame, hdr.caplen, 1, p.f)) break; break;
if (1 != fread(frame, hdr.caplen, 1, p.f))
break;
{ {
struct ether_header *eh = (struct ether_header *)frame; struct ether_header *eh = (struct ether_header *) frame;
struct iphdr *ih = (struct iphdr *)(frame + sizeof(struct ether_header)); struct iphdr *ih = (struct iphdr *) (frame + sizeof(struct ether_header));
uint32_t a; uint32_t a;
/* VLAN tag */ /*
* VLAN tag
*/
if (ntohs(eh->ether_type) == 0x8100) { if (ntohs(eh->ether_type) == 0x8100) {
ih = (struct iphdr *)((char *)ih + 4); ih = (struct iphdr *) ((char *) ih + 4);
} }
a = ntohl(ih->saddr); a = ntohl(ih->saddr);
@ -105,21 +116,25 @@ main(int argc, char *argv[])
} }
ok = 0; ok = 0;
if (! out[octet].f) { if (!out[octet].f) {
char fn[9]; char fn[9];
FILE *f; FILE *f;
sprintf(fn, "%d.pcap", octet); sprintf(fn, "%d.pcap", octet);
if (NULL == (f = fopen(fn, "wb"))) break; if (NULL == (f = fopen(fn, "wb")))
if (-1 == pcap_open_out(&out[octet], f)) break; break;
if (-1 == pcap_open_out(&out[octet], f))
break;
} }
if (-1 == pcap_write_pkthdr(&out[octet], &hdr)) break; if (-1 == pcap_write_pkthdr(&out[octet], &hdr))
if (1 != fwrite(frame, hdr.caplen, 1, out[octet].f)) break; break;
if (1 != fwrite(frame, hdr.caplen, 1, out[octet].f))
break;
} }
if (! ok) { if (!ok) {
perror("Error"); perror("Error");
return 1; return 1;
} }

25
pcap.c
View File

@ -4,7 +4,7 @@
#include "pcap.h" #include "pcap.h"
int int
pcap_open_in(struct pcap_file *ctx, FILE *f) pcap_open_in(struct pcap_file *ctx, FILE * f)
{ {
struct pcap_file_header h; struct pcap_file_header h;
@ -19,21 +19,25 @@ pcap_open_in(struct pcap_file *ctx, FILE *f)
} else { } else {
return -1; return -1;
} }
if ((h.version_major != 2) || (h.version_minor != 4)) return -1; if ((h.version_major != 2) || (h.version_minor != 4))
return -1;
if (ctx->swap) h.snaplen = bswap32(h.snaplen); if (ctx->swap)
if (h.snaplen > MAXFRAME) return -1; h.snaplen = bswap32(h.snaplen);
if (h.snaplen > MAXFRAME)
return -1;
ctx->f = f; ctx->f = f;
return 0; return 0;
} }
int int
pcap_open_out(struct pcap_file *ctx, FILE *f) pcap_open_out(struct pcap_file *ctx, FILE * f)
{ {
struct pcap_file_header h = { MAGIC, 2, 4, 0, 0, MAXFRAME, 1 }; struct pcap_file_header h = { MAGIC, 2, 4, 0, 0, MAXFRAME, 1 };
if (1 != fwrite(&h, sizeof(h), 1, f)) return -1; if (1 != fwrite(&h, sizeof(h), 1, f))
return -1;
ctx->f = f; ctx->f = f;
ctx->swap = 0; ctx->swap = 0;
@ -54,7 +58,8 @@ pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr)
hdr->len = bswap32(hdr->len); hdr->len = bswap32(hdr->len);
} }
if (hdr->caplen > MAXFRAME) return -1; if (hdr->caplen > MAXFRAME)
return -1;
return 0; return 0;
} }
@ -71,9 +76,11 @@ pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr)
hdr->caplen = bswap32(hdr->caplen); hdr->caplen = bswap32(hdr->caplen);
hdr->len = bswap32(hdr->len); hdr->len = bswap32(hdr->len);
if (1 != fwrite(&ohdr, sizeof(ohdr), 1, ctx->f)) return -1; if (1 != fwrite(&ohdr, sizeof(ohdr), 1, ctx->f))
return -1;
} else { } else {
if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f)) return -1; if (1 != fwrite(hdr, sizeof(*hdr), 1, ctx->f))
return -1;
} }
return 0; return 0;

12
pcap.h
View File

@ -32,11 +32,11 @@ struct pcap_pkthdr {
}; };
#ifndef max #ifndef max
# define max(a, b) ((a)>(b)?(a):(b)) #define max(a, b) ((a)>(b)?(a):(b))
#endif #endif
#ifndef min #ifndef min
# define min(a, b) ((a)<(b)?(a):(b)) #define min(a, b) ((a)<(b)?(a):(b))
#endif #endif
#define bswap32(i) (((i & 0xff000000) >> 030) | \ #define bswap32(i) (((i & 0xff000000) >> 030) | \
@ -47,7 +47,9 @@ struct pcap_pkthdr {
((i & 0x00ff) << 010)) ((i & 0x00ff) << 010))
/* Debugging help */ /*
* Debugging help
*/
#define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args) #define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#define DUMP() DUMPf("") #define DUMP() DUMPf("")
#define DUMP_d(v) DUMPf("%s = %d", #v, v) #define DUMP_d(v) DUMPf("%s = %d", #v, v)
@ -57,8 +59,8 @@ struct pcap_pkthdr {
#define DUMP_c(v) DUMPf("%s = %c", #v, v) #define DUMP_c(v) DUMPf("%s = %c", #v, v)
#define DUMP_p(v) DUMPf("%s = %p", #v, v) #define DUMP_p(v) DUMPf("%s = %p", #v, v)
int pcap_open_in(struct pcap_file *ctx, FILE *f); int pcap_open_in(struct pcap_file *ctx, FILE * f);
int pcap_open_out(struct pcap_file *ctx, FILE *f); int pcap_open_out(struct pcap_file *ctx, FILE * f);
int pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr); int pcap_read_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr);
int pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr); int pcap_write_pkthdr(struct pcap_file *ctx, struct pcap_pkthdr *hdr);
void pcap_close(struct pcap_file *ctx); void pcap_close(struct pcap_file *ctx);

37
pcat.c
View File

@ -15,11 +15,7 @@
void void
ip4_addr(char *addr_s, uint32_t addr) ip4_addr(char *addr_s, uint32_t addr)
{ {
snprintf(addr_s, 16, "%u.%u.%u.%u", snprintf(addr_s, 16, "%u.%u.%u.%u", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr >> 0) & 0xff);
(addr >> 24) & 0xff,
(addr >> 16) & 0xff,
(addr >> 8) & 0xff,
(addr >> 0) & 0xff);
} }
void void
@ -51,14 +47,7 @@ process_tcp(struct stream *s, char *saddr_s, char *daddr_s)
printf("TCP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport); printf("TCP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport);
// shut the compiler up // shut the compiler up
if (false && if (false && urgent && chksum && window && flags && ack && seq && false);
urgent &&
chksum &&
window &&
flags &&
ack &&
seq &&
false);
} }
void void
@ -72,10 +61,7 @@ process_udp(struct stream *s, char *saddr_s, char *daddr_s)
printf("UDP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport); printf("UDP4 %s:%u %s:%u ", saddr_s, sport, daddr_s, dport);
// Now, do some shit! // Now, do some shit!
if (false && if (false && len && chksum && false);
len &&
chksum &&
false);
} }
void void
@ -119,14 +105,10 @@ process_ip4(struct stream *s)
print_payload(s); print_payload(s);
/* Placate compiler */ /*
if (false && * Placate compiler
chksum && */
id && if (false && chksum && id && tos && ttl && off && false);
tos &&
ttl &&
off &&
false);
} }
@ -167,11 +149,12 @@ print_frame(struct pcap_pkthdr *hdr, char const *frame)
} }
void void
pcat(FILE *f) pcat(FILE * f)
{ {
struct pcap_file p; struct pcap_file p;
if (-1 == pcap_open_in(&p, f)) return; if (-1 == pcap_open_in(&p, f))
return;
for (;;) { for (;;) {
struct pcap_pkthdr hdr; struct pcap_pkthdr hdr;

View File

@ -21,7 +21,8 @@ usage(int ret)
int int
read_next(struct input_file *file) read_next(struct input_file *file)
{ {
if (! file->active) return -1; if (!file->active)
return -1;
if (-1 == pcap_read_pkthdr(&file->p, &file->next)) { if (-1 == pcap_read_pkthdr(&file->p, &file->next)) {
pcap_close(&file->p); pcap_close(&file->p);
@ -37,21 +38,25 @@ read_next(struct input_file *file)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct input_file files[argc-1]; struct input_file files[argc - 1];
int nfiles = 0; int nfiles = 0;
int nopen; int nopen;
int i; int i;
struct pcap_file out; struct pcap_file out;
if (1 == argc) return usage(0); if (1 == argc)
return usage(0);
/* Open input files */ /*
for (i = 0; i < argc-1; i += 1) { * Open input files
char *fn = argv[i+1]; */
for (i = 0; i < argc - 1; i += 1) {
char *fn = argv[i + 1];
struct input_file *cur = &files[nfiles]; struct input_file *cur = &files[nfiles];
FILE *f; FILE *f;
if ('-' == fn[0]) return usage(0); if ('-' == fn[0])
return usage(0);
f = fopen(fn, "rb"); f = fopen(fn, "rb");
if (NULL == f) { if (NULL == f) {
@ -81,7 +86,9 @@ main(int argc, char *argv[])
char frame[MAXFRAME]; char frame[MAXFRAME];
size_t len; size_t len;
/* Find next oldest frame */ /*
* Find next oldest frame
*/
for (i = 0; i < nfiles; i += 1) { for (i = 0; i < nfiles; i += 1) {
if (files[i].active && if (files[i].active &&
((files[i].next.ts.tv_sec < cur->next.ts.tv_sec) || ((files[i].next.ts.tv_sec < cur->next.ts.tv_sec) ||
@ -91,22 +98,30 @@ main(int argc, char *argv[])
} }
} }
/* Make sure it'll fit */ /*
* Make sure it'll fit
*/
if (cur->next.caplen > sizeof(frame)) { 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) len);
return EX_SOFTWARE; return EX_SOFTWARE;
} }
/* Read it */ /*
* Read it
*/
len = fread(frame, 1, cur->next.caplen, cur->p.f); len = fread(frame, 1, cur->next.caplen, cur->p.f);
if (len < cur->next.caplen) { if (len < cur->next.caplen) {
/* Short read */ /*
* Short read
*/
cur->next.caplen = len; cur->next.caplen = len;
pcap_close(&cur->p); pcap_close(&cur->p);
cur->active = 0; cur->active = 0;
} }
/* Write header + frame */ /*
* Write header + frame
*/
if (len) { if (len) {
if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) { if (1 != fwrite(&cur->next, sizeof(cur->next), 1, stdout)) {
perror("error"); perror("error");

View File

@ -5,7 +5,7 @@ main(int argc, char *argv[])
{ {
printf("printf "); printf("printf ");
while (! feof(stdin)) { while (!feof(stdin)) {
int c = getchar(); int c = getchar();
switch (c) { switch (c) {

21
puniq.c
View File

@ -26,7 +26,7 @@ main(int argc, char *argv[])
FILE *f; FILE *f;
struct pcap_file p; struct pcap_file p;
if ((! fn) || (0 == strcmp("-", fn))) { if ((!fn) || (0 == strcmp("-", fn))) {
f = stdin; f = stdin;
} else { } else {
f = fopen(fn, "rb"); f = fopen(fn, "rb");
@ -42,15 +42,20 @@ main(int argc, char *argv[])
} }
while (1) { while (1) {
if (-1 == pcap_read_pkthdr(&p, &hdr[cur])) break; if (-1 == pcap_read_pkthdr(&p, &hdr[cur]))
if (1 != fread(frame[cur], hdr[cur].caplen, 1, p.f)) break; break;
if (1 != fread(frame[cur], hdr[cur].caplen, 1, p.f))
break;
if ((0 == memcmp(&hdr[0], &hdr[1], sizeof(hdr[0]))) && if ((0 == memcmp(&hdr[0], &hdr[1], sizeof(hdr[0]))) && (0 == memcmp(frame[0], frame[1], hdr[cur].caplen))) {
(0 == memcmp(frame[0], frame[1], hdr[cur].caplen))) { /*
/* Skip this duplicate */ * Skip this duplicate
*/
} else { } else {
if (-1 == pcap_write_pkthdr(&out, &hdr[cur])) break; if (-1 == pcap_write_pkthdr(&out, &hdr[cur]))
if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f)) break; break;
if (1 != fwrite(frame[cur], hdr[cur].caplen, 1, out.f))
break;
} }
cur = (1 - cur); cur = (1 - cur);
} }

View File

@ -25,11 +25,11 @@ sskip(struct stream *s, size_t count)
} }
bool bool
sread(struct stream *s, void *buf, size_t count) sread(struct stream * s, void *buf, size_t count)
{ {
void const *d = s->buf; void const *d = s->buf;
if (! sskip(s, count)) { if (!sskip(s, count)) {
return false; return false;
} }
memcpy(buf, d, count); memcpy(buf, d, count);
@ -38,69 +38,56 @@ sread(struct stream *s, void *buf, size_t count)
} }
uint8_t uint8_t
read_uint8(struct stream *s) read_uint8(struct stream * s)
{ {
uint8_t *d = (uint8_t *)s->buf; uint8_t *d = (uint8_t *) s->buf;
if (! sskip(s, 1)) { if (!sskip(s, 1)) {
return 0; return 0;
} }
return d[0]; return d[0];
} }
uint16_t uint16_t
read_uint16be(struct stream *s) read_uint16be(struct stream * s)
{ {
uint8_t *d = (uint8_t *)s->buf; uint8_t *d = (uint8_t *) s->buf;
if (! sskip(s, 2)) { if (!sskip(s, 2)) {
return 0; return 0;
} }
return ( return ((d[0] << 8) | (d[1] << 0));
(d[0] << 8) |
(d[1] << 0));
} }
uint16_t uint16_t
read_uint16le(struct stream *s) read_uint16le(struct stream * s)
{ {
uint8_t *d = (uint8_t *)s->buf; uint8_t *d = (uint8_t *) s->buf;
if (! sskip(s, 2)) { if (!sskip(s, 2)) {
return 0; return 0;
} }
return ( return ((d[0] << 0) | (d[1] << 8));
(d[0] << 0) |
(d[1] << 8));
} }
uint32_t uint32_t
read_uint32be(struct stream *s) read_uint32be(struct stream * s)
{ {
uint8_t *d = (uint8_t *)s->buf; uint8_t *d = (uint8_t *) s->buf;
if (! sskip(s, 4)) { if (!sskip(s, 4)) {
return 0; return 0;
} }
return ( return ((d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3] << 0));
(d[0] << 24) |
(d[1] << 16) |
(d[2] << 8) |
(d[3] << 0));
} }
uint32_t uint32_t
read_uint32le(struct stream *s) read_uint32le(struct stream * s)
{ {
uint8_t *d = (uint8_t *)s->buf; uint8_t *d = (uint8_t *) s->buf;
if (! sskip(s, 4)) { if (!sskip(s, 4)) {
return 0; return 0;
} }
return ( return ((d[0] << 0) | (d[1] << 8) | (d[2] << 16) | (d[3] << 24));
(d[0] << 0) |
(d[1] << 8) |
(d[2] << 16) |
(d[3] << 24));
} }

View File

@ -1,7 +1,6 @@
/* Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> /*
* * Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* This file is in the public domain. I make no promises * of this program.
* about the functionality of this program.
*/ */
#include <stdio.h> #include <stdio.h>

9
xor.c
View File

@ -1,7 +1,6 @@
/* xor filter -- 2012 Zephyr <zephyr@dirtbags.net> /*
* * xor filter -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* This file is in the public domain. I make no promises * of this program.
* about the functionality of this program.
*/ */
#include <stdio.h> #include <stdio.h>
@ -31,7 +30,7 @@ main(int argc, char *argv[])
int c = getchar(); int c = getchar();
unsigned char mask; unsigned char mask;
if (! argv[arg]) { if (!argv[arg]) {
arg = start; arg = start;
} }
mask = strtol(argv[arg++], NULL, base); mask = strtol(argv[arg++], NULL, base);