.dcx file format: C++/STL parsing library

DCX is a simple extension of PCX image format allowing to bundle many PCX images (typically, pages of a document) in one file. It saw some limited use in DOS-era fax software, but was largely superseeded with multi-page TIFFs and PDFs since then.

File extension

dcx

KS implementation details

License: CC0-1.0

References

This page hosts a formal specification of .dcx file format 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 local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.dcx", std::ifstream::binary);
    #include <sstream>
    
    std::istringstream is(str);
    #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:
    pcx_dcx_t data(&ks);

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

data.magic() // => get magic

C++/STL source code to parse .dcx file format

pcx_dcx.h

#ifndef PCX_DCX_H_
#define PCX_DCX_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>
#include <vector>

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

/**
 * DCX is a simple extension of PCX image format allowing to bundle
 * many PCX images (typically, pages of a document) in one file. It saw
 * some limited use in DOS-era fax software, but was largely
 * superseeded with multi-page TIFFs and PDFs since then.
 */

class pcx_dcx_t : public kaitai::kstruct {

public:
    class pcx_offset_t;

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

private:
    void _read();

public:
    ~pcx_dcx_t();

    class pcx_offset_t : public kaitai::kstruct {

    public:

        pcx_offset_t(kaitai::kstream* p__io, pcx_dcx_t* p__parent = 0, pcx_dcx_t* p__root = 0);

    private:
        void _read();

    public:
        ~pcx_offset_t();

    private:
        bool f_body;
        pcx_t* m_body;
        bool n_body;

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

    private:

    public:
        pcx_t* body();

    private:
        uint32_t m_ofs_body;
        pcx_dcx_t* m__root;
        pcx_dcx_t* m__parent;

    public:
        uint32_t ofs_body() const { return m_ofs_body; }
        pcx_dcx_t* _root() const { return m__root; }
        pcx_dcx_t* _parent() const { return m__parent; }
    };

private:
    std::string m_magic;
    std::vector<pcx_offset_t*>* m_files;
    pcx_dcx_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::string magic() const { return m_magic; }
    std::vector<pcx_offset_t*>* files() const { return m_files; }
    pcx_dcx_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // PCX_DCX_H_

pcx_dcx.cpp

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

#include "pcx_dcx.h"


#include "pcx.h"

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

void pcx_dcx_t::_read() {
    m_magic = m__io->ensure_fixed_contents(std::string("\xB1\x68\xDE\x3A", 4));
    m_files = new std::vector<pcx_offset_t*>();
    {
        int i = 0;
        pcx_offset_t* _;
        do {
            _ = new pcx_offset_t(m__io, this, m__root);
            m_files->push_back(_);
            i++;
        } while (!(_->ofs_body() == 0));
    }
}

pcx_dcx_t::~pcx_dcx_t() {
    for (std::vector<pcx_offset_t*>::iterator it = m_files->begin(); it != m_files->end(); ++it) {
        delete *it;
    }
    delete m_files;
}

pcx_dcx_t::pcx_offset_t::pcx_offset_t(kaitai::kstream* p__io, pcx_dcx_t* p__parent, pcx_dcx_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_body = false;
    _read();
}

void pcx_dcx_t::pcx_offset_t::_read() {
    m_ofs_body = m__io->read_u4le();
}

pcx_dcx_t::pcx_offset_t::~pcx_offset_t() {
    if (f_body && !n_body) {
        delete m_body;
    }
}

pcx_t* pcx_dcx_t::pcx_offset_t::body() {
    if (f_body)
        return m_body;
    n_body = true;
    if (ofs_body() != 0) {
        n_body = false;
        std::streampos _pos = m__io->pos();
        m__io->seek(ofs_body());
        m_body = new pcx_t(m__io);
        m__io->seek(_pos);
    }
    f_body = true;
    return m_body;
}