Skip to content

Commit

Permalink
fix: detect stream correctly in presence of whitespace
Browse files Browse the repository at this point in the history
Signed-off-by: Aditya Wasan <[email protected]>
  • Loading branch information
Skrilltrax authored and msfjarvis committed Oct 12, 2023
1 parent e29d889 commit 4f4b4ea
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 5 additions & 3 deletions src/kotlin/kage/Age.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,16 @@ public object Age {
val markSupportedStream =
if (srcStream.markSupported()) srcStream else BufferedInputStream(srcStream)

markSupportedStream.mark(ArmorInputStream.HEADER.length)
// Check if the InputStream contains whitespace + header
val readLimit = ArmorInputStream.MAX_WHITESPACE + ArmorInputStream.HEADER.length
markSupportedStream.mark(readLimit)

val headerStr = markSupportedStream.readNBytes(ArmorInputStream.HEADER.length).decodeToString()
val initialString = markSupportedStream.readNBytes(readLimit).decodeToString()

markSupportedStream.reset()

val decodedStream =
if (headerStr.startsWith(ArmorInputStream.HEADER_START)) {
if (initialString.contains(ArmorInputStream.HEADER_START)) {
ArmorInputStream(markSupportedStream)
} else markSupportedStream

Expand Down
3 changes: 1 addition & 2 deletions src/kotlin/kage/crypto/stream/ArmorInputStream.kt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ internal class ArmorInputStream(src: InputStream) : InputStream() {

const val COLUMNS_PER_LINE = 64
const val BYTES_PER_LINE = COLUMNS_PER_LINE / 4 * 3

private const val MAX_WHITESPACE = 1024
const val MAX_WHITESPACE = 1024
}
}

0 comments on commit 4f4b4ea

Please sign in to comment.