2003-03-06 21:46:56 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
2006-01-18 17:39:49 +00:00
|
|
|
* Copyright (C) 2002-2006 The ScummVM project
|
2002-08-24 15:31:37 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* 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, write to the Free Software
|
2005-10-18 01:30:26 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-08-24 15:31:37 +00:00
|
|
|
*
|
2006-02-11 09:55:41 +00:00
|
|
|
* $URL$
|
|
|
|
* $Id$
|
2002-08-24 15:31:37 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2005-06-24 15:23:51 +00:00
|
|
|
#include "common/stdafx.h"
|
2003-10-03 18:33:57 +00:00
|
|
|
#include "scumm/smush/chunk.h"
|
2004-07-26 23:15:01 +00:00
|
|
|
#include "scumm/scumm.h"
|
2006-03-03 15:16:02 +00:00
|
|
|
#include "scumm/file.h"
|
2002-08-24 15:31:37 +00:00
|
|
|
|
2002-09-13 18:04:17 +00:00
|
|
|
#include "common/file.h"
|
2002-10-23 12:12:19 +00:00
|
|
|
#include "common/str.h"
|
2003-09-10 12:31:20 +00:00
|
|
|
#include "common/util.h"
|
2002-08-31 13:29:10 +00:00
|
|
|
|
2003-10-03 18:33:57 +00:00
|
|
|
namespace Scumm {
|
|
|
|
|
2005-07-30 21:11:48 +00:00
|
|
|
BaseChunk::BaseChunk() :
|
2003-03-17 12:28:50 +00:00
|
|
|
_type(0),
|
|
|
|
_size(0),
|
2006-05-18 00:20:53 +00:00
|
|
|
_curPos(0),
|
|
|
|
_name("") {
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
bool BaseChunk::eof() const {
|
2005-07-30 21:11:48 +00:00
|
|
|
return _curPos >= _size;
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
uint32 BaseChunk::tell() const {
|
2005-07-30 21:11:48 +00:00
|
|
|
return _curPos;
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2005-07-30 21:11:48 +00:00
|
|
|
Chunk::type BaseChunk::getType() const {
|
|
|
|
return _type;
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2005-07-30 21:11:48 +00:00
|
|
|
uint32 BaseChunk::getSize() const {
|
|
|
|
return _size;
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
bool BaseChunk::seek(int32 delta, seek_type dir) {
|
2006-07-14 13:33:58 +00:00
|
|
|
switch (dir) {
|
2003-09-24 06:33:59 +00:00
|
|
|
case seek_cur:
|
|
|
|
_curPos += delta;
|
|
|
|
break;
|
|
|
|
case seek_start:
|
|
|
|
if (delta < 0)
|
|
|
|
error("invalid seek request");
|
|
|
|
|
|
|
|
_curPos = (uint32)delta;
|
|
|
|
break;
|
|
|
|
case seek_end:
|
|
|
|
if (delta > 0 || _size < (uint32)-delta)
|
|
|
|
error("invalid seek request");
|
|
|
|
|
|
|
|
_curPos = (uint32)(_size + delta);
|
|
|
|
break;
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
2006-05-18 00:20:53 +00:00
|
|
|
|
2003-06-07 00:45:32 +00:00
|
|
|
if (_curPos > _size) {
|
2006-10-13 08:51:26 +00:00
|
|
|
// It may happen that user misused our SAN compression tool
|
|
|
|
// and ignored FLU index for videos which are used by INSANE.
|
|
|
|
// This will lead to incorrect seek requests
|
|
|
|
//
|
2006-10-14 12:31:08 +00:00
|
|
|
// In fact it may happen only within INSANE, so do not even check for it
|
|
|
|
warning("Looks like you compressed file %s in wrong way. It has FLU index which was not updated", _name.c_str());
|
2002-08-24 15:31:37 +00:00
|
|
|
error("invalid seek request : %d > %d (delta == %d)", _curPos, _size, delta);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-04-23 17:47:40 +00:00
|
|
|
FileChunk::FileChunk(BaseScummFile *data, int offset) {
|
2005-06-26 23:37:59 +00:00
|
|
|
_data = data;
|
|
|
|
_deleteData = false;
|
|
|
|
|
|
|
|
_data->seek(offset, seek_start);
|
|
|
|
_type = _data->readUint32BE();
|
|
|
|
_size = _data->readUint32BE();
|
|
|
|
_offset = _data->pos();
|
|
|
|
_curPos = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FileChunk::FileChunk(const Common::String &name, int offset) {
|
|
|
|
_data = new ScummFile();
|
|
|
|
_deleteData = true;
|
2006-04-23 17:39:31 +00:00
|
|
|
if (!g_scumm->openFile(*_data, name))
|
2005-03-07 00:38:18 +00:00
|
|
|
error("FileChunk: Unable to open file %s", name.c_str());
|
|
|
|
|
2005-06-26 23:37:59 +00:00
|
|
|
_data->seek(offset, seek_start);
|
|
|
|
_type = _data->readUint32BE();
|
|
|
|
_size = _data->readUint32BE();
|
|
|
|
_offset = _data->pos();
|
2003-05-25 11:39:08 +00:00
|
|
|
_curPos = 0;
|
2006-05-18 00:20:53 +00:00
|
|
|
_name = name;
|
2003-05-25 11:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FileChunk::~FileChunk() {
|
2005-06-26 23:37:59 +00:00
|
|
|
if (_deleteData)
|
|
|
|
delete _data;
|
2003-05-25 11:39:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Chunk *FileChunk::subBlock() {
|
2005-06-26 23:37:59 +00:00
|
|
|
FileChunk *ptr = new FileChunk(_data, _offset + _curPos);
|
2003-06-21 20:33:46 +00:00
|
|
|
seek(sizeof(Chunk::type) + sizeof(uint32) + ptr->getSize());
|
2003-05-25 11:39:08 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2005-06-26 23:37:59 +00:00
|
|
|
void FileChunk::reseek() {
|
|
|
|
_data->seek(_offset + _curPos);
|
|
|
|
}
|
|
|
|
|
2003-03-06 08:36:56 +00:00
|
|
|
bool FileChunk::read(void *buffer, uint32 size) {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (size <= 0 || (_curPos + size) > _size)
|
2003-03-06 08:36:56 +00:00
|
|
|
error("invalid buffer read request");
|
2003-03-17 12:28:50 +00:00
|
|
|
|
2005-06-26 23:37:59 +00:00
|
|
|
_data->read(buffer, size);
|
2002-08-24 15:31:37 +00:00
|
|
|
_curPos += size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-08-25 22:23:47 +00:00
|
|
|
int8 FileChunk::getChar() {
|
2003-06-21 20:33:46 +00:00
|
|
|
return (int8)getByte();
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2002-08-30 07:24:45 +00:00
|
|
|
byte FileChunk::getByte() {
|
2003-06-21 20:33:46 +00:00
|
|
|
_curPos++;
|
|
|
|
|
|
|
|
if (_curPos > _size)
|
2003-03-06 08:36:56 +00:00
|
|
|
error("invalid byte read request");
|
2003-03-17 12:28:50 +00:00
|
|
|
|
2005-06-26 23:37:59 +00:00
|
|
|
return _data->readByte();
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2002-08-30 07:24:45 +00:00
|
|
|
int16 FileChunk::getShort() {
|
2003-06-21 20:33:46 +00:00
|
|
|
return (int16)getWord();
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2002-08-30 07:24:45 +00:00
|
|
|
uint16 FileChunk::getWord() {
|
2003-06-21 20:33:46 +00:00
|
|
|
_curPos += 2;
|
|
|
|
|
|
|
|
if (_curPos > _size)
|
2003-03-06 08:36:56 +00:00
|
|
|
error("invalid word read request");
|
2003-03-17 12:28:50 +00:00
|
|
|
|
2005-06-26 23:37:59 +00:00
|
|
|
return _data->readUint16LE();
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2002-08-30 07:24:45 +00:00
|
|
|
uint32 FileChunk::getDword() {
|
2003-06-21 20:33:46 +00:00
|
|
|
_curPos += 4;
|
|
|
|
|
|
|
|
if (_curPos > _size)
|
2003-03-06 08:36:56 +00:00
|
|
|
error("invalid dword read request");
|
2003-03-17 12:28:50 +00:00
|
|
|
|
2005-06-26 23:37:59 +00:00
|
|
|
return _data->readUint32LE();
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
MemoryChunk::MemoryChunk(byte *data) {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (data == 0)
|
2003-03-17 12:28:50 +00:00
|
|
|
error("Chunk() called with NULL pointer");
|
|
|
|
|
2002-08-24 17:18:55 +00:00
|
|
|
_type = (Chunk::type)READ_BE_UINT32(data);
|
2002-08-30 07:24:45 +00:00
|
|
|
_size = READ_BE_UINT32(data + 4);
|
|
|
|
_data = data + sizeof(Chunk::type) + sizeof(uint32);
|
2002-08-24 15:31:37 +00:00
|
|
|
_curPos = 0;
|
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
Chunk *MemoryChunk::subBlock() {
|
|
|
|
MemoryChunk *ptr = new MemoryChunk(_data + _curPos);
|
2002-08-30 07:24:45 +00:00
|
|
|
seek(sizeof(Chunk::type) + sizeof(uint32) + ptr->getSize());
|
2002-08-24 15:31:37 +00:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2005-06-26 23:37:59 +00:00
|
|
|
void MemoryChunk::reseek() {
|
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
bool MemoryChunk::read(void *buffer, uint32 size) {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (size <= 0 || (_curPos + size) > _size)
|
2003-03-17 12:28:50 +00:00
|
|
|
error("invalid buffer read request");
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
memcpy(buffer, _data + _curPos, size);
|
|
|
|
_curPos += size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
int8 MemoryChunk::getChar() {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (_curPos >= _size)
|
2003-03-17 12:28:50 +00:00
|
|
|
error("invalid char read request");
|
|
|
|
|
2002-08-24 15:31:37 +00:00
|
|
|
return _data[_curPos++];
|
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
byte MemoryChunk::getByte() {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (_curPos >= _size)
|
2003-03-17 12:28:50 +00:00
|
|
|
error("invalid byte read request");
|
|
|
|
|
2003-03-06 08:36:56 +00:00
|
|
|
byte *ptr = (byte *)(_data + _curPos);
|
2002-08-24 15:31:37 +00:00
|
|
|
_curPos += 1;
|
|
|
|
return *ptr;
|
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
int16 MemoryChunk::getShort() {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (_curPos >= _size - 1)
|
2003-03-17 12:28:50 +00:00
|
|
|
error("invalid int16 read request");
|
|
|
|
|
2002-08-30 07:24:45 +00:00
|
|
|
int16 buffer = getWord();
|
2003-03-06 08:36:56 +00:00
|
|
|
return *((int16 *)&buffer);
|
2002-08-24 15:31:37 +00:00
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
uint16 MemoryChunk::getWord() {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (_curPos >= _size - 1)
|
2003-03-17 12:28:50 +00:00
|
|
|
error("invalid word read request");
|
|
|
|
|
2003-03-06 08:36:56 +00:00
|
|
|
uint16 *ptr = (uint16 *)(_data + _curPos);
|
2002-08-24 15:31:37 +00:00
|
|
|
_curPos += 2;
|
|
|
|
return READ_LE_UINT16(ptr);
|
|
|
|
}
|
|
|
|
|
2003-05-25 11:39:08 +00:00
|
|
|
uint32 MemoryChunk::getDword() {
|
2003-06-07 00:45:32 +00:00
|
|
|
if (_curPos >= _size - 3)
|
2003-03-17 12:28:50 +00:00
|
|
|
error("invalid dword read request");
|
|
|
|
|
2003-03-06 08:36:56 +00:00
|
|
|
uint32 *ptr = (uint32 *)(_data + _curPos);
|
2002-08-24 15:31:37 +00:00
|
|
|
_curPos += 4;
|
|
|
|
return READ_LE_UINT32(ptr);
|
|
|
|
}
|
2003-10-03 18:33:57 +00:00
|
|
|
|
|
|
|
} // End of namespace Scumm
|