Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gpongelli/gpsafeswiftpointer
A framework containing a safe wrapper for UnsafeMutablePointer with integer to byte array generics.
https://github.com/gpongelli/gpsafeswiftpointer
Last synced: 3 months ago
JSON representation
A framework containing a safe wrapper for UnsafeMutablePointer with integer to byte array generics.
- Host: GitHub
- URL: https://github.com/gpongelli/gpsafeswiftpointer
- Owner: gpongelli
- Created: 2015-07-28T07:18:49.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2019-01-02T16:34:44.000Z (about 6 years ago)
- Last Synced: 2024-10-09T22:31:48.988Z (3 months ago)
- Language: Swift
- Size: 86.9 KB
- Stars: 11
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# GPSafeSwiftPointer
[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)
[![Build Status](https://travis-ci.com/gpongelli/GPSafeSwiftPointer.svg?branch=master)](https://travis-ci.com/gpongelli/GPSafeSwiftPointer)
[![Tag](https://img.shields.io/github/tag/gpongelli/GPSafeSwiftPointer.svg)](https://github.com/gpongelli/GPSafeSwiftPointer/releases)
[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/gpongelli/GPSafeSwiftPointer/blob/master/LICENSE.txt)
![Swift](https://img.shields.io/badge/Swift-2.1%20|%203.0%20|%204.2-green.svg)A framework containing a safe wrapper for UnsafeMutablePointer with integer to byte array generics.
## Requirements
### Swift4.2 branch
- Xcode 10
- Swift 4.2
### Swift3 branch
- Xcode 8
- Swift 3
### Swift2.1 branch
- Xcode 7
- Swift 2## Motivation
Why I made this framework? Because I found UnsafeMutablePointer very **unsafe** .
[UnsafeMutablePointer Reference](https://developer.apple.com/library/prerelease/mac/documentation/Swift/Reference/Swift_UnsafeMutablePointer_Structure/index.html#//apple_ref/swift/structcm/UnsafeMutablePointer/s:ZFVSs20UnsafeMutablePointer5allocurFMGS_q__FSiGS_q__) for alloc method defines
> Allocate memory for num objects of type Memory.So it's expected to
```swift
let test = UnsafeMutablePointer.alloc(1)
test[0] = 5
print(test[0]) // correctly prints 5
```but it's not expected to have these result IMHO
```swift
print(test[1]) // why prints 0 ?!
print(test[2]) // why again prints 0 ?!
...
print(test[7]) // why prints 127 ?!
```
Playground are you serious :flushed: :interrobang: :interrobang:This is one of the reason I created this class, having these results
```swift
let test = GPSafeSwiftPointer() // pointer to one UInt8
test[0] = 5
print(test[0]) // correctly prints Optional(5)
print(test[1]) // correctly returns nil
...
test[2] = 4 // this instruction returns without setting any value
print(test[2]) // correctly returns nil as before
```Another reason that let me create this framework was about memory management, because after `alloc()` and `initialize()` it's developer's responsibility to call `dealloc()` and `destroy()`; this class automatically call these methods on deinit.
## Installation
### Carthage
Insert `github "gpongelli/GPSafeSwiftPointer" ~> 0.3` in your Cartfile## Usage
Some simple usage are reported here, some usage are already showed in motivation paragraph, for other example see iOS tests file.### Initialization
GPSafeSwiftPointer variables can be initializated as follows1. let test = GPSafeSwiftPointer<_type_>() // alloc one element of type _type_
2. let test = GPSafeSwiftPointer<_type_>(allocatedMemory: _k_) // alloc _k_ elements of type _type_
3. let test = GPSafeSwiftPointer<_type_>(initializeWithValue: _n_) // alloc one element of type _type_ initialized with value _n_### Underlying UnsafeMutablePointer
Having a
```
let test = GPSafeSwiftPointer()
test.ump
```
returns the underlying UnsafeMutablePointer to be used when calling Apple methods as I do for `SecRandomCopyBytes` .### Byte Array Protocol for Integers
Test file contains some tests about all integer types conversion to and from byte array, having Big Endian coding.So for example, an UInt16 equal to _24288_ will be converted to an array of two UInt8 containing __[94, 224]__ having these representation:
Representation | MSB | LSB
:--------------|:--------:|:--------:
integer | 94 | 224
binary | 01011110 | 11100000
hexadecimal | 0x5E | 0xE0Converting back the hexadecimal or binary representation will lead to the original value.
### Byte Array Protocol for UnsafeMutablePointer and GPSafeSwiftPointer
Looking at test file you'll find two test about UnsafeMutablePointer and GPSafeSwiftPointer using Byte Array protocol.The called method and the results are the same as for simple integer variables, but in this case the pointed integer is used without any hassle.
### UnsafeBitCast
Using GPSafeSwiftPointer and Byte Array protocol to return the contained value as byte array, it can be avoided to use unsafeBitCast to have the pointed value casted as another type as erroneously wrote in some blog post around internet, :arrow_right: Swift prevents to do this :arrow_left: .Looking at the relative test, it's clear that an UnsafeMutablePointer of any type casted to UInt (32 bit) does not return the pointed value; inspecting the results with playground it's easy to see that the returned value is the **address** that UnsafeMutablePointer contains, not it's **pointed value** .
The only unsafeBitCast that works is to the same type of original pointed type, as showed inside the test.
A typical example I use for GPSafeSwiftPointer instead of unsafeBitCast is when generating random bytes with `SecRandomCopyBytes` .
## Author
Gabriele Pongelli## Contribution
Thanks to @krzyzanowskim to his [gist](https://gist.github.com/krzyzanowskim/c84d039d1542c1a82731), I've included his code inside this project adding some changes I commented to his gist simply because I prefer Big Endian to Little Endian representation.### License
GPSafeSwiftPointer is available under the MIT license. See the LICENSE file for more info.