ZX Spectrum tape file: C++98/STL parsing library

TAP files are used by emulators of ZX Spectrum computer (released in 1982 by Sinclair Research). TAP file stores blocks of data as if they are written to magnetic tape, which was used as primary media for ZX Spectrum. Contents of this file can be viewed as a very simple linear filesystem, storing named files with some basic metainformation prepended as a header.

File extension

tap

KS implementation details

License: CC0-1.0

References

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

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

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.tap", 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:
    zx_spectrum_tap_t data(&ks);
    

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

data.blocks() // => get blocks

C++98/STL source code to parse ZX Spectrum tape file

zx_spectrum_tap.h

#ifndef ZX_SPECTRUM_TAP_H_
#define ZX_SPECTRUM_TAP_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 < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * TAP files are used by emulators of ZX Spectrum computer (released in
 * 1982 by Sinclair Research). TAP file stores blocks of data as if
 * they are written to magnetic tape, which was used as primary media
 * for ZX Spectrum. Contents of this file can be viewed as a very
 * simple linear filesystem, storing named files with some basic
 * metainformation prepended as a header.
 * \sa https://sinclair.wiki.zxnet.co.uk/wiki/TAP_format Source
 */

class zx_spectrum_tap_t : public kaitai::kstruct {

public:
    class block_t;
    class program_params_t;
    class bytes_params_t;
    class header_t;
    class array_params_t;

    enum flag_enum_t {
        FLAG_ENUM_HEADER = 0,
        FLAG_ENUM_DATA = 255
    };

    enum header_type_enum_t {
        HEADER_TYPE_ENUM_PROGRAM = 0,
        HEADER_TYPE_ENUM_NUM_ARRAY = 1,
        HEADER_TYPE_ENUM_CHAR_ARRAY = 2,
        HEADER_TYPE_ENUM_BYTES = 3
    };

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

private:
    void _read();
    void _clean_up();

public:
    ~zx_spectrum_tap_t();

    class block_t : public kaitai::kstruct {

    public:

        block_t(kaitai::kstream* p__io, zx_spectrum_tap_t* p__parent = 0, zx_spectrum_tap_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~block_t();

    private:
        uint16_t m_len_block;
        flag_enum_t m_flag;
        header_t* m_header;
        bool n_header;

    public:
        bool _is_null_header() { header(); return n_header; };

    private:
        std::string m_data;
        bool n_data;

    public:
        bool _is_null_data() { data(); return n_data; };

    private:
        std::string m_headerless_data;
        bool n_headerless_data;

    public:
        bool _is_null_headerless_data() { headerless_data(); return n_headerless_data; };

    private:
        zx_spectrum_tap_t* m__root;
        zx_spectrum_tap_t* m__parent;

    public:
        uint16_t len_block() const { return m_len_block; }
        flag_enum_t flag() const { return m_flag; }
        header_t* header() const { return m_header; }
        std::string data() const { return m_data; }
        std::string headerless_data() const { return m_headerless_data; }
        zx_spectrum_tap_t* _root() const { return m__root; }
        zx_spectrum_tap_t* _parent() const { return m__parent; }
    };

    class program_params_t : public kaitai::kstruct {

    public:

        program_params_t(kaitai::kstream* p__io, zx_spectrum_tap_t::header_t* p__parent = 0, zx_spectrum_tap_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~program_params_t();

    private:
        uint16_t m_autostart_line;
        uint16_t m_len_program;
        zx_spectrum_tap_t* m__root;
        zx_spectrum_tap_t::header_t* m__parent;

    public:
        uint16_t autostart_line() const { return m_autostart_line; }
        uint16_t len_program() const { return m_len_program; }
        zx_spectrum_tap_t* _root() const { return m__root; }
        zx_spectrum_tap_t::header_t* _parent() const { return m__parent; }
    };

    class bytes_params_t : public kaitai::kstruct {

    public:

        bytes_params_t(kaitai::kstream* p__io, zx_spectrum_tap_t::header_t* p__parent = 0, zx_spectrum_tap_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~bytes_params_t();

    private:
        uint16_t m_start_address;
        std::string m_reserved;
        zx_spectrum_tap_t* m__root;
        zx_spectrum_tap_t::header_t* m__parent;

    public:
        uint16_t start_address() const { return m_start_address; }
        std::string reserved() const { return m_reserved; }
        zx_spectrum_tap_t* _root() const { return m__root; }
        zx_spectrum_tap_t::header_t* _parent() const { return m__parent; }
    };

    class header_t : public kaitai::kstruct {

    public:

        header_t(kaitai::kstream* p__io, zx_spectrum_tap_t::block_t* p__parent = 0, zx_spectrum_tap_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~header_t();

    private:
        header_type_enum_t m_header_type;
        std::string m_filename;
        uint16_t m_len_data;
        kaitai::kstruct* m_params;
        bool n_params;

    public:
        bool _is_null_params() { params(); return n_params; };

    private:
        uint8_t m_checksum;
        zx_spectrum_tap_t* m__root;
        zx_spectrum_tap_t::block_t* m__parent;

    public:
        header_type_enum_t header_type() const { return m_header_type; }
        std::string filename() const { return m_filename; }
        uint16_t len_data() const { return m_len_data; }
        kaitai::kstruct* params() const { return m_params; }

        /**
         * Bitwise XOR of all bytes including the flag byte
         */
        uint8_t checksum() const { return m_checksum; }
        zx_spectrum_tap_t* _root() const { return m__root; }
        zx_spectrum_tap_t::block_t* _parent() const { return m__parent; }
    };

    class array_params_t : public kaitai::kstruct {

    public:

        array_params_t(kaitai::kstream* p__io, zx_spectrum_tap_t::header_t* p__parent = 0, zx_spectrum_tap_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~array_params_t();

    private:
        uint8_t m_reserved;
        uint8_t m_var_name;
        std::string m_reserved1;
        zx_spectrum_tap_t* m__root;
        zx_spectrum_tap_t::header_t* m__parent;

    public:
        uint8_t reserved() const { return m_reserved; }

        /**
         * Variable name (1..26 meaning A$..Z$ +192)
         */
        uint8_t var_name() const { return m_var_name; }
        std::string reserved1() const { return m_reserved1; }
        zx_spectrum_tap_t* _root() const { return m__root; }
        zx_spectrum_tap_t::header_t* _parent() const { return m__parent; }
    };

private:
    std::vector<block_t*>* m_blocks;
    zx_spectrum_tap_t* m__root;
    kaitai::kstruct* m__parent;

public:
    std::vector<block_t*>* blocks() const { return m_blocks; }
    zx_spectrum_tap_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // ZX_SPECTRUM_TAP_H_

zx_spectrum_tap.cpp

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

#include "zx_spectrum_tap.h"
#include "kaitai/exceptions.h"

zx_spectrum_tap_t::zx_spectrum_tap_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, zx_spectrum_tap_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_blocks = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void zx_spectrum_tap_t::_read() {
    m_blocks = new std::vector<block_t*>();
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_blocks->push_back(new block_t(m__io, this, m__root));
            i++;
        }
    }
}

zx_spectrum_tap_t::~zx_spectrum_tap_t() {
    _clean_up();
}

void zx_spectrum_tap_t::_clean_up() {
    if (m_blocks) {
        for (std::vector<block_t*>::iterator it = m_blocks->begin(); it != m_blocks->end(); ++it) {
            delete *it;
        }
        delete m_blocks; m_blocks = 0;
    }
}

zx_spectrum_tap_t::block_t::block_t(kaitai::kstream* p__io, zx_spectrum_tap_t* p__parent, zx_spectrum_tap_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_header = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void zx_spectrum_tap_t::block_t::_read() {
    m_len_block = m__io->read_u2le();
    m_flag = static_cast<zx_spectrum_tap_t::flag_enum_t>(m__io->read_u1());
    n_header = true;
    if ( ((len_block() == 19) && (flag() == zx_spectrum_tap_t::FLAG_ENUM_HEADER)) ) {
        n_header = false;
        m_header = new header_t(m__io, this, m__root);
    }
    n_data = true;
    if (len_block() == 19) {
        n_data = false;
        m_data = m__io->read_bytes((header()->len_data() + 4));
    }
    n_headerless_data = true;
    if (flag() == zx_spectrum_tap_t::FLAG_ENUM_DATA) {
        n_headerless_data = false;
        m_headerless_data = m__io->read_bytes((len_block() - 1));
    }
}

zx_spectrum_tap_t::block_t::~block_t() {
    _clean_up();
}

void zx_spectrum_tap_t::block_t::_clean_up() {
    if (!n_header) {
        if (m_header) {
            delete m_header; m_header = 0;
        }
    }
    if (!n_data) {
    }
    if (!n_headerless_data) {
    }
}

zx_spectrum_tap_t::program_params_t::program_params_t(kaitai::kstream* p__io, zx_spectrum_tap_t::header_t* p__parent, zx_spectrum_tap_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void zx_spectrum_tap_t::program_params_t::_read() {
    m_autostart_line = m__io->read_u2le();
    m_len_program = m__io->read_u2le();
}

zx_spectrum_tap_t::program_params_t::~program_params_t() {
    _clean_up();
}

void zx_spectrum_tap_t::program_params_t::_clean_up() {
}

zx_spectrum_tap_t::bytes_params_t::bytes_params_t(kaitai::kstream* p__io, zx_spectrum_tap_t::header_t* p__parent, zx_spectrum_tap_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void zx_spectrum_tap_t::bytes_params_t::_read() {
    m_start_address = m__io->read_u2le();
    m_reserved = m__io->read_bytes(2);
}

zx_spectrum_tap_t::bytes_params_t::~bytes_params_t() {
    _clean_up();
}

void zx_spectrum_tap_t::bytes_params_t::_clean_up() {
}

zx_spectrum_tap_t::header_t::header_t(kaitai::kstream* p__io, zx_spectrum_tap_t::block_t* p__parent, zx_spectrum_tap_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void zx_spectrum_tap_t::header_t::_read() {
    m_header_type = static_cast<zx_spectrum_tap_t::header_type_enum_t>(m__io->read_u1());
    m_filename = kaitai::kstream::bytes_strip_right(m__io->read_bytes(10), 32);
    m_len_data = m__io->read_u2le();
    n_params = true;
    switch (header_type()) {
    case zx_spectrum_tap_t::HEADER_TYPE_ENUM_PROGRAM: {
        n_params = false;
        m_params = new program_params_t(m__io, this, m__root);
        break;
    }
    case zx_spectrum_tap_t::HEADER_TYPE_ENUM_NUM_ARRAY: {
        n_params = false;
        m_params = new array_params_t(m__io, this, m__root);
        break;
    }
    case zx_spectrum_tap_t::HEADER_TYPE_ENUM_CHAR_ARRAY: {
        n_params = false;
        m_params = new array_params_t(m__io, this, m__root);
        break;
    }
    case zx_spectrum_tap_t::HEADER_TYPE_ENUM_BYTES: {
        n_params = false;
        m_params = new bytes_params_t(m__io, this, m__root);
        break;
    }
    }
    m_checksum = m__io->read_u1();
}

zx_spectrum_tap_t::header_t::~header_t() {
    _clean_up();
}

void zx_spectrum_tap_t::header_t::_clean_up() {
    if (!n_params) {
        if (m_params) {
            delete m_params; m_params = 0;
        }
    }
}

zx_spectrum_tap_t::array_params_t::array_params_t(kaitai::kstream* p__io, zx_spectrum_tap_t::header_t* p__parent, zx_spectrum_tap_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void zx_spectrum_tap_t::array_params_t::_read() {
    m_reserved = m__io->read_u1();
    m_var_name = m__io->read_u1();
    m_reserved1 = m__io->read_bytes(2);
    if (!(reserved1() == std::string("\x00\x80", 2))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x00\x80", 2), reserved1(), _io(), std::string("/types/array_params/seq/2"));
    }
}

zx_spectrum_tap_t::array_params_t::~array_params_t() {
    _clean_up();
}

void zx_spectrum_tap_t::array_params_t::_clean_up() {
}