From b093bf432a7d3a8178950eaa4e8c0491217630f9 Mon Sep 17 00:00:00 2001 From: mlong Date: Mon, 8 Mar 2021 11:34:43 -0600 Subject: [PATCH] Adding asdisk converters --- src/diskfiles/diskstore/asdiskdata.cpp | 175 +++++++++++++++++++++ src/diskfiles/diskstore/asdiskdata.h | 116 ++++++++++++++ src/diskfiles/diskstore/asdiskimporter.cpp | 112 +++++++++++++ src/diskfiles/diskstore/asdiskimporter.h | 36 +++++ src/diskfiles/diskstore/asdiskstore.cpp | 24 +++ src/diskfiles/diskstore/asdiskstore.h | 34 ++++ src/src.pro | 10 +- 7 files changed, 505 insertions(+), 2 deletions(-) create mode 100644 src/diskfiles/diskstore/asdiskdata.cpp create mode 100644 src/diskfiles/diskstore/asdiskdata.h create mode 100644 src/diskfiles/diskstore/asdiskimporter.cpp create mode 100644 src/diskfiles/diskstore/asdiskimporter.h create mode 100644 src/diskfiles/diskstore/asdiskstore.cpp create mode 100644 src/diskfiles/diskstore/asdiskstore.h diff --git a/src/diskfiles/diskstore/asdiskdata.cpp b/src/diskfiles/diskstore/asdiskdata.cpp new file mode 100644 index 0000000..cfda6c6 --- /dev/null +++ b/src/diskfiles/diskstore/asdiskdata.cpp @@ -0,0 +1,175 @@ +/***************************************************************************** +* AppleSAWS - The Apple Software Analysis WorkShop * +* Copyright (C) 2015-2021 Mark D. Long * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program. If not, see . * +*****************************************************************************/ + +#include "asdiskdata.h" + +ASDiskData::ASDiskData() +{ + +} + +ASDiskData::~ASDiskData() +{ + +} + +void ASDiskData::setUseSectors(int numSectors, int numTracks) +{ + m_numsectors = numSectors; + m_numtracks = numTracks; + m_dataformat = DataFormat::Sectors; +} + +void ASDiskData::setUseBlocks(int numBlocks) +{ + m_numblocks = numBlocks; + m_dataformat = DataFormat::Blocks; +} + +void ASDiskData::addSector(int track, int sector, QByteArray sectordata) +{ + auto offset = tsToOffset(track,sector); + m_chunks[offset] = sectordata; +} + +void ASDiskData::addBlock(int number, QByteArray blockdata) +{ + m_chunks[number] = blockdata; +} + +ASDiskData::SectorData &ASDiskData::getSector(int track, int sector) +{ + auto offset = tsToOffset(track,sector); + return m_chunks[offset]; +} + +ASDiskData::BlockData &ASDiskData::getBlock(int blocknum) +{ + return m_chunks[blocknum]; +} + +int ASDiskData::numTracks() const +{ + return m_numtracks; +} + +int ASDiskData::numSectorsPerTrack() const +{ + return m_numsectors; +} + +int ASDiskData::sectorSize() const +{ + return m_sectorsize; +} + +int ASDiskData::numBlocks() const +{ + return m_numblocks; +} + +int ASDiskData::blockSize() const +{ + return m_blocksize; +} + +QDataStream &ASDiskData::read(QDataStream &dataStream) +{ + QString id; + dataStream >> id; + if (id == "ASDiskData") + { + quint32 version; + dataStream >> version; + + if (version == 1) + { + bool doWeUseSectors; + dataStream >> doWeUseSectors; + if (doWeUseSectors) + { + m_dataformat = DataFormat::Sectors; + } + else + { + m_dataformat = DataFormat::Blocks; + } + dataStream >> m_sectorsize; + dataStream >> m_blocksize; + dataStream >> m_numsectors; + dataStream >> m_numtracks; + dataStream >> m_numblocks; + dataStream >> m_filename; + dataStream >> m_fstype; + dataStream >> m_fsname; + m_metadata.clear(); + dataStream >> m_metadata; + m_chunks.clear(); + dataStream >> m_chunks; + m_original_file_contents.clear(); + dataStream >> m_original_file_contents; + } + else + { + qWarning("Unrecognized version in ASDiskData stream!"); + } + } + else + { + qWarning("Cannot read ASDiskData from input stream!"); + } + + return dataStream; +} + +QDataStream &ASDiskData::write(QDataStream &dataStream) const +{ + dataStream << "ASDiskData" << quint32(1); // ID & Version + dataStream << (m_dataformat == DataFormat::Sectors); + dataStream << m_sectorsize << m_blocksize; + dataStream << m_numsectors << m_numtracks << m_numblocks; + dataStream << m_filename; + dataStream << m_fstype; + dataStream << m_fsname; + dataStream << m_metadata; + dataStream << m_chunks; + dataStream << m_original_file_contents; + + return dataStream; +} + +void ASDiskData::setFSInfo(QString name, int fstypeval) +{ + m_fsname = name; + m_fstype = fstypeval; +} + +int ASDiskData::tsToOffset(int track, int sector) +{ + return (track * m_numsectors) + sector; +} + +QDataStream &operator<<(QDataStream &out, const ASDiskData &outObject) +{ + return outObject.write(out); +} + +QDataStream &operator>>(QDataStream &in, ASDiskData &inObject) +{ + return inObject.read(in); +} diff --git a/src/diskfiles/diskstore/asdiskdata.h b/src/diskfiles/diskstore/asdiskdata.h new file mode 100644 index 0000000..b7299cb --- /dev/null +++ b/src/diskfiles/diskstore/asdiskdata.h @@ -0,0 +1,116 @@ +/***************************************************************************** +* AppleSAWS - The Apple Software Analysis WorkShop * +* Copyright (C) 2015-2021 Mark D. Long * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program. If not, see . * +*****************************************************************************/ + +#ifndef ASDISKDATA_H +#define ASDISKDATA_H + +#include +#include +#include +#include + +class ASDiskData +{ + using AttributeMap = QMap; + using ChunkData = QByteArray; + using DataMap = QMap; + +public: + using SectorData = QByteArray; + using BlockData = QByteArray; + + enum class DataFormat { Sectors, Blocks }; + + ASDiskData(); + virtual ~ASDiskData(); + + QStringList attributeList() const { return m_metadata.keys(); } + QVariant getAttribute(QString key) const; + bool setAttribute(QString key, QVariant value); + + void setUseSectors(int numSectors = 16, int numTracks = 35); + void setUseBlocks(int numBlocks); + void setDataFormat(DataFormat format) { m_dataformat = format; } + + bool useSectors() const { return m_dataformat == DataFormat::Sectors; } + bool useBlocks() const { return m_dataformat == DataFormat::Blocks; } + DataFormat dataFormat() const { return m_dataformat; } + + void addSector(int track, int sector, QByteArray sectordata); + void addBlock(int number, QByteArray blockdata); + + SectorData &getSector(int track, int sector); + BlockData &getBlock(int blocknum); + + int numTracks() const; + int numSectorsPerTrack() const; + void setNumSectorsPerTrack(int val) { m_numsectors = val; } + void setNumTracks(int val) { m_numtracks = val; } + void setSectorSize(int size) { m_sectorsize = size; } + int sectorSize() const; + + int numBlocks() const; + void setNumBlocks(int val) { m_numblocks = val; } + void setBlockSize(int size) { m_blocksize = size; } + int blockSize() const; + + void setFilename(const QString &filename) { m_filename = filename; } + const QString &filename() const { return m_filename; } + + void setFSInfo(QString name, int fstypeval); + + QString fileSystemName() const { return m_fsname; }; + int fileSystemType() const { return m_fstype; } + + void setOriginalFileContents(QByteArray data) { m_original_file_contents = data; } + QByteArray originalFileContents() const { return m_original_file_contents; } + + QDataStream &read(QDataStream &dataStream); + QDataStream &write(QDataStream &dataStream) const; + +protected: + int tsToOffset(int track, int sector); + +protected: + QString m_filename { "[unknown]" }; + QString m_fsname { "[unknown]" }; + int m_fstype { 0 }; + + DataFormat m_dataformat { DataFormat::Sectors }; + + int m_sectorsize { 256 }; + int m_blocksize { 512 }; + + int m_numsectors { -1 }; + int m_numtracks { -1 }; + int m_numblocks { -1 }; + + AttributeMap m_metadata; + + DataMap m_chunks; + + QByteArray m_original_file_contents; +}; + +QDataStream &operator<<(QDataStream &out, const ASDiskData &outData); + +QDataStream &operator>>(QDataStream &in, ASDiskData &inData); + + + +#endif // ASDISKDATA_H diff --git a/src/diskfiles/diskstore/asdiskimporter.cpp b/src/diskfiles/diskstore/asdiskimporter.cpp new file mode 100644 index 0000000..43380d0 --- /dev/null +++ b/src/diskfiles/diskstore/asdiskimporter.cpp @@ -0,0 +1,112 @@ +/***************************************************************************** +* AppleSAWS - The Apple Software Analysis WorkShop * +* Copyright (C) 2015-2021 Mark D. Long * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program. If not, see . * +*****************************************************************************/ + +#include "asdiskimporter.h" +#include "DiskImg.h" + +#include +#include + +ASDiskImporter::ASDiskImporter(QObject *parent) : QObject(parent) +{ + if (!DiskImgLib::Global::GetAppInitCalled()) + { + DiskImgLib::Global::AppInit(); + } +} + +bool ASDiskImporter::importImage(QString filename, ASDiskData &into) +{ + bool retval = false; + + DiskImgLib::DiskImg image; + auto localname = QDir::toNativeSeparators(filename); + auto fileinfo = QFileInfo(filename); + auto result = image.OpenImage(filename.toLatin1(),QDir::separator().toLatin1(),true); + + if (result == DiskImgLib::kDIErrNone) + { + result = image.AnalyzeImage(); + if (result == DiskImgLib::kDIErrNone) + { + auto filesystemformat = image.GetFSFormat(); + QString fsformatstr = DiskImgLib::DiskImg::ToString(filesystemformat); + + auto fs = image.OpenAppropriateDiskFS(false); + result = fs->Initialize(&image,DiskImgLib::DiskFS::kInitFull); + if (result == DiskImgLib::kDIErrNone) + { + into.setFilename(fileinfo.fileName()); + + into.setFSInfo(fsformatstr, (int) filesystemformat); + + int numblocks = image.GetNumBlocks(); + into.setNumBlocks(numblocks); + int numtracks = image.GetNumTracks(); + into.setNumTracks(numtracks); + int numsectors = image.GetNumSectPerTrack();\ + into.setNumSectorsPerTrack(numsectors); + + if (image.ShowAsBlocks()) + { + into.setDataFormat(ASDiskData::DataFormat::Blocks); + + char *buffer = new char[512]; // Todo: Remove magic number + for (int idx = 0; idx < numblocks; idx++) + { + image.ReadBlock(idx,buffer); + QByteArray array(buffer, 512); + into.addBlock(idx,array); + } + delete[] buffer; + } + else + { + into.setDataFormat(ASDiskData::DataFormat::Sectors); + + char *buffer = new char[256]; // Todo: Remove magic number + for (int track = 0; track < numtracks; track++) + { + for (int sector = 0; sector < numsectors; sector++) + { + image.ReadTrackSector(track,sector,buffer); + QByteArray array(buffer, 256); + into.addSector(track,sector,array); + } + } + delete[] buffer; + + } + + QFile orig(filename); + if (orig.open(QIODevice::ReadOnly)) + { + auto data = orig.readAll(); + orig.close(); + into.setOriginalFileContents(data); + retval = true; + } + + image.RemoveDiskFS(fs); + } + } + image.CloseImage(); + } + + return retval; +} diff --git a/src/diskfiles/diskstore/asdiskimporter.h b/src/diskfiles/diskstore/asdiskimporter.h new file mode 100644 index 0000000..d0cc7a5 --- /dev/null +++ b/src/diskfiles/diskstore/asdiskimporter.h @@ -0,0 +1,36 @@ +/***************************************************************************** +* AppleSAWS - The Apple Software Analysis WorkShop * +* Copyright (C) 2015-2021 Mark D. Long * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program. If not, see . * +*****************************************************************************/ + +#ifndef ASDISKIMPORTER_H +#define ASDISKIMPORTER_H + +#include + +#include "asdiskdata.h" + +class ASDiskImporter : public QObject +{ + Q_OBJECT +public: + explicit ASDiskImporter(QObject *parent = nullptr); + + bool importImage(QString filename, ASDiskData &into); + +}; + +#endif // ASDISKIMPORTER_H diff --git a/src/diskfiles/diskstore/asdiskstore.cpp b/src/diskfiles/diskstore/asdiskstore.cpp new file mode 100644 index 0000000..3139b78 --- /dev/null +++ b/src/diskfiles/diskstore/asdiskstore.cpp @@ -0,0 +1,24 @@ +/***************************************************************************** +* AppleSAWS - The Apple Software Analysis WorkShop * +* Copyright (C) 2015-2021 Mark D. Long * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program. If not, see . * +*****************************************************************************/ + +#include "asdiskstore.h" + +ASDiskStore::ASDiskStore(QObject *parent) : QObject(parent) +{ + +} diff --git a/src/diskfiles/diskstore/asdiskstore.h b/src/diskfiles/diskstore/asdiskstore.h new file mode 100644 index 0000000..c09c0c8 --- /dev/null +++ b/src/diskfiles/diskstore/asdiskstore.h @@ -0,0 +1,34 @@ +/***************************************************************************** +* AppleSAWS - The Apple Software Analysis WorkShop * +* Copyright (C) 2015-2021 Mark D. Long * +* * +* This program is free software: you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation, either version 3 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program. If not, see . * +*****************************************************************************/ + +#ifndef ASDISKSTORE_H +#define ASDISKSTORE_H + +#include + +class ASDiskStore : public QObject +{ + Q_OBJECT +public: + explicit ASDiskStore(QObject *parent = nullptr); + +signals: + +}; + +#endif // ASDISKSTORE_H diff --git a/src/src.pro b/src/src.pro index b97dbfe..bd517fa 100644 --- a/src/src.pro +++ b/src/src.pro @@ -114,7 +114,10 @@ SOURCES += \ ./util/opcodes.cpp \ diskfiles/cpressdiskfs.cpp \ diskfiles/cpressdiskimage.cpp \ - diskfiles/cpressfile.cpp + diskfiles/cpressfile.cpp \ + diskfiles/diskstore/asdiskdata.cpp \ + diskfiles/diskstore/asdiskimporter.cpp \ + diskfiles/diskstore/asdiskstore.cpp HEADERS += \ @@ -199,7 +202,10 @@ HEADERS += \ ./ui/widgets/asciiinfodialog.h \ diskfiles/cpressdiskfs.h \ diskfiles/cpressdiskimage.h \ - diskfiles/cpressfile.h + diskfiles/cpressfile.h \ + diskfiles/diskstore/asdiskdata.h \ + diskfiles/diskstore/asdiskimporter.h \ + diskfiles/diskstore/asdiskstore.h FORMS += \ ./sequence/sequenceviewer.ui \