From e7410855435a045db3acb4a34dd7f42444fa24a5 Mon Sep 17 00:00:00 2001 From: Christian Rorvik Date: Sat, 19 Oct 2024 10:49:55 +0200 Subject: [PATCH] UUID byte order (#34) * Fix UUID byte order in accordance with RFC4122 * bump version to 1.0.0 --- Project.toml | 2 +- src/reader.jl | 8 ++++---- src/writer.jl | 4 ++-- test/reader_tests.jl | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Project.toml b/Project.toml index 31ca382..693e77e 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LightBSON" uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41" authors = ["Christian Rorvik "] -version = "0.2.21" +version = "1.0.0" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" diff --git a/src/reader.jl b/src/reader.jl index d2f3ee4..58f1b02 100644 --- a/src/reader.jl +++ b/src/reader.jl @@ -243,7 +243,7 @@ end ) len = load_bits_(Int32, p) len != 16 && error("Unexpected UUID length $len") - return unsafe_load(Ptr{UUID}(p + 5)) + return UUID(ntoh(unsafe_load(Ptr{UInt128}(p + 5)))) end end @@ -255,7 +255,7 @@ end subtype == BSON_SUBTYPE_UUID_OLD || throw(BSONConversionError(reader.type, subtype, BSONUUIDOld)) len = load_bits_(Int32, p) len != 16 && error("Unexpected UUID length $len") - return BSONUUIDOld(unsafe_load(Ptr{UUID}(p + 5))) + return BSONUUIDOld(UUID(ntoh(unsafe_load(Ptr{UInt128}(p + 5))))) end end @@ -439,9 +439,9 @@ function read_field_(reader::AbstractBSONReader, ::Type{Any}) x = reader[BSONBinary] data = x.data if x.subtype == BSON_SUBTYPE_UUID && length(data) == 16 - GC.@preserve data unsafe_load(Ptr{UUID}(pointer(data))) + GC.@preserve data UUID(ntoh(unsafe_load(Ptr{UInt128}(pointer(data))))) elseif x.subtype == BSON_SUBTYPE_UUID_OLD && length(data) == 16 - GC.@preserve data BSONUUIDOld(unsafe_load(Ptr{UUID}(pointer(data)))) + GC.@preserve data BSONUUIDOld(UUID(ntoh(unsafe_load(Ptr{UInt128}(pointer(data)))))) else x end diff --git a/src/writer.jl b/src/writer.jl index 0504e7e..e7ec277 100644 --- a/src/writer.jl +++ b/src/writer.jl @@ -71,13 +71,13 @@ end @inline function wire_store_(p::Ptr{UInt8}, x::UUID) unsafe_store!(Ptr{Int32}(p), Int32(16)) unsafe_store!(p + 4, BSON_SUBTYPE_UUID) - unsafe_store!(Ptr{UUID}(p + 5), x) + unsafe_store!(Ptr{UInt128}(p + 5), hton(UInt128(x.value))) end @inline function wire_store_(p::Ptr{UInt8}, x::BSONUUIDOld) unsafe_store!(Ptr{Int32}(p), Int32(16)) unsafe_store!(p + 4, BSON_SUBTYPE_UUID_OLD) - unsafe_store!(Ptr{UUID}(p + 5), x.value) + unsafe_store!(Ptr{UInt128}(p + 5), hton(UInt128(x.value))) end @inline function wire_store_(p::Ptr{UInt8}, x::Union{BSONBinary, UnsafeBSONBinary}) diff --git a/test/reader_tests.jl b/test/reader_tests.jl index accb105..eb4c5a4 100644 --- a/test/reader_tests.jl +++ b/test/reader_tests.jl @@ -153,7 +153,7 @@ end io = IOBuffer() write(io, htol(Int32(16))) write(io, BSON_SUBTYPE_UUID) - unsafe_write(io, Ref(x), 16) + unsafe_write(io, Ref(hton(UInt128(x))), 16) reader = BSONReader(single_field_doc_(BSON_TYPE_BINARY, take!(io))) @test reader["x"][UUID] == x @test reader["x"][Any] == x