Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class XSSFBCellHeader {
* @param cell cell buffer to update
*/
public static void parse(byte[] data, int offset, int currentRow, XSSFBCellHeader cell) {
int colNum = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
int colNum = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
int styleIdx = XSSFBUtils.get24BitInt(data, offset); offset += 3;
//TODO: range checking
boolean showPhonetic = false;//TODO: fill this out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,49 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.util.LittleEndianConsts;

/**
* @since 3.16-beta3
* @since 6.0.0
*/
@Internal
class XSSFBCellRange {
public class XSSFBCellRange {

//TODO: Convert this to generate an AreaReference
public static final int length = 4 * LittleEndianConsts.INT_SIZE;

private int firstRow;
private int lastRow;
private int firstCol;
private int lastCol;

public XSSFBCellRange(int firstRow, int lastRow, int firstCol, int lastCol) {
this.firstRow = firstRow;
this.lastRow = lastRow;
this.firstCol = firstCol;
this.lastCol = lastCol;
}

XSSFBCellRange() {
}

public static final int length = 4* LittleEndianConsts.INT_SIZE;
/**
* Parses an RfX cell range from the data starting at the offset.
* This performs no range checking.
* @param data raw bytes
* @param offset offset at which to start reading from data
* @param cellRange to overwrite. If null, a new cellRange will be created.
* @return a mutable cell range.
* @param cellRange to update. If null, a new cellRange will be created.
* @return the updated or new cell range
*/
public static XSSFBCellRange parse(byte[] data, int offset, XSSFBCellRange cellRange) {
if (cellRange == null) {
cellRange = new XSSFBCellRange();
}
cellRange.firstRow = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
cellRange.lastRow = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
cellRange.firstCol = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
cellRange.lastCol = XSSFBUtils.castToInt(LittleEndian.getUInt(data, offset));

cellRange.firstRow = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
cellRange.lastRow = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
cellRange.firstCol = Math.toIntExact(LittleEndian.getUInt(data, offset)); offset += LittleEndianConsts.INT_SIZE;
cellRange.lastCol = Math.toIntExact(LittleEndian.getUInt(data, offset));
return cellRange;
}

int firstRow;
int lastRow;
int firstCol;
int lastCol;


public int getFirstRow() { return firstRow; }
public int getLastRow() { return lastRow; }
public int getFirstCol() { return firstCol; }
public int getLastCol() { return lastCol; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ public void handleRecord(int id, byte[] data) throws XSSFBParseException {
switch (recordType) {
case BrtBeginComment:
int offset = 0;
authorId = XSSFBUtils.castToInt(LittleEndian.getUInt(data)); offset += LittleEndianConsts.INT_SIZE;
authorId = Math.toIntExact(LittleEndian.getUInt(data)); offset += LittleEndianConsts.INT_SIZE;
cellRange = XSSFBCellRange.parse(data, offset, cellRange);
offset+= XSSFBCellRange.length;
//for strict parsing; confirm that firstRow==lastRow and firstCol==colLats (2.4.28)
cellAddress = new CellAddress(cellRange.firstRow, cellRange.firstCol);
cellAddress = new CellAddress(cellRange.getFirstRow(), cellRange.getFirstCol());
break;
case BrtCommentText:
XSSFBRichStr xssfbRichStr = XSSFBRichStr.build(data, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public void handleRecord(int recordType, byte[] data) throws XSSFBParseException
xlWideStringBuffer.setLength(0);
/*offset +=*/ XSSFBUtils.readXLWideString(data, offset, xlWideStringBuffer);
String display = xlWideStringBuffer.toString();
CellRangeAddress cellRangeAddress = new CellRangeAddress(hyperlinkCellRange.firstRow, hyperlinkCellRange.lastRow, hyperlinkCellRange.firstCol, hyperlinkCellRange.lastCol);
CellRangeAddress cellRangeAddress = new CellRangeAddress(hyperlinkCellRange.getFirstRow(), hyperlinkCellRange.getLastRow(), hyperlinkCellRange.getFirstCol(), hyperlinkCellRange.getLastCol());

String url = relIdToHyperlink.get(relId);
if (location.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ Licensed to the Apache Software Foundation (ASF) under one or more
import org.apache.poi.util.Internal;

/**
* @since 3.16-beta3
* @since 6.0.0
*/
@Internal
class XSSFBRichStr {
public class XSSFBRichStr {

public static XSSFBRichStr build(byte[] bytes, int offset) throws XSSFBParseException {
byte first = bytes[offset];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ public void handleRecord(int recordType, byte[] data) throws XSSFBParseException
strings.add(rstr.getString());
break;
case BrtBeginSst:
count = XSSFBUtils.castToInt(LittleEndian.getUInt(data,0));
uniqueCount = XSSFBUtils.castToInt(LittleEndian.getUInt(data, 4));
count = Math.toIntExact(LittleEndian.getUInt(data,0));
uniqueCount = Math.toIntExact(LittleEndian.getUInt(data, 4));
break;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public void handleRecord(int id, byte[] data) throws XSSFBParseException {

switch(type) {
case BrtRowHdr:
int rw = XSSFBUtils.castToInt(LittleEndian.getUInt(data, 0));
int rw = Math.toIntExact(LittleEndian.getUInt(data, 0));
if (rw > 0x00100000) {//could make sure this is larger than currentRow, according to spec?
throw new XSSFBParseException("Row number beyond allowable range: "+rw);
}
Expand Down Expand Up @@ -298,7 +298,7 @@ private void handleCellRk(byte[] data) {

private void handleBrtCellIsst(byte[] data) {
beforeCellValue(data);
int idx = XSSFBUtils.castToInt(LittleEndian.getUInt(data, XSSFBCellHeader.length));
int idx = Math.toIntExact(LittleEndian.getUInt(data, XSSFBCellHeader.length));
RichTextString rtss = stringsTable.getItemAt(idx);
handleStringCellValue(rtss.getString());
}
Expand Down
19 changes: 2 additions & 17 deletions poi-ooxml/src/main/java/org/apache/poi/xssf/binary/XSSFBUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Licensed to the Apache Software Foundation (ASF) under one or more

import java.nio.charset.StandardCharsets;

import org.apache.poi.ooxml.POIXMLException;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;

Expand All @@ -38,7 +37,7 @@ public class XSSFBUtils {
* @return number of bytes read
* @throws XSSFBParseException if there was an exception during reading
*/
static int readXLNullableWideString(byte[] data, int offset, StringBuilder sb) throws XSSFBParseException {
public static int readXLNullableWideString(byte[] data, int offset, StringBuilder sb) throws XSSFBParseException {
long numChars = LittleEndian.getUInt(data, offset);
if (numChars < 0) {
throw new XSSFBParseException("too few chars to read");
Expand Down Expand Up @@ -85,23 +84,9 @@ public static int readXLWideString(byte[] data, int offset, StringBuilder sb) th
return numBytes;
}

static int castToInt(long val) {
if (val < Integer.MAX_VALUE && val > Integer.MIN_VALUE) {
return (int)val;
}
throw new POIXMLException("val ("+val+") can't be cast to int");
}

static short castToShort(int val) {
if (val < Short.MAX_VALUE && val > Short.MIN_VALUE) {
return (short)val;
}
throw new POIXMLException("val ("+val+") can't be cast to short");

}

//TODO: move to LittleEndian?
static int get24BitInt( byte[] data, int offset) {
public static int get24BitInt(byte[] data, int offset) {
int i = offset;
int b0 = data[i++] & 0xFF;
int b1 = data[i++] & 0xFF;
Expand Down