Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Working With Binary Data in Swift

JP Simard
October 29, 2015

Working With Binary Data in Swift

Presented at Swift Summit SF 2015.

Source available here: https://github.com/jpsim/talks

JP Simard

October 29, 2015
Tweet

More Decks by JP Simard

Other Decks in Programming

Transcript

  1. func encode<T>(var value: T) -> NSData { return withUnsafePointer(&value) {

    p in NSData(bytes: p, length: sizeofValue(value)) } } func decode<T>(data: NSData) -> T { let pointer = UnsafeMutablePointer<T>.alloc(sizeof(T)) data.getBytes(pointer, length: sizeof(T)) return pointer.move() }
  2. enum Either<T> { case Left(T) case Right(T) } let value

    = Either.Left("Swift Summit") let data = encode(value) data // => <NSData> let decoded: Either<String> = decode(data) decoded // => Either.Left("Swift Summit")
  3. Result 00 00 00 00 00 00 00 00 30

    00 00 00 00 00 00 00 40 4c 81 01 01 00 00 00 00 00 00 00 0c 00 00 00 78 4c 81 01 01 00 00 00 07 00 00 00 14 00 00 00 b0 4c 81 01 01 00 00 00 12 00 00 00 1e 00 00 00 00
  4. Result 00 00 00 00 00 00 00 00 30

    00 00 00 00 00 00 00 --------16 bytes------- --------16 bytes------- 40 4c 81 01 01 00 00 00 00 00 00 00 0c 00 00 00 --------16 bytes------- --------16 bytes------- 78 4c 81 01 01 00 00 00 07 00 00 00 14 00 00 00 --------16 bytes------- --------16 bytes------- b0 4c 81 01 01 00 00 00 12 00 00 00 1e 00 00 00 --------16 bytes------- --------16 bytes------- 00
  5. !

  6. tokens = 16.stride(through: numberOfTokens * 16, by: 16).map { parserOffset

    in var uid = UInt64(0), offset = 0, length = 0 data.getBytes(&uid, range: NSRange(location: parserOffset, length: 8)) data.getBytes(&offset, range: NSRange(location: 8 + parserOffset, length: 4)) data.getBytes(&length, range: NSRange(location: 12 + parserOffset, length: 4)) }
  7. tokens = 16.stride(through: numberOfTokens * 16, by: 16).map { parserOffset

    in var uid = UInt64(0), offset = 0, length = 0 data.getBytes(&uid, range: NSRange(location: parserOffset, length: 8)) data.getBytes(&offset, range: NSRange(location: 8 + parserOffset, length: 4)) data.getBytes(&length, range: NSRange(location: 12 + parserOffset, length: 4)) return SyntaxToken( type: stringForSourceKitUID(uid) ?? "unknown", offset: offset, length: length >> 1 ) }
  8. Collection of Bytes → Making our own → Conforming to

    ExtensibleCollectionType → What Index type should we use? Int?
  9. Links → SourceKittenFramework SyntaxMap → Convert structs and enums to

    NSData → robnapier.net/nsdata → Simon Lewis on parsing OLE/COM → github.com/realm/jazzy → realm.io
  10. try! ask(...) struct Question { let value: String let canJPAnswer:

    Bool } func ask<S: SequenceType where S.Generator.Element == Question>(questions: S) throws { // excercise for attendees }