22using System . Buffers . Binary ;
33using System . Collections . Immutable ;
44using System . IO ;
5+ using System . Linq ;
56using System . Text ;
67
78namespace TotalImage . FileSystems . ISO
@@ -13,6 +14,13 @@ public class IsoPrimaryVolumeDescriptor : IsoVolumeDescriptor
1314 {
1415 private static readonly char [ ] trimCharacters = new char [ ] { '\0 ' , ' ' } ;
1516
17+ private static readonly byte [ ] [ ] jolietEscapeSequences = new byte [ ] [ ]
18+ {
19+ new byte [ ] { 0x25 , 0x2f , 0x40 } , // Level 1
20+ new byte [ ] { 0x25 , 0x2f , 0x43 } , // Level 2
21+ new byte [ ] { 0x25 , 0x2f , 0x45 } // Level 3
22+ } ;
23+
1624 /// <summary>
1725 /// The system that can read the first 16 sectors of the image
1826 /// </summary>
@@ -138,6 +146,11 @@ public class IsoPrimaryVolumeDescriptor : IsoVolumeDescriptor
138146 /// </summary>
139147 public ImmutableArray < byte > ApplicationContent { get ; }
140148
149+ /// <summary>
150+ /// Indicates whether the volume descriptor is a Joliet secondary volume descriptor
151+ /// </summary>
152+ public bool IsJolietVolumeDescriptor { get ; }
153+
141154 /// <summary>
142155 /// Create an ISO 9660 primary volume descriptor
143156 /// </summary>
@@ -148,12 +161,31 @@ public class IsoPrimaryVolumeDescriptor : IsoVolumeDescriptor
148161 public IsoPrimaryVolumeDescriptor ( in ReadOnlySpan < byte > record , in IsoVolumeDescriptorType type , in ImmutableArray < byte > identifier , in byte version )
149162 : base ( type , identifier , version )
150163 {
164+ bool isUnicode = false ;
165+ if ( type == IsoVolumeDescriptorType . SupplementaryVolumeDescriptor )
166+ {
167+ for ( int i = 0 ; i < jolietEscapeSequences . Length ; i ++ )
168+ {
169+ isUnicode = record [ 88 ..91 ] . SequenceEqual ( jolietEscapeSequences [ i ] . AsSpan ( ) ) ;
170+ if ( isUnicode )
171+ {
172+ break ;
173+ }
174+ }
175+ }
176+
177+ IsJolietVolumeDescriptor = isUnicode ;
178+
179+ Encoding encoding = isUnicode
180+ ? Encoding . BigEndianUnicode
181+ : Encoding . ASCII ;
182+
151183 char [ ] textBuffer = new char [ 32 ] ;
152184
153- Encoding . ASCII . GetChars ( record [ 8 ..40 ] , textBuffer ) ;
185+ encoding . GetChars ( record [ 8 ..40 ] , textBuffer ) ;
154186 SystemIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
155187
156- Encoding . ASCII . GetChars ( record [ 40 ..72 ] , textBuffer ) ;
188+ encoding . GetChars ( record [ 40 ..72 ] , textBuffer ) ;
157189 VolumeIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
158190
159191 VolumeSpace = IsoUtilities . ReadUInt32MultiEndian ( record [ 80 ..88 ] ) ;
@@ -170,31 +202,31 @@ public IsoPrimaryVolumeDescriptor(in ReadOnlySpan<byte> record, in IsoVolumeDesc
170202 MPathTableOffset = BinaryPrimitives . ReadUInt32BigEndian ( record [ 148 ..152 ] ) ;
171203 MPathTableOffset = BinaryPrimitives . ReadUInt32BigEndian ( record [ 152 ..156 ] ) ;
172204
173- RootDirectory = new IsoFileSystemObject ( record [ 156 ..190 ] ) ;
205+ RootDirectory = new IsoFileSystemObject ( record [ 156 ..190 ] , isUnicode ) ;
174206
175207 textBuffer = new char [ 128 ] ;
176208
177- Encoding . ASCII . GetChars ( record [ 190 ..318 ] , textBuffer ) ;
209+ encoding . GetChars ( record [ 190 ..318 ] , textBuffer ) ;
178210 VolumeSetIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
179211
180- Encoding . ASCII . GetChars ( record [ 318 ..446 ] , textBuffer ) ;
212+ encoding . GetChars ( record [ 318 ..446 ] , textBuffer ) ;
181213 PublisherIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
182214
183- Encoding . ASCII . GetChars ( record [ 446 ..574 ] , textBuffer ) ;
215+ encoding . GetChars ( record [ 446 ..574 ] , textBuffer ) ;
184216 DataPreparerIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
185217
186- Encoding . ASCII . GetChars ( record [ 574 ..702 ] , textBuffer ) ;
218+ encoding . GetChars ( record [ 574 ..702 ] , textBuffer ) ;
187219 ApplicationIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
188220
189221 textBuffer = new char [ 37 ] ;
190222
191- Encoding . ASCII . GetChars ( record [ 702 ..739 ] , textBuffer ) ;
223+ encoding . GetChars ( record [ 702 ..739 ] , textBuffer ) ;
192224 CopyrightFileIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
193225
194- Encoding . ASCII . GetChars ( record [ 739 ..776 ] , textBuffer ) ;
226+ encoding . GetChars ( record [ 739 ..776 ] , textBuffer ) ;
195227 AbstractFileIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
196228
197- Encoding . ASCII . GetChars ( record [ 776 ..813 ] , textBuffer ) ;
229+ encoding . GetChars ( record [ 776 ..813 ] , textBuffer ) ;
198230 BibliographicFileIdentifier = textBuffer . AsSpan ( ) . TrimEnd ( trimCharacters ) . ToString ( ) ;
199231
200232 textBuffer = new char [ 17 ] ;
0 commit comments