Android nanoapp header: Java parsing library

File extension

napp_header

KS implementation details

License: Apache-2.0
Minimal Kaitai Struct required: 0.9

This page hosts a formal specification of Android nanoapp header using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for Java generated by Kaitai Struct depends on the Java runtime library. You have to install it before you can parse data.

The Java runtime library is published in the Maven Central Repository. Refer to the artifact page for instructions how to add it into your project with the build tool that you use.

Code

Parse a local file and get structure in memory:

AndroidNanoappHeader data = AndroidNanoappHeader.fromFile("path/to/local/file.napp_header");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
AndroidNanoappHeader data = new AndroidNanoappHeader(new ByteBufferKaitaiStream(someArray));

After that, one can get various attributes from the structure by invoking getter methods like:

data.headerVersion() // => get header version

Java source code to parse Android nanoapp header

AndroidNanoappHeader.java

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;
import java.util.Arrays;


/**
 * @see <a href="https://android.googlesource.com/platform/system/chre/+/a7ff61b9/build/build_template.mk#130">Source</a>
 */
public class AndroidNanoappHeader extends KaitaiStruct {
    public static AndroidNanoappHeader fromFile(String fileName) throws IOException {
        return new AndroidNanoappHeader(new ByteBufferKaitaiStream(fileName));
    }

    public AndroidNanoappHeader(KaitaiStream _io) {
        this(_io, null, null);
    }

    public AndroidNanoappHeader(KaitaiStream _io, KaitaiStruct _parent) {
        this(_io, _parent, null);
    }

    public AndroidNanoappHeader(KaitaiStream _io, KaitaiStruct _parent, AndroidNanoappHeader _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        this.headerVersion = this._io.readU4le();
        if (!(headerVersion() == 1)) {
            throw new KaitaiStream.ValidationNotEqualError(1, headerVersion(), _io(), "/seq/0");
        }
        this.magic = this._io.readBytes(4);
        if (!(Arrays.equals(magic(), new byte[] { 78, 65, 78, 79 }))) {
            throw new KaitaiStream.ValidationNotEqualError(new byte[] { 78, 65, 78, 79 }, magic(), _io(), "/seq/1");
        }
        this.appId = this._io.readU8le();
        this.appVersion = this._io.readU4le();
        this.flags = this._io.readU4le();
        this.hubType = this._io.readU8le();
        this.chreApiMajorVersion = this._io.readU1();
        this.chreApiMinorVersion = this._io.readU1();
        this.reserved = this._io.readBytes(6);
        if (!(Arrays.equals(reserved(), new byte[] { 0, 0, 0, 0, 0, 0 }))) {
            throw new KaitaiStream.ValidationNotEqualError(new byte[] { 0, 0, 0, 0, 0, 0 }, reserved(), _io(), "/seq/8");
        }
    }
    private Boolean isSigned;
    public Boolean isSigned() {
        if (this.isSigned != null)
            return this.isSigned;
        boolean _tmp = (boolean) ((flags() & 1) != 0);
        this.isSigned = _tmp;
        return this.isSigned;
    }
    private Boolean isEncrypted;
    public Boolean isEncrypted() {
        if (this.isEncrypted != null)
            return this.isEncrypted;
        boolean _tmp = (boolean) ((flags() & 2) != 0);
        this.isEncrypted = _tmp;
        return this.isEncrypted;
    }
    private Boolean isTcmCapable;
    public Boolean isTcmCapable() {
        if (this.isTcmCapable != null)
            return this.isTcmCapable;
        boolean _tmp = (boolean) ((flags() & 4) != 0);
        this.isTcmCapable = _tmp;
        return this.isTcmCapable;
    }
    private long headerVersion;
    private byte[] magic;
    private long appId;
    private long appVersion;
    private long flags;
    private long hubType;
    private int chreApiMajorVersion;
    private int chreApiMinorVersion;
    private byte[] reserved;
    private AndroidNanoappHeader _root;
    private KaitaiStruct _parent;
    public long headerVersion() { return headerVersion; }
    public byte[] magic() { return magic; }
    public long appId() { return appId; }
    public long appVersion() { return appVersion; }
    public long flags() { return flags; }
    public long hubType() { return hubType; }
    public int chreApiMajorVersion() { return chreApiMajorVersion; }
    public int chreApiMinorVersion() { return chreApiMinorVersion; }
    public byte[] reserved() { return reserved; }
    public AndroidNanoappHeader _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}