cybersecurity · beginner · ~15 min · safe pentest lab
Count how many of the 256 byte values appear — a floating-point-free entropy proxy.
Encrypted or compressed data uses almost all 256 byte values; plaintext and code use a small subset. Implement:
int distinct_byte_count(const unsigned char *buf, size_t n);
Return how many distinct byte values (0-256) occur in the buffer.
buf of n bytes.
Distinct byte count, 0-256.
n==0 returns 0.
#include <stddef.h>
/* Number of DISTINCT byte values (0-256) present in buf. */
int distinct_byte_count(const unsigned char *buf, size_t n){ (void)buf;(void)n; return 0; }
Using an int array indexed by a signed char (negative index); counting occurrences instead of distinct values.
All-same bytes return 1; a full 256-byte permutation returns 256.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.