@@ -101,6 +101,25 @@ public enum CompactImageMapFormat {
101101 return value
102102 }
103103
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+
104123 mutating func decodeAddress( _ count: Int ) -> UInt64 ? {
105124 var word : UInt64
106125 guard let firstByte = iterator. next ( ) else {
@@ -250,7 +269,7 @@ public enum CompactImageMapFormat {
250269 }
251270 }
252271
253- mutating func decode( ) -> ( [ ImageMap . Image ] , ImageMap . WordSize ) ? {
272+ mutating func decode( ) -> ( String , [ ImageMap . Image ] , ImageMap . WordSize ) ? {
254273 // Check the version and decode the size
255274 guard let infoByte = iterator. next ( ) else {
256275 return nil
@@ -274,6 +293,11 @@ public enum CompactImageMapFormat {
274293 wordMask = 0xffffffffffffff00
275294 }
276295
296+ // Now decode the platform
297+ guard let platform = decodeString ( ) else {
298+ return nil
299+ }
300+
277301 // Next is the image count
278302 guard let count = decodeCount ( ) else {
279303 return nil
@@ -392,7 +416,7 @@ public enum CompactImageMapFormat {
392416 wsMap = . sixtyFourBit
393417 }
394418
395- return ( images, wsMap)
419+ return ( platform , images, wsMap)
396420 }
397421 }
398422
@@ -414,6 +438,7 @@ public enum CompactImageMapFormat {
414438 public struct Iterator : IteratorProtocol {
415439 enum State {
416440 case start
441+ case platform( Int )
417442 case count( Int )
418443 case image
419444 case baseAddress( Int )
@@ -483,14 +508,39 @@ public enum CompactImageMapFormat {
483508 size = . sixtyFourBit
484509 }
485510
486- let count = source. images. count
487- let bits = Int . bitWidth - count. leadingZeroBitCount
488- state = . count( 7 * ( bits / 7 ) )
511+ state = . platform( - 1 )
489512
490513 let version : UInt8 = 0
491514 let infoByte = ( version << 2 ) | size. rawValue
492515 return infoByte
493516
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+
494544 case let . count( ndx) :
495545 let count = source. images. count
496546 let byte = UInt8 ( truncatingIfNeeded: ( count >> ndx) & 0x7f )
0 commit comments