Compare commits

..

No commits in common. "113f49f6a4a9054ecd07b83600375a0277173664" and "f454526c5a79944853669b0213c5ce623c3a1bcf" have entirely different histories.

10 changed files with 129 additions and 329 deletions

View file

@ -33,7 +33,7 @@ pub fn build(b: *std.Build) void {
// for actually invoking the compiler.
const lib = b.addLibrary(.{
.linkage = .static,
.name = "msgpack",
.name = "msgpack_zig",
.root_module = lib_mod,
});
@ -47,23 +47,12 @@ pub fn build(b: *std.Build) void {
const lib_unit_tests = b.addTest(.{
.root_module = lib_mod,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const tests_tests = b.addTest(.{
.root_source_file = b.path("tests/root.zig"),
.target = target,
.optimize = optimize,
});
tests_tests.root_module.addImport("msgpack", lib.root_module);
const run_tests_tests = b.addRunArtifact(tests_tests);
// Similar to creating the run step earlier, this exposes a `test` step to
// the `zig build --help` menu, providing a way for the user to request
// running the unit tests.
const test_step = b.step("test", "Run tests");
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
test_step.dependOn(&run_tests_tests.step);
}

View file

@ -6,7 +6,7 @@
//
// It is redundant to include "zig" in this name because it is already
// within the Zig package namespace.
.name = .msgpack,
.name = .msgpack_zig,
// This is a [Semantic Version](https://semver.org/).
// In a future version of Zig it will be used for package deduplication.
@ -24,7 +24,7 @@
// original project's identity. Thus it is recommended to leave the comment
// on the following line intact, so that it shows up in code reviews that
// modify the field.
.fingerprint = 0x6733a7561ee01213, // Changing this has security and trust implications.
.fingerprint = 0x9949832bc6a8b19c, // Changing this has security and trust implications.
// Tracks the earliest Zig version that the package considers to be a
// supported use case.

View file

@ -65,7 +65,7 @@ fn deserialise_uint(bytes: []const u8) DeserialiseError!ObjectLen {
return error.BadLength;
}
const obj = Object{ .uinteger = payload[0] };
const obj = Object{ .integer = @as(i64, payload[0]) };
return .{ .bytes_read = 2, .obj = obj };
},
@ -75,7 +75,7 @@ fn deserialise_uint(bytes: []const u8) DeserialiseError!ObjectLen {
return error.BadLength;
}
const obj = Object{ .uinteger = std.mem.readInt(u16, payload[0..2], .big) };
const obj = Object{ .integer = std.mem.readInt(u16, payload[0..2], .big) };
return .{ .bytes_read = 3, .obj = obj };
},
@ -85,7 +85,7 @@ fn deserialise_uint(bytes: []const u8) DeserialiseError!ObjectLen {
return error.BadLength;
}
const obj = Object{ .uinteger = std.mem.readInt(u32, payload[0..4], .big) };
const obj = Object{ .integer = std.mem.readInt(u32, payload[0..4], .big) };
return .{ .bytes_read = 5, .obj = obj };
},
@ -95,7 +95,13 @@ fn deserialise_uint(bytes: []const u8) DeserialiseError!ObjectLen {
return error.BadLength;
}
const obj = Object{ .uinteger = std.mem.readInt(u64, payload[0..8], .big) };
const i = std.mem.readInt(u64, payload[0..8], .big);
if (i > std.math.maxInt(i64)) {
return error.IntOutOfRange;
}
const obj = Object{ .integer = @intCast(i) };
return .{ .bytes_read = 9, .obj = obj };
},
@ -109,7 +115,7 @@ fn deserialise_int(bytes: []const u8) DeserialiseError!ObjectLen {
const payload = bytes[1..];
std.debug.assert(tag >> 2 == 0b110100);
std.debug.assert(tag >> 2 == 110100);
switch (@as(u2, @intCast(tag & 0b11))) {
0b00 => {
@ -182,7 +188,7 @@ fn deserialise_array(alloc: std.mem.Allocator, bytes: []const u8, len: usize) De
return error.BadLength;
}
const r = deserialise_with_length(alloc, bytes[bytes_read..]) catch |err| {
const r = deserialise_with_count(alloc, bytes[bytes_read..]) catch |err| {
// on error: deinit previous objects (up to i), then return the error
for (0..i) |j| {
array[j].deinit(alloc);
@ -210,7 +216,7 @@ fn deserialise_map(alloc: std.mem.Allocator, bytes: []const u8, len: usize) Dese
return error.BadLength;
}
const r1 = deserialise_with_length(alloc, bytes[bytes_read..]) catch |err| {
const r1 = deserialise_with_count(alloc, bytes[bytes_read..]) catch |err| {
// on error: deinit previous objects (up to i), then return the error
for (0..i) |j| {
array[j].deinit(alloc);
@ -223,7 +229,7 @@ fn deserialise_map(alloc: std.mem.Allocator, bytes: []const u8, len: usize) Dese
entry.*.key = r1.obj;
const r2 = deserialise_with_length(alloc, bytes[bytes_read..]) catch |err| {
const r2 = deserialise_with_count(alloc, bytes[bytes_read..]) catch |err| {
// on error: deinit previous objects (up to i) and current key, then return the error
for (0..i) |j| {
array[j].deinit(alloc);
@ -250,7 +256,7 @@ fn deserialise_ext(alloc: std.mem.Allocator, type_: u8, data: []const u8, len: u
return .{ .extension = .{ .type = type_, .bytes = bytes } };
}
pub fn deserialise_with_length(alloc: std.mem.Allocator, bytes: []const u8) DeserialiseError!ObjectLen {
pub fn deserialise_with_count(alloc: std.mem.Allocator, bytes: []const u8) DeserialiseError!ObjectLen {
if (bytes.len == 0) {
return error.BadLength;
}
@ -259,7 +265,7 @@ pub fn deserialise_with_length(alloc: std.mem.Allocator, bytes: []const u8) Dese
if (tag >> 7 == 0) {
// positive fixint
return .{ .bytes_read = 1, .obj = .{ .uinteger = @as(u64, tag) } };
return .{ .bytes_read = 1, .obj = .{ .integer = @as(i64, tag) } };
}
if (tag >> 5 == 0b111) {
@ -546,9 +552,116 @@ pub fn deserialise_with_length(alloc: std.mem.Allocator, bytes: []const u8) Dese
}
pub fn deserialise(alloc: std.mem.Allocator, bytes: []const u8) DeserialiseError!Object {
const r = try deserialise_with_length(alloc, bytes);
const r = try deserialise_with_count(alloc, bytes);
std.debug.assert(r.bytes_read == bytes.len);
return r.obj;
}
test "pos fixint" {
const alloc = std.testing.allocator;
const bytes = [1]u8{0x07};
const obj = try deserialise(alloc, &bytes);
try std.testing.expectEqual(Object{ .integer = 0x07 }, obj);
}
test "neg fixint" {
const alloc = std.testing.allocator;
const bytes = [1]u8{0b111_11111};
const obj = try deserialise(alloc, &bytes);
try std.testing.expectEqual(Object{ .integer = -1 }, obj);
}
test "raw" {
const alloc = std.testing.allocator;
{
const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' };
const obj = try deserialise(alloc, &bytes);
defer obj.deinit(alloc);
switch (obj) {
.raw => |raw| {
switch (raw) {
.binary => |s| try std.testing.expectEqualStrings("ABC", s),
.string => return error.TestExpectedEqual,
}
},
else => return error.TestExpectedEqual,
}
}
{
const bytes = [_]u8{ 0xc4, 0x00 };
const obj = try deserialise(alloc, &bytes);
defer obj.deinit(alloc);
switch (obj) {
.raw => |raw| {
switch (raw) {
.binary => |s| try std.testing.expectEqualStrings("", s),
.string => return error.TestExpectedEqual,
}
},
else => return error.TestExpectedEqual,
}
}
{
var bytes: [2 + 255]u8 = undefined;
bytes[0] = 0xc4;
bytes[1] = 0xff;
for (bytes[2..]) |*c| {
c.* = 'A';
}
const obj = try deserialise(alloc, &bytes);
defer obj.deinit(alloc);
switch (obj) {
.raw => |raw| {
switch (raw) {
.binary => |s| try std.testing.expectEqualStrings("A" ** 255, s),
.string => return error.TestExpectedEqual,
}
},
else => return error.TestExpectedEqual,
}
}
{
var bytes: [3 + 256]u8 = undefined;
bytes[0] = 0xc5;
bytes[1] = 0x01;
bytes[2] = 0x00;
for (bytes[3..]) |*c| {
c.* = 'A';
}
const obj = try deserialise(alloc, &bytes);
defer obj.deinit(alloc);
switch (obj) {
.raw => |raw| {
switch (raw) {
.binary => |s| try std.testing.expectEqualStrings("A" ** 256, s),
.string => return error.TestExpectedEqual,
}
},
else => return error.TestExpectedEqual,
}
}
}

View file

@ -28,7 +28,6 @@ pub const Object = union(enum) {
nil,
bool: bool,
integer: i64,
uinteger: u64,
float: f64,
raw: Raw,
array: []Object,

View file

@ -1,83 +0,0 @@
const std = @import("std");
const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.deserialise;
fn test_binary(bytes: []const u8, expected: []const u8) !void {
const alloc = std.testing.allocator;
const obj = try deserialise(alloc, bytes);
defer obj.deinit(alloc);
try std.testing.expectEqualStrings(expected, obj.raw.binary);
}
test "binary 1" {
const bytes = [_]u8{ 0xc4, 0x03, 'A', 'B', 'C' };
try test_binary(&bytes, "ABC");
}
test "binary 2" {
const bytes = [_]u8{ 0xc4, 0x00 };
try test_binary(&bytes, "");
}
test "binary 3" {
var bytes: [2 + 255]u8 = undefined;
bytes[0] = 0xc4;
bytes[1] = 0xff;
for (bytes[2..]) |*c| {
c.* = 'A';
}
try test_binary(&bytes, "A" ** 255);
}
test "binary 4" {
var bytes: [3 + 256]u8 = undefined;
bytes[0] = 0xc5;
bytes[1] = 0x01;
bytes[2] = 0x00;
for (bytes[3..]) |*c| {
c.* = 'A';
}
try test_binary(&bytes, "A" ** 256);
}
test "binary 5" {
var bytes: [3 + 65535]u8 = undefined;
bytes[0] = 0xc5;
bytes[1] = 0xff;
bytes[2] = 0xff;
for (bytes[3..]) |*c| {
c.* = 'A';
}
try test_binary(&bytes, "A" ** 65535);
}
test "binary 6" {
var bytes: [5 + 65536]u8 = undefined;
bytes[0] = 0xc6;
bytes[1] = 0x00;
bytes[2] = 0x01;
bytes[3] = 0x00;
bytes[4] = 0x00;
for (bytes[5..]) |*c| {
c.* = 'A';
}
try test_binary(&bytes, "A" ** 65536);
}

View file

@ -1,42 +0,0 @@
const std = @import("std");
pub const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.deserialise;
fn test_int(bytes: []const u8, expected: i64) !void {
const alloc = std.testing.allocator;
const obj = try deserialise(alloc, bytes);
defer obj.deinit(alloc);
try std.testing.expectEqual(expected, obj.integer);
}
test "neg fixint" {
try test_int(&[_]u8{0xFF}, -1);
}
test "int i8" {
try test_int(&[_]u8{ 0xd0, 0x80 }, std.math.minInt(i8));
try test_int(&[_]u8{ 0xd0, 0x7F }, std.math.maxInt(i8));
}
test "int i16" {
try test_int(&[_]u8{ 0xd1, 0x80, 0x00 }, std.math.minInt(i16));
try test_int(&[_]u8{ 0xd1, 0x7f, 0xff }, std.math.maxInt(i16));
}
test "int i32" {
try test_int(&[_]u8{ 0xd2, 0x80, 0x00, 0x00, 0x00 }, std.math.minInt(i32));
try test_int(&[_]u8{ 0xd2, 0x7f, 0xff, 0xff, 0xff }, std.math.maxInt(i32));
}
test "int i64" {
try test_int(&[_]u8{ 0xd3, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, std.math.minInt(i64));
try test_int(&[_]u8{ 0xd3, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(i64));
}

View file

@ -1,41 +0,0 @@
const std = @import("std");
pub const binary = @import("binary.zig");
pub const string = @import("string.zig");
pub const int = @import("int.zig");
pub const uint = @import("uint.zig");
pub const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.deserialise;
test {
@import("std").testing.refAllDecls(@This());
}
test "nil" {
const alloc = std.testing.allocator;
const obj = try deserialise(alloc, &[_]u8{0xc0});
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object.nil, obj);
}
test "bool" {
const alloc = std.testing.allocator;
{
const obj = try deserialise(alloc, &[_]u8{0xc2});
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object{ .bool = false }, obj);
}
{
const obj = try deserialise(alloc, &[_]u8{0xc3});
defer obj.deinit(alloc);
try std.testing.expectEqualDeep(msgpack.Object{ .bool = true }, obj);
}
}

View file

@ -1,84 +0,0 @@
const std = @import("std");
const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.deserialise;
fn test_string(bytes: []const u8, expected: []const u8) !void {
const alloc = std.testing.allocator;
const obj = try deserialise(alloc, bytes);
defer obj.deinit(alloc);
try std.testing.expectEqualStrings(expected, obj.raw.string);
}
test "string 1" {
const bytes = [_]u8{ 0b101_00000 | 0x03, 'A', 'B', 'C' };
try test_string(&bytes, "ABC");
}
test "string 2" {
const bytes = [_]u8{ 0xd9, 0x03, 'A', 'B', 'C' };
try test_string(&bytes, "ABC");
}
test "string 3" {
const bytes = [_]u8{ 0xd9, 0x00 };
try test_string(&bytes, "");
}
test "string 4" {
var bytes: [2 + 255]u8 = undefined;
bytes[0] = 0xd9;
bytes[1] = 0xff;
for (bytes[2..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 255);
}
test "string 5" {
var bytes: [3 + 256]u8 = undefined;
bytes[0] = 0xda;
bytes[1] = 0x01;
bytes[2] = 0x00;
for (bytes[3..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 256);
}
test "string 6" {
var bytes: [3 + 65535]u8 = undefined;
bytes[0] = 0xda;
bytes[1] = 0xff;
bytes[2] = 0xff;
for (bytes[3..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 65535);
}
test "string 7" {
var bytes: [5 + 65536]u8 = undefined;
bytes[0] = 0xdb;
bytes[1] = 0x00;
bytes[2] = 0x01;
bytes[3] = 0x00;
bytes[4] = 0x00;
for (bytes[5..]) |*c| {
c.* = 'A';
}
try test_string(&bytes, "A" ** 65536);
}

View file

@ -1,46 +0,0 @@
const std = @import("std");
pub const msgpack = @import("msgpack");
const deserialise = msgpack.deserialise.deserialise;
fn test_uint(bytes: []const u8, expected: u64) !void {
const alloc = std.testing.allocator;
const obj = try deserialise(alloc, bytes);
defer obj.deinit(alloc);
try std.testing.expectEqual(expected, obj.uinteger);
}
test "pos fixint" {
try test_uint(&[_]u8{0x00}, std.math.minInt(u8));
try test_uint(&[_]u8{0x07}, 0x07);
try test_uint(&[_]u8{0x7F}, 0x7F);
}
test "int u8" {
try test_uint(&[_]u8{ 0xcc, 0x00 }, std.math.minInt(u8));
try test_uint(&[_]u8{ 0xcc, 0xff }, std.math.maxInt(u8));
}
test "int u16" {
try test_uint(&[_]u8{ 0xcd, 0x00, 0x00 }, std.math.minInt(u16));
try test_uint(&[_]u8{ 0xcd, 0xff, 0xff }, std.math.maxInt(u16));
}
test "int u32" {
try test_uint(&[_]u8{ 0xce, 0x00, 0x00, 0x00, 0x00 }, std.math.minInt(u32));
try test_uint(&[_]u8{ 0xce, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(u32));
}
test "int u64" {
try test_uint(&[_]u8{ 0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, std.math.minInt(u64));
try test_uint(&[_]u8{ 0xcf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, std.math.maxInt(u64));
}

View file

@ -1,5 +0,0 @@
pub const deserialise = @import("deserialise/root.zig");
test {
@import("std").testing.refAllDecls(@This());
}