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
13 changes: 8 additions & 5 deletions main/DDF/UnknownEscherRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ public override int FillFields(byte[] data, int offset, IEscherRecordFactory rec
int bytesRemaining = ReadHeader(data, offset);
/*
* Modified by Zhang Zhang
* Have a check between avaliable bytes and bytesRemaining,
* take the avaliable length if the bytesRemaining out of range.
* Have a check between available bytes and bytesRemaining,
* take the available length if the bytesRemaining out of range.
* July 09, 2010
*/
int avaliable = data.Length - (offset + 8);
if (bytesRemaining > avaliable)
int available = data.Length - (offset + 8);
if (bytesRemaining > available)
{
bytesRemaining = avaliable;
bytesRemaining = available;
}
if (IsContainerRecord)
{
Expand All @@ -82,6 +82,9 @@ public override int FillFields(byte[] data, int offset, IEscherRecordFactory rec
}
else
{
if (bytesRemaining < 0) {
bytesRemaining = 0;
}
_thedata = new byte[bytesRemaining];
Array.Copy(data, offset + 8, _thedata, 0, bytesRemaining);
return bytesRemaining + 8;
Expand Down
13 changes: 12 additions & 1 deletion main/POIFS/Storage/HeaderBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public class HeaderBlock : HeaderBlockConstants
0x00, 0x00, // unused
0x00, 0x01
};

private static byte[] MAGIC_MSWRITEa = {
0x31, (byte)0xbe, 0x00, 0x00
};
private static byte[] MAGIC_MSWRITEb = {
0x32, (byte)0xbe, 0x00, 0x00
};
private static byte _default_value = (byte)0xFF;
/**
* What big block Size the file uses. Most files
Expand Down Expand Up @@ -146,6 +151,12 @@ public void PrivateHeaderBlock(byte[] data)
+ "Formats such as Office 2003 XML are not supported");
}

// Old MS Write raw stream
if (cmp(MAGIC_MSWRITEa, data) || cmp(MAGIC_MSWRITEb, data)) {
throw new NotOLE2FileException("The supplied data appears to be in the old MS Write format. "
+ "NPOI doesn't currently support this format");
}

// BIFF2 raw stream
if (cmp(MAGIC_BIFF2, data))
{
Expand Down
5 changes: 5 additions & 0 deletions main/Util/FileInputStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public override int Read()
return inner.ReadByte();
}

public override int Read(byte[] b, int off, int len)
{
return inner.Read(b, off, len);
}

public override long Seek(long offset, SeekOrigin origin)
{
throw new NotImplementedException();
Expand Down
71 changes: 71 additions & 0 deletions main/Util/IOUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public class IOUtils
{
private static POILogger logger = POILogFactory.GetLogger(typeof(IOUtils));

/// <summary>
/// The default buffer size to use for the skip() methods.
/// </summary>
private static int SKIP_BUFFER_SIZE = 2048;
private static byte[] SKIP_BYTE_BUFFER;
/// <summary>
/// The current set global allocation limit override,
/// -1 means limits are applied per record type.
Expand Down Expand Up @@ -417,6 +422,72 @@ public static void CloseQuietly(ICloseable closeable)
}
}

/// <summary>
/// <para>
/// Skips bytes from an input byte stream.
/// This implementation guarantees that it will read as many bytes
/// as possible before giving up; this may not always be the case for
/// skip() implementations in subclasses of <see cref="InputStream"/>.
/// </para>
/// <para>
/// Note that the implementation uses <see cref="InputStream.Read(byte[], int, int)" /> rather
/// than delegating to <see cref="InputStream.Skip(long)" />.
/// This means that the method may be considerably less efficient than using the actual skip implementation,
/// this is done to guarantee that the correct number of bytes are skipped.
/// </para>
/// <para>
/// </para>
/// <para>
/// This mimics POI's <see cref="ReadFully(InputStream, byte[])" />.
/// If the end of file is reached before any bytes are read, returns <tt>-1</tt>. If
/// the end of the file is reached After some bytes are read, returns the
/// number of bytes read. If the end of the file isn't reached before <tt>len</tt>
/// bytes have been read, will return <tt>len</tt> bytes.
/// </para>
/// <para>
/// </para>
/// <para>
/// Copied nearly verbatim from commons-io 41a3e9c
/// </para>
/// </summary>
/// <param name="input">byte stream to skip</param>
/// <param name="toSkip">number of bytes to skip.</param>
/// <returns>number of bytes actually skipped.</returns>
/// <exception cref="IOException"> if there is a problem reading the file</exception>
/// <exception cref="ArgumentException">if toSkip is negative</exception>
/// @see InputStream#skip(long)
///
public static long SkipFully(InputStream input, long toSkip)
{
if (toSkip < 0) {
throw new ArgumentException("Skip count must be non-negative, actual: " + toSkip);
}
if (toSkip == 0) {
return 0L;
}
/*
* N.B. no need to synchronize this because: - we don't care if the buffer is created multiple times (the data
* is ignored) - we always use the same size buffer, so if it it is recreated it will still be OK (if the buffer
* size were variable, we would need to synch. to ensure some other thread did not create a smaller one)
*/
if (SKIP_BYTE_BUFFER == null) {
SKIP_BYTE_BUFFER = new byte[SKIP_BUFFER_SIZE];
}
long remain = toSkip;
while (remain > 0) {
// See https://issues.apache.org/jira/browse/IO-203 for why we use read() rather than delegating to skip()
long n = input.Read(SKIP_BYTE_BUFFER, 0, (int) Math.Min(remain, SKIP_BUFFER_SIZE));
if (n <= 0) { // EOF
break;
}
remain -= n;
}
if (toSkip == remain) {
return -1L;
}
return toSkip - remain;
}

public static byte[] SafelyAllocate(long length, int maxLength)
{
SafelyAllocateCheck(length, maxLength);
Expand Down
25 changes: 10 additions & 15 deletions ooxml/XSSF/Extractor/XSSFExportToXml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public void ExportToXML(Stream os, String encoding, bool validate)
if (table != null)
{

List<CT_TableColumn> tableColumns = table.GetCTTable().tableColumns.GetTableColumnList();
List<XSSFTableColumn> tableColumns = table.GetColumns();

XSSFSheet sheet = table.GetXSSFSheet();

Expand All @@ -185,25 +185,20 @@ public void ExportToXML(Stream os, String encoding, bool validate)
XmlNode tableRootNode = GetNodeByXPath(table.GetCommonXpath(), doc.FirstChild, doc, true);

short startColumnIndex = table.StartCellReference.Col;
for (int j = startColumnIndex; j <= table.EndCellReference.Col; j++)

foreach (XSSFTableColumn tableColumn in tableColumns)
{
XSSFCell cell = (XSSFCell)row.GetCell(j);
XSSFCell cell = (XSSFCell)row.GetCell(startColumnIndex + tableColumn.ColumnIndex);
if (cell != null)
{
int tableColumnIndex = j - startColumnIndex;
if(tableColumnIndex < tableColumns.Count)
XSSFXmlColumnPr xmlColumnPr = tableColumn.GetXmlColumnPr();
if (xmlColumnPr != null)
{
CT_TableColumn ctTableColumn = tableColumns[tableColumnIndex];
if(ctTableColumn.xmlColumnPr != null)
{
XSSFXmlColumnPr pointer = new XSSFXmlColumnPr(table, ctTableColumn,ctTableColumn.xmlColumnPr);
String localXPath = pointer.LocalXPath;
XmlNode currentNode = GetNodeByXPath(localXPath,tableRootNode,doc,false);
XSSFExportToXml.mapCellOnNode(cell, currentNode);
}
String localXPath = xmlColumnPr.LocalXPath;
XmlNode currentNode = GetNodeByXPath(localXPath,tableRootNode,doc,false);
mapCellOnNode(cell, currentNode);
}
}

}

}
Expand All @@ -230,7 +225,7 @@ public void ExportToXML(Stream os, String encoding, bool validate)
/////////////////
//Output the XML
XmlWriterSettings settings = new XmlWriterSettings();
//settings.OmitXmlDeclaration=false;
settings.OmitXmlDeclaration = true;
settings.Indent=true;
settings.Encoding=Encoding.GetEncoding(encoding);
//create string from xml tree
Expand Down
Loading
Loading