File: SessionFile.cpp

package info (click to toggle)
pose 3.0a3-3
  • links: PTS
  • area: contrib
  • in suites: potato
  • size: 15,500 kB
  • ctags: 20,548
  • sloc: ansic: 72,579; cpp: 50,198; perl: 1,336; python: 1,242; sh: 363; makefile: 290
file content (869 lines) | stat: -rw-r--r-- 22,317 bytes parent folder | download
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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
/* -*- mode: C++; tab-width: 4 -*- */
/* ================================================================================== */
/* Copyright (c) 1998-1999 3Com Corporation or its subsidiaries. All rights reserved. */
/* ================================================================================== */

#include "EmulatorCommon.h"
#include "SessionFile.h"

#include "Byteswapping.h"		// Canonical
#include "Miscellaneous.h"		// StMemory, RunLengthEncode, GzipEncode, etc.


/***********************************************************************
 *
 * FUNCTION:	SessionFile constructor
 *
 * DESCRIPTION:	Initialize the SessionFile object.  Sets the fFile data
 *				member to refer to the given FileHandle (which must
 *				exist for the life of the SessionFile).
 *
 * PARAMETERS:	None
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

SessionFile::SessionFile (ChunkFile& f) :
	fFile (f),
	fCanReload (false),
	fCfg (),
	fReadBugFixes (false),
	fChangedBugFixes (false),
	fBugFixes (0)
{
}


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

SessionFile::~SessionFile (void)
{
	if (fChangedBugFixes)
	{
		this->WriteBugFixes (fBugFixes);
	}
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadROMFileReference
 *
 * DESCRIPTION:	Read a reference to a paired ROM file from the session
 *				file.  For robustness, several approaches are used to
 *				find a ROM file.  Because these approaches are platform-
 *				specific, most of the work is off-loaded to the Platform
 *				sub-system.  If the file reference can be read, it's
 *				recorded in the configuration record to be used the next
 *				time a new session is created.
 *
 * PARAMETERS:	f - reference to FileReference to receive the read value.
 *
 * RETURNED:	True if a value could be found, false otherwise.
 *
 ***********************************************************************/

Bool SessionFile::ReadROMFileReference (FileReference& f)
{
	// Look for a ROM reference using a platform-specific method.

	Bool	result = Platform::ReadROMFileReference (fFile, f);

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadRAMImage
 *
 * DESCRIPTION:	Read the RAM image from the session file.  Attempts to
 *				read a number of versions of the image, including
 *				compressed and uncompressed versions.  If the image can
 *				be read, its size is recorded in the configuration
 *				record to be used the next time a new session is created.
 *
 * PARAMETERS:	image - reference to the pointer to receive the address
 *					of the RAM image.
 *
 *				size - reference to the integer to receive the size of
 *					the RAM image.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadRAMImage (void* image)
{
	long	result = this->ReadChunk (kRAMDataTag, image, kGzipCompression);

	if (result == ChunkFile::kChunkNotFound)
		result = this->ReadChunk (kRLERAMDataTag, image, kRLECompression);

	if (result == ChunkFile::kChunkNotFound)
		result = this->ReadChunk (kUncompRAMDataTag, image, kNoCompression);

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadMetaRAMImage
 *
 * DESCRIPTION:	Read the MetaRAM image from the session file.  Attempts
 *				to read a number of versions of the image, including
 *				compressed and uncompressed versions.
 *
 * PARAMETERS:	image - reference to the pointer to receive the address
 *					of the MetaRAM image.
 *
 *				size - reference to the integer to receive the size of
 *					the MetaRAM image.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadMetaRAMImage (void* image)
{
	long	result = this->ReadChunk (kMetaRAMDataTag, image, kGzipCompression);

	if (result == ChunkFile::kChunkNotFound)
		result = this->ReadChunk (kRLEMetaRAMDataTag, image, kRLECompression);

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadMetaROMImage
 *
 * DESCRIPTION:	Read the MetaROM image from the session file.  Attempts
 *				to read a number of versions of the image, including
 *				compressed and uncompressed versions.
 *
 * PARAMETERS:	image - reference to the pointer to receive the address
 *					of the MetaROM image.
 *
 *				size - reference to the integer to receive the size of
 *					the MetaROM image.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadMetaROMImage (void* image)
{
	long	result = this->ReadChunk (kMetaROMDataTag, image, kGzipCompression);

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadHwrDBallType
 *
 * DESCRIPTION:	Read the Dragonball hardware registers from the
 *				session file.
 *
 * PARAMETERS:	hwRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadHwrDBallType (HwrDBallType& hwRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kDBRegs, chunk))
	{
		memcpy (&hwRegs, chunk.GetPointer (), sizeof (hwRegs));
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadHwrDBallEZType
 *
 * DESCRIPTION:	Read the DragonballEZ hardware registers from the
 *				session file.
 *
 * PARAMETERS:	hwRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadHwrDBallEZType (HwrDBallEZType& hwRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kDBEZRegs, chunk))
	{
		memcpy (&hwRegs, chunk.GetPointer (), sizeof (hwRegs));
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadDBallRegs
 *
 * DESCRIPTION:	Read the Dragonball CPU registers structure from the
 *				session file.
 *
 * PARAMETERS:	cpuRegs - reference to the struct to receive the register
 *					data.
 *
 * RETURNED:	True if the data was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadDBallRegs (regstruct& cpuRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kCPURegs, chunk))
	{
		memcpy (&cpuRegs, chunk.GetPointer (), sizeof (cpuRegs));
		return true;
	}

	return false;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadDeviceType
 *
 * DESCRIPTION:	Read the id of the device to emulate.  If the id can be
 *				read, it's recorded in the configuration record to be
 *				used the next time a new session is created.
 *
 * PARAMETERS:	v - reference to the DeviceType to receive the read value.
 *
 * RETURNED:	True if the value was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadDeviceType (DeviceType& v)
{
	uae_u32	type;
	Bool	result = fFile.ReadInt (kDeviceType, type);

	if (result)
	{
		v = (DeviceType) type;
	}

	return result;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadBugFixes
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	v - reference to the BugFixes to receive the read value.
 *
 * RETURNED:	True if the value was found and could be read in.
 *
 ***********************************************************************/

Bool SessionFile::ReadBugFixes (BugFixes& v)
{
	uae_u32	bits;
	Bool	result = fFile.ReadInt (kBugsTag, bits);

	if (result)
	{
		v = (BugFixes) bits;
	}

	return result;
}


Bool SessionFile::ReadSED1375RegsType (SED1375RegsType& sedRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kSED1375Regs, chunk))
	{
		memcpy (&sedRegs, chunk.GetPointer (), sizeof (sedRegs));
		return true;
	}

	return false;
}

Bool SessionFile::ReadSED1375Image (void* image)
{
	long	result = this->ReadChunk (kSED1375Image, image, kGzipCompression);

	return result;
}

Bool SessionFile::ReadSED1375MetaImage (void* image)
{
	long	result = this->ReadChunk (kSED1375MetaImage, image, kGzipCompression);

	return result;
}

Bool SessionFile::ReadSED1375Palette (uae_u16 palette[256])
{
	Chunk	chunk;
	if (fFile.ReadChunk (kSED1375Palette, chunk))
	{
		// Note: "sizeof (palette)" gives 4, not 512.
		int	size = 256 * sizeof (palette[0]);
		memcpy (palette, chunk.GetPointer (), size);
		return true;
	}

	return false;
}


Bool SessionFile::ReadPLDRegsType (HwrJerryPLDType& pldRegs)
{
	Chunk	chunk;
	if (fFile.ReadChunk (kPLDRegs, chunk))
	{
		memcpy (&pldRegs, chunk.GetPointer (), sizeof (pldRegs));
		return true;
	}

	return false;
}


Bool SessionFile::ReadConfiguration (Configuration& cfg)
{
	if (!this->ReadDeviceType (cfg.fDeviceType))
		return false;

	if (!Platform::ROMResourcePresent() && !this->ReadROMFileReference (cfg.fROMFile))
		return false;

	cfg.fRAMSize = this->GetRAMImageSize ();
	if (cfg.fRAMSize == ChunkFile::kChunkNotFound)
		return false;

	cfg.fRAMSize /= 1024;

	return true;
}


long SessionFile::GetRAMImageSize (void)
{
	long	numBytes;

	Chunk	chunk;
	if (fFile.ReadChunk (kRAMDataTag, chunk) || fFile.ReadChunk (kRLERAMDataTag, chunk))
	{
		ChunkStream	s (chunk);
		s >> numBytes;
	}
	else
	{
		numBytes = fFile.FindChunk (kUncompRAMDataTag);
	}

	return numBytes;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteROMFileReference
 *
 * DESCRIPTION:	Write a reference to the ROM file to use for this
 *				session.  For robustness, the reference is written out
 *				in several different ways.  Because the way the file is
 *				later looked up is platform-dependent, most of the work
 *				is off-loaded to the Platform sub-system.
 *
 * PARAMETERS:	f - reference to the FileReference for the ROM file.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteROMFileReference (const FileReference& f)
{
	// Save a ROM reference using a platform-specific method.

	Platform::WriteROMFileReference (fFile, f);

	// Save the name of the ROM file.

	string	romFileName = f.GetFileName ();
	fFile.WriteString (SessionFile::kROMNameTag, romFileName);

	// Remember that we were using this ROM file most recently.

	fCfg.fROMFile = f;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteRAMImage
 *
 * DESCRIPTION:	Write the given data as the RAM image for the session
 *				file.  The data is written using the default compression.
 *
 * PARAMETERS:	image - pointer to the data to be written.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 *				size - number of bytes in the image.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteRAMImage (const void* image, uae_u32 size)
{
	this->WriteChunk (kRAMDataTag, size, image, kGzipCompression);
	fCfg.fRAMSize = size / 1024;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteMetaRAMImage
 *
 * DESCRIPTION:	Write the given data as the MetaRAM image for the session
 *				file.  The data is written using the default compression.
 *
 * PARAMETERS:	image - pointer to the data to be written.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 *				size - number of bytes in the image.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteMetaRAMImage (const void* image, uae_u32 size)
{
	this->WriteChunk (kMetaRAMDataTag, size, image, kGzipCompression);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteMetaROMImage
 *
 * DESCRIPTION:	Write the given data as the MetaROM image for the session
 *				file.  The data is written using the default compression.
 *
 * PARAMETERS:	image - pointer to the data to be written.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 *				size - number of bytes in the image.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteMetaROMImage (const void* image, uae_u32 size)
{
	this->WriteChunk (kMetaROMDataTag, size, image, kGzipCompression);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteHwrDBallType
 *
 * DESCRIPTION:	Write the Dragonball hardware registers to the session
 *				file.
 *
 * PARAMETERS:	hwRegs - the DB registers to write to disk.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteHwrDBallType (const HwrDBallType& hwRegs)
{
	fFile.WriteChunk (kDBRegs, sizeof (hwRegs), &hwRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteHwrDBallEZType
 *
 * DESCRIPTION:	Write the Dragonball EZ hardware registers to the session
 *				file.
 *
 * PARAMETERS:	hwRegs - the DB registers to write to disk.  No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteHwrDBallEZType (const HwrDBallEZType& hwRegs)
{
	fFile.WriteChunk (kDBEZRegs, sizeof (hwRegs), &hwRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteDBallRegs
 *
 * DESCRIPTION:	Write the Dragonball CPU registers to the session file.
 *
 * PARAMETERS:	cpuRegs - the registers to write to disk. No munging
 *					of this data is performed; it is expected that any
 *					byteswapping has already taken place.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteDBallRegs (const regstruct& cpuRegs)
{
	fFile.WriteChunk (kCPURegs, sizeof (cpuRegs), &cpuRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteDeviceType
 *
 * DESCRIPTION:	Write the id of the device to emulate.
 *
 * PARAMETERS:	v - id of the device to emulate.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteDeviceType (const DeviceType& v)
{
	fFile.WriteInt (kDeviceType, (uae_u32) v);

	// Remember that we were using this device most recently.

	fCfg.fDeviceType = v;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteBugFixes
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	v - flags of fixed bugs.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteBugFixes (const BugFixes& v)
{
	fFile.WriteInt (kBugsTag, (uae_u32) v);
}


void SessionFile::WriteSED1375RegsType (const SED1375RegsType& sedRegs)
{
	fFile.WriteChunk (kSED1375Regs, sizeof (sedRegs), &sedRegs);
}

void SessionFile::WriteSED1375Image (const void* image, uae_u32 size)
{
	this->WriteChunk (kSED1375Image, size, image, kGzipCompression);
}

void SessionFile::WriteSED1375MetaImage (const void* image, uae_u32 size)
{
	this->WriteChunk (kSED1375MetaImage, size, image, kGzipCompression);
}

void SessionFile::WriteSED1375Palette (const uae_u16 palette[256])
{
	// Note: "sizeof (palette)" gives 4, not 512.
	int	size = 256 * sizeof (palette[0]);
	fFile.WriteChunk (kSED1375Palette, size, palette);
}

void SessionFile::WritePLDRegsType (const HwrJerryPLDType& pldRegs)
{
	fFile.WriteChunk (kPLDRegs, sizeof (pldRegs), &pldRegs);
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::SetCanReload
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void SessionFile::SetCanReload (Bool val)
{
	fCanReload = val;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::GetCanReload
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

Bool SessionFile::GetCanReload (void)
{
	return fCanReload;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::FixBug
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

void SessionFile::FixBug (BugFix val)
{
	fBugFixes |= val;
	fChangedBugFixes = true;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::IncludesBugFix
 *
 * DESCRIPTION:	.
 *
 * PARAMETERS:	None.
 *
 * RETURNED:	Nothing.
 *
 ***********************************************************************/

Bool SessionFile::IncludesBugFix (BugFix val)
{
	if (!fReadBugFixes)
	{
		this->ReadBugFixes (fBugFixes);
		fReadBugFixes = true;
	}

	return (fBugFixes & val) != 0;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::ReadChunk
 *
 * DESCRIPTION:	Read an arbitrary chunk of data, utilizing the given
 *				type of compression, returning a pointer to the read
 *				data and the size of the data.
 *
 * PARAMETERS:	tag - marker identifying the data to be read.
 *
 *				size - reference to the integer to receive the number
 *					of bytes in the data.  This size may not be the same
 *					as the size of the chunk if the chunk is compressed.
 *
 *				image - reference to the pointer to receive the address
 *					of the read data.
 *
 *				compType - type of compression used for this chunk.  The
 *					caller is expected to know what kind of compression
 *					is being used; the compression type is not stored
 *					along with the chunk.
 *
 * RETURNED:	True if the image was found and could be read in.
 *
 ***********************************************************************/

long SessionFile::ReadChunk (ChunkFile::Tag tag, void* image,
							 CompressionType compType)
{
	// Get the size of the chunk.

	long	chunkSize = fFile.FindChunk (tag);
	if (chunkSize == ChunkFile::kChunkNotFound)
	{
		return chunkSize;
	}

	if (chunkSize)
	{
		// If no compression is being used, just read the data.

		if (compType == kNoCompression)
		{
			fFile.ReadChunk (chunkSize, image);
		}

		// The data is compressed.

		else
		{
			// Use the chunk size to create a buffer.

			StMemory	packedImage (chunkSize);

			// Read the chunk into memory.

			fFile.ReadChunk (chunkSize, packedImage.Get ());

			// The size of the unpacked image is stored as the first 4 bytes
			// of the chunk.

			long		unpackedSize = *(long*) packedImage.Get ();
			Canonical (unpackedSize);

			// Get pointers to the source (packed) data and
			// destination (unpacked) data.

			void*	src = packedImage.Get () + sizeof (long);
			void*	dest = image;

			// Decompress the data into the dest buffer.

			if (compType == kGzipCompression)
				::GzipDecode (&src, &dest, chunkSize - sizeof (long), unpackedSize);
			else
				::RunLengthDecode (&src, &dest, chunkSize - sizeof (long), unpackedSize);
		}
	}
	
	return true;
}


/***********************************************************************
 *
 * FUNCTION:	SessionFile::WriteChunk
 *
 * DESCRIPTION:	Write an arbitrary chunk of data, utilizing the given
 *				type of compression.
 *
 * PARAMETERS:	tag - marker used to later retrieve the data.
 *
 *				size - number of bytes in the image to write.
 *
 *				image - pointer to the image to write.
 *
 *				compType - type of compression to use.
 *
 * RETURNED:	Nothing
 *
 ***********************************************************************/

void SessionFile::WriteChunk (ChunkFile::Tag tag, uae_u32 size,
				const void* image, CompressionType compType)
{
	// No compression to be used; just write the data out as-is.

	if (compType == kNoCompression)
	{
		fFile.WriteChunk (tag, size, image);
	}

	// Use some form of compression.

	else
	{
		// Get the worst-case size for the compressed data.

		long		worstPackedSize = sizeof (long) +
						((compType == kGzipCompression)
							? ::GzipWorstSize (size)
							: ::RunLengthWorstSize (size));

		// Create a new buffer to hold the compressed data.

		StMemory	packedImage (worstPackedSize);

		// Write out the uncompressed size of the data as the first
		// 4 bytes of the chunk.

		Canonical (size);
		*(long*) packedImage.Get () = size;
		Canonical (size);

		// Get pointers to the source (unpacked) data and
		// destination (packed) data.

		void*	src = (void*) image;
		void*	dest = packedImage.Get () + sizeof (long);

		// Compress the data.

		if (compType == kGzipCompression)
			::GzipEncode (&src, &dest, size, worstPackedSize);
		else
			::RunLengthEncode (&src, &dest, size, worstPackedSize);

		// Calculate the compressed size of the data.

		long	packedSize = (char*) dest - (char*) packedImage;

		// Write the compressed data to the file.

		fFile.WriteChunk (tag, packedSize, packedImage.Get ());
	}
}