exif: C++/STL parsing library

This page hosts a formal specification of exif using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream).
    • One can open a stream for reading from a local file:
      #include <fstream>
      
      std::ifstream is("path/to/local/file.exif", std::ifstream::binary);
    • Or one can prepare a stream for reading from existing std::string str:
      #include <sstream>
      
      std::istringstream is(str);
    • Or one can parse arbitrary char* buffer in memory, given that we know its size:
      #include <sstream>
      
      const char buf[] = { ... };
      std::string str(buf, sizeof buf);
      std::istringstream is(str);
  2. We need to wrap our input stream into Kaitai stream:
    #include <kaitai/kaitaistream.h>
    
    kaitai::kstream ks(&is);
  3. And finally, we can invoke the parsing:
    exif_t data(&ks);

After that, one can get various attributes from the structure by invoking getter methods like:

data.endianness() // => get endianness

C++/STL source code to parse exif

exif.h

#ifndef EXIF_H_
#define EXIF_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"

#include <stdint.h>

#if KAITAI_STRUCT_VERSION < 7000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.7 or later is required"
#endif
class exif_be_t;
class exif_le_t;

class exif_t : public kaitai::kstruct {

public:

    exif_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, exif_t* p__root = 0);

private:
    void _read();

public:
    ~exif_t();

private:
    uint16_t m_endianness;
    kaitai::kstruct* m_body;
    bool n_body;

public:
    bool _is_null_body() { body(); return n_body; };

private:
    exif_t* m__root;
    kaitai::kstruct* m__parent;

public:
    uint16_t endianness() const { return m_endianness; }
    kaitai::kstruct* body() const { return m_body; }
    exif_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // EXIF_H_

exif.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "exif.h"


#include "exif_be.h"
#include "exif_le.h"

exif_t::exif_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, exif_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    _read();
}

void exif_t::_read() {
    m_endianness = m__io->read_u2le();
    n_body = true;
    switch (endianness()) {
    case 18761: {
        n_body = false;
        m_body = new exif_le_t(m__io);
        break;
    }
    case 19789: {
        n_body = false;
        m_body = new exif_be_t(m__io);
        break;
    }
    }
}

exif_t::~exif_t() {
    if (!n_body) {
        delete m_body;
    }
}