use of org.apache.tika.parser.chm.exception.ChmParsingException in project tika by apache.
the class ChmLzxBlock method createLengthTreeLenTable.
private void createLengthTreeLenTable(int offset, int tablelen, short[] pretreetable, short[] prelentable) throws TikaException {
if (prelentable == null || getChmSection() == null || pretreetable == null || prelentable == null)
throw new ChmParsingException("is null");
// represents offset
int i = offset;
// local counters
int z, y, x;
while (i < tablelen) {
//Read HUFF sym to z
z = pretreetable[getChmSection().peekBits(ChmConstants.LZX_PRETREE_TABLEBITS)];
if (z >= ChmConstants.LZX_PRETREE_NUM_ELEMENTS) {
// 1 bug, should be
// 20
x = ChmConstants.LZX_PRETREE_TABLEBITS;
do {
x++;
z <<= 1;
z += getChmSection().checkBit(x);
} while ((z = pretreetable[z]) >= ChmConstants.LZX_PRETREE_NUM_ELEMENTS);
}
getChmSection().getSyncBits(prelentable[z]);
if (z < 17) {
z = getState().getLengthTreeLengtsTable()[i] - z;
if (z < 0)
z = z + 17;
getState().getLengthTreeLengtsTable()[i] = (short) z;
i++;
} else if (z == 17) {
y = getChmSection().getSyncBits(4);
y += 4;
for (int j = 0; j < y; j++) if (i < getState().getLengthTreeLengtsTable().length)
getState().getLengthTreeLengtsTable()[i++] = 0;
} else if (z == 18) {
y = getChmSection().getSyncBits(5);
y += 20;
for (int j = 0; j < y; j++) //no tolerate //if (i < getState().getLengthTreeLengtsTable().length)
getState().getLengthTreeLengtsTable()[i++] = 0;
} else if (z == 19) {
y = getChmSection().getSyncBits(1);
y += 4;
z = pretreetable[getChmSection().peekBits(ChmConstants.LZX_PRETREE_TABLEBITS)];
if (z >= ChmConstants.LZX_PRETREE_NUM_ELEMENTS) {
// 20
// 6
x = ChmConstants.LZX_PRETREE_TABLEBITS;
do {
x++;
z <<= 1;
z += getChmSection().checkBit(x);
} while (//LZX_MAINTREE_TABLEBITS);
(z = pretreetable[z]) >= ChmConstants.LZX_PRETREE_NUM_ELEMENTS);
}
getChmSection().getSyncBits(prelentable[z]);
z = getState().getLengthTreeLengtsTable()[i] - z;
if (z < 0)
z = z + 17;
for (int j = 0; j < y; j++) getState().getLengthTreeLengtsTable()[i++] = (short) z;
}
}
}
use of org.apache.tika.parser.chm.exception.ChmParsingException in project tika by apache.
the class ChmBlockInfo method getChmBlockInfo.
/**
* Returns an information related to the chmBlockInfo
*
* @param dle
* - DirectoryListingEntry
* @param bytesPerBlock
* - int, = chmLzxcResetTable.block_length
* @param clcd
* - ChmLzxcControlData
* @param chmBlockInfo
* - ChmBlockInfo
*
* @return ChmBlockInfo
* @throws TikaException
*/
protected ChmBlockInfo getChmBlockInfo(DirectoryListingEntry dle, int bytesPerBlock, ChmLzxcControlData clcd, ChmBlockInfo chmBlockInfo) throws TikaException {
if (!validateParameters(dle, bytesPerBlock, clcd, chmBlockInfo))
throw new ChmParsingException("Please check you parameters");
chmBlockInfo.setStartBlock(dle.getOffset() / bytesPerBlock);
chmBlockInfo.setEndBlock((dle.getOffset() + dle.getLength()) / bytesPerBlock);
chmBlockInfo.setStartOffset(dle.getOffset() % bytesPerBlock);
chmBlockInfo.setEndOffset((dle.getOffset() + dle.getLength()) % bytesPerBlock);
// potential problem with casting long to int
chmBlockInfo.setIniBlock(chmBlockInfo.startBlock - chmBlockInfo.startBlock % (int) clcd.getResetInterval());
// % (int) clcd.getResetInterval());
return chmBlockInfo;
}
use of org.apache.tika.parser.chm.exception.ChmParsingException in project tika by apache.
the class ChmLzxBlock method createMainTreeLenTable.
private void createMainTreeLenTable(int offset, int tablelen, short[] pretreetable, short[] prelentable) throws TikaException {
if (pretreetable == null)
throw new ChmParsingException("pretreetable is null");
int i = offset;
int z, y, x;
while (i < tablelen) {
int f = getChmSection().peekBits(ChmConstants.LZX_PRETREE_TABLEBITS);
z = pretreetable[f];
if (z >= ChmConstants.LZX_PRETREE_MAXSYMBOLS) {
x = ChmConstants.LZX_PRETREE_TABLEBITS;
do {
x++;
z <<= 1;
z += getChmSection().checkBit(x);
} while ((z = pretreetable[z]) >= ChmConstants.LZX_PRETREE_MAXSYMBOLS);
}
getChmSection().getSyncBits(prelentable[z]);
if (z < 17) {
z = getState().getMainTreeLengtsTable()[i] - z;
if (z < 0)
z = z + 17;
getState().mainTreeLengtsTable[i] = (short) z;
i++;
} else if (z == 17) {
y = getChmSection().getSyncBits(4);
y += 4;
for (int j = 0; j < y; j++) {
assertInRange(getState().getMainTreeLengtsTable(), i);
getState().mainTreeLengtsTable[i++] = 0;
}
} else if (z == 18) {
y = getChmSection().getSyncBits(5);
y += 20;
for (int j = 0; j < y; j++) {
assertInRange(getState().getMainTreeLengtsTable(), i);
getState().mainTreeLengtsTable[i++] = 0;
}
} else if (z == 19) {
y = getChmSection().getSyncBits(1);
y += 4;
z = pretreetable[getChmSection().peekBits(ChmConstants.LZX_PRETREE_TABLEBITS)];
if (z >= ChmConstants.LZX_PRETREE_MAXSYMBOLS) {
x = ChmConstants.LZX_PRETREE_TABLEBITS;
do {
x++;
z <<= 1;
z += getChmSection().checkBit(x);
} while ((z = pretreetable[z]) >= ChmConstants.LZX_PRETREE_MAXSYMBOLS);
}
getChmSection().getSyncBits(prelentable[z]);
z = getState().mainTreeLengtsTable[i] - z;
if (z < 0)
z = z + 17;
for (int j = 0; j < y; j++) if (i < getState().getMainTreeLengtsTable().length)
getState().mainTreeLengtsTable[i++] = (short) z;
}
}
}
use of org.apache.tika.parser.chm.exception.ChmParsingException in project tika by apache.
the class ChmLzxcControlData method parse.
// @Override
public void parse(byte[] data, ChmLzxcControlData chmLzxcControlData) throws TikaException {
if (data == null || (data.length < ChmConstants.CHM_LZXC_MIN_LEN))
throw new ChmParsingException("we want at least 0x18 bytes");
chmLzxcControlData.setDataRemained(data.length);
chmLzxcControlData.setSize(unmarshalUInt32(data, chmLzxcControlData.getSize()));
chmLzxcControlData.unmarshalCharArray(data, chmLzxcControlData, ChmConstants.CHM_SIGNATURE_LEN);
chmLzxcControlData.setVersion(unmarshalUInt32(data, chmLzxcControlData.getVersion()));
chmLzxcControlData.setResetInterval(unmarshalUInt32(data, chmLzxcControlData.getResetInterval()));
chmLzxcControlData.setWindowSize(unmarshalUInt32(data, chmLzxcControlData.getWindowSize()));
chmLzxcControlData.setWindowsPerReset(unmarshalUInt32(data, chmLzxcControlData.getWindowsPerReset()));
if (data.length >= ChmConstants.CHM_LZXC_V2_LEN)
chmLzxcControlData.setUnknown_18(unmarshalUInt32(data, chmLzxcControlData.getUnknown_18()));
else
chmLzxcControlData.setUnknown_18(0);
if (chmLzxcControlData.getVersion() == 2) {
chmLzxcControlData.setWindowSize(getWindowSize() * ChmConstants.CHM_WINDOW_SIZE_BLOCK);
}
if (chmLzxcControlData.getWindowSize() == 0 || chmLzxcControlData.getResetInterval() == 0)
throw new ChmParsingException("window size / resetInterval should be more than zero");
if (chmLzxcControlData.getWindowSize() == 1)
throw new ChmParsingException("window size / resetInterval should be more than 1");
/* checks a signature */
if (!new String(chmLzxcControlData.getSignature(), UTF_8).equals(ChmConstants.LZXC))
throw new ChmParsingException("the signature does not seem to be correct");
}
use of org.apache.tika.parser.chm.exception.ChmParsingException in project tika by apache.
the class ChmLzxcControlData method unmarshalUInt32.
private long unmarshalUInt32(byte[] data, long dest) throws ChmParsingException {
assert (data != null && data.length > 0);
if (4 > getDataRemained())
throw new ChmParsingException("4 > dataLenght");
dest = data[this.getCurrentPlace()] | data[this.getCurrentPlace() + 1] << 8 | data[this.getCurrentPlace() + 2] << 16 | data[this.getCurrentPlace() + 3] << 24;
setDataRemained(this.getDataRemained() - 4);
this.setCurrentPlace(this.getCurrentPlace() + 4);
return dest;
}
Aggregations