cybersecurity · beginner · ~15 min · safe pentest lab

Count distinct byte values

Count how many of the 256 byte values appear — a floating-point-free entropy proxy.

Challenge

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.

Input format

buf of n bytes.

Output format

Distinct byte count, 0-256.

Constraints

n==0 returns 0.

Starter code

#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; }

Common mistakes

Using an int array indexed by a signed char (negative index); counting occurrences instead of distinct values.

Edge cases to handle

All-same bytes return 1; a full 256-byte permutation returns 256.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.