@@ -101,6 +101,25 @@ public enum CompactImageMapFormat {
101
101
return value
102
102
}
103
103
104
+ mutating func decodeString( ) -> String ? {
105
+ guard let utf8Length = iterator. next ( ) else {
106
+ return nil
107
+ }
108
+
109
+ var bytes : [ UInt8 ] = [ ]
110
+ bytes. reserveCapacity ( Int ( utf8Length) )
111
+
112
+ for _ in 0 ..< utf8Length {
113
+ guard let byte = iterator. next ( ) else {
114
+ return nil
115
+ }
116
+
117
+ bytes. append ( byte)
118
+ }
119
+
120
+ return String ( decoding: bytes, as: UTF8 . self)
121
+ }
122
+
104
123
mutating func decodeAddress( _ count: Int ) -> UInt64 ? {
105
124
var word : UInt64
106
125
guard let firstByte = iterator. next ( ) else {
@@ -250,7 +269,7 @@ public enum CompactImageMapFormat {
250
269
}
251
270
}
252
271
253
- mutating func decode( ) -> ( [ ImageMap . Image ] , ImageMap . WordSize ) ? {
272
+ mutating func decode( ) -> ( String , [ ImageMap . Image ] , ImageMap . WordSize ) ? {
254
273
// Check the version and decode the size
255
274
guard let infoByte = iterator. next ( ) else {
256
275
return nil
@@ -274,6 +293,11 @@ public enum CompactImageMapFormat {
274
293
wordMask = 0xffffffffffffff00
275
294
}
276
295
296
+ // Now decode the platform
297
+ guard let platform = decodeString ( ) else {
298
+ return nil
299
+ }
300
+
277
301
// Next is the image count
278
302
guard let count = decodeCount ( ) else {
279
303
return nil
@@ -392,7 +416,7 @@ public enum CompactImageMapFormat {
392
416
wsMap = . sixtyFourBit
393
417
}
394
418
395
- return ( images, wsMap)
419
+ return ( platform , images, wsMap)
396
420
}
397
421
}
398
422
@@ -414,6 +438,7 @@ public enum CompactImageMapFormat {
414
438
public struct Iterator : IteratorProtocol {
415
439
enum State {
416
440
case start
441
+ case platform( Int )
417
442
case count( Int )
418
443
case image
419
444
case baseAddress( Int )
@@ -483,14 +508,39 @@ public enum CompactImageMapFormat {
483
508
size = . sixtyFourBit
484
509
}
485
510
486
- let count = source. images. count
487
- let bits = Int . bitWidth - count. leadingZeroBitCount
488
- state = . count( 7 * ( bits / 7 ) )
511
+ state = . platform( - 1 )
489
512
490
513
let version : UInt8 = 0
491
514
let infoByte = ( version << 2 ) | size. rawValue
492
515
return infoByte
493
516
517
+ case let . platform( ndx) :
518
+ let length = UInt8 ( source. platform. utf8. count)
519
+ let byte : UInt8
520
+
521
+ if ndx == - 1 {
522
+ // The length byte comes first
523
+ byte = length
524
+ } else {
525
+ byte = source. platform. utf8 [
526
+ source. platform. utf8. index (
527
+ source. platform. utf8. startIndex,
528
+ offsetBy: ndx
529
+ )
530
+ ]
531
+ }
532
+
533
+ // If we're done, move to the .count state
534
+ if ndx + 1 == length {
535
+ let count = source. images. count
536
+ let bits = Int . bitWidth - count. leadingZeroBitCount
537
+ state = . count( 7 * ( bits / 7 ) )
538
+ } else {
539
+ state = . platform( ndx + 1 )
540
+ }
541
+
542
+ return byte
543
+
494
544
case let . count( ndx) :
495
545
let count = source. images. count
496
546
let byte = UInt8 ( truncatingIfNeeded: ( count >> ndx) & 0x7f )
0 commit comments