Skip to content
This repository has been archived by the owner on Sep 21, 2020. It is now read-only.

Commit

Permalink
fix method not found bug when run in low java versoin
Browse files Browse the repository at this point in the history
cast ByteBuffer to Buffer when call position method
  • Loading branch information
Liu Dong committed Nov 20, 2017
1 parent 641df18 commit 2edad17
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 138 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Apk-parser has been submitted to maven central repo. With maven, you can add apk
<dependency>
<groupId>net.dongliu</groupId>
<artifactId>apk-parser</artifactId>
<version>2.4.0</version>
<version>2.4.1</version>
</dependency>
```
From version 2.0, apk-parser requires java7. The last version support java6 is 1.7.4.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<artifactId>apk-parser</artifactId>
<name>apk-parser</name>
<packaging>jar</packaging>
<version>2.4.0</version>
<version>2.4.1</version>
<url>https://github.com/xiaxiaocao/apk-parser</url>
<developers>
<developer>
Expand Down
46 changes: 23 additions & 23 deletions src/main/java/net/dongliu/apk/parser/parser/BinaryXmlParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import net.dongliu.apk.parser.struct.*;
import net.dongliu.apk.parser.struct.resource.ResourceTable;
import net.dongliu.apk.parser.struct.xml.*;
import net.dongliu.apk.parser.utils.Buffers;
import net.dongliu.apk.parser.utils.ByteBuffers;
import net.dongliu.apk.parser.utils.ParseUtils;
import net.dongliu.apk.parser.utils.Utils;

Expand Down Expand Up @@ -109,12 +109,12 @@ public void parse() {
default:
if (chunkHeader.getChunkType() >= ChunkType.XML_FIRST_CHUNK &&
chunkHeader.getChunkType() <= ChunkType.XML_LAST_CHUNK) {
Buffers.skip(buffer, chunkHeader.getBodySize());
ByteBuffers.skip(buffer, chunkHeader.getBodySize());
} else {
throw new ParserException("Unexpected chunk type:" + chunkHeader.getChunkType());
}
}
buffer.position((int) (beginPos + chunkHeader.getBodySize()));
ByteBuffers.position(buffer, beginPos + chunkHeader.getBodySize());
chunkHeader = readChunkHeader();
}
}
Expand Down Expand Up @@ -160,12 +160,12 @@ private XmlNodeStartTag readXmlNodeStartTag() {

// read attributes.
// attributeStart and attributeSize are always 20 (0x14)
int attributeStart = Buffers.readUShort(buffer);
int attributeSize = Buffers.readUShort(buffer);
int attributeCount = Buffers.readUShort(buffer);
int idIndex = Buffers.readUShort(buffer);
int classIndex = Buffers.readUShort(buffer);
int styleIndex = Buffers.readUShort(buffer);
int attributeStart = ByteBuffers.readUShort(buffer);
int attributeSize = ByteBuffers.readUShort(buffer);
int attributeCount = ByteBuffers.readUShort(buffer);
int idIndex = ByteBuffers.readUShort(buffer);
int classIndex = ByteBuffers.readUShort(buffer);
int styleIndex = ByteBuffers.readUShort(buffer);

// read attributes
Attributes attributes = new Attributes(attributeCount);
Expand Down Expand Up @@ -272,7 +272,7 @@ private long[] readXmlResourceMap(XmlResourceMapHeader chunkHeader) {
int count = chunkHeader.getBodySize() / 4;
long[] resourceIds = new long[count];
for (int i = 0; i < count; i++) {
resourceIds[i] = Buffers.readUInt(buffer);
resourceIds[i] = ByteBuffers.readUInt(buffer);
}
return resourceIds;
}
Expand All @@ -285,34 +285,34 @@ private ChunkHeader readChunkHeader() {
}

long begin = buffer.position();
int chunkType = Buffers.readUShort(buffer);
int headerSize = Buffers.readUShort(buffer);
long chunkSize = Buffers.readUInt(buffer);
int chunkType = ByteBuffers.readUShort(buffer);
int headerSize = ByteBuffers.readUShort(buffer);
long chunkSize = ByteBuffers.readUInt(buffer);

switch (chunkType) {
case ChunkType.XML:
return new XmlHeader(chunkType, headerSize, chunkSize);
case ChunkType.STRING_POOL:
StringPoolHeader stringPoolHeader = new StringPoolHeader(chunkType, headerSize, chunkSize);
stringPoolHeader.setStringCount(Buffers.readUInt(buffer));
stringPoolHeader.setStyleCount(Buffers.readUInt(buffer));
stringPoolHeader.setFlags(Buffers.readUInt(buffer));
stringPoolHeader.setStringsStart(Buffers.readUInt(buffer));
stringPoolHeader.setStylesStart(Buffers.readUInt(buffer));
buffer.position((int) (begin + headerSize));
stringPoolHeader.setStringCount(ByteBuffers.readUInt(buffer));
stringPoolHeader.setStyleCount(ByteBuffers.readUInt(buffer));
stringPoolHeader.setFlags(ByteBuffers.readUInt(buffer));
stringPoolHeader.setStringsStart(ByteBuffers.readUInt(buffer));
stringPoolHeader.setStylesStart(ByteBuffers.readUInt(buffer));
ByteBuffers.position(buffer, begin + headerSize);
return stringPoolHeader;
case ChunkType.XML_RESOURCE_MAP:
buffer.position((int) (begin + headerSize));
ByteBuffers.position(buffer, begin + headerSize);
return new XmlResourceMapHeader(chunkType, headerSize, chunkSize);
case ChunkType.XML_START_NAMESPACE:
case ChunkType.XML_END_NAMESPACE:
case ChunkType.XML_START_ELEMENT:
case ChunkType.XML_END_ELEMENT:
case ChunkType.XML_CDATA:
XmlNodeHeader header = new XmlNodeHeader(chunkType, headerSize, chunkSize);
header.setLineNum((int) Buffers.readUInt(buffer));
header.setCommentRef((int) Buffers.readUInt(buffer));
buffer.position((int) (begin + headerSize));
header.setLineNum((int) ByteBuffers.readUInt(buffer));
header.setCommentRef((int) ByteBuffers.readUInt(buffer));
ByteBuffers.position(buffer, begin + headerSize);
return header;
case ChunkType.NULL:
return new NullHeader(chunkType, headerSize, chunkSize);
Expand Down
64 changes: 32 additions & 32 deletions src/main/java/net/dongliu/apk/parser/parser/DexParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import net.dongliu.apk.parser.struct.StringPool;
import net.dongliu.apk.parser.struct.dex.DexClassStruct;
import net.dongliu.apk.parser.struct.dex.DexHeader;
import net.dongliu.apk.parser.utils.Buffers;
import net.dongliu.apk.parser.utils.ByteBuffers;

import java.io.IOException;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -36,7 +36,7 @@ public DexParser(ByteBuffer buffer) {

public void parse() {
// read magic
String magic = new String(Buffers.readBytes(buffer, 8));
String magic = new String(ByteBuffers.readBytes(buffer, 8));
if (!magic.startsWith("dex\n")) {
return;
}
Expand Down Expand Up @@ -88,7 +88,7 @@ public void parse() {
* read class info.
*/
private DexClassStruct[] readClass(long classDefsOff, int classDefsSize) {
buffer.position((int) classDefsOff);
ByteBuffers.position(buffer, classDefsOff);

DexClassStruct[] dexClassStructs = new DexClassStruct[classDefsSize];
for (int i = 0; i < classDefsSize; i++) {
Expand All @@ -98,11 +98,11 @@ private DexClassStruct[] readClass(long classDefsOff, int classDefsSize) {
dexClassStruct.setAccessFlags(buffer.getInt());
dexClassStruct.setSuperclassIdx(buffer.getInt());

dexClassStruct.setInterfacesOff(Buffers.readUInt(buffer));
dexClassStruct.setInterfacesOff(ByteBuffers.readUInt(buffer));
dexClassStruct.setSourceFileIdx(buffer.getInt());
dexClassStruct.setAnnotationsOff(Buffers.readUInt(buffer));
dexClassStruct.setClassDataOff(Buffers.readUInt(buffer));
dexClassStruct.setStaticValuesOff(Buffers.readUInt(buffer));
dexClassStruct.setAnnotationsOff(ByteBuffers.readUInt(buffer));
dexClassStruct.setClassDataOff(ByteBuffers.readUInt(buffer));
dexClassStruct.setStaticValuesOff(ByteBuffers.readUInt(buffer));
dexClassStructs[i] = dexClassStruct;
}

Expand All @@ -113,10 +113,10 @@ private DexClassStruct[] readClass(long classDefsOff, int classDefsSize) {
* read types.
*/
private int[] readTypes(long typeIdsOff, int typeIdsSize) {
buffer.position((int) typeIdsOff);
ByteBuffers.position(buffer, typeIdsOff);
int[] typeIds = new int[typeIdsSize];
for (int i = 0; i < typeIdsSize; i++) {
typeIds[i] = (int) Buffers.readUInt(buffer);
typeIds[i] = (int) ByteBuffers.readUInt(buffer);
}
return typeIds;
}
Expand Down Expand Up @@ -146,7 +146,7 @@ private StringPool readStrings(long[] offsets) {
stringpool.set(entry.getIdx(), lastStr);
continue;
}
buffer.position((int) entry.getOffset());
ByteBuffers.position(buffer, entry.getOffset());
lastOffset = entry.getOffset();
String str = readString();
lastStr = str;
Expand All @@ -159,10 +159,10 @@ private StringPool readStrings(long[] offsets) {
* read string identifiers list.
*/
private long[] readStringPool(long stringIdsOff, int stringIdsSize) {
buffer.position((int) stringIdsOff);
ByteBuffers.position(buffer, stringIdsOff);
long offsets[] = new long[stringIdsSize];
for (int i = 0; i < stringIdsSize; i++) {
offsets[i] = Buffers.readUInt(buffer);
offsets[i] = ByteBuffers.readUInt(buffer);
}

return offsets;
Expand All @@ -186,17 +186,17 @@ private String readString(int strLen) {
char[] chars = new char[strLen];

for (int i = 0; i < strLen; i++) {
short a = Buffers.readUByte(buffer);
short a = ByteBuffers.readUByte(buffer);
if ((a & 0x80) == 0) {
// ascii char
chars[i] = (char) a;
} else if ((a & 0xe0) == 0xc0) {
// read one more
short b = Buffers.readUByte(buffer);
short b = ByteBuffers.readUByte(buffer);
chars[i] = (char) (((a & 0x1F) << 6) | (b & 0x3F));
} else if ((a & 0xf0) == 0xe0) {
short b = Buffers.readUByte(buffer);
short c = Buffers.readUByte(buffer);
short b = ByteBuffers.readUByte(buffer);
short c = ByteBuffers.readUByte(buffer);
chars[i] = (char) (((a & 0x0F) << 12) | ((b & 0x3F) << 6) | (c & 0x3F));
} else if ((a & 0xf0) == 0xf0) {
//throw new UTFDataFormatException();
Expand Down Expand Up @@ -227,7 +227,7 @@ private int readVarInts() {
if (count > 4) {
throw new ParserException("read varints error.");
}
s = Buffers.readUByte(buffer);
s = ByteBuffers.readUByte(buffer);
i |= (s & 0x7f) << (count * 7);
count++;
} while ((s & 0x80) != 0);
Expand All @@ -241,44 +241,44 @@ private DexHeader readDexHeader() {
buffer.getInt();

// signature skip
Buffers.readBytes(buffer, DexHeader.kSHA1DigestLen);
ByteBuffers.readBytes(buffer, DexHeader.kSHA1DigestLen);

DexHeader header = new DexHeader();
header.setFileSize(Buffers.readUInt(buffer));
header.setHeaderSize(Buffers.readUInt(buffer));
header.setFileSize(ByteBuffers.readUInt(buffer));
header.setHeaderSize(ByteBuffers.readUInt(buffer));

// skip?
Buffers.readUInt(buffer);
ByteBuffers.readUInt(buffer);

// static link data
header.setLinkSize(Buffers.readUInt(buffer));
header.setLinkOff(Buffers.readUInt(buffer));
header.setLinkSize(ByteBuffers.readUInt(buffer));
header.setLinkOff(ByteBuffers.readUInt(buffer));

// the map data is just the same as dex header.
header.setMapOff(Buffers.readUInt(buffer));
header.setMapOff(ByteBuffers.readUInt(buffer));

header.setStringIdsSize(buffer.getInt());
header.setStringIdsOff(Buffers.readUInt(buffer));
header.setStringIdsOff(ByteBuffers.readUInt(buffer));

header.setTypeIdsSize(buffer.getInt());
header.setTypeIdsOff(Buffers.readUInt(buffer));
header.setTypeIdsOff(ByteBuffers.readUInt(buffer));

header.setProtoIdsSize(buffer.getInt());
header.setProtoIdsOff(Buffers.readUInt(buffer));
header.setProtoIdsOff(ByteBuffers.readUInt(buffer));

header.setFieldIdsSize(buffer.getInt());
header.setFieldIdsOff(Buffers.readUInt(buffer));
header.setFieldIdsOff(ByteBuffers.readUInt(buffer));

header.setMethodIdsSize(buffer.getInt());
header.setMethodIdsOff(Buffers.readUInt(buffer));
header.setMethodIdsOff(ByteBuffers.readUInt(buffer));

header.setClassDefsSize(buffer.getInt());
header.setClassDefsOff(Buffers.readUInt(buffer));
header.setClassDefsOff(ByteBuffers.readUInt(buffer));

header.setDataSize(buffer.getInt());
header.setDataOff(Buffers.readUInt(buffer));
header.setDataOff(ByteBuffers.readUInt(buffer));

buffer.position((int) header.getHeaderSize());
ByteBuffers.position(buffer, header.getHeaderSize());

return header;
}
Expand Down
Loading

0 comments on commit 2edad17

Please sign in to comment.