File: histogram.c

package info (click to toggle)
outguess 1%3A0.2.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 2,344 kB
  • sloc: ansic: 22,623; sh: 4,717; asm: 284; makefile: 45
file content (104 lines) | stat: -rw-r--r-- 1,760 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <sys/types.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>

#include "config.h"
#include "outguess.h"

char *progname;

void
usage(void)
{

	fprintf(stderr, "%s: <file>\n", progname);
}

void
histogram_simple(u_char *data, int bits)
{
	int i;
	int one = 0, zero = 0;

	for (i = 0; i < bits; i++)
		if (TEST_BIT(data, i))
			one++;
		else
			zero++;
	
	fprintf(stdout, "Bits: %6d\n", bits);
	fprintf(stdout, "One:  %6d, %f\n", one, (float)one/bits);
	fprintf(stdout, "Zero: %6d, %f\n", zero, (float)zero/bits);
}

#define MAXRUNLEN	25

void
histogram_runlen(u_char *data, int bits)
{
	int buckets[MAXRUNLEN];
	int what, count, i;

	memset(buckets, 0, sizeof(buckets));
	what = TEST_BIT(data, 0);
	count = 1;
	for (i = 1; i < bits; i++) {
		if (TEST_BIT(data, i) != what) {
			if (count >= MAXRUNLEN)
				count = MAXRUNLEN - 1;
			buckets[count]++;

			what = TEST_BIT(data, i);
			count = 1;
		} else
			count++;
	}
	if (count >= MAXRUNLEN)
		count = MAXRUNLEN - 1;
	buckets[count]++;

	for (i = 1; i < MAXRUNLEN; i++) {
		fprintf(stdout, "%3d:  %6d, %f\n",
			i, buckets[i], (float)buckets[i]/bits);
	}
}

int
main(int argc, char *argv[])
{
	FILE *fin;
	char *data;
	int bits, bytes, res;

	progname = argv[0];
	
	argv++;
	argc--;

	if (argc != 1) {
		usage();
		exit(1);
	}
	
	if ((fin = fopen(argv[0], "r")) == NULL)
		err(1, "fopen");

	if ((res = fread(&bits, sizeof(int), 1, fin)) != 1)
		err(1, "fread(1): %d", res);

	bits = ntohl(bits);
	bytes = (bits + 7) / 8;
	if ((data = malloc(bytes)) == NULL)
		err(1, "malloc");

	if ((res = fread(data, sizeof(char), bytes, fin)) != bytes)
		err(1, "fread(2): %d", res);

	histogram_simple(data, bits);
	histogram_runlen(data, bits);

	exit(0);
}