///| Wayland binary wire protocol encoder and decoder in pure MoonBit. ///| pub(all) struct WireWriter { buf : Array[Byte] } ///| pub fn WireWriter::new() -> WireWriter { { buf: [] } } ///| pub fn WireWriter::u32(self : WireWriter, v : UInt) -> Unit { self.buf.push((v & 0xFFU).to_byte()) self.buf.push(((v >> 8) & 0xFFU).to_byte()) self.buf.push(((v >> 16) & 0xFFU).to_byte()) self.buf.push(((v >> 24) & 0xFFU).to_byte()) } ///| pub fn WireWriter::i32(self : WireWriter, v : Int) -> Unit { self.u32(v.reinterpret_as_uint()) } ///| pub fn WireWriter::str(self : WireWriter, s : String) -> Unit { let utf8 = @sys.str_to_utf8_bytes(s) let len_with_nul = utf8.length() + 1 self.u32(len_with_nul.reinterpret_as_uint()) for i = 0; i < utf8.length(); i = i + 1 { self.buf.push(utf8[i]) } self.buf.push(b'\x00') while self.buf.length() % 4 != 0 { self.buf.push(b'\x00') } } ///| pub fn WireWriter::array(self : WireWriter, data : Array[Byte]) -> Unit { self.u32(data.length().reinterpret_as_uint()) for b in data { self.buf.push(b) } while self.buf.length() % 4 != 0 { self.buf.push(b'\x00') } } ///| pub fn WireWriter::finish_msg(self : WireWriter, object_id : UInt, opcode : UInt) -> Bytes { let total_len = 8 + self.buf.length() let out : Array[Byte] = Array(capacity=total_len) // object_id out.push((object_id & 0xFFU).to_byte()) out.push(((object_id >> 8) & 0xFFU).to_byte()) out.push(((object_id >> 16) & 0xFFU).to_byte()) out.push(((object_id >> 24) & 0xFFU).to_byte()) // (size << 16) | opcode let word2 = (total_len.reinterpret_as_uint() << 16) | (opcode & 0xFFFFU) out.push((word2 & 0xFFU).to_byte()) out.push(((word2 >> 8) & 0xFFU).to_byte()) out.push(((word2 >> 16) & 0xFFU).to_byte()) out.push(((word2 >> 24) & 0xFFU).to_byte()) for b in self.buf { out.push(b) } Bytes::from_array(out) } ///| pub(all) struct WireReader { data : Array[Byte] mut pos : Int } ///| pub fn WireReader::from_array(data : Array[Byte], start : Int) -> WireReader { { data, pos: start } } ///| pub fn WireReader::u32(self : WireReader) -> UInt { if self.pos + 4 > self.data.length() { return 0U } let b0 = self.data[self.pos].to_uint() let b1 = self.data[self.pos + 1].to_uint() let b2 = self.data[self.pos + 2].to_uint() let b3 = self.data[self.pos + 3].to_uint() self.pos = self.pos + 4 b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) } ///| pub fn WireReader::i32(self : WireReader) -> Int { self.u32().reinterpret_as_int() } ///| pub fn WireReader::str(self : WireReader) -> String { let len = self.u32().reinterpret_as_int() if len <= 0 || self.pos + len > self.data.length() { return "" } let buf = StringBuilder::new() // Exclude trailing NUL let str_len = if len > 0 { len - 1 } else { 0 } for i = 0; i < str_len; i = i + 1 { let b = self.data[self.pos + i].to_int() if b != 0 { buf.write_char(Int::unsafe_to_char(b)) } } let padded = (len + 3) / 4 * 4 self.pos = self.pos + padded buf.to_string() } ///| pub fn WireReader::array(self : WireReader) -> Array[Byte] { let len = self.u32().reinterpret_as_int() let out : Array[Byte] = [] if len <= 0 || self.pos + len > self.data.length() { return out } for i = 0; i < len; i = i + 1 { out.push(self.data[self.pos + i]) } let padded = (len + 3) / 4 * 4 self.pos = self.pos + padded out } ///| pub fn read_u32_le(data : Array[Byte], off : Int) -> UInt { let b0 = data[off].to_uint() let b1 = data[off + 1].to_uint() let b2 = data[off + 2].to_uint() let b3 = data[off + 3].to_uint() b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) }