file-handling · beginner · ~10 min
Recognise BOM byte signatures with correct ordering.
Detect a byte-order mark at the start of a buffer — the first step in robust text loading, since a stray BOM silently corrupts parsing.
Implement int detect_bom(const uint8_t *buf, size_t n) that inspects the leading bytes of buf and reports which BOM (if any) is present.
buf: a byte buffer (may be NULL).n: number of valid bytes in buf.Return:
1 for the UTF-8 BOM EF BB BF,2 for UTF-16LE FF FE,3 for UTF-16BE FE FF,0 for no BOM, including NULL or too-short input.{0xEF,0xBB,0xBF,...} -> 1 (UTF-8)
{0xFF,0xFE,...} -> 2 (UTF-16LE)
{0xFE,0xFF,...} -> 3 (UTF-16BE)
{'h','e','l',...} -> 0
{0xFF} (n=1) -> 0 (too short)
FF FE vs FE FF distinguishes LE from BE — order matters.n.A leading BOM silently corrupts parsing if you don't skip it; detecting one is the first step in robust text loading.
buf: byte buffer (may be NULL). n: valid byte count.
int: 1=UTF-8, 2=UTF-16LE, 3=UTF-16BE, 0=none/NULL/too-short.
Check the 3-byte BOM first; respect n so you never read past the buffer.
#include <stdint.h>
#include <stddef.h>
int detect_bom(const uint8_t *buf, size_t n) {
/* TODO */
(void)buf; (void)n;
return 0;
}
Swapping LE/BE. Checking the 2-byte BOM before the 3-byte one. Reading past a short buffer.
No BOM. Only one byte present. NULL.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.