From ce6422572b75130a207ceb74c38382acbd0b3071 Mon Sep 17 00:00:00 2001 From: Sebastien Lebreton Date: Thu, 23 Nov 2023 16:14:05 +0100 Subject: [PATCH] Move to C# 12 and rework API --- Dexer.Debug/Dexer.Debug.csproj | 2 +- Dexer.Debug/Program.cs | 3 +- Dexer.Tests/BaseCollectorTest.cs | 13 +- Dexer.Tests/BaseTest.cs | 14 +- Dexer.Tests/CollectorTest.cs | 2 +- Dexer.Tests/Dexer.Tests.csproj | 10 +- Dexer.Tests/InstructionTest.cs | 7 +- Dexer.Tests/NumericalTest.cs | 8 +- Dexer.Tests/RoundtripTest.cs | 5 +- Dexer.Tests/SortTest.cs | 6 +- Dexer/Core/AccessFlags.cs | 4 +- Dexer/Core/Annotation.cs | 20 +- Dexer/Core/AnnotationArgument.cs | 12 +- Dexer/Core/AnnotationVisibility.cs | 2 +- Dexer/Core/ArrayType.cs | 5 +- Dexer/Core/ClassDefinition.cs | 29 ++- Dexer/Core/ClassReference.cs | 27 +-- Dexer/Core/Dex.cs | 24 +- Dexer/Core/DexHeader.cs | 6 +- Dexer/Core/FieldDefinition.cs | 22 +- Dexer/Core/FieldReference.cs | 14 +- Dexer/Core/IAnnotationProvider.cs | 4 +- Dexer/Core/IMemberDefinition.cs | 2 +- Dexer/Core/IMemberReference.cs | 6 +- Dexer/Core/MethodDefinition.cs | 28 +-- Dexer/Core/MethodReference.cs | 21 +- Dexer/Core/Parameter.cs | 23 +- Dexer/Core/PrimitiveType.cs | 2 +- Dexer/Core/Prototype.cs | 24 +- Dexer/Core/TypeReference.cs | 3 +- Dexer/Dexer.csproj | 21 +- Dexer/Extensions/BinaryReaderExtensions.cs | 32 +-- Dexer/Extensions/BinaryWriterExtensions.cs | 6 +- Dexer/Extensions/ByteArrayExtensions.cs | 3 +- Dexer/Extensions/ListExtensions.cs | 9 +- Dexer/IO/AnnotationSet.cs | 4 +- Dexer/IO/CatchSet.cs | 6 +- Dexer/IO/Collectors/AnnotationComparer.cs | 4 +- Dexer/IO/Collectors/ArgumentComparer.cs | 20 +- Dexer/IO/Collectors/BaseCollector.cs | 15 +- Dexer/IO/Collectors/CatchSetComparer.cs | 2 +- .../IO/Collectors/ClassDefinitionComparer.cs | 3 +- Dexer/IO/Collectors/ClassReferenceComparer.cs | 3 +- Dexer/IO/Collectors/DependecyCollector.cs | 7 +- .../IO/Collectors/FieldDefinitionComparer.cs | 3 +- Dexer/IO/Collectors/FieldReferenceComparer.cs | 3 +- .../IO/Collectors/MethodDefinitionComparer.cs | 3 +- .../IO/Collectors/MethodReferenceComparer.cs | 3 +- Dexer/IO/Collectors/ModelShuffler.cs | 3 +- Dexer/IO/Collectors/ModelSorter.cs | 3 +- Dexer/IO/Collectors/PrototypeCollector.cs | 2 +- Dexer/IO/Collectors/PrototypeComparer.cs | 4 +- Dexer/IO/Collectors/StringCollector.cs | 4 +- Dexer/IO/Collectors/StringComparer.cs | 4 +- Dexer/IO/Collectors/TopologicalSorter.cs | 23 +- Dexer/IO/Collectors/TypeReferenceComparer.cs | 3 +- Dexer/IO/DebugInstructionException.cs | 11 +- Dexer/IO/DexConsts.cs | 16 +- Dexer/IO/DexReader.cs | 128 ++++------ Dexer/IO/DexWriter.cs | 222 +++++++++--------- Dexer/IO/InstructionException.cs | 11 +- Dexer/IO/InstructionReader.cs | 44 ++-- Dexer/IO/InstructionWriter.cs | 65 +++-- Dexer/IO/MalformedException.cs | 9 +- Dexer/IO/Markers/ClassDefinitionMarkers.cs | 10 +- Dexer/IO/Markers/HeaderMarkers.cs | 26 +- Dexer/IO/Markers/Marker.cs | 7 +- Dexer/IO/Markers/SignatureMarker.cs | 7 +- Dexer/IO/Markers/SizeOffset.cs | 14 +- Dexer/IO/Markers/SizeOffsetMarker.cs | 7 +- Dexer/IO/Markers/UShortMarker.cs | 7 +- Dexer/IO/Markers/UintMarker.cs | 7 +- Dexer/Instructions/Catch.cs | 11 +- Dexer/Instructions/DebugInfo.cs | 19 +- Dexer/Instructions/DebugInstruction.cs | 7 +- Dexer/Instructions/ExceptionHandler.cs | 19 +- Dexer/Instructions/Instruction.cs | 27 +-- Dexer/Instructions/MethodBody.cs | 14 +- Dexer/Instructions/OffsetStatistics.cs | 2 +- Dexer/Instructions/PackedSwitchData.cs | 6 +- Dexer/Instructions/Register.cs | 11 +- Dexer/Instructions/SparseSwitchData.cs | 6 +- Dexer/Metadata/Map.cs | 4 +- Dexer/Metadata/MapItem.cs | 2 +- Dexer/Metadata/TypeCodes.cs | 2 +- Dexer/Metadata/TypeDescriptor.cs | 12 +- Dexer/Metadata/TypeDescriptors.cs | 2 +- Dexer/Metadata/ValueFormat.cs | 5 +- Dexer/Metadata/ValueFormats.cs | 2 +- Directory.Build.props | 18 ++ Directory.Packages.props | 16 ++ LICENSE | 2 +- 92 files changed, 528 insertions(+), 791 deletions(-) create mode 100644 Directory.Build.props create mode 100644 Directory.Packages.props diff --git a/Dexer.Debug/Dexer.Debug.csproj b/Dexer.Debug/Dexer.Debug.csproj index 7f181ad..2f3e64b 100644 --- a/Dexer.Debug/Dexer.Debug.csproj +++ b/Dexer.Debug/Dexer.Debug.csproj @@ -1,7 +1,7 @@  - net6 + net8.0 Exe false diff --git a/Dexer.Debug/Program.cs b/Dexer.Debug/Program.cs index b266fba..10a2eaa 100644 --- a/Dexer.Debug/Program.cs +++ b/Dexer.Debug/Program.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; using Dexer.Core; namespace Dexer.Debug; diff --git a/Dexer.Tests/BaseCollectorTest.cs b/Dexer.Tests/BaseCollectorTest.cs index 6b333a8..c198690 100644 --- a/Dexer.Tests/BaseCollectorTest.cs +++ b/Dexer.Tests/BaseCollectorTest.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,9 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; -using System.Linq; using Dexer.Core; using Dexer.IO.Collectors; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -31,7 +28,7 @@ namespace Dexer.Tests; [TestClass] public class BaseCollectorTest : BaseTest { - internal void TestCollector(Func> provider) where TC : BaseCollector, new() + internal void TestCollector(Func> provider) where TC : BaseCollector, new() where T : notnull { foreach (var file in GetTestFiles()) { @@ -39,7 +36,7 @@ public class BaseCollectorTest : BaseTest } } - internal TC TestCollector(Func> provider, string file) where TC : BaseCollector, new() + internal TC TestCollector(Func> provider, string file) where TC : BaseCollector, new() where T : notnull { TestContext.WriteLine("Testing {0}", file); var dex = Dex.Read(file); @@ -51,10 +48,10 @@ public class BaseCollectorTest : BaseTest var destKeys = collector.Items.ToDictionary(kv => kv.Key); foreach (var key in sourceKeys.Keys) - Assert.IsTrue(collector.Items.ContainsKey(key) || (key.ToString() == "this"), "Item '{0}' not collected", key); + Assert.IsTrue(collector.Items.ContainsKey(key) || key.ToString() == "this", "Item '{0}' not collected", key); foreach (var key in destKeys.Keys) - Assert.IsTrue(sourceKeys.ContainsKey(key) || (key.ToString() == "this"), "Item '{0}' is 'over' collected", key); + Assert.IsTrue(sourceKeys.ContainsKey(key) || key.ToString() == "this", "Item '{0}' is 'over' collected", key); return collector; } diff --git a/Dexer.Tests/BaseTest.cs b/Dexer.Tests/BaseTest.cs index dd0ea78..11f188e 100644 --- a/Dexer.Tests/BaseTest.cs +++ b/Dexer.Tests/BaseTest.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; -using System.IO; using System.Text; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -39,17 +37,13 @@ public string[] GetTestFiles() return Directory.GetFiles(basePath, "*.dex"); } - public TestContext TestContext - { - get; - set; - } + public required TestContext TestContext { get; set; } - public void DumpList(string filename, List list) + public static void DumpList(string filename, List list) { var output = new StringBuilder(); foreach (var item in list) - output.AppendLine(item.ToString()); + output.AppendLine(item?.ToString()); File.WriteAllText(filename, output.ToString()); } diff --git a/Dexer.Tests/CollectorTest.cs b/Dexer.Tests/CollectorTest.cs index 408bf07..500feec 100644 --- a/Dexer.Tests/CollectorTest.cs +++ b/Dexer.Tests/CollectorTest.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer.Tests/Dexer.Tests.csproj b/Dexer.Tests/Dexer.Tests.csproj index 6592333..dbbba69 100644 --- a/Dexer.Tests/Dexer.Tests.csproj +++ b/Dexer.Tests/Dexer.Tests.csproj @@ -1,15 +1,15 @@  - net6 + net8.0 false - - - - + + + + diff --git a/Dexer.Tests/InstructionTest.cs b/Dexer.Tests/InstructionTest.cs index 2320399..3e88030 100644 --- a/Dexer.Tests/InstructionTest.cs +++ b/Dexer.Tests/InstructionTest.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; using Dexer.Core; using Microsoft.VisualStudio.TestTools.UnitTesting; using Dexer.Instructions; @@ -51,8 +49,7 @@ public void TestUpdateInstructionOffsets() var offsets = new List(); foreach (var ins in method.Body.Instructions) { - if (!coverage.ContainsKey(ins.OpCode)) - coverage.Add(ins.OpCode, 0); + coverage.TryAdd(ins.OpCode, 0); offsets.Add(ins.Offset); coverage[ins.OpCode]++; } diff --git a/Dexer.Tests/NumericalTest.cs b/Dexer.Tests/NumericalTest.cs index 68bea47..fa7486b 100644 --- a/Dexer.Tests/NumericalTest.cs +++ b/Dexer.Tests/NumericalTest.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -20,11 +20,7 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using Microsoft.VisualStudio.TestTools.UnitTesting; -using System.IO; using Dexer.Extensions; -using System; -using System.Collections.Generic; -using System.Linq; namespace Dexer.Tests; @@ -37,7 +33,7 @@ public void TestNumber(Func readProvider, Action (T)Convert.ChangeType(item, typeof(T)))) + foreach (var expected in values.Select(item => (T)Convert.ChangeType(item, typeof(T))!)) { TestContext.WriteLine("{0}, {0:x}", expected); diff --git a/Dexer.Tests/RoundtripTest.cs b/Dexer.Tests/RoundtripTest.cs index 4a5fc74..82ca987 100644 --- a/Dexer.Tests/RoundtripTest.cs +++ b/Dexer.Tests/RoundtripTest.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,9 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; -using System.IO; -using System.Linq; using Dexer.Core; using Dexer.Extensions; using Microsoft.VisualStudio.TestTools.UnitTesting; diff --git a/Dexer.Tests/SortTest.cs b/Dexer.Tests/SortTest.cs index bd50dd9..653f3ad 100644 --- a/Dexer.Tests/SortTest.cs +++ b/Dexer.Tests/SortTest.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; using Dexer.Core; using Dexer.Extensions; using Dexer.IO.Collectors; @@ -32,7 +30,7 @@ namespace Dexer.Tests; [TestClass] public class SortTest : BaseCollectorTest { - public void TestGlobalSort(Func> provider, IComparer comparer) + public void TestGlobalSort(Func> provider, IComparer comparer) where T : notnull { foreach (var file in GetTestFiles()) { diff --git a/Dexer/Core/AccessFlags.cs b/Dexer/Core/AccessFlags.cs index 2e2382d..4d5c77f 100644 --- a/Dexer/Core/AccessFlags.cs +++ b/Dexer/Core/AccessFlags.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; - namespace Dexer.Core; [Flags] diff --git a/Dexer/Core/Annotation.cs b/Dexer/Core/Annotation.cs index 1645818..119e682 100644 --- a/Dexer/Core/Annotation.cs +++ b/Dexer/Core/Annotation.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,25 +19,17 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using System.Text; using Dexer.Metadata; -using System; -using System.Linq; namespace Dexer.Core; -public class Annotation : IEquatable +public class Annotation(ClassReference type) : IEquatable { - public ClassReference Type { get; set; } - public List Arguments { get; set; } + public ClassReference Type { get; } = type; + public List Arguments { get; } = []; public AnnotationVisibility Visibility { get; set; } - public Annotation() - { - Arguments = new List(); - } - public override string ToString() { var builder = new StringBuilder(); @@ -55,7 +47,7 @@ public override string ToString() return builder.ToString(); } - public bool Equals(Annotation other) + public bool Equals(Annotation? other) { if (other == null) return false; @@ -66,7 +58,7 @@ public bool Equals(Annotation other) return !Arguments.Where((t, i) => !t.Equals(other.Arguments[i])).Any(); } - public override bool Equals(object obj) + public override bool Equals(object? obj) { return obj is Annotation annotation && Equals(annotation); } diff --git a/Dexer/Core/AnnotationArgument.cs b/Dexer/Core/AnnotationArgument.cs index 371b622..d2aef3b 100644 --- a/Dexer/Core/AnnotationArgument.cs +++ b/Dexer/Core/AnnotationArgument.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,17 +19,15 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Linq; using Dexer.Metadata; using System.Text; -using System; namespace Dexer.Core; -public class AnnotationArgument : IEquatable +public class AnnotationArgument(string name, object? value) : IEquatable { - public string Name { get; set; } - public object Value { get; set; } + public string Name { get; } = name; + public object? Value { get; } = value; public override string ToString() { @@ -47,7 +45,7 @@ public bool Equals(AnnotationArgument other) return Name.Equals(other.Name) && ValueFormat.GetFormat(Value).Equals(ValueFormat.GetFormat(other.Value)) - && (ValueFormat.GetFormat(Value) == ValueFormats.Array && ArrayEquals(Value as Array, other.Value as Array) || Equals(Value, other.Value)); + && (ValueFormat.GetFormat(Value) == ValueFormats.Array && ArrayEquals((Array)Value!, (Array)other.Value!) || Equals(Value, other.Value)); } internal static bool ArrayEquals(Array array1, Array array2) diff --git a/Dexer/Core/AnnotationVisibility.cs b/Dexer/Core/AnnotationVisibility.cs index 4ac98a0..b0f4cf4 100644 --- a/Dexer/Core/AnnotationVisibility.cs +++ b/Dexer/Core/AnnotationVisibility.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/Core/ArrayType.cs b/Dexer/Core/ArrayType.cs index bd013c6..11d0c75 100644 --- a/Dexer/Core/ArrayType.cs +++ b/Dexer/Core/ArrayType.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -27,8 +27,9 @@ public class ArrayType : CompositeType { public TypeReference ElementType { get; set; } - public ArrayType() + public ArrayType(TypeReference elementType) { + ElementType = elementType; TypeDescriptor = TypeDescriptors.Array; } diff --git a/Dexer/Core/ClassDefinition.cs b/Dexer/Core/ClassDefinition.cs index 408eb86..2c51892 100644 --- a/Dexer/Core/ClassDefinition.cs +++ b/Dexer/Core/ClassDefinition.cs @@ -19,10 +19,8 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using System.Globalization; using Dexer.Metadata; -using System.Linq; using Dexer.IO; using Dexer.IO.Collectors; @@ -31,31 +29,32 @@ namespace Dexer.Core; public class ClassDefinition : ClassReference, IMemberDefinition { public AccessFlags AccessFlags { get; set; } - public ClassReference SuperClass { get; set; } + public ClassReference? SuperClass { get; set; } public List InnerClasses { get; set; } public List Interfaces { get; set; } - public string SourceFile { get; set; } + public string? SourceFile { get; set; } public List Annotations { get; set; } public List Fields { get; set; } public List Methods { get; set; } - public ClassDefinition Owner { get; set; } - internal ClassDefinition() +#pragma warning disable CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes). + public ClassDefinition? Owner { get; set; } +#pragma warning restore CS8766 // Nullability of reference types in return type doesn't match implicitly implemented member (possibly because of nullability attributes). + + internal ClassDefinition(string fullname) : base(fullname) { TypeDescriptor = TypeDescriptors.FullyQualifiedName; - Interfaces = new List(); - Annotations = new List(); - Fields = new List(); - Methods = new List(); - InnerClasses = new List(); + Interfaces = []; + Annotations = []; + Fields = []; + Methods = []; + InnerClasses = []; + Fullname = fullname; } - internal ClassDefinition(ClassReference cref) : this() + internal ClassDefinition(ClassReference cref) : this(cref.Fullname) { - Fullname = cref.Fullname; - Namespace = cref.Namespace; - Name = cref.Name; } public IEnumerable GetMethods(string name) diff --git a/Dexer/Core/ClassReference.cs b/Dexer/Core/ClassReference.cs index 95041b5..1cb2958 100644 --- a/Dexer/Core/ClassReference.cs +++ b/Dexer/Core/ClassReference.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; using System.Globalization; using System.Text; using Dexer.Metadata; @@ -31,8 +30,8 @@ public class ClassReference : CompositeType, IMemberReference public const char NamespaceSeparator = '.'; public const char InternalNamespaceSeparator = '/'; - public string Namespace { get; set; } - public string Name { get; set; } + public string Namespace { get; private set; } = string.Empty; + public string Name { get; private set; } = string.Empty; public string Fullname { @@ -46,6 +45,12 @@ public string Fullname } set { + Name = string.Empty; + Namespace = string.Empty; + + if (string.IsNullOrEmpty(value)) + return; + value = value.Replace(InternalNamespaceSeparator, NamespaceSeparator); var items = value.Split(NamespaceSeparator); if (items.Length > 0) @@ -54,11 +59,6 @@ public string Fullname Array.Resize(ref items, items.Length - 1); Namespace = string.Join(NamespaceSeparator.ToString(CultureInfo.InvariantCulture), items); } - else - { - Name = string.Empty; - Namespace = string.Empty; - } } } @@ -67,20 +67,15 @@ public override string ToString() return Fullname; } - public ClassReference() + public ClassReference(string fullname) { TypeDescriptor = TypeDescriptors.FullyQualifiedName; - } - - public ClassReference(string fullname) : this() - { Fullname = fullname; } public bool Equals(ClassReference other) { - return base.Equals(other) - && Fullname == other.Fullname; + return base.Equals(other) && Fullname == other.Fullname; } public override bool Equals(TypeReference other) diff --git a/Dexer/Core/Dex.cs b/Dexer/Core/Dex.cs index 824c2bc..17d8e56 100644 --- a/Dexer/Core/Dex.cs +++ b/Dexer/Core/Dex.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; -using System.IO; using Dexer.IO; namespace Dexer.Core; @@ -86,7 +84,7 @@ public static Dex Read(string filename, bool bufferize) public void Write(Stream stream, bool bufferize) { var deststream = stream; - MemoryStream memorystream = null; + MemoryStream? memorystream = null; if (bufferize) { @@ -99,7 +97,7 @@ public void Write(Stream stream, bool bufferize) var writer = new DexWriter(this); writer.WriteTo(binaryWriter); - if (!bufferize) + if (memorystream == null) return; memorystream.Position = 0; @@ -115,21 +113,21 @@ public void Write(string filename, bool bufferize) public Dex() { - Classes = new List(); - TypeReferences = new List(); - FieldReferences = new List(); - MethodReferences = new List(); + Classes = []; + TypeReferences = []; + FieldReferences = []; + MethodReferences = []; - Prototypes = new List(); - Strings = new List(); + Prototypes = []; + Strings = []; } - public ClassDefinition GetClass(string fullname) + public ClassDefinition? GetClass(string fullname) { return GetClass(fullname, Classes); } - internal ClassDefinition GetClass(string fullname, List container) + internal ClassDefinition? GetClass(string fullname, List container) { foreach (var item in container) { diff --git a/Dexer/Core/DexHeader.cs b/Dexer/Core/DexHeader.cs index bd289ac..06e3bb1 100644 --- a/Dexer/Core/DexHeader.cs +++ b/Dexer/Core/DexHeader.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,9 +23,9 @@ namespace Dexer.Core; internal class DexHeader { - internal byte[] Magic { get; set; } + internal byte[] Magic { get; set; } = []; internal uint CheckSum { get; set; } - internal byte[] Signature { get; set; } + internal byte[] Signature { get; set; } = []; internal uint FileSize { get; set; } internal uint HeaderSize { get; set; } diff --git a/Dexer/Core/FieldDefinition.cs b/Dexer/Core/FieldDefinition.cs index 1174cf8..93b7b6f 100644 --- a/Dexer/Core/FieldDefinition.cs +++ b/Dexer/Core/FieldDefinition.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,34 +19,24 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.Core; -public class FieldDefinition : FieldReference, IMemberDefinition +public class FieldDefinition(ClassDefinition owner, TypeReference type, string name) : FieldReference(owner, type, name), IMemberDefinition { public AccessFlags AccessFlags { get; set; } public new ClassDefinition Owner { - get => base.Owner as ClassDefinition; + get => (ClassDefinition)base.Owner; set => base.Owner = value; } - public List Annotations { get; set; } - public object Value { get; set; } - - public FieldDefinition() - { - Annotations = new List(); - } + public List Annotations { get; set; } = []; + public object? Value { get; set; } // for prefetching - internal FieldDefinition(FieldReference fref) : this() + internal FieldDefinition(FieldReference fref) : this((ClassDefinition) fref.Owner, fref.Type, fref.Name) { - Owner = fref.Owner as ClassDefinition; - Type = fref.Type; - Name = fref.Name; } // ReSharper disable ValueParameterNotUsed diff --git a/Dexer/Core/FieldReference.cs b/Dexer/Core/FieldReference.cs index cfa4da8..ad5aed8 100644 --- a/Dexer/Core/FieldReference.cs +++ b/Dexer/Core/FieldReference.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,15 +23,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.Core; -public class FieldReference : IMemberReference +public class FieldReference(ClassReference owner, TypeReference type, string name) : IMemberReference { - public ClassReference Owner { get; set; } - public string Name { get; set; } - public TypeReference Type { get; set; } - - internal FieldReference() - { - } + public ClassReference Owner { get; set; } = owner; + public string Name { get; set; } = name; + public TypeReference Type { get; set; } = type; public override string ToString() { diff --git a/Dexer/Core/IAnnotationProvider.cs b/Dexer/Core/IAnnotationProvider.cs index 8fa4543..c493af8 100644 --- a/Dexer/Core/IAnnotationProvider.cs +++ b/Dexer/Core/IAnnotationProvider.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.Core; public interface IAnnotationProvider diff --git a/Dexer/Core/IMemberDefinition.cs b/Dexer/Core/IMemberDefinition.cs index cdc5142..40bed2e 100644 --- a/Dexer/Core/IMemberDefinition.cs +++ b/Dexer/Core/IMemberDefinition.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/Core/IMemberReference.cs b/Dexer/Core/IMemberReference.cs index 07aa799..4e33f8a 100644 --- a/Dexer/Core/IMemberReference.cs +++ b/Dexer/Core/IMemberReference.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,11 +19,9 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; - namespace Dexer.Core; public interface IMemberReference : IEquatable { - string Name { get; set; } + string Name { get; } } diff --git a/Dexer/Core/MethodDefinition.cs b/Dexer/Core/MethodDefinition.cs index e421ad6..9c0c109 100644 --- a/Dexer/Core/MethodDefinition.cs +++ b/Dexer/Core/MethodDefinition.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,42 +19,26 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Instructions; namespace Dexer.Core; -public class MethodDefinition : MethodReference, IMemberDefinition +public class MethodDefinition(ClassDefinition owner, string name, Prototype prototype) : MethodReference(owner, name, prototype), IMemberDefinition { public AccessFlags AccessFlags { get; set; } public new ClassDefinition Owner { - get => base.Owner as ClassDefinition; + get => (ClassDefinition)base.Owner; set => base.Owner = value; } - public List Annotations { get; set; } - public MethodBody Body { get; set; } - - public MethodDefinition() - { - Annotations = new List(); - } + public List Annotations { get; set; } = []; + public MethodBody? Body { get; set; } // for prefetching - internal MethodDefinition(MethodReference mref) : this() - { - Owner = mref.Owner as ClassDefinition; - Name = mref.Name; - Prototype = mref.Prototype; - } - - public MethodDefinition(ClassDefinition owner, string name, Prototype prototype) : this() + internal MethodDefinition(MethodReference mref) : this((ClassDefinition)mref.Owner, mref.Name, mref.Prototype) { - Owner = owner; - Name = name; - Prototype = prototype; } public bool IsVirtual diff --git a/Dexer/Core/MethodReference.cs b/Dexer/Core/MethodReference.cs index 147c7f9..26bddd3 100644 --- a/Dexer/Core/MethodReference.cs +++ b/Dexer/Core/MethodReference.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,22 +23,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.Core; -public class MethodReference : IMemberReference +public class MethodReference(CompositeType owner, string name, Prototype prototype) : IMemberReference { - public CompositeType Owner { get; set; } - public string Name { get; set; } - public Prototype Prototype { get; set; } - - public MethodReference() - { - } - - public MethodReference(CompositeType owner, string name, Prototype prototype) : this() - { - Owner = owner; - Name = name; - Prototype = prototype; - } + public CompositeType Owner { get; set; } = owner; + public string Name { get; set; } = name; + public Prototype Prototype { get; set; } = prototype; public override string ToString() { diff --git a/Dexer/Core/Parameter.cs b/Dexer/Core/Parameter.cs index 28d5529..ff399e0 100644 --- a/Dexer/Core/Parameter.cs +++ b/Dexer/Core/Parameter.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,25 +19,12 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; -using System; - namespace Dexer.Core; -public class Parameter : IAnnotationProvider, ICloneable, IEquatable +public class Parameter(TypeReference type) : IAnnotationProvider, ICloneable, IEquatable { - public List Annotations { get; set; } - public TypeReference Type { get; set; } - - public Parameter() - { - Annotations = new List(); - } - - public Parameter(TypeReference type) : this() - { - Type = type; - } + public List Annotations { get; set; } = []; + public TypeReference Type { get; set; } = type; public override string ToString() { @@ -51,7 +38,7 @@ internal Parameter Clone() object ICloneable.Clone() { - var result = new Parameter {Type = Type}; + var result = new Parameter(Type); return result; } diff --git a/Dexer/Core/PrimitiveType.cs b/Dexer/Core/PrimitiveType.cs index 728745b..5e85a8d 100644 --- a/Dexer/Core/PrimitiveType.cs +++ b/Dexer/Core/PrimitiveType.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/Core/Prototype.cs b/Dexer/Core/Prototype.cs index 2ec50ba..0de253b 100644 --- a/Dexer/Core/Prototype.cs +++ b/Dexer/Core/Prototype.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,29 +19,15 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; -using System.Linq; using System.Text; -using System; using Dexer.Metadata; namespace Dexer.Core; -public class Prototype : ICloneable, IEquatable +public class Prototype(TypeReference returntype, params Parameter[] parameters) : ICloneable, IEquatable { - public TypeReference ReturnType { get; set; } - public List Parameters { get; set; } - - public Prototype() - { - Parameters = new List(); - } - - public Prototype(TypeReference returntype, params Parameter[] parameters) : this() - { - ReturnType = returntype; - Parameters = new List(parameters); - } + public TypeReference ReturnType { get; set; } = returntype; + public List Parameters { get; set; } = new List(parameters); public bool ContainsAnnotation() { @@ -73,7 +59,7 @@ internal Prototype Clone() object ICloneable.Clone() { - var result = new Prototype {ReturnType = ReturnType}; + var result = new Prototype(ReturnType); foreach (var p in Parameters) { diff --git a/Dexer/Core/TypeReference.cs b/Dexer/Core/TypeReference.cs index 6ceafd2..4da547f 100644 --- a/Dexer/Core/TypeReference.cs +++ b/Dexer/Core/TypeReference.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -20,7 +20,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using Dexer.Metadata; -using System; namespace Dexer.Core; diff --git a/Dexer/Dexer.csproj b/Dexer/Dexer.csproj index f22a995..9a6cb70 100644 --- a/Dexer/Dexer.csproj +++ b/Dexer/Dexer.csproj @@ -2,25 +2,18 @@ netstandard2.0 - 10.0 - - true + true snupkg - - Sebastien Lebreton - https://github.com/sailro/dexer - 0.8.0 - Sebastien Lebreton - en-US - DEX Android Dalvik - Dexer is a .NET library to read, manipulate and write Android/Dalvik DEX files - MIT - https://github.com/sailro/dexer + README.md - + + + + + diff --git a/Dexer/Extensions/BinaryReaderExtensions.cs b/Dexer/Extensions/BinaryReaderExtensions.cs index 62f2584..1e6d40d 100644 --- a/Dexer/Extensions/BinaryReaderExtensions.cs +++ b/Dexer/Extensions/BinaryReaderExtensions.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,9 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.IO; - namespace Dexer.Extensions; public static class BinaryReaderExtensions @@ -83,28 +80,13 @@ public static string ReadMUTF8String(this BinaryReader reader) for (int j = 0, jLength = chars.Length; j < jLength; j++) { int data = reader.ReadByte(); - switch (data >> 4) + chars[j] = (data >> 4) switch { - case 0: - case 1: - case 2: - case 3: - case 4: - case 5: - case 6: - case 7: - chars[j] = (char)data; - break; - case 12: - case 13: - chars[j] = (char)(((data & 0x1F) << 6) | (reader.ReadByte() & 0x3F)); - break; - case 14: - chars[j] = (char)(((data & 0x0F) << 12) | ((reader.ReadByte() & 0x3F) << 6) | (reader.ReadByte() & 0x3F)); - break; - default: - throw new ArgumentException("illegal MUTF8 byte"); - } + 0 or 1 or 2 or 3 or 4 or 5 or 6 or 7 => (char)data, + 12 or 13 => (char)(((data & 0x1F) << 6) | (reader.ReadByte() & 0x3F)), + 14 => (char)(((data & 0x0F) << 12) | ((reader.ReadByte() & 0x3F) << 6) | (reader.ReadByte() & 0x3F)), + _ => throw new ArgumentException("illegal MUTF8 byte"), + }; } reader.ReadByte(); // 0 padded; diff --git a/Dexer/Extensions/BinaryWriterExtensions.cs b/Dexer/Extensions/BinaryWriterExtensions.cs index c529751..de84a3f 100644 --- a/Dexer/Extensions/BinaryWriterExtensions.cs +++ b/Dexer/Extensions/BinaryWriterExtensions.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.IO; using Dexer.IO.Markers; namespace Dexer.Extensions; @@ -193,6 +191,7 @@ private static long TripleShift(long n, int s) return (n >> s) + (2 << ~s); } +#pragma warning disable IDE0060 public static int GetByteCountForSignedPackedNumber(this BinaryWriter writer, long value) { var requiredBits = 65 - NumberOfLeadingZeros(value ^ (value >> 63)); @@ -211,6 +210,7 @@ public static int GetByteCountForUnsignedPackedNumber(this BinaryWriter writer, return result; } +#pragma warning restore IDE0060 public static void WritePackedSignedNumber(this BinaryWriter writer, long value) { diff --git a/Dexer/Extensions/ByteArrayExtensions.cs b/Dexer/Extensions/ByteArrayExtensions.cs index 8fc3311..a78d969 100644 --- a/Dexer/Extensions/ByteArrayExtensions.cs +++ b/Dexer/Extensions/ByteArrayExtensions.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Linq; using System.Text; namespace Dexer.Extensions; diff --git a/Dexer/Extensions/ListExtensions.cs b/Dexer/Extensions/ListExtensions.cs index 1b2639e..ad398c2 100644 --- a/Dexer/Extensions/ListExtensions.cs +++ b/Dexer/Extensions/ListExtensions.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,14 +19,11 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; - namespace Dexer.Extensions; public static class ListExtensions { - private static readonly Random Rnd = new(); + private static readonly Random _rnd = new(); public static void Shuffle(this List list) { @@ -36,7 +33,7 @@ public static void Shuffle(this List list) for (var i = list.Count - 1; i >= 0; i--) { var item = list[i]; - var index = Rnd.Next(i + 1); + var index = _rnd.Next(i + 1); list[i] = list[index]; list[index] = item; diff --git a/Dexer/IO/AnnotationSet.cs b/Dexer/IO/AnnotationSet.cs index aa71cc3..ad856cb 100644 --- a/Dexer/IO/AnnotationSet.cs +++ b/Dexer/IO/AnnotationSet.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; using System.Globalization; using Dexer.Core; using System.Text; diff --git a/Dexer/IO/CatchSet.cs b/Dexer/IO/CatchSet.cs index 2d1e29d..ccc902f 100644 --- a/Dexer/IO/CatchSet.cs +++ b/Dexer/IO/CatchSet.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; using System.Globalization; using System.Text; using Dexer.Instructions; @@ -29,7 +27,7 @@ namespace Dexer.IO; internal class CatchSet : List, IEquatable { - public Instruction CatchAll { get; set; } + public Instruction? CatchAll { get; set; } public CatchSet(ExceptionHandler handler) { diff --git a/Dexer/IO/Collectors/AnnotationComparer.cs b/Dexer/IO/Collectors/AnnotationComparer.cs index aa21e4d..f393878 100644 --- a/Dexer/IO/Collectors/AnnotationComparer.cs +++ b/Dexer/IO/Collectors/AnnotationComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/ArgumentComparer.cs b/Dexer/IO/Collectors/ArgumentComparer.cs index 434bc40..bf4cc94 100644 --- a/Dexer/IO/Collectors/ArgumentComparer.cs +++ b/Dexer/IO/Collectors/ArgumentComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; using Dexer.Core; using Dexer.Metadata; @@ -28,7 +26,7 @@ namespace Dexer.IO.Collectors; internal class ArgumentComparer : IComparer { - private static int CompareValue(object x, object y) + private static int CompareValue(object? x, object? y) { var xf = ValueFormat.GetFormat(x); var yf = ValueFormat.GetFormat(y); @@ -49,22 +47,22 @@ private static int CompareValue(object x, object y) case ValueFormats.Double: case ValueFormats.Boolean: case ValueFormats.String: - return ((IComparable)x).CompareTo(y); + return ((IComparable)x!).CompareTo(y); case ValueFormats.Null: return 0; case ValueFormats.Type: - return new TypeReferenceComparer().Compare((TypeReference)x, (TypeReference)y); + return new TypeReferenceComparer().Compare((TypeReference)x!, (TypeReference)y!); case ValueFormats.Field: case ValueFormats.Enum: - return new FieldReferenceComparer().Compare((FieldReference)x, (FieldReference)y); + return new FieldReferenceComparer().Compare((FieldReference)x!, (FieldReference)y!); case ValueFormats.Method: - return new MethodReferenceComparer().Compare((MethodReference)x, (MethodReference)y); + return new MethodReferenceComparer().Compare((MethodReference)x!, (MethodReference)y!); case ValueFormats.Annotation: - return new AnnotationComparer().Compare((Annotation)x, (Annotation)y); + return new AnnotationComparer().Compare((Annotation)x!, (Annotation)y!); case ValueFormats.Array: - var ax = (Array)x; - var ay = (Array)y; + var ax = (Array)x!; + var ay = (Array)y!; for (var i = 0; i < Math.Min(ax.Length, ay.Length); i++) { result = CompareValue(ax.GetValue(i), ay.GetValue(i)); diff --git a/Dexer/IO/Collectors/BaseCollector.cs b/Dexer/IO/Collectors/BaseCollector.cs index 435eeb4..7405dbf 100644 --- a/Dexer/IO/Collectors/BaseCollector.cs +++ b/Dexer/IO/Collectors/BaseCollector.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,9 +19,7 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; using System.Collections; -using System.Collections.Generic; using Dexer.Core; using Dexer.Instructions; @@ -33,7 +31,7 @@ internal class BaseCollector public BaseCollector() { - Items = new Dictionary(); + Items = []; } public List ToList() @@ -145,7 +143,10 @@ public virtual void Collect(ClassDefinition @class) Collect(@class.Interfaces); Collect(@class.Methods); Collect(@class.SourceFile); - Collect(@class.SuperClass); + + if (@class.SuperClass != null) + Collect(@class.SuperClass); + Collect(@class as ClassReference); } @@ -278,7 +279,7 @@ public virtual void Collect(DebugInstruction instruction) Collect(instruction.Operands); } - public virtual void Collect(object obj) + public virtual void Collect(object? obj) { switch (obj) { @@ -320,7 +321,7 @@ public virtual void Collect(object obj) } } - public virtual void Collect(string str) + public virtual void Collect(string? str) { } } diff --git a/Dexer/IO/Collectors/CatchSetComparer.cs b/Dexer/IO/Collectors/CatchSetComparer.cs index aa2e976..1103f74 100644 --- a/Dexer/IO/Collectors/CatchSetComparer.cs +++ b/Dexer/IO/Collectors/CatchSetComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/IO/Collectors/ClassDefinitionComparer.cs b/Dexer/IO/Collectors/ClassDefinitionComparer.cs index 97f29bc..cd0db32 100644 --- a/Dexer/IO/Collectors/ClassDefinitionComparer.cs +++ b/Dexer/IO/Collectors/ClassDefinitionComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/ClassReferenceComparer.cs b/Dexer/IO/Collectors/ClassReferenceComparer.cs index 7192ee9..f7d565f 100644 --- a/Dexer/IO/Collectors/ClassReferenceComparer.cs +++ b/Dexer/IO/Collectors/ClassReferenceComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/DependecyCollector.cs b/Dexer/IO/Collectors/DependecyCollector.cs index de01597..bf1a15e 100644 --- a/Dexer/IO/Collectors/DependecyCollector.cs +++ b/Dexer/IO/Collectors/DependecyCollector.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -40,7 +40,10 @@ public override void Collect(ClassDefinition @class) { Collect(@class.InnerClasses); Collect(@class.Interfaces); - Collect(@class.SuperClass); + + if (@class.SuperClass != null) + Collect(@class.SuperClass); + Collect(@class as ClassReference); } } diff --git a/Dexer/IO/Collectors/FieldDefinitionComparer.cs b/Dexer/IO/Collectors/FieldDefinitionComparer.cs index 732e1f2..7d61d91 100644 --- a/Dexer/IO/Collectors/FieldDefinitionComparer.cs +++ b/Dexer/IO/Collectors/FieldDefinitionComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/FieldReferenceComparer.cs b/Dexer/IO/Collectors/FieldReferenceComparer.cs index 0c72cb1..eb26d4c 100644 --- a/Dexer/IO/Collectors/FieldReferenceComparer.cs +++ b/Dexer/IO/Collectors/FieldReferenceComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/MethodDefinitionComparer.cs b/Dexer/IO/Collectors/MethodDefinitionComparer.cs index 547bbf2..81e88d6 100644 --- a/Dexer/IO/Collectors/MethodDefinitionComparer.cs +++ b/Dexer/IO/Collectors/MethodDefinitionComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/MethodReferenceComparer.cs b/Dexer/IO/Collectors/MethodReferenceComparer.cs index 4204b34..1579a2d 100644 --- a/Dexer/IO/Collectors/MethodReferenceComparer.cs +++ b/Dexer/IO/Collectors/MethodReferenceComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/ModelShuffler.cs b/Dexer/IO/Collectors/ModelShuffler.cs index 59ecac6..724479d 100644 --- a/Dexer/IO/Collectors/ModelShuffler.cs +++ b/Dexer/IO/Collectors/ModelShuffler.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; using Dexer.Extensions; diff --git a/Dexer/IO/Collectors/ModelSorter.cs b/Dexer/IO/Collectors/ModelSorter.cs index 82d3469..75d384d 100644 --- a/Dexer/IO/Collectors/ModelSorter.cs +++ b/Dexer/IO/Collectors/ModelSorter.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/PrototypeCollector.cs b/Dexer/IO/Collectors/PrototypeCollector.cs index ecf9ed9..7ab0572 100644 --- a/Dexer/IO/Collectors/PrototypeCollector.cs +++ b/Dexer/IO/Collectors/PrototypeCollector.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/IO/Collectors/PrototypeComparer.cs b/Dexer/IO/Collectors/PrototypeComparer.cs index f9dd4cd..d9973d6 100644 --- a/Dexer/IO/Collectors/PrototypeComparer.cs +++ b/Dexer/IO/Collectors/PrototypeComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,9 +19,7 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; -using System; namespace Dexer.IO.Collectors; diff --git a/Dexer/IO/Collectors/StringCollector.cs b/Dexer/IO/Collectors/StringCollector.cs index 1320a4a..551bb19 100644 --- a/Dexer/IO/Collectors/StringCollector.cs +++ b/Dexer/IO/Collectors/StringCollector.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -46,7 +46,7 @@ public override void Collect(TypeReference tref) Collect(TypeDescriptor.Encode(tref)); } - public override void Collect(string str) + public override void Collect(string? str) { base.Collect(str); diff --git a/Dexer/IO/Collectors/StringComparer.cs b/Dexer/IO/Collectors/StringComparer.cs index d2c3d03..c211d11 100644 --- a/Dexer/IO/Collectors/StringComparer.cs +++ b/Dexer/IO/Collectors/StringComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.IO.Collectors; internal class StringComparer : IComparer diff --git a/Dexer/IO/Collectors/TopologicalSorter.cs b/Dexer/IO/Collectors/TopologicalSorter.cs index 5a5ada5..6e6e422 100644 --- a/Dexer/IO/Collectors/TopologicalSorter.cs +++ b/Dexer/IO/Collectors/TopologicalSorter.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,11 +19,8 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; -using System.Linq; - namespace Dexer.IO.Collectors; + /* Taken from (great thanks) * http://stackoverflow.com/questions/1982592/topological-sorting-using-linq */ @@ -33,7 +30,7 @@ internal interface IPartialComparer int? PartialCompare(T x, T y); } -internal class ReferenceEqualityComparer : IEqualityComparer +internal class ReferenceEqualityComparer : IEqualityComparer where T : notnull { public bool Equals(T x, T y) { @@ -48,7 +45,7 @@ public int GetHashCode(T obj) internal class TopologicalSorter { - private class DepthFirstSearch + private class DepthFirstSearch where TElement : notnull { readonly IEnumerable _elements; readonly IPartialComparer _comparer; @@ -71,7 +68,7 @@ IPartialComparer comparer selector, referenceComparer ); - _sorted = new List(); + _sorted = []; } public IEnumerable VisitAll() @@ -102,10 +99,7 @@ void Visit(TElement element) } } - public IEnumerable TopologicalSort( - IList elements, - IPartialComparer comparer - ) + public IEnumerable TopologicalSort(IList elements, IPartialComparer comparer) where TElement : notnull { var search = new DepthFirstSearch( elements, @@ -115,10 +109,7 @@ IPartialComparer comparer return search.VisitAll(); } - public IEnumerable TopologicalSort( - IList elements, - Func selector, IPartialComparer comparer - ) + public IEnumerable TopologicalSort(IList elements, Func selector, IPartialComparer comparer) where TElement : notnull { var search = new DepthFirstSearch( elements, diff --git a/Dexer/IO/Collectors/TypeReferenceComparer.cs b/Dexer/IO/Collectors/TypeReferenceComparer.cs index bbeb847..f08a1b6 100644 --- a/Dexer/IO/Collectors/TypeReferenceComparer.cs +++ b/Dexer/IO/Collectors/TypeReferenceComparer.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using Dexer.Core; using Dexer.Metadata; diff --git a/Dexer/IO/DebugInstructionException.cs b/Dexer/IO/DebugInstructionException.cs index f059af4..f24d200 100644 --- a/Dexer/IO/DebugInstructionException.cs +++ b/Dexer/IO/DebugInstructionException.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,12 +23,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.IO; -public class DebugInstructionException : MalformedException +public class DebugInstructionException(DebugInstruction instruction, string message) : MalformedException(message) { - public DebugInstruction Instruction { get; set; } - - public DebugInstructionException(DebugInstruction instruction, string message) : base(message) - { - Instruction = instruction; - } + public DebugInstruction Instruction { get; set; } = instruction; } diff --git a/Dexer/IO/DexConsts.cs b/Dexer/IO/DexConsts.cs index ac0f4cb..58021b6 100644 --- a/Dexer/IO/DexConsts.cs +++ b/Dexer/IO/DexConsts.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,11 +23,11 @@ namespace Dexer.IO; internal static class DexConsts { - internal static readonly byte[] FileMagic = {0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0x00}; - internal const uint Endian = 0x12345678; - internal const uint ReverseEndian = 0x78563412; - internal const uint NoIndex = 0xffffffff; - internal const char InnerClassMarker = '$'; - internal const int SignatureSize = 20; - internal const int HeaderSize = 0x70; + public static readonly byte[] FileMagic = [0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0x00]; + public const uint Endian = 0x12345678; + public const uint ReverseEndian = 0x78563412; + public const uint NoIndex = 0xffffffff; + public const char InnerClassMarker = '$'; + public const int SignatureSize = 20; + public const int HeaderSize = 0x70; } diff --git a/Dexer/IO/DexReader.cs b/Dexer/IO/DexReader.cs index 1c7b9d6..ce4aeff 100644 --- a/Dexer/IO/DexReader.cs +++ b/Dexer/IO/DexReader.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,10 +19,7 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; using System.Collections; -using System.Collections.Generic; -using System.IO; using Dexer.Core; using Dexer.Extensions; using Dexer.Instructions; @@ -30,18 +27,11 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.IO; -public class DexReader +public class DexReader(Dex dex) { - private Dex Dex { get; } - internal Map Map { get; set; } - internal DexHeader Header { get; set; } - - public DexReader(Dex dex) - { - Dex = dex; - Map = new Map(); - Header = new DexHeader(); - } + private Dex Dex { get; } = dex; + internal Map Map { get; } = []; + internal DexHeader Header { get; } = new DexHeader(); #region Header @@ -309,7 +299,7 @@ private List ReadAnnotationSet(BinaryReader reader, uint annotationO private Annotation ReadAnnotation(BinaryReader reader, uint annotationOffset) { - Annotation annotation = null; + Annotation annotation = default!; reader.PreserveCurrentPosition(annotationOffset, () => { var visibility = reader.ReadByte(); @@ -324,14 +314,12 @@ private Annotation ReadEncodedAnnotation(BinaryReader reader) var typeIndex = (int)reader.ReadULEB128(); var elementSize = (int)reader.ReadULEB128(); - var annotation = new Annotation {Type = (ClassReference)Dex.TypeReferences[typeIndex]}; + var annotation = new Annotation((ClassReference)Dex.TypeReferences[typeIndex]); for (var i = 0; i < elementSize; i++) { - var argument = new AnnotationArgument(); var nameIndex = (int)reader.ReadULEB128(); - argument.Name = Dex.Strings[nameIndex]; - argument.Value = ReadValue(reader); + var argument = new AnnotationArgument(Dex.Strings[nameIndex], ReadValue(reader)); annotation.Arguments.Add(argument); } @@ -354,7 +342,7 @@ private void ReadPrototypes(BinaryReader reader) var returnTypeIndex = reader.ReadInt32(); var parametersOffset = reader.ReadUInt32(); - var prototype = new Prototype {ReturnType = Dex.TypeReferences[returnTypeIndex]}; + var prototype = new Prototype(Dex.TypeReferences[returnTypeIndex]); if (parametersOffset > 0) { @@ -373,9 +361,8 @@ private void ReadParameters(BinaryReader reader, Prototype prototype, uint param var typecount = reader.ReadUInt32(); for (var j = 0; j < typecount; j++) { - var parameter = new Parameter(); var typeIndex = reader.ReadUInt16(); - parameter.Type = Dex.TypeReferences[typeIndex]; + var parameter = new Parameter(Dex.TypeReferences[typeIndex]); prototype.Parameters.Add(parameter); } }); @@ -459,48 +446,31 @@ private object[] ReadValues(BinaryReader reader) return array.ToArray(); } - private object ReadValue(BinaryReader reader) + private object? ReadValue(BinaryReader reader) { int data = reader.ReadByte(); var valueFormat = data & 0x1F; var valueArgument = data >> 5; - switch ((ValueFormats)valueFormat) + return (ValueFormats)valueFormat switch { - case ValueFormats.Byte: - return (sbyte)reader.ReadSignedPackedNumber(valueArgument + 1); - case ValueFormats.Short: - return (short)reader.ReadSignedPackedNumber(valueArgument + 1); - case ValueFormats.Char: - return (char)reader.ReadUnsignedPackedNumber(valueArgument + 1); - case ValueFormats.Int: - return (int)reader.ReadSignedPackedNumber(valueArgument + 1); - case ValueFormats.Long: - return reader.ReadSignedPackedNumber(valueArgument + 1); - case ValueFormats.Float: - return BitConverter.ToSingle(BitConverter.GetBytes((int)reader.ReadSignedPackedNumber(valueArgument + 1)), 0); - case ValueFormats.Double: - return BitConverter.Int64BitsToDouble(reader.ReadSignedPackedNumber(valueArgument + 1)); - case ValueFormats.String: - return Dex.Strings[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)]; - case ValueFormats.Type: - return Dex.TypeReferences[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)]; - case ValueFormats.Field: - case ValueFormats.Enum: - return Dex.FieldReferences[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)]; - case ValueFormats.Method: - return Dex.MethodReferences[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)]; - case ValueFormats.Array: - return ReadValues(reader); - case ValueFormats.Annotation: - return ReadEncodedAnnotation(reader); - case ValueFormats.Null: - return null; - case ValueFormats.Boolean: - return valueArgument != 0; - default: - throw new ArgumentException(); - } + ValueFormats.Byte => (sbyte)reader.ReadSignedPackedNumber(valueArgument + 1), + ValueFormats.Short => (short)reader.ReadSignedPackedNumber(valueArgument + 1), + ValueFormats.Char => (char)reader.ReadUnsignedPackedNumber(valueArgument + 1), + ValueFormats.Int => (int)reader.ReadSignedPackedNumber(valueArgument + 1), + ValueFormats.Long => reader.ReadSignedPackedNumber(valueArgument + 1), + ValueFormats.Float => BitConverter.ToSingle(BitConverter.GetBytes((int)reader.ReadSignedPackedNumber(valueArgument + 1)), 0), + ValueFormats.Double => BitConverter.Int64BitsToDouble(reader.ReadSignedPackedNumber(valueArgument + 1)), + ValueFormats.String => Dex.Strings[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)], + ValueFormats.Type => Dex.TypeReferences[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)], + ValueFormats.Field or ValueFormats.Enum => Dex.FieldReferences[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)], + ValueFormats.Method => Dex.MethodReferences[(int)reader.ReadUnsignedPackedNumber(valueArgument + 1)], + ValueFormats.Array => ReadValues(reader), + ValueFormats.Annotation => ReadEncodedAnnotation(reader), + ValueFormats.Null => null, + ValueFormats.Boolean => valueArgument != 0, + _ => throw new ArgumentException(), + }; } private void ReadFieldDefinitions(BinaryReader reader, ClassDefinition classDefinition, uint fieldcount) @@ -568,6 +538,9 @@ private void ReadMethodBody(BinaryReader reader, MethodDefinition mdef, uint cod private void ReadDebugInfo(BinaryReader reader, MethodDefinition mdef, uint debugOffset) { + if (mdef.Body == null) + return; + reader.PreserveCurrentPosition(debugOffset, () => { var debugInfo = new DebugInfo(mdef.Body); @@ -580,7 +553,7 @@ private void ReadDebugInfo(BinaryReader reader, MethodDefinition mdef, uint debu for (var i = 0; i < parametersSize; i++) { var index = reader.ReadULEB128P1(); - string name = null; + string? name = null; if (index != DexConsts.NoIndex && index >= 0) name = Dex.Strings[(int)index]; debugInfo.Parameters.Add(name); @@ -593,7 +566,7 @@ private void ReadDebugInfo(BinaryReader reader, MethodDefinition mdef, uint debu uint registerIndex; long nameIndex; - string name; + string? name; switch (ins.OpCode) { @@ -637,7 +610,7 @@ private void ReadDebugInfo(BinaryReader reader, MethodDefinition mdef, uint debu ins.Operands.Add(name); var typeIndex = reader.ReadULEB128P1(); - TypeReference type = null; + TypeReference? type = null; if (typeIndex != DexConsts.NoIndex && typeIndex >= 0) type = Dex.TypeReferences[(int)typeIndex]; ins.Operands.Add(type); @@ -645,7 +618,7 @@ private void ReadDebugInfo(BinaryReader reader, MethodDefinition mdef, uint debu if (isExtended) { var signatureIndex = reader.ReadULEB128P1(); - string signature = null; + string? signature = null; if (signatureIndex != DexConsts.NoIndex && signatureIndex >= 0) signature = Dex.Strings[(int)signatureIndex]; ins.Operands.Add(signature); @@ -667,6 +640,9 @@ private void ReadDebugInfo(BinaryReader reader, MethodDefinition mdef, uint debu private void ReadExceptionHandlers(BinaryReader reader, MethodDefinition mdef, InstructionReader instructionReader, ushort triesSize) { + if (mdef.Body == null) + return; + var exceptionLookup = new Dictionary>(); for (var i = 0; i < triesSize; i++) { @@ -675,17 +651,15 @@ private void ReadExceptionHandlers(BinaryReader reader, MethodDefinition mdef, I var endOffset = startOffset + insCount - 1; uint handlerOffset = reader.ReadUInt16(); - var ehandler = new ExceptionHandler(); - mdef.Body.Exceptions.Add(ehandler); if (!exceptionLookup.ContainsKey(handlerOffset)) { - exceptionLookup.Add(handlerOffset, new List()); + exceptionLookup.Add(handlerOffset, []); } - exceptionLookup[handlerOffset].Add(ehandler); - ehandler.TryStart = instructionReader.Lookup[(int)startOffset]; // The last code unit covered (inclusive) is start_addr + insn_count - 1 - ehandler.TryEnd = instructionReader.LookupLast[(int)endOffset]; + var ehandler = new ExceptionHandler(instructionReader.Lookup[(int)startOffset], instructionReader.LookupLast[(int)endOffset]); + mdef.Body.Exceptions.Add(ehandler); + exceptionLookup[handlerOffset].Add(ehandler); } var baseOffset = reader.BaseStream.Position; @@ -701,7 +675,7 @@ private void ReadExceptionHandlers(BinaryReader reader, MethodDefinition mdef, I { var typeIndex = reader.ReadULEB128(); var offset = reader.ReadULEB128(); - var @catch = new Catch {Type = Dex.TypeReferences[(int)typeIndex], Instruction = instructionReader.Lookup[(int)offset]}; + var @catch = new Catch(Dex.TypeReferences[(int)typeIndex], instructionReader.Lookup[(int)offset]); // As catch handler can be used in several tries, let's clone the catch foreach (var ehandler in exceptionLookup[(uint)itemoffset]) @@ -747,13 +721,11 @@ private void ReadMethodReferences(BinaryReader reader) int prototypeIndex = reader.ReadUInt16(); int nameIndex = reader.ReadInt32(); - var mref = new MethodReference - { - Owner = (CompositeType)Dex.TypeReferences[classIndex], - // Clone the prototype so we can annotate & update it easily - Prototype = Dex.Prototypes[prototypeIndex].Clone(), - Name = Dex.Strings[nameIndex] - }; + var mref = new MethodReference( + (CompositeType)Dex.TypeReferences[classIndex], + Dex.Strings[nameIndex], + Dex.Prototypes[prototypeIndex].Clone() // Clone the prototype so we can annotate & update it easily + ); Dex.MethodReferences.Add(mref); } @@ -770,7 +742,7 @@ private void ReadFieldReferences(BinaryReader reader) var typeIndex = reader.ReadUInt16(); var nameIndex = reader.ReadInt32(); - var fref = new FieldReference {Owner = (ClassReference)Dex.TypeReferences[classIndex], Type = Dex.TypeReferences[typeIndex], Name = Dex.Strings[nameIndex]}; + var fref = new FieldReference((ClassReference)Dex.TypeReferences[classIndex], Dex.TypeReferences[typeIndex], Dex.Strings[nameIndex]); Dex.FieldReferences.Add(fref); } diff --git a/Dexer/IO/DexWriter.cs b/Dexer/IO/DexWriter.cs index 40dd7be..90caf12 100644 --- a/Dexer/IO/DexWriter.cs +++ b/Dexer/IO/DexWriter.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,11 +19,7 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; using System.Globalization; -using System.IO; -using System.Linq; using System.Security.Cryptography; using System.Text; using Dexer.Core; @@ -36,53 +32,34 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.IO; -internal class DexWriter +internal class DexWriter(Dex dex) { - internal Dex Dex { get; set; } - internal Map Map { get; set; } - private List FlatClasses { get; set; } - - private HeaderMarkers HeaderMarkers { get; } - private List StringMarkers { get; } - private List PrototypeTypeListMarkers { get; } - private List ClassDefinitionsMarkers { get; } - private Dictionary AnnotationSetRefListMarkers { get; } - private Dictionary AnnotationSetMarkers { get; } - private Dictionary DebugMarkers { get; } - - private Dictionary TypeLists { get; } - private Dictionary AnnotationSets { get; } - private Dictionary AnnotationSetRefLists { get; } - private Dictionary Codes { get; } - - internal Dictionary StringLookup { get; set; } - internal Dictionary PrototypeLookup { get; set; } - internal Dictionary TypeLookup { get; set; } - internal Dictionary MethodLookup { get; set; } - internal Dictionary FieldLookup { get; set; } - - internal byte[] Signature { get; set; } + internal Dex Dex { get; set; } = dex; + internal Map Map { get; set; } = []; + private List FlatClasses { get; set; } = []; + + private HeaderMarkers HeaderMarkers { get; } = new HeaderMarkers(); + private List StringMarkers { get; } = []; + private List PrototypeTypeListMarkers { get; } = []; + private List ClassDefinitionsMarkers { get; } = []; + private Dictionary AnnotationSetRefListMarkers { get; } = []; + private Dictionary AnnotationSetMarkers { get; } = []; + private Dictionary DebugMarkers { get; } = []; + + private Dictionary TypeLists { get; } = []; + private Dictionary AnnotationSets { get; } = []; + private Dictionary AnnotationSetRefLists { get; } = []; + private Dictionary Codes { get; } = []; + + internal Dictionary StringLookup { get; set; } = []; + internal Dictionary PrototypeLookup { get; set; } = []; + internal Dictionary TypeLookup { get; set; } = []; + internal Dictionary MethodLookup { get; set; } = []; + internal Dictionary FieldLookup { get; set; } = []; + + internal byte[] Signature { get; set; } = []; internal uint CheckSum { get; set; } - public DexWriter(Dex dex) - { - Dex = dex; - Map = new Map(); - - HeaderMarkers = new HeaderMarkers(); - StringMarkers = new List(); - PrototypeTypeListMarkers = new List(); - ClassDefinitionsMarkers = new List(); - AnnotationSetRefListMarkers = new Dictionary(); - AnnotationSetMarkers = new Dictionary(); - DebugMarkers = new Dictionary(); - - TypeLists = new Dictionary(); - AnnotationSets = new Dictionary(); - AnnotationSetRefLists = new Dictionary(); - Codes = new Dictionary(); - } - #region Header private void WriteHeader(BinaryWriter writer) @@ -140,7 +117,7 @@ private void WriteStringId(BinaryWriter writer) { if (Dex.Strings.Count > 0) { - HeaderMarkers.StringsMarker.Value = new SizeOffset((uint)Dex.Strings.Count, (uint)writer.BaseStream.Position); + HeaderMarkers.StringsMarker!.Value = new SizeOffset((uint)Dex.Strings.Count, (uint)writer.BaseStream.Position); Map.Add(TypeCodes.StringId, new MapItem(TypeCodes.StringId, (uint)Dex.Strings.Count, (uint)writer.BaseStream.Position)); } @@ -189,7 +166,7 @@ private void WritePrototypeId(BinaryWriter writer) { if (Dex.Prototypes.Count > 0) { - HeaderMarkers.PrototypesMarker.Value = new SizeOffset((uint)Dex.Prototypes.Count, (uint)writer.BaseStream.Position); + HeaderMarkers.PrototypesMarker!.Value = new SizeOffset((uint)Dex.Prototypes.Count, (uint)writer.BaseStream.Position); Map.Add(TypeCodes.ProtoId, new MapItem(TypeCodes.ProtoId, (uint)Dex.Prototypes.Count, (uint)writer.BaseStream.Position)); } @@ -232,8 +209,7 @@ private void WriteTypeLists(BinaryWriter writer) var sectionOffset = (uint)writer.BaseStream.Position; for (var i = 0; i < FlatClasses.Count; i++) - WriteTypeList(writer, ref sectionOffset, ComputeTypeList(FlatClasses[i].Interfaces), - ClassDefinitionsMarkers[i].InterfacesMarker); + WriteTypeList(writer, ref sectionOffset, ComputeTypeList(FlatClasses[i].Interfaces), ClassDefinitionsMarkers[i].InterfacesMarker!); for (var i = 0; i < Dex.Prototypes.Count; i++) WriteTypeList(writer, ref sectionOffset, ComputeTypeList(Dex.Prototypes[i].Parameters), @@ -270,7 +246,7 @@ private void WriteFieldId(BinaryWriter writer) { if (Dex.FieldReferences.Count > 0) { - HeaderMarkers.FieldReferencesMarker.Value = new SizeOffset((uint)Dex.FieldReferences.Count, (uint)writer.BaseStream.Position); + HeaderMarkers.FieldReferencesMarker!.Value = new SizeOffset((uint)Dex.FieldReferences.Count, (uint)writer.BaseStream.Position); Map.Add(TypeCodes.FieldId, new MapItem(TypeCodes.FieldId, (uint)Dex.FieldReferences.Count, (uint)writer.BaseStream.Position)); } @@ -290,7 +266,7 @@ private void WriteMethodId(BinaryWriter writer) { if (Dex.MethodReferences.Count > 0) { - HeaderMarkers.MethodReferencesMarker.Value = new SizeOffset((uint)Dex.MethodReferences.Count, (uint)writer.BaseStream.Position); + HeaderMarkers.MethodReferencesMarker!.Value = new SizeOffset((uint)Dex.MethodReferences.Count, (uint)writer.BaseStream.Position); Map.Add(TypeCodes.MethodId, new MapItem(TypeCodes.MethodId, (uint)Dex.MethodReferences.Count, (uint)writer.BaseStream.Position)); } @@ -310,7 +286,7 @@ private void WriteTypeId(BinaryWriter writer) { if (Dex.TypeReferences.Count > 0) { - HeaderMarkers.TypeReferencesMarker.Value = new SizeOffset((uint)Dex.TypeReferences.Count, (uint)writer.BaseStream.Position); + HeaderMarkers.TypeReferencesMarker!.Value = new SizeOffset((uint)Dex.TypeReferences.Count, (uint)writer.BaseStream.Position); Map.Add(TypeCodes.TypeId, new MapItem(TypeCodes.TypeId, (uint)Dex.TypeReferences.Count, (uint)writer.BaseStream.Position)); } @@ -329,7 +305,7 @@ private void WriteClassDef(BinaryWriter writer) if (FlatClasses.Count > 0) { writer.EnsureSectionAlignment(ref sectionOffset, 4); - HeaderMarkers.ClassDefinitionsMarker.Value = new SizeOffset((uint)FlatClasses.Count, sectionOffset); + HeaderMarkers.ClassDefinitionsMarker!.Value = new SizeOffset((uint)FlatClasses.Count, sectionOffset); Map.Add(TypeCodes.ClassDef, new MapItem(TypeCodes.ClassDef, (uint)FlatClasses.Count, sectionOffset)); } @@ -344,7 +320,7 @@ private void WriteClassDef(BinaryWriter writer) writer.Write((uint)flatclass.AccessFlags); writer.Write(flatclass.SuperClass == null ? DexConsts.NoIndex : (uint)TypeLookup[flatclass.SuperClass]); markers.InterfacesMarker = writer.MarkUInt(); - writer.Write(string.IsNullOrEmpty(flatclass.SourceFile) ? DexConsts.NoIndex : (uint)StringLookup[flatclass.SourceFile]); + writer.Write(string.IsNullOrEmpty(flatclass.SourceFile) ? DexConsts.NoIndex : (uint)StringLookup[flatclass.SourceFile!]); markers.AnnotationsMarker = writer.MarkUInt(); markers.ClassDataMarker = writer.MarkUInt(); markers.StaticValuesMarker = writer.MarkUInt(); @@ -466,7 +442,7 @@ private void WriteEncodedAnnotation(BinaryWriter writer, Annotation annotation) } } - private void WriteValue(BinaryWriter writer, object value) + private void WriteValue(BinaryWriter writer, object? value) { int valueArgument = 0; @@ -489,17 +465,29 @@ private void WriteValue(BinaryWriter writer, object value) valueArgument = writer.GetByteCountForSignedPackedNumber(BitConverter.DoubleToInt64Bits(Convert.ToDouble(value))) - 1; break; case ValueFormats.String: - valueArgument = writer.GetByteCountForUnsignedPackedNumber(StringLookup[(string)value]) - 1; + if (value is not string str) + throw new ArgumentException($"Expecting {format}"); + + valueArgument = writer.GetByteCountForUnsignedPackedNumber(StringLookup[str]) - 1; break; case ValueFormats.Type: - valueArgument = writer.GetByteCountForUnsignedPackedNumber(TypeLookup[(TypeReference)value]) - 1; + if (value is not TypeReference tref) + throw new ArgumentException($"Expecting {format}"); + + valueArgument = writer.GetByteCountForUnsignedPackedNumber(TypeLookup[tref]) - 1; break; case ValueFormats.Field: case ValueFormats.Enum: - valueArgument = writer.GetByteCountForUnsignedPackedNumber(FieldLookup[(FieldReference)value]) - 1; + if (value is not FieldReference fref) + throw new ArgumentException($"Expecting {format}"); + + valueArgument = writer.GetByteCountForUnsignedPackedNumber(FieldLookup[fref]) - 1; break; case ValueFormats.Method: - valueArgument = writer.GetByteCountForUnsignedPackedNumber(MethodLookup[(MethodReference)value]) - 1; + if (value is not MethodReference mref) + throw new ArgumentException($"Expecting {format}"); + + valueArgument = writer.GetByteCountForUnsignedPackedNumber(MethodLookup[mref]) - 1; break; case ValueFormats.Boolean: valueArgument = Convert.ToInt32(Convert.ToBoolean(value)); @@ -528,23 +516,29 @@ private void WriteValue(BinaryWriter writer, object value) writer.WritePackedSignedNumber(BitConverter.DoubleToInt64Bits(Convert.ToDouble(value))); break; case ValueFormats.String: - writer.WriteUnsignedPackedNumber(StringLookup[(string)value]); + writer.WriteUnsignedPackedNumber(StringLookup[(string)value!]); break; case ValueFormats.Type: - writer.WriteUnsignedPackedNumber(TypeLookup[(TypeReference)value]); + writer.WriteUnsignedPackedNumber(TypeLookup[(TypeReference)value!]); break; case ValueFormats.Field: case ValueFormats.Enum: - writer.WriteUnsignedPackedNumber(FieldLookup[(FieldReference)value]); + writer.WriteUnsignedPackedNumber(FieldLookup[(FieldReference)value!]); break; case ValueFormats.Method: - writer.WriteUnsignedPackedNumber(MethodLookup[(MethodReference)value]); + writer.WriteUnsignedPackedNumber(MethodLookup[(MethodReference)value!]); break; case ValueFormats.Array: - WriteValues(writer, (object[])value); + if (value is not object[] array) + throw new ArgumentException($"Expecting {format}"); + + WriteValues(writer, array); break; case ValueFormats.Annotation: - WriteEncodedAnnotation(writer, value as Annotation); + if (value is not Annotation annotation) + throw new ArgumentException($"Expecting {format}"); + + WriteEncodedAnnotation(writer, annotation); break; case ValueFormats.Null: case ValueFormats.Boolean: @@ -554,7 +548,7 @@ private void WriteValue(BinaryWriter writer, object value) } } - private void WriteValues(BinaryWriter writer, ICollection values) + private void WriteValues(BinaryWriter writer, ICollection values) { writer.WriteULEB128((uint)values.Count); foreach (var value in values) @@ -642,8 +636,7 @@ private void WriteAnnotationsDirectory(BinaryWriter writer) annotatedParametersList.Add(method); } - var total = @class.Annotations.Count + annotatedFields.Count + annotatedMethods.Count + - annotatedParametersList.Count; + var total = @class.Annotations.Count + annotatedFields.Count + annotatedMethods.Count + annotatedParametersList.Count; if (total <= 0) continue; @@ -657,14 +650,14 @@ private void WriteAnnotationsDirectory(BinaryWriter writer) var set = new AnnotationSet(@class); if (classAnnotationSets.ContainsKey(set)) { - ClassDefinitionsMarkers[index].AnnotationsMarker.Value = classAnnotationSets[set]; + ClassDefinitionsMarkers[index].AnnotationsMarker!.Value = classAnnotationSets[set]; continue; } classAnnotationSets.Add(set, (uint)writer.BaseStream.Position); } - ClassDefinitionsMarkers[index].AnnotationsMarker.Value = (uint)writer.BaseStream.Position; + ClassDefinitionsMarkers[index].AnnotationsMarker!.Value = (uint)writer.BaseStream.Position; count++; if (@class.Annotations.Count > 0) @@ -676,28 +669,25 @@ private void WriteAnnotationsDirectory(BinaryWriter writer) writer.Write(annotatedMethods.Count); writer.Write(annotatedParametersList.Count); - var fields = new List(annotatedFields); - fields.Sort(new FieldReferenceComparer()); - foreach (var field in fields) + annotatedFields.Sort(new FieldReferenceComparer()); + foreach (var field in annotatedFields) { writer.Write(FieldLookup[field]); - writer.Write(AnnotationSets[new AnnotationSet(field as IAnnotationProvider)]); + writer.Write(AnnotationSets[new AnnotationSet(field)]); } - var methods = new List(annotatedMethods); - methods.Sort(new MethodReferenceComparer()); - foreach (var method in methods) + annotatedMethods.Sort(new MethodReferenceComparer()); + foreach (var method in annotatedMethods) { writer.Write(MethodLookup[method]); - writer.Write(AnnotationSets[new AnnotationSet(method as IAnnotationProvider)]); + writer.Write(AnnotationSets[new AnnotationSet(method)]); } - methods = new List(annotatedParametersList); - methods.Sort(new MethodReferenceComparer()); - foreach (var method in methods) + annotatedParametersList.Sort(new MethodReferenceComparer()); + foreach (var method in annotatedParametersList) { writer.Write(MethodLookup[method]); - writer.Write(AnnotationSetRefLists[(MethodDefinition)method]); + writer.Write(AnnotationSetRefLists[method]); } } @@ -749,7 +739,7 @@ private void WriteCode(BinaryWriter writer) var set = new CatchSet(handler); if (!catchHandlers.ContainsKey(set)) - catchHandlers.Add(set, new List()); + catchHandlers.Add(set, []); catchHandlers[set].Add(handler); } @@ -793,10 +783,10 @@ private void WriteCode(BinaryWriter writer) #region DebugInfo - private static void CheckOperand(DebugInstruction ins, int operandCount, params Type[] types) + private static void CheckOperand(DebugInstruction ins, params Type[] types) { - if (ins.Operands.Count != operandCount) - throw new DebugInstructionException(ins, $"Expecting {operandCount} operands"); + if (ins.Operands.Count != types.Length) + throw new DebugInstructionException(ins, $"Expecting {types.Length} operands"); for (int i = 0; i < ins.Operands.Count; i++) { @@ -844,40 +834,40 @@ private void WriteDebugInfo(BinaryWriter writer) if (string.IsNullOrEmpty(parameter)) writer.WriteULEB128P1(DexConsts.NoIndex); else - writer.WriteULEB128P1(StringLookup[parameter]); + writer.WriteULEB128P1(StringLookup[parameter!]); } foreach (var ins in debugInfo.DebugInstructions) { - string name; + string? name; writer.Write((byte)ins.OpCode); switch (ins.OpCode) { case DebugOpCodes.AdvancePc: // uleb128 addr_diff - CheckOperand(ins, 1, typeof(uint)); + CheckOperand(ins, typeof(uint)); writer.WriteULEB128(Convert.ToUInt32(ins.Operands[0])); break; case DebugOpCodes.AdvanceLine: // sleb128 line_diff - CheckOperand(ins, 1, typeof(int)); + CheckOperand(ins, typeof(int)); writer.WriteSLEB128(Convert.ToInt32(ins.Operands[0])); break; case DebugOpCodes.EndLocal: case DebugOpCodes.RestartLocal: // uleb128 register_num - CheckOperand(ins, 1, typeof(Register)); - writer.WriteULEB128((uint)((Register)ins.Operands[0]).Index); + CheckOperand(ins, typeof(Register)); + writer.WriteULEB128((uint)((Register)ins.Operands[0]!).Index); break; case DebugOpCodes.SetFile: // uleb128p1 name_idx - CheckOperand(ins, 1, typeof(string)); - name = (string)ins.Operands[0]; + CheckOperand(ins, typeof(string)); + name = ins.Operands[0] as string; if (string.IsNullOrEmpty(name)) writer.WriteULEB128P1(DexConsts.NoIndex); else - writer.WriteULEB128P1(StringLookup[name]); + writer.WriteULEB128P1(StringLookup[name!]); break; case DebugOpCodes.StartLocalExtended: case DebugOpCodes.StartLocal: @@ -886,19 +876,19 @@ private void WriteDebugInfo(BinaryWriter writer) var isExtended = ins.OpCode == DebugOpCodes.StartLocalExtended; if (isExtended) - CheckOperand(ins, 4, typeof(Register), typeof(string), typeof(TypeReference), typeof(string)); + CheckOperand(ins, typeof(Register), typeof(string), typeof(TypeReference), typeof(string)); else - CheckOperand(ins, 3, typeof(Register), typeof(string), typeof(TypeReference)); + CheckOperand(ins, typeof(Register), typeof(string), typeof(TypeReference)); - writer.WriteULEB128((uint)((Register)ins.Operands[0]).Index); + writer.WriteULEB128((uint)((Register)ins.Operands[0]!).Index); - name = (string)ins.Operands[1]; + name = ins.Operands[1] as string; if (string.IsNullOrEmpty(name)) writer.WriteULEB128P1(DexConsts.NoIndex); else - writer.WriteULEB128P1(StringLookup[name]); + writer.WriteULEB128P1(StringLookup[name!]); - var type = (TypeReference)ins.Operands[2]; + var type = (TypeReference)ins.Operands[2]!; if (type == null) writer.WriteULEB128P1(DexConsts.NoIndex); else @@ -906,11 +896,11 @@ private void WriteDebugInfo(BinaryWriter writer) if (isExtended) { - var signature = (string)ins.Operands[3]; + var signature = ins.Operands[3] as string; if (string.IsNullOrEmpty(signature)) writer.WriteULEB128P1(DexConsts.NoIndex); else - writer.WriteULEB128P1(StringLookup[signature]); + writer.WriteULEB128P1(StringLookup[signature!]); } break; @@ -955,7 +945,7 @@ private void WriteEncodedArray(BinaryWriter writer) for (var c = 0; c < FlatClasses.Count; c++) { var @class = FlatClasses[c]; - var values = new List(); + var values = new List(); var lastNonNullIndex = -1; for (var i = 0; i < @class.Fields.Count; i++) @@ -1016,7 +1006,7 @@ private void WriteEncodedArray(BinaryWriter writer) writer.Write(buffer); } - ClassDefinitionsMarkers[c].StaticValuesMarker.Value = buffers[key]; + ClassDefinitionsMarkers[c].StaticValuesMarker!.Value = buffers[key]; } } @@ -1045,7 +1035,7 @@ private void WriteClassData(BinaryWriter writer) if (staticFields.Count + instanceFields.Count + virtualMethods.Count + directMethods.Count <= 0) continue; - ClassDefinitionsMarkers[i].ClassDataMarker.Value = (uint)writer.BaseStream.Position; + ClassDefinitionsMarkers[i].ClassDataMarker!.Value = (uint)writer.BaseStream.Position; count++; writer.WriteULEB128((uint)staticFields.Count); @@ -1105,7 +1095,7 @@ private void WriteMapList(BinaryWriter writer) var sectionOffset = (uint)writer.BaseStream.Position; writer.EnsureSectionAlignment(ref sectionOffset, 4); - HeaderMarkers.MapMarker.Value = sectionOffset; + HeaderMarkers.MapMarker!.Value = sectionOffset; Map.Add(TypeCodes.MapList, new MapItem(TypeCodes.MapList, 1, sectionOffset)); writer.Write(Map.Count); @@ -1118,13 +1108,13 @@ private void WriteMapList(BinaryWriter writer) } var filesize = (uint)writer.BaseStream.Position; - HeaderMarkers.FileSizeMarker.Value = filesize; + HeaderMarkers.FileSizeMarker!.Value = filesize; var lastEntry = TypeCodes.Header; foreach (var type in Map.Keys) { if (lastEntry == TypeCodes.ClassDef) - HeaderMarkers.DataMarker.Value = new SizeOffset(filesize - Map[type].Offset, Map[type].Offset); + HeaderMarkers.DataMarker!.Value = new SizeOffset(filesize - Map[type].Offset, Map[type].Offset); lastEntry = type; } @@ -1136,7 +1126,7 @@ private void WriteMapList(BinaryWriter writer) private byte[] ComputeSHA1Signature(BinaryWriter writer) { - writer.Seek((int)HeaderMarkers.SignatureMarker.Positions[0] + DexConsts.SignatureSize, SeekOrigin.Begin); + writer.Seek((int)HeaderMarkers.SignatureMarker!.Positions[0] + DexConsts.SignatureSize, SeekOrigin.Begin); var crypto = new SHA1CryptoServiceProvider(); var signature = crypto.ComputeHash(writer.BaseStream); HeaderMarkers.SignatureMarker.Value = signature; @@ -1145,7 +1135,7 @@ private byte[] ComputeSHA1Signature(BinaryWriter writer) private uint ComputeAdlerCheckSum(BinaryWriter writer) { - writer.Seek((int)HeaderMarkers.SignatureMarker.Positions[0], SeekOrigin.Begin); + writer.Seek((int)HeaderMarkers.SignatureMarker!.Positions[0], SeekOrigin.Begin); ushort s1 = 1; ushort s2 = 0; int value; @@ -1156,7 +1146,7 @@ private uint ComputeAdlerCheckSum(BinaryWriter writer) } var checksum = (uint)(s2 << 16 | s1); - HeaderMarkers.CheckSumMarker.Value = checksum; + HeaderMarkers.CheckSumMarker!.Value = checksum; return checksum; } diff --git a/Dexer/IO/InstructionException.cs b/Dexer/IO/InstructionException.cs index da7c631..24c2077 100644 --- a/Dexer/IO/InstructionException.cs +++ b/Dexer/IO/InstructionException.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,12 +23,7 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.IO; -public class InstructionException : MalformedException +public class InstructionException(Instruction instruction, string message) : MalformedException(message) { - public Instruction Instruction { get; set; } - - public InstructionException(Instruction instruction, string message) : base(message) - { - Instruction = instruction; - } + public Instruction Instruction { get; set; } = instruction; } diff --git a/Dexer/IO/InstructionReader.cs b/Dexer/IO/InstructionReader.cs index 1cc5986..81f7c08 100644 --- a/Dexer/IO/InstructionReader.cs +++ b/Dexer/IO/InstructionReader.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,9 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; -using System.Collections.Generic; -using System.IO; using Dexer.Core; using Dexer.Instructions; @@ -30,57 +27,56 @@ namespace Dexer.IO; internal class InstructionReader { private MethodDefinition MethodDefinition { get; } - private List LazyInstructionsSetters { get; } + private List LazyInstructionsSetters { get; } = []; private Dex Dex { get; } - internal int[] Codes { get; set; } - private int[] Lower { get; set; } - private int[] Upper { get; set; } - private int _ip; + internal int[] Codes { get; set; } = []; + private int[] Lower { get; set; } = []; + private int[] Upper { get; set; } = []; + private int _ip = 0; private uint InstructionsSize { get; set; } - internal Dictionary Lookup; // instructions by starting offset - internal Dictionary LookupLast; // instructions by ending offset + internal Dictionary Lookup { get; } = []; // instructions by starting offset + internal Dictionary LookupLast { get; } = []; // instructions by ending offset public InstructionReader(Dex dex, MethodDefinition mdef) { + if (mdef.Body == null) + throw new ArgumentNullException(nameof(mdef.Body)); + Dex = dex; MethodDefinition = mdef; - Lookup = new Dictionary(); - LookupLast = new Dictionary(); - LazyInstructionsSetters = new List(); - _ip = 0; } private void ReadvA(Instruction ins) { - ins.Registers.Add(MethodDefinition.Body.Registers[Upper[_ip] & 0xF]); + ins.Registers.Add(MethodDefinition.Body!.Registers[Upper[_ip] & 0xF]); } private void ReadvAA(Instruction ins) { - ins.Registers.Add(MethodDefinition.Body.Registers[Upper[_ip++]]); + ins.Registers.Add(MethodDefinition.Body!.Registers[Upper[_ip++]]); } private void ReadvAAAA(Instruction ins) { _ip++; - ins.Registers.Add(MethodDefinition.Body.Registers[Codes[_ip++]]); + ins.Registers.Add(MethodDefinition.Body!.Registers[Codes[_ip++]]); } private void ReadvB(Instruction ins) { - ins.Registers.Add(MethodDefinition.Body.Registers[Upper[_ip++] >> 4]); + ins.Registers.Add(MethodDefinition.Body!.Registers[Upper[_ip++] >> 4]); } private void ReadvBB(Instruction ins) { - ins.Registers.Add(MethodDefinition.Body.Registers[Lower[_ip]]); + ins.Registers.Add(MethodDefinition.Body!.Registers[Lower[_ip]]); } private void ReadvBBBB(Instruction ins) { - ins.Registers.Add(MethodDefinition.Body.Registers[Codes[_ip++]]); + ins.Registers.Add(MethodDefinition.Body!.Registers[Codes[_ip++]]); } private void ReadvCC(Instruction ins) @@ -133,7 +129,7 @@ private byte ReadUByte() public void ReadFrom(BinaryReader reader) { - var registers = MethodDefinition.Body.Registers; + var registers = MethodDefinition.Body!.Registers; InstructionsSize = reader.ReadUInt32(); Codes = new int[InstructionsSize]; @@ -553,7 +549,7 @@ private void SetRegistersByMask(Instruction ins, int registerMask) { var registerCount = registerMask >> 20; for (var i = 0; i < registerCount; i++) - ins.Registers.Add(MethodDefinition.Body.Registers[(registerMask >> (i * 4)) & 0xF]); + ins.Registers.Add(MethodDefinition.Body!.Registers[(registerMask >> (i * 4)) & 0xF]); } // ReSharper disable UnusedParameter.Local @@ -652,6 +648,6 @@ private object[] ExtractArrayData(int offset) if (offset - baseOffset != (elementsize * elementcount + 1) / 2 + 4) throw new MalformedException("Unexpected Fill-array-data blocksize"); - return items.ToArray(); + return [.. items]; } } diff --git a/Dexer/IO/InstructionWriter.cs b/Dexer/IO/InstructionWriter.cs index eb57172..42b7bc8 100644 --- a/Dexer/IO/InstructionWriter.cs +++ b/Dexer/IO/InstructionWriter.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,11 +19,8 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.IO; using Dexer.Core; using Dexer.Instructions; -using System.Collections.Generic; -using System; namespace Dexer.IO; @@ -31,24 +28,24 @@ internal class InstructionWriter { private DexWriter DexWriter { get; } private MethodDefinition MethodDefinition { get; } - internal ushort[] Codes { get; set; } - private int _ip; - private int _extraOffset; + internal ushort[] Codes { get; set; } = []; + private int _ip = 0; + private int _extraOffset = 0; - internal Dictionary LookupLast; // ending offsets by instruction + internal Dictionary LookupLast { get; } = []; // ending offsets by instruction public InstructionWriter(DexWriter dexWriter, MethodDefinition method) { + if (method.Body == null) + throw new ArgumentNullException(nameof(method.Body)); + DexWriter = dexWriter; MethodDefinition = method; - LookupLast = new Dictionary(); - _ip = 0; - _extraOffset = 0; } public void WriteTo(BinaryWriter writer) { - var stats = MethodDefinition.Body.UpdateInstructionOffsets(); + var stats = MethodDefinition.Body!.UpdateInstructionOffsets(); var alignedCodeUnits = stats.CodeUnits; if (stats.ExtraCodeUnits > 0 && alignedCodeUnits % 2 != 0) @@ -461,20 +458,20 @@ private static int GetRegisterMask(Instruction ins) private void WriteNibble(Instruction ins) { - Codes[_ip++] |= (ushort)((int)ins.Operand << 12); + Codes[_ip++] |= (ushort)(Convert.ToSByte(ins.Operand) << 12); } - private void WriteSByte(object value) + private void WriteSByte(object? value) { Codes[_ip++] |= (ushort)(Convert.ToSByte(value) << 8); } private void WriteSbyteInstructionOffset(Instruction ins) { - if (!(ins.Operand is Instruction)) + if (ins.Operand is not Instruction instruction) throw new InstructionException(ins, "Expecting Instruction"); - WriteSByte(((Instruction)ins.Operand).Offset - ins.Offset); + WriteSByte(instruction.Offset - ins.Offset); } private void WriteSByte(Instruction ins) @@ -484,42 +481,42 @@ private void WriteSByte(Instruction ins) private void WriteShortInstructionOffset(Instruction ins) { - if (!(ins.Operand is Instruction)) + if (ins.Operand is not Instruction instruction) throw new InstructionException(ins, "Expecting Instruction"); - WriteShort(((Instruction)ins.Operand).Offset - ins.Offset, ref _ip); + WriteShort(instruction.Offset - ins.Offset, ref _ip); } private void WriteShortFieldIndex(Instruction ins) { - if (!(ins.Operand is FieldReference)) + if (ins.Operand is not FieldReference field) throw new InstructionException(ins, "Expecting FieldReference"); - WriteUShort(DexWriter.FieldLookup[(FieldReference)ins.Operand], ref _ip); + WriteUShort(DexWriter.FieldLookup[field], ref _ip); } private void WriteShortMethodIndex(Instruction ins) { - if (!(ins.Operand is MethodReference)) + if (ins.Operand is not MethodReference method) throw new InstructionException(ins, "Expecting MethodReference"); - WriteUShort(DexWriter.MethodLookup[(MethodReference)ins.Operand], ref _ip); + WriteUShort(DexWriter.MethodLookup[method], ref _ip); } private void WriteShortStringIndex(Instruction ins) { - if (ins.Operand is not string operand) + if (ins.Operand is not string str) throw new InstructionException(ins, "Expecting String"); - WriteUShort(DexWriter.StringLookup[operand], ref _ip); + WriteUShort(DexWriter.StringLookup[str], ref _ip); } private void WriteShortTypeIndex(Instruction ins) { - if (ins.Operand is not TypeReference operand) + if (ins.Operand is not TypeReference tref) throw new InstructionException(ins, "Expecting TypeReference"); - WriteUShort(DexWriter.TypeLookup[operand], ref _ip); + WriteUShort(DexWriter.TypeLookup[tref], ref _ip); } private void WriteShort(Instruction ins, int shift) @@ -533,7 +530,7 @@ private void WriteShort(Instruction ins) WriteShort(ins.Operand, ref _ip); } - private void WriteShort(object value, ref int codeUnitOffset) + private void WriteShort(object? value, ref int codeUnitOffset) { Codes[codeUnitOffset++] = (ushort)Convert.ToInt16(value); } @@ -545,21 +542,21 @@ private void WriteUShort(object value, ref int codeUnitOffset) private void WriteIntInstructionOffset(Instruction ins) { - if (!(ins.Operand is Instruction)) + if (ins.Operand is not Instruction instruction) throw new InstructionException(ins, "Expecting Instruction"); - WriteInt(((Instruction)ins.Operand).Offset - ins.Offset, ref _ip); + WriteInt(instruction.Offset - ins.Offset, ref _ip); } private void WriteIntStringIndex(Instruction ins) { - if (ins.Operand is not string operand) + if (ins.Operand is not string str) throw new InstructionException(ins, "Expecting String"); - WriteUInt(DexWriter.StringLookup[operand], ref _ip); + WriteUInt(DexWriter.StringLookup[str], ref _ip); } - private void WriteInt(object value, ref int codeUnitOffset) + private void WriteInt(object? value, ref int codeUnitOffset) { var result = Convert.ToInt32(value); Codes[codeUnitOffset++] = (ushort)(result & 0xffff); @@ -578,7 +575,7 @@ private void WriteInt(Instruction ins) WriteInt(ins.Operand, ref _ip); } - private void WriteLong(object value, ref int codeUnitOffset) + private void WriteLong(object? value, ref int codeUnitOffset) { var result = Convert.ToInt64(value); Codes[codeUnitOffset++] = (ushort)(result & 0xffff); @@ -635,7 +632,7 @@ private void WriteArrayData(Instruction ins) { case 1: if (next) - Codes[_extraOffset++] |= (ushort)((byte)(Convert.ToSByte(element)) << 8); + Codes[_extraOffset++] |= (ushort)((byte)Convert.ToSByte(element) << 8); else Codes[_extraOffset] |= (byte)Convert.ToSByte(element); next = !next; diff --git a/Dexer/IO/MalformedException.cs b/Dexer/IO/MalformedException.cs index f01275a..648781d 100644 --- a/Dexer/IO/MalformedException.cs +++ b/Dexer/IO/MalformedException.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,13 +19,8 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System; - namespace Dexer.IO; -public class MalformedException : Exception +public class MalformedException(string message) : Exception(message) { - public MalformedException(string message) : base(message) - { - } } diff --git a/Dexer/IO/Markers/ClassDefinitionMarkers.cs b/Dexer/IO/Markers/ClassDefinitionMarkers.cs index 05bcf1c..91c9233 100644 --- a/Dexer/IO/Markers/ClassDefinitionMarkers.cs +++ b/Dexer/IO/Markers/ClassDefinitionMarkers.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,8 +23,8 @@ namespace Dexer.IO.Markers; internal class ClassDefinitionMarkers { - public UIntMarker InterfacesMarker { get; set; } - public UIntMarker AnnotationsMarker { get; set; } - public UIntMarker ClassDataMarker { get; set; } - public UIntMarker StaticValuesMarker { get; set; } + public UIntMarker? InterfacesMarker { get; set; } + public UIntMarker? AnnotationsMarker { get; set; } + public UIntMarker? ClassDataMarker { get; set; } + public UIntMarker? StaticValuesMarker { get; set; } } diff --git a/Dexer/IO/Markers/HeaderMarkers.cs b/Dexer/IO/Markers/HeaderMarkers.cs index 04b35e6..5588541 100644 --- a/Dexer/IO/Markers/HeaderMarkers.cs +++ b/Dexer/IO/Markers/HeaderMarkers.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -23,16 +23,16 @@ namespace Dexer.IO.Markers; internal class HeaderMarkers { - public UIntMarker CheckSumMarker { get; set; } - public UIntMarker FileSizeMarker { get; set; } - public SignatureMarker SignatureMarker { get; set; } - public SizeOffsetMarker LinkMarker { get; set; } - public UIntMarker MapMarker { get; set; } - public SizeOffsetMarker StringsMarker { get; set; } - public SizeOffsetMarker TypeReferencesMarker { get; set; } - public SizeOffsetMarker PrototypesMarker { get; set; } - public SizeOffsetMarker FieldReferencesMarker { get; set; } - public SizeOffsetMarker MethodReferencesMarker { get; set; } - public SizeOffsetMarker ClassDefinitionsMarker { get; set; } - public SizeOffsetMarker DataMarker { get; set; } + public UIntMarker? CheckSumMarker { get; set; } + public UIntMarker? FileSizeMarker { get; set; } + public SignatureMarker? SignatureMarker { get; set; } + public SizeOffsetMarker? LinkMarker { get; set; } + public UIntMarker? MapMarker { get; set; } + public SizeOffsetMarker? StringsMarker { get; set; } + public SizeOffsetMarker? TypeReferencesMarker { get; set; } + public SizeOffsetMarker? PrototypesMarker { get; set; } + public SizeOffsetMarker? FieldReferencesMarker { get; set; } + public SizeOffsetMarker? MethodReferencesMarker { get; set; } + public SizeOffsetMarker? ClassDefinitionsMarker { get; set; } + public SizeOffsetMarker? DataMarker { get; set; } } diff --git a/Dexer/IO/Markers/Marker.cs b/Dexer/IO/Markers/Marker.cs index 69416eb..6a8760e 100644 --- a/Dexer/IO/Markers/Marker.cs +++ b/Dexer/IO/Markers/Marker.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,9 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.IO; -using System.Collections.Generic; - namespace Dexer.IO.Markers; internal abstract class Marker @@ -43,7 +40,7 @@ public void CloneMarker() protected Marker(BinaryWriter writer) { Writer = writer; - Positions = new List(); + Positions = []; CloneMarker(); } } diff --git a/Dexer/IO/Markers/SignatureMarker.cs b/Dexer/IO/Markers/SignatureMarker.cs index deda0b9..8c50c15 100644 --- a/Dexer/IO/Markers/SignatureMarker.cs +++ b/Dexer/IO/Markers/SignatureMarker.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,12 +19,11 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.IO; using Dexer.Extensions; namespace Dexer.IO.Markers; -internal class SignatureMarker : Marker +internal class SignatureMarker(BinaryWriter writer) : Marker(writer) { public override byte[] Value { @@ -43,6 +42,4 @@ public override void Allocate() { Writer.Write(new byte[DexConsts.SignatureSize]); } - - public SignatureMarker(BinaryWriter writer) : base(writer) { } } diff --git a/Dexer/IO/Markers/SizeOffset.cs b/Dexer/IO/Markers/SizeOffset.cs index 77099e7..d229302 100644 --- a/Dexer/IO/Markers/SizeOffset.cs +++ b/Dexer/IO/Markers/SizeOffset.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -21,14 +21,8 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.IO.Markers; -internal class SizeOffset +internal class SizeOffset(uint size, uint offset) { - public uint Size { get; set; } - public uint Offset { get; set; } - - public SizeOffset(uint size, uint offset) - { - Size = size; - Offset = offset; - } + public uint Size { get; set; } = size; + public uint Offset { get; set; } = offset; } diff --git a/Dexer/IO/Markers/SizeOffsetMarker.cs b/Dexer/IO/Markers/SizeOffsetMarker.cs index 3659a5b..94abdc2 100644 --- a/Dexer/IO/Markers/SizeOffsetMarker.cs +++ b/Dexer/IO/Markers/SizeOffsetMarker.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,12 +19,11 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.IO; using Dexer.Extensions; namespace Dexer.IO.Markers; -internal class SizeOffsetMarker : Marker +internal class SizeOffsetMarker(BinaryWriter writer) : Marker(writer) { public override SizeOffset Value { @@ -43,8 +42,6 @@ public override SizeOffset Value } } - public SizeOffsetMarker(BinaryWriter writer) : base(writer) { } - public override void Allocate() { Writer.Write((uint)0); diff --git a/Dexer/IO/Markers/UShortMarker.cs b/Dexer/IO/Markers/UShortMarker.cs index ef95e3e..e6e5b35 100644 --- a/Dexer/IO/Markers/UShortMarker.cs +++ b/Dexer/IO/Markers/UShortMarker.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,12 +19,11 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.IO; using Dexer.Extensions; namespace Dexer.IO.Markers; -internal class UShortMarker : Marker +internal class UShortMarker(BinaryWriter writer) : Marker(writer) { public override ushort Value { @@ -43,6 +42,4 @@ public override void Allocate() { Writer.Write((ushort)0); } - - public UShortMarker(BinaryWriter writer) : base(writer) { } } diff --git a/Dexer/IO/Markers/UintMarker.cs b/Dexer/IO/Markers/UintMarker.cs index 29f0a0a..98dc358 100644 --- a/Dexer/IO/Markers/UintMarker.cs +++ b/Dexer/IO/Markers/UintMarker.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,12 +19,11 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.IO; using Dexer.Extensions; namespace Dexer.IO.Markers; -internal class UIntMarker : Marker +internal class UIntMarker(BinaryWriter writer) : Marker(writer) { public override uint Value { @@ -43,6 +42,4 @@ public override void Allocate() { Writer.Write((uint)0); } - - public UIntMarker(BinaryWriter writer) : base(writer) { } } diff --git a/Dexer/Instructions/Catch.cs b/Dexer/Instructions/Catch.cs index 4cbbdc2..85a2d46 100644 --- a/Dexer/Instructions/Catch.cs +++ b/Dexer/Instructions/Catch.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -21,16 +21,15 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using System.Globalization; using Dexer.Core; -using System; using System.Text; using Dexer.Metadata; namespace Dexer.Instructions; -public class Catch : ICloneable, IEquatable +public class Catch(TypeReference type, Instruction instruction) : ICloneable, IEquatable { - public TypeReference Type { get; set; } - public Instruction Instruction { get; set; } + public TypeReference Type { get; set; } = type; + public Instruction Instruction { get; set; } = instruction; internal Catch Clone() { @@ -39,7 +38,7 @@ internal Catch Clone() object ICloneable.Clone() { - var result = new Catch {Type = Type, Instruction = Instruction}; + var result = new Catch(Type, Instruction); return result; } diff --git a/Dexer/Instructions/DebugInfo.cs b/Dexer/Instructions/DebugInfo.cs index b5b4c56..1a24f7a 100644 --- a/Dexer/Instructions/DebugInfo.cs +++ b/Dexer/Instructions/DebugInfo.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,21 +19,12 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.Instructions; -public class DebugInfo +public class DebugInfo(MethodBody body) { - public List Parameters { get; set; } - public List DebugInstructions { get; set; } + public List Parameters { get; set; } = []; + public List DebugInstructions { get; set; } = []; public uint LineStart { get; set; } - public MethodBody Owner { get; set; } - - public DebugInfo(MethodBody body) - { - Owner = body; - Parameters = new List(); - DebugInstructions = new List(); - } + public MethodBody Owner { get; set; } = body; } diff --git a/Dexer/Instructions/DebugInstruction.cs b/Dexer/Instructions/DebugInstruction.cs index f5b3b35..6828ce7 100644 --- a/Dexer/Instructions/DebugInstruction.cs +++ b/Dexer/Instructions/DebugInstruction.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,7 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using System.Text; namespace Dexer.Instructions; @@ -27,11 +26,11 @@ namespace Dexer.Instructions; public class DebugInstruction { public DebugOpCodes OpCode { get; set; } - public List Operands { get; set; } + public List Operands { get; set; } public DebugInstruction() { - Operands = new List(); + Operands = []; } public override string ToString() diff --git a/Dexer/Instructions/ExceptionHandler.cs b/Dexer/Instructions/ExceptionHandler.cs index bbae2e1..74426db 100644 --- a/Dexer/Instructions/ExceptionHandler.cs +++ b/Dexer/Instructions/ExceptionHandler.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,19 +19,12 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.Instructions; -public class ExceptionHandler +public class ExceptionHandler(Instruction tryStart, Instruction tryEnd) { - public Instruction TryStart { get; set; } - public Instruction TryEnd { get; set; } - public Instruction CatchAll { get; set; } - public List Catches { get; set; } - - public ExceptionHandler() - { - Catches = new List(); - } + public Instruction TryStart { get; set; } = tryStart; + public Instruction TryEnd { get; set; } = tryEnd; + public Instruction? CatchAll { get; set; } + public List Catches { get; set; } = []; } diff --git a/Dexer/Instructions/Instruction.cs b/Dexer/Instructions/Instruction.cs index cdc55c4..baaef66 100644 --- a/Dexer/Instructions/Instruction.cs +++ b/Dexer/Instructions/Instruction.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,10 +19,7 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; using System.Text; -using System; - namespace Dexer.Instructions; public class Instruction : IEquatable @@ -30,28 +27,14 @@ public class Instruction : IEquatable public OpCodes OpCode { get; set; } public int Offset { get; set; } public List Registers { get; set; } - public object Operand { get; set; } + public object? Operand { get; set; } public Instruction() { - Registers = new List(); - } - - public Instruction(OpCodes opcode, params Register[] registers) - : this(opcode, null, registers) - { - } - - public Instruction(OpCodes opcode) - : this(opcode, null, null) - { - } - - public Instruction(OpCodes opcode, object operand) : this(opcode, operand, null) - { + Registers = []; } - public Instruction(OpCodes opcode, object operand, params Register[] registers) : this() + public Instruction(OpCodes opcode, object? operand = null, params Register[]? registers) : this() { OpCode = opcode; Operand = operand; @@ -88,7 +71,7 @@ public override string ToString() return builder.ToString(); } - public bool Equals(Instruction other) + public bool Equals(Instruction? other) { // Should be OK because we only use this after proper computation of offsets. // Mainly used by CatchSet to detect dupe lists. diff --git a/Dexer/Instructions/MethodBody.cs b/Dexer/Instructions/MethodBody.cs index 239f5a6..434f854 100644 --- a/Dexer/Instructions/MethodBody.cs +++ b/Dexer/Instructions/MethodBody.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; -using System; using Dexer.IO; using System.Runtime.InteropServices; using Dexer.Core; @@ -29,7 +27,7 @@ namespace Dexer.Instructions; public class MethodBody { - public DebugInfo DebugInfo { get; set; } + public DebugInfo? DebugInfo { get; set; } public List Registers { get; set; } public List Instructions { get; set; } public List Exceptions { get; set; } @@ -40,19 +38,19 @@ public class MethodBody public MethodBody(MethodDefinition method, int registersSize) { Owner = method; - Registers = new List(); + Registers = []; for (var i = 0; i < registersSize; i++) { Registers.Add(new Register(i)); } - Instructions = new List(); - Exceptions = new List(); + Instructions = []; + Exceptions = []; } internal static void CheckArrayData(Instruction ins, out Array elements, out Type elementtype, out int elementsize) { - if (!(ins.Operand is Array) || ((Array)ins.Operand).Length == 0) + if (ins.Operand is not Array || ((Array)ins.Operand).Length == 0) throw new InstructionException(ins, "Expecting non empty Array"); elements = (Array)ins.Operand; diff --git a/Dexer/Instructions/OffsetStatistics.cs b/Dexer/Instructions/OffsetStatistics.cs index f1c7503..d9c16de 100644 --- a/Dexer/Instructions/OffsetStatistics.cs +++ b/Dexer/Instructions/OffsetStatistics.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/Instructions/PackedSwitchData.cs b/Dexer/Instructions/PackedSwitchData.cs index 86ea762..993cd24 100644 --- a/Dexer/Instructions/PackedSwitchData.cs +++ b/Dexer/Instructions/PackedSwitchData.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.Instructions; public class PackedSwitchData @@ -30,6 +28,6 @@ public class PackedSwitchData public PackedSwitchData() { - Targets = new List(); + Targets = []; } } diff --git a/Dexer/Instructions/Register.cs b/Dexer/Instructions/Register.cs index a3b06b2..c790806 100644 --- a/Dexer/Instructions/Register.cs +++ b/Dexer/Instructions/Register.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -21,14 +21,9 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ namespace Dexer.Instructions; -public class Register +public class Register(int index) { - public int Index { get; set; } - - public Register(int index) - { - Index = index; - } + public int Index { get; set; } = index; public override string ToString() { diff --git a/Dexer/Instructions/SparseSwitchData.cs b/Dexer/Instructions/SparseSwitchData.cs index 348a9ce..28f5187 100644 --- a/Dexer/Instructions/SparseSwitchData.cs +++ b/Dexer/Instructions/SparseSwitchData.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.Instructions; public class SparseSwitchData @@ -29,6 +27,6 @@ public class SparseSwitchData public SparseSwitchData() { - Targets = new SortedDictionary(); + Targets = []; } } diff --git a/Dexer/Metadata/Map.cs b/Dexer/Metadata/Map.cs index 5092fc2..5cd06f8 100644 --- a/Dexer/Metadata/Map.cs +++ b/Dexer/Metadata/Map.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -19,8 +19,6 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ -using System.Collections.Generic; - namespace Dexer.Metadata; public class Map : Dictionary diff --git a/Dexer/Metadata/MapItem.cs b/Dexer/Metadata/MapItem.cs index a23f296..4df18b7 100644 --- a/Dexer/Metadata/MapItem.cs +++ b/Dexer/Metadata/MapItem.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/Metadata/TypeCodes.cs b/Dexer/Metadata/TypeCodes.cs index ff8b865..0f40d3a 100644 --- a/Dexer/Metadata/TypeCodes.cs +++ b/Dexer/Metadata/TypeCodes.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/Metadata/TypeDescriptor.cs b/Dexer/Metadata/TypeDescriptor.cs index 107194f..f31f6fd 100644 --- a/Dexer/Metadata/TypeDescriptor.cs +++ b/Dexer/Metadata/TypeDescriptor.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -29,7 +29,7 @@ public class TypeDescriptor internal static TypeReference Allocate(string tdString) { if (string.IsNullOrEmpty(tdString)) - return null; + throw new ArgumentNullException(nameof(tdString)); var prefix = tdString[0]; var td = (TypeDescriptors)prefix; @@ -45,9 +45,11 @@ internal static TypeReference Allocate(string tdString) TypeDescriptors.Long => PrimitiveType.Long, TypeDescriptors.Short => PrimitiveType.Short, TypeDescriptors.Void => PrimitiveType.Void, - TypeDescriptors.Array => new ArrayType(), - TypeDescriptors.FullyQualifiedName => new ClassReference(), - _ => null + + TypeDescriptors.Array => new ArrayType(null!), // we pass null temporarily, TypeDescriptor.Fill will initialize the ElementType shortly + TypeDescriptors.FullyQualifiedName => new ClassReference(null!), // we pass null temporarily, TypeDescriptor.Fill will initialize the Fullname shortly + + _ => throw new ArgumentException(nameof(tdString)) }; } diff --git a/Dexer/Metadata/TypeDescriptors.cs b/Dexer/Metadata/TypeDescriptors.cs index 9ece989..d6bddf2 100644 --- a/Dexer/Metadata/TypeDescriptors.cs +++ b/Dexer/Metadata/TypeDescriptors.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Dexer/Metadata/ValueFormat.cs b/Dexer/Metadata/ValueFormat.cs index 51f4974..5f6de84 100644 --- a/Dexer/Metadata/ValueFormat.cs +++ b/Dexer/Metadata/ValueFormat.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the @@ -20,13 +20,12 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ using Dexer.Core; -using System; namespace Dexer.Metadata; public static class ValueFormat { - public static ValueFormats GetFormat(object value) + public static ValueFormats GetFormat(object? value) { return value switch { diff --git a/Dexer/Metadata/ValueFormats.cs b/Dexer/Metadata/ValueFormats.cs index 6153b5a..5c24fec 100644 --- a/Dexer/Metadata/ValueFormats.cs +++ b/Dexer/Metadata/ValueFormats.cs @@ -1,4 +1,4 @@ -/* Dexer Copyright (c) 2010-2022 Sebastien Lebreton +/* Dexer Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..c9be1db --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,18 @@ + + + enable + enable + latest + + Sebastien Lebreton + https://github.com/sailro/dexer + 1.0.0 + Sebastien Lebreton + en-US + + DEX Android Dalvik + Dexer is a .NET library to read, manipulate and write Android/Dalvik DEX files + MIT + https://github.com/sailro/dexer + + \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props new file mode 100644 index 0000000..7349904 --- /dev/null +++ b/Directory.Packages.props @@ -0,0 +1,16 @@ + + + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE index 40227e8..05e0d7b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2010-2021 Sebastien Lebreton +Copyright (c) 2010-2023 Sebastien Lebreton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal