-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use variable length encoding instead short for offsets (can over…
…flow) (#1489)
- Loading branch information
Showing
10 changed files
with
130 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
jadx-gui/src/main/java/jadx/gui/utils/codecache/disk/adapters/BaseDataAdapter.java
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
jadx-gui/src/main/java/jadx/gui/utils/codecache/disk/adapters/DataAdapterHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package jadx.gui.utils.codecache.disk.adapters; | ||
|
||
import java.io.DataInput; | ||
import java.io.DataOutput; | ||
import java.io.IOException; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
|
||
public class DataAdapterHelper { | ||
|
||
public static void writeNullableUTF(DataOutput out, @Nullable String str) throws IOException { | ||
if (str == null) { | ||
out.writeByte(0); | ||
} else { | ||
out.writeByte(1); | ||
out.writeUTF(str); | ||
} | ||
} | ||
|
||
public static @Nullable String readNullableUTF(DataInput in) throws IOException { | ||
if (in.readByte() == 0) { | ||
return null; | ||
} | ||
return in.readUTF(); | ||
} | ||
|
||
/** | ||
* Write unsigned variable length integer (ULEB128 encoding) | ||
*/ | ||
public static void writeUVInt(DataOutput out, int val) throws IOException { | ||
if (val < 0) { | ||
throw new IllegalArgumentException("Expect value >= 0, got: " + val); | ||
} | ||
int current = val; | ||
int next = val; | ||
while (true) { | ||
next >>>= 7; | ||
if (next == 0) { | ||
// last byte | ||
out.writeByte(current & 0x7f); | ||
return; | ||
} | ||
out.writeByte((current & 0x7f) | 0x80); | ||
current = next; | ||
} | ||
} | ||
|
||
/** | ||
* Read unsigned variable length integer (ULEB128 encoding) | ||
*/ | ||
public static int readUVInt(DataInput in) throws IOException { | ||
int result = 0; | ||
int shift = 0; | ||
while (true) { | ||
byte v = in.readByte(); | ||
result |= (v & (byte) 0x7f) << shift; | ||
shift += 7; | ||
if ((v & 0x80) != 0x80) { | ||
return result; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
jadx-gui/src/test/java/jadx/gui/utils/codecache/disk/adapters/DataAdapterHelperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package jadx.gui.utils.codecache.disk.adapters; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.DataInput; | ||
import java.io.DataInputStream; | ||
import java.io.DataOutputStream; | ||
import java.io.IOException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class DataAdapterHelperTest { | ||
|
||
@Test | ||
void uvInt() throws IOException { | ||
checkUVIntFor(0); | ||
checkUVIntFor(7); | ||
checkUVIntFor(0x7f); | ||
checkUVIntFor(0x80); | ||
checkUVIntFor(0x256); | ||
checkUVIntFor(Byte.MAX_VALUE); | ||
checkUVIntFor(Short.MAX_VALUE); | ||
checkUVIntFor(Integer.MAX_VALUE); | ||
} | ||
|
||
private void checkUVIntFor(int val) throws IOException { | ||
assertThat(writeReadUVInt(val)).isEqualTo(val); | ||
} | ||
|
||
private int writeReadUVInt(int val) throws IOException { | ||
ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); | ||
DataOutputStream out = new DataOutputStream(byteOut); | ||
DataAdapterHelper.writeUVInt(out, val); | ||
|
||
DataInput in = new DataInputStream(new ByteArrayInputStream(byteOut.toByteArray())); | ||
return DataAdapterHelper.readUVInt(in); | ||
} | ||
} |