|
| 1 | +/* |
| 2 | + * Copyright (c) Kuba Szczodrzyński 2022-12-19. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.github.cloudcutter.work.common |
| 6 | + |
| 7 | +import java.nio.ByteBuffer |
| 8 | +import java.nio.ByteOrder |
| 9 | + |
| 10 | +object Struct { |
| 11 | + |
| 12 | + private fun String.getEndianness() = when (this[0]) { |
| 13 | + '<' -> ByteOrder.LITTLE_ENDIAN |
| 14 | + '>', '!' -> ByteOrder.BIG_ENDIAN |
| 15 | + else -> ByteOrder.nativeOrder() |
| 16 | + } |
| 17 | + |
| 18 | + private fun String.getFormatList() = this |
| 19 | + .replace(" ", "") |
| 20 | + .replace("""(\d+[^s\d])""".toRegex()) { |
| 21 | + it.value |
| 22 | + .last() |
| 23 | + .toString() |
| 24 | + .repeat(it.value.dropLast(1).toInt()) |
| 25 | + } |
| 26 | + .replace("""\$\d+s|\d+s""".toRegex()) { " ${it.value} " } |
| 27 | + .split(" ") |
| 28 | + .filter { it.isNotBlank() } |
| 29 | + .flatMap { if (it.last() == 's') listOf(it) else it.split("") } |
| 30 | + .map { if (it == "s") "1s" else it } |
| 31 | + .filter { it.isNotBlank() } |
| 32 | + .dropWhile { it[0] in "@=<>!" } |
| 33 | + |
| 34 | + private fun getLengthFields(format: List<String>): Map<Int, Int> { |
| 35 | + // Map<length field, data field> |
| 36 | + val lengthFields = mutableMapOf<Int, Int>() |
| 37 | + |
| 38 | + // find all dynamically-sized strings |
| 39 | + var nullCount = 0 |
| 40 | + for ((index, field) in format.withIndex()) { |
| 41 | + if (field == "x") { |
| 42 | + nullCount++ |
| 43 | + continue |
| 44 | + } |
| 45 | + if (!field.startsWith("$")) |
| 46 | + continue |
| 47 | + val fieldIndex = field.drop(1).dropLast(1).toInt() |
| 48 | + lengthFields[fieldIndex] = index - nullCount |
| 49 | + } |
| 50 | + return lengthFields |
| 51 | + } |
| 52 | + |
| 53 | + fun pack(formatString: String, valuesRaw: List<Any>): ByteArray { |
| 54 | + val endianness = formatString.getEndianness() |
| 55 | + val format = formatString.getFormatList() |
| 56 | + val lengthFields = getLengthFields(format) |
| 57 | + |
| 58 | + // replace 'length fields' with actual length |
| 59 | + val values = valuesRaw.mapIndexed { index, value -> |
| 60 | + if (index in lengthFields) |
| 61 | + (valuesRaw[lengthFields[index] ?: 0] as ByteArray).size |
| 62 | + else |
| 63 | + value |
| 64 | + } |
| 65 | + |
| 66 | + var nullCount = 0 |
| 67 | + val totalLength = format.mapIndexed { index, field -> |
| 68 | + if (field == "x") |
| 69 | + nullCount++ |
| 70 | + when (field.last()) { |
| 71 | + 'x' -> 1 |
| 72 | + 'c', 'b', 'B' -> 1 |
| 73 | + '?' -> 1 |
| 74 | + 'h', 'H' -> 2 |
| 75 | + 'i', 'I' -> 4 |
| 76 | + 'l', 'L' -> 4 |
| 77 | + 'q', 'Q' -> 8 |
| 78 | + 'f' -> 4 |
| 79 | + 'd' -> 8 |
| 80 | + 's' -> if (field[0] == '$') |
| 81 | + (values[index - nullCount] as ByteArray).size |
| 82 | + else |
| 83 | + field.dropLast(1).toInt() |
| 84 | + 'S' -> (values[index - nullCount] as ByteArray).size |
| 85 | + else -> throw UnsupportedOperationException("Format specifier $field not supported") |
| 86 | + } |
| 87 | + }.sum() |
| 88 | + |
| 89 | + val buf = ByteBuffer.allocate(totalLength).order(endianness) |
| 90 | + nullCount = 0 |
| 91 | + for ((index, field) in format.withIndex()) { |
| 92 | + val spec = field.last() |
| 93 | + if (spec == 'x') { |
| 94 | + nullCount++ |
| 95 | + buf.put(0) |
| 96 | + continue |
| 97 | + } |
| 98 | + val value = values[index - nullCount] |
| 99 | + println("Packing field $index - $field($spec) - $value") |
| 100 | + when (spec) { |
| 101 | + 'c' -> buf.putChar(value as Char) |
| 102 | + 'b', 'B' -> buf.put((value as Number).toByte()) |
| 103 | + '?' -> buf.put(if (value == true) 1 else 0) |
| 104 | + 'h', 'H' -> buf.putShort((value as Number).toShort()) |
| 105 | + 'i', 'I' -> buf.putInt((value as Number).toInt()) |
| 106 | + 'l', 'L' -> buf.putInt((value as Number).toInt()) |
| 107 | + 'q', 'Q' -> buf.putLong((value as Number).toLong()) |
| 108 | + 'f' -> buf.putFloat((value as Number).toFloat()) |
| 109 | + 'd' -> buf.putDouble((value as Number).toDouble()) |
| 110 | + 's' -> { |
| 111 | + val size = (value as ByteArray).size |
| 112 | + if (field[0] == '$') { |
| 113 | + buf.put(value) |
| 114 | + } else { |
| 115 | + val fieldSize = field.dropLast(1).toInt() |
| 116 | + when { |
| 117 | + size > fieldSize -> buf.put(value.take(fieldSize).toByteArray()) |
| 118 | + size < fieldSize -> { |
| 119 | + buf.put(value) |
| 120 | + buf.put(ByteArray(fieldSize - size) { 0 }) |
| 121 | + } |
| 122 | + else -> buf.put(value) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + 'S' -> buf.put(value as ByteArray) |
| 127 | + else -> throw UnsupportedOperationException("Format specifier $field not supported") |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return buf.array() |
| 132 | + } |
| 133 | + |
| 134 | + fun unpack(formatString: String, data: ByteArray): List<Any> { |
| 135 | + val endianness = formatString.getEndianness() |
| 136 | + val format = formatString.getFormatList() |
| 137 | + val values = mutableListOf<Any>() |
| 138 | + |
| 139 | + val buf = ByteBuffer.wrap(data).order(endianness) |
| 140 | + for ((index, field) in format.withIndex()) { |
| 141 | + val spec = field.last() |
| 142 | + print("Unpacking field $index - $field($spec) - ") |
| 143 | + @Suppress("UsePropertyAccessSyntax") |
| 144 | + val value = when (spec) { |
| 145 | + 'x' -> buf.get() |
| 146 | + 'c' -> buf.getChar() |
| 147 | + 'b' -> buf.get().toInt() |
| 148 | + 'B' -> buf.get().toUByte().toInt() |
| 149 | + '?' -> buf.get().toInt() == 1 |
| 150 | + 'h' -> buf.getShort().toInt() |
| 151 | + 'H' -> buf.getShort().toUShort().toInt() |
| 152 | + 'i' -> buf.getInt() |
| 153 | + 'I' -> buf.getInt().toUInt().toInt() |
| 154 | + 'l' -> buf.getInt().toLong() |
| 155 | + 'L' -> buf.getInt().toUInt().toLong() |
| 156 | + 'q' -> buf.getLong() |
| 157 | + 'Q' -> buf.getLong().toULong().toLong() |
| 158 | + 'f' -> buf.getFloat() |
| 159 | + 'd' -> buf.getDouble() |
| 160 | + 's' -> { |
| 161 | + val size = if (field[0] == '$') { |
| 162 | + val fieldIndex = field.drop(1).dropLast(1).toInt() |
| 163 | + (values[fieldIndex] as Number).toInt() |
| 164 | + } else { |
| 165 | + field.dropLast(1).toInt() |
| 166 | + } |
| 167 | + val byteArray = ByteArray(size) |
| 168 | + buf.get(byteArray) |
| 169 | + byteArray |
| 170 | + } |
| 171 | + 'S' -> data.sliceArray(buf.position() until data.size) |
| 172 | + else -> throw UnsupportedOperationException("Format specifier $field not supported") |
| 173 | + } |
| 174 | + println(value) |
| 175 | + if (spec == 'x') |
| 176 | + continue |
| 177 | + values.add(value) |
| 178 | + } |
| 179 | + |
| 180 | + return values |
| 181 | + } |
| 182 | +} |
0 commit comments