DumpDVD/DumpDVD/Dvd/DvdRipper.cpp

230 lines
6.2 KiB
C++

#include "DvdRipper.hpp"
#include <dvdcss.h>
#include <iostream>
#include <fstream>
#include <thread>
#include <sstream>
#include <filesystem>
#include "../Scsi/IoCtl.hpp"
#include "../Scsi/OpticalDrive.hpp"
#include "../Dvd/TitleKey.hpp"
namespace Li::Dvd {
uint32_t DvdRipper::FileSoFar() {
return this->fileReadSoFar;
}
uint32_t DvdRipper::FileLen() {
return this->fileRemain;
}
float DvdRipper::PercentDone() {
return (((float)this->sectorsReadSoFar / (float)this->drive->Sectors()));
}
uint32_t DvdRipper::SectorsReadSoFar() {
return this->sectorsReadSoFar;
}
std::string DvdRipper::OutputPath() {
return this->outputPath;
}
std::byte* DvdRipper::GetDiscKey() {
return this->discKey;
}
std::byte* DvdRipper::GetTitleKey() {
return this->titleKey;
}
bool DvdRipper::Done() {
return this->done;
}
bool DvdRipper::Error() {
return this->error;
}
std::string DvdRipper::ErrorMessage() {
return this->errorMsg;
}
int DvdRipper::read1SectorATimeSkippingBadSectors(int toRead) {
int nmRd = 0;
for (int i = 0; i < toRead; i++) {
for (int retry = 0; retry < 10; retry++) {
memset(this->tmpBuffer, 0x00, DVDCSS_BLOCK_SIZE);
int dataRd = dvdcss_read(driveIoCtl->GetDvdCssHandle(), this->tmpBuffer, 1, DVDCSS_READ_DECRYPT);
if (dataRd < 0) {
dvdcss_seek(driveIoCtl->GetDvdCssHandle(), this->sectorsReadSoFar, DVDCSS_SEEK_KEY);
if (retry+1 >= 10)
dvdcss_seek(driveIoCtl->GetDvdCssHandle(), this->sectorsReadSoFar + 1, DVDCSS_SEEK_KEY);
continue; // retry
}
else {
this->iso->write((const char*)this->tmpBuffer, DVDCSS_BLOCK_SIZE * dataRd);
break;
}
}
nmRd += 1;
this->sectorsReadSoFar += 1;
this->fileReadSoFar += 1;
if (this->sectorsReadSoFar > this->drive->Sectors()) // check if we've read the last sector ...
break;
}
return nmRd;
}
void DvdRipper::readSectors() {
do {
int toRead = this->sectorsAtOnce;
if ((this->fileReadSoFar + toRead) > this->fileRemain) {
toRead = (this->fileRemain - this->fileReadSoFar);
}
int numRead = dvdcss_read(driveIoCtl->GetDvdCssHandle(), this->tmpBuffer, toRead, DVDCSS_READ_DECRYPT);
if (dvdcss_was_error(driveIoCtl->GetDvdCssHandle())) {
this->errorMsg = std::string(dvdcss_error(driveIoCtl->GetDvdCssHandle()));
}
if (numRead < 0) {
dvdcss_seek(driveIoCtl->GetDvdCssHandle(), this->sectorsReadSoFar, DVDCSS_SEEK_KEY);
read1SectorATimeSkippingBadSectors(toRead);
}
else {
this->iso->write((const char*)this->tmpBuffer, DVDCSS_BLOCK_SIZE * numRead);
this->sectorsReadSoFar += numRead;
this->fileReadSoFar += numRead;
}
} while ( (this->fileReadSoFar < this->fileRemain) &&
(this->sectorsReadSoFar < this->drive->Sectors()) &&
this->keepRunning);
this->inFile = Li::Dvd::TitleKey::IsSectorInFile(this->sectorsReadSoFar);
this->fileReadSoFar = 0;
this->fileRemain = 0;
}
void DvdRipper::readNonFileSectors() {
dvdcss_title(this->driveIoCtl->GetDvdCssHandle(), this->sectorsReadSoFar);
this->fileRemain = Li::Dvd::TitleKey::GetDistanceToNextFile(this->sectorsReadSoFar);
if ((this->sectorsReadSoFar + this->fileRemain) > this->drive->Sectors()) {
this->fileRemain = (this->drive->Sectors() - this->sectorsReadSoFar);
}
this->fileReadSoFar = 0;
this->readSectors();
}
void DvdRipper::readFileSectors() {
dvdcss_title(this->driveIoCtl->GetDvdCssHandle(), this->sectorsReadSoFar);
this->titleKey = (std::byte*)dvdcss_get_cur_titlekey(this->driveIoCtl->GetDvdCssHandle());
sectorInfo* sectorInf = Li::Dvd::TitleKey::GetSectorInfo(this->sectorsReadSoFar);
this->fileRemain = (sectorInf->endSector - sectorInf->startSector);
if ((this->sectorsReadSoFar + this->fileRemain) > this->drive->Sectors()) {
this->fileRemain = (this->drive->Sectors() - this->sectorsReadSoFar);
}
this->fileReadSoFar = 0;
this->readSectors();
}
void DvdRipper::ripThread() {
this->sectorsReadSoFar = 0;
Li::Dvd::TitleKey::GetTitleKeys(driveIoCtl->GetDvdCssHandle());
dvdcss_seek(driveIoCtl->GetDvdCssHandle(), 0, DVDCSS_SEEK_KEY);
this->inFile = Li::Dvd::TitleKey::IsSectorInFile(this->sectorsReadSoFar);
do {
if (this->inFile)
this->readFileSectors(); // read & decrypt all files (including those associated w the filesystem)
else
this->readNonFileSectors(); // read the data not associated with any file on the filesystem.
} while (this->sectorsReadSoFar < this->drive->Sectors() && this->keepRunning);
this->done = true;
}
DvdRipper::DvdRipper(Li::Scsi::OpticalDrive* opticalDrive, std::string outputISO, uint32_t sectorsAtOnce) {
this->drive = opticalDrive;
this->outputPath = outputISO;
this->sectorsAtOnce = sectorsAtOnce;
this->sectorsReadSoFar = 0;
this->fileReadSoFar = 0;
this->fileRemain = 0;
this->inFile = false;
this->done = false;
this->error = false;
this->errorMsg = "";
this->keepRunning = false;
this->ripinThread = nullptr;
this->tmpBuffer = new std::byte[DVDCSS_BLOCK_SIZE * this->sectorsAtOnce];
}
DvdRipper::~DvdRipper() {
delete this->tmpBuffer;
if (this->ripinThread != nullptr) {
this->EndRip();
}
if (this->iso != nullptr) {
if (this->iso->is_open())
this->iso->close();
delete this->iso;
}
}
void DvdRipper::StartRip(uint32_t driveSpeed) {
this->driveIoCtl = new Li::Scsi::IoCtl(this->drive->DrivePath());
this->driveIoCtl->AllowReadingPastDisc();
this->driveIoCtl->SetDriveSpeed(driveSpeed, driveSpeed);
this->driveIoCtl->ExclusiveLockDrive();
this->discKey = (std::byte*)dvdcss_get_cur_disckey(this->driveIoCtl->GetDvdCssHandle());
dvdcss_title(this->driveIoCtl->GetDvdCssHandle(), 0);
this->titleKey = (std::byte*)dvdcss_get_cur_titlekey(this->driveIoCtl->GetDvdCssHandle());
this->iso = new std::ofstream(this->outputPath, std::ios::binary);
this->keepRunning = true;
this->ripinThread = new std::thread(&DvdRipper::ripThread, this);
}
void DvdRipper::EndRip() {
this->done = false;
this->error = false;
this->errorMsg = "";
this->sectorsReadSoFar = 0;
this->fileReadSoFar = 0;
this->keepRunning = false;
this->ripinThread->join();
delete this->driveIoCtl;
delete this->ripinThread;
this->ripinThread = nullptr;
iso->close();
delete iso;
this->iso = nullptr;
this->driveIoCtl->ExclusiveUnlockDrive();
}
}