exif: Ruby 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

Parse a local file and get structure in memory:

data = Exif.from_file("path/to/local/file.exif")

Or parse structure from a string of bytes:

bytes = "\x00\x01\x02..."
data = Exif.new(Kaitai::Struct::Stream.new(bytes))

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

data.endianness # => get endianness

Ruby source code to parse exif

exif.rb

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

require 'kaitai/struct/struct'

unless Gem::Version.new(Kaitai::Struct::VERSION) >= Gem::Version.new('0.7')
  raise "Incompatible Kaitai Struct Ruby API: 0.7 or later is required, but you have #{Kaitai::Struct::VERSION}"
end

class Exif < Kaitai::Struct::Struct
  def initialize(_io, _parent = nil, _root = self)
    super(_io, _parent, _root)
    _read
  end

  def _read
    @endianness = @_io.read_u2le
    case endianness
    when 18761
      @body = ExifLe.new(@_io)
    when 19789
      @body = ExifBe.new(@_io)
    end
    self
  end
  attr_reader :endianness
  attr_reader :body
end