Skip to content

Commit

Permalink
Check EOF while reading smali
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed May 6, 2024
1 parent 101b36c commit 1d38dc1
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 10 deletions.
15 changes: 12 additions & 3 deletions src/main/java/com/reandroid/dex/model/DexFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ public void parseSmaliFile(File file) throws IOException {
requireNotClosed();
fromSmali(SmaliReader.of(file));
}
public void fromSmali(SmaliReader reader) throws IOException {
public void fromSmaliMultipleClasses(SmaliReader reader) throws IOException {
requireNotClosed();
while (SmaliDirective.parse(reader, false) == SmaliDirective.CLASS){
SmaliClass smaliClass = new SmaliClass();
Expand All @@ -506,9 +506,18 @@ public void fromSmali(SmaliReader reader) throws IOException {
reader.skipWhitespacesOrComment();
}
}
public void fromSmali(SmaliClass smaliClass) throws IOException {
public DexClass fromSmali(SmaliReader reader) throws IOException {
requireNotClosed();
getDexLayout().fromSmali(smaliClass);
SmaliClass smaliClass = new SmaliClass();
smaliClass.parse(reader);
DexClass dexClass = fromSmali(smaliClass);
reader.skipWhitespacesOrComment();
return dexClass;
}
public DexClass fromSmali(SmaliClass smaliClass) throws IOException {
requireNotClosed();
ClassId classId = getDexLayout().fromSmali(smaliClass);
return create(classId);
}

public byte[] getBytes() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/reandroid/dex/sections/DexLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,8 @@ public boolean merge(MergeOptions options, DexLayout dexFile){
}
return getSectionList().merge(options, dexFile.getSectionList());
}
public void fromSmali(SmaliClass smaliClass) throws IOException {
getSectionList().fromSmali(smaliClass);
public ClassId fromSmali(SmaliClass smaliClass) throws IOException {
return getSectionList().fromSmali(smaliClass);
}
@Override
public byte[] getBytes(){
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/reandroid/dex/sections/SectionList.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,10 @@ public boolean merge(MergeOptions options, SectionList sectionList){
}
return mergedAll;
}
public void fromSmali(SmaliClass smaliClass) throws IOException {
public ClassId fromSmali(SmaliClass smaliClass) throws IOException {
ClassId classId = getOrCreateSectionItem(SectionType.CLASS_ID, smaliClass.getKey());
classId.fromSmali(smaliClass);
return classId;
}
private static<T1 extends Section<?>> Comparator<T1> getOffsetComparator() {
return (section1, section2) -> {
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/com/reandroid/dex/smali/SmaliDirective.java
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ public static SmaliDirective parse(SmaliReader reader){
return parse(reader, true);
}
public static SmaliDirective parse(SmaliReader reader, boolean skip){
if(reader == null){
if(reader == null || reader.finished()){
return null;
}
int position = reader.position();
Expand Down Expand Up @@ -218,9 +218,11 @@ public static SmaliDirective parse(SmaliReader reader, boolean skip){
return directive;
}
private static SmaliDirective directiveOf(SmaliReader reader){
for(SmaliDirective smaliDirective : VALUES){
if(smaliDirective.readMatches(reader)){
return smaliDirective;
if(!reader.finished()) {
for(SmaliDirective smaliDirective : VALUES){
if(smaliDirective.readMatches(reader)){
return smaliDirective;
}
}
}
return null;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/reandroid/dex/smali/SmaliReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ public int startsWithSqueezeSpaces(byte[] bytes){
return -1;
}
}
if(index == bytesLength) {
return bytesLength;
}
return -1;
}
public int indexOf(char ch){
Expand Down Expand Up @@ -290,6 +293,9 @@ public int indexOf(byte[] bytes){
}
private boolean equalsAt(int index, byte[] bytes){
int length = bytes.length;
if(length > available() - index) {
return false;
}
for(int i = 0; i < length; i++){
if(bytes[i] != get(i + index)){
return false;
Expand All @@ -298,6 +304,9 @@ private boolean equalsAt(int index, byte[] bytes){
return true;
}
public boolean skipWhitespacesOrComment(){
if(finished()) {
return false;
}
boolean result = false;
if(get() == '#'){
nextLine();
Expand All @@ -312,6 +321,9 @@ public boolean skipWhitespacesOrComment(){
return result;
}
public boolean skipWhitespaces(){
if(finished()) {
return false;
}
int pos = position();
int nextPosition = pos;
int end = pos + available();
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/reandroid/dex/smali/model/SmaliClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public void parse(SmaliReader reader) throws IOException {
reader.skipWhitespacesOrComment();
}
private boolean parseNext(SmaliReader reader) throws IOException {
if(reader.finished()) {
return false;
}
reader.skipWhitespacesOrComment();
SmaliDirective directive = SmaliDirective.parse(reader, false);
if(directive == SmaliDirective.CLASS){
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/reandroid/dex/smali/model/SmaliDefSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public void parse(SmaliReader reader) throws IOException {
}
}
private boolean parseNext(SmaliReader reader) throws IOException {
if(reader.finished()){
return false;
}
reader.skipWhitespacesOrComment();
SmaliDirective directive = SmaliDirective.parse(reader, false);
if(directive != getSmaliDirective()){
Expand Down

0 comments on commit 1d38dc1

Please sign in to comment.