Skip to content

Commit

Permalink
Merge pull request #7 from glenebob/encoding
Browse files Browse the repository at this point in the history
Encoding, Cleanup
  • Loading branch information
LTRData authored Mar 6, 2024
2 parents c544553 + c3dc159 commit 0065318
Show file tree
Hide file tree
Showing 53 changed files with 398 additions and 314 deletions.
6 changes: 4 additions & 2 deletions Library/DiscUtils.Core/ApplePartitionMap/PartitionMapEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ public int Size

public int ReadFrom(ReadOnlySpan<byte> buffer)
{
var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

Signature = EndianUtilities.ToUInt16BigEndian(buffer);
MapEntries = EndianUtilities.ToUInt32BigEndian(buffer.Slice(4));
PhysicalBlockStart = EndianUtilities.ToUInt32BigEndian(buffer.Slice(8));
PhysicalBlocks = EndianUtilities.ToUInt32BigEndian(buffer.Slice(12));
Name = EndianUtilities.BytesToString(buffer.Slice(16, 32)).TrimEnd('\0');
Type = EndianUtilities.BytesToString(buffer.Slice(48, 32)).TrimEnd('\0');
Name = latin1Encoding.GetString(buffer.Slice(16, 32)).TrimEnd('\0');
Type = latin1Encoding.GetString(buffer.Slice(48, 32)).TrimEnd('\0');
LogicalBlockStart = EndianUtilities.ToUInt32BigEndian(buffer.Slice(80));
LogicalBlocks = EndianUtilities.ToUInt32BigEndian(buffer.Slice(84));
Flags = EndianUtilities.ToUInt32BigEndian(buffer.Slice(88));
Expand Down
16 changes: 11 additions & 5 deletions Library/DiscUtils.Core/Archives/TarFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
// DEALINGS IN THE SOFTWARE.
//

namespace DiscUtils.Archives;

using LTRData.Extensions.Buffers;
using Streams;
using DiscUtils.Streams;
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;

namespace DiscUtils.Archives;

/// <summary>
/// Minimal tar file format implementation.
/// </summary>
Expand Down Expand Up @@ -75,7 +75,11 @@ public TarFile(Stream fileStream)
try
{
_fileStream.ReadExactly(buffer, 0, (int)hdr.FileLength);
long_path = EndianUtilities.BytesToString(TarHeader.ReadNullTerminatedString(buffer.AsSpan(0, (int)hdr.FileLength)));

long_path = EncodingUtilities
.GetLatin1Encoding()
.GetString(TarHeader.ReadNullTerminatedString(buffer.AsSpan(0, (int)hdr.FileLength)));

_fileStream.Position += -(buffer.Length & 511) & 511;
}
finally
Expand Down Expand Up @@ -225,7 +229,9 @@ public static IEnumerable<TarFileData> EnumerateFiles(Stream archive)
{
archive.ReadExactly(data, 0, (int)hdr.FileLength);

long_path = EndianUtilities.BytesToString(TarHeader.ReadNullTerminatedString(data.AsSpan(0, (int)hdr.FileLength)));
long_path = EncodingUtilities
.GetLatin1Encoding()
.GetString(TarHeader.ReadNullTerminatedString(data.AsSpan(0, (int)hdr.FileLength)));
}
finally
{
Expand Down
42 changes: 22 additions & 20 deletions Library/DiscUtils.Core/Archives/TarHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
// DEALINGS IN THE SOFTWARE.
//

namespace DiscUtils.Archives;

using Streams;
using System;
using System.Linq;
using DiscUtils.Streams;

namespace DiscUtils.Archives;

public sealed class TarHeader
{
Expand Down Expand Up @@ -69,28 +68,29 @@ public TarHeader(byte[] buffer, int offset)

public TarHeader(ReadOnlySpan<byte> buffer)
{
FileName = EndianUtilities.BytesToString(ReadNullTerminatedString(buffer.Slice(0, 100)));
var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

FileName = latin1Encoding.GetString(ReadNullTerminatedString(buffer.Slice(0, 100)));
FileMode = (UnixFilePermissions)OctalToLong(ReadNullTerminatedString(buffer.Slice(100, 8)));
OwnerId = (int)OctalToLong(ReadNullTerminatedString(buffer.Slice(108, 8)));
GroupId = (int)OctalToLong(ReadNullTerminatedString(buffer.Slice(116, 8)));
FileLength = ParseFileLength(buffer.Slice(124, 12));
ModificationTime = DateTimeOffset.FromUnixTimeSeconds((uint)OctalToLong(ReadNullTerminatedString(buffer.Slice(136, 12))));
CheckSum = (int)OctalToLong(ReadNullTerminatedString(buffer.Slice(148, 8)));
FileType = (UnixFileType)buffer[156];
LinkName = EndianUtilities.BytesToString(ReadNullTerminatedString(buffer.Slice(157, 100)));
Magic = EndianUtilities.BytesToString(ReadNullTerminatedString(buffer.Slice(257, 6)));
LinkName = latin1Encoding.GetString(ReadNullTerminatedString(buffer.Slice(157, 100)));
Magic = latin1Encoding.GetString(ReadNullTerminatedString(buffer.Slice(257, 6)));
Version = (int)OctalToLong(ReadNullTerminatedString(buffer.Slice(263, 2)));
OwnerName = EndianUtilities.BytesToString(ReadNullTerminatedString(buffer.Slice(265, 32)));
GroupName = EndianUtilities.BytesToString(ReadNullTerminatedString(buffer.Slice(297, 32)));
OwnerName = latin1Encoding.GetString(ReadNullTerminatedString(buffer.Slice(265, 32)));
GroupName = latin1Encoding.GetString(ReadNullTerminatedString(buffer.Slice(297, 32)));
DevMajor = (int)OctalToLong(ReadNullTerminatedString(buffer.Slice(329, 8)));
DevMinor = (int)OctalToLong(ReadNullTerminatedString(buffer.Slice(337, 8)));

var prefix = ReadNullTerminatedString(buffer.Slice(345, 131));

if (!prefix.IsEmpty)
{
var parentPath = EndianUtilities.BytesToString(prefix);
FileName = $"{parentPath}/{FileName}";
FileName = $"{latin1Encoding.GetString(prefix)}/{FileName}";
}

LastAccessTime = DateTimeOffset.FromUnixTimeSeconds((uint)OctalToLong(ReadNullTerminatedString(buffer.Slice(476, 12))));
Expand Down Expand Up @@ -153,9 +153,11 @@ public void WriteTo(Span<byte> buffer)
{
buffer.Clear();

var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

if (FileName.Length < 100)
{
EndianUtilities.StringToBytes(FileName, buffer.Slice(0, 99));
latin1Encoding.GetBytes(FileName, buffer.Slice(0, 99));
}
else
{
Expand All @@ -167,17 +169,17 @@ public void WriteTo(Span<byte> buffer)
}

var prefix = FileName.AsSpan(0, split);
EndianUtilities.StringToBytes(prefix, buffer.Slice(345, 130));
latin1Encoding.GetBytes(prefix, buffer.Slice(345, 130));

var filename = FileName.AsSpan(split + 1);
EndianUtilities.StringToBytes(filename, buffer.Slice(0, 99));
latin1Encoding.GetBytes(filename, buffer.Slice(0, 99));
}

EndianUtilities.StringToBytes(LongToOctal((long)FileMode, 7), buffer.Slice(100, 7));
EndianUtilities.StringToBytes(LongToOctal(OwnerId, 7), buffer.Slice(108, 7));
EndianUtilities.StringToBytes(LongToOctal(GroupId, 7), buffer.Slice(116, 7));
EndianUtilities.StringToBytes(LongToOctal(FileLength, 11), buffer.Slice(124, 11));
EndianUtilities.StringToBytes(LongToOctal(ModificationTime.ToUnixTimeSeconds(), 11), buffer.Slice(136, 11));
latin1Encoding.GetBytes(LongToOctal((long)FileMode, 7), buffer.Slice(100, 7));
latin1Encoding.GetBytes(LongToOctal(OwnerId, 7), buffer.Slice(108, 7));
latin1Encoding.GetBytes(LongToOctal(GroupId, 7), buffer.Slice(116, 7));
latin1Encoding.GetBytes(LongToOctal(FileLength, 11), buffer.Slice(124, 11));
latin1Encoding.GetBytes(LongToOctal(ModificationTime.ToUnixTimeSeconds(), 11), buffer.Slice(136, 11));

buffer.Slice(148, 8).Fill((byte)' ');

Expand All @@ -189,7 +191,7 @@ public void WriteTo(Span<byte> buffer)
checkSum += buffer[i];
}

EndianUtilities.StringToBytes(LongToOctal(checkSum, 7), buffer.Slice(148, 7));
latin1Encoding.GetBytes(LongToOctal(checkSum, 7), buffer.Slice(148, 7));
buffer[155] = 0;
}

Expand Down
8 changes: 5 additions & 3 deletions Library/DiscUtils.Core/LogicalDiskManager/DatabaseHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ internal class DatabaseHeader

public void ReadFrom(ReadOnlySpan<byte> buffer)
{
Signature = EndianUtilities.BytesToString(buffer.Slice(0x00, 4));
var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

Signature = latin1Encoding.GetString(buffer.Slice(0x00, 4));
NumVBlks = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x04));
BlockSize = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x08));
HeaderSize = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x0C));
Unknown1 = EndianUtilities.ToUInt16BigEndian(buffer.Slice(0x10));
VersionNum = EndianUtilities.ToUInt16BigEndian(buffer.Slice(0x12));
VersionDenom = EndianUtilities.ToUInt16BigEndian(buffer.Slice(0x14));
GroupName = EndianUtilities.BytesToString(buffer.Slice(0x16, 31)).Trim('\0');
DiskGroupId = EndianUtilities.BytesToString(buffer.Slice(0x35, 0x40)).Trim('\0');
GroupName = latin1Encoding.GetString(buffer.Slice(0x16, 31)).Trim('\0');
DiskGroupId = latin1Encoding.GetString(buffer.Slice(0x35, 0x40)).Trim('\0');

// May be wrong way round...
CommittedSequence = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x75));
Expand Down
14 changes: 11 additions & 3 deletions Library/DiscUtils.Core/LogicalDiskManager/DatabaseRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ protected static string ReadVarString(byte[] buffer, ref int offset)
{
int length = buffer[offset];

var result = EndianUtilities.BytesToString(buffer, offset + 1, length);
var result = EncodingUtilities
.GetLatin1Encoding()
.GetString(buffer, offset + 1, length);

offset += length + 1;
return result;
}
Expand Down Expand Up @@ -116,7 +119,10 @@ protected static ulong ReadULong(byte[] buffer, ref int offset)
protected static string ReadString(byte[] buffer, int len, ref int offset)
{
offset += len;
return EndianUtilities.BytesToString(buffer, offset - len, len);

return EncodingUtilities
.GetLatin1Encoding()
.GetString(buffer, offset - len, len);
}

protected static Guid ReadBinaryGuid(byte[] buffer, ref int offset)
Expand All @@ -129,7 +135,9 @@ protected static Guid ReadBinaryGuid(byte[] buffer, ref int offset)

protected virtual void DoReadFrom(ReadOnlySpan<byte> buffer)
{
Signature = EndianUtilities.BytesToString(buffer.Slice(0x00, 4));
var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

Signature = latin1Encoding.GetString(buffer.Slice(0x00, 4));
Label = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x04));
Counter = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x08));
Valid = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x0C));
Expand Down
12 changes: 7 additions & 5 deletions Library/DiscUtils.Core/LogicalDiskManager/PrivateHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,19 @@ internal class PrivateHeader

public void ReadFrom(ReadOnlySpan<byte> buffer)
{
Signature = EndianUtilities.BytesToString(buffer.Slice(0x00, 8));
var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

Signature = latin1Encoding.GetString(buffer.Slice(0x00, 8));
Checksum = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x08));
Version = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x0C));
Timestamp = DateTime.FromFileTimeUtc(EndianUtilities.ToInt64BigEndian(buffer.Slice(0x10)));
Unknown2 = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x18));
Unknown3 = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x20));
Unknown4 = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x28));
DiskId = EndianUtilities.BytesToString(buffer.Slice(0x30, 0x40)).Trim('\0');
HostId = EndianUtilities.BytesToString(buffer.Slice(0x70, 0x40)).Trim('\0');
DiskGroupId = EndianUtilities.BytesToString(buffer.Slice(0xB0, 0x40)).Trim('\0');
DiskGroupName = EndianUtilities.BytesToString(buffer.Slice(0xF0, 31)).Trim('\0');
DiskId = latin1Encoding.GetString(buffer.Slice(0x30, 0x40)).Trim('\0');
HostId = latin1Encoding.GetString(buffer.Slice(0x70, 0x40)).Trim('\0');
DiskGroupId = latin1Encoding.GetString(buffer.Slice(0xB0, 0x40)).Trim('\0');
DiskGroupName = latin1Encoding.GetString(buffer.Slice(0xF0, 31)).Trim('\0');
Unknown5 = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x10F));
DataStartLba = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x11B));
DataSizeLba = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x123));
Expand Down
10 changes: 6 additions & 4 deletions Library/DiscUtils.Core/LogicalDiskManager/TocBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
// DEALINGS IN THE SOFTWARE.
//

using DiscUtils.Streams;
using System;
using DiscUtils.Streams;

namespace DiscUtils.LogicalDiskManager;

Expand All @@ -45,17 +45,19 @@ internal class TocBlock

public void ReadFrom(ReadOnlySpan<byte> buffer)
{
Signature = EndianUtilities.BytesToString(buffer.Slice(0x00, 8));
var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

Signature = latin1Encoding.GetString(buffer.Slice(0x00, 8));
Checksum = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x08));
SequenceNumber = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x0C));
Unknown1 = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x14));
Unknown2 = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x1C));
Item1Str = EndianUtilities.BytesToString(buffer.Slice(0x24, 10)).Trim('\0');
Item1Str = latin1Encoding.GetString(buffer.Slice(0x24, 10)).Trim('\0');
Item1Start = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x2E));
Item1Size = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x36));
Unknown3 = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x3E));
Unknown4 = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x42));
Item2Str = EndianUtilities.BytesToString(buffer.Slice(0x46, 10)).Trim('\0');
Item2Str = latin1Encoding.GetString(buffer.Slice(0x46, 10)).Trim('\0');
Item2Start = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x50));
Item2Size = EndianUtilities.ToInt64BigEndian(buffer.Slice(0x58));
Unknown5 = EndianUtilities.ToUInt32BigEndian(buffer.Slice(0x60));
Expand Down
9 changes: 7 additions & 2 deletions Library/DiscUtils.Core/Partitions/GptHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ public GptHeader(GptHeader toCopy)

public bool ReadFrom(ReadOnlySpan<byte> buffer)
{
Signature = EndianUtilities.BytesToString(buffer.Slice(0, 8));
var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

Signature = latin1Encoding.GetString(buffer.Slice(0, 8));
Version = EndianUtilities.ToUInt32LittleEndian(buffer.Slice(8));
HeaderSize = EndianUtilities.ToInt32LittleEndian(buffer.Slice(12));
Crc = EndianUtilities.ToUInt32LittleEndian(buffer.Slice(16));
Expand Down Expand Up @@ -111,7 +113,10 @@ public void WriteTo(Span<byte> buffer)
Buffer.CopyTo(buffer);

// Next, write the fields
EndianUtilities.StringToBytes(Signature, buffer.Slice(0, 8));
EncodingUtilities
.GetLatin1Encoding()
.GetBytes(Signature, buffer.Slice(0, 8));

EndianUtilities.WriteBytesLittleEndian(Version, buffer.Slice(8));
EndianUtilities.WriteBytesLittleEndian(HeaderSize, buffer.Slice(12));
EndianUtilities.WriteBytesLittleEndian((uint)0, buffer.Slice(16));
Expand Down
12 changes: 8 additions & 4 deletions Library/DiscUtils.Fat/FatFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,9 @@ private static void WriteBPB(
bootSector[2] = 0x90;

// OEM Name
EndianUtilities.StringToBytes("DISCUTIL", bootSector.Slice(3, 8));
EncodingUtilities
.GetLatin1Encoding()
.GetBytes("DISCUTIL", bootSector.Slice(3, 8));

// Bytes Per Sector (512)
bootSector[11] = 0;
Expand Down Expand Up @@ -1610,11 +1612,13 @@ private static void WriteBS(Span<byte> bootSector, bool isFloppy, uint volId, st
// Volume Id
EndianUtilities.WriteBytesLittleEndian(volId, bootSector.Slice(3));

var encoding = EncodingUtilities.GetLatin1Encoding();

// Volume Label
EndianUtilities.StringToBytes(label.PadRight(11, ' ').AsSpan(0, 11), bootSector.Slice(7, 11));
encoding.GetBytes(label.PadRight(11, ' ').AsSpan(0, 11), bootSector.Slice(7, 11));

// File System Type
EndianUtilities.StringToBytes(fsType, bootSector.Slice(18, 8));
encoding.GetBytes(fsType, bootSector.Slice(18, 8));
}

private static FatType DetectFATType(byte[] bpb)
Expand Down Expand Up @@ -1927,7 +1931,7 @@ public static FatFileSystem FormatFloppy(Stream stream, FloppyDiskType type, str
}
else
{
throw new ArgumentException("Unrecognised Floppy Disk type", nameof(type));
throw new ArgumentException("Unrecognized Floppy Disk type", nameof(type));
}

stream.Write(bpb);
Expand Down
3 changes: 2 additions & 1 deletion Library/DiscUtils.Fat/FileName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Runtime.InteropServices;
using System.Text;
using DiscUtils.Internal;
using DiscUtils.Streams;
using DiscUtils.Streams.Compatibility;

namespace DiscUtils.Fat;
Expand Down Expand Up @@ -105,7 +106,7 @@ public FileName(string name, Encoding encoding)

Span<byte> bytes = stackalloc byte[encoding.GetByteCount(name)];

encoding.GetBytes(name.AsSpan(), bytes);
encoding.GetBytes(name, bytes);

if (bytes.Length == 0)
{
Expand Down
9 changes: 5 additions & 4 deletions Library/DiscUtils.Iscsi/ScsiInquiryStandardResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,13 @@ public override void ReadFrom(byte[] buffer, int offset, int count)
return;
}

var latin1Encoding = EncodingUtilities.GetLatin1Encoding();

DeviceType = (LunClass)(buffer[0] & 0x1F);
Removable = (buffer[1] & 0x80) != 0;
SpecificationVersion = buffer[2];

VendorId = EndianUtilities.BytesToString(buffer, 8, 8);
ProductId = EndianUtilities.BytesToString(buffer, 16, 16);
ProductRevision = EndianUtilities.BytesToString(buffer, 32, 4);
VendorId = latin1Encoding.GetString(buffer, 8, 8);
ProductId = latin1Encoding.GetString(buffer, 16, 16);
ProductRevision = latin1Encoding.GetString(buffer, 32, 4);
}
}
2 changes: 1 addition & 1 deletion Library/DiscUtils.Iso9660/BaseVolumeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
// DEALINGS IN THE SOFTWARE.
//

using DiscUtils.Streams.Compatibility;
using System;
using System.Text;
using DiscUtils.Streams;

namespace DiscUtils.Iso9660;

Expand Down
Loading

0 comments on commit 0065318

Please sign in to comment.