Skip to content

Instantly share code, notes, and snippets.

@bjodah
Last active September 15, 2023 18:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bjodah/9c95c6814947e854dab5 to your computer and use it in GitHub Desktop.
Save bjodah/9c95c6814947e854dab5 to your computer and use it in GitHub Desktop.
count byte freqencies
a.out
zeros.bin
output.txt
0 0.000000
1 0.000000
2 0.000000
3 0.000000
4 0.000000
5 0.000000
6 0.000000
7 0.000000
8 0.000000
9 0.000000
10 0.076923
11 0.000000
12 0.000000
13 0.000000
14 0.000000
15 0.000000
16 0.000000
17 0.000000
18 0.000000
19 0.000000
20 0.000000
21 0.000000
22 0.000000
23 0.000000
24 0.000000
25 0.000000
26 0.000000
27 0.000000
28 0.000000
29 0.000000
30 0.000000
31 0.000000
32 0.076923
33 0.076923
34 0.000000
35 0.000000
36 0.000000
37 0.000000
38 0.000000
39 0.000000
40 0.000000
41 0.000000
42 0.000000
43 0.000000
44 0.000000
45 0.000000
46 0.000000
47 0.000000
48 0.000000
49 0.000000
50 0.000000
51 0.000000
52 0.000000
53 0.000000
54 0.000000
55 0.000000
56 0.000000
57 0.000000
58 0.000000
59 0.000000
60 0.000000
61 0.000000
62 0.000000
63 0.000000
64 0.000000
65 0.000000
66 0.000000
67 0.000000
68 0.000000
69 0.000000
70 0.000000
71 0.000000
72 0.076923
73 0.000000
74 0.000000
75 0.000000
76 0.000000
77 0.000000
78 0.000000
79 0.000000
80 0.000000
81 0.000000
82 0.000000
83 0.000000
84 0.000000
85 0.000000
86 0.000000
87 0.076923
88 0.000000
89 0.000000
90 0.000000
91 0.000000
92 0.000000
93 0.000000
94 0.000000
95 0.000000
96 0.000000
97 0.000000
98 0.000000
99 0.000000
100 0.076923
101 0.076923
102 0.000000
103 0.000000
104 0.000000
105 0.000000
106 0.000000
107 0.000000
108 0.230769
109 0.000000
110 0.000000
111 0.153846
112 0.000000
113 0.000000
114 0.076923
115 0.000000
116 0.000000
117 0.000000
118 0.000000
119 0.000000
120 0.000000
121 0.000000
122 0.000000
123 0.000000
124 0.000000
125 0.000000
126 0.000000
127 0.000000
128 0.000000
129 0.000000
130 0.000000
131 0.000000
132 0.000000
133 0.000000
134 0.000000
135 0.000000
136 0.000000
137 0.000000
138 0.000000
139 0.000000
140 0.000000
141 0.000000
142 0.000000
143 0.000000
144 0.000000
145 0.000000
146 0.000000
147 0.000000
148 0.000000
149 0.000000
150 0.000000
151 0.000000
152 0.000000
153 0.000000
154 0.000000
155 0.000000
156 0.000000
157 0.000000
158 0.000000
159 0.000000
160 0.000000
161 0.000000
162 0.000000
163 0.000000
164 0.000000
165 0.000000
166 0.000000
167 0.000000
168 0.000000
169 0.000000
170 0.000000
171 0.000000
172 0.000000
173 0.000000
174 0.000000
175 0.000000
176 0.000000
177 0.000000
178 0.000000
179 0.000000
180 0.000000
181 0.000000
182 0.000000
183 0.000000
184 0.000000
185 0.000000
186 0.000000
187 0.000000
188 0.000000
189 0.000000
190 0.000000
191 0.000000
192 0.000000
193 0.000000
194 0.000000
195 0.000000
196 0.000000
197 0.000000
198 0.000000
199 0.000000
200 0.000000
201 0.000000
202 0.000000
203 0.000000
204 0.000000
205 0.000000
206 0.000000
207 0.000000
208 0.000000
209 0.000000
210 0.000000
211 0.000000
212 0.000000
213 0.000000
214 0.000000
215 0.000000
216 0.000000
217 0.000000
218 0.000000
219 0.000000
220 0.000000
221 0.000000
222 0.000000
223 0.000000
224 0.000000
225 0.000000
226 0.000000
227 0.000000
228 0.000000
229 0.000000
230 0.000000
231 0.000000
232 0.000000
233 0.000000
234 0.000000
235 0.000000
236 0.000000
237 0.000000
238 0.000000
239 0.000000
240 0.000000
241 0.000000
242 0.000000
243 0.000000
244 0.000000
245 0.000000
246 0.000000
247 0.000000
248 0.000000
249 0.000000
250 0.000000
251 0.000000
252 0.000000
253 0.000000
254 0.000000
255 0.000000
#include <stdio.h>
#define BUFFERLEN 4096
int main(){
// This program reads standard input and calculate frequencies of different
// bytes and present the frequences for each byte value upon exit.
//
// Example:
//
// $ echo "Hello world" | ./a.out
//
// Copyright (c) 2015 Björn Dahlgren
// Open source: MIT License
long long tot = 0; // long long guaranteed to be 64 bits i.e. 16 exabyte
long long n[256]; // One byte == 8 bits => 256 unique bytes
const int bufferlen = BUFFERLEN;
char buffer[BUFFERLEN];
int i;
size_t nread;
for (i=0; i<256; ++i)
n[i] = 0;
do {
nread = fread(buffer, 1, bufferlen, stdin);
for (i = 0; i < nread; ++i)
++n[(unsigned char)buffer[i]];
tot += nread;
} while (nread == bufferlen);
// here you may want to inspect ferror of feof
for (i=0; i<256; ++i){
printf("%d ", i);
printf("%f\n", n[i]/(float)tot);
}
return 0;
}
SHELL := /bin/bash
CFLAGS?=-Wall -O2 -march=native
.PHONY: test
a.out: main.c
$(CC) $(CFLAGS) $^
test: a.out
echo "Hello World!" | ./a.out >output.txt
diff blessed_hello_world.txt output.txt
dd if=/dev/zero of=zeros.bin bs=1k count=1
cat zeros.bin | ./a.out | head -n 1 | diff blessed_zeros_head.txt -
cat zeros.bin | ./a.out | tail -n 1 | diff blessed_zeros_tail.txt -
echo -n -e '\xFF' | ./a.out | tail -n 1 | diff blessed_ff.txt -
@Schokn-Itrch
Copy link

#include <stdio.h>
#include <fcntl.h> // 4 correct EOL processing

#define BUFFERLEN 4096

int main(){
// This program reads standard input and calculate frequencies of different
// bytes and present the frequences for each byte value upon exit.
//
// Example:
//
// $ echo "Hello world" | ./a.out
//
// Copyright (c) 2015 Bjorn Dahlgren
// Open source: MIT License

long long tot = 0; // long long guaranteed to be 64 bits i.e. 16 exabyte
long long n[256]; // One byte == 8 bits => 256 unique bytes

const int bufferlen = BUFFERLEN;
char buffer[BUFFERLEN];
int i;
size_t nread;

_setmode(fileno(stdin), O_BINARY); // 4 correct EOL processing

for (i=0; i<256; ++i)
    n[i] = 0;

do {
    nread = fread(buffer, 1, bufferlen, stdin);
    for (i = 0; i < nread; ++i)
        ++n[(unsigned char)buffer[i]];
    tot += nread;
} while (nread == bufferlen);
// here you may want to inspect ferror of feof

for (i=0; i<256; ++i){
    printf("%03d ", i);
    printf("%4.32f\n", n[i]/(float)tot); // 4 insane correct output statistics for very rare bytes
}
return 0;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment