Git Product home page Git Product logo

binarykit's Introduction

BinaryKit

BinaryKit helps you to break down binary data into bits and bytes, easily access specific parts and write data to binary.

Access Bytes

By using any read* method (readByte(), readBytes(quantity:), readBit(), โ€ฆ), BinaryKit will increment an internal cursor (or reading offset) to the end of the requested bit or byte, so the next read* method can continue from there.

Any get* method (getByte(index:), getBytes(range:), getBit(index:), โ€ฆ) will give access to binary data at any given location โ€” without incrementing the internal cursor.

Here are the methods you can call:

var binary = Binary(bytes: [0xDE, 0xAD, 0xBE, 0xEF, โ€ฆ])

// Reads exactly 1 byte and
// increments the cursor by 1 byte 
try binary.readByte()

// Reads the next 4 bytes and
// increments the cursor by 4 bytes
try binary.readBytes(4)

// Reads the next 1 bit and
// increments the cursor by 1 bit
try binary.readBit()

// Reads the next 4 bits and
// increments the cursor by 4 bits
try binary.readBits(4)

Example

var binary = Binary(bytes: [0b1_1_0_1_1_1_0_0])
//                            | | | | | | | | 
//                            | | | | | | | try binary.readBit()  // 0
//                            | | | | | | try binary.readBit()    // 0
//                            | | | | | try binary.readBit()      // 1
//                            | | | | try binary.readBit()        // 1
//                            | | | try binary.readBit()          // 1
//                            | | try binary.readBit()            // 0
//                            | try binary.readBit()              // 1
//                            try binary.readBit()                // 1

This shows how easy it is, to break down an IPv4 header.

var binary = Binary(bytes: [0x1B, 0x44, โ€ฆ])
let version                         = try binary.readBits(4)
let internetHeaderLength            = try binary.readBits(4)
let differentiatedServicesCodePoint = try binary.readBits(6)
let explicitCongestionNotification  = try binary.readBits(2)
let totalLength                     = try binary.readBytes(2)
let identification                  = try binary.readBytes(2)
let flags                           = try binary.readBits(4)
let fragmentOffset                  = try binary.readBits(12)
let timeToLive                      = try binary.readByte()
let protocolNumber                  = try binary.readByte()
let headerChecksum                  = try binary.readBytes(2)
let sourceIpAddress                 = try binary.readBytes(4)
let destinationIpAddress            = try binary.readBytes(4)
...

Store Bytes

Use the write* methods to store different types to binary.

var binary = Binary()
binary.writeInt32(1_350_849_546)
binary.writeString("Hello World!")
binary.writeBytes([0xFF, 0xCC, 0x00, 0x01])
binary.writeBool(true)

Contact

Other Projects

  • Clippy โ€” Clippy from Microsoft Office is back and runs on macOS! Written in Swift.
  • GrammaticalNumber โ€” Turns singular words to the plural and vice-versa in Swift.
  • HackMan โ€” Stop writing boilerplate code yourself. Let hackman do it for you via the command line.
  • ISO8859 โ€” Convert ISO8859 1-16 Encoded Text to String in Swift. Supports iOS, tvOS, watchOS and macOS.
  • SpriteMap โ€” SpriteMap helps you to extract sprites out of a sprite map. Written in Swift.
  • StringCase โ€” Converts String to lowerCamelCase, UpperCamelCase and snake_case. Tested and written in Swift.
  • TinyConsole โ€” TinyConsole is a micro-console that can help you log and display information inside an iOS application, where having a connection to a development computer is not possible.

License

BinaryKit is released under the MIT License.

binarykit's People

Contributors

aserdobintsev avatar cosmo avatar wacumov 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.