Skip to content

Commit

Permalink
Restructure B45.
Browse files Browse the repository at this point in the history
  • Loading branch information
yspreen committed Apr 21, 2021
1 parent 530f71a commit 5dad435
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
8 changes: 8 additions & 0 deletions PatientScannerDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
CE3CC93C2628A7820079FB78 /* ASN1.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE3CC93B2628A7820079FB78 /* ASN1.swift */; };
CE3CC9442628C2130079FB78 /* CBOR.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE3CC9432628C2130079FB78 /* CBOR.swift */; };
CE44798D26304D8F009A836B /* JSONSchema in Frameworks */ = {isa = PBXBuildFile; productRef = CE44798C26304D8F009A836B /* JSONSchema */; };
CE44799226306C86009A836B /* String.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE44799126306C86009A836B /* String.swift */; };
CE44799726306C9B009A836B /* Data+Base45.swift in Sources */ = {isa = PBXBuildFile; fileRef = CE44799626306C9B009A836B /* Data+Base45.swift */; };
CE7DE7FA2625EF18007E6694 /* SwiftCBOR in Frameworks */ = {isa = PBXBuildFile; productRef = CE7DE7F92625EF18007E6694 /* SwiftCBOR */; };
CEA1555D262F63B30024B7AC /* EuDgcSchema.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEA1555C262F63B30024B7AC /* EuDgcSchema.swift */; };
CEA15563262F6DAB0024B7AC /* ChildDismissedDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEA15562262F6DAB0024B7AC /* ChildDismissedDelegate.swift */; };
Expand Down Expand Up @@ -71,6 +73,8 @@
CE1BDF98262A4CD600766F97 /* X509.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = X509.swift; sourceTree = "<group>"; };
CE3CC93B2628A7820079FB78 /* ASN1.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ASN1.swift; sourceTree = "<group>"; };
CE3CC9432628C2130079FB78 /* CBOR.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CBOR.swift; sourceTree = "<group>"; };
CE44799126306C86009A836B /* String.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = String.swift; sourceTree = "<group>"; };
CE44799626306C9B009A836B /* Data+Base45.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Base45.swift"; sourceTree = "<group>"; };
CEA1555C262F63B30024B7AC /* EuDgcSchema.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EuDgcSchema.swift; sourceTree = "<group>"; };
CEA15562262F6DAB0024B7AC /* ChildDismissedDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChildDismissedDelegate.swift; sourceTree = "<group>"; };
CEA1556A262F784E0024B7AC /* SelfSizedTableView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SelfSizedTableView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -160,6 +164,8 @@
CEFAD8712625F29E009AFEF9 /* String+JSON.swift */,
CE157F80262E1F7A00FE4821 /* Date.swift */,
CE157F9A262E2A9F00FE4821 /* SwiftCBOR.CBOR.swift */,
CE44799126306C86009A836B /* String.swift */,
CE44799626306C9B009A836B /* Data+Base45.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -415,6 +421,8 @@
CEC2C4C32625ED030056E406 /* JWK.swift in Sources */,
CEC2C4C42625ED030056E406 /* Base45.swift in Sources */,
CE3CC9442628C2130079FB78 /* CBOR.swift in Sources */,
CE44799226306C86009A836B /* String.swift in Sources */,
CE44799726306C9B009A836B /* Data+Base45.swift in Sources */,
CE13CF0F262DD0D80070C80E /* FullFloatingPanelLayout.swift in Sources */,
CEA1556B262F784E0024B7AC /* SelfSizedTableView.swift in Sources */,
CE157F81262E1F7A00FE4821 /* Date.swift in Sources */,
Expand Down
34 changes: 34 additions & 0 deletions PatientScannerDemo/Extensions/Data+Base45.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Data+Base45.swift
// PatientScannerDemo
//
// Created by Yannick Spreen on 4/21/21.
//

import Foundation


extension Data {
public func toBase45()->String {
var o = String()
for i in stride(from:0, to:self.count, by: 2) {
if (self.count - i > 1) {
let x : Int = (Int(self[i])<<8) + Int(self[i+1])
let e : Int = x / (45*45)
let x2 : Int = x % (45*45)
let d : Int = x2 / 45
let c : Int = x2 % 45
o.append(BASE45_CHARSET[c])
o.append(BASE45_CHARSET[d])
o.append(BASE45_CHARSET[e])
} else {
let x2 : Int = Int(self[i])
let d : Int = x2 / 45
let c : Int = x2 % 45
o.append(BASE45_CHARSET[c])
o.append(BASE45_CHARSET[d])
}
}
return o
}
}
14 changes: 14 additions & 0 deletions PatientScannerDemo/Extensions/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// String.swift
// PatientScannerDemo
//
// Created by Yannick Spreen on 4/21/21.
//

import Foundation

extension String {
subscript(i: Int) -> String {
return String(self[index(startIndex, offsetBy: i)])
}
}
42 changes: 6 additions & 36 deletions PatientScannerDemo/Services/Base45.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@

import Foundation


let BASE45_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"

extension String {
enum Base45Error: Error {
case Base64InvalidCharacter
case Base64InvalidLength
}

public func fromBase45() throws -> Data {
let BASE45_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
var d = Data()
var o = Data()

Expand All @@ -25,13 +27,13 @@ extension String {
throw Base45Error.Base64InvalidCharacter
}
}
for i in stride(from:0, to:d.count, by: 3) {
for i in stride(from: 0, to: d.count, by: 3) {
if (d.count - i < 2) {
throw Base45Error.Base64InvalidLength
}
var x : UInt32 = UInt32(d[i]) + UInt32(d[i+1])*45
var x: UInt32 = UInt32(d[i]) + UInt32(d[i + 1]) * 45
if (d.count - i >= 3) {
x += 45 * 45 * UInt32(d[i+2])
x += 45 * 45 * UInt32(d[i + 2])
if x >= 256 * 256 {
throw Base45Error.Base64InvalidCharacter
}
Expand All @@ -42,35 +44,3 @@ extension String {
return o
}
}

extension String {
subscript(i: Int) -> String {
return String(self[index(startIndex, offsetBy: i)])
}
}

extension Data {
public func toBase45()->String {
let BASE45_CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:"
var o = String()
for i in stride(from:0, to:self.count, by: 2) {
if (self.count - i > 1) {
let x : Int = (Int(self[i])<<8) + Int(self[i+1])
let e : Int = x / (45*45)
let x2 : Int = x % (45*45)
let d : Int = x2 / 45
let c : Int = x2 % 45
o.append(BASE45_CHARSET[c])
o.append(BASE45_CHARSET[d])
o.append(BASE45_CHARSET[e])
} else {
let x2 : Int = Int(self[i])
let d : Int = x2 / 45
let c : Int = x2 % 45
o.append(BASE45_CHARSET[c])
o.append(BASE45_CHARSET[d])
}
}
return o
}
}

0 comments on commit 5dad435

Please sign in to comment.