OK, second pass at the revised ZIP file handling. It's still broken for

opening ROMs, but at least the ROM launcher now properly descends into
ZIP 'folders'.

Removed obsolete unzip.h|c code, which hasn't been updated since 1998
and fails to compile on some systems.  Replaced it with ZipHandler class,
which is a much nicer interface (and about half the code too).


git-svn-id: svn://svn.code.sf.net/p/stella/code/trunk@2603 8b62c5a3-ac7e-4cc8-8f21-d9a121418aba
This commit is contained in:
stephena 2013-02-12 02:24:17 +00:00
parent 96b3d31806
commit b6b6835435
11 changed files with 952 additions and 1581 deletions

View File

@ -21,6 +21,7 @@
//============================================================================
#include "bspf.hxx"
#include "OSystem.hxx"
#include "FSNodeFactory.hxx"
#include "FSNodeZIP.hxx"
@ -45,54 +46,70 @@ FilesystemNodeZIP::FilesystemNodeZIP(const string& p)
// Not a ZIP file
_path = _shortPath = _virtualFile = "";
_isValid = _isDirectory = _isFile = _isVirtual = false;
cerr << "Not a ZIP file\n";
return;
}
_zipFile = p.substr(0, pos+4);
if(pos+5 < p.length())
_virtualFile = p.substr(pos+5);
// A ZIP file is, behind the scenes, still a real file in the filesystem
// Hence, we need to create a real filesystem node for it
AbstractFSNode* tmp = FilesystemNodeFactory::create(_zipFile,
FilesystemNodeFactory::SYSTEM);
AbstractFSNode* tmp = FilesystemNodeFactory::create(
_zipFile, FilesystemNodeFactory::SYSTEM);
_realNode = Common::SharedPtr<AbstractFSNode>(tmp);
setFlags(_zipFile, _virtualFile, _realNode);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
FilesystemNodeZIP::FilesystemNodeZIP(const string& zipfile, const string& virtualfile,
Common::SharedPtr<AbstractFSNode> realnode)
{
setFlags(zipfile, virtualfile, realnode);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void FilesystemNodeZIP::setFlags(const string& zipfile,
const string& virtualfile,
Common::SharedPtr<AbstractFSNode> realnode)
{
_zipFile = zipfile;
_virtualFile = virtualfile;
_realNode = realnode;
_path = _realNode->getPath();
_shortPath = _realNode->getShortPath();
// Is a file component present?
if(pos+5 < p.length())
if(_virtualFile.size() != 0)
{
_isVirtual = true;
_virtualFile = p.substr(pos+5);
_path += (BSPF_PATH_SEPARATOR + _virtualFile);
_shortPath += (BSPF_PATH_SEPARATOR + _virtualFile);
}
else
{
_isVirtual = false;
_virtualFile = "";
}
cerr << "FilesystemNodeZIP: " << p << endl
<< "path: " << _path << endl
<< "spath: " << getShortPath() << endl
<< "zipFile: " << _zipFile << endl
<< "virtualFile: " << _virtualFile << endl
<< endl;
_isDirectory = !_isVirtual;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool FilesystemNodeZIP::getChildren(AbstractFSList& myList, ListMode mode,
bool hidden) const
{
cerr << "getChildren: " << _path << endl;
assert(_isDirectory);
// Files within ZIP archives don't contain children
if(_isVirtual)
return false;
return false;
ZipHandler& zip = OSystem::zip(_path);
while(zip.hasNext())
{
FilesystemNodeZIP entry(_path, zip.next(), _realNode);
myList.push_back(new FilesystemNodeZIP(entry));
}
return true;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

View File

@ -42,9 +42,8 @@ class FilesystemNodeZIP : public AbstractFSNode
/**
* Creates a FilesystemNodeZIP for a given path.
*
* @param path String with the path the new node should point to.
* @param verify true if the isValid and isDirectory/isFile flags should
* be verified during the construction.
* @param path String with the path the new node should point to.
* @param node Raw pointer to use for the internal FSNode
*/
FilesystemNodeZIP(const string& path);
@ -67,6 +66,13 @@ class FilesystemNodeZIP : public AbstractFSNode
bool getChildren(AbstractFSList& list, ListMode mode, bool hidden) const;
AbstractFSNode* getParent() const;
private:
FilesystemNodeZIP(const string& zipfile, const string& virtualfile,
Common::SharedPtr<AbstractFSNode> realnode);
void setFlags(const string& zipfile, const string& virtualfile,
Common::SharedPtr<AbstractFSNode> realnode);
protected:
Common::SharedPtr<AbstractFSNode> _realNode;
string _zipFile, _virtualFile;

605
src/common/ZipHandler.cxx Normal file
View File

@ -0,0 +1,605 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2013 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id$
//============================================================================
#include "ZipHandler.hxx"
#include <cctype>
#include <cstdlib>
#include <zlib.h>
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ZipHandler::ZipHandler()
: myZip(NULL)
{
for (int cachenum = 0; cachenum < ZIP_CACHE_SIZE; cachenum++)
myZipCache[cachenum] = NULL;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ZipHandler::~ZipHandler()
{
zip_file_cache_clear();
free_zip_file(myZip);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ZipHandler::open(const string& filename)
{
// Close already open file
if(myZip)
zip_file_close(myZip);
// And open a new one
zip_file_open(filename.c_str(), &myZip);
reset();
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ZipHandler::reset()
{
/* reset the position and go from there */
if(myZip)
myZip->cd_pos = 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ZipHandler::hasNext()
{
return myZip && (myZip->cd_pos < myZip->ecd.cd_size);
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
string ZipHandler::next()
{
if(myZip)
{
const zip_file_header* header = NULL;
do {
header = zip_file_next_file(myZip);
}
while(header && header->uncompressed_length == 0);
return header ? header->filename : EmptyString;
}
else
return EmptyString;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ZipHandler::decompress(uInt8* buffer, uInt32 length)
{
return myZip && (zip_file_decompress(myZip, buffer, length) == ZIPERR_NONE);
}
/*-------------------------------------------------
replaces functionality of various osd_xxx
file access functions
-------------------------------------------------*/
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ZipHandler::stream_open(const char* filename, fstream** stream,
uInt64& length)
{
fstream* in = new fstream(filename, fstream::in | fstream::binary);
if(!in || !in->is_open())
{
*stream = NULL;
length = 0;
return false;
}
else
{
in->exceptions( ios_base::failbit | ios_base::badbit | ios_base::eofbit );
*stream = in;
in->seekg(0, ios::end);
length = in->tellg();
in->seekg(0, ios::beg);
return true;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void ZipHandler::stream_close(fstream** stream)
{
if(*stream)
{
if((*stream)->is_open())
(*stream)->close();
delete *stream;
*stream = NULL;
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool ZipHandler::stream_read(fstream* stream, void* buffer, uInt64 offset,
uInt32 length, uInt32& actual)
{
try
{
stream->seekg(offset);
stream->read((char*)buffer, length);
actual = stream->gcount();
return true;
}
catch(...)
{
return false;
}
return false;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/*-------------------------------------------------
zip_file_open - opens a ZIP file for reading
-------------------------------------------------*/
ZipHandler::zip_error ZipHandler::zip_file_open(const char *filename, zip_file **zip)
{
zip_error ziperr = ZIPERR_NONE;
uInt32 read_length;
zip_file *newzip;
char *string;
int cachenum;
bool success;
/* ensure we start with a NULL result */
*zip = NULL;
/* see if we are in the cache, and reopen if so */
for (cachenum = 0; cachenum < ZIP_CACHE_SIZE; cachenum++)
{
zip_file *cached = myZipCache[cachenum];
/* if we have a valid entry and it matches our filename, use it and remove from the cache */
if (cached != NULL && cached->filename != NULL && strcmp(filename, cached->filename) == 0)
{
*zip = cached;
myZipCache[cachenum] = NULL;
return ZIPERR_NONE;
}
}
/* allocate memory for the zip_file structure */
newzip = (zip_file *)malloc(sizeof(*newzip));
if (newzip == NULL)
return ZIPERR_OUT_OF_MEMORY;
memset(newzip, 0, sizeof(*newzip));
/* open the file */
if(!stream_open(filename, &newzip->file, newzip->length))
{
ziperr = ZIPERR_FILE_ERROR;
goto error;
}
/* read ecd data */
ziperr = read_ecd(newzip);
if (ziperr != ZIPERR_NONE)
goto error;
/* verify that we can work with this zipfile (no disk spanning allowed) */
if (newzip->ecd.disk_number != newzip->ecd.cd_start_disk_number || newzip->ecd.cd_disk_entries != newzip->ecd.cd_total_entries)
{
ziperr = ZIPERR_UNSUPPORTED;
goto error;
}
/* allocate memory for the central directory */
newzip->cd = (uInt8 *)malloc(newzip->ecd.cd_size + 1);
if (newzip->cd == NULL)
{
ziperr = ZIPERR_OUT_OF_MEMORY;
goto error;
}
/* read the central directory */
success = stream_read(newzip->file, newzip->cd, newzip->ecd.cd_start_disk_offset, newzip->ecd.cd_size, read_length);
if (!success || read_length != newzip->ecd.cd_size)
{
ziperr = success ? ZIPERR_FILE_TRUNCATED : ZIPERR_FILE_ERROR;
goto error;
}
/* make a copy of the filename for caching purposes */
string = (char *)malloc(strlen(filename) + 1);
if (string == NULL)
{
ziperr = ZIPERR_OUT_OF_MEMORY;
goto error;
}
strcpy(string, filename);
newzip->filename = string;
*zip = newzip;
return ZIPERR_NONE;
error:
free_zip_file(newzip);
return ziperr;
}
/*-------------------------------------------------
zip_file_close - close a ZIP file and add it
to the cache
-------------------------------------------------*/
void ZipHandler::zip_file_close(zip_file *zip)
{
int cachenum;
/* close the open files */
if (zip->file)
stream_close(&zip->file);
/* find the first NULL entry in the cache */
for (cachenum = 0; cachenum < ZIP_CACHE_SIZE; cachenum++)
if (myZipCache[cachenum] == NULL)
break;
/* if no room left in the cache, free the bottommost entry */
if (cachenum == ZIP_CACHE_SIZE)
free_zip_file(myZipCache[--cachenum]);
/* move everyone else down and place us at the top */
if (cachenum != 0)
memmove(&myZipCache[1], &myZipCache[0], cachenum * sizeof(myZipCache[0]));
myZipCache[0] = zip;
}
/*-------------------------------------------------
zip_file_cache_clear - clear the ZIP file
cache and free all memory
-------------------------------------------------*/
void ZipHandler::zip_file_cache_clear(void)
{
/* clear call cache entries */
for (int cachenum = 0; cachenum < ZIP_CACHE_SIZE; cachenum++)
if (myZipCache[cachenum] != NULL)
{
cerr << "free cache: " << myZipCache[cachenum]->filename << endl;
free_zip_file(myZipCache[cachenum]);
myZipCache[cachenum] = NULL;
}
}
/***************************************************************************
CONTAINED FILE ACCESS
***************************************************************************/
/*-------------------------------------------------
zip_file_next_entry - return the next entry
in the ZIP
-------------------------------------------------*/
const ZipHandler::zip_file_header* ZipHandler::zip_file_next_file(zip_file *zip)
{
/* fix up any modified data */
if (zip->header.raw != NULL)
{
zip->header.raw[ZIPCFN + zip->header.filename_length] = zip->header.saved;
zip->header.raw = NULL;
}
/* if we're at or past the end, we're done */
if (zip->cd_pos >= zip->ecd.cd_size)
return NULL;
/* extract file header info */
zip->header.raw = zip->cd + zip->cd_pos;
zip->header.rawlength = ZIPCFN;
zip->header.signature = read_dword(zip->header.raw + ZIPCENSIG);
zip->header.version_created = read_word (zip->header.raw + ZIPCVER);
zip->header.version_needed = read_word (zip->header.raw + ZIPCVXT);
zip->header.bit_flag = read_word (zip->header.raw + ZIPCFLG);
zip->header.compression = read_word (zip->header.raw + ZIPCMTHD);
zip->header.file_time = read_word (zip->header.raw + ZIPCTIM);
zip->header.file_date = read_word (zip->header.raw + ZIPCDAT);
zip->header.crc = read_dword(zip->header.raw + ZIPCCRC);
zip->header.compressed_length = read_dword(zip->header.raw + ZIPCSIZ);
zip->header.uncompressed_length = read_dword(zip->header.raw + ZIPCUNC);
zip->header.filename_length = read_word (zip->header.raw + ZIPCFNL);
zip->header.extra_field_length = read_word (zip->header.raw + ZIPCXTL);
zip->header.file_comment_length = read_word (zip->header.raw + ZIPCCML);
zip->header.start_disk_number = read_word (zip->header.raw + ZIPDSK);
zip->header.internal_attributes = read_word (zip->header.raw + ZIPINT);
zip->header.external_attributes = read_dword(zip->header.raw + ZIPEXT);
zip->header.local_header_offset = read_dword(zip->header.raw + ZIPOFST);
zip->header.filename = (char *)zip->header.raw + ZIPCFN;
/* make sure we have enough data */
zip->header.rawlength += zip->header.filename_length;
zip->header.rawlength += zip->header.extra_field_length;
zip->header.rawlength += zip->header.file_comment_length;
if (zip->cd_pos + zip->header.rawlength > zip->ecd.cd_size)
return NULL;
/* NULL terminate the filename */
zip->header.saved = zip->header.raw[ZIPCFN + zip->header.filename_length];
zip->header.raw[ZIPCFN + zip->header.filename_length] = 0;
/* advance the position */
zip->cd_pos += zip->header.rawlength;
return &zip->header;
}
/*-------------------------------------------------
zip_file_decompress - decompress a file
from a ZIP into the target buffer
-------------------------------------------------*/
ZipHandler::zip_error ZipHandler::zip_file_decompress(zip_file *zip, void *buffer, uInt32 length)
{
zip_error ziperr;
uInt64 offset;
/* if we don't have enough buffer, error */
if (length < zip->header.uncompressed_length)
return ZIPERR_BUFFER_TOO_SMALL;
/* make sure the info in the header aligns with what we know */
if (zip->header.start_disk_number != zip->ecd.disk_number)
return ZIPERR_UNSUPPORTED;
/* get the compressed data offset */
ziperr = get_compressed_data_offset(zip, &offset);
if (ziperr != ZIPERR_NONE)
return ziperr;
/* handle compression types */
switch (zip->header.compression)
{
case 0:
ziperr = decompress_data_type_0(zip, offset, buffer, length);
break;
case 8:
ziperr = decompress_data_type_8(zip, offset, buffer, length);
break;
default:
ziperr = ZIPERR_UNSUPPORTED;
break;
}
return ziperr;
}
/***************************************************************************
CACHE MANAGEMENT
***************************************************************************/
/*-------------------------------------------------
free_zip_file - free all the data for a
zip_file
-------------------------------------------------*/
void ZipHandler::free_zip_file(zip_file *zip)
{
if (zip != NULL)
{
if (zip->file) cerr << "free: " << zip->filename << endl;
if (zip->file)
stream_close(&zip->file);
if (zip->filename != NULL)
free((void *)zip->filename);
if (zip->ecd.raw != NULL)
free(zip->ecd.raw);
if (zip->cd != NULL)
free(zip->cd);
free(zip);
}
}
/***************************************************************************
ZIP FILE PARSING
***************************************************************************/
/*-------------------------------------------------
read_ecd - read the ECD data
-------------------------------------------------*/
ZipHandler::zip_error ZipHandler::read_ecd(zip_file *zip)
{
uInt32 buflen = 1024;
uInt8 *buffer;
/* we may need multiple tries */
while (buflen < 65536)
{
uInt32 read_length;
Int32 offset;
/* max out the buffer length at the size of the file */
if (buflen > zip->length)
buflen = zip->length;
/* allocate buffer */
buffer = (uInt8 *)malloc(buflen + 1);
if (buffer == NULL)
return ZIPERR_OUT_OF_MEMORY;
/* read in one buffers' worth of data */
bool success = stream_read(zip->file, buffer, zip->length - buflen, buflen, read_length);
if (!success || read_length != buflen)
{
free(buffer);
return ZIPERR_FILE_ERROR;
}
/* find the ECD signature */
for (offset = buflen - 22; offset >= 0; offset--)
if (buffer[offset + 0] == 'P' && buffer[offset + 1] == 'K' && buffer[offset + 2] == 0x05 && buffer[offset + 3] == 0x06)
break;
/* if we found it, fill out the data */
if (offset >= 0)
{
/* reuse the buffer as our ECD buffer */
zip->ecd.raw = buffer;
zip->ecd.rawlength = buflen - offset;
/* append a NULL terminator to the comment */
memmove(&buffer[0], &buffer[offset], zip->ecd.rawlength);
zip->ecd.raw[zip->ecd.rawlength] = 0;
/* extract ecd info */
zip->ecd.signature = read_dword(zip->ecd.raw + ZIPESIG);
zip->ecd.disk_number = read_word (zip->ecd.raw + ZIPEDSK);
zip->ecd.cd_start_disk_number = read_word (zip->ecd.raw + ZIPECEN);
zip->ecd.cd_disk_entries = read_word (zip->ecd.raw + ZIPENUM);
zip->ecd.cd_total_entries = read_word (zip->ecd.raw + ZIPECENN);
zip->ecd.cd_size = read_dword(zip->ecd.raw + ZIPECSZ);
zip->ecd.cd_start_disk_offset = read_dword(zip->ecd.raw + ZIPEOFST);
zip->ecd.comment_length = read_word (zip->ecd.raw + ZIPECOML);
zip->ecd.comment = (const char *)(zip->ecd.raw + ZIPECOM);
return ZIPERR_NONE;
}
/* didn't find it; free this buffer and expand our search */
free(buffer);
if (buflen < zip->length)
buflen *= 2;
else
return ZIPERR_BAD_SIGNATURE;
}
return ZIPERR_OUT_OF_MEMORY;
}
/*-------------------------------------------------
get_compressed_data_offset - return the
offset of the compressed data
-------------------------------------------------*/
ZipHandler::zip_error ZipHandler::get_compressed_data_offset(zip_file *zip, uInt64 *offset)
{
uInt32 read_length;
/* make sure the file handle is open */
if (zip->file == NULL && !stream_open(zip->filename, &zip->file, zip->length))
return ZIPERR_FILE_ERROR;
/* now go read the fixed-sized part of the local file header */
bool success = stream_read(zip->file, zip->buffer, zip->header.local_header_offset, ZIPNAME, read_length);
if (!success || read_length != ZIPNAME)
return success ? ZIPERR_FILE_TRUNCATED : ZIPERR_FILE_ERROR;
/* compute the final offset */
*offset = zip->header.local_header_offset + ZIPNAME;
*offset += read_word(zip->buffer + ZIPFNLN);
*offset += read_word(zip->buffer + ZIPXTRALN);
return ZIPERR_NONE;
}
/***************************************************************************
DECOMPRESSION INTERFACES
***************************************************************************/
/*-------------------------------------------------
decompress_data_type_0 - "decompress"
type 0 data (which is uncompressed)
-------------------------------------------------*/
ZipHandler::zip_error ZipHandler::decompress_data_type_0(zip_file *zip, uInt64 offset, void *buffer, uInt32 length)
{
uInt32 read_length;
/* the data is uncompressed; just read it */
bool success = stream_read(zip->file, buffer, offset, zip->header.compressed_length, read_length);
if (!success)
return ZIPERR_FILE_ERROR;
else if (read_length != zip->header.compressed_length)
return ZIPERR_FILE_TRUNCATED;
else
return ZIPERR_NONE;
}
/*-------------------------------------------------
decompress_data_type_8 - decompress
type 8 data (which is deflated)
-------------------------------------------------*/
ZipHandler::zip_error ZipHandler::decompress_data_type_8(zip_file *zip, uInt64 offset, void *buffer, uInt32 length)
{
uInt32 input_remaining = zip->header.compressed_length;
uInt32 read_length;
z_stream stream;
int zerr;
/* make sure we don't need a newer mechanism */
if (zip->header.version_needed > 0x14)
return ZIPERR_UNSUPPORTED;
/* reset the stream */
memset(&stream, 0, sizeof(stream));
stream.next_out = (Bytef *)buffer;
stream.avail_out = length;
/* initialize the decompressor */
zerr = inflateInit2(&stream, -MAX_WBITS);
if (zerr != Z_OK)
return ZIPERR_DECOMPRESS_ERROR;
/* loop until we're done */
while (1)
{
/* read in the next chunk of data */
bool success = stream_read(zip->file, zip->buffer, offset, BSPF_min(input_remaining, (uInt32)sizeof(zip->buffer)), read_length);
if (!success)
{
inflateEnd(&stream);
return ZIPERR_FILE_ERROR;
}
offset += read_length;
/* if we read nothing, but still have data left, the file is truncated */
if (read_length == 0 && input_remaining > 0)
{
inflateEnd(&stream);
return ZIPERR_FILE_TRUNCATED;
}
/* fill out the input data */
stream.next_in = zip->buffer;
stream.avail_in = read_length;
input_remaining -= read_length;
/* add a dummy byte at end of compressed data */
if (input_remaining == 0)
stream.avail_in++;
/* now inflate */
zerr = inflate(&stream, Z_NO_FLUSH);
if (zerr == Z_STREAM_END)
break;
if (zerr != Z_OK)
{
inflateEnd(&stream);
return ZIPERR_DECOMPRESS_ERROR;
}
}
/* finish decompression */
zerr = inflateEnd(&stream);
if (zerr != Z_OK)
return ZIPERR_DECOMPRESS_ERROR;
/* if anything looks funny, report an error */
if (stream.avail_out > 0 || input_remaining > 0)
return ZIPERR_DECOMPRESS_ERROR;
return ZIPERR_NONE;
}

264
src/common/ZipHandler.hxx Normal file
View File

@ -0,0 +1,264 @@
//============================================================================
//
// SSSS tt lll lll
// SS SS tt ll ll
// SS tttttt eeee ll ll aaaa
// SSSS tt ee ee ll ll aa
// SS tt eeeeee ll ll aaaaa -- "An Atari 2600 VCS Emulator"
// SS SS tt ee ll ll aa aa
// SSSS ttt eeeee llll llll aaaaa
//
// Copyright (c) 1995-2013 by Bradford W. Mott, Stephen Anthony
// and the Stella Team
//
// See the file "License.txt" for information on usage and redistribution of
// this file, and for a DISCLAIMER OF ALL WARRANTIES.
//
// $Id$
//============================================================================
#ifndef ZIP_HANDLER_HXX
#define ZIP_HANDLER_HXX
#include <fstream>
#include "bspf.hxx"
/***************************************************************************
Copyright Aaron Giles
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* Neither the name 'MAME' nor the names of its contributors may be
used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
***************************************************************************/
#define ZIP_DECOMPRESS_BUFSIZE 16384
/**
This class implements a thin wrapper around the zip file management code
from the MAME project.
@author Wrapper class by Stephen Anthony, with main functionality
by Aaron Giles
*/
class ZipHandler
{
public:
ZipHandler();
virtual ~ZipHandler();
// Open ZIP file for processing
void open(const string& filename);
// The following form an iterator for processing the filenames in the ZIP file
void reset(); // Reset iterator to first file
bool hasNext(); // Answer whether there are more files present
string next(); // Get next file
// Decompress the currently selected file, return false on any errors
bool decompress(uInt8* buffer, uInt32 length);
private:
// Replaces functionaity of various osd_xxxx functions
static bool stream_open(const char* filename, fstream** stream, uInt64& length);
static void stream_close(fstream** stream);
static bool stream_read(fstream* stream, void* buffer, uInt64 offset, uInt32 length, uInt32& actual);
/* Error types */
enum zip_error
{
ZIPERR_NONE = 0,
ZIPERR_OUT_OF_MEMORY,
ZIPERR_FILE_ERROR,
ZIPERR_BAD_SIGNATURE,
ZIPERR_DECOMPRESS_ERROR,
ZIPERR_FILE_TRUNCATED,
ZIPERR_FILE_CORRUPT,
ZIPERR_UNSUPPORTED,
ZIPERR_BUFFER_TOO_SMALL
};
/* contains extracted file header information */
struct zip_file_header
{
uInt32 signature; /* central file header signature */
uInt16 version_created; /* version made by */
uInt16 version_needed; /* version needed to extract */
uInt16 bit_flag; /* general purpose bit flag */
uInt16 compression; /* compression method */
uInt16 file_time; /* last mod file time */
uInt16 file_date; /* last mod file date */
uInt32 crc; /* crc-32 */
uInt32 compressed_length; /* compressed size */
uInt32 uncompressed_length; /* uncompressed size */
uInt16 filename_length; /* filename length */
uInt16 extra_field_length; /* extra field length */
uInt16 file_comment_length; /* file comment length */
uInt16 start_disk_number; /* disk number start */
uInt16 internal_attributes; /* internal file attributes */
uInt32 external_attributes; /* external file attributes */
uInt32 local_header_offset; /* relative offset of local header */
const char* filename; /* filename */
uInt8* raw; /* pointer to the raw data */
uInt32 rawlength; /* length of the raw data */
uInt8 saved; /* saved byte from after filename */
};
/* contains extracted end of central directory information */
struct zip_ecd
{
uInt32 signature; /* end of central dir signature */
uInt16 disk_number; /* number of this disk */
uInt16 cd_start_disk_number; /* number of the disk with the start of the central directory */
uInt16 cd_disk_entries; /* total number of entries in the central directory on this disk */
uInt16 cd_total_entries; /* total number of entries in the central directory */
uInt32 cd_size; /* size of the central directory */
uInt32 cd_start_disk_offset; /* offset of start of central directory with respect to the starting disk number */
uInt16 comment_length; /* .ZIP file comment length */
const char* comment; /* .ZIP file comment */
uInt8* raw; /* pointer to the raw data */
uInt32 rawlength; /* length of the raw data */
};
/* describes an open ZIP file */
struct zip_file
{
const char* filename; /* copy of ZIP filename (for caching) */
fstream* file; /* C++ fstream file handle */
uInt64 length; /* length of zip file */
zip_ecd ecd; /* end of central directory */
uInt8* cd; /* central directory raw data */
uInt32 cd_pos; /* position in central directory */
zip_file_header header; /* current file header */
uInt8 buffer[ZIP_DECOMPRESS_BUFSIZE]; /* buffer for decompression */
};
enum {
/* number of open files to cache */
ZIP_CACHE_SIZE = 8,
/* offsets in end of central directory structure */
ZIPESIG = 0x00,
ZIPEDSK = 0x04,
ZIPECEN = 0x06,
ZIPENUM = 0x08,
ZIPECENN = 0x0a,
ZIPECSZ = 0x0c,
ZIPEOFST = 0x10,
ZIPECOML = 0x14,
ZIPECOM = 0x16,
/* offsets in central directory entry structure */
ZIPCENSIG = 0x00,
ZIPCVER = 0x04,
ZIPCOS = 0x05,
ZIPCVXT = 0x06,
ZIPCEXOS = 0x07,
ZIPCFLG = 0x08,
ZIPCMTHD = 0x0a,
ZIPCTIM = 0x0c,
ZIPCDAT = 0x0e,
ZIPCCRC = 0x10,
ZIPCSIZ = 0x14,
ZIPCUNC = 0x18,
ZIPCFNL = 0x1c,
ZIPCXTL = 0x1e,
ZIPCCML = 0x20,
ZIPDSK = 0x22,
ZIPINT = 0x24,
ZIPEXT = 0x26,
ZIPOFST = 0x2a,
ZIPCFN = 0x2e,
/* offsets in local file header structure */
ZIPLOCSIG = 0x00,
ZIPVER = 0x04,
ZIPGENFLG = 0x06,
ZIPMTHD = 0x08,
ZIPTIME = 0x0a,
ZIPDATE = 0x0c,
ZIPCRC = 0x0e,
ZIPSIZE = 0x12,
ZIPUNCMP = 0x16,
ZIPFNLN = 0x1a,
ZIPXTRALN = 0x1c,
ZIPNAME = 0x1e
};
private:
/* ----- ZIP file access ----- */
/* open a ZIP file and parse its central directory */
zip_error zip_file_open(const char *filename, zip_file **zip);
/* close a ZIP file (may actually be left open due to caching) */
void zip_file_close(zip_file *zip);
/* clear out all open ZIP files from the cache */
void zip_file_cache_clear(void);
/* ----- contained file access ----- */
/* find the next file in the ZIP */
const zip_file_header *zip_file_next_file(zip_file *zip);
/* decompress the most recently found file in the ZIP */
zip_error zip_file_decompress(zip_file *zip, void *buffer, uInt32 length);
inline static uInt16 read_word(uInt8* buf)
{
return (buf[1] << 8) | buf[0];
}
inline static uInt32 read_dword(uInt8* buf)
{
return (buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0];
}
/* cache management */
static void free_zip_file(zip_file *zip);
/* ZIP file parsing */
static zip_error read_ecd(zip_file *zip);
static zip_error get_compressed_data_offset(zip_file *zip, uInt64 *offset);
/* decompression interfaces */
static zip_error decompress_data_type_0(zip_file *zip, uInt64 offset, void *buffer, uInt32 length);
static zip_error decompress_data_type_8(zip_file *zip, uInt64 offset, void *buffer, uInt32 length);
private:
zip_file* myZip;
zip_file* myZipCache[ZIP_CACHE_SIZE];
};
#endif /* ZIP_HANDLER_HXX */

View File

@ -10,7 +10,8 @@ MODULE_OBJS := \
src/common/FSNodeZIP.o \
src/common/PNGLibrary.o \
src/common/MouseControl.o \
src/common/RectList.o
src/common/RectList.o \
src/common/ZipHandler.o
MODULE_DIRS += \
src/common

View File

@ -50,7 +50,6 @@
#endif
#include "FSNode.hxx"
#include "unzip.h"
#include "MD5.hxx"
#include "Cart.hxx"
#include "Settings.hxx"
@ -198,6 +197,7 @@ OSystem::~OSystem()
delete mySerialPort;
delete myPNGLib;
delete myZipHandler;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -309,6 +309,9 @@ bool OSystem::create()
// Create PNG handler
myPNGLib = new PNGLibrary();
// Create ZIP handler
myZipHandler = new ZipHandler();
return true;
}
@ -829,6 +832,8 @@ uInt8* OSystem::openROM(string file, string& md5, uInt32& size)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
bool OSystem::loadFromZIP(const string& filename, uInt8** image, uInt32& size)
{
return false;
#if 0 // FIXME
// First determine if this actually is a ZIP file
// by seeing if it contains the '.zip' extension
size_t extpos = BSPF_findIgnoreCase(filename, ".zip");
@ -913,6 +918,7 @@ bool OSystem::loadFromZIP(const string& filename, uInt8** image, uInt32& size)
}
}
return false;
#endif
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -1247,3 +1253,6 @@ OSystem& OSystem::operator = (const OSystem&)
assert(false);
return *this;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ZipHandler* OSystem::myZipHandler = 0;

View File

@ -42,6 +42,7 @@ namespace GUI {
#include "Array.hxx"
#include "FrameBuffer.hxx"
#include "PNGLibrary.hxx"
#include "ZipHandler.hxx"
#include "bspf.hxx"
struct Resolution {
@ -180,6 +181,17 @@ class OSystem
*/
PNGLibrary& png() const { return *myPNGLib; }
/**
Get the ZIP handler of the system.
@return The ZIP object, using the given file
*/
static ZipHandler& zip(const string& file)
{
myZipHandler->open(file);
return *myZipHandler;
}
/**
This method should be called to load the current settings from an rc file.
It first loads the settings from the config file, then informs subsystems
@ -589,6 +601,9 @@ class OSystem
// Indicates whether to stop the main loop
bool myQuitLoop;
// ZIP static reference variable responsible for accessing ZIP files
static ZipHandler* myZipHandler;
private:
enum { kNumUIPalettes = 2 };
string myBaseDir;

View File

@ -66,7 +66,6 @@ MODULE_OBJS := \
src/emucore/TIASnd.o \
src/emucore/TIATables.o \
src/emucore/TrackBall.o \
src/emucore/unzip.o \
src/emucore/Thumbulator.o
MODULE_DIRS += \

File diff suppressed because it is too large Load Diff

View File

@ -1,275 +0,0 @@
/* unzip.h -- IO for uncompress .zip files using zlib
Version 0.15 beta, Mar 19th, 1998,
Copyright (C) 1998 Gilles Vollant
This unzip package allow extract file from .ZIP file, compatible with PKZip 2.04g
WinZip, InfoZip tools and compatible.
Encryption and multi volume ZipFile (span) are not supported.
Old compressions used by old PKZip 1.x are not supported
THIS IS AN ALPHA VERSION. AT THIS STAGE OF DEVELOPPEMENT, SOMES API OR STRUCTURE
CAN CHANGE IN FUTURE VERSION !!
I WAIT FEEDBACK at mail info@winimage.com
Visit also http://www.winimage.com/zLibDll/unzip.htm for evolution
Condition of use and distribution are the same than zlib :
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
/* for more info about .ZIP format, see
ftp://ftp.cdrom.com/pub/infozip/doc/appnote-970311-iz.zip
PkWare has also a specification at :
ftp://ftp.pkware.com/probdesc.zip */
#ifndef _unz_H
#define _unz_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _ZLIB_H
#include <zlib.h>
#endif
#if defined(STRICTUNZIP) || defined(STRICTZIPUNZIP)
/* like the STRICT of WIN32, we define a pointer that cannot be converted
from (void*) without cast */
typedef struct TagunzFile__ { int unused; } unzFile__;
typedef unzFile__ *unzFile;
#else
typedef voidp unzFile;
#endif
#define UNZ_OK (0)
#define UNZ_END_OF_LIST_OF_FILE (-100)
#define UNZ_ERRNO (Z_ERRNO)
#define UNZ_EOF (0)
#define UNZ_PARAMERROR (-102)
#define UNZ_BADZIPFILE (-103)
#define UNZ_INTERNALERROR (-104)
#define UNZ_CRCERROR (-105)
/* tm_unz contain date/time info */
typedef struct tm_unz_s
{
uInt tm_sec; /* seconds after the minute - [0,59] */
uInt tm_min; /* minutes after the hour - [0,59] */
uInt tm_hour; /* hours since midnight - [0,23] */
uInt tm_mday; /* day of the month - [1,31] */
uInt tm_mon; /* months since January - [0,11] */
uInt tm_year; /* years - [1980..2044] */
} tm_unz;
/* unz_global_info structure contain global data about the ZIPfile
These data comes from the end of central dir */
typedef struct unz_global_info_s
{
uLong number_entry; /* total number of entries in
the central dir on this disk */
uLong size_comment; /* size of the global comment of the zipfile */
} unz_global_info;
/* unz_file_info contain information about a file in the zipfile */
typedef struct unz_file_info_s
{
uLong version; /* version made by 2 bytes */
uLong version_needed; /* version needed to extract 2 bytes */
uLong flag; /* general purpose bit flag 2 bytes */
uLong compression_method; /* compression method 2 bytes */
uLong dosDate; /* last mod file date in Dos fmt 4 bytes */
uLong crc; /* crc-32 4 bytes */
uLong compressed_size; /* compressed size 4 bytes */
uLong uncompressed_size; /* uncompressed size 4 bytes */
uLong size_filename; /* filename length 2 bytes */
uLong size_file_extra; /* extra field length 2 bytes */
uLong size_file_comment; /* file comment length 2 bytes */
uLong disk_num_start; /* disk number start 2 bytes */
uLong internal_fa; /* internal file attributes 2 bytes */
uLong external_fa; /* external file attributes 4 bytes */
tm_unz tmu_date;
} unz_file_info;
extern int ZEXPORT unzStringFileNameCompare OF ((const char* fileName1,
const char* fileName2,
int iCaseSensitivity));
/*
Compare two filename (fileName1,fileName2).
If iCaseSenisivity = 1, comparision is case sensitivity (like strcmp)
If iCaseSenisivity = 2, comparision is not case sensitivity (like strcmpi
or strcasecmp)
If iCaseSenisivity = 0, case sensitivity is defaut of your operating system
(like 1 on Unix, 2 on Windows)
*/
extern unzFile ZEXPORT unzOpen OF((const char *path));
/*
Open a Zip file. path contain the full pathname (by example,
on a Windows NT computer "c:\\zlib\\zlib111.zip" or on an Unix computer
"zlib/zlib111.zip".
If the zipfile cannot be opened (file don't exist or in not valid), the
return value is NULL.
Else, the return value is a unzFile Handle, usable with other function
of this unzip package.
*/
extern int ZEXPORT unzClose OF((unzFile file));
/*
Close a ZipFile opened with unzipOpen.
If there is files inside the .Zip opened with unzOpenCurrentFile (see later),
these files MUST be closed with unzipCloseCurrentFile before call unzipClose.
return UNZ_OK if there is no problem. */
extern int ZEXPORT unzGetGlobalInfo OF((unzFile file,
unz_global_info *pglobal_info));
/*
Write info about the ZipFile in the *pglobal_info structure.
No preparation of the structure is needed
return UNZ_OK if there is no problem. */
extern int ZEXPORT unzGetGlobalComment OF((unzFile file,
char *szComment,
uLong uSizeBuf));
/*
Get the global comment string of the ZipFile, in the szComment buffer.
uSizeBuf is the size of the szComment buffer.
return the number of byte copied or an error code <0
*/
/***************************************************************************/
/* Unzip package allow you browse the directory of the zipfile */
extern int ZEXPORT unzGoToFirstFile OF((unzFile file));
/*
Set the current file of the zipfile to the first file.
return UNZ_OK if there is no problem
*/
extern int ZEXPORT unzGoToNextFile OF((unzFile file));
/*
Set the current file of the zipfile to the next file.
return UNZ_OK if there is no problem
return UNZ_END_OF_LIST_OF_FILE if the actual file was the latest.
*/
extern int ZEXPORT unzLocateFile OF((unzFile file,
const char *szFileName,
int iCaseSensitivity));
/*
Try locate the file szFileName in the zipfile.
For the iCaseSensitivity signification, see unzStringFileNameCompare
return value :
UNZ_OK if the file is found. It becomes the current file.
UNZ_END_OF_LIST_OF_FILE if the file is not found
*/
extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file,
unz_file_info *pfile_info,
char *szFileName,
uLong fileNameBufferSize,
void *extraField,
uLong extraFieldBufferSize,
char *szComment,
uLong commentBufferSize));
/*
Get Info about the current file
if pfile_info!=NULL, the *pfile_info structure will contain somes info about
the current file
if szFileName!=NULL, the filemane string will be copied in szFileName
(fileNameBufferSize is the size of the buffer)
if extraField!=NULL, the extra field information will be copied in extraField
(extraFieldBufferSize is the size of the buffer).
This is the Central-header version of the extra field
if szComment!=NULL, the comment string of the file will be copied in szComment
(commentBufferSize is the size of the buffer)
*/
/***************************************************************************/
/* for reading the content of the current zipfile, you can open it, read data
from it, and close it (you can close it before reading all the file)
*/
extern int ZEXPORT unzOpenCurrentFile OF((unzFile file));
/*
Open for reading data the current file in the zipfile.
If there is no error, the return value is UNZ_OK.
*/
extern int ZEXPORT unzCloseCurrentFile OF((unzFile file));
/*
Close the file in zip opened with unzOpenCurrentFile
Return UNZ_CRCERROR if all the file was read but the CRC is not good
*/
extern int ZEXPORT unzReadCurrentFile OF((unzFile file,
voidp buf,
unsigned len));
/*
Read bytes from the current file (opened by unzOpenCurrentFile)
buf contain buffer where data must be copied
len the size of buf.
return the number of byte copied if somes bytes are copied
return 0 if the end of file was reached
return <0 with error code if there is an error
(UNZ_ERRNO for IO error, or zLib error for uncompress error)
*/
extern z_off_t ZEXPORT unztell OF((unzFile file));
/*
Give the current position in uncompressed data
*/
extern int ZEXPORT unzeof OF((unzFile file));
/*
return 1 if the end of file was reached, 0 elsewhere
*/
extern int ZEXPORT unzGetLocalExtrafield OF((unzFile file,
voidp buf,
unsigned len));
/*
Read extra field from the current file (opened by unzOpenCurrentFile)
This is the local-header version of the extra field (sometimes, there is
more info in the local-header version than in the central-header)
if buf==NULL, it return the size of the local extra field
if buf!=NULL, len is the size of the buffer, the extra header is copied in
buf.
the return value is the number of bytes copied in buf, or (if <0)
the error code
*/
#ifdef __cplusplus
}
#endif
#endif /* _unz_H */

View File

@ -44,7 +44,6 @@
#include "StringList.hxx"
#include "StringListWidget.hxx"
#include "Widget.hxx"
#include "unzip.h"
#include "LauncherDialog.hxx"
@ -349,9 +348,9 @@ void LauncherDialog::loadDirListing()
files.reserve(2048);
if(myCurrentNode.isDirectory())
{
myCurrentNode.getChildren(files, FilesystemNode::kListAll);
}
#if 0
else
{
unzFile tz;
@ -394,6 +393,7 @@ void LauncherDialog::loadDirListing()
}
}
}
#endif
// Add '[..]' to indicate previous folder
if(myCurrentNode.hasParent())
@ -528,6 +528,7 @@ bool LauncherDialog::matchPattern(const string& s, const string& pattern) const
int LauncherDialog::filesInArchive(const string& archive)
{
int count = -1;
#if 0
unzFile tz;
if((tz = unzOpen(archive.c_str())) != NULL)
{
@ -562,6 +563,7 @@ int LauncherDialog::filesInArchive(const string& archive)
}
}
}
#endif
return count;
}
@ -606,9 +608,10 @@ void LauncherDialog::handleCommand(CommandSender* sender, int cmd,
const string& md5 = myGameList->md5(item);
string extension;
const FilesystemNode romnode(rom);
int numFilesInArchive = filesInArchive(rom);
bool isArchive = !myGameList->isDir(item) &&
BSPF_endsWithIgnoreCase(rom, ".zip");
bool isArchive = false;//!myGameList->isDir(item) && BSPF_endsWithIgnoreCase(rom, ".zip");
// Directory's should be selected (ie, enter them and redisplay)
// Archives should be entered if they contain more than 1 file
@ -617,7 +620,7 @@ void LauncherDialog::handleCommand(CommandSender* sender, int cmd,
instance().frameBuffer().showMessage("Archive does not contain any valid ROM files",
kMiddleCenter, true);
}
else if((isArchive && numFilesInArchive > 1) || myGameList->isDir(item))
else if(/*(isArchive && numFilesInArchive > 1) ||*/ romnode.isDirectory())
{
string dirname = "";
if(myGameList->name(item) == " [..]")
@ -628,7 +631,7 @@ void LauncherDialog::handleCommand(CommandSender* sender, int cmd,
}
else
{
myCurrentNode = FilesystemNode(rom);
myCurrentNode = romnode;
myNodeNames.push(myGameList->name(item));
}
updateListing(dirname);
@ -637,7 +640,7 @@ void LauncherDialog::handleCommand(CommandSender* sender, int cmd,
{
if(LauncherFilterDialog::isValidRomName(rom, extension))
{
if(instance().createConsole(rom, md5))
if(instance().createConsole(romnode.getPath(), md5))
instance().settings().setString("lastrom", myList->getSelectedString());
else
instance().frameBuffer().showMessage(