Skip to content

Commit

Permalink
Merge pull request #402 from iamcarbon/allocations-2
Browse files Browse the repository at this point in the history
Introduce and use Indexed BufferReader methods
  • Loading branch information
drewnoakes authored Feb 6, 2024
2 parents 60e3fe7 + bec7461 commit f48737f
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 346 deletions.
26 changes: 15 additions & 11 deletions MetadataExtractor/Formats/Avi/AviRiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void ProcessChunk(string fourCc, byte[] payload)
{
case "strh":
{
var reader = new ByteArrayReader(payload, isMotorolaByteOrder: false);
var reader = new BufferReader(payload, isBigEndian: false);

var directory = GetOrCreateAviDirectory();
try
Expand Down Expand Up @@ -101,8 +101,13 @@ public void ProcessChunk(string fourCc, byte[] payload)
{
var directory = GetOrCreateAviDirectory();

var reader = new ByteArrayReader(payload, isMotorolaByteOrder: false);
try
var reader = new BufferReader(payload, isBigEndian: false);

if (payload.Length < 40)
{
directory.AddError("Insufficient bytes for AviRiff chunk 'avih'");
}
else
{
//int dwMicroSecPerFrame = reader.GetInt32(0);
//int dwMaxBytesPerSec = reader.GetInt32(4);
Expand All @@ -120,21 +125,20 @@ public void ProcessChunk(string fourCc, byte[] payload)
directory.Set(AviDirectory.TagHeight, dwHeight);
directory.Set(AviDirectory.TagStreams, dwStreams);
}
catch (IOException e)
{
directory.AddError("Exception reading AviRiff chunk 'avih' : " + e.Message);
}

break;
}
case "IDIT":
{
var reader = new ByteArrayReader(payload);
var str = reader.GetString(0, payload.Length, Encoding.ASCII);
if (str.Length == 26 && str.EndsWith("\n\0", StringComparison.Ordinal))
string str;
if (payload.Length is 26 && payload.AsSpan().EndsWith("\n\0"u8))
{
// ?0A 00? "New Line" + padded to nearest WORD boundary
str = str.Substring(0, 24);
str = Encoding.ASCII.GetString(payload.AsSpan(0, 24));
}
else
{
str = Encoding.ASCII.GetString(payload);
}
GetOrCreateAviDirectory().Set(AviDirectory.TagDateTimeOriginal, str);
break;
Expand Down
4 changes: 2 additions & 2 deletions MetadataExtractor/Formats/Exif/ExifDescriptorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ public abstract class ExifDescriptorBase<T>(T directory)
return ret;
}

IndexedReader reader = new ByteArrayReader(values);
var reader = new BufferReader(values, isBigEndian: true);

// first two values should be read as 16-bits (2 bytes)
var item0 = reader.GetInt16(0);
Expand All @@ -588,7 +588,7 @@ public abstract class ExifDescriptorBase<T>(T directory)
if (end > values.Length) // sanity check in case of byte order problems; calculated 'end' should be <= length of the values
{
// try swapping byte order (I have seen this order different than in EXIF)
reader = reader.WithByteOrder(!reader.IsMotorolaByteOrder);
reader = new BufferReader(values, isBigEndian: !reader.IsBigEndian);
item0 = reader.GetInt16(0);
item1 = reader.GetInt16(2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,29 +167,25 @@ public sealed class PanasonicMakernoteDescriptor(PanasonicMakernoteDirectory dir
if (values is null)
return null;

IndexedReader reader = new ByteArrayReader(values);

try
{
int val1 = reader.GetUInt16(0);
int val2 = reader.GetUInt16(2);
if (val1 == -1 && val2 == 1)
return "Slim Low";
if (val1 == -3 && val2 == 2)
return "Slim High";
if (val1 == 0 && val2 == 0)
return "Off";
if (val1 == 1 && val2 == 1)
return "Stretch Low";
if (val1 == 3 && val2 == 2)
return "Stretch High";

return "Unknown (" + val1 + " " + val2 + ")";
}
catch (IOException)
if (values.Length < 2 + 2)
{
return null;
}

var reader = new BufferReader(values, isBigEndian: true);

int val1 = reader.GetUInt16(0);
int val2 = reader.GetUInt16(2);

return (val1, val2) switch
{
(-1, 1) => "Slim Low",
(-3, 2) => "Slim High",
(0, 0) => "Off",
(1, 1) => "Stretch Low",
(3, 2) => "Stretch High",
_ => $"Unknown ({val1} {val2})"
};
}

public string? GetIntelligentExposureDescription()
Expand Down
6 changes: 5 additions & 1 deletion MetadataExtractor/Formats/Icc/IccDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private enum IccTagType
if (bytes is null)
return Directory.GetString(tagType);

var reader = new ByteArrayReader(bytes);
var reader = new BufferReader(bytes, isBigEndian: true);

var iccTagType = (IccTagType)reader.GetInt32(0);

Expand All @@ -71,6 +71,10 @@ private enum IccTagType
case IccTagType.Desc:
{
var stringLength = reader.GetInt32(8);

if (stringLength < 0 || stringLength > bytes.Length)
return null;

return Encoding.UTF8.GetString(bytes, 12, stringLength - 1);
}

Expand Down
Loading

0 comments on commit f48737f

Please sign in to comment.