From 3cccae2dd9a12f9d0817bbb5db0a459a3a55950f Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Thu, 22 Feb 2024 21:27:07 -0800 Subject: [PATCH] Spanify BplistReader --- .../Formats/Apple/BplistReader.cs | 33 +++++-------------- .../AppleRunTimeMakernoteDirectory.cs | 2 +- .../PublicAPI/net462/PublicAPI.Unshipped.txt | 4 +-- .../PublicAPI/net8.0/PublicAPI.Unshipped.txt | 4 +-- .../netstandard1.3/PublicAPI.Unshipped.txt | 4 +-- .../netstandard2.1/PublicAPI.Unshipped.txt | 4 +-- 6 files changed, 18 insertions(+), 33 deletions(-) diff --git a/MetadataExtractor/Formats/Apple/BplistReader.cs b/MetadataExtractor/Formats/Apple/BplistReader.cs index f59e0b3e3..974db2d28 100644 --- a/MetadataExtractor/Formats/Apple/BplistReader.cs +++ b/MetadataExtractor/Formats/Apple/BplistReader.cs @@ -11,42 +11,27 @@ public sealed class BplistReader // https://opensource.apple.com/source/CF/CF-550/CFBinaryPList.c // https://synalysis.com/how-to-decode-apple-binary-property-list-files/ - private static ReadOnlySpan BplistHeader => "bplist00"u8; - /// /// Gets whether starts with the expected header bytes. /// - public static bool IsValid(byte[] bplist) + public static bool IsValid(ReadOnlySpan bplist) { - if (bplist.Length < BplistHeader.Length) - { - return false; - } - - for (int i = 0; i < BplistHeader.Length; i++) - { - if (bplist[i] != BplistHeader[i]) - { - return false; - } - } - - return true; + return bplist.StartsWith("bplist00"u8); } - public static PropertyListResults Parse(byte[] bplist) + public static PropertyListResults Parse(ReadOnlySpan bplist) { if (!IsValid(bplist)) { throw new ArgumentException("Input is not a bplist.", nameof(bplist)); } - Trailer trailer = ReadTrailer(); + Trailer trailer = ReadTrailer(bplist); int offset = checked((int)(trailer.OffsetTableOffset + trailer.TopObject)); - var reader = new BufferReader(bplist.AsSpan(offset), isBigEndian: true); + var reader = new BufferReader(bplist.Slice(offset), isBigEndian: true); - int[] offsets = new int[(int)trailer.NumObjects]; + var offsets = new int[(int)trailer.NumObjects]; for (int i = 0; i < (int)trailer.NumObjects; i++) { @@ -64,7 +49,7 @@ public static PropertyListResults Parse(byte[] bplist) for (int i = 0; i < offsets.Length; i++) { - reader = new BufferReader(bplist.AsSpan(offsets[i]), isBigEndian: true); + reader = new BufferReader(bplist.Slice(offsets[i]), isBigEndian: true); byte b = reader.GetByte(); @@ -90,9 +75,9 @@ public static PropertyListResults Parse(byte[] bplist) return new PropertyListResults(objects, trailer); - Trailer ReadTrailer() + static Trailer ReadTrailer(ReadOnlySpan bplist) { - var reader = new BufferReader(bplist.AsSpan(bplist.Length - Trailer.SizeBytes), isBigEndian: true); + var reader = new BufferReader(bplist.Slice(bplist.Length - Trailer.SizeBytes), isBigEndian: true); // Skip 5-byte unused values, 1-byte sort version. reader.Skip(5 + 1); diff --git a/MetadataExtractor/Formats/Exif/Makernotes/AppleRunTimeMakernoteDirectory.cs b/MetadataExtractor/Formats/Exif/Makernotes/AppleRunTimeMakernoteDirectory.cs index 1b86bc37d..78caf5e7f 100644 --- a/MetadataExtractor/Formats/Exif/Makernotes/AppleRunTimeMakernoteDirectory.cs +++ b/MetadataExtractor/Formats/Exif/Makernotes/AppleRunTimeMakernoteDirectory.cs @@ -11,7 +11,7 @@ public sealed class AppleRunTimeMakernoteDirectory : Directory public const int TagScale = 3; public const int TagValue = 4; - private static readonly Dictionary _tagNameMap = new(); + private static readonly Dictionary _tagNameMap = new(4); static AppleRunTimeMakernoteDirectory() { diff --git a/MetadataExtractor/PublicAPI/net462/PublicAPI.Unshipped.txt b/MetadataExtractor/PublicAPI/net462/PublicAPI.Unshipped.txt index 4ae22099b..be31a00dc 100644 --- a/MetadataExtractor/PublicAPI/net462/PublicAPI.Unshipped.txt +++ b/MetadataExtractor/PublicAPI/net462/PublicAPI.Unshipped.txt @@ -132,8 +132,8 @@ override MetadataExtractor.IO.IndexedCapturingReader.GetBytes(int index, System. override MetadataExtractor.IO.IndexedSeekingReader.GetBytes(int index, System.Span bytes) -> void override MetadataExtractor.IO.SequentialByteArrayReader.GetBytes(System.Span bytes) -> void override MetadataExtractor.IO.SequentialStreamReader.GetBytes(System.Span bytes) -> void -static MetadataExtractor.Formats.Apple.BplistReader.IsValid(byte[]! bplist) -> bool -static MetadataExtractor.Formats.Apple.BplistReader.Parse(byte[]! bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! +static MetadataExtractor.Formats.Apple.BplistReader.IsValid(System.ReadOnlySpan bplist) -> bool +static MetadataExtractor.Formats.Apple.BplistReader.Parse(System.ReadOnlySpan bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! static MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory.Parse(byte[]! bytes) -> MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory! static MetadataExtractor.Formats.Png.PngChunkType.operator !=(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool static MetadataExtractor.Formats.Png.PngChunkType.operator ==(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool diff --git a/MetadataExtractor/PublicAPI/net8.0/PublicAPI.Unshipped.txt b/MetadataExtractor/PublicAPI/net8.0/PublicAPI.Unshipped.txt index ce84c159f..c0342bb97 100644 --- a/MetadataExtractor/PublicAPI/net8.0/PublicAPI.Unshipped.txt +++ b/MetadataExtractor/PublicAPI/net8.0/PublicAPI.Unshipped.txt @@ -131,8 +131,8 @@ override MetadataExtractor.IO.IndexedCapturingReader.GetBytes(int index, System. override MetadataExtractor.IO.IndexedSeekingReader.GetBytes(int index, System.Span bytes) -> void override MetadataExtractor.IO.SequentialByteArrayReader.GetBytes(System.Span bytes) -> void override MetadataExtractor.IO.SequentialStreamReader.GetBytes(System.Span bytes) -> void -static MetadataExtractor.Formats.Apple.BplistReader.IsValid(byte[]! bplist) -> bool -static MetadataExtractor.Formats.Apple.BplistReader.Parse(byte[]! bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! +static MetadataExtractor.Formats.Apple.BplistReader.IsValid(System.ReadOnlySpan bplist) -> bool +static MetadataExtractor.Formats.Apple.BplistReader.Parse(System.ReadOnlySpan bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! static MetadataExtractor.Formats.Exif.ExifReader.JpegSegmentPreamble.get -> System.ReadOnlySpan static MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory.Parse(byte[]! bytes) -> MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory! static MetadataExtractor.Formats.Icc.IccReader.JpegSegmentPreamble.get -> System.ReadOnlySpan diff --git a/MetadataExtractor/PublicAPI/netstandard1.3/PublicAPI.Unshipped.txt b/MetadataExtractor/PublicAPI/netstandard1.3/PublicAPI.Unshipped.txt index 4ae22099b..be31a00dc 100644 --- a/MetadataExtractor/PublicAPI/netstandard1.3/PublicAPI.Unshipped.txt +++ b/MetadataExtractor/PublicAPI/netstandard1.3/PublicAPI.Unshipped.txt @@ -132,8 +132,8 @@ override MetadataExtractor.IO.IndexedCapturingReader.GetBytes(int index, System. override MetadataExtractor.IO.IndexedSeekingReader.GetBytes(int index, System.Span bytes) -> void override MetadataExtractor.IO.SequentialByteArrayReader.GetBytes(System.Span bytes) -> void override MetadataExtractor.IO.SequentialStreamReader.GetBytes(System.Span bytes) -> void -static MetadataExtractor.Formats.Apple.BplistReader.IsValid(byte[]! bplist) -> bool -static MetadataExtractor.Formats.Apple.BplistReader.Parse(byte[]! bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! +static MetadataExtractor.Formats.Apple.BplistReader.IsValid(System.ReadOnlySpan bplist) -> bool +static MetadataExtractor.Formats.Apple.BplistReader.Parse(System.ReadOnlySpan bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! static MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory.Parse(byte[]! bytes) -> MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory! static MetadataExtractor.Formats.Png.PngChunkType.operator !=(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool static MetadataExtractor.Formats.Png.PngChunkType.operator ==(MetadataExtractor.Formats.Png.PngChunkType left, MetadataExtractor.Formats.Png.PngChunkType right) -> bool diff --git a/MetadataExtractor/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt b/MetadataExtractor/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt index ce84c159f..c0342bb97 100644 --- a/MetadataExtractor/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt +++ b/MetadataExtractor/PublicAPI/netstandard2.1/PublicAPI.Unshipped.txt @@ -131,8 +131,8 @@ override MetadataExtractor.IO.IndexedCapturingReader.GetBytes(int index, System. override MetadataExtractor.IO.IndexedSeekingReader.GetBytes(int index, System.Span bytes) -> void override MetadataExtractor.IO.SequentialByteArrayReader.GetBytes(System.Span bytes) -> void override MetadataExtractor.IO.SequentialStreamReader.GetBytes(System.Span bytes) -> void -static MetadataExtractor.Formats.Apple.BplistReader.IsValid(byte[]! bplist) -> bool -static MetadataExtractor.Formats.Apple.BplistReader.Parse(byte[]! bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! +static MetadataExtractor.Formats.Apple.BplistReader.IsValid(System.ReadOnlySpan bplist) -> bool +static MetadataExtractor.Formats.Apple.BplistReader.Parse(System.ReadOnlySpan bplist) -> MetadataExtractor.Formats.Apple.BplistReader.PropertyListResults! static MetadataExtractor.Formats.Exif.ExifReader.JpegSegmentPreamble.get -> System.ReadOnlySpan static MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory.Parse(byte[]! bytes) -> MetadataExtractor.Formats.Exif.Makernotes.AppleRunTimeMakernoteDirectory! static MetadataExtractor.Formats.Icc.IccReader.JpegSegmentPreamble.get -> System.ReadOnlySpan