Coder Social home page Coder Social logo

binutils's Introduction

BinUtils

Swift functions to ease working with binary formats

hexlify(data)

let data = "Hello".dataUsingEncoding(.utf8)!
assert(hexlify(data) == "48656C6C6F")

unhexlify(hexstr)

let s = String(data: unhexlify("48656C6C6F")!, encoding: .utf8)
assert(s == "Hello")

pack(fmt, [v1, v2, ...])

let d = pack("<h2I3sf", [1, 2, 3, "asd", 0.5])
assert(d == unhexlify("0100 02000000 03000000 617364 0000003f"))

unpack(fmt, string)

let a = unpack(">hBsf", unhexlify("0500 01 41 3fc00000")!)
assert(a[0] as? Int == 1280)
assert(a[1] as? Int == 1)
assert(a[2] as? String == "A")
assert(a[3] as? Double == 1.5)
let f = FileHandle(forReadingAtPath: "/bin/ls")!
let b = unpack("<2H", f.readDataOfLength(4))
f.closeFile()
assert(b[0] as? Int == 64207)
assert(b[1] as? Int == 65261)

caveats

pack() and unpack() should behave as Python's struct module https://docs.python.org/2/library/struct.html BUT:

  • native size and alignment @ is not supported
  • as a consequence, the byte order specifier character is mandatory and must be among =<>!
  • native byte order = assumes a little-endian system (eg. Intel x86)
  • Pascal strings p and native pointers P are not supported

Byte Order Format

Character Byte Order
= little-endian
< little-endian
> big-endian
! network (big-endian)

Format Characters

Format C Type Swift Type Size
x pad byte - 1
c char String of length 1 1
b signed char Int8 1
B unsigned char UInt8 1
? _Bool Bool 1
h short Int16 2
H unsigned short UInt16 2
i int Int32 4
I unsigned int UInt32 4
l long Int32 4
L unsigned long UInt32 4
q long long Int64 8
Q unsigned long long Int64 8
f float Float32 4
d double Float64 8
s char[] String

binutils's People

Contributors

aidansteele avatar nst avatar swissquote-nst avatar

Watchers

 avatar  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.