Skip to content

Commit

Permalink
Fix UUID byte order in accordance with RFC4122
Browse files Browse the repository at this point in the history
  • Loading branch information
ancapdev committed Oct 18, 2024
1 parent a8df966 commit 2267a23
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LightBSON"
uuid = "a4a7f996-b3a6-4de6-b9db-2fa5f350df41"
authors = ["Christian Rorvik <[email protected]>"]
version = "0.2.21"
version = "0.2.22"

[deps]
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
Expand Down
8 changes: 4 additions & 4 deletions src/reader.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/writer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
2 changes: 1 addition & 1 deletion test/reader_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2267a23

Please sign in to comment.