diff --git a/src/client.zig b/src/client.zig index c2e0fde..53dc3fa 100644 --- a/src/client.zig +++ b/src/client.zig @@ -52,7 +52,6 @@ pub const Client = struct { try write.add_message(list.writer(), s.allocator, map); try dumpDebug("ClientInit.bin", list); - try s.stream.writer().writeAll(list.items); } pub fn quassel_login(s: *Client, username: []const u8, password: []const u8) !void { diff --git a/src/qtshit/read.zig b/src/qtshit/read.zig index 44cf4cf..919a77a 100644 --- a/src/qtshit/read.zig +++ b/src/qtshit/read.zig @@ -28,11 +28,25 @@ pub fn get_bytearray(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayL } pub fn get_string(reader: anytype, allocator: *std.mem.Allocator) ![]u8 { - return try reader.readUntilDelimiterAlloc( - allocator, - '\x00', - 1024, - ); + var data = std.ArrayList(u8).init(allocator); + defer data.deinit(); + var length = try reader.readIntBig(i32); + + var index: usize = 0; + while (true) { + if (index == length) break; + + const byte = try reader.readIntBig(u16); + try data.append(@intCast(u8, byte)); + index += 1; + } + + var ut8Str = try allocator.alloc(u8, @intCast(usize, length)); + for (data.items) |char, i| { + ut8Str[i] = char; + } + + return ut8Str; } pub fn get_stringlist(reader: anytype, allocator: *std.mem.Allocator) !std.ArrayList([]const u8) { @@ -85,5 +99,6 @@ pub fn get_variant_t(reader: anytype, type_id: u32, allocator: *std.mem.Allocato pub fn get_variant(reader: anytype, allocator: *std.mem.Allocator) !qvar.QVariant { var type_id = try get_qvariant(reader); + _ = try get_byte(reader); return try get_variant_t(reader, type_id, allocator); } diff --git a/src/qtshit/test.zig b/src/qtshit/test.zig index 37a57d0..1472611 100644 --- a/src/qtshit/test.zig +++ b/src/qtshit/test.zig @@ -48,7 +48,7 @@ test "read/write string" { var arr = "Hello World!".*; - try write.add_string(byteList.writer(), &arr); + try write.add_string(byteList.writer(), global_allocator, &arr); var fBS = std.io.fixedBufferStream(byteList.items); var val = try read.get_string(fBS.reader(), global_allocator); @@ -87,7 +87,7 @@ test "read/write stringlist" { try arr.append("Hewwo"); try arr.append("World"); - try write.add_stringlist(byteList.writer(), arr); + try write.add_stringlist(byteList.writer(), global_allocator, arr); var fBS = std.io.fixedBufferStream(byteList.items); var val = try read.get_stringlist(fBS.reader(), global_allocator); diff --git a/src/qtshit/write.zig b/src/qtshit/write.zig index 78f1925..71fd3da 100644 --- a/src/qtshit/write.zig +++ b/src/qtshit/write.zig @@ -19,15 +19,14 @@ pub fn add_bytearray(writer: anytype, array: std.ArrayList(u8)) !void { } pub fn add_string(writer: anytype, allocator: *std.mem.Allocator, str: []const u8) !void { + try writer.writeIntBig(i32, @intCast(i32, str.len)); var ut16Str = try allocator.alloc(u16, str.len); defer allocator.free(ut16Str); for (str) |character, index| { ut16Str[index] = std.mem.nativeToBig(u16, @as(u16, character)); } - try writer.writeAll(std.mem.sliceAsBytes(ut16Str)); - try writer.writeByte('\x00'); - + try writer.writeAll(std.mem.sliceAsBytes(ut16Str)); } pub fn add_stringlist(writer: anytype, allocator: *std.mem.Allocator, strList: std.ArrayList([]const u8)) !void { @@ -40,6 +39,7 @@ pub fn add_stringlist(writer: anytype, allocator: *std.mem.Allocator, strList: s // Call this and Then write your type pub fn add_qvariant_with_id(writer: anytype, type_id: u32) !void { try add_int(writer, type_id); + try add_byte(writer, 0); } pub fn add_qvariant(writer: anytype, allocator: *std.mem.Allocator, variant: qvar.QVariant) !void { @@ -82,7 +82,7 @@ pub fn add_qvariantmap(writer: anytype, allocator: *std.mem.Allocator, map: std. // qvariantmap type try add_int(writer, 8); // 4 - // validity? + // unknown value try add_byte(writer, 0); // 1 // elements try add_int(writer, map.count()); // 4