1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
|
////////////////////////////////////////////////////////////////////////////////////////
//
// Nestopia - NES/Famicom emulator written in C++
//
// Copyright (C) 2003-2008 Martin Freij
//
// This file is part of Nestopia.
//
// Nestopia 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.
//
// Nestopia 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 Nestopia; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
////////////////////////////////////////////////////////////////////////////////////////
#include <cstring>
#include <new>
#include "NstStream.hpp"
#include "NstVector.hpp"
#include "NstChecksum.hpp"
#include "NstPatcher.hpp"
#include "NstFile.hpp"
#include "api/NstApiUser.hpp"
namespace Nes
{
namespace Core
{
struct File::Context
{
Checksum checksum;
Vector<byte> data;
};
File::File()
: context( *new Context )
{
}
File::~File()
{
delete &context;
}
void File::Load(const byte* data,dword size) const
{
NST_ASSERT( data && size );
context.checksum.Clear();
context.checksum.Compute( data, size );
context.data.Destroy();
}
void File::Load(byte* data,dword size,Type type) const
{
NST_ASSERT( data && size );
context.data.Assign( data, size );
bool altered = false;
const LoadBlock loadBlock = {data,size};
Load( type, &loadBlock, 1, &altered );
if (altered)
context.checksum.Clear();
}
void File::Load(Type type,byte* data,dword size) const
{
const LoadBlock loadBlock = {data,size};
Load( type, &loadBlock, 1 );
}
void File::Load(const Type type,const LoadBlock* const loadBlock,const uint loadBlockCount,bool* const altered) const
{
class Loader : public Api::User::File
{
const Action action;
const LoadBlock* const loadBlock;
const uint loadBlockCount;
bool* const altered;
Action GetAction() const throw()
{
return action;
}
ulong GetMaxSize() const throw()
{
dword maxsize = 0;
for (const LoadBlock* NST_RESTRICT it=loadBlock, *const end=loadBlock+loadBlockCount; it != end; ++it)
maxsize += it->size;
return maxsize;
}
Result SetContent(const void* data,ulong filesize) throw()
{
if (altered)
*altered = true;
if (!data || !filesize)
return RESULT_ERR_INVALID_PARAM;
const byte* NST_RESTRICT filedata = static_cast<const byte*>(data);
for (const LoadBlock* NST_RESTRICT it=loadBlock, *const end=loadBlock+loadBlockCount; it != end; ++it)
{
if (const dword size = NST_MIN(filesize,it->size))
{
std::memcpy( it->data, filedata, size );
filedata += size;
filesize -= size;
}
}
return RESULT_OK;
}
void GetRawStorage(void*& data, ulong& size) const throw()
{
if (loadBlockCount == 1)
{
data = loadBlock->data;
size = loadBlock->size;
}
else
{
data = 0;
size = 0;
}
}
Result SetContent(std::istream& stdStream) throw()
{
if (altered)
*altered = true;
try
{
Nes::Core::Stream::In stream( &stdStream );
if (ulong length = stream.Length())
{
for (const LoadBlock* NST_RESTRICT it=loadBlock, *const end=loadBlock+loadBlockCount; it != end; ++it)
{
if (const dword size = NST_MIN(length,it->size))
{
stream.Read( it->data, size );
length -= size;
}
}
}
else
{
return RESULT_ERR_INVALID_PARAM;
}
}
catch (Result result)
{
return result;
}
catch (const std::bad_alloc&)
{
return RESULT_ERR_OUT_OF_MEMORY;
}
catch (...)
{
return RESULT_ERR_GENERIC;
}
return RESULT_OK;
}
Result SetPatchContent(std::istream& stream) throw()
{
if (altered)
*altered = true;
Patcher patcher;
Result result = patcher.Load( stream );
if (NES_FAILED(result))
return result;
if (loadBlockCount > 1)
{
Patcher::Block* const patchBlocks = new (std::nothrow) Patcher::Block [loadBlockCount];
if (!patchBlocks)
return RESULT_ERR_OUT_OF_MEMORY;
for (uint i=0; i < loadBlockCount; ++i)
{
patchBlocks[i].data = loadBlock[i].data;
patchBlocks[i].size = loadBlock[i].size;
}
result = patcher.Test( patchBlocks, loadBlockCount );
delete [] patchBlocks;
}
else
{
result = patcher.Test( loadBlockCount ? loadBlock->data : NULL, loadBlockCount ? loadBlock->size : 0 );
}
if (NES_SUCCEEDED(result))
{
for (dword i=0, offset=0; i < loadBlockCount; offset += loadBlock[i].size, ++i)
patcher.Patch( loadBlock[i].data, loadBlock[i].data, loadBlock[i].size, offset );
}
return result;
}
public:
Loader(Type t,const LoadBlock* l,uint c,bool* a)
:
action
(
t == EEPROM ? LOAD_EEPROM :
t == TAPE ? LOAD_TAPE :
t == TURBOFILE ? LOAD_TURBOFILE :
t == DISK ? LOAD_FDS :
LOAD_BATTERY
),
loadBlock (l),
loadBlockCount (c),
altered (a)
{
if (altered)
*altered = false;
}
};
{
Loader loader( type, loadBlock, loadBlockCount, altered );
Api::User::fileIoCallback( loader );
}
context.checksum.Clear();
for (const LoadBlock* NST_RESTRICT it=loadBlock, *const end=loadBlock+loadBlockCount; it != end; ++it)
context.checksum.Compute( it->data, it->size );
}
void File::Load(const Type type,Vector<byte>& buffer,const dword maxsize) const
{
NST_ASSERT( maxsize && type != DISK );
class Loader : public Api::User::File
{
const Action action;
Vector<byte>& buffer;
const dword maxsize;
Action GetAction() const throw()
{
return action;
}
ulong GetMaxSize() const throw()
{
return maxsize;
}
Result SetContent(const void* filedata,ulong filesize) throw()
{
if (!filedata || !filesize)
return RESULT_ERR_INVALID_PARAM;
try
{
buffer.Assign( static_cast<const byte*>(filedata), NST_MIN(filesize,maxsize) );
}
catch (const std::bad_alloc&)
{
return RESULT_ERR_OUT_OF_MEMORY;
}
catch (...)
{
return RESULT_ERR_GENERIC;
}
return RESULT_OK;
}
Result SetContent(std::istream& stdStream) throw()
{
try
{
Nes::Core::Stream::In stream( &stdStream );
if (const ulong length = stream.Length())
{
buffer.Resize( NST_MIN(length,maxsize) );
try
{
stream.Read( buffer.Begin(), buffer.Size() );
}
catch (...)
{
buffer.Destroy();
throw;
}
}
else
{
return RESULT_ERR_INVALID_PARAM;
}
}
catch (Result result)
{
return result;
}
catch (const std::bad_alloc&)
{
return RESULT_ERR_OUT_OF_MEMORY;
}
catch (...)
{
return RESULT_ERR_GENERIC;
}
return RESULT_OK;
}
public:
Loader(Type t,Vector<byte>& b,dword m)
:
action
(
t == EEPROM ? LOAD_EEPROM :
t == TAPE ? LOAD_TAPE :
t == TURBOFILE ? LOAD_TURBOFILE :
LOAD_BATTERY
),
buffer (b),
maxsize (m)
{
}
};
{
Loader loader( type, buffer, maxsize );
Api::User::fileIoCallback( loader );
}
if (buffer.Size())
Load( buffer.Begin(), buffer.Size() );
}
void File::Save(Type type,const byte* data,dword size) const
{
const SaveBlock saveBlock = {data,size};
Save( type, &saveBlock, 1 );
}
void File::Save(const Type type,const SaveBlock* const saveBlock,const uint saveBlockCount) const
{
NST_ASSERT( saveBlock && saveBlockCount );
Checksum checksum;
for (const SaveBlock *NST_RESTRICT it=saveBlock, *const end=saveBlock+saveBlockCount; it != end; ++it)
checksum.Compute( it->data, it->size );
if (checksum != context.checksum)
{
class Saver : public Api::User::File
{
const Action action;
const SaveBlock* const saveBlock;
const uint saveBlockCount;
mutable Vector<byte> buffer;
const Vector<byte> original;
Action GetAction() const throw()
{
return action;
}
ulong GetMaxSize() const throw()
{
dword size = 0;
for (const SaveBlock* NST_RESTRICT it=saveBlock, *const end=saveBlock+saveBlockCount; it != end; ++it)
size += it->size;
return size;
}
Result GetContent(const void*& filedata,ulong& filesize) const throw()
{
if (saveBlockCount <= 1)
{
filedata = saveBlock->data;
filesize = saveBlock->size;
}
else
{
if (!buffer.Size())
{
try
{
buffer.Resize( Saver::GetMaxSize() );
}
catch (...)
{
filedata = NULL;
filesize = 0;
return RESULT_ERR_OUT_OF_MEMORY;
}
dword offset = 0;
for (const SaveBlock* NST_RESTRICT it=saveBlock, *const end=saveBlock+saveBlockCount; it != end; ++it)
{
std::memcpy( &buffer[offset], it->data, it->size );
offset += it->size;
}
}
filedata = buffer.Begin();
filesize = buffer.Size();
}
return RESULT_OK;
}
Result GetContent(std::ostream& stdStream) const throw()
{
try
{
Nes::Core::Stream::Out stream( &stdStream );
for (const SaveBlock* NST_RESTRICT it=saveBlock, *const end=saveBlock+saveBlockCount; it != end; ++it)
{
if (it->size)
stream.Write( it->data, it->size );
}
}
catch (Result result)
{
return result;
}
catch (const std::bad_alloc&)
{
return RESULT_ERR_OUT_OF_MEMORY;
}
catch (...)
{
return RESULT_ERR_GENERIC;
}
return RESULT_OK;
}
Result GetPatchContent(Patch format,std::ostream& stream) const throw()
{
if (!original.Size() || (format != PATCH_UPS && format != PATCH_IPS))
return RESULT_ERR_UNSUPPORTED;
const void* data;
ulong size;
Result result = Saver::GetContent( data, size );
if (NES_FAILED(result))
return result;
if (size != original.Size())
return RESULT_ERR_UNSUPPORTED;
Patcher patcher;
result = patcher.Create
(
format == PATCH_UPS ? Patcher::UPS : Patcher::IPS,
original.Begin(),
static_cast<const byte*>(data),
size
);
if (NES_FAILED(result))
return result;
return patcher.Save( stream );
}
public:
Saver(Type t,const SaveBlock* s,uint c,const Vector<byte>& o)
:
action
(
t == EEPROM ? SAVE_EEPROM :
t == TAPE ? SAVE_TAPE :
t == TURBOFILE ? SAVE_TURBOFILE :
t == DISK ? SAVE_FDS :
SAVE_BATTERY
),
saveBlock (s),
saveBlockCount (c),
original (o)
{
}
};
Saver saver( type, saveBlock, saveBlockCount, context.data );
Api::User::fileIoCallback( saver );
}
}
}
}
|