Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pure kotlin dependency-free Marshaller/Unmarshaller and native #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.11'
ext.kotlin_version = '1.3.20'
repositories {
mavenCentral()
maven {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2019 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package pbandk.conformance

import platform.posix.*
import kotlinx.cinterop.cstr
import kotlinx.cinterop.usePinned
import kotlinx.cinterop.addressOf

actual object Platform {
actual fun stderrPrintln(str: String) {
val strn = str + "\n"
val cstr = strn.cstr
write(2, cstr, cstr.size.toULong())
}

actual fun stdinReadIntLE() = ByteArray(4).let {
if (readBytes(0, it) != 4) null else
it.foldRight(0) { byte, acc ->
(acc shl 8) or (byte.toInt() and 0xff)
}
}

private fun readBytes(fd: Int, arr: ByteArray): Int {
arr.usePinned {
return read(fd, it.addressOf(0), arr.size.toULong()).toInt()
}
}

actual fun stdinReadFull(arr: ByteArray) =
require(readBytes(0, arr) == arr.size) { "Unable to read full byte array" }

actual fun stdoutWriteIntLE(v: Int) =
stdoutWriteFull (ByteArray(4) { (v shr (8 * it)).toByte() })

actual fun stdoutWriteFull(arr: ByteArray) {
arr.usePinned {
write(1, it.addressOf(0), arr.size.toULong())
}
}

actual inline fun <T> doTry(fn: () -> T, errFn: (Any) -> T) = try { fn() } catch (e: Exception) { errFn(e) }
}
4 changes: 4 additions & 0 deletions conformance/conformance-native/src/main/kotlin/pbandk/main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

fun main(args: Array<String>) {
pbandk.conformance.main(args)
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ inline fun debug(fn: () -> String) { if (logDebug) Platform.stderrPrintln(fn())

fun main(args: Array<String>) {
// Read the request from stdin and write response into stdout
Platform.stdoutWriteResponse(runGenerator(Platform.stdinReadRequest()))
try {
Platform.stdoutWriteResponse(runGenerator(Platform.stdinReadRequest()))
} catch (e: Exception) {
Platform.stderrPrintln("Caught exception $e")
throw e
}
}

fun runGenerator(request: CodeGeneratorRequest): CodeGeneratorResponse {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2019 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package pbandk.gen

import pbandk.gen.pb.CodeGeneratorRequest
import pbandk.gen.pb.CodeGeneratorResponse
import platform.posix.*
import kotlinx.cinterop.cstr
import kotlinx.cinterop.usePinned
import kotlinx.cinterop.addressOf

actual object Platform {
actual fun stderrPrintln(str: String) {
val strn = str + "\n"
val cstr = strn.cstr
write(2, cstr, cstr.size.toULong())
}

private fun readBytes(fd: Int): ByteArray {
var ret = byteArrayOf()
val buf = ByteArray(8192)
buf.usePinned {
while (true) {
val res = read(fd, it.addressOf(0), buf.size.toULong())
if (res <= 0)
break
ret += buf.sliceArray(0 until res.toInt())
}
}
return ret
}

actual fun stdinReadRequest(): CodeGeneratorRequest {
val buf = readBytes(0)
try {
val cg = CodeGeneratorRequest.protoUnmarshal(buf)
return cg
} catch (e: Exception) {
stderrPrintln("Unmarshall exception: $e")
throw e
}
}

actual fun stdoutWriteResponse(resp: CodeGeneratorResponse) {
val buf = resp.protoMarshal()
buf.usePinned {
write(1, it.addressOf(0), buf.size.toULong())
}
}

actual fun serviceGenerator(cliParams: Map<String, String>): ServiceGenerator? = null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

fun main(args: Array<String>) {
pbandk.gen.main(args)
}
14 changes: 8 additions & 6 deletions runtime/runtime-common/src/main/kotlin/pbandk/Marshaller.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package pbandk

expect class Marshaller {
interface ByteArrayMarshaller: Marshaller {
fun complete(): ByteArray
}

interface Marshaller {
fun writeTag(tag: Int): Marshaller
fun writeDouble(value: Double)
fun writeFloat(value: Float)
Expand All @@ -28,10 +31,9 @@ expect class Marshaller {
createEntry: (K, V, Map<Int, pbandk.UnknownField>) -> T
)

// May not return a value if wasn't created with allocate
fun complete(): ByteArray?

companion object {
fun allocate(size: Int): Marshaller
fun allocate(size: Int): ByteArrayMarshaller = marshallerAllocate(size)
}
}
}

internal expect fun marshallerAllocate(size: Int): ByteArrayMarshaller
2 changes: 1 addition & 1 deletion runtime/runtime-common/src/main/kotlin/pbandk/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface Message<T : Message<T>> {
operator fun plus(other: T?): T
val protoSize: Int
fun protoMarshal(m: Marshaller)
fun protoMarshal() = Marshaller.allocate(protoSize).also(::protoMarshal).complete()!!
fun protoMarshal() = Marshaller.allocate(protoSize).also(::protoMarshal).complete()

interface Companion<T : Message<T>> {
fun protoUnmarshal(u: Unmarshaller): T
Expand Down
6 changes: 4 additions & 2 deletions runtime/runtime-common/src/main/kotlin/pbandk/Sizer.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pbandk

expect object Sizer {
interface SizerInterface {
fun tagSize(fieldNum: Int): Int
fun doubleSize(value: Double): Int
fun floatSize(value: Float): Int
Expand All @@ -25,4 +25,6 @@ expect object Sizer {
map: Map<K, V>,
createEntry: (K, V, Map<Int, pbandk.UnknownField>) -> T
): Int
}
}

expect object Sizer : SizerInterface
9 changes: 6 additions & 3 deletions runtime/runtime-common/src/main/kotlin/pbandk/Unmarshaller.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package pbandk

expect class Unmarshaller {
interface Unmarshaller {

// 0 means there is no next tag
fun getTotalBytesRead(): Int
fun readTag(): Int
fun readDouble(): Double
fun readFloat(): Float
Expand Down Expand Up @@ -45,6 +46,8 @@ expect class Unmarshaller {
fun unknownFields(): Map<Int, UnknownField>

companion object {
fun fromByteArray(arr: ByteArray): Unmarshaller
fun fromByteArray(arr: ByteArray): Unmarshaller = unmarshallerByteArray(arr)
}
}
}

internal expect fun unmarshallerByteArray(arr: ByteArray): Unmarshaller
5 changes: 3 additions & 2 deletions runtime/runtime-common/src/main/kotlin/pbandk/Util.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pbandk

expect object Util {
interface UtilInterface {
fun stringToUtf8(str: String): ByteArray
fun utf8ToString(bytes: ByteArray): String
}
}
expect object Util : UtilInterface
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package pbandk.impl

import pbandk.Message

/**
* Thrown when a protocol message being parsed is invalid in some way, e.g. it contains a malformed
* varint or a negative byte length.
*
* @author [email protected] Kenton Varda
*/
open class InvalidProtocolBufferException : Exception {
var unfinishedMessage: Message<*>? = null

constructor(description: String) : super(description)

constructor(e: Exception) : super(e.message, e)

constructor(description: String, e: Exception) : super(description, e) {}

/** Exception indicating that and unexpected wire type was encountered for a field. */
class InvalidWireTypeException(description: String)
: InvalidProtocolBufferException(description)

companion object {
internal fun truncatedMessage(): InvalidProtocolBufferException {
return InvalidProtocolBufferException(
"While parsing a protocol message, the input ended unexpectedly "
+ "in the middle of a field. This could mean either that the "
+ "input has been truncated or that an embedded message "
+ "misreported its own length.")
}

internal fun negativeSize(): InvalidProtocolBufferException {
return InvalidProtocolBufferException(
("CodedInputStream encountered an embedded string or message " + "which claimed to have negative size."))
}

internal fun malformedVarint(): InvalidProtocolBufferException {
return InvalidProtocolBufferException("CodedInputStream encountered a malformed varint.")
}

internal fun invalidTag(): InvalidProtocolBufferException {
return InvalidProtocolBufferException("Protocol message contained an invalid tag (zero).")
}

internal fun invalidEndTag(): InvalidProtocolBufferException {
return InvalidProtocolBufferException(
"Protocol message end-group tag did not match expected tag.")
}

internal fun invalidWireType(): InvalidWireTypeException {
return InvalidWireTypeException("Protocol message tag had invalid wire type.")
}

internal fun recursionLimitExceeded(): InvalidProtocolBufferException {
return InvalidProtocolBufferException(
("Protocol message had too many levels of nesting. May be malicious. " + "Use CodedInputStream.setRecursionLimit() to increase the depth limit."))
}

internal fun sizeLimitExceeded(): InvalidProtocolBufferException {
return InvalidProtocolBufferException(
("Protocol message was too large. May be malicious. " + "Use CodedInputStream.setSizeLimit() to increase the size limit."))
}

internal fun parseFailure(): InvalidProtocolBufferException {
return InvalidProtocolBufferException("Failed to parse the message.")
}

internal fun invalidUtf8(): InvalidProtocolBufferException {
return InvalidProtocolBufferException("Protocol message had invalid UTF-8.")
}
}
}
Loading