tunnelkit/Sources/_TunnelKitUtils/Data+Manipulation.swift

234 lines
7.7 KiB
Swift
Raw Normal View History

2018-08-23 08:19:25 +00:00
//
// Data+Manipulation.swift
// TunnelKit
2018-08-23 08:19:25 +00:00
//
// Created by Davide De Rosa on 2/3/17.
2020-12-27 16:29:39 +00:00
// Copyright (c) 2021 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of TunnelKit.
//
// TunnelKit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TunnelKit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright (c) 2018-Present Private Internet Access
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2018-08-23 08:19:25 +00:00
//
import Foundation
// hex -> Data conversion code from: http://stackoverflow.com/questions/32231926/nsdata-from-hex-string
// Data -> hex conversion code from: http://stackoverflow.com/questions/39075043/how-to-convert-data-to-hex-string-in-swift
2021-10-25 14:27:27 +00:00
/// :nodoc:
2018-08-23 08:19:25 +00:00
extension UnicodeScalar {
2021-10-25 14:27:27 +00:00
public var hexNibble: UInt8 {
2018-08-23 08:19:25 +00:00
let value = self.value
if 48 <= value && value <= 57 {
return UInt8(value - 48)
}
else if 65 <= value && value <= 70 {
return UInt8(value - 55)
}
else if 97 <= value && value <= 102 {
return UInt8(value - 87)
}
fatalError("\(self) not a legal hex nibble")
}
}
2021-10-25 14:27:27 +00:00
/// :nodoc:
2018-08-23 08:19:25 +00:00
extension Data {
2021-10-25 14:27:27 +00:00
public init(hex: String) {
2018-08-23 08:19:25 +00:00
let scalars = hex.unicodeScalars
var bytes = Array<UInt8>(repeating: 0, count: (scalars.count + 1) >> 1)
for (index, scalar) in scalars.enumerated() {
var nibble = scalar.hexNibble
if index & 1 == 0 {
nibble <<= 4
}
bytes[index >> 1] |= nibble
}
2019-03-30 19:25:48 +00:00
self = Data(bytes)
2018-08-23 08:19:25 +00:00
}
2021-10-25 14:27:27 +00:00
public func toHex() -> String {
2018-08-23 08:19:25 +00:00
return map { String(format: "%02hhx", $0) }.joined()
}
2021-10-25 14:27:27 +00:00
public mutating func zero() {
2018-08-23 08:19:25 +00:00
resetBytes(in: 0..<count)
}
2021-10-25 14:27:27 +00:00
public mutating func zero(from: Int, to: Int) {
2018-08-23 08:19:25 +00:00
resetBytes(in: from..<to)
}
}
2021-10-25 14:27:27 +00:00
/// :nodoc:
2018-08-23 08:19:25 +00:00
extension Data {
2021-10-25 14:27:27 +00:00
public mutating func append(_ value: UInt16) {
2018-08-23 08:19:25 +00:00
var localValue = value
2020-04-05 15:08:23 +00:00
let buffer = withUnsafePointer(to: &localValue) {
return UnsafeBufferPointer(start: $0, count: 1)
}
2018-08-23 08:19:25 +00:00
append(buffer)
}
2021-10-25 14:27:27 +00:00
public mutating func append(_ value: UInt32) {
2018-08-23 08:19:25 +00:00
var localValue = value
2020-04-05 15:08:23 +00:00
let buffer = withUnsafePointer(to: &localValue) {
return UnsafeBufferPointer(start: $0, count: 1)
}
2018-08-23 08:19:25 +00:00
append(buffer)
}
2021-10-25 14:27:27 +00:00
public mutating func append(_ value: UInt64) {
2018-08-23 08:19:25 +00:00
var localValue = value
2020-04-05 15:08:23 +00:00
let buffer = withUnsafePointer(to: &localValue) {
return UnsafeBufferPointer(start: $0, count: 1)
}
2018-08-23 08:19:25 +00:00
append(buffer)
}
2021-10-25 14:27:27 +00:00
public mutating func append(nullTerminatedString: String) {
2018-08-23 08:19:25 +00:00
append(nullTerminatedString.data(using: .ascii)!)
append(UInt8(0))
}
2021-10-25 14:27:27 +00:00
public func nullTerminatedString(from: Int) -> String? {
2018-08-23 08:19:25 +00:00
var nullOffset: Int?
for i in from..<count {
if (self[i] == 0) {
nullOffset = i
break
}
}
guard let to = nullOffset else {
return nil
}
return String(data: subdata(in: from..<to), encoding: .ascii)
}
// best
2021-10-25 14:27:27 +00:00
public func UInt16Value(from: Int) -> UInt16 {
2018-08-23 08:19:25 +00:00
var value: UInt16 = 0
for i in 0..<2 {
let byte = self[from + i]
// print("byte: \(String(format: "%x", byte))")
value |= (UInt16(byte) << UInt16(8 * i))
}
// print("value: \(String(format: "%x", value))")
return value
}
@available(*, deprecated)
func UInt16ValueFromPointers(from: Int) -> UInt16 {
return subdata(in: from..<(from + 2)).withUnsafeBytes { $0.pointee }
}
@available(*, deprecated)
func UInt16ValueFromReboundPointers(from: Int) -> UInt16 {
let data = subdata(in: from..<(from + 2))
// print("data: \(data.toHex())")
let value = data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> UInt16 in
bytes.withMemoryRebound(to: UInt16.self, capacity: 1) {
$0.pointee
}
}
// print("value: \(String(format: "%x", value))")
return value
}
@available(*, deprecated)
func UInt32ValueFromBuffer(from: Int) -> UInt32 {
var value: UInt32 = 0
for i in 0..<4 {
let byte = self[from + i]
// print("byte: \(String(format: "%x", byte))")
value |= (UInt32(byte) << UInt32(8 * i))
}
// print("value: \(String(format: "%x", value))")
return value
}
// best
2021-10-25 14:27:27 +00:00
public func UInt32Value(from: Int) -> UInt32 {
2019-03-30 19:30:29 +00:00
return subdata(in: from..<(from + 4)).withUnsafeBytes {
$0.load(as: UInt32.self)
}
2018-08-23 08:19:25 +00:00
}
@available(*, deprecated)
func UInt32ValueFromReboundPointers(from: Int) -> UInt32 {
let data = subdata(in: from..<(from + 4))
// print("data: \(data.toHex())")
let value = data.withUnsafeBytes { (bytes: UnsafePointer<UInt8>) -> UInt32 in
bytes.withMemoryRebound(to: UInt32.self, capacity: 1) {
$0.pointee
}
}
// print("value: \(String(format: "%x", value))")
return value
}
2021-10-25 14:27:27 +00:00
public func networkUInt16Value(from: Int) -> UInt16 {
2018-08-23 08:19:25 +00:00
return UInt16(bigEndian: subdata(in: from..<(from + 2)).withUnsafeBytes {
2019-03-30 19:30:29 +00:00
$0.load(as: UInt16.self)
2018-08-23 08:19:25 +00:00
})
}
2021-10-25 14:27:27 +00:00
public func networkUInt32Value(from: Int) -> UInt32 {
2018-08-23 08:19:25 +00:00
return UInt32(bigEndian: subdata(in: from..<(from + 4)).withUnsafeBytes {
2019-03-30 19:30:29 +00:00
$0.load(as: UInt32.self)
2018-08-23 08:19:25 +00:00
})
}
}
2021-10-25 14:27:27 +00:00
/// :nodoc:
2018-08-23 08:19:25 +00:00
extension Data {
2021-10-25 14:27:27 +00:00
public func subdata(offset: Int, count: Int) -> Data {
2018-08-23 08:19:25 +00:00
return subdata(in: offset..<(offset + count))
}
}
2021-10-25 14:27:27 +00:00
/// :nodoc:
2018-08-23 08:19:25 +00:00
extension Array where Element == Data {
2021-10-25 14:27:27 +00:00
public var flatCount: Int {
2019-06-05 12:14:15 +00:00
return reduce(0) { $0 + $1.count }
2018-08-23 08:19:25 +00:00
}
}
2019-03-30 19:30:29 +00:00
2021-10-25 14:27:27 +00:00
/// :nodoc:
2019-03-30 19:30:29 +00:00
extension UnsafeRawBufferPointer {
2021-10-25 14:27:27 +00:00
public var bytePointer: UnsafePointer<Element> {
2019-03-30 19:30:29 +00:00
return bindMemory(to: Element.self).baseAddress!
}
}
2021-10-25 14:27:27 +00:00
/// :nodoc:
2019-03-30 19:30:29 +00:00
extension UnsafeMutableRawBufferPointer {
2021-10-25 14:27:27 +00:00
public var bytePointer: UnsafeMutablePointer<Element> {
2019-03-30 19:30:29 +00:00
return bindMemory(to: Element.self).baseAddress!
}
}