Skip to content

Commit

Permalink
Merge pull request #2 from Hassaniiii/issue#1
Browse files Browse the repository at this point in the history
The issue has been resolved
  • Loading branch information
Hassan Shahbazi authored Jul 26, 2018
2 parents 45c504f + f8b7971 commit 1b57b35
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 12 deletions.
31 changes: 22 additions & 9 deletions CBORSwift/Classes/Encoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ class Encoder: NSObject {
return encoded
}

private class func prepareHeaderByteArray(bytes: inout [UInt8], measure: Int) {
let upperBound: UInt64 = 4294967295

if measure >= 0 && measure <= 23 {}
else if measure >= 24 && measure <= 255 { bytes = 24.decimal_binary }
else if measure >= 256 && measure <= 65535 { bytes = 25.decimal_binary }
else if measure >= 65536 && measure <= upperBound { bytes = 26.decimal_binary }
class func prepareEncodedResponse(encodedArray: inout [UInt8]) -> String {
var response = ""
if encodedArray.count > 64 {
response = [UInt8](encodedArray[0..<64]).binary_decimal.hex
encodedArray = [UInt8](encodedArray[64..<encodedArray.count])
}
response.append(Data(bytes: encodedArray).binary_decimal.hex)
return response
}

class func getIncludedEncodings(item: AnyObject) -> String {
Expand All @@ -36,6 +37,16 @@ class Encoder: NSObject {
}
}

private extension Encoder {
private class func prepareHeaderByteArray(bytes: inout [UInt8], measure: Int) {
if measure >= 0 && measure <= 23 {}
else if measure >= 24 && measure <= UInt8.max { bytes = 24.decimal_binary }
else if measure >= UInt16.min && measure <= UInt16.max { bytes = 25.decimal_binary }
else if measure >= UInt32.min && measure <= UInt32.max { bytes = 26.decimal_binary }
else if measure >= UInt64.min && measure <= UInt64.max { bytes = 27.decimal_binary }
}
}

extension NSObject: Any {
@objc internal func encode() -> String {
return self.encode()
Expand All @@ -47,8 +58,10 @@ extension NSNumber {
let major: MajorType = (self.intValue < 0) ? .major1 : .major0
let measure = (self.intValue < 0) ? (self.intValue * -1) - 1 : self.intValue

let encodedArray = Encoder.prepareByteArray(major: major, measure: measure)
return Data(bytes: encodedArray).binary_decimal.hex
var encodedArray = Encoder.prepareByteArray(major: major, measure: measure)
let response = Encoder.prepareEncodedResponse(encodedArray: &encodedArray)

return response
}
}

Expand Down
11 changes: 8 additions & 3 deletions CBORSwift/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,18 @@ extension String {
bits.append(UInt8(chrInt))
}

let bitsSize = bits.count % 8
if bitsSize == 0 {
if bits.count % 8 == 0 {
return bits
}

var bitDiff = (bits.count / 8) + 1
if bitDiff > 2 {
if bitDiff > 8 {
bitDiff = 16
}
else if bitDiff > 4 {
bitDiff = 8
}
else if bitDiff > 2 {
bitDiff = 4
}

Expand Down
10 changes: 10 additions & 0 deletions CBORSwiftTests/CBOREncoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ class CBOREncoderTests: XCTestCase {
XCTAssertEqual([0x1A, 0x02, 0x8F, 0x5A, 0xAF], encoded)
}

func test_6_encodeBigIntegers() {
var encoded = CBOR.encode(NSNumber(value: 1531842146400))
XCTAssertNotNil(encoded)
XCTAssertEqual([0x1B, 0x00, 0x00, 0x01, 0x64, 0xA8, 0xE8, 0x30, 0x60], encoded)

encoded = CBOR.encode(NSNumber(value: 999999999999999999))
XCTAssertNotNil(encoded)
XCTAssertEqual([0x1B, 0x0D, 0xE0, 0xB6, 0xB3, 0xA7, 0x63, 0xFF, 0xFF], encoded)
}

//MARK:- Negative integer encoding
func test_2_encodeSimpleNeg() {
var encoded = CBOR.encode(NSNumber(value: -1))
Expand Down
8 changes: 8 additions & 0 deletions CBORSwiftTests/CBORSwiftTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ class CBORSwiftTests: XCTestCase {
XCTAssertEqual(Data(bytes: [0x01, 0x00, 0x01]), major.get())
}

func test_2_convertions() {
var binary = "111111111111111111111111111111111111111111111111111111111111111"
XCTAssertEqual(9223372036854775807, binary.bytes.binary_decimal)

binary = "0001101100000000000000010110010010101000111010000011000001100000"
XCTAssertEqual(1945556570866200672, binary.bytes.binary_decimal)
}

func test_9_FinalComprehensiveTest_Encode() {
let id_1 = NSByteString("687134968222EC17202E42505F8ED2B16AE22F16BB05B88C25DB9E602645F141".lowercased())

Expand Down

0 comments on commit 1b57b35

Please sign in to comment.