Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce and use Indexed BufferReader methods #402

Merged
merged 12 commits into from
Feb 6, 2024
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;
}
Comment on lines +170 to 173
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The length is validated upfront, so we don't have to worry about an IOException below.


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
2 changes: 1 addition & 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: false);

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

Expand Down
Loading
Loading