Skip to content

Commit

Permalink
[DEX] Any byte-width number formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Nov 27, 2024
1 parent 69fd554 commit 5684732
Show file tree
Hide file tree
Showing 34 changed files with 1,327 additions and 561 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/reandroid/arsc/base/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,31 @@ public static void putBigEndianLong(byte[] bytes, int offset, long value){
index --;
}
}
public static long getUnsignedNumber(byte[] bytes, int offset, int size){
if ((offset + size) > bytes.length) {
return 0;
}
long result = 0;
int index = offset + size - 1;
while (index >= offset) {
result = result << 8;
result |= (bytes[index] & 0xff);
index --;
}
return result;
}
public static void putNumber(byte[] bytes, int offset, int size, long value) {
if((offset + size) > bytes.length){
return;
}
int index = offset;
offset = index + size;
while (index < offset) {
bytes[index] = (byte) (value & 0xff);
value = value >>> 8;
index++;
}
}
public static byte[] getBytes(byte[] bytes, int offset, int length){
if(bytes.length == 0){
return new byte[0];
Expand Down
82 changes: 82 additions & 0 deletions src/main/java/com/reandroid/arsc/item/NumberBlock.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (C) 2022 github.com/REAndroid
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.reandroid.arsc.item;

import com.reandroid.utils.NumberX;

public class NumberBlock extends BlockItem implements LongReference {

public NumberBlock(int width) {
super(width);
}

@Override
public long getLong() {
return NumberX.valueOfUnsigned(width(), unsigned()).longValue();
}
@Override
public void set(long value) {
byte[] bytes = getBytesInternal();
int length = bytes.length;
validate(length, value);
putNumber(bytes, 0, length, value);
}
@Override
public int get() {
return (int) getLong();
}
@Override
public void set(int value) {
set((long) value);
}

public int width() {
return countBytes();
}
public void width(int width) {
if (width == width()) {
return;
}
long value = getLong();
setBytesLength(width, false);
set(value);
}
private long unsigned() {
byte[] bytes = getBytesInternal();
return getUnsignedNumber(bytes, 0, bytes.length);
}
private void validate(int width, long value) {
if (value != 0) {
if (width == 0) {
throw new NumberFormatException("Width == 0 for value = " + value);
}
if ((value < 0 && value < NumberX.minValueForWidth(width)) ||
(value > 0 && value > NumberX.maxValueForWidth(width))) {
throw new NumberFormatException("Out of range for width = " +
width + ", value = " + value);
}
}
}

public String toHexString() {
return NumberX.toHexString(width(), getLong());
}

@Override
public String toString() {
return toHexString();
}
}
25 changes: 0 additions & 25 deletions src/main/java/com/reandroid/dex/base/DexBlockItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,4 @@ protected static long getSignedNumber(byte[] bytes, int offset, int size){
}
return result;
}
protected static long getUnsignedNumber(byte[] bytes, int offset, int size){
if((offset + size)> bytes.length){
return 0;
}
long result = 0;
int index = offset + size - 1;
while (index>=offset){
result = result << 8;
result |= (bytes[index] & 0xff);
index --;
}
return result;
}
protected static void putNumber(byte[] bytes, int offset, int size, long value){
if((offset + size) > bytes.length){
return;
}
int index = offset;
offset = index + size;
while (index<offset){
bytes[index] = (byte) (value & 0xff);
value = value >>> 8;
index++;
}
}
}
Loading

0 comments on commit 5684732

Please sign in to comment.