# libsfile — the sfile decode engine as a C library The `sfile` CLI is a thin wrapper over `libsfile`: the decode engine (`src/sfile_lib.c`, public API in `src/sfile.h`) plus the geotag decoders (`src/geotag.c`, API in `src/geotag.h`). Other tools (stag, sgeo, scar, …) can link it instead of shelling out to the `sfile` binary — and get exactly the same results the CLI prints, since the CLI consumes the same `sfile_result` struct. ## Building ```sh make lib # libsfile.a + libsfile.so.1.1.0 (+ .so.1/.so links) make # also builds ./sfile and ./scar make install # /usr/local/{bin,lib,include} (PREFIX=... to change) ``` `make install` puts `libsfile.a`, `libsfile.so.1.1.0` (plus the `libsfile.so.1` / `libsfile.so` symlinks) in `$(PREFIX)/lib` and `sfile.h` + `geotag.h` in `$(PREFIX)/include`. The library is new in sfile 1.1.0; `sfile_result` is a plain struct with no ABI stability promise across versions — recompile dependents when upgrading. ## API (`src/sfile.h`) | function | what it does | |---|---| | `sfile_decode_file(path, res)` | decode a file from disk; returns 0 identified, 2 unreadable, 3 unknown (the CLI exit codes). Also runs `geotag_scan()` for the `geotags` fields. | | `sfile_decode_mem(buf, n, display_name, fsize, res)` | decode bytes already in memory; same return codes. `display_name` is used for the extension fallback. No geotag scan (no path to scan — call `geotag_scan()` yourself if you have one). | | `sfile_free_result(res)` | release everything the decode allocated. | | `sfile_result_to_json(res)` | serialize the result as one compact JSON object (see `docs/json.md`); malloc'd, caller frees. | | `sfile_format_count()` / `sfile_get_format_info(i, info)` | format-table introspection (powers `sfile --list`). | | `sfile_field_kind_name(kind)` | `"u8"` / `"u16"` / … / `"ascii"` / `"bytes"`. | | `geotag_scan(path, task, out, max)` | (`src/geotag.h`) fill `out[]` with the locations found (EXIF GPS, XMP, QuickTime, GeoTIFF, EXR, PSD, CR3); returns the count, 0 when the format has a mechanism but no location, -1 when it has none. | `sfile_result` carries the path and size, the identified format (task/ext/name/kind/endian, `identified_by`, magic bytes, the other formats sharing the magic), the decoded header fields (`offset`, `size`, `name`, `kind`, `value` formatted exactly as the CLI prints it, plus a JSON-friendly `raw` and the numeric value), the text fallback decode (`doc_type` + XML/JSON/first-line strings), the hex dump for unknown/no-layout files, and the geotags (`geotags[8]` + `ngeotags` + `geotag_status`, the `geotag_scan()` return: -1 no mechanism, 0 none found, >0 count). Ownership: strings pointing into the format table (format name/ext, field names, candidate exts) are borrowed — do not free them. Everything else in the struct is owned and freed by `sfile_free_result()`. Threading: the table walk holds no shared state, except the geotag scan, which reuses `geotag.c`'s static result buffer — concurrent `sfile_decode_file()` calls are not thread-safe; serialize them or use `sfile_decode_mem()`. ## Minimal example ```c /* minimal.c - decode a file with libsfile and print its format + fields */ #include #include "sfile.h" int main(int argc, char **argv) { if (argc < 2) return 1; sfile_result r; int st = sfile_decode_file(argv[1], &r); if (st != 0) { printf("cannot decode: %s\n", r.error); sfile_free_result(&r); return st; } printf("%s (%s), task %d, %d fields\n", r.name, r.ext, r.task, r.nfields); for (int i = 0; i < r.nfields; i++) printf(" %s = %s\n", r.fields[i].name, r.fields[i].value); if (r.ngeotags > 0) printf("geotag: %g, %g (%s)\n", r.geotags[0].lat, r.geotags[0].lon, r.geotags[0].source); sfile_free_result(&r); return 0; } ``` ```sh # shared library gcc -O2 -Wall -Wextra -std=c11 -Isrc -o minimal minimal.c -L. -lsfile -Wl,-rpath,$PWD # or static gcc -O2 -Wall -Wextra -std=c11 -Isrc -o minimal minimal.c libsfile.a ``` Real output (verified against both): ``` $ ./minimal sample.png PNG image (.PNG), task 564, 10 fields signature = 89 50 4e 47 0d 0a 1a 0a ihdr_length = 13 ihdr_type = "IHDR" width = 800 height = 600 bit_depth = 8 color_type = 2 compression = 0 filter = 0 interlace = 0 $ ./minimal test-gps.jpg JPEG image (.JPG), task 332, 4 fields soi = ff d8 marker = ff e1 segment_length = 258 identifier = "Exif" geotag: 37.7749, -122.419 (EXIF GPS IFD (JPEG APP1 segment)) ``` The field values are the CLI's own strings (`"IHDR"` quoted, bytes as hex), so a program printing them reproduces the CLI's header table exactly. `tests/test_library.c` is a fuller example (it also exercises `sfile_decode_mem()` and `sfile_result_to_json()`). ## Tests `tests/test_lib.sh` (`make test-lib`) compiles `tests/test_library.c` against the static library *and* the shared library, then diffs the field lines each prints against the `./sfile` CLI's header table on the same files, checks the `--mem` path, the EXIF GPS geotag (JPEG fixture), and that the library's own JSON parses and carries the right geotags.