Skip to content

Commit fc82f7d

Browse files
committed
use String.cString to convert to (non-const) char *)
1 parent 4d5d003 commit fc82f7d

File tree

1 file changed

+10
-41
lines changed

1 file changed

+10
-41
lines changed

lib/Ziti.swift

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -412,36 +412,17 @@ import CZitiPrivate
412412
let refresh_interval = 90
413413
#endif
414414

415-
// convert key and id info to char * types that ziti-sdk-c can use.
416-
// also considered .withCString - https://stackoverflow.com/questions/31378120/convert-swift-string-into-cchar-pointer
417-
let ctrlPtr = UnsafeMutablePointer<Int8>.allocate(capacity: id.ztAPI.count + 1)
418-
ctrlPtr.initialize(from: id.ztAPI, count: id.ztAPI.count + 1)
419-
420-
let certPEMPtr = UnsafeMutablePointer<Int8>.allocate(capacity: certPEM.count + 1)
421-
certPEMPtr.initialize(from: certPEM, count: certPEM.count + 1)
422-
423-
let privKeyPEMPtr = UnsafeMutablePointer<Int8>.allocate(capacity: privKeyPEM.count + 1)
424-
privKeyPEMPtr.initialize(from: privKeyPEM, count: privKeyPEM.count + 1)
425-
426-
var caPEMPtr:UnsafeMutablePointer<Int8>? = nil // todo empty string
427-
if (id.ca != nil) {
428-
caPEMPtr = UnsafeMutablePointer<Int8>.allocate(capacity: id.ca!.count + 1)
429-
caPEMPtr!.initialize(from: id.ca!, count: id.ca!.count + 1)
430-
}
431-
432415
// set up the ziti_config with our cert, etc.
433416
var ctrls:model_list = model_list()
434417
id.ztAPIs?.forEach { c in
435-
let ctrlPtr = UnsafeMutablePointer<Int8>.allocate(capacity: c.count + 1)
436-
ctrlPtr.initialize(from: c, count: c.count + 1)
437-
model_list_append(&ctrls, ctrlPtr)
418+
model_list_append(&ctrls, c.cstring)
438419
}
439420

440421
var zitiCfg = ziti_config(
441-
controller_url: ctrlPtr,
422+
controller_url: id.ztAPI.cstring,
442423
controllers: ctrls,
443-
id: ziti_id_cfg(cert: certPEMPtr, key: privKeyPEMPtr, ca: caPEMPtr, oidc: nil),
444-
cfg_source: nil) // todo what is cfg_source?
424+
id: ziti_id_cfg(cert: certPEM.cstring, key: privKeyPEM.cstring, ca: id.ca?.cstring, oidc: nil),
425+
cfg_source: nil)
445426

446427
var zitiStatus = ziti_context_init(&self.ztx, &zitiCfg)
447428
guard zitiStatus == Ziti.ZITI_OK else {
@@ -450,24 +431,6 @@ import CZitiPrivate
450431
initCallback(ZitiError(errStr, errorCode: Int(zitiStatus)))
451432
return
452433
}
453-
454-
ctrlPtr.deallocate()
455-
certPEMPtr.deallocate()
456-
privKeyPEMPtr.deallocate()
457-
if (caPEMPtr != nil) {
458-
caPEMPtr!.deallocate()
459-
}
460-
461-
withUnsafeMutablePointer(to: &ctrls) { ctrlListPtr in
462-
var i = model_list_iterator(ctrlListPtr)
463-
while i != nil {
464-
let ctrlPtr = model_list_it_element(i)
465-
if let ctrl = UnsafeMutablePointer<CChar>(OpaquePointer(ctrlPtr)) {
466-
ctrl.deallocate()
467-
}
468-
i = model_list_it_next(i)
469-
}
470-
}
471434

472435
ziti_log_init_wrapper(loop)
473436

@@ -1160,3 +1123,9 @@ func scan<
11601123
}
11611124
return result
11621125
}
1126+
1127+
extension String {
1128+
var cstring: UnsafePointer<CChar> {
1129+
(self as NSString).cString(using: String.Encoding.utf8.rawValue)!
1130+
}
1131+
}

0 commit comments

Comments
 (0)