Git Product home page Git Product logo

binlib's Introduction

BinLib

BinLib is a Kotlin library designed for flexible manipulation of binary data. It provides a robust set of tools for reading, writing, and managing structured binary data, making it ideal for applications involving binary file parsing, network protocol implementation, and data serialization/deserialization with precise control over the binary format.

Features

  • Type-Safe Binary Structures: Define complex data structures with various primitive and custom types.
  • Bit Field Manipulation: Precise control over individual bits or groups of bits within bytes.
  • Dynamic and Exact String Handling: Support for strings with variable or fixed lengths.
  • Nested Structures and Arrays: Facilitate complex data representations with nested structures and arrays of types.
  • Endianess Support: Compatible with both Big Endian and Little Endian byte orders.
  • Type Decorators: Customize serialization/deserialization logic for advanced type conversions.

Not a Feature

  • Object Mapping

Kotlin example

Define custom structures.

    // sub structure
    val int3Struct: Structure = struct(
        "i32" to Int32,
        "i16" to Int16,
        "i8" to Int8,
    )
    
    // parent structure
    val parentStruct = struct(
        "i8" to Int8,
        "struct" to int3Struct,
    )

Write data to the buffer.

    // allocate 4 +2 +1 +1 = 8 byte
    val buffer = ByteBuffer.allocate(8)
    // write data
    parentStruct.write(buffer, mapOf(
        "struct" to mapOf<String, Any>(
            "i32" to 0x0FFFFFF1,
            "i16" to 0x0FF1,
            "i8" to 0x01
        ),
        "i8" to 0x10))

Read data from the buffer.

    // prepare buffer for reading
    buffer.flip()
    // read data and flatten the map
    val map = parentStruct.read(buffer).flatten(".")
    // asserts
    assertEquals(0x0FFFFFF1, map["struct.i32"])
    assertEquals(0x0FF1, map["struct.i16"])
    assertEquals(0x01, map["struct.i8"])
    assertEquals(0x10, map["i8"])

binlib's People

Contributors

mknblch avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.