-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathSerialization.kt
262 lines (228 loc) · 8.39 KB
/
Serialization.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package nl.tudelft.ipv8.messaging
import java.nio.Buffer
import java.nio.ByteBuffer
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.full.memberProperties
import kotlin.reflect.full.primaryConstructor
const val SERIALIZED_USHORT_SIZE = 2
const val SERIALIZED_UINT_SIZE = 4
const val SERIALIZED_INT_SIZE = 4
const val SERIALIZED_ULONG_SIZE = 8
const val SERIALIZED_LONG_SIZE = 8
const val SERIALIZED_UBYTE_SIZE = 1
const val SERIALIZED_PUBLIC_KEY_SIZE = 74
const val HASH_SIZE = 32
const val SIGNATURE_SIZE = 64
const val SERIALIZED_SHA1_HASH_SIZE = 20
interface Serializable {
fun serialize(): ByteArray
}
interface Deserializable<T> {
fun deserialize(buffer: ByteArray, offset: Int = 0): Pair<T, Int>
}
/**
* Serializes the object and returns the buffer.
* Alternative to manually defining the serialize function.
*/
interface AutoSerializable : Serializable {
override fun serialize(): ByteArray {
return this::class.primaryConstructor!!.parameters.map { param ->
val value =
this.javaClass.kotlin.memberProperties.find { it.name == param.name }!!.get(this)
simpleSerialize(value)
}.reduce { acc, bytes -> acc + bytes }
}
}
///**
// * Deserializes the object from the buffer and returns the object and the new offset.
// * Alternative to manually defining the deserialize function.
// */
//inline fun <reified T> Deserializable<T>.autoDeserialize(buffer: ByteArray, offset: Int = 0): Pair<T, Int> {
// TODO()
//}
fun <U> simpleSerialize(data: U): ByteArray {
return when (data) {
is Int -> serializeInt(data)
is Long -> serializeLong(data)
is UByte -> serializeUChar(data)
is UInt -> serializeUInt(data)
is UShort -> serializeUShort(data.toInt())
is ULong -> serializeULong(data)
is String -> serializeVarLen(data.toByteArray())
is ByteArray -> serializeVarLen(data)
is Boolean -> serializeBool(data)
is Enum<*> -> serializeUInt(data.ordinal.toUInt())
is Serializable -> data.serialize()
else -> throw IllegalArgumentException("Unsupported serialization type")
}
}
inline fun <reified U> simpleDeserialize(buffer: ByteArray, offset: Int = 0): Pair<U, Int> {
val (value, off) = when (U::class) {
Int::class -> Pair(deserializeInt(buffer, offset) as U, SERIALIZED_INT_SIZE)
Long::class -> Pair(deserializeLong(buffer, offset) as U, SERIALIZED_LONG_SIZE)
UByte::class -> Pair(deserializeUChar(buffer, offset) as U, SERIALIZED_UBYTE_SIZE)
UShort::class -> Pair(
deserializeUShort(buffer, offset).toUShort() as U,
SERIALIZED_USHORT_SIZE
)
UInt::class -> Pair(deserializeUInt(buffer, offset) as U, SERIALIZED_UINT_SIZE)
ULong::class -> Pair(deserializeULong(buffer, offset) as U, SERIALIZED_ULONG_SIZE)
String::class -> {
val (data, len) = deserializeVarLen(buffer, offset)
Pair(data.decodeToString() as U, len)
}
ByteArray::class -> {
val (data, len) = deserializeVarLen(buffer, offset)
Pair(data as U, len)
}
Boolean::class -> Pair(deserializeBool(buffer, offset) as U, 1)
else -> {
if (U::class.isSubclassOf(Enum::class)) {
val ordinal = deserializeUInt(buffer, offset).toInt()
val values = U::class.java.enumConstants
Pair(values[ordinal] as U, SERIALIZED_UINT_SIZE)
} else throw IllegalArgumentException("Unsupported deserialization type")
}
}
return (value to (offset + off))
}
fun serializeBool(data: Boolean): ByteArray {
val value = if (data) 1 else 0
val array = ByteArray(1)
array[0] = value.toByte()
return array
}
fun deserializeBool(buffer: ByteArray, offset: Int = 0): Boolean {
return buffer[offset].toInt() > 0
}
fun serializeUShort(value: Int): ByteArray {
val bytes = ByteArray(SERIALIZED_USHORT_SIZE)
bytes[1] = (value and 0xFF).toByte()
bytes[0] = ((value shr 8) and 0xFF).toByte()
return bytes
}
fun serializeUShort(value: UShort): ByteArray {
val bytes = ByteBuffer.allocate(SERIALIZED_USHORT_SIZE)
bytes.putShort(value.toShort())
return bytes.array()
}
fun deserializeUShort(buffer: ByteArray, offset: Int = 0): Int {
return (((buffer[offset].toInt() and 0xFF) shl 8) or (buffer[offset + 1].toInt() and 0xFF))
}
fun deserializeRealUShort(buffer: ByteArray, offset: Int = 0): UShort {
val buf = ByteBuffer.wrap(buffer, offset, SERIALIZED_USHORT_SIZE)
return buf.short.toUShort()
}
fun serializeUInt(value: UInt): ByteArray {
val bytes = UByteArray(SERIALIZED_UINT_SIZE)
for (i in 0 until SERIALIZED_UINT_SIZE) {
bytes[i] = ((value shr ((7 - i) * 8)) and 0xFFu).toUByte()
}
return bytes.toByteArray()
}
fun deserializeUInt(buffer: ByteArray, offset: Int = 0): UInt {
val ubuffer = buffer.toUByteArray()
var result = 0u
for (i in 0 until SERIALIZED_UINT_SIZE) {
result = (result shl 8) or ubuffer[offset + i].toUInt()
}
return result
}
fun serializeULong(value: ULong): ByteArray {
val bytes = UByteArray(SERIALIZED_ULONG_SIZE)
for (i in 0 until SERIALIZED_ULONG_SIZE) {
bytes[i] = ((value shr ((7 - i) * 8)) and 0xFFu).toUByte()
}
return bytes.toByteArray()
}
fun deserializeULong(buffer: ByteArray, offset: Int = 0): ULong {
val ubuffer = buffer.toUByteArray()
var result = 0uL
for (i in 0 until SERIALIZED_ULONG_SIZE) {
result = (result shl 8) or ubuffer[offset + i].toULong()
}
return result
}
fun serializeLong(value: Long): ByteArray {
val buffer = ByteBuffer.allocate(SERIALIZED_LONG_SIZE)
buffer.putLong(value)
return buffer.array()
}
fun deserializeLong(bytes: ByteArray, offset: Int = 0): Long {
val buffer = ByteBuffer.allocate(SERIALIZED_LONG_SIZE)
buffer.put(bytes.copyOfRange(offset, offset + SERIALIZED_LONG_SIZE))
// In JDK 8 this returns a Buffer.
(buffer as Buffer).flip()
return buffer.long
}
fun serializeInt(value: Int): ByteArray {
val buffer = ByteBuffer.allocate(SERIALIZED_INT_SIZE)
buffer.putInt(value)
return buffer.array()
}
fun deserializeInt(bytes: ByteArray, offset: Int = 0): Int {
val buffer = ByteBuffer.allocate(SERIALIZED_INT_SIZE)
buffer.put(bytes.copyOfRange(offset, offset + SERIALIZED_INT_SIZE))
buffer.flip()
return buffer.int
}
fun serializeUChar(char: UByte): ByteArray {
return byteArrayOf(char.toByte())
}
fun deserializeUChar(buffer: ByteArray, offset: Int = 0): UByte {
val ubuffer = buffer.toUByteArray()
return ubuffer[offset]
}
fun serializeVarLen(bytes: ByteArray): ByteArray {
return serializeUInt(bytes.size.toUInt()) + bytes
}
fun deserializeVarLen(buffer: ByteArray, offset: Int = 0): Pair<ByteArray, Int> {
val len = deserializeUInt(buffer, offset).toInt()
val payload = buffer.copyOfRange(
offset + SERIALIZED_UINT_SIZE,
offset + SERIALIZED_UINT_SIZE + len
)
return Pair(payload, SERIALIZED_UINT_SIZE + len)
}
fun deserializeRecursively(buffer: ByteArray, offset: Int = 0): Array<ByteArray> {
if (buffer.isEmpty()) {
return arrayOf()
}
val len = deserializeUInt(buffer, offset).toInt()
val payload = buffer.copyOfRange(
offset + SERIALIZED_UINT_SIZE,
offset + SERIALIZED_UINT_SIZE + len
)
return arrayOf(payload) + deserializeRecursively(
buffer.copyOfRange(
offset + SERIALIZED_UINT_SIZE + len,
buffer.size
), offset
)
}
fun deserializeAmount(
buffer: ByteArray,
amount: Int,
offset: Int = 0
): Pair<Array<ByteArray>, ByteArray> {
val returnValues = arrayListOf<ByteArray>()
var localOffset = offset
for (i in 0 until amount) {
val len = deserializeUInt(buffer, localOffset).toInt()
val payload = buffer.copyOfRange(
localOffset + SERIALIZED_UINT_SIZE,
localOffset + SERIALIZED_UINT_SIZE + len
)
localOffset += SERIALIZED_UINT_SIZE + len
returnValues.add(payload)
}
return Pair(returnValues.toTypedArray(), buffer.copyOfRange(localOffset, buffer.size))
}
/**
* Can only be used as the last element in a payload as it will consume the remainder of the
* input (avoid if possible).
*/
fun deserializeRaw(buffer: ByteArray, offset: Int = 0): Pair<ByteArray, Int> {
val len = buffer.size - offset
return Pair(buffer.copyOfRange(offset, buffer.size), len)
}