-
Notifications
You must be signed in to change notification settings - Fork 2k
/
chd.cpp
3514 lines (3091 loc) · 106 KB
/
chd.cpp
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
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// license:BSD-3-Clause
// copyright-holders:Aaron Giles
/***************************************************************************
MAME Compressed Hunks of Data file format
***************************************************************************/
#include "chd.h"
#include "avhuff.h"
#include "cdrom.h"
#include "corefile.h"
#include "coretmpl.h"
#include "flac.h"
#include "hashing.h"
#include "multibyte.h"
#include "eminline.h"
#include <zlib.h>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <ctime>
#include <new>
#include <tuple>
//**************************************************************************
// CONSTANTS
//**************************************************************************
// standard metadata formats
const char *HARD_DISK_METADATA_FORMAT = "CYLS:%d,HEADS:%d,SECS:%d,BPS:%d";
const char *CDROM_TRACK_METADATA_FORMAT = "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d";
const char *CDROM_TRACK_METADATA2_FORMAT = "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d PREGAP:%d PGTYPE:%s PGSUB:%s POSTGAP:%d";
const char *GDROM_TRACK_METADATA_FORMAT = "TRACK:%d TYPE:%s SUBTYPE:%s FRAMES:%d PAD:%d PREGAP:%d PGTYPE:%s PGSUB:%s POSTGAP:%d";
const char *AV_METADATA_FORMAT = "FPS:%d.%06d WIDTH:%d HEIGHT:%d INTERLACED:%d CHANNELS:%d SAMPLERATE:%d";
static const uint32_t METADATA_HEADER_SIZE = 16; // metadata header size
static const uint8_t V34_MAP_ENTRY_FLAG_TYPE_MASK = 0x0f; // what type of hunk
static const uint8_t V34_MAP_ENTRY_FLAG_NO_CRC = 0x10; // no CRC is present
// V3-V4 entry types
enum
{
V34_MAP_ENTRY_TYPE_INVALID = 0, // invalid type
V34_MAP_ENTRY_TYPE_COMPRESSED = 1, // standard compression
V34_MAP_ENTRY_TYPE_UNCOMPRESSED = 2, // uncompressed data
V34_MAP_ENTRY_TYPE_MINI = 3, // mini: use offset as raw data
V34_MAP_ENTRY_TYPE_SELF_HUNK = 4, // same as another hunk in this file
V34_MAP_ENTRY_TYPE_PARENT_HUNK = 5, // same as a hunk in the parent file
V34_MAP_ENTRY_TYPE_2ND_COMPRESSED = 6 // compressed with secondary algorithm (usually FLAC CDDA)
};
// V5 compression types
enum
{
///< codec #0
// these types are live when running
COMPRESSION_TYPE_0 = 0,
///< codec #1
COMPRESSION_TYPE_1 = 1,
///< codec #2
COMPRESSION_TYPE_2 = 2,
///< codec #3
COMPRESSION_TYPE_3 = 3,
///< no compression; implicit length = hunkbytes
COMPRESSION_NONE = 4,
///< same as another block in this chd
COMPRESSION_SELF = 5,
///< same as a hunk's worth of units in the parent chd
COMPRESSION_PARENT = 6,
///< start of small RLE run (4-bit length)
// these additional pseudo-types are used for compressed encodings:
COMPRESSION_RLE_SMALL,
///< start of large RLE run (8-bit length)
COMPRESSION_RLE_LARGE,
///< same as the last COMPRESSION_SELF block
COMPRESSION_SELF_0,
///< same as the last COMPRESSION_SELF block + 1
COMPRESSION_SELF_1,
///< same block in the parent
COMPRESSION_PARENT_SELF,
///< same as the last COMPRESSION_PARENT block
COMPRESSION_PARENT_0,
///< same as the last COMPRESSION_PARENT block + 1
COMPRESSION_PARENT_1
};
//**************************************************************************
// TYPE DEFINITIONS
//**************************************************************************
// ======================> metadata_entry
// description of where a metadata entry lives within the file
struct chd_file::metadata_entry
{
uint64_t offset; // offset within the file of the header
uint64_t next; // offset within the file of the next header
uint64_t prev; // offset within the file of the previous header
uint32_t length; // length of the metadata
uint32_t metatag; // metadata tag
uint8_t flags; // flag bits
};
// ======================> metadata_hash
struct chd_file::metadata_hash
{
uint8_t tag[4]; // tag of the metadata in big-endian
util::sha1_t sha1; // hash data
};
//**************************************************************************
// INLINE FUNCTIONS
//**************************************************************************
//-------------------------------------------------
// be_read_sha1 - fetch a sha1_t from a data
// stream in bigendian order
//-------------------------------------------------
inline util::sha1_t chd_file::be_read_sha1(const uint8_t *base) const noexcept
{
util::sha1_t result;
memcpy(&result.m_raw[0], base, sizeof(result.m_raw));
return result;
}
//-------------------------------------------------
// be_write_sha1 - write a sha1_t to a data
// stream in bigendian order
//-------------------------------------------------
inline void chd_file::be_write_sha1(uint8_t *base, util::sha1_t value) noexcept
{
memcpy(base, &value.m_raw[0], sizeof(value.m_raw));
}
//-------------------------------------------------
// file_read - read from the file at the given
// offset.
//-------------------------------------------------
inline std::error_condition chd_file::file_read(uint64_t offset, void *dest, uint32_t length) const noexcept
{
// no file = failure
if (UNEXPECTED(!m_file))
return std::error_condition(error::NOT_OPEN);
// seek and read
std::error_condition err;
err = m_file->seek(offset, SEEK_SET);
if (UNEXPECTED(err))
return err;
size_t count;
std::tie(err, count) = read(*m_file, dest, length);
if (UNEXPECTED(!err && (count != length)))
return std::error_condition(std::errc::io_error); // TODO: revisit this error code (happens if file is truncated)
return err;
}
//-------------------------------------------------
// file_write - write to the file at the given
// offset.
//-------------------------------------------------
inline std::error_condition chd_file::file_write(uint64_t offset, const void *source, uint32_t length) noexcept
{
// no file = failure
if (UNEXPECTED(!m_file))
return std::error_condition(error::NOT_OPEN);
// seek and write
std::error_condition err;
err = m_file->seek(offset, SEEK_SET);
if (UNEXPECTED(err))
return err;
return write(*m_file, source, length).first;
}
//-------------------------------------------------
// file_append - append to the file at the given
// offset, ensuring we start at the given
// alignment; on failure throw an error
//-------------------------------------------------
inline uint64_t chd_file::file_append(const void *source, uint32_t length, uint32_t alignment)
{
// no file = failure
if (UNEXPECTED(!m_file))
throw std::error_condition(error::NOT_OPEN);
// seek to the end and align if necessary
std::error_condition err;
err = m_file->seek(0, SEEK_END);
if (UNEXPECTED(err))
throw err;
if (alignment != 0)
{
uint64_t offset;
err = m_file->tell(offset);
if (UNEXPECTED(err))
throw err;
uint32_t delta = offset % alignment;
if (delta != 0)
{
// pad with 0's from a local buffer
uint8_t buffer[1024];
memset(buffer, 0, sizeof(buffer));
delta = alignment - delta;
while (delta != 0)
{
uint32_t bytes_to_write = std::min<std::size_t>(sizeof(buffer), delta);
size_t count;
std::tie(err, count) = write(*m_file, buffer, bytes_to_write);
if (UNEXPECTED(err))
throw err;
delta -= count;
}
}
}
// write the real data
uint64_t offset;
err = m_file->tell(offset);
if (UNEXPECTED(err))
throw err;
std::tie(err, std::ignore) = write(*m_file, source, length);
if (UNEXPECTED(err))
throw err;
return offset;
}
//-------------------------------------------------
// bits_for_value - return the number of bits
// necessary to represent all numbers 0..value
//-------------------------------------------------
inline uint8_t chd_file::bits_for_value(uint64_t value) noexcept
{
uint8_t result = 0;
while (value != 0)
{
value >>= 1;
result++;
}
return result;
}
//**************************************************************************
// CHD FILE MANAGEMENT
//**************************************************************************
/**
* @fn chd_file::chd_file()
*
* @brief -------------------------------------------------
* chd_file - constructor
* -------------------------------------------------.
*/
chd_file::chd_file()
{
// reset state
close();
}
/**
* @fn chd_file::~chd_file()
*
* @brief -------------------------------------------------
* ~chd_file - destructor
* -------------------------------------------------.
*/
chd_file::~chd_file()
{
// close any open files
close();
}
/**
* @fn util::random_read chd_file::file()
*
* @brief -------------------------------------------------
* file - return our underlying file
* -------------------------------------------------.
*
* @return A random_read.
*/
util::random_read &chd_file::file()
{
assert(m_file);
return *m_file;
}
bool chd_file::parent_missing() const noexcept
{
if (m_parent_missing)
return true;
else if (!m_parent)
return false;
else
return m_parent->parent_missing();
}
/**
* @fn util::sha1_t chd_file::sha1()
*
* @brief -------------------------------------------------
* sha1 - return our SHA1 value
* -------------------------------------------------.
*
* @return A sha1_t.
*/
util::sha1_t chd_file::sha1() const noexcept
{
// read the big-endian version
uint8_t rawbuf[sizeof(util::sha1_t)];
std::error_condition err = file_read(m_sha1_offset, rawbuf, sizeof(rawbuf));
if (UNEXPECTED(err))
return util::sha1_t::null; // on failure, return null
return be_read_sha1(rawbuf);
}
/**
* @fn util::sha1_t chd_file::raw_sha1()
*
* @brief -------------------------------------------------
* raw_sha1 - return our raw SHA1 value
* -------------------------------------------------.
*
* @exception CHDERR_UNSUPPORTED_VERSION Thrown when a chderr unsupported version error
* condition occurs.
*
* @return A sha1_t.
*/
util::sha1_t chd_file::raw_sha1() const noexcept
{
try
{
// determine offset within the file for data-only
if (UNEXPECTED(!m_rawsha1_offset))
throw std::error_condition(error::UNSUPPORTED_VERSION);
// read the big-endian version
uint8_t rawbuf[sizeof(util::sha1_t)];
std::error_condition err = file_read(m_rawsha1_offset, rawbuf, sizeof(rawbuf));
if (UNEXPECTED(err))
throw err;
return be_read_sha1(rawbuf);
}
catch (std::error_condition const &)
{
// on failure, return null
return util::sha1_t::null;
}
}
/**
* @fn util::sha1_t chd_file::parent_sha1()
*
* @brief -------------------------------------------------
* parent_sha1 - return our parent's SHA1 value
* -------------------------------------------------.
*
* @exception CHDERR_UNSUPPORTED_VERSION Thrown when a chderr unsupported version error
* condition occurs.
*
* @return A sha1_t.
*/
util::sha1_t chd_file::parent_sha1() const noexcept
{
try
{
// determine offset within the file
if (UNEXPECTED(!m_parentsha1_offset))
throw std::error_condition(error::UNSUPPORTED_VERSION);
// read the big-endian version
uint8_t rawbuf[sizeof(util::sha1_t)];
std::error_condition err = file_read(m_parentsha1_offset, rawbuf, sizeof(rawbuf));
if (UNEXPECTED(err))
throw err;
return be_read_sha1(rawbuf);
}
catch (std::error_condition const &)
{
// on failure, return nullptr
return util::sha1_t::null;
}
}
/**
* @fn std::error_condition chd_file::hunk_info(uint32_t hunknum, chd_codec_type &compressor, uint32_t &compbytes)
*
* @brief -------------------------------------------------
* hunk_info - return information about this hunk
* -------------------------------------------------.
*
* @param hunknum The hunknum.
* @param [in,out] compressor The compressor.
* @param [in,out] compbytes The compbytes.
*
* @return A std::error_condition.
*/
std::error_condition chd_file::hunk_info(uint32_t hunknum, chd_codec_type &compressor, uint32_t &compbytes)
{
// error if invalid
if (hunknum >= m_hunkcount)
return std::error_condition(error::HUNK_OUT_OF_RANGE);
// get the map pointer
switch (m_version)
{
// v3/v4 map entries
case 3:
case 4:
{
uint8_t const *const rawmap = &m_rawmap[16 * hunknum];
switch (rawmap[15] & V34_MAP_ENTRY_FLAG_TYPE_MASK)
{
case V34_MAP_ENTRY_TYPE_COMPRESSED:
compressor = CHD_CODEC_ZLIB;
compbytes = get_u16be(&rawmap[12]) + (rawmap[14] << 16);
break;
case V34_MAP_ENTRY_TYPE_UNCOMPRESSED:
compressor = CHD_CODEC_NONE;
compbytes = m_hunkbytes;
break;
case V34_MAP_ENTRY_TYPE_MINI:
compressor = CHD_CODEC_MINI;
compbytes = 0;
break;
case V34_MAP_ENTRY_TYPE_SELF_HUNK:
compressor = CHD_CODEC_SELF;
compbytes = 0;
break;
case V34_MAP_ENTRY_TYPE_PARENT_HUNK:
compressor = CHD_CODEC_PARENT;
compbytes = 0;
break;
}
}
break;
// v5 map entries
case 5:
{
uint8_t const *const rawmap = &m_rawmap[m_mapentrybytes * hunknum];
if (!compressed())
{
// uncompressed case
if (get_u32be(&rawmap[0]) == 0)
{
compressor = CHD_CODEC_PARENT;
compbytes = 0;
}
else
{
compressor = CHD_CODEC_NONE;
compbytes = m_hunkbytes;
}
}
else
{
// compressed case
switch (rawmap[0])
{
case COMPRESSION_TYPE_0:
case COMPRESSION_TYPE_1:
case COMPRESSION_TYPE_2:
case COMPRESSION_TYPE_3:
compressor = m_compression[rawmap[0]];
compbytes = get_u24be(&rawmap[1]);
break;
case COMPRESSION_NONE:
compressor = CHD_CODEC_NONE;
compbytes = m_hunkbytes;
break;
case COMPRESSION_SELF:
compressor = CHD_CODEC_SELF;
compbytes = 0;
break;
case COMPRESSION_PARENT:
compressor = CHD_CODEC_PARENT;
compbytes = 0;
break;
default:
return error::UNKNOWN_COMPRESSION;
}
}
}
break;
}
return std::error_condition();
}
/**
* @fn void chd_file::set_raw_sha1(sha1_t rawdata)
*
* @brief -------------------------------------------------
* set_raw_sha1 - set our SHA1 values
* -------------------------------------------------.
*
* @param rawdata The rawdata.
*/
std::error_condition chd_file::set_raw_sha1(util::sha1_t rawdata) noexcept
{
uint64_t const offset = (m_rawsha1_offset != 0) ? m_rawsha1_offset : m_sha1_offset;
assert(offset != 0);
// create a big-endian version
uint8_t rawbuf[sizeof(util::sha1_t)];
be_write_sha1(rawbuf, rawdata);
// write to the header
std::error_condition err = file_write(offset, rawbuf, sizeof(rawbuf));
if (UNEXPECTED(err))
return err;
try
{
// if we have a separate rawsha1_offset, update the full sha1 as well
if (m_rawsha1_offset != 0)
metadata_update_hash();
}
catch (std::error_condition const &err)
{
return err;
}
catch (std::bad_alloc const &)
{
return std::errc::not_enough_memory;
}
return std::error_condition();
}
/**
* @fn void chd_file::set_parent_sha1(sha1_t parent)
*
* @brief -------------------------------------------------
* set_parent_sha1 - set the parent SHA1 value
* -------------------------------------------------.
*
* @exception CHDERR_INVALID_FILE Thrown when a chderr invalid file error condition occurs.
*
* @param parent The parent.
*/
std::error_condition chd_file::set_parent_sha1(util::sha1_t parent) noexcept
{
// if no file, fail
if (UNEXPECTED(!m_file))
return std::error_condition(error::INVALID_FILE);
assert(m_parentsha1_offset != 0);
// create a big-endian version
uint8_t rawbuf[sizeof(util::sha1_t)];
be_write_sha1(rawbuf, parent);
// write to the header
return file_write(m_parentsha1_offset, rawbuf, sizeof(rawbuf));
}
/**
* @fn std::error_condition chd_file::create(util::random_read_write::ptr &&file, uint64_t logicalbytes, uint32_t hunkbytes, uint32_t unitbytes, const chd_codec_type (&compression)[4])
*
* @brief -------------------------------------------------
* create - create a new file with no parent using an existing opened file handle
* -------------------------------------------------.
*
* @param [in,out] file The file.
* @param logicalbytes The logicalbytes.
* @param hunkbytes The hunkbytes.
* @param unitbytes The unitbytes.
* @param compression The compression.
*
* @return A std::error_condition.
*/
std::error_condition chd_file::create(
util::random_read_write::ptr &&file,
uint64_t logicalbytes,
uint32_t hunkbytes,
uint32_t unitbytes,
const chd_codec_type (&compression)[4])
{
// make sure we don't already have a file open
if (UNEXPECTED(m_file))
return error::ALREADY_OPEN;
else if (UNEXPECTED(!file))
return std::errc::invalid_argument;
// set the header parameters
m_logicalbytes = logicalbytes;
m_hunkbytes = hunkbytes;
m_unitbytes = unitbytes;
memcpy(m_compression, compression, sizeof(m_compression));
m_parent.reset();
// take ownership of the file
m_file = std::move(file);
return create_common();
}
/**
* @fn std::error_condition chd_file::create(util::random_read_write::ptr &&file, uint64_t logicalbytes, uint32_t hunkbytes, const chd_codec_type (&compression)[4], chd_file &parent)
*
* @brief -------------------------------------------------
* create - create a new file with a parent using an existing opened file handle
* -------------------------------------------------.
*
* @param [in,out] file The file.
* @param logicalbytes The logicalbytes.
* @param hunkbytes The hunkbytes.
* @param compression The compression.
* @param [in,out] parent The parent.
*
* @return A std::error_condition.
*/
std::error_condition chd_file::create(
util::random_read_write::ptr &&file,
uint64_t logicalbytes,
uint32_t hunkbytes,
const chd_codec_type (&compression)[4],
chd_file &parent)
{
// make sure we don't already have a file open
if (UNEXPECTED(m_file))
return error::ALREADY_OPEN;
else if (UNEXPECTED(!file))
return std::errc::invalid_argument;
// set the header parameters
m_logicalbytes = logicalbytes;
m_hunkbytes = hunkbytes;
m_unitbytes = parent.unit_bytes();
memcpy(m_compression, compression, sizeof(m_compression));
m_parent = std::shared_ptr<chd_file>(std::shared_ptr<chd_file>(), &parent);
// take ownership of the file
m_file = std::move(file);
return create_common();
}
/**
* @fn std::error_condition chd_file::create(std::string_view filename, uint64_t logicalbytes, uint32_t hunkbytes, uint32_t unitbytes, const chd_codec_type (&compression)[4])
*
* @brief -------------------------------------------------
* create - create a new file with no parent using a filename
* -------------------------------------------------.
*
* @param filename Filename of the file.
* @param logicalbytes The logicalbytes.
* @param hunkbytes The hunkbytes.
* @param unitbytes The unitbytes.
* @param compression The compression.
*
* @return A std::error_condition.
*/
std::error_condition chd_file::create(
std::string_view filename,
uint64_t logicalbytes,
uint32_t hunkbytes,
uint32_t unitbytes,
const chd_codec_type (&compression)[4])
{
// make sure we don't already have a file open
if (UNEXPECTED(m_file))
return error::ALREADY_OPEN;
// create the new file
util::core_file::ptr file;
std::error_condition filerr = util::core_file::open(filename, OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, file);
if (UNEXPECTED(filerr))
return filerr;
// create the file normally, then claim the file
std::error_condition chderr = create(std::move(file), logicalbytes, hunkbytes, unitbytes, compression);
// if an error happened, close and delete the file
if (UNEXPECTED(chderr))
{
file.reset();
osd_file::remove(std::string(filename)); // FIXME: allow osd_file to use std::string_view
}
return chderr;
}
/**
* @fn std::error_condition chd_file::create(std::string_view filename, uint64_t logicalbytes, uint32_t hunkbytes, const chd_codec_type (&compression)[4], chd_file &parent)
*
* @brief -------------------------------------------------
* create - create a new file with a parent using a filename
* -------------------------------------------------.
*
* @param filename Filename of the file.
* @param logicalbytes The logicalbytes.
* @param hunkbytes The hunkbytes.
* @param compression The compression.
* @param [in,out] parent The parent.
*
* @return A std::error_condition.
*/
std::error_condition chd_file::create(
std::string_view filename,
uint64_t logicalbytes,
uint32_t hunkbytes,
const chd_codec_type (&compression)[4],
chd_file &parent)
{
// make sure we don't already have a file open
if (UNEXPECTED(m_file))
return error::ALREADY_OPEN;
// create the new file
util::core_file::ptr file;
std::error_condition filerr = util::core_file::open(filename, OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE, file);
if (UNEXPECTED(filerr))
return filerr;
// create the file normally, then claim the file
std::error_condition chderr = create(std::move(file), logicalbytes, hunkbytes, compression, parent);
// if an error happened, close and delete the file
if (UNEXPECTED(chderr))
{
file.reset();
osd_file::remove(std::string(filename)); // FIXME: allow osd_file to use std::string_view
}
return chderr;
}
/**
* @fn std::error_condition chd_file::open(std::string_view filename, bool writeable, chd_file *parent)
*
* @brief -------------------------------------------------
* open - open an existing file for read or read/write
* -------------------------------------------------.
*
* @param filename Filename of the file.
* @param writeable true if writeable.
* @param [in,out] parent If non-null, the parent.
*
* @return A std::error_condition.
*/
std::error_condition chd_file::open(
std::string_view filename,
bool writeable,
chd_file *parent,
const open_parent_func &open_parent)
{
// make sure we don't already have a file open
if (UNEXPECTED(m_file))
return error::ALREADY_OPEN;
// open the file
const uint32_t openflags = writeable ? (OPEN_FLAG_READ | OPEN_FLAG_WRITE) : OPEN_FLAG_READ;
util::core_file::ptr file;
std::error_condition filerr = util::core_file::open(filename, openflags, file);
if (UNEXPECTED(filerr))
return filerr;
// now open the CHD
return open(std::move(file), writeable, parent, open_parent);
}
/**
* @fn std::error_condition chd_file::open(util::random_read_write::ptr &&file, bool writeable, chd_file *parent)
*
* @brief -------------------------------------------------
* open - open an existing file for read or read/write
* -------------------------------------------------.
*
* @param [in,out] file The file.
* @param writeable true if writeable.
* @param [in,out] parent If non-null, the parent.
*
* @return A std::error_condition.
*/
std::error_condition chd_file::open(
util::random_read_write::ptr &&file,
bool writeable,
chd_file *parent,
const open_parent_func &open_parent)
{
// make sure we don't already have a file open
if (UNEXPECTED(m_file))
return error::ALREADY_OPEN;
else if (UNEXPECTED(!file))
return std::errc::invalid_argument;
// open the file
m_file = std::move(file);
m_parent = std::shared_ptr<chd_file>(std::shared_ptr<chd_file>(), parent);
m_cachehunk = ~0;
return open_common(writeable, open_parent);
}
/**
* @fn void chd_file::close()
*
* @brief -------------------------------------------------
* close - close a CHD file for access
* -------------------------------------------------.
*/
void chd_file::close()
{
// reset file characteristics
m_file.reset();
m_allow_reads = false;
m_allow_writes = false;
// reset core parameters from the header
m_version = HEADER_VERSION;
m_logicalbytes = 0;
m_mapoffset = 0;
m_metaoffset = 0;
m_hunkbytes = 0;
m_hunkcount = 0;
m_unitbytes = 0;
m_unitcount = 0;
std::fill(std::begin(m_compression), std::end(m_compression), 0);
m_parent.reset();
m_parent_missing = false;
// reset key offsets within the header
m_mapoffset_offset = 0;
m_metaoffset_offset = 0;
m_sha1_offset = 0;
m_rawsha1_offset = 0;
m_parentsha1_offset = 0;
// reset map information
m_mapentrybytes = 0;
m_rawmap.clear();
// reset compression management
for (auto & elem : m_decompressor)
elem.reset();
m_compressed.clear();
// reset caching
m_cache.clear();
m_cachehunk = ~0;
}
std::error_condition chd_file::codec_process_hunk(uint32_t hunknum)
{
// punt if no file
if (UNEXPECTED(!m_file))
return std::error_condition(error::NOT_OPEN);
// return an error if out of range
if (UNEXPECTED(hunknum >= m_hunkcount))
return std::error_condition(error::HUNK_OUT_OF_RANGE);
// wrap this for clean reporting
try
{
// get a pointer to the map entry
switch (m_version)
{
// v3/v4 map entries
case 3:
case 4:
{
uint8_t const *const rawmap = &m_rawmap[16 * hunknum];
uint64_t const blockoffs = get_u64be(&rawmap[0]);
switch (rawmap[15] & V34_MAP_ENTRY_FLAG_TYPE_MASK)
{
case V34_MAP_ENTRY_TYPE_COMPRESSED:
{
uint32_t const blocklen = get_u16be(&rawmap[12]) | (uint32_t(rawmap[14]) << 16);
std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen);
if (UNEXPECTED(err))
return err;
m_decompressor[0]->process(&m_compressed[0], blocklen);
return std::error_condition();
}
case V34_MAP_ENTRY_TYPE_UNCOMPRESSED:
case V34_MAP_ENTRY_TYPE_MINI:
return std::error_condition(error::UNSUPPORTED_FORMAT);
case V34_MAP_ENTRY_TYPE_SELF_HUNK:
return codec_process_hunk(blockoffs);
case V34_MAP_ENTRY_TYPE_PARENT_HUNK:
if (UNEXPECTED(m_parent_missing))
return std::error_condition(error::REQUIRES_PARENT);
return m_parent->codec_process_hunk(blockoffs);
}
}
break;
// v5 map entries
case 5:
{
if (UNEXPECTED(!compressed()))
return std::error_condition(error::UNSUPPORTED_FORMAT);
// compressed case
uint8_t const *const rawmap = &m_rawmap[m_mapentrybytes * hunknum];
uint32_t const blocklen = get_u24be(&rawmap[1]);
uint64_t const blockoffs = get_u48be(&rawmap[4]);
switch (rawmap[0])
{
case COMPRESSION_TYPE_0:
case COMPRESSION_TYPE_1:
case COMPRESSION_TYPE_2:
case COMPRESSION_TYPE_3:
{
std::error_condition err = file_read(blockoffs, &m_compressed[0], blocklen);
if (UNEXPECTED(err))
return err;
auto &decompressor = *m_decompressor[rawmap[0]];
decompressor.process(&m_compressed[0], blocklen);
return std::error_condition();
}
case COMPRESSION_NONE:
return std::error_condition(error::UNSUPPORTED_FORMAT);
case COMPRESSION_SELF:
return codec_process_hunk(blockoffs);
case COMPRESSION_PARENT:
if (UNEXPECTED(m_parent_missing))
return std::error_condition(error::REQUIRES_PARENT);
return m_parent->codec_process_hunk(blockoffs / (m_parent->hunk_bytes() / m_parent->unit_bytes()));
}
break;
}
}
// if we get here, the map contained an unsupported block type
return std::error_condition(error::INVALID_DATA);
}
catch (std::error_condition const &err)
{
// just return errors
return err;
}
}
/**
* @fn std::error_condition chd_file::read_hunk(uint32_t hunknum, void *buffer)
*
* @brief -------------------------------------------------
* read - read a single hunk from the CHD file
* -------------------------------------------------.
*