https://qiita.com/gnagaoka/items/1a1c862b9def660abb77
There are N structures. The number of arrays is written shortly before Zubya, and after reading it, read the structure for that number. In other words, the internal values are an array, so it looks like the following.
public class LIST<T extends DataTypeInterface> extends SingleDataType<ArrayList<T>> {
/**
*Loop stop judgment
*/
private BiPredicate<Integer,Integer> isNext;
/**
*Factory 2
*/
private IntFunction<T> factory;
/**
* constructor
* @param factory factory
* @param isNext Whether to move to the next element
*/
public LIST( IntFunction<T> factory,
BiPredicate<Integer,Integer> isNext){
this.factory = factory;
this.isNext = isNext;
this.set(new ArrayList<>());
}
/**
*Return the size of the object
*
* @Byte size of return object
*/
@Override
public Integer getSize() {
return this.get().stream().mapToInt(DataTypeInterface::getSize).sum();
}
/**
* 1.Record the current offset position of the buffer<br>
* 2.Get the number of scans<br>
* 3.Only the number of scans 4,Process 5<br>
* 4.Perform preDecompile to create an instance<br>
* 5.Execute the instance decompile method<br>
* <br>
** Child elements that failed to decompile are skipped.<br>
* <br>
* @param buf buffer
*/
@Override
public void decompile(ByteBuffer buf){
this.setOffset(buf.position());
Integer index = 0;
Integer length = 0;
while (this.isNext.test(index, length)) {
try {
T s = this.factory.apply(index);
s.decompile(buf);
this.get().add(s);
} catch (LISTSkipException e) {
continue;
} catch (LISTStopException e) {
break;
} finally {
index++;
length = buf.position() - this.getOffset();
}
}
}
/**
* 1.Record the current offset position of the buffer<br>
* 2.Execute the compile method on all child elements<br>
* <br>
** Child elements that failed to compile are skipped
*
* @param buf buffer
*/
@Override
public void compile(ByteBuffer buf) throws IllegalStateException,IllegalArgumentException {
this.setOffset(buf.position());
for(T s : this.get()){
try {
s.compile(buf);
} catch (LISTSkipException e) {
continue;
} catch (LISTStopException e) {
break;
}
}
}
/*
advanced method
*/
/**
*Get the first element of the array
* @return first element
*/
public T first(){
return this.get(0);
}
/**
*Get the second element of the array
* @return Last element
*/
public T second(){
return this.get(1);
}
/**
*Get the last element of the array
* @return Last element
*/
public T last(){
return this.get().get(this.get().size() - 1);
}
/**
*Add the last element of the array
* @return Last element
*/
public T push(){
return this.get(this.get().size());
}
/**
*Internal value[index]To get.
*
* @param index index
* @return index value
*/
public T get(Integer index) {
T x;
try {
x = this.get().get(index);
}catch (IndexOutOfBoundsException e){
x = this.factory.apply(index);
/*Umaru up to index*/
while((this.get().size()) < (index+1)){
x = this.factory.apply(this.get().size());
this.get().add(x);
}
}
return x;
}
}
The points are isNext and factory. By using these, you can refer to other feels using lambda for the number of arrays. The usage is as follows.
@PROPERTY(order = 5)
public USHORT numHoge= new USHORT();
@PROPERTY(order = 6)
public LIST<HogeObject> items = new LIST<>(
(i) -> new HogeObject(),
(i,len) -> this.numHoge.get() > i
);
It is quite possible that the offset value to the structure is defined first, and then the structure is read after moving to the offset destination. The offset is often also from the beginning of its own struct, not from the beginning of the binary. It's like this.
public class OFFSETW<T extends DataTypeInterface> extends CompositeDataType {
/**
*Reference to offset
*/
protected NumericDataType offset;
/**
*Reference to byte length
*/
protected NumericDataType lengthW;
/**
*Reference to checksum
*/
protected NumericDataType checkSum;
/**
*Reference to base offset
*/
protected DataTypeInterface baseObject;
/*
structure
*/
/**
*Reference to reference object
*/
@PROPERTY(order = 1)
public T reference;
/*
structure method
*/
/**
* constructor<br>
*Define a reference object that is offset from the base object
*
* @param offset offset
* @param baseObject Base object
* @param reference reference object
*/
public OFFSETW(NumericDataType offset,DataTypeInterface baseObject,T reference) {
this.offset = offset;
this.reference = reference;
this.baseObject = baseObject;
}
/**
* constructor<br>
*Define a reference object that is offset from the base object
*
* @param buf buffer
* @param offset offset
* @param baseObject Base object
* @param reference reference object
*/
public OFFSETW(ByteBuffer buf , NumericDataType offset,DataTypeInterface baseObject,T reference) {
this.offset = offset;
this.reference = reference;
this.baseObject = baseObject;
this.decompile(buf);
}
/**
* constructor
*Define a reference object that is offset from the base object<br>
* <br>
** This constructor is a little special, and the byte length of the target reference object is set to the specified location.<br>
*Link It also calculates a checksum from that byte string and links it to the specified location.<br>
*This is especially useful for cmap subtables and Font tables.<br>
*
* ToDO:It is necessary to consider whether to make it a different class
*
* @param offset offset
* @param baseObject Base object
* @param reference reference object
* @param length Byte length object
* @param checkSum checksum object
*/
public OFFSETW(NumericDataType offset,
NumericDataType length,
NumericDataType checkSum,
DataTypeInterface baseObject,
T reference
) {
this.offset = offset;
this.reference = reference;
this.baseObject = baseObject;
this.lengthW = length;
this.checkSum = checkSum;
}
/**
*Converts an object to a byte buffer.<br>
*Only the offset value conversion is done.<br>
*Since the offset is unknown, 0 is written.<br>
*The offset to which the offset value is written is recorded.<br>
*
* @param buf buffer
*/
@Override
public void compile(ByteBuffer buf) {
this.setOffset(buf.position());
Integer offset = buf.position();
buf.position(this.offset.getOffset());
this.offset.setNumber(offset - baseObject.getOffset());
this.offset.compile(buf);
buf.position(offset);
this.reference.compile(buf);
/*Make it divisible by 4*/
offset = buf.position();
Long length = offset.longValue() - this.getOffset();
/*Indivisible*/
Long mod = length % 4;
if(mod > 0){
/*padding*/
for(Integer j = 0; j< (4-mod) ; j++){
buf.put((byte)0);
}
}
/*Offset update*/
offset = buf.position();
/*When byte length write is specified*/
if(this.lengthW != null){
buf.position(this.lengthW.getOffset());
this.lengthW.setNumber(length);
this.lengthW.compile(buf);
buf.position(offset);
}
}
/*
advanced method
*/
/**
*Get child elements
*
* @return child element
*/
@Override
public T get() {
return this.reference;
}
/**
*Converts a byte buffer to an object.<br>
*Converts a byte buffer to an object.<br>
*The offset destination is decompiled at the same time as the offset value is read.<br>
*At that time, you can also add the base offset to the offset value.<br>
*Set to 0 if the base offset does not exist.<br>
*
* @param buf buffer
*/
@Override
public void decompile(ByteBuffer buf){
/**
*offset is 0 is null
*/
if(this.offset.get().intValue() == 0){
return ;
}
/**
*Remember the current offset
*/
Integer old_offset = buf.position();
/**
*Record offset destination
*/
this.setOffset(this.baseObject.getOffset() + this.offset.get().intValue());
/**
*Fly to offset destination
*/
buf.position(this.getOffset());
/**
*decompile
*/
this.reference.decompile(buf);
/**
*Undo the offset
*/
buf.position(old_offset);
}
}
It's easy (^ ω ^).
@PROPERTY(order = 1)
public USHORT gesoOffset = new USHORT();
// ...
//Write it in the back
@PROPERTY(order = 23)
public OFFSETW<Geso> geso= new OFFSETW<>(
this.gesoOffset,
this,
new Geso()
);
By using it together with LIST, you can read that Offset is an array.
@PROPERTY(order = 1)
public USHORT hogeCount = new USHORT()
@PROPERTY(order = 2)
public LIST<HogeRecord> hogeRecords= new LIST<>(
(i) -> new HogeRecord(this),
(i,len) -> this.hogeCount.get() > i
);
@PROPERTY(order = 3)
public LIST<OFFSETW<HogeHoge>> scripts = new LIST<>(
(i) -> new OFFSETW<>(
this.hogeRecords.get().get(i).offset,
this,
new HogeHoge()
),
(i,len) -> this.hogeCount.get() > i
);
There is also a reading at such times.
public class R<Z extends DataTypeInterface> extends CompositeDataType {
/**
*Define decompilation
* buf → (new,decompile) → instance
*/
public Function<ByteBuffer, Z> decompiler;
/**
*Define compilation
* buf,Z → (compile) →
*/
public BiConsumer<ByteBuffer, Z> compiler;
/**
* factory
* → (new) → instance
*/
protected Supplier<Z> factory;
/**
*Effectiveness|Invalid judgment
*/
public Supplier<Boolean> enable;
/**
*Content
*/
public Z content;
/**
* constructor
* <p>
*enable to enable or disable this object
*Please pass the lambda expression.
*
* @param compiler Function executed at compile time
* @Function executed at the time of param decompiler decomile
* @param enable True:Effectiveness,False:Invalid
*/
public R(
Function<ByteBuffer, Z> decompiler,
BiConsumer<ByteBuffer, Z> compiler,
Supplier<Boolean> enable) {
this.decompiler = decompiler;
this.compiler = compiler;
this.enable = enable;
}
/**
* constructor
* <p>
*enable to enable or disable this object
*Please pass the lambda expression.
*
* @param factory instance creation//ToDO:I want to change it so that it can be generated from Generics ...
* @param enable True:Effectiveness,False:Invalid
*/
public R(
Supplier<Z> factory,
Supplier<Boolean> enable) {
this.factory= factory;
this.enable = enable;
}
/**
* getter
*
* @return content
*/
public Z get() {
if(this.content == null){
this.content = this.factory.get();
}
return this.content;
}
@Override
public void decompile(ByteBuffer buf) {
this.setOffset(buf.position());
if (this.enable.get()) {
if(this.decompiler != null ){
this.content = this.decompiler.apply(buf);
}else{
this.content = this.factory.get();
this.content.decompile(buf);
}
}
}
@Override
public void compile(ByteBuffer buf) {
this.setOffset(buf.position());
if (this.enable.get()) {
if(this.compiler != null ){
this.compiler.accept(buf,this.content);
}else{
this.content.compile(buf);
}
}
}
@Override
public Object exports() {
if (this.enable.get()) {
return this.content.exports();
} else {
return null;
}
}
}
It can be used when the data structure differs depending on the format number.
@PROPERTY(order = 1)
public USHORT formatNum = new USHORT();
@PROPERTY(order = 2)
public R<GesoGesoFormat1> gesogesoFormat1= new R<>(
()-> new GesoGesoFormat1(this),
()-> this.formatNum.get() == 1
);
@PROPERTY(order = 2)
public R<GesoGesoFormat2> gesogesoFormat2= new R<>(
()-> new GesoGesoFormat2(this),
()-> this.formatNum.get() == 2
);
By combining LIST, R, and OFFSET, even complex data structures can be written structurally.
end
Recommended Posts