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.
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.
All parsing code for C++11/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.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
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);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
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
#pragma once
// 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 <memory>
#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 = nullptr, zx_spectrum_tap_t* p__root = nullptr);
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 = nullptr, zx_spectrum_tap_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~block_t();
private:
uint16_t m_len_block;
flag_enum_t m_flag;
std::unique_ptr<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.get(); }
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 = nullptr, zx_spectrum_tap_t* p__root = nullptr);
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 = nullptr, zx_spectrum_tap_t* p__root = nullptr);
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 = nullptr, zx_spectrum_tap_t* p__root = nullptr);
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;
std::unique_ptr<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.get(); }
/**
* 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 = nullptr, zx_spectrum_tap_t* p__root = nullptr);
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::unique_ptr<std::vector<std::unique_ptr<block_t>>> m_blocks;
zx_spectrum_tap_t* m__root;
kaitai::kstruct* m__parent;
public:
std::vector<std::unique_ptr<block_t>>* blocks() const { return m_blocks.get(); }
zx_spectrum_tap_t* _root() const { return m__root; }
kaitai::kstruct* _parent() const { return m__parent; }
};
// 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 = nullptr;
_read();
}
void zx_spectrum_tap_t::_read() {
m_blocks = std::unique_ptr<std::vector<std::unique_ptr<block_t>>>(new std::vector<std::unique_ptr<block_t>>());
{
int i = 0;
while (!m__io->is_eof()) {
m_blocks->push_back(std::move(std::unique_ptr<block_t>(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() {
}
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 = nullptr;
_read();
}
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 = std::unique_ptr<header_t>(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 (!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;
_read();
}
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;
_read();
}
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;
_read();
}
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 = std::unique_ptr<program_params_t>(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 = std::unique_ptr<array_params_t>(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 = std::unique_ptr<array_params_t>(new array_params_t(m__io, this, m__root));
break;
}
case zx_spectrum_tap_t::HEADER_TYPE_ENUM_BYTES: {
n_params = false;
m_params = std::unique_ptr<bytes_params_t>(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) {
}
}
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;
_read();
}
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() {
}