diff --git a/app/src/main/java/com/bitchat/android/mesh/FragmentManager.kt b/app/src/main/java/com/bitchat/android/mesh/FragmentManager.kt index 3a10f998..ada67f92 100644 --- a/app/src/main/java/com/bitchat/android/mesh/FragmentManager.kt +++ b/app/src/main/java/com/bitchat/android/mesh/FragmentManager.kt @@ -3,7 +3,6 @@ package com.bitchat.android.mesh import android.util.Log import com.bitchat.android.protocol.BitchatPacket import com.bitchat.android.protocol.MessageType -import com.bitchat.android.protocol.MessagePadding import com.bitchat.android.model.FragmentPayload import kotlinx.coroutines.* import java.util.concurrent.ConcurrentHashMap @@ -66,20 +65,19 @@ class FragmentManager { Log.w(TAG, "Rejecting invalid outbound fragment limit: $maxFragments") return emptyList() } - val encoded = packet.toBinaryData() - if (encoded == null) { + // Fragment the unpadded frame; each fragment will be encoded (and padded) independently - iOS fix. + // Ask the encoder to skip padding rather than un-padding afterwards: + // pad() declines a request needing more than 255 bytes and + // optimalBlockSize() returns the input size above 2048, so frames of + // 513-768, 1009-1792 and >=2033 bytes are never padded. Those end + // with the last byte of the signature, and unpad() reads a value of + // 0x01 as a one-byte pad and strips a real signature byte. + val fullData = packet.toBinaryData(padding = false) + if (fullData == null) { Log.e(TAG, "Failed to encode packet to binary data") return emptyList() } - // Fragment the unpadded frame; each fragment will be encoded (and padded) independently - iOS fix - val fullData = try { - MessagePadding.unpad(encoded) - } catch (e: Exception) { - Log.e(TAG, "Failed to unpad data: ${e.message}", e) - return emptyList() - } - // iOS logic: if data.count > 512 && packet.type != MessageType.fragment.rawValue if (fullData.size <= FRAGMENT_SIZE_THRESHOLD) { return listOf(packet) // No fragmentation needed