Skip to content

Instantly share code, notes, and snippets.

Created November 28, 2017 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/273e1a4e01daece84e48523da9dc82a8 to your computer and use it in GitHub Desktop.
Save anonymous/273e1a4e01daece84e48523da9dc82a8 to your computer and use it in GitHub Desktop.
import kotlinx.cinterop.*
import platform.posix.*
fun encodeB(ss : String):String {
var s = ss
println("S length: ${s.length}")
var c = s.length % 3
println("C init: ${c}")
c = 0
while (c < s.length)
{
println("C before ${c}")
c += 3
println("C after ${c}")
if (c >= 9) {
println("CC is now ${c}")
}
}
return ""
}
fun writeBinaryFile(file: String, outData: ByteArray) {
println("Writing to file: " + file)
val fd = open(file, O_CREAT or O_WRONLY or O_TRUNC, S_IRWXU)
val bytes = write(fd, outData.toCValues(), outData.size.signExtend<size_t>())
close(fd)
if (bytes < 0) {
println("Error writing file...$file")
} else {
println("Finished writing to file. Bytes: " + bytes)
}
}
fun readFileData(path: String): ByteArray? {
return memScoped {
val info = alloc<stat>()
if (stat(path, info.ptr) != 0) return null
// We're not planning to serve files > 4G, right?
val size = info.st_size.toInt()
val result = ByteArray(size)
val file = fopen(path, "rb")
if (file == null) return null
var position = 0
while (position < size) {
val toRead = minOf(size - position, 4096)
val read = fread(result.refTo(position), 1, toRead.signExtend(), file).toInt()
if (read <= 0) break
position += read
}
fclose(file)
result
}
}
fun main(args: Array<String>) {
println("I'm here!")
// echo BROKEN > /tmp/broken.txt
// cd /tmp && gpg -c broken.txt
// I use passphrase of single char b above
// Now, run this program. Almost every time, I get a core dump
var contents = readFileData("/tmp/broken.txt.gpg")!!
var contentAsString = kotlin.text.fromUtf8Array(contents!!, 0, contents!!.size)
var encoded = encodeB(contentAsString)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment