Skip to content

Instantly share code, notes, and snippets.

@C0BR4cH
Created April 21, 2017 08:16
Show Gist options
  • Save C0BR4cH/c184c3d552f394c50b36714653c345cc to your computer and use it in GitHub Desktop.
Save C0BR4cH/c184c3d552f394c50b36714653c345cc to your computer and use it in GitHub Desktop.
Removal of OLE header from images
public static class OLEObjectUtil
{
private const string BITMAP_OLE_HEADER = "BM";
private const string JPG_OLE_HEADER = "\u00FF\u00D8\u00FF";
private const string PNG_OLE_HEADER = "\u0089PNG\r\n\u001a\n";
private const string GIF_OLE_HEADER = "GIF8";
private const string TIFF_OLE_HEADER = "II*\u0000";
public static byte[] RemoveOleHeader(byte[] inputBytes)
{
string temp = Encoding.ASCII.GetString(inputBytes);
// Get the first 300 characters from the string
string strVTemp = temp.Substring(0, 300);
// Search for the header
int headerPos = -1;
if (strVTemp.IndexOf(BITMAP_OLE_HEADER) != -1)
{
headerPos = strVTemp.IndexOf(BITMAP_OLE_HEADER);
}
else if (strVTemp.IndexOf(JPG_OLE_HEADER) != -1)
{
headerPos = strVTemp.IndexOf(JPG_OLE_HEADER);
}
else if (strVTemp.IndexOf(PNG_OLE_HEADER) != -1)
{
headerPos = strVTemp.IndexOf(PNG_OLE_HEADER);
}
else if (strVTemp.IndexOf(GIF_OLE_HEADER) != -1)
{
headerPos = strVTemp.IndexOf(GIF_OLE_HEADER);
}
else if (strVTemp.IndexOf(TIFF_OLE_HEADER) != -1)
{
headerPos = strVTemp.IndexOf(TIFF_OLE_HEADER);
}
else
{
// No OLE image header found, return original bytes
return inputBytes;
}
// Copy data without header to new array and return it
using (MemoryStream ms = new MemoryStream())
{
ms.Write(inputBytes, headerPos, inputBytes.Length - headerPos);
return ms.ToArray();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment