chore: KMP audit — commonize code, centralize utilities, eliminate dead abstractions (#5133)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
James Rich 2026-04-14 21:17:50 -05:00 committed by GitHub
parent 50ade01e55
commit 72b981f73b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
132 changed files with 2186 additions and 916 deletions

View file

@ -20,47 +20,41 @@ package org.meshtastic.core.takserver
import kotlin.time.Instant
fun CoTMessage.toXml(): String {
val sb = StringBuilder()
sb.append(
fun CoTMessage.toXml(): String = buildString {
append(
"<?xml version='1.0' encoding='UTF-8' standalone='yes'?><event version='2.0' uid='${uid.xmlEscaped()}' type='$type' time='${time.toXmlString()}' start='${start.toXmlString()}' stale='${stale.toXmlString()}' how='$how'><point lat='$latitude' lon='$longitude' hae='$hae' ce='$ce' le='$le'/><detail>",
)
contact?.let {
sb.append(
append(
"<contact endpoint='${it.endpoint ?: DEFAULT_TAK_ENDPOINT}' callsign='${it.callsign.xmlEscaped()}'/><uid Droid='${it.callsign.xmlEscaped()}'/>",
)
}
group?.let { sb.append("<__group role='${it.role.xmlEscaped()}' name='${it.name.xmlEscaped()}'/>") }
group?.let { append("<__group role='${it.role.xmlEscaped()}' name='${it.name.xmlEscaped()}'/>") }
status?.let { sb.append("<status battery='${it.battery}'/>") }
status?.let { append("<status battery='${it.battery}'/>") }
track?.let { sb.append("<track course='${it.course}' speed='${it.speed}'/>") }
track?.let { append("<track course='${it.course}' speed='${it.speed}'/>") }
if (chat != null) {
val senderUid = uid.geoChatSenderUid()
val messageId = uid.geoChatMessageId()
sb.append(
append(
"<__chat parent='RootContactGroup' groupOwner='false' messageId='$messageId' chatroom='${chat.chatroom.xmlEscaped()}' id='${chat.chatroom.xmlEscaped()}' senderCallsign='${chat.senderCallsign?.xmlEscaped() ?: ""}'><chatgrp uid0='${senderUid.xmlEscaped()}' uid1='${chat.chatroom.xmlEscaped()}' id='${chat.chatroom.xmlEscaped()}'/></__chat>",
)
sb.append("<link uid='${senderUid.xmlEscaped()}' type='a-f-G-U-C' relation='p-p'/>")
sb.append("<__serverdestination destinations='0.0.0.0:4242:tcp:${senderUid.xmlEscaped()}'/>")
sb.append(
append("<link uid='${senderUid.xmlEscaped()}' type='a-f-G-U-C' relation='p-p'/>")
append("<__serverdestination destinations='0.0.0.0:4242:tcp:${senderUid.xmlEscaped()}'/>")
append(
"<remarks source='BAO.F.ATAK.${senderUid.xmlEscaped()}' to='${chat.chatroom.xmlEscaped()}' time='${time.toXmlString()}'>${chat.message.xmlEscaped()}</remarks>",
)
} else if (!remarks.isNullOrEmpty()) {
sb.append("<remarks>${remarks.xmlEscaped()}</remarks>")
append("<remarks>${remarks.xmlEscaped()}</remarks>")
}
rawDetailXml?.let {
if (it.isNotEmpty()) {
sb.append(it)
}
}
rawDetailXml?.takeIf { it.isNotEmpty() }?.let { append(it) }
sb.append("</detail></event>")
return sb.toString()
append("</detail></event>")
}
private fun Instant.toXmlString(): String = this.toString()

View file

@ -16,12 +16,16 @@
*/
package org.meshtastic.core.takserver.fountain
import okio.ByteString.Companion.toByteString
internal expect object ZlibCodec {
fun compress(data: ByteArray): ByteArray?
fun decompress(data: ByteArray): ByteArray?
}
internal expect object CryptoCodec {
fun sha256Prefix8(data: ByteArray): ByteArray
internal object CryptoCodec {
private const val PREFIX_SIZE = 8
fun sha256Prefix8(data: ByteArray): ByteArray = data.toByteString().sha256().toByteArray().copyOf(PREFIX_SIZE)
}

View file

@ -24,8 +24,6 @@ import kotlinx.cinterop.ptr
import kotlinx.cinterop.reinterpret
import kotlinx.cinterop.usePinned
import kotlinx.cinterop.value
import platform.CoreCrypto.CC_SHA256
import platform.CoreCrypto.CC_SHA256_DIGEST_LENGTH
import platform.zlib.Z_BUF_ERROR
import platform.zlib.Z_OK
import platform.zlib.compress
@ -105,20 +103,3 @@ internal actual object ZlibCodec {
return null
}
}
internal actual object CryptoCodec {
@OptIn(ExperimentalForeignApi::class)
actual fun sha256Prefix8(data: ByteArray): ByteArray {
val digest = ByteArray(CC_SHA256_DIGEST_LENGTH)
if (data.isNotEmpty()) {
data.usePinned { dataPin ->
digest.usePinned { digestPin ->
CC_SHA256(dataPin.addressOf(0), data.size.toUInt(), digestPin.addressOf(0).reinterpret())
}
}
} else {
digest.usePinned { digestPin -> CC_SHA256(null, 0u, digestPin.addressOf(0).reinterpret()) }
}
return digest.copyOf(8)
}
}

View file

@ -17,7 +17,6 @@
package org.meshtastic.core.takserver.fountain
import java.io.ByteArrayOutputStream
import java.security.MessageDigest
import java.util.zip.Deflater
import java.util.zip.Inflater
@ -66,10 +65,3 @@ internal actual object ZlibCodec {
}
}
}
internal actual object CryptoCodec {
actual fun sha256Prefix8(data: ByteArray): ByteArray {
val digest = MessageDigest.getInstance("SHA-256")
return digest.digest(data).copyOf(8)
}
}