{"id":17212412,"url":"https://github.com/gpongelli/gpsafeswiftpointer","last_synced_at":"2025-04-13T22:56:10.366Z","repository":{"id":35547563,"uuid":"39819070","full_name":"gpongelli/GPSafeSwiftPointer","owner":"gpongelli","description":"A framework containing a safe wrapper for UnsafeMutablePointer with integer to byte array generics.","archived":false,"fork":false,"pushed_at":"2019-01-02T16:34:44.000Z","size":89,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-12T00:46:17.137Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/gpongelli.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-07-28T07:18:49.000Z","updated_at":"2019-08-22T05:35:36.000Z","dependencies_parsed_at":"2022-09-14T04:22:15.830Z","dependency_job_id":null,"html_url":"https://github.com/gpongelli/GPSafeSwiftPointer","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpongelli%2FGPSafeSwiftPointer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpongelli%2FGPSafeSwiftPointer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpongelli%2FGPSafeSwiftPointer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gpongelli%2FGPSafeSwiftPointer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gpongelli","download_url":"https://codeload.github.com/gpongelli/GPSafeSwiftPointer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248794569,"owners_count":21162614,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-15T02:59:58.453Z","updated_at":"2025-04-13T22:56:10.345Z","avatar_url":"https://github.com/gpongelli.png","language":"Swift","readme":"# GPSafeSwiftPointer\n[![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/Carthage/Carthage)\n[![Build Status](https://travis-ci.com/gpongelli/GPSafeSwiftPointer.svg?branch=master)](https://travis-ci.com/gpongelli/GPSafeSwiftPointer)\n[![Tag](https://img.shields.io/github/tag/gpongelli/GPSafeSwiftPointer.svg)](https://github.com/gpongelli/GPSafeSwiftPointer/releases)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/gpongelli/GPSafeSwiftPointer/blob/master/LICENSE.txt)\n![Swift](https://img.shields.io/badge/Swift-2.1%20|%203.0%20|%204.2-green.svg)\n\nA framework containing a safe wrapper for UnsafeMutablePointer with integer to byte array generics.\n\n## Requirements\n### Swift4.2 branch\n- Xcode 10\n- Swift 4.2\n### Swift3 branch\n- Xcode 8\n- Swift 3\n### Swift2.1 branch\n- Xcode 7\n- Swift 2\n\n## Motivation\nWhy I made this framework? Because I found UnsafeMutablePointer very **unsafe** .\n[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\n\u003e Allocate memory for num objects of type Memory.\n\nSo it's expected to \n```swift\nlet test = UnsafeMutablePointer\u003cUInt8\u003e.alloc(1)\ntest[0] = 5\nprint(test[0])  // correctly prints 5\n```\n\nbut it's not expected to have these result IMHO\n```swift\nprint(test[1])  // why prints 0 ?!\nprint(test[2])  // why again prints 0 ?!\n...\nprint(test[7])  // why prints 127 ?!\n```\nPlayground are you serious :flushed: :interrobang: :interrobang:\n\nThis is one of the reason I created this class, having these results\n```swift\nlet test = GPSafeSwiftPointer\u003cUInt8\u003e()  // pointer to one UInt8\ntest[0] = 5\nprint(test[0])  // correctly prints Optional(5)\nprint(test[1])  // correctly returns nil \n...\ntest[2] = 4  // this instruction returns without setting any value\nprint(test[2])  // correctly returns nil as before\n```\n\nAnother 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.\n\n\n## Installation\n### Carthage\nInsert `github \"gpongelli/GPSafeSwiftPointer\" ~\u003e 0.3` in your Cartfile\n\n\n## Usage\nSome simple usage are reported here, some usage are already showed in motivation paragraph, for other example see iOS tests file.\n\n\n### Initialization\nGPSafeSwiftPointer variables can be initializated as follows\n\n1. let test = GPSafeSwiftPointer\u003c_type_\u003e()  // alloc one element of type _type_\n2. let test = GPSafeSwiftPointer\u003c_type_\u003e(allocatedMemory: _k_)  // alloc _k_ elements of type _type_\n3. let test = GPSafeSwiftPointer\u003c_type_\u003e(initializeWithValue: _n_)  // alloc one element of type _type_ initialized with value _n_\n\n\n### Underlying UnsafeMutablePointer\nHaving a \n```\nlet test = GPSafeSwiftPointer\u003cTYPE\u003e() \ntest.ump \n```\nreturns the underlying UnsafeMutablePointer\u003cTYPE\u003e to be used when calling Apple methods as I do for `SecRandomCopyBytes` .\n\n\n### Byte Array Protocol for Integers\nTest file contains some tests about all integer types conversion to and from byte array, having Big Endian coding.\n\nSo for example, an UInt16 equal to _24288_ will be converted to an array of two UInt8 containing __[94, 224]__ having these representation:\n\nRepresentation |  MSB     |   LSB\n:--------------|:--------:|:--------:\ninteger        |    94    |   224\nbinary         | 01011110 | 11100000\nhexadecimal    |   0x5E   |   0xE0\n\nConverting back the hexadecimal or binary representation will lead to the original value.\n\n\n### Byte Array Protocol for UnsafeMutablePointer and GPSafeSwiftPointer\nLooking at test file you'll find two test about UnsafeMutablePointer and GPSafeSwiftPointer using Byte Array protocol.\n\nThe 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.\n\n\n### UnsafeBitCast \nUsing 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: .\n\nLooking 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** .\n\nThe only unsafeBitCast that works is to the same type of original pointed type, as showed inside the test.\n\nA typical example I use for GPSafeSwiftPointer instead of unsafeBitCast is when generating random bytes with `SecRandomCopyBytes` .\n\n\n## Author\nGabriele Pongelli\n\n\n## Contribution\nThanks 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.\n\n\n### License\nGPSafeSwiftPointer is available under the MIT license. See the LICENSE file for more info.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpongelli%2Fgpsafeswiftpointer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgpongelli%2Fgpsafeswiftpointer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgpongelli%2Fgpsafeswiftpointer/lists"}