cybersecurity · intermediate · ~15 min · safe pentest lab

Recover an 8.3 filename from a FAT directory entry

Fixed-layout filename reconstruction with a sentinel-byte rule.

Challenge

Your job

Implement:

#include <stdint.h>
int recover_8_3(const uint8_t *entry, char *out);

entry points to a 32-byte FAT directory entry. Bytes 0..10 are the 8.3 short name (8 base + 3 extension, space-padded ASCII).

Rules:

  • If entry[0] == 0xE5, replace the first byte of the rendered name with '_' (deleted-entry convention).
  • Trim trailing spaces from base and extension separately.
  • If the trimmed extension is empty, omit the ..
  • Always uppercase ASCII letters in the output.
  • Output buffer is at least 13 bytes.
  • Return the number of bytes written, excluding the NUL.
  • Return -1 if entry or out is NULL.

Examples

  • base="HELLO ", ext="TXT" → HELLO.TXT → 9
  • base="README ", ext=" " → README → 6
  • entry[0]=0xE5, rest "ELLO TXT" → _ELLO.TXT → 9

Hints

  1. (concept) Two trim passes: from the right of each field, walk back while seeing space.
  2. (common bug) Using strncpy and trying to trim later. It's simpler to walk byte-by-byte.

Why this matters

The 0xE5 deletion convention is what every FAT recovery tool uses to list deleted files. Reproducing it in C makes the rule concrete.

Input format

A pointer to a 32-byte buffer and a >=13-byte output buffer.

Output format

Number of bytes written (excluding NUL), or -1 on NULL inputs.

Constraints

Output up to 13 bytes (8 + 1 + 3 + 1). Uppercase ASCII. No reads past byte 11.

Starter code

#include <stdint.h>
int recover_8_3(const uint8_t *entry, char *out) {
    /* TODO */
    (void)entry; (void)out;
    return -1;
}

Common mistakes

Forgetting trailing spaces. Including the dot when extension is empty. Mishandling the 0xE5 rule (it applies only to position 0).

Edge cases to handle

No extension. Maximum-length 8 + 3 name. Deleted entry that's also lowercase before deletion.

Complexity

O(1) — at most 12 bytes processed.

Background lessons

Up next

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