diff --git a/EosioSwift/Extensions/DataExtensions.swift b/EosioSwift/Extensions/DataExtensions.swift index 5569a319..d77d1709 100644 --- a/EosioSwift/Extensions/DataExtensions.swift +++ b/EosioSwift/Extensions/DataExtensions.swift @@ -57,19 +57,38 @@ public extension Data { /// /// - Parameter hexString: A Base16 encoded string. init?(hexString: String) { + guard let hexData = hexString.data(using: .ascii) else { return nil } + let len = hexString.count / 2 - var data = Data(capacity: len) - for i in 0...allocate(capacity: len) + + for offset in stride(from: 0, to: hexString.count, by: 2) { + let bytes = Data(bytes: baseAddress+offset, count: 2) + + guard let string = String(data: bytes, encoding: .ascii) else { + dataPtr.deallocate() + return + } + + guard let num = UInt8(string, radix: 16) else { + dataPtr.deallocate() + return + } + dataPtr[dataPtrOffset] = num + dataPtrOffset += 1 } + + data = Data(bytesNoCopy: dataPtr, count: len, deallocator: .free) } - self = data + + guard let validData = data else { return nil } + self = validData } /// Returns the SHA256 hash of the data. diff --git a/EosioSwiftTests/DataExtensionsTests.swift b/EosioSwiftTests/DataExtensionsTests.swift index f8d4c577..28773063 100644 --- a/EosioSwiftTests/DataExtensionsTests.swift +++ b/EosioSwiftTests/DataExtensionsTests.swift @@ -37,6 +37,12 @@ class DataExtensionsTests: XCTestCase { XCTAssertNotNil(data) } + func test_initWithHexString_withLargeValidHexString_shouldSucceed() { + // swiftlint:disable:next line_length + let data = Data(hexString: "7b0d0a2020202020202020227472616e73616374696f6e5f6964223a202232646534636433383263326532333163386133616338306163666365613439336464326439653731373862343664313635323833636639316332636536313231222c0d0a20202020202020202270726f636573736564223a207b0d0a202020202020202020202020226964223a202232646534636433383263326532333163386133616338306163666365613439336464326439653731373862343664313635323833636639316332636536313231222c0d0a20202020202020202020202022626c6f636b5f6e756d223a2032313130323331382c0d0a20202020202020202020202022626c6f636b5f74696d65223a2022323031392d30312d32385431363a34363a34392e303030222c0d0a2020202020202020202020202270726f64756365725f626c6f636b5f6964223a206e756c6c2c0d0a2020202020202020202020202272656365697074223a207b0d0a2020202020202020202020202020202022737461747573223a20226578656375746564222c0d0a20202020202020202020202020202020226370755f75736167655f7573223a20323037322c0d0a20202020202020202020202020202020226e65745f75736167655f776f726473223a2033360d0a2020202020202020202020207d2c0d0a20202020202020202020202022656c6170736564223a20323037322c0d0a202020202020202020202020226e65745f7573616765223a203238382c0d0a202020202020202020202020227363686564756c6564223a2066616c73652c0d0a20202020202020202020202022616374696f6e5f747261636573223a205b0d0a202020202020202020202020202020207b0d0a20202020202020202020202020202020202020202272656365697074223a207b0d0a202020202020202020202020202020202020202020202020227265636569766572223a2022656f73696f2e617373657274222c0d0a202020202020202020202020202020202020202020202020226163745f646967657374223a202261346361656564643565353832346464393136633161616162633834663061313134646462646138333732386338633233626138353964346138613933373231222c0d0a20202020202020202020202020202020202020202020202022676c6f62616c5f73657175656e6365223a2032313130373632382c0d0a20202020202020202020202020202020202020202020202022726563765f73657175656e6365223a203333332c0d0a20202020202020202020202020202020202020202020202022617574685f73657175656e6365223a205b5d2c0d0a20202020202020202020202020202020202020202020202022636f64655f73657175656e6365223a20312c0d0a202020202020202020202020202020202020202020202020226162695f73657175656e6365223a20310d0a20202020202020202020202020202020202020207d2c0d0a202020202020202020202020202020202020202022616374223a207b0d0a202020202020202020202020202020202020202020202020226163636f756e74223a2022656f73696f2e617373657274222c0d0a202020202020202020202020202020202020202020202020226e616d65223a202272657175697265222c0d0a20202020202020202020202020202020202020202020202022617574686f72697a6174696f6e223a205b5d2c0d0a2020202020202020202020202020202020202020202020202264617461223a207b0d0a2020202020202020202020202020202020202020202020202020202022636861696e5f706172616d735f68617368223a202263626464393536663532616364393130633363393538313336643732663835363064313834366263376366333135376635666266623732643330303164653435222c0d0a20202020202020202020202020202020202020202020202020202020226d616e69666573745f6964223a202239376634613166646265636461366435396339366134333030396663356535643762386636333962313236396337376365633731383436306463633139636233222c0d0a2020202020202020202020202020202020202020202020202020202022616374696f6e73223a205b0d0a20202020202020202020202020202020202020202020202020202020202020207b0d0a20202020202020202020202020202020202020202020202020202020202020202020202022636f6e7472616374223a2022656f73696f2e746f6b656e222c0d0a20202020202020202020202020202020202020202020202020202020202020202020202022616374696f6e223a20227472616e73666572220d0a20202020202020202020202020202020202020202020202020202020202020207d0d0a202020202020202020202020202020202020202020202020202020205d2c0d0a20202020202020202020202020202020202020202020202020202020226162695f686173686573223a205b0d0a20202020202020202020202020202020202020202020202020202020202020202234333836346435616630666532393464343464313963363132303336636265386330393834313463346131326135613762623062666537646231353536323438220d0a202020202020202020202020202020202020202020202020202020205d0d0a2020202020202020202020202020202020202020202020207d2c0d0a202020202020202020202020202020202020202020202020226865785f64617461223a2022636264643935366635326163643931306333633935383133366437326638353630643138343662633763663331353766356662666237326433303031646534353937663461316664626563646136643539633936613433303039666335653564376238663633396231323639633737636563373138343630646363313963623330313030613638323334303365613330353530303030303035373264336363646364303134333836346435616630666532393464343464313963363132303336636265386330393834313463346131326135613762623062666537646231353536323438220d0a20202020202020202020202020202020202020207d2c0d0a202020202020202020202020202020202020202022636f6e746578745f66726565223a2066616c73652c0d0a202020202020202020202020202020202020202022656c6170736564223a20313138332c0d0a2020202020202020202020202020202020202020226370755f7573616765223a20302c0d0a202020202020202020202020202020202020202022636f6e736f6c65223a2022222c0d0a202020202020202020202020202020202020202022746f74616c5f6370755f7573616765223a20302c0d0a2020202020202020202020202020202020202020227472785f6964223a202232646534636433383263326532333163386133616338306163666365613439336464326439653731373862343664313635323833636639316332636536313231222c0d0a202020202020202020202020202020202020202022626c6f636b5f6e756d223a2032313130323331382c0d0a202020202020202020202020202020202020202022626c6f636b5f74696d65223a2022323031392d30312d32385431363a34363a34392e303030222c0d0a20202020202020202020202020202020202020202270726f64756365725f626c6f636b5f6964223a206e756c6c2c0d0a2020202020202020202020202020202020202020226163636f756e745f72616d5f64656c746173223a205b5d2c0d0a202020202020202020202020202020202020202022696e6c696e655f747261636573223a205b5d0d0a202020202020202020202020202020207d2c0d0a202020202020202020202020202020207b0d0a20202020202020202020202020202020202020202272656365697074223a207b0d0a202020202020202020202020202020202020202020202020227265636569766572223a2022656f73696f2e746f6b656e222c0d0a202020202020202020202020202020202020202020202020226163745f646967657374223a202239656162323339643636643133633334623963633335613666373966623266366436316132643964663961343834303735633832653635643733613063626338222c0d0a20202020202020202020202020202020202020202020202022676c6f62616c5f73657175656e6365223a2032313130373632392c0d0a20202020202020202020202020202020202020202020202022726563765f73657175656e6365223a20313336392c0d0a20202020202020202020202020202020202020202020202022617574685f73657175656e6365223a205b0d0a202020202020202020202020202020202020202020202020202020205b0d0a20202020202020202020202020202020202020202020202020202020202020202263727970746b6565706572222c0d0a20202020202020202020202020202020202020202020202020202020202020203837380d0a202020202020202020202020202020202020202020202020202020205d0d0a2020202020202020202020202020202020202020202020205d2c0d0a20202020202020202020202020202020202020202020202022636f64655f73657175656e6365223a20312c0d0a202020202020202020202020202020202020202020202020226162695f73657175656e6365223a20340d0a20202020202020202020202020202020202020207d2c0d0a202020202020202020202020202020202020202022616374223a207b0d0a202020202020202020202020202020202020202020202020226163636f756e74223a2022656f73696f2e746f6b656e222c0d0a202020202020202020202020202020202020202020202020226e616d65223a20227472616e73666572222c0d0a20202020202020202020202020202020202020202020202022617574686f72697a6174696f6e223a205b0d0a202020202020202020202020202020202020202020202020202020207b0d0a2020202020202020202020202020202020202020202020202020202020202020226163746f72223a202263727970746b6565706572222c0d0a2020202020202020202020202020202020202020202020202020202020202020227065726d697373696f6e223a2022616374697665220d0a202020202020202020202020202020202020202020202020202020207d0d0a2020202020202020202020202020202020202020202020205d2c0d0a2020202020202020202020202020202020202020202020202264617461223a207b0d0a202020202020202020202020202020202020202020202020202020202266726f6d223a202263727970746b6565706572222c0d0a2020202020202020202020202020202020202020202020202020202022746f223a20226272616e646f6e222c0d0a20202020202020202020202020202020202020202020202020202020227175616e74697479223a202234322e3030303020454f53222c0d0a20202020202020202020202020202020202020202020202020202020226d656d6f223a2022746865206772617373686f70706572206c696573206865617679220d0a2020202020202020202020202020202020202020202020207d2c0d0a202020202020202020202020202020202020202020202020226865785f64617461223a202230306165616134616331356366643435303030303030363064323334636433646130363830363030303030303030303030343435346635333030303030303030316137343638363532303637373236313733373336383666373037303635373232303663363936353733323036383635363137363739220d0a20202020202020202020202020202020202020207d2c0d0a202020202020202020202020202020202020202022636f6e746578745f66726565223a2066616c73652c0d0a202020202020202020202020202020202020202022656c6170736564223a203534382c0d0a2020202020202020202020202020202020202020226370755f7573616765223a20302c0d0a202020202020202020202020202020202020202022636f6e736f6c65223a2022222c0d0a202020202020202020202020202020202020202022746f74616c5f6370755f7573616765223a20302c0d0a2020202020202020202020202020202020202020227472785f6964223a202232646534636433383263326532333163386133616338306163666365613439336464326439653731373862343664313635323833636639316332636536313231222c0d0a202020202020202020202020202020202020202022626c6f636b5f6e756d223a2032313130323331382c0d0a202020202020202020202020202020202020202022626c6f636b5f74696d65223a2022323031392d30312d32385431363a34363a34392e303030222c0d0a20202020202020202020202020202020202020202270726f64756365725f626c6f636b5f6964223a206e756c6c2c0d0a2020202020202020202020202020202020202020226163636f756e745f72616d5f64656c746173223a205b5d2c0d0a202020202020202020202020202020202020202022696e6c696e655f747261636573223a205b0d0a2020202020202020202020202020202020202020202020207b0d0a202020202020202020202020202020202020202020202020202020202272656365697074223a207b0d0a2020202020202020202020202020202020202020202020202020202020202020227265636569766572223a202263727970746b6565706572222c0d0a2020202020202020202020202020202020202020202020202020202020202020226163745f646967657374223a202239656162323339643636643133633334623963633335613666373966623266366436316132643964663961343834303735633832653635643733613063626338222c0d0a202020202020202020202020202020202020202020202020202020202020202022676c6f62616c5f73657175656e6365223a2032313130373633302c0d0a202020202020202020202020202020202020202020202020202020202020202022726563765f73657175656e6365223a203439372c0d0a202020202020202020202020202020202020202020202020202020202020202022617574685f73657175656e6365223a205b0d0a2020202020202020202020202020202020202020202020202020202020202020202020205b0d0a202020202020202020202020202020202020202020202020202020202020202020202020202020202263727970746b6565706572222c0d0a202020202020202020202020202020202020202020202020202020202020202020202020202020203837390d0a2020202020202020202020202020202020202020202020202020202020202020202020205d0d0a20202020202020202020202020202020202020202020202020202020202020205d2c0d0a202020202020202020202020202020202020202020202020202020202020202022636f64655f73657175656e6365223a20312c0d0a2020202020202020202020202020202020202020202020202020202020202020226162695f73657175656e6365223a20340d0a202020202020202020202020202020202020202020202020202020207d2c0d0a2020202020202020202020202020202020202020202020202020202022616374223a207b0d0a2020202020202020202020202020202020202020202020202020202020202020226163636f756e74223a2022656f73696f2e746f6b656e222c0d0a2020202020202020202020202020202020202020202020202020202020202020226e616d65223a20227472616e73666572222c0d0a202020202020202020202020202020202020202020202020202020202020202022617574686f72697a6174696f6e223a205b0d0a2020202020202020202020202020202020202020202020202020202020202020202020207b0d0a20202020202020202020202020202020202020202020202020202020202020202020202020202020226163746f72223a202263727970746b6565706572222c0d0a20202020202020202020202020202020202020202020202020202020202020202020202020202020227065726d697373696f6e223a2022616374697665220d0a2020202020202020202020202020202020202020202020202020202020202020202020207d0d0a20202020202020202020202020202020202020202020202020202020202020205d2c0d0a20202020202020202020202020202020202020202020202020202020202020202264617461223a207b0d0a2020202020202020202020202020202020202020202020202020202020202020202020202266726f6d223a202263727970746b6565706572222c0d0a20202020202020202020202020202020202020202020202020202020202020202020202022746f223a20226272616e646f6e222c0d0a202020202020202020202020202020202020202020202020202020202020202020202020227175616e74697479223a202234322e3030303020454f53222c0d0a202020202020202020202020202020202020202020202020202020202020202020202020226d656d6f223a2022746865206772617373686f70706572206c696573206865617679220d0a20202020202020202020202020202020202020202020202020202020202020207d2c0d0a2020202020202020202020202020202020202020202020202020202020202020226865785f64617461223a202230306165616134616331356366643435303030303030363064323334636433646130363830363030303030303030303030343435346635333030303030303030316137343638363532303637373236313733373336383666373037303635373232303663363936353733323036383635363137363739220d0a202020202020202020202020202020202020202020202020202020207d2c0d0a2020202020202020202020202020202020202020202020202020202022636f6e746578745f66726565223a2066616c73652c0d0a2020202020202020202020202020202020202020202020202020202022656c6170736564223a20342c0d0a20202020202020202020202020202020202020202020202020202020226370755f7573616765223a20302c0d0a2020202020202020202020202020202020202020202020202020202022636f6e736f6c65223a2022222c0d0a2020202020202020202020202020202020202020202020202020202022746f74616c5f6370755f7573616765223a20302c0d0a20202020202020202020202020202020202020202020202020202020227472785f6964223a202232646534636433383263326532333163386133616338306163666365613439336464326439653731373862343664313635323833636639316332636536313231222c0d0a2020202020202020202020202020202020202020202020202020202022626c6f636b5f6e756d223a2032313130323331382c0d0a2020202020202020202020202020202020202020202020202020202022626c6f636b5f74696d65223a2022323031392d30312d32385431363a34363a34392e303030222c0d0a202020202020202020202020202020202020202020202020202020202270726f64756365725f626c6f636b5f6964223a206e756c6c2c0d0a20202020202020202020202020202020202020202020202020202020226163636f756e745f72616d5f64656c746173223a205b5d2c0d0a2020202020202020202020202020202020202020202020202020202022696e6c696e655f747261636573223a205b5d0d0a2020202020202020202020202020202020202020202020207d2c0d0a2020202020202020202020202020202020202020202020207b0d0a202020202020202020202020202020202020202020202020202020202272656365697074223a207b0d0a2020202020202020202020202020202020202020202020202020202020202020227265636569766572223a20226272616e646f6e222c0d0a2020202020202020202020202020202020202020202020202020202020202020226163745f646967657374223a202239656162323339643636643133633334623963633335613666373966623266366436316132643964663961343834303735633832653635643733613063626338222c0d0a202020202020202020202020202020202020202020202020202020202020202022676c6f62616c5f73657175656e6365223a2032313130373633312c0d0a202020202020202020202020202020202020202020202020202020202020202022726563765f73657175656e6365223a203538332c0d0a202020202020202020202020202020202020202020202020202020202020202022617574685f73657175656e6365223a205b0d0a2020202020202020202020202020202020202020202020202020202020202020202020205b0d0a202020202020202020202020202020202020202020202020202020202020202020202020202020202263727970746b6565706572222c0d0a202020202020202020202020202020202020202020202020202020202020202020202020202020203838300d0a2020202020202020202020202020202020202020202020202020202020202020202020205d0d0a20202020202020202020202020202020202020202020202020202020202020205d2c0d0a202020202020202020202020202020202020202020202020202020202020202022636f64655f73657175656e6365223a20312c0d0a2020202020202020202020202020202020202020202020202020202020202020226162695f73657175656e6365223a20340d0a202020202020202020202020202020202020202020202020202020207d2c0d0a2020202020202020202020202020202020202020202020202020202022616374223a207b0d0a2020202020202020202020202020202020202020202020202020202020202020226163636f756e74223a2022656f73696f2e746f6b656e222c0d0a2020202020202020202020202020202020202020202020202020202020202020226e616d65223a20227472616e73666572222c0d0a202020202020202020202020202020202020202020202020202020202020202022617574686f72697a6174696f6e223a205b0d0a2020202020202020202020202020202020202020202020202020202020202020202020207b0d0a20202020202020202020202020202020202020202020202020202020202020202020202020202020226163746f72223a202263727970746b6565706572222c0d0a20202020202020202020202020202020202020202020202020202020202020202020202020202020227065726d697373696f6e223a2022616374697665220d0a2020202020202020202020202020202020202020202020202020202020202020202020207d0d0a20202020202020202020202020202020202020202020202020202020202020205d2c0d0a20202020202020202020202020202020202020202020202020202020202020202264617461223a207b0d0a2020202020202020202020202020202020202020202020202020202020202020202020202266726f6d223a202263727970746b6565706572222c0d0a20202020202020202020202020202020202020202020202020202020202020202020202022746f223a20226272616e646f6e222c0d0a202020202020202020202020202020202020202020202020202020202020202020202020227175616e74697479223a202234322e3030303020454f53222c0d0a202020202020202020202020202020202020202020202020202020202020202020202020226d656d6f223a2022746865206772617373686f70706572206c696573206865617679220d0a20202020202020202020202020202020202020202020202020202020202020207d2c0d0a2020202020202020202020202020202020202020202020202020202020202020226865785f64617461223a202230306165616134616331356366643435303030303030363064323334636433646130363830363030303030303030303030343435346635333030303030303030316137343638363532303637373236313733373336383666373037303635373232303663363936353733323036383635363137363739220d0a202020202020202020202020202020202020202020202020202020207d2c0d0a2020202020202020202020202020202020202020202020202020202022636f6e746578745f66726565223a2066616c73652c0d0a2020202020202020202020202020202020202020202020202020202022656c6170736564223a20362c0d0a20202020202020202020202020202020202020202020202020202020226370755f7573616765223a20302c0d0a2020202020202020202020202020202020202020202020202020202022636f6e736f6c65223a2022222c0d0a2020202020202020202020202020202020202020202020202020202022746f74616c5f6370755f7573616765223a20302c0d0a20202020202020202020202020202020202020202020202020202020227472785f6964223a202232646534636433383263326532333163386133616338306163666365613439336464326439653731373862343664313635323833636639316332636536313231222c0d0a2020202020202020202020202020202020202020202020202020202022626c6f636b5f6e756d223a2032313130323331382c0d0a2020202020202020202020202020202020202020202020202020202022626c6f636b5f74696d65223a2022323031392d30312d32385431363a34363a34392e303030222c0d0a202020202020202020202020202020202020202020202020202020202270726f64756365725f626c6f636b5f6964223a206e756c6c2c0d0a20202020202020202020202020202020202020202020202020202020226163636f756e745f72616d5f64656c746173223a205b5d2c0d0a2020202020202020202020202020202020202020202020202020202022696e6c696e655f747261636573223a205b5d0d0a2020202020202020202020202020202020202020202020207d0d0a20202020202020202020202020202020202020205d0d0a202020202020202020202020202020207d0d0a2020202020202020202020205d2c0d0a20202020202020202020202022657863657074223a206e756c6c0d0a20202020202020207d0d0a202020207d") + XCTAssertNotNil(data) + } + func test_initWithHexString_withInvalidStringArgument_shouldReturnNil() { let data = Data(hexString: "Some invalid string") XCTAssertNil(data)