|
| 1 | +# Copilot Instructions for magicnumber |
| 2 | + |
| 3 | +## Project Overview |
| 4 | +A Go library that identifies file types by reading byte signatures (magic numbers) rather than relying on file extensions or MIME types. Supports 90+ file format signatures across images, video, audio, archives, executables, documents, and text formats. |
| 5 | + |
| 6 | +## Build, Test, and Lint Commands |
| 7 | + |
| 8 | +### Using Task (preferred) |
| 9 | +```bash |
| 10 | +# List all available tasks |
| 11 | +task --list-all |
| 12 | + |
| 13 | +# Run tests |
| 14 | +task test # Standard test run (no caching) |
| 15 | +task testr # Run tests with race detection (slower) |
| 16 | + |
| 17 | +# Lint and format |
| 18 | +task lint # Run gofumpt formatter and golangci-lint |
| 19 | + |
| 20 | +# Static analysis |
| 21 | +task nil # Run nilaway static analysis for nil dereferences |
| 22 | + |
| 23 | +# Dependencies |
| 24 | +task update # Update all dependencies to latest version |
| 25 | +task patch # Update only patch versions of dependencies |
| 26 | + |
| 27 | +# Documentation |
| 28 | +task doc # Generate and browse module documentation locally |
| 29 | +``` |
| 30 | + |
| 31 | +### Direct Go commands |
| 32 | +```bash |
| 33 | +# Test a single package |
| 34 | +go test -count 1 -v ./archive.go ./archive_test.go |
| 35 | + |
| 36 | +# Test a single function |
| 37 | +go test -run TestArchive -v -count 1 ./... |
| 38 | + |
| 39 | +# Run with verbose output and detailed test names |
| 40 | +go test -v -count 1 ./... |
| 41 | + |
| 42 | +# Lint check |
| 43 | +golangci-lint run -c .golangci.yaml |
| 44 | +``` |
| 45 | + |
| 46 | +## High-Level Architecture |
| 47 | + |
| 48 | +### Core Design Pattern |
| 49 | +The library uses a **matcher pattern** where each file signature is associated with a `Matcher` function that performs byte-level validation: |
| 50 | +- `Matcher` is a function type: `func(io.ReaderAt) bool` that checks if a reader contains a specific file signature |
| 51 | +- `Finder` is a map of `Signature -> Matcher` that contains all detection logic |
| 52 | +- Signatures are defined as an `iota` enum starting from `ZeroByte` (-2) |
| 53 | + |
| 54 | +### Main Components |
| 55 | + |
| 56 | +**magicnumber.go** (core API) |
| 57 | +- `Signature` enum: ~90 file type constants |
| 58 | +- `Find(io.ReaderAt) Signature`: Main entry point, returns identified file type |
| 59 | +- `MatchExt(filename, reader)`: Validates if file content matches its extension |
| 60 | +- `New() *Finder`: Returns map of all signature matchers |
| 61 | +- `Ext()`: Returns map of signatures to file extensions |
| 62 | +- Helper types: `Extension`, `Finder`, `Matcher` |
| 63 | + |
| 64 | +**Format-specific modules** (grouped by category): |
| 65 | +- `executable.go`: DOS/Windows executables, self-extracting archives (PKLITE, PKSFX) |
| 66 | +- `archive.go`: ZIP variants, RAR, TAR, 7z, GZip, etc. (uses PKWARE detection logic) |
| 67 | +- `media.go`: Images (JPEG, PNG, BMP, TIFF), video (MP4, AVI, MOV), audio (MP3, WAV, FLAC, OGG) |
| 68 | +- `cdimage.go`: CD/DVD ISO formats (ISO 9660, Nero, PowerISO, Alcohol 120) |
| 69 | +- `text.go`: Text and document formats (UTF-8/16/32, ANSI, PDF, RTF) |
| 70 | +- `id3.go`: ID3 tag parsing for MP3 metadata |
| 71 | +- `synthesismusic.go`: Tracker music formats (MOD, IT, XM, MTM) |
| 72 | + |
| 73 | +**knowns.go**: Currently empty; placeholder for additional data structures |
| 74 | + |
| 75 | +### Detection Strategy |
| 76 | +1. `Find()` iterates through all matchers in `Finder` map |
| 77 | +2. For text/special cases (ANSI, plain text, XBIN), it applies secondary checks in specific order |
| 78 | +3. Returns `Unknown` if no signature matches |
| 79 | +4. Returns `ZeroByte` for empty files |
| 80 | + |
| 81 | +### Testing |
| 82 | +- Each module has a corresponding `*_test.go` file |
| 83 | +- Tests use actual file samples from `testdata/` directory |
| 84 | +- Pattern: `TestSignatureName` function names (e.g., `TestArchive`, `TestMSExe`) |
| 85 | +- Use `go test -count 1` to disable caching (important for file I/O tests) |
| 86 | + |
| 87 | +## Performance Considerations |
| 88 | + |
| 89 | +### Key Bottleneck: Repeated `New()` Calls |
| 90 | +**Current Issue**: Every function that detects file types creates a new `Finder` map with all matchers: |
| 91 | +- `Find()` calls `New()` once per file |
| 92 | +- `MatchExt()` calls `New()` once per file |
| 93 | +- Category functions (`Archive()`, `Image()`, `Video()`, `Document()`, etc.) in knowns.go call `New()` once each |
| 94 | + |
| 95 | +**Impact**: Building the ~90-entry matcher map and dereference with `*New()` happens on every detection call. For high-volume batch processing, this is inefficient. |
| 96 | + |
| 97 | +### Optimization Opportunities |
| 98 | +1. **Cache `New()` result** - Create Finder once at package init or module load, then reuse |
| 99 | + ```go |
| 100 | + var ( |
| 101 | + defaultFinder *Finder |
| 102 | + ) |
| 103 | + |
| 104 | + func init() { |
| 105 | + defaultFinder = New() |
| 106 | + } |
| 107 | + ``` |
| 108 | + |
| 109 | +2. **Lazy initialization** - If caching entire map isn't desired, lazy-initialize on first use |
| 110 | + |
| 111 | +3. **Specialized matchers** - Category functions already filter to smaller lists, which is good for targeted detection |
| 112 | + |
| 113 | +### Current Strength: Minimal Byte Reading |
| 114 | +- Matchers only read necessary bytes (often 2-6 bytes from specific offsets) |
| 115 | +- No full file buffering |
| 116 | +- Good for large files or streaming scenarios |
| 117 | + |
| 118 | +### Testing Impact |
| 119 | +Tests use `go test -count 1` to avoid caching, ensuring fresh reads. This is important and should be preserved. |
| 120 | + |
| 121 | +## Key Conventions |
| 122 | + |
| 123 | +### Code Organization |
| 124 | +- **One file per format category**, not one per signature type |
| 125 | +- Matcher functions placed near related helpers (e.g., `Pklite()` near other DOS executables) |
| 126 | +- Internal helper functions in same file as their public matchers (e.g., `NotASCII()` in text.go) |
| 127 | + |
| 128 | +### Naming Conventions |
| 129 | +- Matcher functions: PascalCase, match signature constant names (e.g., `MSExe()` for `MicrosoftExecutable`) |
| 130 | +- Signature constants: Descriptive PascalCase (e.g., `PKWAREZip`, `MicrosoftExecutable`) |
| 131 | +- `.String()` returns lowercase/hyphenated descriptive names for UX |
| 132 | +- `.Title()` returns full format names (e.g., "JPEG File Interchange Format") |
| 133 | + |
| 134 | +### Byte Reading Pattern |
| 135 | +All matchers follow a consistent pattern for safety: |
| 136 | +```go |
| 137 | +func MatcherName(r io.ReaderAt) bool { |
| 138 | + const size = N // number of bytes to read |
| 139 | + const offset = M // where to read from (usually 0) |
| 140 | + p := make([]byte, size) |
| 141 | + sr := io.NewSectionReader(r, offset, size) |
| 142 | + if n, err := sr.Read(p); err != nil || n < size { |
| 143 | + return false // handle read errors gracefully |
| 144 | + } |
| 145 | + // bytes.Equal() or bytes.Compare() or bitmask checks |
| 146 | + return /* validation logic */ |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +### Testing Files |
| 151 | +- Test files use sample files from `testdata/` directory |
| 152 | +- Test pattern: Read file → pass to function → assert signature |
| 153 | +- Some matchers have multiple test cases for different variants |
| 154 | + |
| 155 | +### Linting Configuration |
| 156 | +- Uses golangci-lint with custom config in `.golangci.yaml` |
| 157 | +- gofumpt formatter enabled for consistent formatting |
| 158 | +- Max complexity: 17 (cyclop), max function length: 60 lines |
| 159 | +- Several linters disabled: exhaustive, ireturn, varnamelen, wsl variants |
| 160 | +- Test files get leniency on complexity/function length rules |
| 161 | + |
| 162 | +### Dependencies |
| 163 | +- `github.com/nalgeon/be`: Likely used for byte order operations (check usage) |
| 164 | +- `golang.org/x/text`: Text encoding support (UTF-8/16/32 detection) |
| 165 | +- `go.uber.org/nilaway`: Static nil pointer analysis tool |
| 166 | + |
| 167 | +### Documentation Sources |
| 168 | +Magic number byte values sourced from: |
| 169 | +- Gary Kessler's File Signatures Table |
| 170 | +- Just Solve the File Format Problem (Archive Team) |
| 171 | +- OSDev Wiki |
| 172 | +- Wikipedia List of File Signatures |
0 commit comments