@@ -30,6 +30,10 @@ open class AndroidInjection {
3030
3131 static var injectionNumber = 0
3232
33+ open class func connectAndRun( ) {
34+ connectAndRun ( forMainThread: { $0 ( ) } )
35+ }
36+
3337 open class func connectAndRun( forMainThread: @escaping ( @escaping ( ) -> ( ) ) -> ( ) ) {
3438 if androidInjectionHost == " NNN.NNN.NNN.NNN " {
3539 NSLog ( " Injection: AndroidInjectionHost.swift has not been updated, please build again. " )
@@ -88,17 +92,17 @@ open class AndroidInjection {
8892 var value : Int32 = Int32 ( INJECTION_PORT)
8993 let valueLength = MemoryLayout . size ( ofValue: value)
9094 if serverWrite == nil || fwrite ( & value, 1 , valueLength, serverWrite) != valueLength {
91- NSLog ( " Injection: Could not write magic to %d %p : %s " , serverSocket, serverWrite! , strerror ( errno) )
95+ NSLog ( " Injection: Could not write magic to %d: %s " , serverSocket, strerror ( errno) )
9296 return
9397 }
9498
9599 if !#file. withCString ( {
96100 filepath in
97- value = Int32 ( strlen ( filepath) )
101+ value = Int32 ( strlen ( filepath) + 1 )
98102 return fwrite ( & value, 1 , valueLength, serverWrite) == valueLength &&
99103 fwrite ( filepath, 1 , Int ( value) , serverWrite) == value
100104 } ) {
101- NSLog ( " Injection: Could not write filepath to %d %p : %s " , serverSocket, serverWrite! , strerror ( errno) )
105+ NSLog ( " Injection: Could not write filepath to %d: %s " , serverSocket, strerror ( errno) )
102106 return
103107 }
104108 fflush ( serverWrite)
@@ -109,45 +113,57 @@ open class AndroidInjection {
109113 while fread ( & compressedLength, 1 , valueLength, serverRead) == valueLength &&
110114 fread ( & uncompressedLength, 1 , valueLength, serverRead) == valueLength,
111115 var compressedBuffer = malloc ( Int ( compressedLength) ) ,
112- var uncompressedBuffer = malloc ( Int ( uncompressedLength) ) ,
113- fread ( compressedBuffer, 1 , Int ( compressedLength) , serverRead) == compressedLength {
116+ var uncompressedBuffer = malloc ( Int ( uncompressedLength) ) {
114117 defer {
115118 free ( compressedBuffer)
116119 free ( uncompressedBuffer)
117120 }
118- if compressedLength == valueLength && uncompressedLength == valueLength {
121+
122+ if compressedLength == 1 && uncompressedLength == 1 {
119123 continue
120124 }
121125
126+ if fread ( compressedBuffer, 1 , Int ( compressedLength) , serverRead) != compressedLength {
127+ NSLog ( " Injection: Could not read %d compressed bytes: %s " ,
128+ compressedLength, strerror ( errno) )
129+ break
130+ }
131+
122132 NSLog ( " Injection: received %d/%d bytes " , compressedLength, uncompressedLength)
123133
124- var destLen = uLongf ( uncompressedLength)
125- if uncompress ( uncompressedBuffer. assumingMemoryBound ( to: Bytef . self) , & destLen,
126- compressedBuffer. assumingMemoryBound ( to: Bytef . self) ,
127- uLong ( compressedLength) ) != Z_OK || destLen != uLongf ( uncompressedLength) {
128- NSLog ( " Injection: uncompression failure " )
129- break
134+ let libraryPath : String
135+ if uncompressedLength == 1 {
136+ libraryPath = String ( cString: compressedBuffer. assumingMemoryBound ( to: UInt8 . self) )
130137 }
138+ else {
139+ var destLen = uLongf ( uncompressedLength)
140+ if uncompress ( uncompressedBuffer. assumingMemoryBound ( to: Bytef . self) , & destLen,
141+ compressedBuffer. assumingMemoryBound ( to: Bytef . self) ,
142+ uLong ( compressedLength) ) != Z_OK || destLen != uLongf ( uncompressedLength) {
143+ NSLog ( " Injection: uncompression failure " )
144+ break
145+ }
131146
132- AndroidInjection . injectionNumber += 1
133- let libraryPath = NSTemporaryDirectory ( ) + " injection \( AndroidInjection . injectionNumber) .so "
134- let libraryFILE = fopen ( libraryPath, " w " )
135- if libraryFILE == nil ||
136- fwrite ( uncompressedBuffer, 1 , Int ( uncompressedLength) , libraryFILE) != uncompressedLength {
137- NSLog ( " Injection: Could not write library file " )
138- break
147+ AndroidInjection . injectionNumber += 1
148+ libraryPath = NSTemporaryDirectory ( ) + " injection \( AndroidInjection . injectionNumber) .so "
149+ let libraryFILE = fopen ( libraryPath, " w " )
150+ if libraryFILE == nil ||
151+ fwrite ( uncompressedBuffer, 1 , Int ( uncompressedLength) , libraryFILE) != uncompressedLength {
152+ NSLog ( " Injection: Could not write library file " )
153+ break
154+ }
155+ fclose ( libraryFILE)
139156 }
140- fclose ( libraryFILE)
141157
142158 forMainThread ( {
143- NSLog ( " Injection: Wrote to \( libraryPath) , injecting ..." )
159+ NSLog ( " Injection: injecting \( libraryPath) ... " )
144160 let error = loadAndInject ( library: libraryPath)
145- var status = Int32 ( error == nil ? 0 : strlen ( error) )
161+ var status = Int32 ( error == nil ? 0 : strlen ( error) + 1 )
146162 if fwrite ( & status, 1 , valueLength, serverWrite) != valueLength {
147- NSLog ( " Injection: Could not write status " )
163+ NSLog ( " Injection: Could not write status: %s " , strerror ( errno ) )
148164 }
149165 if error != nil && fwrite ( error, 1 , Int ( status) , serverWrite) != status {
150- NSLog ( " Injection: Could not write error string " )
166+ NSLog ( " Injection: Could not write error string: %s " , strerror ( errno ) )
151167 }
152168 fflush ( serverWrite)
153169 NSLog ( " Injection complete. " )
@@ -256,7 +272,8 @@ open class AndroidInjection {
256272 }
257273
258274 let vtableOffset = byteAddr ( & existingClass. pointee. IVarDestroyer) - byteAddr( existingClass)
259- let vtableLength = Int ( existingClass. pointee. ClassSize) - pointerSize * 2 - vtableOffset
275+ let vtableLength = Int ( existingClass. pointee. ClassSize -
276+ existingClass. pointee. ClassAddressPoint) - vtableOffset
260277 NSLog ( " \( unsafeBitCast ( classMetadata, to: AnyClass . self) ) , vtable length: \( vtableLength) " )
261278 memcpy ( byteAddr ( existingClass) + vtableOffset, byteAddr ( classMetadata) + vtableOffset, vtableLength)
262279 }
0 commit comments