Skip to content

Commit

Permalink
Better RIFF error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Apr 29, 2022
1 parent fb4de2b commit 839d1c1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 17 deletions.
5 changes: 5 additions & 0 deletions MetadataExtractor/Formats/Avi/AviRiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public void ProcessChunk(string fourCc, byte[] payload)
}
}

public void AddError(string errorMessage)
{
GetOrCreateAviDirectory().AddError(errorMessage);
}

private AviDirectory GetOrCreateAviDirectory()
{
if (_directory == null)
Expand Down
2 changes: 2 additions & 0 deletions MetadataExtractor/Formats/Riff/IRiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ public interface IRiffHandler
/// <param name="fourCc">the four character code of the chunk</param>
/// <param name="payload">they payload of the chunk as a byte array</param>
void ProcessChunk(string fourCc, byte[] payload);

void AddError(string errorMessage);
}
}
5 changes: 5 additions & 0 deletions MetadataExtractor/Formats/Riff/RiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,10 @@ public void ProcessChunk(string fourCc, byte[] payload)
public abstract bool ShouldAcceptRiffIdentifier(string identifier);

public abstract bool ShouldAcceptList(string fourCc);

public void AddError(string errorMessage)
{
_directories.Add(new ErrorDirectory(errorMessage));
}
}
}
40 changes: 23 additions & 17 deletions MetadataExtractor/Formats/Riff/RiffReader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) Drew Noakes and contributors. All Rights Reserved. Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

using System.IO;
using System.Text;
using MetadataExtractor.IO;

Expand All @@ -22,30 +23,35 @@ public sealed class RiffReader
/// <summary>Processes a RIFF data sequence.</summary>
/// <param name="reader">The <see cref="SequentialReader"/> from which the data should be read.</param>
/// <param name="handler">The <see cref="IRiffHandler"/> that will coordinate processing and accept read values.</param>
/// <exception cref="RiffProcessingException">An error occurred during the processing of RIFF data that could not be ignored or recovered from.</exception>
/// <exception cref="System.IO.IOException">an error occurred while accessing the required data</exception>
public void ProcessRiff(SequentialReader reader, IRiffHandler handler)
{
// RIFF files are always little-endian
reader = reader.WithByteOrder(isMotorolaByteOrder: false);
try
{
// RIFF files are always little-endian
reader = reader.WithByteOrder(isMotorolaByteOrder: false);

// PROCESS FILE HEADER
// PROCESS FILE HEADER

var fileFourCc = reader.GetString(4, Encoding.ASCII);
if (fileFourCc != "RIFF")
throw new RiffProcessingException("Invalid RIFF header: " + fileFourCc);
var fileFourCc = reader.GetString(4, Encoding.ASCII);
if (fileFourCc != "RIFF")
throw new RiffProcessingException("Invalid RIFF header: " + fileFourCc);

// The total size of the chunks that follow plus 4 bytes for the 'WEBP' or 'AVI ' FourCC
int fileSize = reader.GetInt32();
int sizeLeft = fileSize;
string identifier = reader.GetString(4, Encoding.ASCII);
sizeLeft -= 4;
// The total size of the chunks that follow plus 4 bytes for the 'WEBP' or 'AVI ' FourCC
int fileSize = reader.GetInt32();
int sizeLeft = fileSize;
string identifier = reader.GetString(4, Encoding.ASCII);
sizeLeft -= 4;

if (!handler.ShouldAcceptRiffIdentifier(identifier))
return;
if (!handler.ShouldAcceptRiffIdentifier(identifier))
return;

var maxPosition = reader.Position + sizeLeft;
ProcessChunks(reader, maxPosition, handler);
var maxPosition = reader.Position + sizeLeft;
ProcessChunks(reader, maxPosition, handler);
}
catch (System.Exception e) when (e is ImageProcessingException or IOException)
{
handler.AddError(e.Message);
}
}

// PROCESS CHUNKS
Expand Down
5 changes: 5 additions & 0 deletions MetadataExtractor/Formats/WebP/WebpRiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -196,5 +196,10 @@ public void ProcessChunk(string fourCc, byte[] payload)
}
}
}

public void AddError(string errorMessage)
{
_directories.Add(new ErrorDirectory(errorMessage));
}
}
}

0 comments on commit 839d1c1

Please sign in to comment.