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
TARGETS = pmerge puniq p4split hd
TARGETS += pyesc printfesc pcat
TARGETS = pmerge puniq p4split pcat
TARGETS += hd pyesc printfesc xor unhex
all: $(TARGETS)

15
hd.c
View File

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

View File

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

25
pcap.c
View File

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

12
pcap.h
View File

@ -32,11 +32,11 @@ struct pcap_pkthdr {
};
#ifndef max
# define max(a, b) ((a)>(b)?(a):(b))
#define max(a, b) ((a)>(b)?(a):(b))
#endif
#ifndef min
# define min(a, b) ((a)<(b)?(a):(b))
#define min(a, b) ((a)<(b)?(a):(b))
#endif
#define bswap32(i) (((i & 0xff000000) >> 030) | \
@ -47,7 +47,9 @@ struct pcap_pkthdr {
((i & 0x00ff) << 010))
/* Debugging help */
/*
* Debugging help
*/
#define DUMPf(fmt, args...) fprintf(stderr, "%s:%s:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##args)
#define DUMP() DUMPf("")
#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_p(v) DUMPf("%s = %p", #v, v)
int pcap_open_in(struct pcap_file *ctx, FILE *f);
int pcap_open_out(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_read_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);

37
pcat.c
View File

@ -15,11 +15,7 @@
void
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);
snprintf(addr_s, 16, "%u.%u.%u.%u", (addr >> 24) & 0xff, (addr >> 16) & 0xff, (addr >> 8) & 0xff, (addr >> 0) & 0xff);
}
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);
// shut the compiler up
if (false &&
urgent &&
chksum &&
window &&
flags &&
ack &&
seq &&
false);
if (false && urgent && chksum && window && flags && ack && seq && false);
}
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);
// Now, do some shit!
if (false &&
len &&
chksum &&
false);
if (false && len && chksum && false);
}
void
@ -119,14 +105,10 @@ process_ip4(struct stream *s)
print_payload(s);
/* Placate compiler */
if (false &&
chksum &&
id &&
tos &&
ttl &&
off &&
false);
/*
* Placate compiler
*/
if (false && chksum && id && tos && ttl && off && false);
}
@ -167,11 +149,12 @@ print_frame(struct pcap_pkthdr *hdr, char const *frame)
}
void
pcat(FILE *f)
pcat(FILE * f)
{
struct pcap_file p;
if (-1 == pcap_open_in(&p, f)) return;
if (-1 == pcap_open_in(&p, f))
return;
for (;;) {
struct pcap_pkthdr hdr;

View File

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

View File

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

21
puniq.c
View File

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

View File

@ -25,11 +25,11 @@ sskip(struct stream *s, size_t count)
}
bool
sread(struct stream *s, void *buf, size_t count)
sread(struct stream * s, void *buf, size_t count)
{
void const *d = s->buf;
if (! sskip(s, count)) {
if (!sskip(s, count)) {
return false;
}
memcpy(buf, d, count);
@ -38,69 +38,56 @@ sread(struct stream *s, void *buf, size_t count)
}
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 d[0];
}
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 (
(d[0] << 8) |
(d[1] << 0));
return ((d[0] << 8) | (d[1] << 0));
}
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 (
(d[0] << 0) |
(d[1] << 8));
return ((d[0] << 0) | (d[1] << 8));
}
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 (
(d[0] << 24) |
(d[1] << 16) |
(d[2] << 8) |
(d[3] << 0));
return ((d[0] << 24) | (d[1] << 16) | (d[2] << 8) | (d[3] << 0));
}
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 (
(d[0] << 0) |
(d[1] << 8) |
(d[2] << 16) |
(d[3] << 24));
return ((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>
*
* This file is in the public domain. I make no promises
* about the functionality of this program.
/*
* Hex Decoder -- 2012 Zephyr <zephyr@dirtbags.net> This file is in the public domain. I make no promises about the functionality
* of this program.
*/
#include <stdio.h>

9
xor.c
View File

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