#include <dpl/zip_input.h>
#include <dpl/scoped_close.h>
#include <dpl/binary_queue.h>
-#include <dpl/scoped_free.h>
+#include <dpl/free_deleter.h>
#include <memory>
-#include <dpl/scoped_array.h>
#include <dpl/foreach.h>
-#include <dpl/log/log.h>
+#include <dpl/log/wrt_log.h>
#include <new>
#include <minizip/unzip.h>
}
if (unzClose(m_file) != UNZ_OK) {
- LogPedantic("Failed to close zip input file");
+ WrtLogD("Failed to close zip input file");
}
}
};
public:
- Device(const std::string &fileName)
+
+ Device (const Device &) = delete;
+ Device & operator= (const Device &) = delete;
+
+ Device(const std::string &fileName) :
+ m_handle(-1),
+ m_size(0),
+ m_address(NULL)
{
- LogPedantic("Creating file mapping");
+ WrtLogD("Creating file mapping");
// Open device and map it to user space
int file = TEMP_FAILURE_RETRY(open(fileName.c_str(), O_RDONLY));
m_size = size;
m_address = static_cast<unsigned char *>(address);
- LogPedantic("Created file mapping: " << fileName <<
- " of size: " << m_size <<
- " at address: " << std::hex <<
- static_cast<void *>(m_address));
+ WrtLogD("Created file mapping: %s of size: %lli at address: %p",
+ fileName.c_str(), m_size, m_address);
}
~Device()
// Close mapping
if (munmap(m_address, static_cast<size_t>(m_size)) == -1) {
int error = errno;
- LogPedantic("Failed to munmap file. errno = " << error);
+ WrtLogD("Failed to munmap file. errno = %i", error);
}
-
+ m_address= NULL;
// Close file descriptor
if (close(m_handle) == -1) {
int error = errno;
- LogPedantic("Failed to close file. errno = " << error);
+ WrtLogD("Failed to close file. errno = %i", error);
}
+ m_handle = -1;
}
// zlib_filefunc64_def interface: files
void* buf,
uLong size)
{
+ if(!buf){
+ WrtLogE("target buffer is null");
+ return -1;
+ }
+
Device *device = static_cast<Device *>(opaque);
File *deviceFile = static_cast<File *>(pstream);
+ if(device->m_address == NULL) {
+ WrtLogE("Device: already closed");
+ return -1;
+ }
+
// Check if offset is out of bounds
if (deviceFile->offset >= device->m_size) {
- LogPedantic("Device: read offset out of bounds");
+ WrtLogD("Device: read offset out of bounds");
return -1;
}
bytesToRead = static_cast<off64_t>(size);
}
+ if(device->m_size < deviceFile->offset + bytesToRead){
+ WrtLogE("Device : end offset out of bounds");
+ return -1;
+ }
// Do copy
memcpy(buf,
device->m_address + deviceFile->offset,
uLong /*size*/)
{
// Not supported by device
- LogPedantic("Unsupported function called!");
+ WrtLogD("Unsupported function called!");
return -1;
}
m_totalUncompressedSize(0),
m_fileInfos()
{
- LogPedantic("Zip input file: " << fileName);
+ WrtLogD("Zip input file: %s", fileName.c_str());
// Create master device
- LogPedantic("Creating master device");
+ WrtLogD("Creating master device");
std::unique_ptr<Device> device(new Device(fileName));
// Open master file
interface.zerror_file = &Device::testerror_file;
interface.opaque = device.get();
- LogPedantic("Opening zip file");
+ WrtLogD("Opening zip file");
unzFile file = unzOpen2_64(NULL, &interface);
if (file == NULL) {
- LogPedantic("Failed to open zip file");
+ WrtLogD("Failed to open zip file");
// Some errror occured
ThrowMsg(Exception::OpenFailed,
m_masterFile = scopedUnzClose.Release();
m_device = device.release();
- LogPedantic("Zip file opened");
+ WrtLogD("Zip file opened");
}
ZipInput::~ZipInput()
{
// Close zip
if (unzClose(static_cast<unzFile>(m_masterFile)) != UNZ_OK) {
- LogPedantic("Failed to close zip input file");
+ WrtLogD("Failed to close zip input file");
}
// Close device
if (unzGetGlobalInfo(static_cast<unzFile>(masterFile),
&globalInfo) != UNZ_OK)
{
- LogPedantic("Failed to read zip global info");
+ WrtLogD("Failed to read zip global info");
ThrowMsg(Exception::ReadGlobalInfoFailed,
"Failed to read global info");
m_numberOfFiles = static_cast<size_t>(globalInfo.number_entry);
m_globalCommentSize = static_cast<size_t>(globalInfo.size_comment);
- LogPedantic("Number of files: " << m_numberOfFiles);
- LogPedantic("Global comment size: " << m_globalCommentSize);
+ WrtLogD("Number of files: %u", m_numberOfFiles);
+ WrtLogD("Global comment size: %u", m_globalCommentSize);
}
void ZipInput::ReadGlobalComment(void *masterFile)
{
- ScopedArray<char> comment(new char[m_globalCommentSize + 1]);
+ std::unique_ptr<char[]> comment(new char[m_globalCommentSize + 1]);
if (unzGetGlobalComment(static_cast<unzFile>(masterFile),
- comment.Get(),
+ comment.get(),
m_globalCommentSize + 1) != UNZ_OK)
{
- LogPedantic("Failed to read zip global comment");
+ WrtLogD("Failed to read zip global comment");
ThrowMsg(Exception::ReadGlobalCommentFailed,
"Failed to read global comment");
}
- m_globalComment = comment.Get();
- LogPedantic("Global comment: " << m_globalComment);
+ m_globalComment = comment.get();
+ WrtLogD("Global comment: %s", m_globalComment.c_str());
}
void ZipInput::ReadInfos(void *masterFile)
m_fileInfos.reserve(m_numberOfFiles);
if (unzGoToFirstFile(static_cast<unzFile>(masterFile)) != UNZ_OK) {
- LogPedantic("Failed to go to first file");
+ WrtLogD("Failed to go to first file");
ThrowMsg(Exception::SeekFileFailed, "Failed to seek first file");
}
if (unzGetFilePos(static_cast<unzFile>(masterFile),
&filePos) != UNZ_OK)
{
- LogPedantic("Failed to get file pos");
+ WrtLogD("Failed to get file pos");
ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info");
}
NULL,
0) != UNZ_OK)
{
- LogPedantic("Failed to get file pos");
+ WrtLogD("Failed to get file pos");
ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info");
}
- ScopedArray<char> fileName(new char[fileInfo.size_filename + 1]);
- ScopedArray<char> fileComment(new char[fileInfo.size_file_comment + 1]);
+ std::unique_ptr<char[]> fileName(new char[fileInfo.size_filename + 1]);
+ std::unique_ptr<char[]> fileComment(new char[fileInfo.size_file_comment + 1]);
if (unzGetCurrentFileInfo64(static_cast<unzFile>(masterFile),
&fileInfo,
- fileName.Get(),
+ fileName.get(),
fileInfo.size_filename + 1,
NULL,
0,
- fileComment.Get(),
+ fileComment.get(),
fileInfo.size_file_comment + 1) != UNZ_OK)
{
- LogPedantic("Failed to get file pos");
+ WrtLogD("Failed to get file pos");
ThrowMsg(Exception::FileInfoFailed, "Failed to get zip file info");
}
static_cast<size_t>(filePos.pos_in_zip_directory),
static_cast<size_t>(filePos.num_of_file)
),
- std::string(fileName.Get()),
- std::string(fileComment.Get()),
+ std::string(fileName.get()),
+ std::string(fileComment.get()),
static_cast<off64_t>(fileInfo.compressed_size),
static_cast<off64_t>(fileInfo.uncompressed_size)
)
if (unzGoToNextFile(
static_cast<unzFile>(masterFile)) != UNZ_OK)
{
- LogPedantic("Failed to go to next file");
+ WrtLogD("Failed to go to next file");
ThrowMsg(Exception::FileInfoFailed,
"Failed to go to next file");
interface.zerror_file = &Device::testerror_file;
interface.opaque = device;
- LogPedantic("Opening zip file");
+ WrtLogD("Opening zip file");
unzFile file = unzOpen2_64(NULL, &interface);
if (file == NULL) {
- LogPedantic("Failed to open zip file");
+ WrtLogD("Failed to open zip file");
// Some errror occured
ThrowMsg(ZipInput::Exception::OpenFileFailed,
};
if (unzGoToFilePos64(file, &filePos) != UNZ_OK) {
- LogPedantic("Failed to seek to zip file");
+ WrtLogD("Failed to seek to zip file");
// Some errror occured
ThrowMsg(ZipInput::Exception::OpenFileFailed,
// Open current file for reading
if (unzOpenCurrentFile(file) != UNZ_OK) {
- LogPedantic("Failed to open current zip file");
+ WrtLogD("Failed to open current zip file");
// Some errror occured
ThrowMsg(ZipInput::Exception::OpenFileFailed,
// Release scoped unz close
m_file = scopedUnzClose.Release();
- LogPedantic("Zip file opened");
+ WrtLogD("Zip file opened");
}
ZipInput::File::~File()
{
// Close current file for reading
if (unzCloseCurrentFile(static_cast<unzFile>(m_file)) != UNZ_OK) {
- LogPedantic("Failed to close current zip input file");
+ WrtLogD("Failed to close current zip input file");
}
// Close zip file
if (unzClose(static_cast<unzFile>(m_file)) != UNZ_OK) {
- LogPedantic("Failed to close zip input file");
+ WrtLogD("Failed to close zip input file");
}
}
size;
// Extract zip file data (one-copy)
- ScopedFree<void> rawBuffer(malloc(sizeToRead));
+ std::unique_ptr<void,free_deleter> rawBuffer(malloc(sizeToRead));
if (!rawBuffer) {
throw std::bad_alloc();
// Do unpack
int bytes = unzReadCurrentFile(static_cast<unzFile>(m_file),
- rawBuffer.Get(),
+ rawBuffer.get(),
sizeToRead);
// Internal unzipper error
if (bytes < 0) {
- LogPedantic("Extract failed. Error: " << bytes);
+ WrtLogD("Extract failed. Error: %i", bytes);
ThrowMsg(ZipInput::Exception::ReadFileFailed,
"Failed to extract file with error: " << bytes);
// Data was read (may be zero bytes)
DPL::BinaryQueueAutoPtr buffer(new DPL::BinaryQueue());
- buffer->AppendUnmanaged(rawBuffer.Get(),
+ buffer->AppendUnmanaged(rawBuffer.get(),
static_cast<size_t>(bytes),
&DPL::BinaryQueue::BufferDeleterFree,
NULL);
- rawBuffer.Release();
+ rawBuffer.release();
return buffer;
}