Skip to content

Updated CBORParser to use relative data indices #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CBORCoding.podspec
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Pod::Spec.new do |s|

s.name = "CBORCoding"
s.version = "1.3.0"
s.version = "1.3.1"
s.summary = "A CBOR Encoder and Decoder"
s.description = <<-DESC
A lightweight framework containing a coder pair for encoding and decoding `Codable` conforming types to and from CBOR document format for iOS, macOS, tvOS, and watchOS.
Expand Down
32 changes: 16 additions & 16 deletions Sources/CBORCoding/CBOR.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ public struct CBOR {
chunks.reserveCapacity(numberOfChunks)

for i in 0 ..< numberOfChunks {
let range = ((i * chunkSize) ..< ((i + 1) * chunkSize)).clamped(to: 0 ..< numberOfBytes)
let lowerBound = data.index(data.startIndex, offsetBy: i * chunkSize)
let upperBound = data.index(data.startIndex, offsetBy: (i + 1) * chunkSize, limitedBy: data.endIndex) ?? data.endIndex

chunks.append(Data(data[range]))
chunks.append(Data(data[lowerBound ..< upperBound]))
}

self.chunks = chunks
Expand Down Expand Up @@ -184,9 +185,10 @@ public struct CBOR {
chunks.reserveCapacity(numberOfChunks)

for i in 0 ..< numberOfChunks {
let range = ((i * chunkSize) ..< ((i + 1) * chunkSize)).clamped(to: 0 ..< numberOfBytes)
let lowerBound = data.index(data.startIndex, offsetBy: i * chunkSize)
let upperBound = data.index(data.startIndex, offsetBy: (i + 1) * chunkSize, limitedBy: data.endIndex) ?? data.endIndex

chunks.append(Data(data[range]))
chunks.append(Data(data[lowerBound ..< upperBound]))
}

self.chunks = chunks
Expand Down Expand Up @@ -291,9 +293,9 @@ extension CBOR {

init?(bits: Data) {
guard !bits.isEmpty else { return nil }
guard CBOR.majorType(for: bits[0]) == .tag else { return nil }
guard CBOR.majorType(for: bits[bits.startIndex]) == .tag else { return nil }

let additonalInfo = CBOR.additionalInfo(for: bits[0])
let additonalInfo = CBOR.additionalInfo(for: bits[bits.startIndex])
switch additonalInfo {
case 0: self = .standardDateTime
case 1: self = .epochDateTime
Expand All @@ -307,7 +309,7 @@ extension CBOR {
case 24:
guard bits.count >= 2 else { return nil }

switch bits[1] {
switch bits[bits.index(after: bits.startIndex)] {
case 24: self = .encodedCBORData
case 32: self = .uri
case 33: self = .base64URL
Expand All @@ -320,7 +322,7 @@ extension CBOR {
case 25:
guard bits.count >= 3 else { return nil }

switch (bits[1], bits[2]) {
switch (bits[bits.index(bits.startIndex, offsetBy: 1)], bits[bits.index(bits.startIndex, offsetBy: 2)]) {
case (0xD9, 0xF7): self = .selfDescribedCBOR
default: return nil
}
Expand Down Expand Up @@ -387,12 +389,12 @@ extension CBOR {
internal var stringValue: String
internal var intValue: Int?

internal init?(stringValue: String) {
internal init(stringValue: String) {
self.stringValue = stringValue
self.intValue = nil
}

internal init?(intValue: Int) {
internal init(intValue: Int) {
self.stringValue = "\(intValue)"
self.intValue = intValue
}
Expand All @@ -406,9 +408,7 @@ extension CBOR {

// MARK: Constants

// swiftlint:disable force_unwrapping
internal static let `super` = CodingKey(stringValue: "super")!
// swiftlint:enable force_unwrapping
internal static let `super` = CodingKey(stringValue: "super")
}

// MARK: Internal Methods
Expand Down Expand Up @@ -459,16 +459,16 @@ extension DecodingError {
let expectedTypes: String
if expected.count > 2 {
var types = expected.map { "\($0)" }
types[types.endIndex - 1] = "or " + types[types.endIndex - 1]
types[types.index(before: types.endIndex)] = "or " + types[types.index(before: types.endIndex)]

expectedTypes = types.joined(separator: ", ")
} else if expected.count > 1 {
expectedTypes = expected.map({ "\($0)" }).joined(separator: " or ")
} else {
expectedTypes = "\(expected[0])"
expectedTypes = "\(expected[expected.startIndex])"
}

self = .typeMismatch(expected[0], Context(codingPath: path, debugDescription: "Expected to decode \(expectedTypes) but found \(actual) instead."))
self = .typeMismatch(expected[expected.startIndex], Context(codingPath: path, debugDescription: "Expected to decode \(expectedTypes) but found \(actual) instead."))

case let .dataCorrupted(description):
self = .dataCorrupted(Context(codingPath: path, debugDescription: description))
Expand Down
12 changes: 3 additions & 9 deletions Sources/CBORCoding/CBOREncoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,7 @@ internal class __CBOREncoder: CBOREncoderProtocol, SingleValueEncodingContainer

do {
for (key, value) in dictionary {
// swiftlint:disable force_unwrapping
let codingKey = CBOR.CodingKey(stringValue: key)!
// swiftlint:enable force_unwrapping
let codingKey = CBOR.CodingKey(stringValue: key)

codingPath.append(codingKey)
defer { codingPath.removeLast() }
Expand All @@ -616,9 +614,7 @@ internal class __CBOREncoder: CBOREncoderProtocol, SingleValueEncodingContainer

do {
for (key, value) in dictionary {
// swiftlint:disable force_unwrapping
let codingKey = CBOR.CodingKey(intValue: key)!
// swiftlint:enable force_unwrapping
let codingKey = CBOR.CodingKey(intValue: key)

codingPath.append(codingKey)
defer { codingPath.removeLast() }
Expand Down Expand Up @@ -1120,9 +1116,7 @@ private class __CBORReferencingEncoder: __CBOREncoder {
array.insert(value, at: index)

case let .dictionary(dictionary, key):
// swiftlint:disable force_unwrapping
dictionary[CBOR.CodingKey(stringValue: key)!] = value
// swiftlint:enable force_unwrapping
dictionary[CBOR.CodingKey(stringValue: key)] = value
}
}

Expand Down
Loading