Skip to content

Commit e8b22ce

Browse files
authored
CDROM Constants
1 parent d277ece commit e8b22ce

File tree

3 files changed

+53
-23
lines changed

3 files changed

+53
-23
lines changed

SabreTools.Serialization/Extensions/CDROM.cs

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,19 @@ public static class CDROM
1313
/// </summary>
1414
public class ISO9660Stream : Stream
1515
{
16-
// Constant variables
16+
// Base CDROM stream (2352-byte sector)
1717
private readonly Stream _baseStream;
18-
private const long _baseSectorSize = 2352;
19-
private long _isoSectorSize = 2048;
20-
// TODO: Support flexible sector size (MODE2_FORM2)
2118

2219
// State variables
2320
private long _position = 0;
2421
private SectorMode _currentMode = SectorMode.UNKNOWN;
2522
private long _userDataStart = 16;
2623
private long _userDataEnd = 2064;
24+
private long _isoSectorSize = Constants.Mode1DataSize;
2725

2826
public ISO9660Stream(Stream inputStream)
2927
{
30-
if (inputStream == null)
31-
throw new ArgumentNullException("Stream cannot be null.", nameof(inputStream));
32-
else if (!inputStream.CanSeek || !inputStream.CanRead)
28+
if (!inputStream.CanSeek || !inputStream.CanRead)
3329
throw new ArgumentException("Stream must be readable and seekable.", nameof(inputStream));
3430
_baseStream = inputStream;
3531
}
@@ -47,7 +43,7 @@ public override long Length
4743
{
4844
get
4945
{
50-
return (_baseStream.Length / _baseSectorSize) * _isoSectorSize;
46+
return (_baseStream.Length / Constants.CDROMSectorSize) * _isoSectorSize;
5147
}
5248
}
5349

@@ -79,10 +75,10 @@ public override long Position
7975
SetState(_position);
8076

8177
// Get the number of ISO sectors before current position
82-
long isoPosition = (_position / _baseSectorSize) * _isoSectorSize;
78+
long isoPosition = (_position / Constants.CDROMSectorSize) * _isoSectorSize;
8379

8480
// Add the within-sector position
85-
long remainder = _position % _baseSectorSize;
81+
long remainder = _position % Constants.CDROMSectorSize;
8682
if (remainder > _userDataEnd)
8783
isoPosition += _isoSectorSize;
8884
else if (remainder > _userDataStart)
@@ -106,13 +102,13 @@ public override int Read(byte[] buffer, int offset, int count)
106102
while (remaining > 0 && _position < _baseStream.Length)
107103
{
108104
// Determine location of current sector
109-
long baseStreamOffset = (_position / _baseSectorSize) * _baseSectorSize;
105+
long baseStreamOffset = (_position / Constants.CDROMSectorSize) * Constants.CDROMSectorSize;
110106

111107
// Set the current sector's mode and user data location
112108
SetState(baseStreamOffset);
113109

114110
// Deal with case where base position is not in ISO stream
115-
long remainder = _position % _baseSectorSize;
111+
long remainder = _position % Constants.CDROMSectorSize;
116112
long sectorOffset = remainder - _userDataStart;
117113
if (remainder < _userDataStart)
118114
{
@@ -122,9 +118,9 @@ public override int Read(byte[] buffer, int offset, int count)
122118
}
123119
else if (remainder >= _userDataEnd)
124120
{
125-
baseStreamOffset += _baseSectorSize;
121+
baseStreamOffset += Constants.CDROMSectorSize;
126122
sectorOffset = 0;
127-
_position += _baseSectorSize - _userDataEnd + _userDataStart;
123+
_position += Constants.CDROMSectorSize - _userDataEnd + _userDataStart;
128124
}
129125
else
130126
baseStreamOffset += remainder;
@@ -159,7 +155,7 @@ public override int Read(byte[] buffer, int offset, int count)
159155
// Update state for base stream
160156
_position = _baseStream.Position;
161157
if (readEntireSector)
162-
_position += (_baseSectorSize - _userDataEnd) + _userDataStart;
158+
_position += (Constants.CDROMSectorSize - _userDataEnd) + _userDataStart;
163159

164160
// Update state for ISO stream
165161
totalRead += bytesRead;
@@ -192,7 +188,7 @@ public override long Seek(long offset, SeekOrigin origin)
192188
}
193189

194190
// Get the number of ISO sectors before current position
195-
long newPosition = (targetPosition / _isoSectorSize) * _baseSectorSize;
191+
long newPosition = (targetPosition / _isoSectorSize) * Constants.CDROMSectorSize;
196192

197193
// Set the current sector's mode and user data location
198194
SetState(newPosition);
@@ -212,7 +208,7 @@ public override long Seek(long offset, SeekOrigin origin)
212208
private void SetState(long sectorLocation)
213209
{
214210
long oldPosition = _baseStream.Position;
215-
long modePosition = (sectorLocation - sectorLocation % _baseSectorSize) + 15;
211+
long modePosition = (sectorLocation - sectorLocation % Constants.CDROMSectorSize) + 15;
216212
_baseStream.Seek(modePosition, SeekOrigin.Begin);
217213
byte modeByte = _baseStream.ReadByteValue();
218214
if (modeByte == 0)
@@ -237,21 +233,21 @@ private void SetState(long sectorLocation)
237233
case SectorMode.MODE1:
238234
_userDataStart = 16;
239235
_userDataEnd = 2064;
240-
//_isoSectorSize = 2048;
236+
//_isoSectorSize = Constants.Mode1DataSize;
241237
break;
242238

243239
case SectorMode.MODE2_FORM1:
244240
_userDataStart = 24;
245241
_userDataEnd = 2072;
246-
//_isoSectorSize = 2048;
242+
//_isoSectorSize = Constants.Form1DataSize;
247243
break;
248244

249245
case SectorMode.MODE2_FORM2:
250246
_userDataStart = 24;
251247
_userDataEnd = 2072;
252248
// TODO: Support flexible sector length
253249
//_userDataEnd = 2348;
254-
//_isoSectorSize = 2324;
250+
//_isoSectorSize = Constants.Form2DataSize;
255251
break;
256252

257253
case SectorMode.MODE0:
@@ -260,13 +256,13 @@ private void SetState(long sectorLocation)
260256
_userDataEnd = 2064;
261257
// TODO: Support flexible sector length
262258
//_userDataEnd = 2352;
263-
//_isoSectorSize = 2336;
259+
//_isoSectorSize = Constants.Mode0DataSize;
264260
break;
265261

266262
case SectorMode.UNKNOWN:
267263
_userDataStart = 16;
268264
_userDataEnd = 2064;
269-
//_isoSectorSize = 2048;
265+
//_isoSectorSize = Constants.Mode1DataSize;
270266
break;
271267
}
272268

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace SabreTools.Data.Models.ISO9660
2+
{
3+
/// <summary>
4+
/// CDROM constant values
5+
/// </summary>
6+
/// <see href="https://ecma-international.org/wp-content/uploads/ECMA-119_5th_edition_december_2024.pdf"/>
7+
public static class Constants
8+
{
9+
/// <summary>
10+
/// Size of a complete CDROM data sector
11+
/// </summary>
12+
public const long CDROMSectorSize = 2352;
13+
14+
/// <summary>
15+
/// Size of user data length for Mode0 / Mode2 Formless
16+
/// </summary>
17+
public const long Mode0DataSize = 2336;
18+
19+
/// <summary>
20+
/// Size of user data length for Mode1 / Mode2 Form1
21+
/// </summary>
22+
public const long Mode1DataSize = 2048;
23+
24+
/// <summary>
25+
/// Size of user data length for Mode1 / Mode2 Form1
26+
/// </summary>
27+
public const long Form1DataSize = 2048;
28+
29+
/// <summary>
30+
/// Size of user data length for Mode2 Form2
31+
/// </summary>
32+
public const long Form2DataSize = 2324;
33+
}
34+
}

SabreTools.Serialization/Models/ISO9660/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace SabreTools.Data.Models.ISO9660
22
{
33
/// <summary>
4-
/// ISO9660 filesystem extent
4+
/// ISO9660 constant values and arrays
55
/// </summary>
66
/// <see href="https://ecma-international.org/wp-content/uploads/ECMA-119_5th_edition_december_2024.pdf"/>
77
public static class Constants

0 commit comments

Comments
 (0)