Exiv2 (C++)
A C++ metadata library and command-line tool: typed Exif, IPTC and XMP containers over a per-format Image class hierarchy, a TIFF parser built as a Composite walked by Visitors, a large maker-note decoder, and a security history that turned a CVE-heavy parser into a fuzzed, bounds-checked one.
| Field | Value |
|---|---|
| Language | C++20 (README.md); XMP through the bundled Adobe XMP Toolkit (xmpsdk/) and expat |
| License | GPL-2.0-or-later (README.md) |
| Repository | Exiv2/exiv2 |
| Revision read | be479c1b7c27409582f62eaaac3a738130d869b1 (October 27, 2026, main, CMake version 1.00.0.9, the 1.0 development line); shallow clone, one commit |
| Documentation | exiv2.org; Doxygen comments in include/exiv2/; README.md; SECURITY.md |
| Category | Metadata: read, write and convert Exif, IPTC, XMP and ICC across about 30 still, RAW and video containers |
| Tier | B |
| Last reviewed | November 7, 2026 |
NOTE
Claims marked verified were demonstrated with crafted little-endian TIFF files (90 to 3690 bytes) in /tmp/exiv2-probe/ against the exiv2 0.28.8 command-line tool from nixpkgs (the pinned revision is later main; every check exercised below is present at both). The clone is shallow, so the history of individual CVE fixes could not be read from it. No performance number in this page was measured.
Overview
What it solves
Exiv2 reads and rewrites the image metadata embedded in a file without touching its pixels. The README states the scope in one line (README.md):
"Exiv2 is a C++ library and a command-line utility to read, write, delete and modify Exif, IPTC, XMP and ICC image metadata."
Every supported format has a class that knows where that format keeps each metadata kind (JPEG APP1/APP2/APP13 segments, PNG chunks, TIFF IFDs, ISOBMFF boxes, RIFF chunks), and three format-independent containers hold the decoded result. RAW formats such as CR2, NEF, ARW, ORF and RW2 are TIFF variants, so most of the library's depth is a TIFF and maker-note engine.
Design philosophy
The model is typed and keyed: every datum is a Key (Exif.Photo.ExposureTime, Iptc.Application2.Caption, Xmp.dc.subject) bound to a polymorphic Value of a declared type, held in a container with standard C++ iterators. The top-level class comment sets out the division of labour (image.hpp):
"Image has containers to store image metadata and subclasses implement read and save metadata from and to specific image formats. Most client apps will obtain an Image instance by calling a static ImageFactory method."
Writing is conservative by default: the encoder first tries to patch the original bytes in place and only rebuilds the structure when it must (exif.hpp). Safety is a process rather than a design principle: SECURITY.md defines a security bug as one reachable with "a simple command like: exiv2 poc-file" and adds "We also always treat OSS-Fuzz crashes [...] as security issues." The defences in the code (checked buffers, enforce, overflow-checked arithmetic, recursion limits) are retrofits, visible as comments such as // prevent out-of-bounds read in memcmp on next line in the JPEG reader (jpgimage.cpp).
How it works
ImageFactory and the format registry
ImageFactory::open takes a path, a memory block or a BasicIo, then probes a constexpr registry in order; each row names a constructor, a type check and the access mode per metadata kind (image.cpp):
constexpr Registry registry[] = {
// image type creation fct type check Exif mode IPTC mode XMP mode Comment mode
{ImageType::jpeg, newJpegInstance, isJpegType, amReadWrite, amReadWrite, amReadWrite, amReadWrite},
// ...
{ImageType::crw, newCrwInstance, isCrwType, amReadWrite, amNone, amNone, amReadWrite},
// ...
{ImageType::arw, newTiffInstance, isTiffType, amRead, amRead, amRead, amNone},
// ...
{ImageType::bmff, newBmffInstance, isBmffType, amRead, amRead, amRead, amNone},The matching row's constructor receives the IO object and an ImageCtorParams carrying max_recursion_depth, fixed at 1000 by the factory (image.cpp). Subclasses (JpegImage, PngImage, TiffImage, Cr2Image, BmffImage, QuickTimeVideo, …) implement the pure virtuals readMetadata() and writeMetadata() and fill exifData_, iptcData_, xmpData_, xmpPacket_, comment_ and iccProfile_ (image.hpp).
BasicIo: one abstract stream
BasicIo is a seekable byte stream with read, write, seek, tell, and an mmap() that gives "direct access to the IO data. For files, this is done by mapping the file into the process's address space; for memory blocks, this allows direct access to the memory block" (basicio.hpp). Implementations are FileIo, MemIo, XPathIo (stdin or a data URI spooled to a temporary file) and RemoteIo with HttpIo and CurlIo. RemoteIo fetches in blocks and offers populateFakeData() to "avoid allocating memory for parts of the file that contain image-date (non-metadata/pixel data)" (basicio.hpp), so a remote JPEG can be inspected without downloading its scan data.
Writes never edit the target in place at the stream level. JpegBase::writeMetadata renders the whole new file into a MemIo and swaps it in with transfer (jpgimage.cpp):
MemIo tempIo;
doWriteMetadata(tempIo); // may throw
io_->close();
io_->transfer(tempIo); // may throwThe three containers and the value model
ExifData is a std::list of Exifdatum (exif.hpp); operator[] inserts a missing key, so lookups that should not mutate go through findKey. IptcData and XmpData follow the same shape. Each datum's Value is created from a TypeId (types.hpp) that merges the twelve TIFF field types, the BigTIFF 8-byte types, IPTC string/date/time, and XMP xmpText, xmpAlt, xmpBag, xmpSeq and langAlt. The numeric values are one template, ValueType!T in D terms, instantiated as UShortValue, URationalValue, DoubleValue and so on (value.hpp).
Tag knowledge is compiled data. A TagInfo row holds tag number, name, title, description, IFD, section, expected type, expected count and a PrintFct that turns the raw value into a human string (tags.hpp); the IFD0 table opens with ProcessingSoftware at 0x000b (tags_int.cpp). Metadatum::print(const ExifData*) passes the whole container to that function (metadatum.hpp), because interpreting a maker-note lens id needs the camera make, model and other tags.
Cross-standard mapping is a table of pairs in convert.cpp, for example Exif.Image.Orientation to Xmp.tiff.Orientation (convert.cpp), exposed as copyExifToXmp, syncExifWithXmp, copyIptcToXmp and their inverses.
The TIFF engine: Composite plus Visitor
Exif is a TIFF structure, so every TIFF-family format and the Exif payload of JPEG, PNG, WebP and HEIF go through TiffParserWorker. The parsed tree is a Composite of TiffComponent nodes, each "uniquely identified by a tag, group tuple" and implemented "as a NVI (Non-Virtual Interface) [with] an interface for visitors (Visitor pattern)" (tiffcomposite_int.hpp). Node classes distinguish plain entries (TiffEntry), entries that point at data elsewhere (TiffDataEntry, TiffImageEntry for strips and tiles, TiffSizeEntry for their lengths), directories, sub-IFDs, maker-note entries (TiffMnEntry, TiffIfdMakernote) and binary arrays (TiffBinaryArray, TiffBinaryElement).
Which node a tag becomes is decided by TiffCreator::tiffGroupTable_, keyed by (tag, group) (tiffimage_int.cpp):
// IFD0
{{0x8769, IfdId::ifd0Id}, &newTiffSubIfd<IfdId::exifId>},
{{0x8825, IfdId::ifd0Id}, &newTiffSubIfd<IfdId::gpsId>},
{{0x0111, IfdId::ifd0Id}, &newTiffImageData<0x0117, IfdId::ifd0Id>},
{{0x0117, IfdId::ifd0Id}, &newTiffImageSize<0x0111, IfdId::ifd0Id>},
// ...
{{0xc634, IfdId::ifd0Id}, &newTiffMnEntry},
{{Tag::next, IfdId::ifd0Id}, &newTiffDirectory<IfdId::ifd1Id>},
{{Tag::all, IfdId::ifd0Id}, &newTiffEntry},Operations are visitors over that tree (tiffvisitor_int.hpp): TiffReader builds it from bytes, TiffDecoder copies it into ExifData/IptcData/ XmpData (decoding embedded XMP at tag 0x02bc and IPTC on the way), TiffEncoder writes container changes back into it, TiffCopier and TiffFinder support rewriting and lookup. A visitor can stop traversal or flag an unknown maker note through two GoEvent flags.
Maker notes
A maker note is a vendor blob inside Exif tag 0x927c, usually an IFD with its own header, sometimes its own byte order, and sometimes offsets relative to the maker note instead of the TIFF header. TiffRwState carries exactly those two degrees of freedom, with the comment that "some camera vendors encode their makernotes in TIFF IFDs using offsets relative to (somewhere near) the start of the makernote data" (tiffvisitor_int.hpp). TiffMnCreator::registry_ selects a parser by prefix of Exif.Image.Make ("Canon", "NIKON", "OLYMPUS", "SONY", …) and a second set of rows rebuilds a maker note by group when writing (makernote_int.cpp). Each vendor has an MnHeader subclass that reads its signature and reports ifdOffset(), byteOrder() and baseOffset(). Vendor sub-records that are packed arrays rather than IFDs are described by ArrayCfg/ArrayDef tables, including element type, size element, fillers and an optional CryptFct (Nikon's nikonCrypt) (tiffimage_int.cpp).
Writing: non-intrusive first
ExifParser::encode "is parsed and updated in-place if possible ('non-intrusive' writing). If that is not possible (e.g., if new tags were added), the entire Exif structure is re-written" (exif.hpp). For JPEG it then fits the result into one APP1 segment of at most 65527 bytes, dropping preview tags over 32 kB, then unknown tags over 4 kB and known tags over 20 kB (exif.cpp; the header comment says 40 kB), and "succeeds even if the end result is still larger than the allowed size" (exif.hpp). Verified: setting Exif.Image.Make from Abc to Xyz in a 90-byte TIFF changed three bytes and kept the size; adding a 21-byte Exif.Image.Artist rebuilt the file to 60 bytes, dropping the 64 unreferenced trailing bytes the original carried.
Analysis
1. Pixel and memory model
Not applicable, because Exiv2 never decodes pixels. Image records only pixelWidth_ and pixelHeight_, read from headers such as the JPEG SOFn segment (jpgimage.cpp).
2. Genericity mechanism
Not applicable to pixels. The genericity that exists is over metadata: a polymorphic Value hierarchy with one ValueType template for the numeric types, and virtual dispatch over the Image subclasses and the TIFF visitors.
3. Colour and metadata
Exiv2 is the clearest example in this catalog of the metadata half of Q5 done in full, and of the colour half left out. Exif, IPTC and XMP get typed keys, typed values, per-tag print functions and a conversion table between standards; the ICC profile is an opaque DataBuf whose only check is that the length in its first four bytes equals the buffer size (image.cpp). JPEG APP2 chunks are appended in file order, and the chunk and count bytes only decide when the length check runs (jpgimage.cpp, image.cpp); nothing parses tags, primaries or the transfer curve, and there is no CICP model. Orientation, the one tag a viewer must act on, is a convenience lookup that "will return expected/desired metadata most of the time" (easyaccess.hpp). PreviewManager extracts the embedded JPEG previews that RAW files carry, which is how a viewer can show a RAW file without a RAW decoder.
Compared with ExifTool, the split is where tag knowledge lives. ExifTool's tables are Perl hashes interpreted at run time, with per-tag write rules and conversion code inline (Exif.pm, Exif.pm); Exiv2 compiles the same kind of knowledge into constexpr C++ arrays (TagInfo, tiffGroupTable_, ArrayCfg) and adds a class per structural role. ExifTool changes by editing a table; Exiv2 gives a typed API, an in-process library and static dispatch, at the price of a recompile per new tag and a much larger C++ surface to secure.
4. I/O and codec architecture
The format layer is a closed, ordered registry of probe functions plus virtual readMetadata/writeMetadata; there is no plugin interface. All parsing is pull over BasicIo or over a fully buffered metadata block (ExifParser::decode takes a pointer and a size and "no checks are performed" on the format (exif.hpp)); nothing is suspendable in the sense of push and pull decoding. The container and bitstream split is clean in one direction only: Exiv2 parses containers and never decodes a bitstream.
The TIFF engine is the part with lessons for a declarative binary description (Q4). Its table rows describe what a binary-format DSL would have to express: byte order chosen at run time from the header, a second byte order and a second base offset inside a nested record (maker notes), offset/size tag pairs that must be resolved together (newTiffImageData<0x0117,…> with newTiffImageSize<0x0111,…>), packed arrays with optional size elements and encryption, and loops that the reader must break. The wired baseline lists the run-time byte order as gap G5 and names EXIF among the offset-addressed structures no acceptance set covers (G15); the nested base offset, the paired tags and cycle detection are not among its listed gaps.
5. Hostile input and safety
Exiv2 is a case study in retrofitting hostile-input defences onto a parser written for cooperative files. The tree carries 37 regression tests named after CVEs in tests/bugfixes/github/ (for example test_CVE_2017_11336.py, which pins the exact stderr for a 60-byte proof of concept) among 296 bug-fix test scripts in all (224 under github/, 72 under redmine/). The defences now present:
- Checked buffers.
DataBuf::read_uint8,cmpBytesandc_datathrowstd::out_of_rangeon an out-of-range offset (types.cpp);Sliceis a range-checked view (slice.hpp). enforceandSafe::add.enforce(cond, ErrorCode::kerCorruptedMetadata)throws on a failed precondition (enforce.hpp);Safe::addand friends wrap compiler overflow builtins (safe_op.hpp).- Structural limits.
TiffReaderrejects an IFD with more than 256 entries, refuses a directory it has already visited, and skips an entry whose count is at least0x10000000(tiffvisitor_int.cpp,tiffvisitor_int.cpp,tiffvisitor_int.cpp).BmffImage::boxHandlerthrows on a revisited box address, too many visits or depth pastmax_recursion_depth_(bmffimage.cpp). - Degrade, do not fail. Most TIFF violations log and truncate rather than throw, so damaged camera files still yield their good tags.
Verified with crafted little-endian TIFFs (exit status 0 in every case):
| Input | exiv2 -pa result |
|---|---|
| IFD0's next pointer points back at IFD0 | Thumbnail pointer references previously read Image directory; ignored.; Make still read |
| IFD0 entry count 300 | Directory Image with 300 entries considered invalid; not read.; no tags |
ASCII entry, count 64, offset 0x7fffffff | … is out of bounds: Offset = 0x7fffffff; truncating the entry; empty Software |
RATIONAL entry, count 0xffffffff | … has invalid size 4294967295*8; skipping entry. |
The 256-entry cap is a heuristic ("Sanity check with an 'unreasonably' large number"), not a format rule, so a legal TIFF with more entries loses its whole IFD silently except for a log line. Limits are not caller-configurable beyond the recursion depth, which ImageFactory hard-codes; a caller can choose it only by constructing the format class itself with an ImageCtorParams (image.hpp). See the hostile-input papers.
6. Allocation and ownership
Ownership is std::unique_ptr for Image and BasicIo and std::shared_ptr for TIFF tree children (TiffComponent::Components). Metadata lives in node-based std::list containers of heap-allocated Value objects, and writes build the whole output file in a MemIo before swapping. That suits a desktop tool editing one file; it is the opposite of a @nogc decoder.
7. CPU parallelism and SIMD
There is no internal parallelism. The README's guarantee is per instance: "Different instances of the same class can safely be used concurrently in multiple threads", with two exceptions, XmpParser::initialize and XmpProperties::registerNs, which the application must serialize before starting threads (README.md). There is no SIMD code.
8. GPU offload and interop
Not applicable, because Exiv2 has no pixel data to move to a GPU.
9. Temporal, animation and containers
The video classes (QuickTimeVideo, AsfVideo, RiffVideo, MatroskaVideo) read container metadata only, with no write support, and only when built with EXV_ENABLE_VIDEO (image.cpp). There is no frame or animation model.
10. Testing and conformance
Three layers: GoogleTest unit tests in unitTests/ (including test_safe_op.cpp, test_enforce.cpp, test_slice.cpp and XMP concurrency tests), a Python system-test runner in tests/ whose cases pin exact stdout and stderr per proof-of-concept file in test/data/, and libFuzzer targets in fuzz/. The main target opens, prints every datum, prints all six structure views and writes back (fuzz-read-print-write.cpp); it runs "for a short period of time on every pull request", with a dictionary generated by a CodeQL query that collects "string literals that are used in conditions" (fuzz/README.md), and on OSS-Fuzz, whose crashes count as security issues (SECURITY.md). Fuzzing the write path, not only the read path, is the notable choice: round-trip writes are where in-place patching meets corrupt offsets.
Lessons for Sparkles
- Q5. The minimum metadata model has more than one standard in it: Exif, IPTC and XMP each need their own key space and value types, plus a mapping table between them (
convert.cpp). ICC can start as an opaque, length-checked blob, as Exiv2's does, provided the colour model elsewhere parses it. - Q5. Human-readable printing needs context beyond the tag (
print(const ExifData*)); a D inspector built on the font-explorerinspecttemplate needs the same access to sibling tags. - Q4. Exif and maker notes are a stress test for a
sparkles:wiredbinary description: run-time byte order, nested base offsets, paired offset/size tags, packed vendor arrays and cycle detection. Exiv2 handles them with hand-written classes plusconstexprtables; a D description could make the tables the single source and generate the walkers by introspection. - Q4, Q6. Exiv2's pull-over-
BasicIodesign with whole-file rewrite through a temporary does not transfer to a completion-based loop. What does transfer isRemoteIo's block map: fetch metadata ranges, mark pixel ranges as not needed. - Q8. The CVE-named regression corpus with pinned stderr, and a fuzz target that exercises read, print and write, are directly reusable practices; Exiv2's
test/data/proofs of concept can serve as a differential corpus for a D Exif reader (license permitting). - Q8. The retrofit history argues for limits and checked reads from the first commit. D's bounds-checked slices give the
DataBuf/Sliceguarantees for free in@safecode;Expectedreplacesenforce-and-throw innothrow @nogcpaths. - Q9. Exiv2 is a metadata library separate from any codec, and it still needs a container parser per format. That is evidence that format container parsing is shared between a metadata package and a pixel codec package, whichever owns it.
Strengths
- Typed, iterable containers for Exif, IPTC and XMP with one key syntax.
- Maker-note decoders for 15 camera-make prefixes in its registry, with per-vendor headers, offsets and encrypted binary arrays.
- Non-intrusive in-place Exif updates that preserve unknown structures.
- Remote and memory IO behind one
BasicIointerface. - A mature security process: OSS-Fuzz, per-PR fuzzing, CVE regression tests, overflow helpers.
Weaknesses
- GPL-2.0-or-later, which rules out vendoring into permissively licensed code.
- A large hand-written C++ parser surface with a long CVE record; defences are retrofits and heuristics (the 256-entry cap).
- No caller-configurable limits;
ImageFactoryfixes the recursion depth at 1000. - Exceptions everywhere,
std::listof heapValueobjects, whole-file rewrite through memory. - ICC is a blob; no CICP or colour semantics.
- XMP depends on the Adobe XMP Toolkit, whose initialisation is not thread-safe.
Key design decisions and trade-offs
| Decision | Rationale | Trade-off |
|---|---|---|
Compiled tag tables (TagInfo, tiffGroupTable_) | Typed keys and static dispatch (tags.hpp) | A new tag needs a recompile |
| Composite tree walked by visitors | One parse serves read, decode, encode and copy (tiffcomposite_int.hpp) | A large class surface to secure |
| Non-intrusive writing first | Preserves unknown structures (exif.hpp) | Two write paths; the intrusive one drops data to fit APP1 |
| Log and truncate on violations | Damaged camera files still yield their good tags | Silent data loss, such as the 256-entry cap |
Sources
- Exiv2 source at
be479c1b7c27409582f62eaaac3a738130d869b1:include/exiv2/,src/,fuzz/,tests/,README.md,SECURITY.md. - ExifTool source at
2200871d9cef988051d2a99d67df3bda6cbb30a8,lib/Image/ExifTool/Exif.pm. - Probes:
/tmp/exiv2-probe/gen.pyand the crafted TIFFs, run with nixpkgsexiv20.28.8; reproduced independently with/tmp/exiv2-verify/gen.dagainst the same binary.