read HeaderBlock properly & fix bug #8

This commit is contained in:
REAndroid 2023-01-13 04:59:33 -05:00
parent b628d6540f
commit 8d786b2844
4 changed files with 27 additions and 23 deletions

View File

@ -129,7 +129,7 @@ package com.reandroid.lib.arsc.chunk;
public void onBlockLoaded(BlockReader reader, Block sender) throws IOException {
if(sender==mPackageId){
int headerSize=getHeaderBlock().getHeaderSize();
if(headerSize!=288){
if(headerSize<288){
mTypeIdOffset.set(0);
mTypeIdOffsetContainer.setItem(null);
}

View File

@ -90,12 +90,12 @@ package com.reandroid.lib.arsc.chunk.xml;
if(headerBlock==null){
return;
}
ChunkType chunkType=headerBlock.getChunkType();
if(chunkType!=ChunkType.XML){
throw new IOException("Not ResXmlBlock: "+headerBlock);
}
BlockReader chunkReader=reader.create(reader.getPosition(), headerBlock.getChunkSize());
getHeaderBlock().readBytes(chunkReader);
headerBlock=getHeaderBlock();
headerBlock.readBytes(chunkReader);
// android/aapt2 accepts 0x0000 (NULL) chunk type as XML, it could
// be android's bug and might be fixed in the future until then lets fix it ourselves
headerBlock.setType(ChunkType.XML);
while (chunkReader.isAvailable()){
boolean readOk=readNext(chunkReader);
if(!readOk){

View File

@ -24,25 +24,8 @@ import java.io.IOException;
import java.io.InputStream;
public class AnyHeader extends HeaderBlock{
private final ByteArray extraBytes;
public AnyHeader() {
super(ChunkType.NULL.ID);
this.extraBytes = new ByteArray();
super.addChild(extraBytes);
}
public ByteArray getExtraBytes() {
return extraBytes;
}
@Override
void onHeaderSizeLoaded(int size){
int max = 0x0000ffff;
if(size > max){
size=max;
}else if(size<0){
size=0;
}
extraBytes.setSize(size-8);
super.onHeaderSizeLoaded(size);
}
public int readBytes(InputStream inputStream) throws IOException {
int result=0;

View File

@ -20,6 +20,7 @@ import com.reandroid.lib.arsc.base.Block;
import com.reandroid.lib.arsc.container.ExpandableBlockContainer;
import com.reandroid.lib.arsc.io.BlockLoad;
import com.reandroid.lib.arsc.io.BlockReader;
import com.reandroid.lib.arsc.item.ByteArray;
import com.reandroid.lib.arsc.item.IntegerItem;
import com.reandroid.lib.arsc.item.ShortItem;
@ -30,11 +31,13 @@ import java.io.IOException;
private final ShortItem mHeaderSize;
private final IntegerItem mChunkSize;
private HeaderLoaded mHeaderLoaded;
private final ByteArray extraBytes;
public HeaderBlock(short type){
super(3);
this.mType=new ShortItem(type);
this.mHeaderSize=new ShortItem();
this.mChunkSize=new IntegerItem();
this.extraBytes=new ByteArray();
addChild(mType);
addChild(mHeaderSize);
addChild(mChunkSize);
@ -42,6 +45,9 @@ import java.io.IOException;
this.mHeaderSize.setBlockLoad(this);
this.mChunkSize.setBlockLoad(this);
}
public ByteArray getExtraBytes() {
return extraBytes;
}
public void setHeaderLoaded(HeaderLoaded headerLoaded){
this.mHeaderLoaded=headerLoaded;
}
@ -94,6 +100,21 @@ import java.io.IOException;
setChunkSize(count);
}
@Override
public void onReadBytes(BlockReader reader) throws IOException {
int start=reader.getPosition();
super.onReadBytes(reader);
int readActual=reader.getPosition() - start;
int difference=getHeaderSize()-readActual;
if(difference==0){
return;
}
if(extraBytes.getParent()==null){
addChild(extraBytes);
}
extraBytes.setSize(difference);
extraBytes.readBytes(reader);
}
@Override
public void onBlockLoaded(BlockReader reader, Block sender) throws IOException {
if(sender==this.mType){
onChunkTypeLoaded(mType.get());