Skip to content

Commit

Permalink
Remove null terminator from Base38 String in kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Jun 30, 2023
1 parent a8d7e41 commit d5bae01
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 10 deletions.
8 changes: 1 addition & 7 deletions src/controller/java/src/chip/onboardingpayload/Base38.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fun base38Encode(inBuf: ByteArray, outBuf: CharArray): Unit {

val base38CharactersNeeded = kBase38CharactersNeededInNBytesChunk[bytesInChunk - 1].toByte()

if ((outIdx + base38CharactersNeeded) >= outBuf.size) {
if ((outIdx + base38CharactersNeeded) > outBuf.size) {
throw OnboardingPayloadException("Buffer is too small")
}

Expand All @@ -63,12 +63,6 @@ fun base38Encode(inBuf: ByteArray, outBuf: CharArray): Unit {
value /= kRadix.toInt()
}
}

if (outIdx < outBuf.size) {
outBuf[outIdx] = '\u0000'
} else {
throw OnboardingPayloadException("Buffer is too small")
}
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class QRCodeBasicOnboardingPayloadGenerator(private val payload: OnboardingPaylo

/**
* This function is called to encode the binary data of a payload to a
* base38 null-terminated string.
* base38 string.
*
* The resulting size of the outBuffer span will be the size of data written.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,11 @@ class QRCodeOnboardingPayloadGenerator(private val onboardingPayload: Onboarding
}

var tlvDataLengthInBytes = generateTLVFromOptionalData(onboardingPayload, tlvDataStart, tlvDataStartSize)

val bits = ByteArray(kTotalPayloadDataSizeInBytes + tlvDataLengthInBytes)
val buffer = CharArray(base38EncodedLength(bits.size) + kQRCodePrefix.length)
payloadBase38RepresentationWithTLV(onboardingPayload, buffer, bits, tlvDataStart, tlvDataLengthInBytes)

return buffer.toString()
return String(buffer)
}

private fun generateTLVFromOptionalData(
Expand Down

0 comments on commit d5bae01

Please sign in to comment.