Changed formatting (for the last time)

This commit is contained in:
StudentBlake 2019-04-14 13:17:26 -04:00
parent 5ee76c270d
commit ee752c1e3c
26 changed files with 855 additions and 430 deletions

View file

@ -2,7 +2,6 @@ using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
[assembly: CompilationRelaxations(8)]
[assembly: RuntimeCompatibility(WrapNonExceptionThrows = true)]

View file

@ -1,30 +1,36 @@
using System;
using System.IO;
namespace XCI_Explorer.Helpers {
public class BetterBinaryReader : IDisposable {
namespace XCI_Explorer.Helpers
{
public class BetterBinaryReader : IDisposable
{
public string FileName;
public bool Initiated;
public Stream Stream;
private BinaryReader br;
public BetterBinaryReader() {
public BetterBinaryReader()
{
Initiated = false;
}
public BetterBinaryReader(string file) {
public BetterBinaryReader(string file)
{
Initiated = false;
Load(file);
}
public BetterBinaryReader(Stream s) {
public BetterBinaryReader(Stream s)
{
Initiated = false;
FileName = "";
Stream = s;
br = new BinaryReader(Stream);
}
public void Dispose() {
public void Dispose()
{
Initiated = false;
br.Close();
br = null;
@ -32,90 +38,113 @@ namespace XCI_Explorer.Helpers {
Stream = null;
}
public void Load(string file) {
public void Load(string file)
{
FileName = file;
Stream = new FileStream(file, FileMode.Open);
br = new BinaryReader(Stream);
Initiated = true;
}
public void Seek(long o) {
if (o > -1) {
public void Seek(long o)
{
if (o > -1)
{
Stream.Seek(o, SeekOrigin.Begin);
}
}
public void Skip(long o) {
public void Skip(long o)
{
Stream.Seek(o, SeekOrigin.Current);
}
public long Position() {
public long Position()
{
return Stream.Position;
}
public int Read() {
public int Read()
{
return br.ReadBytes(1)[0];
}
public int Read(byte[] buffer, int index, int count) {
public int Read(byte[] buffer, int index, int count)
{
return br.Read(buffer, index, count);
}
public int Read(char[] buffer, int index, int count) {
public int Read(char[] buffer, int index, int count)
{
return br.Read(buffer, index, count);
}
public byte[] ReadBytes(int l) {
if (l >= 0 && l <= 2147483647) {
public byte[] ReadBytes(int l)
{
if (l >= 0 && l <= 2147483647)
{
return br.ReadBytes(l);
}
return new byte[0];
}
public Stream ReadBytesButLonger(long l) {
public Stream ReadBytesButLonger(long l)
{
MemoryStream memoryStream = new MemoryStream();
for (long num = 0L; num < l; num++) {
for (long num = 0L; num < l; num++)
{
}
Console.WriteLine(memoryStream.Length);
return memoryStream;
}
public string ReadCharsAsString(int l) {
public string ReadCharsAsString(int l)
{
return new string(br.ReadChars(l));
}
public short ReadShort() {
public short ReadShort()
{
return br.ReadInt16();
}
public short ReadInt16() {
public short ReadInt16()
{
return br.ReadInt16();
}
public int ReadInt() {
public int ReadInt()
{
return br.ReadInt32();
}
public int ReadInt32() {
public int ReadInt32()
{
return br.ReadInt32();
}
public long ReadLong() {
public long ReadLong()
{
return br.ReadInt64();
}
public long ReadInt64() {
public long ReadInt64()
{
return br.ReadInt64();
}
public string ReadString() {
public string ReadString()
{
return br.ReadString();
}
private long GreatestDivisor(long n) {
private long GreatestDivisor(long n)
{
long result = 0L;
for (long num = 1L; num < n / 64; num++) {
if (n % num == 0L && num != n) {
for (long num = 1L; num < n / 64; num++)
{
if (n % num == 0L && num != n)
{
result = num;
}
}

View file

@ -1,14 +1,17 @@
using System.Windows.Forms;
namespace XCI_Explorer.Helpers {
public class BetterTreeNode : TreeNode {
namespace XCI_Explorer.Helpers
{
public class BetterTreeNode : TreeNode
{
public long Offset;
public long Size;
public string ExpectedHash;
public string ActualHash;
public long HashedRegionSize;
public BetterTreeNode(string t) {
public BetterTreeNode(string t)
{
base.Text = t;
}
}

View file

@ -5,11 +5,13 @@ using System.Globalization;
using System.Resources;
using System.Runtime.CompilerServices;
namespace XCI_Explorer.Properties {
namespace XCI_Explorer.Properties
{
[GeneratedCode("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[DebuggerNonUserCode]
[CompilerGenerated]
internal class Resources {
internal class Resources
{
private static ResourceManager resourceMan;
private static CultureInfo resourceCulture;
@ -17,7 +19,8 @@ namespace XCI_Explorer.Properties {
[EditorBrowsable(EditorBrowsableState.Advanced)]
internal static ResourceManager ResourceManager {
get {
if (resourceMan == null) {
if (resourceMan == null)
{
resourceMan = new ResourceManager("XCI_Explorer.Properties.Resources", typeof(Resources).Assembly);
}
return resourceMan;
@ -34,7 +37,8 @@ namespace XCI_Explorer.Properties {
}
}
internal Resources() {
internal Resources()
{
}
}
}

View file

@ -2,10 +2,12 @@ using System.CodeDom.Compiler;
using System.Configuration;
using System.Runtime.CompilerServices;
namespace XCI_Explorer.Properties {
namespace XCI_Explorer.Properties
{
[CompilerGenerated]
[GeneratedCode("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed class Settings : ApplicationSettingsBase {
internal sealed class Settings : ApplicationSettingsBase
{
private static Settings defaultInstance = (Settings)SettingsBase.Synchronized(new Settings());
public static Settings Default => defaultInstance;

View file

@ -1,10 +1,12 @@
using System;
using System.Linq;
using System.Text;
namespace XCI_Explorer {
internal static class CNMT {
public class CNMT_Header {
namespace XCI_Explorer
{
internal static class CNMT
{
public class CNMT_Header
{
public byte[] Data;
public long TitleID;
public int TitleVersion;
@ -15,7 +17,8 @@ namespace XCI_Explorer {
public short MetaCount;
public byte[] Reserved2;
public enum TitleType {
public enum TitleType
{
SYSTEM_PROGRAMS = 0x01,
SYSTEM_DATA_ARCHIVES,
SYSTEM_UPDATE,
@ -27,7 +30,8 @@ namespace XCI_Explorer {
DELTA_TITLE
}
public CNMT_Header(byte[] data) {
public CNMT_Header(byte[] data)
{
Data = data;
TitleID = BitConverter.ToInt64(data, 0);
TitleVersion = BitConverter.ToInt32(data, 8);
@ -40,7 +44,8 @@ namespace XCI_Explorer {
}
}
public class CNMT_Entry {
public class CNMT_Entry
{
public byte[] Data;
public byte[] Hash;
public byte[] NcaId;
@ -48,7 +53,8 @@ namespace XCI_Explorer {
public byte Type;
public byte Reserved;
public enum ContentType {
public enum ContentType
{
META,
PROGRAM,
DATA,
@ -58,7 +64,8 @@ namespace XCI_Explorer {
GAME_UPDATE
}
public CNMT_Entry(byte[] data) {
public CNMT_Entry(byte[] data)
{
Data = data;
Hash = Data.Skip(0).Take(32).ToArray();
NcaId = Data.Skip(32).Take(16).ToArray();

View file

@ -3,8 +3,10 @@ using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace XCI_Explorer {
partial class CertForm {
namespace XCI_Explorer
{
partial class CertForm
{
/// <summary>
/// Required designer variable.
/// </summary>
@ -15,8 +17,10 @@ namespace XCI_Explorer {
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
@ -28,7 +32,8 @@ namespace XCI_Explorer {
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
private void InitializeComponent()
{
hbxHexView = new HexBox();
SuspendLayout();
hbxHexView.Dock = DockStyle.Fill;

View file

@ -2,9 +2,12 @@
using System.IO;
using System.Windows.Forms;
namespace XCI_Explorer {
public partial class CertForm : Form {
public CertForm(MainForm mainForm) {
namespace XCI_Explorer
{
public partial class CertForm : Form
{
public CertForm(MainForm mainForm)
{
InitializeComponent();
FileStream fileStream = new FileStream(mainForm.TB_File.Text, FileMode.Open, FileAccess.Read);
byte[] array = new byte[512];

View file

@ -2,16 +2,20 @@ using System;
using System.Linq;
using System.Text;
namespace XCI_Explorer {
internal static class HFS0 {
public class HFS0_Header {
namespace XCI_Explorer
{
internal static class HFS0
{
public class HFS0_Header
{
public byte[] Data;
public string Magic;
public int FileCount;
public int StringTableSize;
public int Reserved;
public HFS0_Header(byte[] data) {
public HFS0_Header(byte[] data)
{
Data = data;
Magic = Encoding.UTF8.GetString(Data.Take(4).ToArray());
FileCount = BitConverter.ToInt32(data, 4);
@ -20,7 +24,8 @@ namespace XCI_Explorer {
}
}
public class HSF0_Entry {
public class HSF0_Entry
{
public byte[] Data;
public long Offset;
public long Size;
@ -30,7 +35,8 @@ namespace XCI_Explorer {
public byte[] Hash;
public string Name;
public HSF0_Entry(byte[] data) {
public HSF0_Entry(byte[] data)
{
Data = data;
Offset = BitConverter.ToInt64(data, 0);
Size = BitConverter.ToInt64(data, 8);

View file

@ -2,8 +2,10 @@
using System.Windows.Forms;
using XCI_Explorer.Helpers;
namespace XCI_Explorer {
partial class MainForm {
namespace XCI_Explorer
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
@ -13,8 +15,10 @@ namespace XCI_Explorer {
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
@ -26,7 +30,8 @@ namespace XCI_Explorer {
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
private void InitializeComponent()
{
this.B_LoadROM = new System.Windows.Forms.Button();
this.TB_File = new System.Windows.Forms.TextBox();
this.TABC_Main = new System.Windows.Forms.TabControl();

File diff suppressed because it is too large Load diff

View file

@ -1,15 +1,19 @@
using System.Linq;
using System.Text;
namespace XCI_Explorer {
public static class NACP {
public class NACP_String {
namespace XCI_Explorer
{
public static class NACP
{
public class NACP_String
{
public byte[] Data;
public byte Check;
public string GameName;
public string GameAuthor;
public NACP_String(byte[] data) {
public NACP_String(byte[] data)
{
Data = data;
Check = Data[0];
GameName = Encoding.UTF8.GetString(Data.Take(512).ToArray());
@ -17,12 +21,14 @@ namespace XCI_Explorer {
}
}
public class NACP_Data {
public class NACP_Data
{
public byte[] Data;
public string GameVer;
public string GameProd;
public NACP_Data(byte[] data) {
public NACP_Data(byte[] data)
{
Data = data;
GameVer = Encoding.UTF8.GetString(Data.Skip(0x60).Take(16).ToArray());
GameProd = Encoding.UTF8.GetString(Data.Skip(0xA8).Take(8).ToArray());

View file

@ -2,9 +2,12 @@ using System;
using System.Linq;
using System.Text;
namespace XCI_Explorer {
internal static class NCA {
public class NCA_Header {
namespace XCI_Explorer
{
internal static class NCA
{
public class NCA_Header
{
public byte[] Data;
public string Magic;
public long TitleID;
@ -14,7 +17,8 @@ namespace XCI_Explorer {
public byte SDKVersion4;
public byte MasterKeyRev;
public NCA_Header(byte[] data) {
public NCA_Header(byte[] data)
{
Data = data;
Magic = Encoding.UTF8.GetString(Data.Skip(512).Take(4).ToArray());
TitleID = BitConverter.ToInt64(data, 528);

View file

@ -2,16 +2,20 @@ using System;
using System.Linq;
using System.Text;
namespace XCI_Explorer {
internal static class PFS0 {
public class PFS0_Header {
namespace XCI_Explorer
{
internal static class PFS0
{
public class PFS0_Header
{
public byte[] Data;
public string Magic;
public int FileCount;
public int StringTableSize;
public int Reserved;
public PFS0_Header(byte[] data) {
public PFS0_Header(byte[] data)
{
Data = data;
Magic = Encoding.UTF8.GetString(Data.Take(4).ToArray());
FileCount = BitConverter.ToInt32(data, 4);
@ -20,7 +24,8 @@ namespace XCI_Explorer {
}
}
public class PFS0_Entry {
public class PFS0_Entry
{
public byte[] Data;
public long Offset;
public long Size;
@ -28,7 +33,8 @@ namespace XCI_Explorer {
public int Reserved;
public string Name;
public PFS0_Entry(byte[] data) {
public PFS0_Entry(byte[] data)
{
Data = data;
Offset = BitConverter.ToInt64(data, 0);
Size = BitConverter.ToInt64(data, 8);

View file

@ -1,15 +1,20 @@
using System;
using System.Windows.Forms;
namespace XCI_Explorer {
internal static class Program {
namespace XCI_Explorer
{
internal static class Program
{
[STAThread]
private static void Main() {
AppDomain.CurrentDomain.AssemblyResolve += (Object sender, ResolveEventArgs args) => {
private static void Main()
{
AppDomain.CurrentDomain.AssemblyResolve += (Object sender, ResolveEventArgs args) =>
{
System.Reflection.AssemblyName embeddedAssembly = new System.Reflection.AssemblyName(args.Name);
String resourceName = "XCI_Explorer" + "." + embeddedAssembly.Name + ".dll";
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) {
using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
{
Byte[] assemblyData = new Byte[stream.Length];
stream.Read(assemblyData, 0, assemblyData.Length);
return System.Reflection.Assembly.Load(assemblyData);

View file

@ -1,14 +1,18 @@
using System.Windows.Forms;
using XCI_Explorer.Helpers;
namespace XCI_Explorer {
public class TreeViewFileSystem {
namespace XCI_Explorer
{
public class TreeViewFileSystem
{
public TreeView treeView;
public TreeViewFileSystem(TreeView tv) {
public TreeViewFileSystem(TreeView tv)
{
}
public BetterTreeNode AddDir(string name, BetterTreeNode parent = null) {
public BetterTreeNode AddDir(string name, BetterTreeNode parent = null)
{
BetterTreeNode betterTreeNode = new BetterTreeNode(name);
betterTreeNode.Offset = -1L;
betterTreeNode.Size = -1L;
@ -16,11 +20,13 @@ namespace XCI_Explorer {
return betterTreeNode;
}
public BetterTreeNode AddFile(string name, BetterTreeNode parent, long offset, long size) {
public BetterTreeNode AddFile(string name, BetterTreeNode parent, long offset, long size)
{
return AddFile(name, parent, offset, size, 0, "", "");
}
public BetterTreeNode AddFile(string name, BetterTreeNode parent, long offset, long size, long HashedRegionSize, string ExpectedHash, string ActualHash) {
public BetterTreeNode AddFile(string name, BetterTreeNode parent, long offset, long size, long HashedRegionSize, string ExpectedHash, string ActualHash)
{
BetterTreeNode betterTreeNode = new BetterTreeNode(name);
betterTreeNode.Offset = offset;
betterTreeNode.Size = size;

View file

@ -2,10 +2,14 @@ using System;
using System.IO;
using System.Linq;
namespace XCI_Explorer {
internal static class Util {
public static string GetCapacity(int id) {
switch (id) {
namespace XCI_Explorer
{
internal static class Util
{
public static string GetCapacity(int id)
{
switch (id)
{
case 248:
return "2GB";
case 240:
@ -21,8 +25,10 @@ namespace XCI_Explorer {
}
}
public static string GetMkey(byte id) {
switch (id) {
public static string GetMkey(byte id)
{
switch (id)
{
case 0:
case 1:
return "MasterKey0 (1.0.0-2.3.0)";
@ -95,25 +101,30 @@ namespace XCI_Explorer {
}
}
public static bool checkFile(string filepath) {
if (File.Exists(filepath)) {
public static bool checkFile(string filepath)
{
if (File.Exists(filepath))
{
return true;
}
return false;
}
public static byte[] StringToByteArray(string hex) {
public static byte[] StringToByteArray(string hex)
{
return (from x in Enumerable.Range(0, hex.Length)
where x % 2 == 0
select Convert.ToByte(hex.Substring(x, 2), 16)).ToArray();
}
public static string Base64Encode(string plainText) {
public static string Base64Encode(string plainText)
{
var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
return System.Convert.ToBase64String(plainTextBytes);
}
public static string Base64Decode(string base64EncodedData) {
public static string Base64Decode(string base64EncodedData)
{
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}

View file

@ -2,9 +2,12 @@ using System;
using System.Linq;
using System.Text;
namespace XCI_Explorer {
public static class XCI {
public class XCI_Header {
namespace XCI_Explorer
{
public static class XCI
{
public class XCI_Header
{
public byte[] Data;
public string Magic;
public byte CardSize1;
@ -12,7 +15,8 @@ namespace XCI_Explorer {
public long HFS0OffsetPartition;
public long HFS0SizeParition;
public XCI_Header(byte[] data) {
public XCI_Header(byte[] data)
{
Data = data;
Magic = Encoding.UTF8.GetString(Data.Skip(256).Take(4).ToArray());
CardSize1 = Data[269];

View file

@ -1,8 +1,10 @@
using System;
using System.IO;
namespace XTSSharp {
public class RandomAccessSectorStream : Stream {
namespace XTSSharp
{
public class RandomAccessSectorStream : Stream
{
private readonly byte[] _buffer;
private readonly int _bufferSize;
private readonly SectorStream _s;
@ -17,25 +19,30 @@ namespace XTSSharp {
public override long Position {
get {
if (!_bufferLoaded) {
if (!_bufferLoaded)
{
return _s.Position + _bufferPos;
}
return _s.Position - _bufferSize + _bufferPos;
}
set {
if (value < 0) {
if (value < 0)
{
throw new ArgumentOutOfRangeException("value");
}
long num = value % _bufferSize;
long position = value - num;
if (_bufferLoaded) {
if (_bufferLoaded)
{
long num2 = _s.Position - _bufferSize;
if (value > num2 && value < num2 + _bufferSize) {
if (value > num2 && value < num2 + _bufferSize)
{
_bufferPos = (int)num;
return;
}
}
if (_bufferDirty) {
if (_bufferDirty)
{
WriteSector();
}
_s.Position = position;
@ -45,33 +52,41 @@ namespace XTSSharp {
}
public RandomAccessSectorStream(SectorStream s)
: this(s, false) {
: this(s, false)
{
}
public RandomAccessSectorStream(SectorStream s, bool isStreamOwned) {
public RandomAccessSectorStream(SectorStream s, bool isStreamOwned)
{
_s = s;
_isStreamOwned = isStreamOwned;
_buffer = new byte[s.SectorSize];
_bufferSize = s.SectorSize;
}
protected override void Dispose(bool disposing) {
protected override void Dispose(bool disposing)
{
Flush();
base.Dispose(disposing);
if (_isStreamOwned) {
if (_isStreamOwned)
{
_s.Dispose();
}
}
public override void Flush() {
if (_bufferDirty) {
public override void Flush()
{
if (_bufferDirty)
{
WriteSector();
}
}
public override long Seek(long offset, SeekOrigin origin) {
public override long Seek(long offset, SeekOrigin origin)
{
long num;
switch (origin) {
switch (origin)
{
case SeekOrigin.Begin:
num = offset;
break;
@ -86,40 +101,50 @@ namespace XTSSharp {
return num;
}
public override void SetLength(long value) {
public override void SetLength(long value)
{
long num = value % _s.SectorSize;
if (num > 0) {
if (num > 0)
{
value = value - num + _bufferSize;
}
_s.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count) {
public override int Read(byte[] buffer, int offset, int count)
{
long position = Position;
if (position + count > _s.Length) {
if (position + count > _s.Length)
{
count = (int)(_s.Length - position);
}
if (!_bufferLoaded) {
if (!_bufferLoaded)
{
ReadSector();
}
int num = 0;
while (count > 0) {
while (count > 0)
{
int num2 = Math.Min(count, _bufferSize - _bufferPos);
Buffer.BlockCopy(_buffer, _bufferPos, buffer, offset, num2);
offset += num2;
_bufferPos += num2;
count -= num2;
num += num2;
if (_bufferPos == _bufferSize) {
if (_bufferPos == _bufferSize)
{
ReadSector();
}
}
return num;
}
public override void Write(byte[] buffer, int offset, int count) {
while (count > 0) {
if (!_bufferLoaded) {
public override void Write(byte[] buffer, int offset, int count)
{
while (count > 0)
{
if (!_bufferLoaded)
{
ReadSector();
}
int num = Math.Min(count, _bufferSize - _bufferPos);
@ -128,19 +153,24 @@ namespace XTSSharp {
_bufferPos += num;
count -= num;
_bufferDirty = true;
if (_bufferPos == _bufferSize) {
if (_bufferPos == _bufferSize)
{
WriteSector();
}
}
}
private void ReadSector() {
if (_bufferLoaded && _bufferDirty) {
private void ReadSector()
{
if (_bufferLoaded && _bufferDirty)
{
WriteSector();
}
if (_s.Position != _s.Length) {
if (_s.Position != _s.Length)
{
int num = _s.Read(_buffer, 0, _buffer.Length);
if (num != _bufferSize) {
if (num != _bufferSize)
{
Array.Clear(_buffer, num, _buffer.Length - num);
}
_bufferLoaded = true;
@ -149,8 +179,10 @@ namespace XTSSharp {
}
}
private void WriteSector() {
if (_bufferLoaded) {
private void WriteSector()
{
if (_bufferLoaded)
{
_s.Seek(-_bufferSize, SeekOrigin.Current);
}
_s.Write(_buffer, 0, _bufferSize);

View file

@ -1,8 +1,10 @@
using System;
using System.IO;
namespace XTSSharp {
public class SectorStream : Stream {
namespace XTSSharp
{
public class SectorStream : Stream
{
private readonly Stream _baseStream;
private readonly long _offset;
private ulong _currentSector;
@ -31,43 +33,54 @@ namespace XTSSharp {
protected ulong CurrentSector => _currentSector;
public SectorStream(Stream baseStream, int sectorSize)
: this(baseStream, sectorSize, 0L) {
: this(baseStream, sectorSize, 0L)
{
}
public SectorStream(Stream baseStream, int sectorSize, long offset) {
public SectorStream(Stream baseStream, int sectorSize, long offset)
{
SectorSize = sectorSize;
_baseStream = baseStream;
_offset = offset;
}
private void ValidateSizeMultiple(long value) {
if (value % SectorSize == 0L) {
private void ValidateSizeMultiple(long value)
{
if (value % SectorSize == 0L)
{
return;
}
throw new ArgumentException($"Value needs to be a multiple of {SectorSize}");
}
protected void ValidateSize(long value) {
if (value == SectorSize) {
protected void ValidateSize(long value)
{
if (value == SectorSize)
{
return;
}
throw new ArgumentException($"Value needs to be {SectorSize}");
}
protected void ValidateSize(int value) {
if (value == SectorSize) {
protected void ValidateSize(int value)
{
if (value == SectorSize)
{
return;
}
throw new ArgumentException($"Value needs to be {SectorSize}");
}
public override void Flush() {
public override void Flush()
{
_baseStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin) {
public override long Seek(long offset, SeekOrigin origin)
{
long num;
switch (origin) {
switch (origin)
{
case SeekOrigin.Begin:
num = offset;
break;
@ -82,19 +95,22 @@ namespace XTSSharp {
return num;
}
public override void SetLength(long value) {
public override void SetLength(long value)
{
ValidateSizeMultiple(value);
_baseStream.SetLength(value);
}
public override int Read(byte[] buffer, int offset, int count) {
public override int Read(byte[] buffer, int offset, int count)
{
ValidateSize(count);
int result = _baseStream.Read(buffer, offset, count);
_currentSector++;
return result;
}
public override void Write(byte[] buffer, int offset, int count) {
public override void Write(byte[] buffer, int offset, int count)
{
ValidateSize(count);
_baseStream.Write(buffer, offset, count);
_currentSector++;

View file

@ -1,24 +1,31 @@
using System;
using System.Security.Cryptography;
namespace XTSSharp {
public class Xts {
namespace XTSSharp
{
public class Xts
{
private readonly SymmetricAlgorithm _key1;
private readonly SymmetricAlgorithm _key2;
protected Xts(Func<SymmetricAlgorithm> create, byte[] key1, byte[] key2) {
if (create == null) {
protected Xts(Func<SymmetricAlgorithm> create, byte[] key1, byte[] key2)
{
if (create == null)
{
throw new ArgumentNullException("create");
}
if (key1 == null) {
if (key1 == null)
{
throw new ArgumentNullException("key1");
}
if (key2 == null) {
if (key2 == null)
{
throw new ArgumentNullException("key2");
}
_key1 = create();
_key2 = create();
if (key1.Length != key2.Length) {
if (key1.Length != key2.Length)
{
throw new ArgumentException("Key lengths don't match");
}
_key1.KeySize = key1.Length * 8;
@ -33,19 +40,24 @@ namespace XTSSharp {
_key2.BlockSize = 128;
}
public XtsCryptoTransform CreateEncryptor() {
public XtsCryptoTransform CreateEncryptor()
{
return new XtsCryptoTransform(_key1.CreateEncryptor(), _key2.CreateEncryptor(), false);
}
public XtsCryptoTransform CreateDecryptor() {
public XtsCryptoTransform CreateDecryptor()
{
return new XtsCryptoTransform(_key1.CreateDecryptor(), _key2.CreateEncryptor(), true);
}
protected static byte[] VerifyKey(int expectedSize, byte[] key) {
if (key == null) {
protected static byte[] VerifyKey(int expectedSize, byte[] key)
{
if (key == null)
{
throw new ArgumentNullException("key");
}
if (key.Length * 8 != expectedSize) {
if (key.Length * 8 != expectedSize)
{
throw new ArgumentException($"Expected key length of {expectedSize} bits, got {key.Length * 8}");
}
return key;

View file

@ -1,22 +1,27 @@
using System;
using System.Security.Cryptography;
namespace XTSSharp {
public class XtsAes128 : Xts {
namespace XTSSharp
{
public class XtsAes128 : Xts
{
private const int KEY_LENGTH = 128;
private const int KEY_BYTE_LENGTH = 16;
protected XtsAes128(Func<SymmetricAlgorithm> create, byte[] key1, byte[] key2)
: base(create, Xts.VerifyKey(128, key1), Xts.VerifyKey(128, key2)) {
: base(create, Xts.VerifyKey(128, key1), Xts.VerifyKey(128, key2))
{
}
public static Xts Create(byte[] key1, byte[] key2) {
public static Xts Create(byte[] key1, byte[] key2)
{
Xts.VerifyKey(128, key1);
Xts.VerifyKey(128, key2);
return new XtsAes128(Aes.Create, key1, key2);
}
public static Xts Create(byte[] key) {
public static Xts Create(byte[] key)
{
Xts.VerifyKey(256, key);
byte[] array = new byte[16];
byte[] array2 = new byte[16];

View file

@ -1,22 +1,27 @@
using System;
using System.Security.Cryptography;
namespace XTSSharp {
public class XtsAes256 : Xts {
namespace XTSSharp
{
public class XtsAes256 : Xts
{
private const int KEY_LENGTH = 256;
private const int KEY_BYTE_LENGTH = 32;
protected XtsAes256(Func<SymmetricAlgorithm> create, byte[] key1, byte[] key2)
: base(create, Xts.VerifyKey(256, key1), Xts.VerifyKey(256, key2)) {
: base(create, Xts.VerifyKey(256, key1), Xts.VerifyKey(256, key2))
{
}
public static Xts Create(byte[] key1, byte[] key2) {
public static Xts Create(byte[] key1, byte[] key2)
{
Xts.VerifyKey(256, key1);
Xts.VerifyKey(256, key2);
return new XtsAes256(Aes.Create, key1, key2);
}
public static Xts Create(byte[] key) {
public static Xts Create(byte[] key)
{
Xts.VerifyKey(512, key);
byte[] array = new byte[32];
byte[] array2 = new byte[32];

View file

@ -1,8 +1,10 @@
using System;
using System.Security.Cryptography;
namespace XTSSharp {
public class XtsCryptoTransform : IDisposable {
namespace XTSSharp
{
public class XtsCryptoTransform : IDisposable
{
private readonly byte[] _cc = new byte[16];
private readonly bool _decrypting;
private readonly ICryptoTransform _key1;
@ -11,11 +13,14 @@ namespace XTSSharp {
private readonly byte[] _t = new byte[16];
private readonly byte[] _tweak = new byte[16];
public XtsCryptoTransform(ICryptoTransform key1, ICryptoTransform key2, bool decrypting) {
if (key1 == null) {
public XtsCryptoTransform(ICryptoTransform key1, ICryptoTransform key2, bool decrypting)
{
if (key1 == null)
{
throw new ArgumentNullException("key1");
}
if (key2 == null) {
if (key2 == null)
{
throw new ArgumentNullException("key2");
}
_key1 = key1;
@ -23,45 +28,55 @@ namespace XTSSharp {
_decrypting = decrypting;
}
public void Dispose() {
public void Dispose()
{
_key1.Dispose();
_key2.Dispose();
}
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset, ulong sector) {
public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset, ulong sector)
{
FillArrayFromSectorLittleEndian(_tweak, sector);
int num = inputCount >> 4;
int num2 = inputCount & 0xF;
_key2.TransformBlock(_tweak, 0, _tweak.Length, _t, 0);
int num3 = (num2 != 0) ? (num - 1) : num;
for (int i = 0; i < num3; i++) {
for (int i = 0; i < num3; i++)
{
TweakCrypt(inputBuffer, inputOffset, outputBuffer, outputOffset, _t);
inputOffset += 16;
outputOffset += 16;
}
if (num2 > 0) {
if (_decrypting) {
if (num2 > 0)
{
if (_decrypting)
{
Buffer.BlockCopy(_t, 0, _cc, 0, 16);
MultiplyByX(_cc);
TweakCrypt(inputBuffer, inputOffset, _pp, 0, _cc);
int j;
for (j = 0; j < num2; j++) {
for (j = 0; j < num2; j++)
{
_cc[j] = inputBuffer[16 + j + inputOffset];
outputBuffer[16 + j + outputOffset] = _pp[j];
}
for (; j < 16; j++) {
for (; j < 16; j++)
{
_cc[j] = _pp[j];
}
TweakCrypt(_cc, 0, outputBuffer, outputOffset, _t);
}
else {
else
{
TweakCrypt(inputBuffer, inputOffset, _cc, 0, _t);
int k;
for (k = 0; k < num2; k++) {
for (k = 0; k < num2; k++)
{
_pp[k] = inputBuffer[16 + k + inputOffset];
outputBuffer[16 + k + outputOffset] = _cc[k];
}
for (; k < 16; k++) {
for (; k < 16; k++)
{
_pp[k] = _cc[k];
}
TweakCrypt(_pp, 0, outputBuffer, outputOffset, _t);
@ -70,7 +85,8 @@ namespace XTSSharp {
return inputCount;
}
private static void FillArrayFromSectorBigEndian(byte[] value, ulong sector) {
private static void FillArrayFromSectorBigEndian(byte[] value, ulong sector)
{
value[7] = (byte)((sector >> 56) & 0xFF);
value[6] = (byte)((sector >> 48) & 0xFF);
value[5] = (byte)((sector >> 40) & 0xFF);
@ -81,7 +97,8 @@ namespace XTSSharp {
value[0] = (byte)(sector & 0xFF);
}
private static void FillArrayFromSectorLittleEndian(byte[] value, ulong sector) {
private static void FillArrayFromSectorLittleEndian(byte[] value, ulong sector)
{
value[8] = (byte)((sector >> 56) & 0xFF);
value[9] = (byte)((sector >> 48) & 0xFF);
value[10] = (byte)((sector >> 40) & 0xFF);
@ -92,26 +109,32 @@ namespace XTSSharp {
value[15] = (byte)(sector & 0xFF);
}
private void TweakCrypt(byte[] inputBuffer, int inputOffset, byte[] outputBuffer, int outputOffset, byte[] t) {
for (int i = 0; i < 16; i++) {
private void TweakCrypt(byte[] inputBuffer, int inputOffset, byte[] outputBuffer, int outputOffset, byte[] t)
{
for (int i = 0; i < 16; i++)
{
outputBuffer[i + outputOffset] = (byte)(inputBuffer[i + inputOffset] ^ t[i]);
}
_key1.TransformBlock(outputBuffer, outputOffset, 16, outputBuffer, outputOffset);
for (int j = 0; j < 16; j++) {
for (int j = 0; j < 16; j++)
{
outputBuffer[j + outputOffset] = (byte)(outputBuffer[j + outputOffset] ^ t[j]);
}
MultiplyByX(t);
}
private static void MultiplyByX(byte[] i) {
private static void MultiplyByX(byte[] i)
{
byte b = 0;
byte b2 = 0;
for (int j = 0; j < 16; j++) {
for (int j = 0; j < 16; j++)
{
b2 = (byte)(i[j] >> 7);
i[j] = (byte)(((i[j] << 1) | b) & 0xFF);
b = b2;
}
if (b2 > 0) {
if (b2 > 0)
{
i[0] ^= 135;
}
}

View file

@ -1,7 +1,9 @@
using System.IO;
namespace XTSSharp {
public class XtsSectorStream : SectorStream {
namespace XTSSharp
{
public class XtsSectorStream : SectorStream
{
public const int DEFAULT_SECTOR_SIZE = 512;
private readonly byte[] _tempBuffer;
private readonly Xts _xts;
@ -9,34 +11,43 @@ namespace XTSSharp {
private XtsCryptoTransform _encryptor;
public XtsSectorStream(Stream baseStream, Xts xts)
: this(baseStream, xts, 512) {
: this(baseStream, xts, 512)
{
}
public XtsSectorStream(Stream baseStream, Xts xts, int sectorSize)
: this(baseStream, xts, sectorSize, 0L) {
: this(baseStream, xts, sectorSize, 0L)
{
}
public XtsSectorStream(Stream baseStream, Xts xts, int sectorSize, long offset)
: base(baseStream, sectorSize, offset) {
: base(baseStream, sectorSize, offset)
{
_xts = xts;
_tempBuffer = new byte[sectorSize];
}
protected override void Dispose(bool disposing) {
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (_encryptor != null) {
if (_encryptor != null)
{
_encryptor.Dispose();
}
if (_decryptor != null) {
if (_decryptor != null)
{
_decryptor.Dispose();
}
}
public override void Write(byte[] buffer, int offset, int count) {
public override void Write(byte[] buffer, int offset, int count)
{
ValidateSize(count);
if (count != 0) {
if (count != 0)
{
ulong currentSector = base.CurrentSector;
if (_encryptor == null) {
if (_encryptor == null)
{
_encryptor = _xts.CreateEncryptor();
}
int count2 = _encryptor.TransformBlock(buffer, offset, count, _tempBuffer, 0, currentSector);
@ -44,14 +55,17 @@ namespace XTSSharp {
}
}
public override int Read(byte[] buffer, int offset, int count) {
public override int Read(byte[] buffer, int offset, int count)
{
ValidateSize(count);
ulong currentSector = base.CurrentSector;
int num = base.Read(_tempBuffer, 0, count);
if (num == 0) {
if (num == 0)
{
return 0;
}
if (_decryptor == null) {
if (_decryptor == null)
{
_decryptor = _xts.CreateDecryptor();
}
return _decryptor.TransformBlock(_tempBuffer, 0, num, buffer, offset, currentSector);

View file

@ -1,17 +1,22 @@
using System.IO;
namespace XTSSharp {
public class XtsStream : RandomAccessSectorStream {
namespace XTSSharp
{
public class XtsStream : RandomAccessSectorStream
{
public XtsStream(Stream baseStream, Xts xts)
: this(baseStream, xts, 512) {
: this(baseStream, xts, 512)
{
}
public XtsStream(Stream baseStream, Xts xts, int sectorSize)
: base(new XtsSectorStream(baseStream, xts, sectorSize), true) {
: base(new XtsSectorStream(baseStream, xts, sectorSize), true)
{
}
public XtsStream(Stream baseStream, Xts xts, int sectorSize, long offset)
: base(new XtsSectorStream(baseStream, xts, sectorSize, offset), true) {
: base(new XtsSectorStream(baseStream, xts, sectorSize, offset), true)
{
}
}
}