mirror of
https://github.com/revanced/ARSCLib.git
synced 2025-04-30 14:24:25 +02:00
create: Multi-purpose FixedLengthString block
This commit is contained in:
parent
34b101f989
commit
bb1fe20f93
@ -15,35 +15,34 @@
|
||||
*/
|
||||
package com.reandroid.lib.arsc.chunk;
|
||||
|
||||
import com.reandroid.lib.arsc.array.LibraryInfoArray;
|
||||
import com.reandroid.lib.arsc.array.PackageArray;
|
||||
import com.reandroid.lib.arsc.array.SpecTypePairArray;
|
||||
import com.reandroid.lib.arsc.base.Block;
|
||||
import com.reandroid.lib.arsc.container.PackageLastBlocks;
|
||||
import com.reandroid.lib.arsc.container.SingleBlockContainer;
|
||||
import com.reandroid.lib.arsc.container.SpecTypePair;
|
||||
import com.reandroid.lib.arsc.group.EntryGroup;
|
||||
import com.reandroid.lib.arsc.io.BlockLoad;
|
||||
import com.reandroid.lib.arsc.io.BlockReader;
|
||||
import com.reandroid.lib.arsc.item.IntegerItem;
|
||||
import com.reandroid.lib.arsc.item.PackageName;
|
||||
import com.reandroid.lib.arsc.item.ReferenceItem;
|
||||
import com.reandroid.lib.arsc.pool.SpecStringPool;
|
||||
import com.reandroid.lib.arsc.pool.TableStringPool;
|
||||
import com.reandroid.lib.arsc.pool.TypeStringPool;
|
||||
import com.reandroid.lib.arsc.value.EntryBlock;
|
||||
import com.reandroid.lib.arsc.value.LibraryInfo;
|
||||
import com.reandroid.lib.json.JSONConvert;
|
||||
import com.reandroid.lib.json.JSONObject;
|
||||
import com.reandroid.lib.arsc.array.LibraryInfoArray;
|
||||
import com.reandroid.lib.arsc.array.SpecTypePairArray;
|
||||
import com.reandroid.lib.arsc.base.Block;
|
||||
import com.reandroid.lib.arsc.container.PackageLastBlocks;
|
||||
import com.reandroid.lib.arsc.container.SingleBlockContainer;
|
||||
import com.reandroid.lib.arsc.container.SpecTypePair;
|
||||
import com.reandroid.lib.arsc.group.EntryGroup;
|
||||
import com.reandroid.lib.arsc.io.BlockLoad;
|
||||
import com.reandroid.lib.arsc.io.BlockReader;
|
||||
import com.reandroid.lib.arsc.item.FixedLengthString;
|
||||
import com.reandroid.lib.arsc.item.IntegerItem;
|
||||
import com.reandroid.lib.arsc.item.ReferenceItem;
|
||||
import com.reandroid.lib.arsc.pool.SpecStringPool;
|
||||
import com.reandroid.lib.arsc.pool.TableStringPool;
|
||||
import com.reandroid.lib.arsc.pool.TypeStringPool;
|
||||
import com.reandroid.lib.arsc.value.EntryBlock;
|
||||
import com.reandroid.lib.arsc.value.LibraryInfo;
|
||||
import com.reandroid.lib.json.JSONConvert;
|
||||
import com.reandroid.lib.json.JSONObject;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
import java.io.IOException;
|
||||
import java.util.*;
|
||||
|
||||
|
||||
public class PackageBlock extends BaseChunk
|
||||
public class PackageBlock extends BaseChunk
|
||||
implements BlockLoad, JSONConvert<JSONObject>, Comparable<PackageBlock> {
|
||||
private final IntegerItem mPackageId;
|
||||
private final PackageName mPackageName;
|
||||
private final FixedLengthString mPackageName;
|
||||
|
||||
private final IntegerItem mTypeStringPoolOffset;
|
||||
private final IntegerItem mTypeStringPoolCount;
|
||||
@ -66,7 +65,7 @@ public class PackageBlock extends BaseChunk
|
||||
public PackageBlock() {
|
||||
super(ChunkType.PACKAGE, 3);
|
||||
this.mPackageId=new IntegerItem();
|
||||
this.mPackageName=new PackageName();
|
||||
this.mPackageName = new FixedLengthString(256);
|
||||
|
||||
this.mTypeStringPoolOffset = new IntegerItem();
|
||||
this.mTypeStringPoolCount = new IntegerItem();
|
||||
|
@ -0,0 +1,85 @@
|
||||
/*
|
||||
* 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.lib.arsc.item;
|
||||
|
||||
import com.reandroid.lib.arsc.io.BlockReader;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class FixedLengthString extends StringItem{
|
||||
private final int bytesLength;
|
||||
public FixedLengthString(int bytesLength){
|
||||
super(true);
|
||||
this.bytesLength=bytesLength;
|
||||
setBytesLength(bytesLength);
|
||||
}
|
||||
@Override
|
||||
byte[] encodeString(String str){
|
||||
if(str==null){
|
||||
return new byte[bytesLength];
|
||||
}
|
||||
byte[] bts=getUtf16Bytes(str);
|
||||
byte[] results=new byte[bytesLength];
|
||||
int len=bts.length;
|
||||
if(len>bytesLength){
|
||||
len=bytesLength;
|
||||
}
|
||||
System.arraycopy(bts, 0, results, 0, len);
|
||||
return results;
|
||||
}
|
||||
@Override
|
||||
String decodeString(){
|
||||
return decodeUtf16Bytes(getBytesInternal());
|
||||
}
|
||||
@Override
|
||||
public StyleItem getStyle(){
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
int calculateReadLength(BlockReader reader){
|
||||
return bytesLength;
|
||||
}
|
||||
private static String decodeUtf16Bytes(byte[] bts){
|
||||
if(isNullBytes(bts)){
|
||||
return null;
|
||||
}
|
||||
int len=getEndNullPosition(bts);
|
||||
return new String(bts,0, len, StandardCharsets.UTF_16LE);
|
||||
}
|
||||
private static int getEndNullPosition(byte[] bts){
|
||||
int max=bts.length;
|
||||
int result=0;
|
||||
boolean found=false;
|
||||
for(int i=1; i<max;i++){
|
||||
byte b0=bts[i-1];
|
||||
byte b1=bts[i];
|
||||
if(b0==0 && b1==0){
|
||||
if(!found){
|
||||
result=i;
|
||||
found=true;
|
||||
}else if(result<i-1){
|
||||
return result;
|
||||
}
|
||||
}else {
|
||||
found=false;
|
||||
}
|
||||
}
|
||||
if(!found){
|
||||
return max;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* 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.lib.arsc.item;
|
||||
|
||||
import com.reandroid.lib.arsc.io.BlockReader;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class PackageName extends StringItem {
|
||||
public PackageName() {
|
||||
super(true);
|
||||
setBytesLength(BYTES_LENGTH);
|
||||
}
|
||||
@Override
|
||||
byte[] encodeString(String str){
|
||||
if(str==null){
|
||||
return new byte[BYTES_LENGTH];
|
||||
}
|
||||
byte[] bts=getUtf16Bytes(str);
|
||||
byte[] results=new byte[BYTES_LENGTH];
|
||||
int len=bts.length;
|
||||
if(len>BYTES_LENGTH){
|
||||
len=BYTES_LENGTH;
|
||||
}
|
||||
System.arraycopy(bts, 0, results, 0, len);
|
||||
return results;
|
||||
}
|
||||
@Override
|
||||
String decodeString(){
|
||||
return decodeUtf16Bytes(getBytesInternal());
|
||||
}
|
||||
@Override
|
||||
public StyleItem getStyle(){
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
int calculateReadLength(BlockReader reader){
|
||||
return BYTES_LENGTH;
|
||||
}
|
||||
private static String decodeUtf16Bytes(byte[] bts){
|
||||
if(isNullBytes(bts)){
|
||||
return null;
|
||||
}
|
||||
int len=getEndNullPosition(bts);
|
||||
return new String(bts,0, len, StandardCharsets.UTF_16LE);
|
||||
}
|
||||
private static int getEndNullPosition(byte[] bts){
|
||||
int max=bts.length;
|
||||
int result=0;
|
||||
boolean found=false;
|
||||
for(int i=1; i<max;i++){
|
||||
byte b0=bts[i-1];
|
||||
byte b1=bts[i];
|
||||
if(b0==0 && b1==0){
|
||||
if(!found){
|
||||
result=i;
|
||||
found=true;
|
||||
}else if(result<i-1){
|
||||
return result;
|
||||
}
|
||||
}else {
|
||||
found=false;
|
||||
}
|
||||
}
|
||||
if(!found){
|
||||
return max;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
private static final int BYTES_LENGTH=256;
|
||||
|
||||
}
|
@ -18,8 +18,8 @@ package com.reandroid.lib.arsc.value;
|
||||
import com.reandroid.lib.arsc.base.Block;
|
||||
import com.reandroid.lib.arsc.base.BlockCounter;
|
||||
import com.reandroid.lib.arsc.io.BlockReader;
|
||||
import com.reandroid.lib.arsc.item.FixedLengthString;
|
||||
import com.reandroid.lib.arsc.item.IntegerItem;
|
||||
import com.reandroid.lib.arsc.item.PackageName;
|
||||
import com.reandroid.lib.json.JSONConvert;
|
||||
import com.reandroid.lib.json.JSONObject;
|
||||
|
||||
@ -28,12 +28,12 @@ import java.io.OutputStream;
|
||||
|
||||
public class LibraryInfo extends Block implements JSONConvert<JSONObject> {
|
||||
private final IntegerItem mPackageId;
|
||||
private final PackageName mPackageName;
|
||||
private final FixedLengthString mPackageName;
|
||||
|
||||
public LibraryInfo(){
|
||||
super();
|
||||
this.mPackageId=new IntegerItem();
|
||||
this.mPackageName=new PackageName();
|
||||
this.mPackageName = new FixedLengthString(256);
|
||||
mPackageId.setIndex(0);
|
||||
mPackageId.setParent(this);
|
||||
mPackageName.setIndex(1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user