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 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682
|
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */
#include "EmulatorCommon.h"
#include "ChunkFile.h"
#include "Byteswapping.h" // Canonical
#include "Platform.h" // ReallocMemory
#include "Platform_Files.h" // StreamHandle
/***********************************************************************
*
* FUNCTION: ChunkFile constructor
*
* DESCRIPTION: Initialize the ChunkFile object. Sets the fStream data
* member to refer to the given FileHandle (which must
* exist for the life of the ChunkFile).
*
* PARAMETERS: s - StreamHandle to utilize for read/write operations.
*
* RETURNED: Nothing
*
***********************************************************************/
ChunkFile::ChunkFile (StreamHandle& s) :
fStream (s)
{
}
/***********************************************************************
*
* FUNCTION: ChunkFile destructor
*
* DESCRIPTION: Releases ChunkFile resources. Currently does nothing.
*
* PARAMETERS: None
*
* RETURNED: Nothing
*
***********************************************************************/
ChunkFile::~ChunkFile (void)
{
}
/***********************************************************************
*
* FUNCTION: ChunkFile::FindChunk
*
* DESCRIPTION: Find the requested chunk. If successful, the file
* marker will be pointing to the chunk data which can
* then be read with ReadChunk.
*
* PARAMETERS: targetTag - the tag of the chunk to find.
*
* RETURNED: Size of the chunk, in bytes. If the chunk can't be
* found, kChunkNotFound is returned.
*
***********************************************************************/
long ChunkFile::FindChunk (Tag targetTag)
{
long len = kChunkNotFound;
long fileOffset = 0;
long fileLength = fStream.GetLength ();
fStream.SetPos (fileOffset, kSeekSet);
while (fileOffset < fileLength)
{
Tag chunkTag;
long chunkLen;
fStream.Read (sizeof (chunkTag), &chunkTag);
fStream.Read (sizeof (chunkLen), &chunkLen);
Canonical (chunkTag);
Canonical (chunkLen);
if (chunkTag == targetTag)
{
len = chunkLen;
break;
}
fStream.SetPos (chunkLen, kSeekCur);
fileOffset += sizeof (chunkTag) + sizeof (chunkLen) + chunkLen;
}
return len;
}
/***********************************************************************
*
* FUNCTION: ChunkFile::ReadChunk
*
* DESCRIPTION: Read a block of bytes into the given buffer.
*
* PARAMETERS: tag - marker identifying the data to read.
*
* chunk - object to contain the returned data.
*
* RETURNED: True if the data was found and read in.
*
***********************************************************************/
Bool ChunkFile::ReadChunk (Tag tag, Chunk& chunk)
{
long size = this->FindChunk (tag);
if (size == kChunkNotFound)
return false;
chunk.SetLength (size);
this->ReadChunk (size, chunk.GetPointer ());
return true;
}
/***********************************************************************
*
* FUNCTION: ChunkFile::ReadInt
*
* DESCRIPTION: Read an integer (scalar) from the file.
*
* PARAMETERS: tag - marker of the value to be read.
*
* val - reference to variable to receive the integer.
*
* RETURNED: True if the value existed; false if the tag could not
* be found.
*
***********************************************************************/
Bool ChunkFile::ReadInt (Tag tag, uae_u8& val)
{
if (ChunkFile::FindChunk (tag) == kChunkNotFound)
return false;
ChunkFile::ReadChunk (sizeof (val), &val);
Canonical (val);
return true;
}
Bool ChunkFile::ReadInt (Tag tag, uae_s8& val)
{
if (ChunkFile::FindChunk (tag) == kChunkNotFound)
return false;
ChunkFile::ReadChunk (sizeof (val), &val);
Canonical (val);
return true;
}
Bool ChunkFile::ReadInt (Tag tag, uae_u16& val)
{
if (ChunkFile::FindChunk (tag) == kChunkNotFound)
return false;
ChunkFile::ReadChunk (sizeof (val), &val);
Canonical (val);
return true;
}
Bool ChunkFile::ReadInt (Tag tag, uae_s16& val)
{
if (ChunkFile::FindChunk (tag) == kChunkNotFound)
return false;
ChunkFile::ReadChunk (sizeof (val), &val);
Canonical (val);
return true;
}
Bool ChunkFile::ReadInt (Tag tag, uae_u32& val)
{
if (ChunkFile::FindChunk (tag) == kChunkNotFound)
return false;
ChunkFile::ReadChunk (sizeof (val), &val);
Canonical (val);
return true;
}
Bool ChunkFile::ReadInt (Tag tag, uae_s32& val)
{
if (ChunkFile::FindChunk (tag) == kChunkNotFound)
return false;
ChunkFile::ReadChunk (sizeof (val), &val);
Canonical (val);
return true;
}
/***********************************************************************
*
* FUNCTION: ChunkFile::ReadString
*
* DESCRIPTION: Read a string from the file.
*
* PARAMETERS: tag - marker of the string to be read.
*
* s - reference to variable to receive the string.
*
* RETURNED: True if the string existed; false if the tag could not
* be found.
*
***********************************************************************/
Bool ChunkFile::ReadString (Tag tag, char* s)
{
long len = ChunkFile::FindChunk (tag);
if (len == kChunkNotFound)
return false;
if (len > 0)
{
ChunkFile::ReadChunk (len, s);
}
s[len] = '\0'; // Null terminator
return true;
}
Bool ChunkFile::ReadString (Tag tag, string& s)
{
long len = ChunkFile::FindChunk (tag);
if (len == kChunkNotFound)
return false;
s.resize (len);
if (len > 0)
{
ChunkFile::ReadChunk (len, &s[0]);
}
return true;
}
/***********************************************************************
*
* FUNCTION: ChunkFile::WriteChunk
*
* DESCRIPTION: Write the given data to the file. The data is marked
* with the given tag. No effort is made to find any
* existing chunks with the same tag and deleting them.
*
* PARAMETERS: tag - marker for the data being written.
*
* chunk - object containing the data to write.
*
* RETURNED: Nothing
*
***********************************************************************/
void ChunkFile::WriteChunk (Tag tag, const Chunk& chunk)
{
this->WriteChunk (tag, chunk.GetLength (), chunk.GetPointer ());
}
/***********************************************************************
*
* FUNCTION: ChunkFile::WriteInt
*
* DESCRIPTION: Write the given value to disk, marking it with the
* given tag.
*
* PARAMETERS: tag - marker used to later retrieve the value.
*
* val - integer to write to disk. All integers are
* stored in Big Endian format, and are written in
* their native size (1, 2, or 4 bytes).
*
* RETURNED: Nothing
*
***********************************************************************/
void ChunkFile::WriteInt (Tag tag, uae_u8 val)
{
Canonical (val);
ChunkFile::WriteChunk (tag, sizeof (val), &val);
}
void ChunkFile::WriteInt (Tag tag, uae_s8 val)
{
Canonical (val);
ChunkFile::WriteChunk (tag, sizeof (val), &val);
}
void ChunkFile::WriteInt (Tag tag, uae_u16 val)
{
Canonical (val);
ChunkFile::WriteChunk (tag, sizeof (val), &val);
}
void ChunkFile::WriteInt (Tag tag, uae_s16 val)
{
Canonical (val);
ChunkFile::WriteChunk (tag, sizeof (val), &val);
}
void ChunkFile::WriteInt (Tag tag, uae_u32 val)
{
Canonical (val);
ChunkFile::WriteChunk (tag, sizeof (val), &val);
}
void ChunkFile::WriteInt (Tag tag, uae_s32 val)
{
Canonical (val);
ChunkFile::WriteChunk (tag, sizeof (val), &val);
}
/***********************************************************************
*
* FUNCTION: ChunkFile::WriteString
*
* DESCRIPTION: Write the given string to disk, marking it with the
* given tag.
*
* PARAMETERS: tag - marker used to later retrieve the value.
*
* s - string to write to disk.
*
* RETURNED: Nothing
*
***********************************************************************/
void ChunkFile::WriteString (Tag tag, const char* s)
{
ChunkFile::WriteChunk (tag, strlen (s), s);
}
void ChunkFile::WriteString (Tag tag, const string& s)
{
ChunkFile::WriteChunk (tag, s.size (), s.c_str ());
}
/***********************************************************************
*
* FUNCTION: ChunkFile::ReadChunk
*
* DESCRIPTION: Read a block of bytes into the given buffer
*
* PARAMETERS: size - number of bytes to read.
*
* data - buffer to write the bytes to.
*
* RETURNED: Nothing (exceptions can be thrown).
*
***********************************************************************/
void ChunkFile::ReadChunk (uae_u32 size, void* data)
{
// Merely read the requested bytes starting at the current location.
fStream.Read (size, data);
}
/***********************************************************************
*
* FUNCTION: ChunkFile::WriteChunk
*
* DESCRIPTION: Write the given data to the file. The data is marked
* with the given tag. No effort is made to find any
* existing chunks with the same tag and deleting them.
*
* PARAMETERS: tag - marker for the data being written.
*
* size - number of bytes to write.
*
* data - pointer to buffer containing bytes to write.
*
* RETURNED: Nothing
*
***********************************************************************/
void ChunkFile::WriteChunk (Tag tag, uae_u32 size, const void* data)
{
// Write the 4-byte tag in Big Endian format.
Canonical (tag);
fStream.Write (sizeof (tag), &tag);
// Write the chunk size in Big Endian format. Return the size
// back to host format when done so that it can be used to write
// the actual data.
Canonical (size);
fStream.Write (sizeof (size), &size);
Canonical (size);
// Write the chunk data.
fStream.Write (size, data);
}
/***********************************************************************
*
* FUNCTION: ChunkFile::GetFile
*
* DESCRIPTION: .
*
* PARAMETERS: None.
*
* RETURNED: Nothing.
*
***********************************************************************/
StreamHandle& ChunkFile::GetStream (void) const
{
return fStream;
}
/***********************************************************************
*
* FUNCTION: Chunk::Chunk
*
* DESCRIPTION: Default constructor. Creates a "chunk" that initially
* has no memory.
*
* PARAMETERS: None
*
* RETURNED: Nothing
*
***********************************************************************/
Chunk::Chunk (void) :
fPtr (NULL),
fUsedSize (0),
fAllocatedSize (0)
{
}
/***********************************************************************
*
* FUNCTION: Chunk::Chunk
*
* DESCRIPTION: Parameterized constructor. Creates a "chunk" with a
* block of memory with the given length.
*
* PARAMETERS: inLength - number of bytes for the initial block of
* memory.
*
* RETURNED: Nothing
*
***********************************************************************/
Chunk::Chunk (long inLength) :
fPtr (NULL),
fUsedSize (0),
fAllocatedSize (0)
{
SetLength (inLength);
}
/***********************************************************************
*
* FUNCTION: Chunk::~Chunk
*
* DESCRIPTION: Destructor. Disposes of the owned block of memory.
*
* PARAMETERS: None
*
* RETURNED: Nothing
*
***********************************************************************/
Chunk::~Chunk (void)
{
Platform::DisposeMemory (fPtr);
}
/***********************************************************************
*
* FUNCTION: Chunk::GetPointer
*
* DESCRIPTION: Returns a pointer to the owned block of memory.
*
* PARAMETERS: None
*
* RETURNED: A pointer to the owned block of memory.
*
***********************************************************************/
void* Chunk::GetPointer (void) const
{
return fPtr;
}
/***********************************************************************
*
* FUNCTION: Chunk::GetLength
*
* DESCRIPTION: Returns the number of useable bytes in the block of
* memory. The actual number of allocated bytes may be
* larger, but shouldn't be used.
*
* PARAMETERS: None
*
* RETURNED: The number of usable bytes in the chunk.
*
***********************************************************************/
long Chunk::GetLength (void) const
{
return fUsedSize;
}
/***********************************************************************
*
* FUNCTION: Chunk::SetLength
*
* DESCRIPTION: Set the number of useable bytes in the owned block of
* memory. The number of bytes actually allocated may
* be greater than passed in. After this call, any
* previous pointers returned by GetPointer may be
* invalid.
*
* PARAMETERS: inLength - the number of useable bytes to make available.
*
* RETURNED: Nothing
*
***********************************************************************/
void Chunk::SetLength (long inLength)
{
if (inLength > fAllocatedSize)
{
const long kSlushFund = 100;
long newAllocatedLength = inLength + kSlushFund;
fPtr = Platform::ReallocMemory (fPtr, newAllocatedLength);
fAllocatedSize = newAllocatedLength;
}
fUsedSize = inLength;
}
// ===========================================================================
// ChunkStream.cpp 1993-1998 Metrowerks Inc. All rights reserved.
// ===========================================================================
//
// A Stream whose bytes are in a Chunk
// ---------------------------------------------------------------------------
// ChunkStream(Handle) Parameterized Constructor [public]
// ---------------------------------------------------------------------------
// Construct from an existing Chunk
ChunkStream::ChunkStream(Chunk& chunk) :
PPStream (),
fChunk (chunk)
{
PPStream::SetLength (fChunk.GetLength ());
}
// ---------------------------------------------------------------------------
// ~ChunkStream Destructor [public]
// ---------------------------------------------------------------------------
ChunkStream::~ChunkStream()
{
}
// ---------------------------------------------------------------------------
// SetLength [public]
// ---------------------------------------------------------------------------
// Set the length, in bytes, of the ChunkStream
void
ChunkStream::SetLength(
uae_s32 inLength)
{
fChunk.SetLength (inLength);
PPStream::SetLength (inLength);
}
// ---------------------------------------------------------------------------
// PutBytes
// ---------------------------------------------------------------------------
// Write bytes from a buffer to a ChunkStream
//
// Returns an error code and passes back the number of bytes actually
// written, which may be less than the number requested if an error occurred.
//
// Grows data Chunk if necessary.
ErrCode
ChunkStream::PutBytes(
const void *inBuffer,
uae_s32 ioByteCount)
{
ErrCode err = errNone;
uae_s32 endOfWrite = GetMarker () + ioByteCount;
if (endOfWrite > GetLength ()) // Need to grow Chunk
{
SetLength (endOfWrite);
}
// Copy bytes into Chunk
if (ioByteCount > 0)
{
memmove ((char*) fChunk.GetPointer () + GetMarker (), inBuffer, ioByteCount);
SetMarker (ioByteCount, kStreamFromMarker);
}
return err;
}
// ---------------------------------------------------------------------------
// GetBytes
// ---------------------------------------------------------------------------
// Read bytes from a ChunkStream to a buffer
//
// Returns an error code and passes back the number of bytes actually
// read, which may be less than the number requested if an error occurred.
//
// Errors:
// readErr Attempt to read past the end of the ChunkStream
ErrCode
ChunkStream::GetBytes(
void *outBuffer,
uae_s32 ioByteCount)
{
ErrCode err = errNone;
// Upper bound is number of bytes from
// marker to end
if ((GetMarker () + ioByteCount) > GetLength ())
{
ioByteCount = GetLength () - GetMarker ();
err = 1;
}
// Copy bytes from Handle into buffer
memmove (outBuffer, (char*) fChunk.GetPointer () + GetMarker (), ioByteCount);
SetMarker (ioByteCount, kStreamFromMarker);
return err;
}
|