aboutsummaryrefslogtreecommitdiff
path: root/SrcShared/ChunkFile.cpp
blob: 620d97dae952c5e6649c0476af958160c8d92c71 (plain)
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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
/* -*- mode: C++; tab-width: 4 -*- */
/* ===================================================================== *\
	Copyright (c) 1999-2001 Palm, Inc. or its subsidiaries.
	All rights reserved.

	This file is part of the Palm OS Emulator.

	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.
\* ===================================================================== */

#include "EmCommon.h"
#include "ChunkFile.h"

#include "Byteswapping.h"		// Canonical
#include "Platform.h"			// ReallocMemory


/***********************************************************************
 *
 * FUNCTION:	ChunkFile constructor
 *
 * DESCRIPTION:	Initialize the ChunkFile object.  Sets the fStream data
 *				member to refer to the given EmStream (which must
 *				exist for the life of the ChunkFile).
 *
 * PARAMETERS:	s - EmStream to utilize for read/write operations.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

ChunkFile::ChunkFile (EmStream& s) :
	fStream (s)
{
}


/***********************************************************************
 *
 * FUNCTION:	ChunkFile destructor
 *
 * DESCRIPTION:	Releases ChunkFile resources.  Currently does nothing.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

ChunkFile::~ChunkFile (void)
{
}


#pragma mark -

/***********************************************************************
 *
 * 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.SetMarker (fileOffset, kStreamFromStart);

	while (fileOffset < fileLength)
	{
		Tag		chunkTag;
		long	chunkLen;

		fStream.GetBytes (&chunkTag, sizeof (chunkTag));
		fStream.GetBytes (&chunkLen, sizeof (chunkLen));

		Canonical (chunkTag);
		Canonical (chunkLen);

		if (chunkTag == targetTag)
		{
			len = chunkLen;
			break;
		}

		fStream.SetMarker (chunkLen, kStreamFromMarker);
		fileOffset += sizeof (chunkTag) + sizeof (chunkLen) + chunkLen;
	}

	return len;
}


/***********************************************************************
 *
 * FUNCTION:	ChunkFile::ReadChunk
 *
 * 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.
 *
 ***********************************************************************/

Bool ChunkFile::ReadChunk (int index, Tag& tag, Chunk& chunk)
{
	long	fileOffset = 0;
	long	fileLength = fStream.GetLength ();

	fStream.SetMarker (fileOffset, kStreamFromStart);

	while (fileOffset < fileLength)
	{
		// Read the chunk's tag and length.

		Tag		chunkTag;
		long	chunkLen;

		fStream.GetBytes (&chunkTag, sizeof (chunkTag));
		fStream.GetBytes (&chunkLen, sizeof (chunkLen));

		Canonical (chunkTag);
		Canonical (chunkLen);

		// If this is the chunk we're looking for, read it in.

		if (index == 0)
		{
			tag = chunkTag;
			chunk.SetLength (chunkLen);
			this->ReadChunk (chunkLen, chunk.GetPointer ());

			// Return that we found the chunk.

			return true;
		}

		// Move to the next chunk.

		fStream.SetMarker (chunkLen, kStreamFromMarker);
		fileOffset += sizeof (chunkTag) + sizeof (chunkLen) + chunkLen;
		--index;
	}

	// Return that we didn't find the chunk.

	return false;
}


#pragma mark -

/***********************************************************************
 *
 * 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, uint8& val)
{
	if (ChunkFile::FindChunk (tag) == kChunkNotFound)
		return false;

	ChunkFile::ReadChunk (sizeof (val), &val);
	Canonical (val);

	return true;
}


Bool ChunkFile::ReadInt (Tag tag, int8& val)
{
	if (ChunkFile::FindChunk (tag) == kChunkNotFound)
		return false;

	ChunkFile::ReadChunk (sizeof (val), &val);
	Canonical (val);

	return true;
}


Bool ChunkFile::ReadInt (Tag tag, uint16& val)
{
	if (ChunkFile::FindChunk (tag) == kChunkNotFound)
		return false;

	ChunkFile::ReadChunk (sizeof (val), &val);
	Canonical (val);

	return true;
}


Bool ChunkFile::ReadInt (Tag tag, int16& val)
{
	if (ChunkFile::FindChunk (tag) == kChunkNotFound)
		return false;

	ChunkFile::ReadChunk (sizeof (val), &val);
	Canonical (val);

	return true;
}


Bool ChunkFile::ReadInt (Tag tag, uint32& val)
{
	if (ChunkFile::FindChunk (tag) == kChunkNotFound)
		return false;

	ChunkFile::ReadChunk (sizeof (val), &val);
	Canonical (val);

	return true;
}


Bool ChunkFile::ReadInt (Tag tag, int32& 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;
}


#pragma mark -

/***********************************************************************
 *
 * 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, uint8 val)
{
	Canonical (val);
	ChunkFile::WriteChunk (tag, sizeof (val), &val);
}


void ChunkFile::WriteInt (Tag tag, int8 val)
{
	Canonical (val);
	ChunkFile::WriteChunk (tag, sizeof (val), &val);
}


void ChunkFile::WriteInt (Tag tag, uint16 val)
{
	Canonical (val);
	ChunkFile::WriteChunk (tag, sizeof (val), &val);
}


void ChunkFile::WriteInt (Tag tag, int16 val)
{
	Canonical (val);
	ChunkFile::WriteChunk (tag, sizeof (val), &val);
}


void ChunkFile::WriteInt (Tag tag, uint32 val)
{
	Canonical (val);
	ChunkFile::WriteChunk (tag, sizeof (val), &val);
}


void ChunkFile::WriteInt (Tag tag, int32 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 ());
}


#pragma mark -

/***********************************************************************
 *
 * 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 (uint32 size, void* data)
{
	// Merely read the requested bytes starting at the current location.

	fStream.GetBytes (data, size);
}


/***********************************************************************
 *
 * 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, uint32 size, const void* data)
{
	// Write the 4-byte tag in Big Endian format.

	Canonical (tag);
	fStream.PutBytes (&tag, sizeof (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.PutBytes (&size, sizeof (size));
	Canonical (size);

	// Write the chunk data.

	fStream.PutBytes (data, size);
}


#pragma mark -

/***********************************************************************
 *
 * FUNCTION:	ChunkFile::GetFile
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

EmStream& ChunkFile::GetStream (void) const
{
	return fStream;
}


#pragma mark -

/***********************************************************************
 *
 * 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:	Copy constructor.  Creates a "chunk" containing a copy
 *				of the given data.
 *
 * PARAMETERS:	other - chunk containing the data to copy.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

Chunk::Chunk (const Chunk& other) :
	fPtr (NULL),
	fUsedSize (0),
	fAllocatedSize (0)
{
	this->SetLength (other.GetLength ());
	memcpy (this->GetPointer (), other.GetPointer (), other.GetLength ());
}


/***********************************************************************
 *
 * FUNCTION:	Chunk::~Chunk
 *
 * DESCRIPTION:	Destructor.  Disposes of the owned block of memory.
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

Chunk::~Chunk (void)
{
	Platform::DisposeMemory (fPtr);
}


/***********************************************************************
 *
 * FUNCTION:	Chunk::operator=
 *
 * DESCRIPTION:	Assignment operator.  Makes the controlled chunk
 *				contain a copy of the data managed by the other chunk.
 *
 * PARAMETERS:	other - chunk containing the data to copy.
 *
 * RETURNED:	Referenced to the controlled object.
 *
 ***********************************************************************/

Chunk& Chunk::operator= (const Chunk& other)
{
	if (this != &other)
	{
		this->SetLength (other.GetLength ());
		memcpy (this->GetPointer (), other.GetPointer (), other.GetLength ());
	}

	return *this;
}


/***********************************************************************
 *
 * 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;
	}

	// As a special case, allow the clean up of memory by setting the
	// length to zero.

	else if (inLength == 0)
	{
		Platform::DisposeMemory (fPtr);
		fAllocatedSize = 0;
	}

	fUsedSize = inLength;
}


// ===========================================================================
//	EmStreamChunk.cpp
// ===========================================================================
//
//	A Stream whose bytes are in a Chunk


// ---------------------------------------------------------------------------
//	¥ EmStreamChunk(Handle)					Parameterized Constructor [public]
// ---------------------------------------------------------------------------
//	Construct from an existing Chunk

EmStreamChunk::EmStreamChunk(Chunk* chunk) :
	EmStream (),
	fChunk (*chunk),
	fOwnedChunk (chunk)
{
	EmStream::SetLength (fChunk.GetLength ());
}


EmStreamChunk::EmStreamChunk(Chunk& chunk) :
	EmStream (),
	fChunk (chunk),
	fOwnedChunk (NULL)
{
	EmStream::SetLength (fChunk.GetLength ());
}


// ---------------------------------------------------------------------------
//	¥ ~EmStreamChunk						Destructor				  [public]
// ---------------------------------------------------------------------------

EmStreamChunk::~EmStreamChunk()
{
	delete fOwnedChunk;
}


// ---------------------------------------------------------------------------
//	¥ SetLength														  [public]
// ---------------------------------------------------------------------------
//	Set the length, in bytes, of the EmStreamChunk

void
EmStreamChunk::SetLength(
	int32	inLength)
{
	fChunk.SetLength (inLength);
	EmStream::SetLength (inLength);
}


// ---------------------------------------------------------------------------
//	¥ PutBytes
// ---------------------------------------------------------------------------
//	Write bytes from a buffer to a EmStreamChunk
//
//	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
EmStreamChunk::PutBytes(
	const void	*inBuffer,
	int32		ioByteCount)
{
	ErrCode	err = errNone;
	int32	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 EmStreamChunk 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 EmStreamChunk

ErrCode
EmStreamChunk::GetBytes(
	void	*outBuffer,
	int32	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;
}