2015-05-17 13:53:03 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2015-2015 Gregory hainaut
|
|
|
|
*
|
|
|
|
* 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 2, 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 GNU Make; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA USA.
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "stdafx.h"
|
|
|
|
#include "GSLzma.h"
|
|
|
|
|
2016-10-21 19:16:50 +00:00
|
|
|
GSDumpFile::GSDumpFile(char* filename, const char* repack_filename) {
|
2015-05-17 13:53:03 +00:00
|
|
|
m_fp = fopen(filename, "rb");
|
2016-10-21 19:16:50 +00:00
|
|
|
if (m_fp == nullptr) {
|
2015-05-17 13:53:03 +00:00
|
|
|
fprintf(stderr, "failed to open %s\n", filename);
|
|
|
|
throw "BAD"; // Just exit the program
|
|
|
|
}
|
2016-10-21 19:16:50 +00:00
|
|
|
|
|
|
|
m_repack_fp = nullptr;
|
|
|
|
if (repack_filename) {
|
|
|
|
m_repack_fp = fopen(repack_filename, "wb");
|
|
|
|
if (m_repack_fp == nullptr)
|
|
|
|
fprintf(stderr, "failed to open %s for repack\n", repack_filename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDumpFile::Repack(void* ptr, size_t size) {
|
|
|
|
if (m_repack_fp == nullptr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
size_t ret = fwrite(ptr, 1, size, m_repack_fp);
|
|
|
|
if (ret != size)
|
|
|
|
fprintf(stderr, "Failed to repack\n");
|
|
|
|
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSDumpFile::~GSDumpFile() {
|
|
|
|
if (m_fp)
|
|
|
|
fclose(m_fp);
|
2016-10-21 19:16:50 +00:00
|
|
|
if (m_repack_fp)
|
|
|
|
fclose(m_repack_fp);
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************/
|
2016-10-21 19:16:50 +00:00
|
|
|
GSDumpLzma::GSDumpLzma(char* filename, const char* repack_filename) : GSDumpFile(filename, repack_filename) {
|
2015-05-17 13:53:03 +00:00
|
|
|
|
|
|
|
memset(&m_strm, 0, sizeof(lzma_stream));
|
|
|
|
|
2015-05-19 09:55:41 +00:00
|
|
|
lzma_ret ret = lzma_stream_decoder(&m_strm, UINT32_MAX, 0);
|
2015-05-17 13:53:03 +00:00
|
|
|
|
|
|
|
if (ret != LZMA_OK) {
|
|
|
|
fprintf(stderr, "Error initializing the decoder! (error code %u)\n", ret);
|
|
|
|
throw "BAD"; // Just exit the program
|
|
|
|
}
|
|
|
|
|
|
|
|
m_buff_size = 1024*1024;
|
|
|
|
m_area = (uint8_t*)_aligned_malloc(m_buff_size, 32);
|
|
|
|
m_inbuf = (uint8_t*)_aligned_malloc(BUFSIZ, 32);
|
|
|
|
m_avail = 0;
|
|
|
|
m_start = 0;
|
|
|
|
|
|
|
|
m_strm.avail_in = 0;
|
|
|
|
m_strm.next_in = m_inbuf;
|
|
|
|
|
|
|
|
m_strm.avail_out = m_buff_size;
|
|
|
|
m_strm.next_out = m_area;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSDumpLzma::Decompress() {
|
|
|
|
lzma_action action = LZMA_RUN;
|
|
|
|
|
|
|
|
m_strm.next_out = m_area;
|
|
|
|
m_strm.avail_out = m_buff_size;
|
|
|
|
|
|
|
|
// Nothing left in the input buffer. Read data from the file
|
|
|
|
if (m_strm.avail_in == 0 && !feof(m_fp)) {
|
|
|
|
m_strm.next_in = m_inbuf;
|
|
|
|
m_strm.avail_in = fread(m_inbuf, 1, BUFSIZ, m_fp);
|
|
|
|
|
|
|
|
if (ferror(m_fp)) {
|
|
|
|
fprintf(stderr, "Read error: %s\n", strerror(errno));
|
|
|
|
throw "BAD"; // Just exit the program
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lzma_ret ret = lzma_code(&m_strm, action);
|
|
|
|
|
|
|
|
if (ret != LZMA_OK) {
|
|
|
|
if (ret == LZMA_STREAM_END)
|
|
|
|
fprintf(stderr, "LZMA decoder finished without error\n\n");
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Decoder error: (error code %u)\n", ret);
|
|
|
|
throw "BAD"; // Just exit the program
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_start = 0;
|
|
|
|
m_avail = m_buff_size - m_strm.avail_out;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GSDumpLzma::IsEof() {
|
2017-05-27 11:21:09 +00:00
|
|
|
return feof(m_fp) && m_avail == 0 && m_strm.avail_in == 0;
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 11:21:09 +00:00
|
|
|
bool GSDumpLzma::Read(void* ptr, size_t size) {
|
2015-05-17 13:53:03 +00:00
|
|
|
size_t off = 0;
|
|
|
|
uint8_t* dst = (uint8_t*)ptr;
|
2016-10-21 19:16:50 +00:00
|
|
|
size_t full_size = size;
|
2017-05-27 11:21:09 +00:00
|
|
|
while (size && !IsEof()) {
|
2015-05-17 13:53:03 +00:00
|
|
|
if (m_avail == 0) {
|
|
|
|
Decompress();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t l = min(size, m_avail);
|
|
|
|
memcpy(dst + off, m_area+m_start, l);
|
|
|
|
m_avail -= l;
|
|
|
|
size -= l;
|
|
|
|
m_start += l;
|
|
|
|
off += l;
|
|
|
|
}
|
2017-05-27 11:21:09 +00:00
|
|
|
|
|
|
|
if (size == 0) {
|
|
|
|
Repack(ptr, full_size);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSDumpLzma::~GSDumpLzma() {
|
|
|
|
lzma_end(&m_strm);
|
|
|
|
|
|
|
|
if (m_inbuf)
|
|
|
|
_aligned_free(m_inbuf);
|
|
|
|
if (m_area)
|
|
|
|
_aligned_free(m_area);
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************/
|
|
|
|
|
2016-10-21 19:16:50 +00:00
|
|
|
GSDumpRaw::GSDumpRaw(char* filename, const char* repack_filename) : GSDumpFile(filename, repack_filename) {
|
2015-09-11 10:19:49 +00:00
|
|
|
m_buff_size = 0;
|
|
|
|
m_area = NULL;
|
|
|
|
m_inbuf = NULL;
|
|
|
|
m_avail = 0;
|
|
|
|
m_start = 0;
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GSDumpRaw::IsEof() {
|
2017-06-09 13:42:39 +00:00
|
|
|
return !!feof(m_fp);
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 11:21:09 +00:00
|
|
|
bool GSDumpRaw::Read(void* ptr, size_t size) {
|
|
|
|
size_t ret = fread(ptr, 1, size, m_fp);
|
|
|
|
if (ret != size && ferror(m_fp)) {
|
|
|
|
fprintf(stderr, "GSDumpRaw:: Read error (%zu/%zu)\n", ret, size);
|
|
|
|
throw "BAD"; // Just exit the program
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret == size) {
|
|
|
|
Repack(ptr, size);
|
|
|
|
return true;
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|
2017-05-27 11:21:09 +00:00
|
|
|
|
|
|
|
return false;
|
2015-05-17 13:53:03 +00:00
|
|
|
}
|