-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVolume.cs
1476 lines (1283 loc) · 43.8 KB
/
Volume.cs
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
// Volume.cs
// Copyright © 2019-2020 Kenneth Gober
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// Future Improvements / To Do
// provide a way to pad an image with leading zeros
// support volume partitioning (more efficiently than ClusteredVolume)
// allow CHSVolume.this to lazily create/grow Tracks as needed
// add missing Sector header fields (track number, head number)
using System;
using System.Text;
namespace FSX
{
abstract class Block
{
public abstract Int32 Size { get; }
public abstract Boolean IsDirty { get; set; }
public abstract Byte this[Int32 offset] { get; set; }
public abstract Int32 CopyTo(Byte[] targetBuffer, Int32 targetOffset);
public abstract Int32 CopyTo(Byte[] targetBuffer, Int32 targetOffset, Int32 blockOffset, Int32 count);
public abstract Int32 CopyFrom(Byte[] sourceBuffer, Int32 sourceOffset, Int32 blockOffset, Int32 count);
public abstract Byte GetByte(Int32 offset);
public abstract Byte GetByte(ref Int32 offset);
public abstract Int16 GetInt16B(Int32 offset);
public abstract Int16 GetInt16B(ref Int32 offset);
public abstract Int16 GetInt16L(Int32 offset);
public abstract Int16 GetInt16L(ref Int32 offset);
public abstract UInt16 GetUInt16B(Int32 offset);
public abstract UInt16 GetUInt16B(ref Int32 offset);
public abstract UInt16 GetUInt16L(Int32 offset);
public abstract UInt16 GetUInt16L(ref Int32 offset);
public abstract Int32 GetInt32B(Int32 offset);
public abstract Int32 GetInt32B(ref Int32 offset);
public abstract Int32 GetInt32L(Int32 offset);
public abstract Int32 GetInt32L(ref Int32 offset);
public abstract Int32 GetInt32P(Int32 offset);
public abstract Int32 GetInt32P(ref Int32 offset);
public abstract UInt32 GetUInt32B(Int32 offset);
public abstract UInt32 GetUInt32B(ref Int32 offset);
public abstract UInt32 GetUInt32L(Int32 offset);
public abstract UInt32 GetUInt32L(ref Int32 offset);
public abstract UInt32 GetUInt32P(Int32 offset);
public abstract UInt32 GetUInt32P(ref Int32 offset);
public abstract String GetString(Int32 offset, Int32 count, Encoding encoding);
public abstract String GetCString(Int32 offset, Int32 maxCount, Encoding encoding);
public abstract Int32 IndexOf(Int32 offset, String pattern, Encoding encoding);
}
abstract class Volume
{
public abstract Volume Base { get; }
public abstract String Source { get; }
public abstract String Info { get; }
public abstract Int32 BlockSize { get; }
public abstract Int32 BlockCount { get; }
public abstract Int32 MinCylinder { get; }
public abstract Int32 MaxCylinder { get; }
public abstract Int32 MinHead { get; }
public abstract Int32 MaxHead { get; }
public abstract Int32 MinSector();
public abstract Int32 MinSector(Int32 cylinder, Int32 head);
public abstract Int32 MaxSector(Int32 cylinder, Int32 head);
public abstract Block this[Int32 lbn] { get; }
public abstract Block this[Int32 cylinder, Int32 head, Int32 sector] { get; }
}
// A Record is a basic implementation of a Block.
class Record : Block
{
private Byte[] mData;
private Boolean mDirty;
public Record(Int32 size)
{
mData = new Byte[size];
}
public Record(Int32 size, Byte value) : this(size)
{
for (Int32 i = 0; i < size; i++) mData[i] = value;
}
public Record(Int32 size, Byte[] data, Int32 offset) : this(size)
{
for (Int32 i = 0; i < size; i++) mData[i] = data[offset++];
}
public override Int32 Size
{
get { return mData.Length; }
}
public override Boolean IsDirty
{
get { return mDirty; }
set { mDirty = value; }
}
public override Byte this[Int32 offset]
{
get { return mData[offset]; }
set { mDirty |= (mData[offset] != value); mData[offset] = value; }
}
public override Int32 CopyTo(Byte[] targetBuffer, Int32 targetOffset)
{
return Buffer.Copy(mData, 0, targetBuffer, targetOffset, mData.Length);
}
public override Int32 CopyTo(Byte[] targetBuffer, Int32 targetOffset, Int32 blockOffset, Int32 count)
{
return Buffer.Copy(mData, blockOffset, targetBuffer, targetOffset, count);
}
public override Int32 CopyFrom(Byte[] sourceBuffer, Int32 sourceOffset, Int32 blockOffset, Int32 count)
{
Int32 n = sourceBuffer.Length - sourceOffset;
if (count > n) count = n;
n = mData.Length - blockOffset;
if (count > n) count = n;
if (!mDirty)
{
for (Int32 i = 0; i < count; i++)
{
if (mData[blockOffset + i] != sourceBuffer[sourceOffset + i])
{
mDirty = true;
break;
}
}
}
return Buffer.Copy(sourceBuffer, sourceOffset, mData, blockOffset, count);
}
public override Byte GetByte(Int32 offset)
{
return Buffer.GetByte(mData, offset);
}
public override Byte GetByte(ref Int32 offset)
{
return Buffer.GetByte(mData, ref offset);
}
public override Int16 GetInt16B(Int32 offset)
{
return Buffer.GetInt16B(mData, offset);
}
public override Int16 GetInt16B(ref Int32 offset)
{
return Buffer.GetInt16B(mData, ref offset);
}
public override Int16 GetInt16L(Int32 offset)
{
return Buffer.GetInt16L(mData, offset);
}
public override Int16 GetInt16L(ref Int32 offset)
{
return Buffer.GetInt16L(mData, ref offset);
}
public override UInt16 GetUInt16B(Int32 offset)
{
return Buffer.GetUInt16B(mData, offset);
}
public override UInt16 GetUInt16B(ref Int32 offset)
{
return Buffer.GetUInt16B(mData, ref offset);
}
public override UInt16 GetUInt16L(Int32 offset)
{
return Buffer.GetUInt16L(mData, offset);
}
public override UInt16 GetUInt16L(ref Int32 offset)
{
return Buffer.GetUInt16L(mData, ref offset);
}
public override Int32 GetInt32B(Int32 offset)
{
return Buffer.GetInt32B(mData, offset);
}
public override Int32 GetInt32B(ref Int32 offset)
{
return Buffer.GetInt32B(mData, ref offset);
}
public override Int32 GetInt32L(Int32 offset)
{
return Buffer.GetInt32L(mData, offset);
}
public override Int32 GetInt32L(ref Int32 offset)
{
return Buffer.GetInt32L(mData, ref offset);
}
public override Int32 GetInt32P(Int32 offset)
{
return Buffer.GetInt32P(mData, offset);
}
public override Int32 GetInt32P(ref Int32 offset)
{
return Buffer.GetInt32P(mData, ref offset);
}
public override UInt32 GetUInt32B(Int32 offset)
{
return Buffer.GetUInt32B(mData, offset);
}
public override UInt32 GetUInt32B(ref Int32 offset)
{
return Buffer.GetUInt32B(mData, ref offset);
}
public override UInt32 GetUInt32L(Int32 offset)
{
return Buffer.GetUInt32L(mData, offset);
}
public override UInt32 GetUInt32L(ref Int32 offset)
{
return Buffer.GetUInt32L(mData, ref offset);
}
public override UInt32 GetUInt32P(Int32 offset)
{
return Buffer.GetUInt32P(mData, offset);
}
public override UInt32 GetUInt32P(ref Int32 offset)
{
return Buffer.GetUInt32P(mData, ref offset);
}
public override String GetString(Int32 offset, Int32 count, Encoding encoding)
{
return encoding.GetString(mData, offset, count);
}
public override String GetCString(Int32 offset, Int32 maxCount, Encoding encoding)
{
Int32 n = mData.Length - offset;
if (n > maxCount) n = maxCount;
for (Int32 i = 0; i < n; i++)
{
if (mData[offset + i] == 0)
{
n = i;
break;
}
}
return encoding.GetString(mData, offset, n);
}
public override Int32 IndexOf(Int32 offset, String pattern, Encoding encoding)
{
Byte[] pat = encoding.GetBytes(pattern);
Int32 n = mData.Length - pat.Length;
for (Int32 i = offset; i < n; i++)
{
if (mData[i] != pat[0]) continue;
Boolean f = false;
for (Int32 j = 1; j < pat.Length; j++)
{
if (mData[i + j] != pat[j])
{
f = true;
break;
}
}
if (f) continue;
return i;
}
return -1;
}
}
// A Sector is a Record with a corresponding sector header.
class Sector : Record
{
private Int32 mID; // range: 0-2147483647
private Int32 mErr;
public Sector(Int32 id, Int32 size) : base(size)
{
if (id < 0) throw new ArgumentOutOfRangeException("id");
mID = id;
}
public Sector(Int32 id, Int32 size, Byte value) : base(size, value)
{
if (id < 0) throw new ArgumentOutOfRangeException("id");
mID = id;
}
public Sector(Int32 id, Int32 size, Byte[] data, Int32 offset) : base(size, data, offset)
{
if (id < 0) throw new ArgumentOutOfRangeException("id");
mID = id;
}
public Int32 ID
{
get { return mID; }
}
public Int32 ErrorCode
{
get { return mErr; }
set { mErr = value; }
}
}
// A Cluster is a group of equal-sized Blocks treated as a single larger Block.
class Cluster : Block
{
private Block[] mData;
private Int32 mBlockSize;
private Boolean mDirty;
public Cluster(Block[] blocks)
{
for (Int32 i = 1; i < blocks.Length; i++) if (blocks[0].Size != blocks[i].Size) throw new ArgumentOutOfRangeException("blocks");
mData = blocks;
mBlockSize = blocks[0].Size;
}
public override Int32 Size
{
get { return mData.Length * mBlockSize; }
}
public override Boolean IsDirty
{
get { return mDirty; }
set { mDirty = value; }
}
public override Byte this[Int32 offset]
{
get { return mData[offset / mBlockSize][offset % mBlockSize]; }
set { mDirty |= (mData[offset / mBlockSize][offset % mBlockSize] != value); mData[offset / mBlockSize][offset % mBlockSize] = value; }
}
public override Int32 CopyTo(Byte[] targetBuffer, Int32 targetOffset)
{
Int32 ct = 0;
for (Int32 i = 0; i < mData.Length; i++)
{
ct += mData[i].CopyTo(targetBuffer, targetOffset);
targetOffset += mBlockSize;
}
return ct;
}
public override Int32 CopyTo(Byte[] targetBuffer, Int32 targetOffset, Int32 blockOffset, Int32 count)
{
Int32 ct = 0;
Int32 i = blockOffset / mBlockSize;
blockOffset %= mBlockSize;
while ((count > 0) && (i < mData.Length))
{
Int32 n = mBlockSize - blockOffset; // number of bytes available to copy in block
if (n > count) n = count;
ct += mData[i++].CopyTo(targetBuffer, targetOffset, blockOffset, n);
targetOffset += n;
blockOffset = 0;
count -= n;
}
return ct;
}
public override Int32 CopyFrom(Byte[] sourceBuffer, Int32 sourceOffset, Int32 blockOffset, Int32 count)
{
Int32 ct = 0;
Int32 i = blockOffset / mBlockSize;
blockOffset %= mBlockSize;
while ((count > 0) && (i < mData.Length))
{
Int32 n = mBlockSize - blockOffset; // number of bytes available to copy in block
if (n > count) n = count;
ct += mData[i].CopyFrom(sourceBuffer, sourceOffset, blockOffset, n);
mDirty |= mData[i++].IsDirty;
sourceOffset += n;
blockOffset = 0;
count -= n;
}
return ct;
}
public override Byte GetByte(Int32 offset)
{
return mData[offset / mBlockSize][offset % mBlockSize];
}
public override Byte GetByte(ref Int32 offset)
{
Byte n = mData[offset / mBlockSize][offset % mBlockSize];
offset++;
return n;
}
public override Int16 GetInt16B(Int32 offset)
{
Int32 i = offset;
return GetInt16B(ref i);
}
public override Int16 GetInt16B(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
Int16 n;
if (q + 1 < mBlockSize)
{
n = mData[p].GetInt16B(q);
}
else
{
Byte[] buf = new Byte[2];
if (BitConverter.IsLittleEndian)
{
buf[1] = mData[p][q];
buf[0] = mData[p + 1][0];
}
else
{
buf[0] = mData[p][q];
buf[1] = mData[p + 1][0];
}
n = BitConverter.ToInt16(buf, 0);
}
offset += 2;
return n;
}
public override Int16 GetInt16L(Int32 offset)
{
Int32 i = offset;
return GetInt16L(ref i);
}
public override Int16 GetInt16L(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
Int16 n;
if (q + 1 < mBlockSize)
{
n = mData[p].GetInt16L(q);
}
else
{
Byte[] buf = new Byte[2];
if (BitConverter.IsLittleEndian)
{
buf[0] = mData[p][q];
buf[1] = mData[p + 1][0];
}
else
{
buf[1] = mData[p][q];
buf[0] = mData[p + 1][0];
}
n = BitConverter.ToInt16(buf, 0);
}
offset += 2;
return n;
}
public override UInt16 GetUInt16B(Int32 offset)
{
Int32 i = offset;
return GetUInt16B(ref i);
}
public override UInt16 GetUInt16B(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
UInt16 n;
if (q + 1 < mBlockSize)
{
n = mData[p].GetUInt16B(q);
}
else
{
Byte[] buf = new Byte[2];
if (BitConverter.IsLittleEndian)
{
buf[1] = mData[p][q];
buf[0] = mData[p + 1][0];
}
else
{
buf[0] = mData[p][q];
buf[1] = mData[p + 1][0];
}
n = BitConverter.ToUInt16(buf, 0);
}
offset += 2;
return n;
}
public override UInt16 GetUInt16L(Int32 offset)
{
Int32 i = offset;
return GetUInt16L(ref i);
}
public override UInt16 GetUInt16L(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
UInt16 n;
if (q + 1 < mBlockSize)
{
n = mData[p].GetUInt16L(q);
}
else
{
Byte[] buf = new Byte[2];
if (BitConverter.IsLittleEndian)
{
buf[0] = mData[p][q];
buf[1] = mData[p + 1][0];
}
else
{
buf[1] = mData[p][q];
buf[0] = mData[p + 1][0];
}
n = BitConverter.ToUInt16(buf, 0);
}
offset += 2;
return n;
}
public override Int32 GetInt32B(Int32 offset)
{
Int32 i = offset;
return GetInt32B(ref i);
}
public override Int32 GetInt32B(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
Int32 n;
if (q + 3 < mBlockSize)
{
n = mData[p].GetInt32B(q);
}
else
{
Byte[] buf = new Byte[4];
Int32 i = mData[p].CopyTo(buf, 0, q, mBlockSize - q);
mData[++p].CopyTo(buf, i, 0, 4 - i);
n = Buffer.GetInt32B(buf, 0);
}
offset += 4;
return n;
}
public override Int32 GetInt32L(Int32 offset)
{
Int32 i = offset;
return GetInt32L(ref i);
}
public override Int32 GetInt32L(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
Int32 n;
if (q + 3 < mBlockSize)
{
n = mData[p].GetInt32L(q);
}
else
{
Byte[] buf = new Byte[4];
Int32 i = mData[p].CopyTo(buf, 0, q, mBlockSize - q);
mData[++p].CopyTo(buf, i, 0, 4 - i);
n = Buffer.GetInt32L(buf, 0);
}
offset += 4;
return n;
}
public override Int32 GetInt32P(Int32 offset)
{
Int32 i = offset;
return GetInt32P(ref i);
}
public override Int32 GetInt32P(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
Int32 n;
if (q + 3 < mBlockSize)
{
n = mData[p].GetInt32P(q);
}
else
{
Byte[] buf = new Byte[4];
Int32 i = mData[p].CopyTo(buf, 0, q, mBlockSize - q);
mData[++p].CopyTo(buf, i, 0, 4 - i);
n = Buffer.GetInt32P(buf, 0);
}
offset += 4;
return n;
}
public override UInt32 GetUInt32B(Int32 offset)
{
Int32 i = offset;
return GetUInt32B(ref i);
}
public override UInt32 GetUInt32B(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
UInt32 n;
if (q + 3 < mBlockSize)
{
n = mData[p].GetUInt32B(q);
}
else
{
Byte[] buf = new Byte[4];
Int32 i = mData[p].CopyTo(buf, 0, q, mBlockSize - q);
mData[++p].CopyTo(buf, i, 0, 4 - i);
n = Buffer.GetUInt32B(buf, 0);
}
offset += 4;
return n;
}
public override UInt32 GetUInt32L(Int32 offset)
{
Int32 i = offset;
return GetUInt32L(ref i);
}
public override UInt32 GetUInt32L(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
UInt32 n;
if (q + 3 < mBlockSize)
{
n = mData[p].GetUInt32L(q);
}
else
{
Byte[] buf = new Byte[4];
Int32 i = mData[p].CopyTo(buf, 0, q, mBlockSize - q);
mData[++p].CopyTo(buf, i, 0, 4 - i);
n = Buffer.GetUInt32L(buf, 0);
}
offset += 4;
return n;
}
public override UInt32 GetUInt32P(Int32 offset)
{
Int32 i = offset;
return GetUInt32P(ref i);
}
public override UInt32 GetUInt32P(ref Int32 offset)
{
Int32 p = offset / mBlockSize;
Int32 q = offset % mBlockSize;
UInt32 n;
if (q + 3 < mBlockSize)
{
n = mData[p].GetUInt32P(q);
}
else
{
Byte[] buf = new Byte[4];
Int32 i = mData[p].CopyTo(buf, 0, q, mBlockSize - q);
mData[++p].CopyTo(buf, i, 0, 4 - i);
n = Buffer.GetUInt32P(buf, 0);
}
offset += 4;
return n;
}
public override String GetString(Int32 offset, Int32 count, Encoding encoding)
{
Int32 p = offset / mBlockSize;
Int32 q = (offset + count - 1) / mBlockSize;
if (p == q)
{
return mData[p].GetString(offset % mBlockSize, count, encoding);
}
else
{
Byte[] buffer = new Byte[count];
count = CopyTo(buffer, 0, offset, count);
return encoding.GetString(buffer, 0, count);
}
}
public override String GetCString(Int32 offset, Int32 maxCount, Encoding encoding)
{
Int32 p = offset / mBlockSize;
Int32 q = (offset + maxCount - 1) / mBlockSize;
if (p == q)
{
return mData[p].GetCString(offset % mBlockSize, maxCount, encoding);
}
else
{
Int32 n = mData.Length * mBlockSize - offset;
if (n > maxCount) n = maxCount;
for (Int32 i = 0; i < n; i++)
{
if (this[offset + i] == 0)
{
n = i;
break;
}
}
q = (offset + n - 1) / mBlockSize;
if (p == q)
{
return mData[p].GetCString(offset % mBlockSize, n, encoding);
}
else
{
Byte[] buffer = new Byte[n];
n = CopyTo(buffer, 0, offset, n);
return encoding.GetString(buffer, 0, n);
}
}
}
public override Int32 IndexOf(Int32 offset, String pattern, Encoding encoding)
{
Byte[] pat = encoding.GetBytes(pattern);
Int32 n = mData.Length * mBlockSize - pat.Length;
for (Int32 i = offset; i < n; i++)
{
if (this[i] != pat[0]) continue;
Boolean f = false;
for (Int32 j = 1; j < pat.Length; j++)
{
if (this[i + j] != pat[j])
{
f = true;
break;
}
}
if (f) continue;
return i;
}
return -1;
}
}
// A Track is a sequence of Sectors. Note that the Sector IDs need not be
// sequential, or even unique, even though an OS may treat them that way.
class Track
{
private Sector[] mData;
private Int32 mMinID = -1;
private Int32 mMaxID = -1;
public Track(Int32 length)
{
mData = new Sector[length];
}
public Int32 Length
{
get { return mData.Length; }
}
public Int32 MinSector
{
get { return mMinID; }
}
public Int32 MaxSector
{
get { return mMaxID; }
}
public Sector this[Int32 index]
{
get
{
return mData[index];
}
set
{
mData[index] = value;
Int32 id = value.ID;
if ((mMinID == -1) || (id < mMinID)) mMinID = id;
if (id > mMaxID) mMaxID = id;
}
}
public Sector Sector(Int32 id)
{
for (Int32 i = 0; i < mData.Length; i++) if (mData[i].ID == id) return mData[i];
return null;
}
}
// An LBAVolume is a simple array of N fixed-size blocks, numbered from 0 to N-1.
// CHS addressing is also possible using C=0, H=0, and S ranging from 1 to N.
class LBAVolume : Volume
{
private String mSource;
private String mInfo;
private Int32 mBlockSize;
private Int32 mBlockCount;
private Record[] mData;
public LBAVolume(String source, String info, Int32 blockSize, Int32 blockCount)
{
mSource = source;
mInfo = info;
mBlockSize = blockSize;
mBlockCount = blockCount;
mData = new Record[blockCount];
}
public LBAVolume(String source, String info, Byte[] data, Int32 blockSize) : this(source, info, blockSize, data.Length / blockSize)
{
for (Int32 i = 0, p = 0; i < mBlockCount; i++, p += blockSize) mData[i] = new Record(blockSize, data, p);
}
public override Volume Base
{
get { return null; }
}
public override String Source
{
get { return mSource; }
}
public override string Info
{
get { return mInfo; }
}
public override Int32 BlockSize
{
get { return mBlockSize; }
}
public override Int32 BlockCount
{
get { return mBlockCount; }
}
public override Int32 MinCylinder
{
get { return 0; }
}
public override Int32 MaxCylinder
{
get { return 0; }
}
public override Int32 MinHead
{
get { return 0; }
}
public override Int32 MaxHead
{
get { return 0; }
}
public override Int32 MinSector()
{
return 1;
}
public override Int32 MinSector(Int32 cylinder, Int32 head)
{
return 1;
}
public override Int32 MaxSector(Int32 cylinder, Int32 head)
{
return mBlockCount;
}
public override Block this[Int32 lbn]
{
get { return (mData[lbn] == null) ? mData[lbn] = new Record(mBlockSize) : mData[lbn]; }
}
public override Block this[Int32 cylinder, Int32 head, Int32 sector]
{
get { return this[sector - 1]; }
}
}
// A CHSVolume is a track-oriented volume, with each track addressed by a Cylinder and Head number.
// The number of Cylinders and Heads is fixed, but the number of Sectors per track may vary.
// Default Cylinder and Head numbering starts at 0, while Sectors within a Track are numbered from 1.
// A CHSVolume normally has a fixed sector size but can be constructed with variable length sectors.
// Uninitialized sectors will be created lazily using the default sector size.
class CHSVolume : Volume
{
private String mSource;
private String mInfo;
private Int32 mBlockSize;
private Int32 mBlockCount;
private Int32 mCyls;
private Int32 mHeads;
private Int32 mMinCyl;
private Int32 mMinHead;
private Int32 mMinSect;
private Track[,] mData;
public CHSVolume(String source, String info, Int32 sectorSize, Int32 minCylinder, Int32 numCylinders, Int32 minHead, Int32 numHeads, Int32 minSector)
{
mSource = source;
mInfo = info;
mBlockSize = sectorSize;
mBlockCount = -1;
mCyls = numCylinders;
mHeads = numHeads;
mMinCyl = minCylinder;
mMinHead = minHead;
mMinSect = minSector;
mData = new Track[numCylinders, numHeads];
}
public CHSVolume(String source, String info, Int32 sectorSize, Int32 numCylinders, Int32 numHeads)
: this(source, info, sectorSize, 0, numCylinders, 0, numHeads, 1)
{
}
public CHSVolume(String source, String info, Byte[] data, Int32 sectorSize, Int32 minCylinder, Int32 numCylinders, Int32 minHead, Int32 numHeads, Int32 minSector, Int32 numSectors)
: this(source, info, sectorSize, minCylinder, numCylinders, minHead, numHeads, minSector)
{
Int32 p = 0;
for (Int32 c = 0; c < numCylinders; c++)
{
for (Int32 h = 0; h < numHeads; h++)