-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathParcel.swift
More file actions
1255 lines (1103 loc) · 39.3 KB
/
Parcel.swift
File metadata and controls
1255 lines (1103 loc) · 39.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// AndroidParcel.swift
// SwiftAndroid
//
// Created by Alsey Coleman Miller on 7/6/25.
//
#if canImport(Android)
import Android
import CAndroidNDK
#endif
import Binder
// MARK: - Parcel
/**
* Android Binder Parcel
*
* This object represents a package of data that can be sent between processes. When transacting, an
* instance of it is automatically created to be used for the transaction. When two processes use
* binder to communicate, they must agree on a format of this parcel to be used in order to transfer
* data. This is usually done in an IDL (see AIDL, specifically).
*/
public struct Parcel: ~Copyable {
internal let handle: Handle
internal init(handle: Handle) {
self.handle = handle
}
deinit {
handle.delete()
}
}
// MARK: - Initialization
public extension Parcel {
/// Directly initialize from a pointer.
init(_ pointer: OpaquePointer) {
self.handle = .init(pointer)
}
/**
* Creates a parcel.
*
* Available since API level 31.
*
* \return A parcel which is not related to any IBinder objects.
*/
init() {
self.handle = .create()
}
}
// MARK: - Properties
public extension Parcel {
/**
* Gets the current position within the parcel.
*
* Available since API level 29.
*/
var dataPosition: Int {
Int(handle.dataPosition)
}
/**
* Returns the total amount of data contained in the parcel.
*
* Available since API level 31.
*/
var dataSize: Int {
Int(handle.dataSize)
}
}
// MARK: - Methods
public extension Parcel {
/// Access the underlying opaque pointer.
func withUnsafePointer<E, Result>(_ body: (OpaquePointer) throws(E) -> Result) throws(E) -> Result where E: Error {
try body(handle.pointer)
}
/**
* Sets the position within the parcel.
*
* This must be called with a position that has been previously returned from
* AParcel_getDataPosition. If writes are made after setting the data position, they must
* be made in the exact same sequence used before resetting data position. Writing over
* objects such as binders or file descriptors is not supported.
*
* Available since API level 29.
*/
func setDataPosition(_ position: Int) throws(AndroidBinderError) {
try handle.setDataPosition(Int32(position)).get()
}
/**
* Resets the parcel to its initial state.
*
* Available since API level 31.
*/
func reset() throws(AndroidBinderError) {
try handle.reset().get()
}
/**
* Appends all data from another parcel into this parcel.
*
* Available since API level 31.
*
* \param other the parcel to read from.
* \param start starting position in \p other (must be a value from getDataPosition).
* \param size number of bytes to copy from \p other.
*/
func appendContents(of other: borrowing Parcel, start: Int = 0, size: Int) throws(AndroidBinderError) {
try handle.appendFrom(other.handle, start: Int32(start), size: Int32(size)).get()
}
/**
* Marshals the parcel data into a byte buffer.
*
* Available since API level 33.
*
* \param start starting position in this parcel.
* \param length number of bytes to marshal, or nil to marshal from \p start to end.
* \return the marshaled bytes.
*/
func marshal(start: Int = 0, length: Int? = nil) throws(AndroidBinderError) -> [UInt8] {
let len = length ?? (dataSize - start)
var buffer = [UInt8](repeating: 0, count: len)
if let base = buffer.withUnsafeMutableBufferPointer({ $0.baseAddress }) {
try handle.marshal(into: base, start: start, length: len).get()
}
return buffer
}
/**
* Unmarshals the parcel from a byte buffer.
*
* Available since API level 33.
*
* \param data the bytes to unmarshal.
*/
func unmarshal(_ data: [UInt8]) throws(AndroidBinderError) {
if let base = data.withUnsafeBufferPointer({ $0.baseAddress }) {
try handle.unmarshal(base, length: data.count).get()
}
}
}
// MARK: - Write Methods
public extension Parcel {
/**
* Writes an Int32 value to the parcel.
*
* Available since API level 29.
*/
func writeInt32(_ value: Int32) throws(AndroidBinderError) {
try handle.writeInt32(value).get()
}
/**
* Writes a UInt32 value to the parcel.
*
* Available since API level 29.
*/
func writeUInt32(_ value: UInt32) throws(AndroidBinderError) {
try handle.writeUInt32(value).get()
}
/**
* Writes an Int64 value to the parcel.
*
* Available since API level 29.
*/
func writeInt64(_ value: Int64) throws(AndroidBinderError) {
try handle.writeInt64(value).get()
}
/**
* Writes a UInt64 value to the parcel.
*
* Available since API level 29.
*/
func writeUInt64(_ value: UInt64) throws(AndroidBinderError) {
try handle.writeUInt64(value).get()
}
/**
* Writes a Bool value to the parcel.
*
* Available since API level 29.
*/
func writeBool(_ value: Bool) throws(AndroidBinderError) {
try handle.writeBool(value).get()
}
/**
* Writes a Float value to the parcel.
*
* Available since API level 29.
*/
func writeFloat(_ value: Float) throws(AndroidBinderError) {
try handle.writeFloat(value).get()
}
/**
* Writes a Double value to the parcel.
*
* Available since API level 29.
*/
func writeDouble(_ value: Double) throws(AndroidBinderError) {
try handle.writeDouble(value).get()
}
/**
* Writes a char16_t value to the parcel.
*
* Available since API level 29.
*/
func writeChar(_ value: UInt16) throws(AndroidBinderError) {
try handle.writeChar(value).get()
}
/**
* Writes an int8_t (byte) value to the parcel.
*
* Available since API level 29.
*/
func writeByte(_ value: Int8) throws(AndroidBinderError) {
try handle.writeByte(value).get()
}
/**
* Writes a String to the parcel encoded as UTF-8.
*
* Available since API level 29.
*/
func writeString(_ value: String) throws(AndroidBinderError) {
try handle.writeString(value).get()
}
/**
* Writes an AIBinder (strong binder reference) to the parcel.
*
* Available since API level 29.
*
* \param binder the binder to write, or nil for a null binder.
*/
func writeStrongBinder(_ binder: AndroidBinder?) throws(AndroidBinderError) {
try handle.writeStrongBinder(binder).get()
}
/**
* Writes a file descriptor to the parcel.
*
* This duplicates the fd when writing, so the caller retains ownership.
*
* Available since API level 29.
*
* \param fd the file descriptor to write, or -1 for a null file descriptor.
*/
func writeFileDescriptor(_ fd: Int32) throws(AndroidBinderError) {
try handle.writeFileDescriptor(fd).get()
}
/**
* Writes an AStatus as the status header of a reply parcel.
*
* Available since API level 29.
*/
func writeStatusHeader(_ status: borrowing Status) throws(AndroidBinderError) {
try handle.writeStatusHeader(status).get()
}
/**
* Writes a nullable Int8 array to the parcel.
*
* Available since API level 29.
*
* \param values the array to write, or nil to write a null array.
*/
func writeByteArray(_ values: [Int8]?) throws(AndroidBinderError) {
try handle.writeByteArray(values).get()
}
/**
* Writes a nullable Int32 array to the parcel.
*
* Available since API level 29.
*/
func writeInt32Array(_ values: [Int32]?) throws(AndroidBinderError) {
try handle.writeInt32Array(values).get()
}
/**
* Writes a nullable UInt32 array to the parcel.
*
* Available since API level 29.
*/
func writeUInt32Array(_ values: [UInt32]?) throws(AndroidBinderError) {
try handle.writeUInt32Array(values).get()
}
/**
* Writes a nullable Int64 array to the parcel.
*
* Available since API level 29.
*/
func writeInt64Array(_ values: [Int64]?) throws(AndroidBinderError) {
try handle.writeInt64Array(values).get()
}
/**
* Writes a nullable UInt64 array to the parcel.
*
* Available since API level 29.
*/
func writeUInt64Array(_ values: [UInt64]?) throws(AndroidBinderError) {
try handle.writeUInt64Array(values).get()
}
/**
* Writes a nullable Float array to the parcel.
*
* Available since API level 29.
*/
func writeFloatArray(_ values: [Float]?) throws(AndroidBinderError) {
try handle.writeFloatArray(values).get()
}
/**
* Writes a nullable Double array to the parcel.
*
* Available since API level 29.
*/
func writeDoubleArray(_ values: [Double]?) throws(AndroidBinderError) {
try handle.writeDoubleArray(values).get()
}
/**
* Writes a nullable char16_t array to the parcel.
*
* Available since API level 29.
*/
func writeCharArray(_ values: [UInt16]?) throws(AndroidBinderError) {
try handle.writeCharArray(values).get()
}
/**
* Writes a nullable Bool array to the parcel.
*
* Available since API level 29.
*/
func writeBoolArray(_ values: [Bool]?) throws(AndroidBinderError) {
try handle.writeBoolArray(values).get()
}
}
// MARK: - Read Methods
public extension Parcel {
/**
* Reads an Int32 value from the parcel.
*
* Available since API level 29.
*/
func readInt32() throws(AndroidBinderError) -> Int32 {
try handle.readInt32().get()
}
/**
* Reads a UInt32 value from the parcel.
*
* Available since API level 29.
*/
func readUInt32() throws(AndroidBinderError) -> UInt32 {
try handle.readUInt32().get()
}
/**
* Reads an Int64 value from the parcel.
*
* Available since API level 29.
*/
func readInt64() throws(AndroidBinderError) -> Int64 {
try handle.readInt64().get()
}
/**
* Reads a UInt64 value from the parcel.
*
* Available since API level 29.
*/
func readUInt64() throws(AndroidBinderError) -> UInt64 {
try handle.readUInt64().get()
}
/**
* Reads a Bool value from the parcel.
*
* Available since API level 29.
*/
func readBool() throws(AndroidBinderError) -> Bool {
try handle.readBool().get()
}
/**
* Reads a Float value from the parcel.
*
* Available since API level 29.
*/
func readFloat() throws(AndroidBinderError) -> Float {
try handle.readFloat().get()
}
/**
* Reads a Double value from the parcel.
*
* Available since API level 29.
*/
func readDouble() throws(AndroidBinderError) -> Double {
try handle.readDouble().get()
}
/**
* Reads a char16_t value from the parcel.
*
* Available since API level 29.
*/
func readChar() throws(AndroidBinderError) -> UInt16 {
try handle.readChar().get()
}
/**
* Reads an int8_t (byte) value from the parcel.
*
* Available since API level 29.
*/
func readByte() throws(AndroidBinderError) -> Int8 {
try handle.readByte().get()
}
/**
* Reads a UTF-8 String from the parcel.
*
* Available since API level 29.
*/
func readString() throws(AndroidBinderError) -> String {
try handle.readString().get()
}
/**
* Reads an AIBinder (strong binder reference) from the parcel.
*
* The returned binder has a +1 strong reference that the caller owns.
*
* Available since API level 29.
*/
func readStrongBinder() throws(AndroidBinderError) -> AndroidBinder {
try handle.readStrongBinder().get()
}
/**
* Reads a file descriptor from the parcel.
*
* The caller owns the returned fd and is responsible for closing it.
*
* Available since API level 29.
*
* \return the file descriptor, or -1 if a null file descriptor was written.
*/
func readFileDescriptor() throws(AndroidBinderError) -> Int32 {
try handle.readFileDescriptor().get()
}
/**
* Reads the status header from a reply parcel.
*
* Available since API level 29.
*/
func readStatusHeader() throws(AndroidBinderError) -> Status {
try handle.readStatusHeader().get()
}
/**
* Reads a nullable Int8 array from the parcel.
*
* Available since API level 29.
*
* \return the array, or nil if a null array was written.
*/
func readByteArray() throws(AndroidBinderError) -> [Int8]? {
try handle.readByteArray().get()
}
/**
* Reads a nullable Int32 array from the parcel.
*
* Available since API level 29.
*/
func readInt32Array() throws(AndroidBinderError) -> [Int32]? {
try handle.readInt32Array().get()
}
/**
* Reads a nullable UInt32 array from the parcel.
*
* Available since API level 29.
*/
func readUInt32Array() throws(AndroidBinderError) -> [UInt32]? {
try handle.readUInt32Array().get()
}
/**
* Reads a nullable Int64 array from the parcel.
*
* Available since API level 29.
*/
func readInt64Array() throws(AndroidBinderError) -> [Int64]? {
try handle.readInt64Array().get()
}
/**
* Reads a nullable UInt64 array from the parcel.
*
* Available since API level 29.
*/
func readUInt64Array() throws(AndroidBinderError) -> [UInt64]? {
try handle.readUInt64Array().get()
}
/**
* Reads a nullable Float array from the parcel.
*
* Available since API level 29.
*/
func readFloatArray() throws(AndroidBinderError) -> [Float]? {
try handle.readFloatArray().get()
}
/**
* Reads a nullable Double array from the parcel.
*
* Available since API level 29.
*/
func readDoubleArray() throws(AndroidBinderError) -> [Double]? {
try handle.readDoubleArray().get()
}
/**
* Reads a nullable char16_t array from the parcel.
*
* Available since API level 29.
*/
func readCharArray() throws(AndroidBinderError) -> [UInt16]? {
try handle.readCharArray().get()
}
/**
* Reads a nullable Bool array from the parcel.
*
* Available since API level 29.
*/
func readBoolArray() throws(AndroidBinderError) -> [Bool]? {
try handle.readBoolArray().get()
}
}
// MARK: - Supporting Types
internal extension Parcel {
struct Handle {
let pointer: OpaquePointer
init(_ pointer: OpaquePointer) {
self.pointer = pointer
}
}
}
// MARK: - Handle: Lifecycle
internal extension Parcel.Handle {
/**
* Creates a parcel.
*
* Available since API level 31.
*/
static func create() -> Parcel.Handle {
guard let pointer = AParcel_create() else {
fatalError("Unable to initialize \(Self.self) \(#function)")
}
return Parcel.Handle(pointer)
}
/**
* Cleans up a parcel.
*
* Available since API level 29.
*/
func delete() {
AParcel_delete(pointer)
}
}
// MARK: - Handle: Position & Size
internal extension Parcel.Handle {
/**
* Gets the current position within the parcel.
*
* Available since API level 29.
*/
var dataPosition: Int32 {
AParcel_getDataPosition(pointer)
}
/**
* Returns the total amount of data contained in the parcel.
*
* Available since API level 31.
*/
var dataSize: Int32 {
AParcel_getDataSize(pointer)
}
/**
* Sets the position within the parcel.
*
* Available since API level 29.
*/
func setDataPosition(_ position: Int32) -> Result<Void, AndroidBinderError> {
AParcel_setDataPosition(pointer, position).mapError()
}
/**
* Resets the parcel to its initial state.
*
* Available since API level 31.
*/
func reset() -> Result<Void, AndroidBinderError> {
AParcel_reset(pointer).mapError()
}
/**
* Appends data from `from` into this parcel.
*
* Available since API level 31.
*/
func appendFrom(_ from: Parcel.Handle, start: Int32, size: Int32) -> Result<Void, AndroidBinderError> {
AParcel_appendFrom(from.pointer, pointer, start, size).mapError()
}
/**
* Marshals the parcel data into a buffer.
*
* Available since API level 33.
*/
func marshal(into buffer: UnsafeMutablePointer<UInt8>, start: Int, length: Int) -> Result<Void, AndroidBinderError> {
AParcel_marshal(pointer, buffer, start, length).mapError()
}
/**
* Unmarshals the parcel from a buffer.
*
* Available since API level 33.
*/
func unmarshal(_ data: UnsafePointer<UInt8>, length: Int) -> Result<Void, AndroidBinderError> {
AParcel_unmarshal(pointer, data, length).mapError()
}
}
// MARK: - Handle: Scalar Writes
internal extension Parcel.Handle {
func writeInt32(_ value: Int32) -> Result<Void, AndroidBinderError> {
AParcel_writeInt32(pointer, value).mapError()
}
func writeUInt32(_ value: UInt32) -> Result<Void, AndroidBinderError> {
AParcel_writeUint32(pointer, value).mapError()
}
func writeInt64(_ value: Int64) -> Result<Void, AndroidBinderError> {
AParcel_writeInt64(pointer, value).mapError()
}
func writeUInt64(_ value: UInt64) -> Result<Void, AndroidBinderError> {
AParcel_writeUint64(pointer, value).mapError()
}
func writeBool(_ value: Bool) -> Result<Void, AndroidBinderError> {
AParcel_writeBool(pointer, value).mapError()
}
func writeFloat(_ value: Float) -> Result<Void, AndroidBinderError> {
AParcel_writeFloat(pointer, value).mapError()
}
func writeDouble(_ value: Double) -> Result<Void, AndroidBinderError> {
AParcel_writeDouble(pointer, value).mapError()
}
func writeChar(_ value: UInt16) -> Result<Void, AndroidBinderError> {
AParcel_writeChar(pointer, value).mapError()
}
func writeByte(_ value: Int8) -> Result<Void, AndroidBinderError> {
AParcel_writeByte(pointer, value).mapError()
}
func writeString(_ value: String) -> Result<Void, AndroidBinderError> {
value.withCString { cStr in
AParcel_writeString(pointer, cStr, Int32(value.utf8.count)).mapError()
}
}
func writeStrongBinder(_ binder: AndroidBinder?) -> Result<Void, AndroidBinderError> {
AParcel_writeStrongBinder(pointer, binder?.handle.pointer).mapError()
}
func writeFileDescriptor(_ fd: Int32) -> Result<Void, AndroidBinderError> {
AParcel_writeParcelFileDescriptor(pointer, fd).mapError()
}
func writeStatusHeader(_ status: borrowing Status) -> Result<Void, AndroidBinderError> {
AParcel_writeStatusHeader(pointer, status.handle.pointer).mapError()
}
}
// MARK: - Handle: Scalar Reads
internal extension Parcel.Handle {
func readInt32() -> Result<Int32, AndroidBinderError> {
var value: Int32 = 0
return AParcel_readInt32(pointer, &value).mapError(value)
}
func readUInt32() -> Result<UInt32, AndroidBinderError> {
var value: UInt32 = 0
return AParcel_readUint32(pointer, &value).mapError(value)
}
func readInt64() -> Result<Int64, AndroidBinderError> {
var value: Int64 = 0
return AParcel_readInt64(pointer, &value).mapError(value)
}
func readUInt64() -> Result<UInt64, AndroidBinderError> {
var value: UInt64 = 0
return AParcel_readUint64(pointer, &value).mapError(value)
}
func readBool() -> Result<Bool, AndroidBinderError> {
var value = false
return AParcel_readBool(pointer, &value).mapError(value)
}
func readFloat() -> Result<Float, AndroidBinderError> {
var value: Float = 0
return AParcel_readFloat(pointer, &value).mapError(value)
}
func readDouble() -> Result<Double, AndroidBinderError> {
var value: Double = 0
return AParcel_readDouble(pointer, &value).mapError(value)
}
func readChar() -> Result<UInt16, AndroidBinderError> {
var value: UInt16 = 0
return AParcel_readChar(pointer, &value).mapError(value)
}
func readByte() -> Result<Int8, AndroidBinderError> {
var value: Int8 = 0
return AParcel_readByte(pointer, &value).mapError(value)
}
func readString() -> Result<String, AndroidBinderError> {
var ctx = ParcelStringReadContext(buffer: nil)
let status = withUnsafeMutablePointer(to: &ctx) { ctxPtr -> binder_status_t in
AParcel_readString(pointer, ctxPtr) { userData, length, outBuffer -> Bool in
guard let userData = userData else { return false }
let buf = UnsafeMutablePointer<CChar>.allocate(capacity: Int(length) + 1)
buf[Int(length)] = 0
userData.assumingMemoryBound(to: ParcelStringReadContext.self).pointee.buffer = buf
outBuffer?.pointee = buf
return true
}
}
defer { ctx.buffer?.deallocate() }
guard status == STATUS_OK else { return status.mapError(as: String.self) }
guard let buf = ctx.buffer else { return .success("") }
return .success(String(cString: buf))
}
func readStrongBinder() -> Result<AndroidBinder, AndroidBinderError> {
var binderPtr: OpaquePointer? = nil
let status = AParcel_readStrongBinder(pointer, &binderPtr)
guard status == STATUS_OK else { return status.mapError(as: AndroidBinder.self) }
guard let ptr = binderPtr else {
return .failure(AndroidBinderError(AndroidBinderError.ErrorCode.unexpectedNull))
}
return .success(AndroidBinder(ptr))
}
func readFileDescriptor() -> Result<Int32, AndroidBinderError> {
var fd: Int32 = -1
return AParcel_readParcelFileDescriptor(pointer, &fd).mapError(fd)
}
func readStatusHeader() -> Result<Status, AndroidBinderError> {
var statusPtr: OpaquePointer? = nil
let statusCode = AParcel_readStatusHeader(pointer, &statusPtr)
guard statusCode == STATUS_OK else { return statusCode.mapError(as: Status.self) }
guard let ptr = statusPtr else {
return .failure(AndroidBinderError(AndroidBinderError.ErrorCode.unexpectedNull))
}
return .success(Status(ptr))
}
}
// MARK: - Handle: Array Writes
internal extension Parcel.Handle {
func writeByteArray(_ values: [Int8]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeByteArray(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeByteArray(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeByteArray(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeInt32Array(_ values: [Int32]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeInt32Array(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeInt32Array(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeInt32Array(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeUInt32Array(_ values: [UInt32]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeUint32Array(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeUint32Array(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeUint32Array(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeInt64Array(_ values: [Int64]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeInt64Array(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeInt64Array(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeInt64Array(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeUInt64Array(_ values: [UInt64]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeUint64Array(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeUint64Array(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeUint64Array(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeFloatArray(_ values: [Float]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeFloatArray(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeFloatArray(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeFloatArray(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeDoubleArray(_ values: [Double]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeDoubleArray(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeDoubleArray(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeDoubleArray(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeCharArray(_ values: [UInt16]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeCharArray(pointer, nil, -1).mapError()
}
if values.isEmpty {
return AParcel_writeCharArray(pointer, nil, 0).mapError()
}
return values.withUnsafeBufferPointer { buf in
AParcel_writeCharArray(pointer, buf.baseAddress, Int32(values.count)).mapError()
}
}
func writeBoolArray(_ values: [Bool]?) -> Result<Void, AndroidBinderError> {
guard let values = values else {
return AParcel_writeBoolArray(pointer, nil, -1, nil).mapError()
}
if values.isEmpty {
return AParcel_writeBoolArray(pointer, nil, 0, nil).mapError()
}
return values.withUnsafeBufferPointer { buf in
var ctx = ParcelBoolWriteContext(elements: buf.baseAddress)
return withUnsafePointer(to: &ctx) { ctxPtr in
AParcel_writeBoolArray(pointer, ctxPtr, Int32(values.count)) { userData, index -> Bool in
userData!.assumingMemoryBound(to: ParcelBoolWriteContext.self).pointee.elements![Int(index)]
}.mapError()
}
}
}
}
// MARK: - Handle: Array Reads
internal extension Parcel.Handle {
func readByteArray() -> Result<[Int8]?, AndroidBinderError> {
var ctx = ParcelInt8ArrayReadContext(buffer: nil, count: 0, isNull: true)
let status = withUnsafeMutablePointer(to: &ctx) { ctxPtr -> binder_status_t in
AParcel_readByteArray(pointer, ctxPtr) { userData, length, outBuffer -> Bool in
let ctx = userData!.assumingMemoryBound(to: ParcelInt8ArrayReadContext.self)
if length < 0 { return true }
ctx.pointee.isNull = false
if length > 0 {
let buf = UnsafeMutablePointer<Int8>.allocate(capacity: Int(length))
ctx.pointee.buffer = buf
ctx.pointee.count = length
outBuffer?.pointee = buf
}
return true
}
}
defer { ctx.buffer?.deallocate() }
guard status == STATUS_OK else { return status.mapError(as: [Int8]?.self) }
if ctx.isNull { return .success(nil) }
if let buf = ctx.buffer {
return .success(Array(UnsafeBufferPointer(start: buf, count: Int(ctx.count))))
}
return .success([])
}
func readInt32Array() -> Result<[Int32]?, AndroidBinderError> {
var ctx = ParcelInt32ArrayReadContext(buffer: nil, count: 0, isNull: true)
let status = withUnsafeMutablePointer(to: &ctx) { ctxPtr -> binder_status_t in
AParcel_readInt32Array(pointer, ctxPtr) { userData, length, outBuffer -> Bool in
let ctx = userData!.assumingMemoryBound(to: ParcelInt32ArrayReadContext.self)
if length < 0 { return true }
ctx.pointee.isNull = false
if length > 0 {
let buf = UnsafeMutablePointer<Int32>.allocate(capacity: Int(length))
ctx.pointee.buffer = buf
ctx.pointee.count = length
outBuffer?.pointee = buf
}
return true