- sfile --json: one JSON object per file (JSON Lines) carrying format, offsets, decoded fields and geotags when present; default CLI output byte-identical (verified against the pre-refactor binary). - libsfile: src/sfile.h + src/sfile_lib.c expose the decoder (sfile_decode_file/mem, sfile_result_to_json, format table introspection); make lib builds libsfile.a and libsfile.so.1.1.0, make install installs them plus the header. - tests/test_json.py: JSON parses and matches the text table on the same files (make test-json); tests/test_library.c + test_lib.sh: static and shared link test (make test-lib).
218 lines
8.5 KiB
Markdown
218 lines
8.5 KiB
Markdown
# sfile --json — machine-readable decode output
|
|
|
|
`sfile --json <file>...` prints the same decode as the human-readable
|
|
table, as JSON: **one compact JSON object per file, one per line**
|
|
(JSON Lines). Each line parses on its own; with several files the
|
|
output is one object per line, in argument order.
|
|
|
|
```sh
|
|
sfile --json photo.png
|
|
sfile --json *.png > decodes.jsonl # one object per file
|
|
```
|
|
|
|
Exit codes are unchanged from the text mode: 0 identified, 1 usage
|
|
error, 2 file unreadable, 3 unknown format. With several files the
|
|
worst code wins (unreadable beats unknown). An unreadable file still
|
|
produces a JSON object on stdout (with `size: null` and an `error`
|
|
string); the `perror`-style message also goes to stderr, so stdout
|
|
stays pure JSON.
|
|
|
|
## Example: a PNG header
|
|
|
|
The CLI emits compact one-line JSON; the object below is the real
|
|
output of `sfile --json` on a 29-byte PNG, reformatted for reading:
|
|
|
|
```json
|
|
{
|
|
"file": "/tmp/fx/sample.png",
|
|
"size": 29,
|
|
"identified": true,
|
|
"format": {"task": 564, "ext": ".PNG", "name": "PNG image",
|
|
"kind": "binary", "endian": "big"},
|
|
"identified_by": "magic",
|
|
"magic": {"bytes": "89 50 4e 47 0d 0a 1a 0a", "offset": 0},
|
|
"header": {
|
|
"endian": "big",
|
|
"fields": [
|
|
{"offset": 0, "size": 8, "name": "signature", "type": "bytes",
|
|
"value": "89 50 4e 47 0d 0a 1a 0a"},
|
|
{"offset": 8, "size": 4, "name": "ihdr_length", "type": "u32",
|
|
"value": 13},
|
|
{"offset": 12, "size": 4, "name": "ihdr_type", "type": "ascii",
|
|
"value": "IHDR"},
|
|
{"offset": 16, "size": 4, "name": "width", "type": "u32",
|
|
"value": 800},
|
|
{"offset": 20, "size": 4, "name": "height", "type": "u32",
|
|
"value": 600},
|
|
{"offset": 24, "size": 1, "name": "bit_depth", "type": "u8",
|
|
"value": 8},
|
|
{"offset": 25, "size": 1, "name": "color_type", "type": "u8",
|
|
"value": 2},
|
|
{"offset": 26, "size": 1, "name": "compression", "type": "u8",
|
|
"value": 0},
|
|
{"offset": 27, "size": 1, "name": "filter", "type": "u8",
|
|
"value": 0},
|
|
{"offset": 28, "size": 1, "name": "interlace", "type": "u8",
|
|
"value": 0}
|
|
]
|
|
},
|
|
"geotags": []
|
|
}
|
|
```
|
|
|
|
Compare with the text mode on the same file: the field values agree
|
|
(`width` 800, `ihdr_type` `"IHDR"`, …) — JSON is a re-encoding of the
|
|
same decode, not a second implementation. `tests/test_json.py`
|
|
checks this parity on every fixture (`make test-json`).
|
|
|
|
## Schema
|
|
|
|
| key | when present | meaning |
|
|
|---|---|---|
|
|
| `file` | always | path as given on the command line |
|
|
| `size` | always | file size in bytes, or `null` when unreadable |
|
|
| `identified` | always | `true` when a format was identified |
|
|
| `error` | `status != 0` | human-readable reason (`"unknown format (no magic match, no known extension)"`, or the `strerror` text for unreadable files) |
|
|
| `format` | identified | `task` (fileformat task number), `ext`, `name`, `kind` (`"binary"` / `"text"` / `"unknown"`), `endian` (`"big"` / `"little"`) |
|
|
| `identified_by` | identified | `"magic"` or `"extension"` |
|
|
| `magic` | magic present | `bytes` (space-separated lowercase hex), `offset` |
|
|
| `other_candidates` | >1 format shares the magic | the other formats' `ext` + `task` (the chosen format excluded) |
|
|
| `header` | binary format with a layout | `endian` + `fields[]` |
|
|
| `document` | text format | see below |
|
|
| `note` | binary format, no layout | `"no header layout derived (derivation: magic-only)"` or `"(derivation: unknown)"` |
|
|
| `hexdump` | unknown, or no layout | first 64 bytes, space-separated lowercase hex |
|
|
| `geotags` | format has a geotagging mechanism | array of locations (empty when none found); omitted when the format has no mechanism at all |
|
|
|
|
### Field objects
|
|
|
|
`{"offset", "size", "name", "type", "value"}` — `offset`/`size` in
|
|
bytes, `name` from the format table, `type` one of `u8 u16 u32 u64 i8
|
|
i16 i32 i64 f32 f64 ascii bytes`.
|
|
|
|
`value` is a JSON number for numeric fields (integers exact; floats
|
|
at full `%.17g` precision, so a latitude survives the round-trip —
|
|
the text table's `%g` shows only 6 significant digits), a JSON string
|
|
for `ascii` (unquoted — the text table quotes it: `"IHDR"`) and for
|
|
`bytes` (space-separated hex, no trailing space).
|
|
|
|
A field that runs past the bytes read (empty/truncated file) has
|
|
`"value": null` and `"unavailable": "beyond header window / EOF"` —
|
|
the same words the text table prints.
|
|
|
|
### Document objects (text formats)
|
|
|
|
```json
|
|
{"type": "xml", "declaration": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
|
|
"root_element": "catalog", "version": "2.0"}
|
|
{"type": "json", "first_key": "name"}
|
|
{"type": "text", "first_line": "Hello, sfile."}
|
|
```
|
|
|
|
Keys that were not found are omitted (a JSON array has just
|
|
`{"type": "json"}`; a text file always has `first_line`, possibly
|
|
`""`).
|
|
|
|
### Geotag objects
|
|
|
|
`geotags` comes from `geotag_scan()` (`src/geotag.c`) — the same
|
|
decoders behind the text mode's `Geotagging:` section (EXIF GPS,
|
|
XMP, QuickTime/ISO 6709, GeoTIFF, EXR, PSD, CR3, …). Each entry:
|
|
|
|
```json
|
|
{"source": "EXIF GPS IFD (JPEG APP1 segment)",
|
|
"latitude": 37.7749, "longitude": -122.41940000000001,
|
|
"altitude_m": 10, "datum": "WGS-84",
|
|
"datetime_utc": "2026-12-01 12:34:56"}
|
|
```
|
|
|
|
`source`, `latitude`, `longitude` are always present;
|
|
`altitude_m`, `datum`, `datetime_utc` only when the decoder found
|
|
them. A file can carry several locations (an MP4 with two
|
|
QuickTime sources yields two entries). `geotags: []` means the
|
|
format has a mechanism but this file carries no location; the key
|
|
is omitted entirely when the format has no mechanism. Header
|
|
fields merely *named* `latitude`/`longitude` (e.g. SPS track
|
|
files, task 678) are decoded as ordinary fields and are **not**
|
|
geotags — they do not produce a `geotags` key.
|
|
|
|
## Example: geotags, shared magic, unknown, unreadable
|
|
|
|
A JPEG with EXIF GPS (the tail of the real output of
|
|
`sfile --json tests/geotag/test-gps.jpg`):
|
|
|
|
```json
|
|
"geotags": [
|
|
{
|
|
"source": "EXIF GPS IFD (JPEG APP1 segment)",
|
|
"latitude": 37.7749,
|
|
"longitude": -122.41940000000001,
|
|
"altitude_m": 10,
|
|
"datum": "WGS-84",
|
|
"datetime_utc": "2026-12-01 12:34:56"
|
|
}
|
|
]
|
|
```
|
|
|
|
The text mode prints the same location as:
|
|
|
|
```
|
|
Geotagging:
|
|
source: EXIF GPS IFD (JPEG APP1 segment)
|
|
gps_latitude: 37.774900 (37°46'29.64"N)
|
|
gps_longitude: -122.419400 (122°25'09.84"W)
|
|
gps_altitude_m: 10.000 (above sea level)
|
|
gps_datum: WGS-84
|
|
gps_datetime_utc: 2026-12-01 12:34:56
|
|
```
|
|
|
|
An MP4 with two QuickTime location sources
|
|
(`tests/geotag/test-gps.mp4`) yields two entries — same
|
|
coordinates, different altitudes:
|
|
|
|
```json
|
|
"geotags": [
|
|
{"source": "QuickTime location (moov/udta (c)xyz atom)",
|
|
"latitude": 37.7749, "longitude": -122.4194,
|
|
"altitude_m": 10, "datum": "WGS-84 (ISO 6709 default)"},
|
|
{"source": "QuickTime location (moov/udta/meta com.apple.quicktime.location.ISO6709)",
|
|
"latitude": 37.7749, "longitude": -122.4194,
|
|
"altitude_m": 30, "datum": "WGS-84 (ISO 6709 default)"}
|
|
]
|
|
```
|
|
|
|
An XML file shares its `<?xml` magic with 47 other formats, so the
|
|
chosen format's disambiguation is recorded:
|
|
|
|
```json
|
|
{"file": "/tmp/fx/doc.xml", "size": 80, "identified": true,
|
|
"format": {"task": 839, "ext": ".XML", "name": "XML", "kind": "text", "endian": "little"},
|
|
"identified_by": "magic", "magic": {"bytes": "3c 3f 78 6d 6c", "offset": 0},
|
|
"other_candidates": [{"ext": ".POM", "task": 565}, {"ext": ".KML", "task": 341},
|
|
{"ext": ".GPX", "task": 266}],
|
|
"document": {"type": "xml", "declaration": "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
|
|
"root_element": "catalog", "version": "2.0"}}
|
|
```
|
|
|
|
(`other_candidates` above is abridged — the real output lists all 47.)
|
|
|
|
Unknown and unreadable files (exit 3 and 2):
|
|
|
|
```json
|
|
{"file": "/tmp/fx/mystery.zzz", "size": 64, "identified": false,
|
|
"error": "unknown format (no magic match, no known extension)",
|
|
"hexdump": "01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40"}
|
|
{"file": "/tmp/fx/does-not-exist.bin", "size": null, "identified": false,
|
|
"error": "No such file or directory"}
|
|
```
|
|
|
|
## Tests
|
|
|
|
`tests/test_json.py` (`make test-json`) builds synthetic fixtures
|
|
(PNG, SPS, JSON, XML, text, unknown, empty PNG, magic-only
|
|
WEBM/WAB, missing file) and checks that every `--json` line parses,
|
|
carries the same facts as the text output on the same file
|
|
(including the `Geotagging:` section vs the `geotags` array), and
|
|
exits with the same code. The geotag cases use the real fixtures in
|
|
`tests/geotag/` (EXIF GPS JPEG, two-result MP4, no-location JPEG).
|
|
Fixture builder: `tests/make_fixtures.py`.
|