Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions main/POIFS/FileSystem/FileMagic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,30 @@ public static FileMagic ValueOf(byte[] magic)
{
foreach(var fm in Values)
{
int i=0;
bool found = true;
foreach(byte[] ma in fm.Value.magic)
{
foreach(byte m in ma)
{
byte d = magic[i++];
if(!(d == m || (m == 0x70 && (d == 0x10 || d == 0x20 || d == 0x40))))
{
found = false;
break;
}
}
if(found)
if(FindMagic(ma, magic))
{
return fm.Key;
}
}
}
return FileMagic.UNKNOWN;
}

private static bool FindMagic(byte[] expected, byte[] actual)
{
int i=0;
foreach(byte expectedByte in expected)
{
byte actualByte = actual[i++];
if((actualByte != expectedByte &&
(expectedByte != 0x70 || (actualByte != 0x10 && actualByte != 0x20 && actualByte != 0x40))))
{
return false;
}
}
return true;
}
/**
* Get the file magic of the supplied InputStream (which MUST
* support mark and reset).<p>
Expand Down
6 changes: 5 additions & 1 deletion main/Util/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ public static byte[] PeekFirstNBytes(Stream stream, int limit)
{
is1.Mark(limit);
}
else
{
stream.Seek(0, SeekOrigin.Begin);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(limit);
if(stream is ByteArrayInputStream inputStream)
Copy(new BoundedInputStream(inputStream, limit), bos);
Expand Down Expand Up @@ -146,7 +150,7 @@ public static byte[] PeekFirstNBytes(Stream stream, int limit)
}
else
{
stream.Position = mark;
stream.Seek(mark, SeekOrigin.Begin);
}

return peekedBytes;
Expand Down
47 changes: 47 additions & 0 deletions testcases/ooxml/POIFS/FileSystem/TestFileMagic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for Additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */

using NPOI.POIFS.FileSystem;
using NPOI.XSSF.UserModel;
using NUnit.Framework;
using NUnit.Framework.Legacy;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestCases.POIFS.FileSystem
{
[TestFixture]
public class TestFileMagic
{
[Test]
public void TestXSSFWorkbookStreamPositionGreaterThanZero()
{
var workbook = new XSSFWorkbook();
workbook.CreateSheet("Nothing here");
var stream = new MemoryStream();
workbook.Write(stream, leaveOpen: true);
ClassicAssert.AreEqual(FileMagic.OOXML, FileMagicContainer.ValueOf(stream));
stream.Position = 1;
ClassicAssert.AreEqual(FileMagic.OOXML, FileMagicContainer.ValueOf(stream));
workbook.Close();
}
}
}
Loading