Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add core:encoding/uuid #3792

Merged
merged 23 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4dacddd
Add `core:encoding/uuid`
Feoramund Jun 21, 2024
31873ed
Fix wrong comments
Feoramund Jun 21, 2024
6da99b8
Make UUID `Identfier` only a distinct byte array
Feoramund Jun 21, 2024
fee8198
Make UUID namespaces `@(rodata)`
Feoramund Jun 21, 2024
4cfbd83
Add version 7 UUID generation
Feoramund Jun 21, 2024
525bfca
Add version 1 UUID generation
Feoramund Jun 21, 2024
9866b54
Add version 6 UUID generation
Feoramund Jun 22, 2024
3aa232a
Move v3 and v5 UUID procs to `uuid/legacy`
Feoramund Jun 22, 2024
fcdba33
Require CSPRNG in UUID generation where applicable
Feoramund Jun 22, 2024
ea771d0
Update `uuid` package documentation
Feoramund Jun 22, 2024
9b265b2
Improve time-related API in `uuid` package
Feoramund Jun 22, 2024
9b3a104
Add buffer-based `to_string` to `uuid` package
Feoramund Jun 22, 2024
95a9c9b
Fix indentation
Feoramund Jun 22, 2024
8b8f8c7
Address minor organizational issues
Feoramund Jun 22, 2024
339b2b2
Add `unsafe_write` to `uuid` package
Feoramund Jun 22, 2024
f634457
Add UUID sorting tests
Feoramund Jun 22, 2024
d559feb
Add `uuid` test for timestamps
Feoramund Jun 22, 2024
859cbf7
Test if v1 and v6 UUID `node` is set correctly
Feoramund Jun 22, 2024
e9b882b
Add vendor-specific version 8 UUID generation (hashing)
Feoramund Jun 22, 2024
5a75cac
Add API for creating custom version 8 UUIDs
Feoramund Jun 22, 2024
4481f9c
Clarify some `uuid` legacy documentation
Feoramund Jun 22, 2024
8a4a3ed
Change how `Time` is constructed in `uuid`
Feoramund Jun 22, 2024
ca58d77
Use new API `from_nanoseconds` in `uuid`
Feoramund Jun 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions core/encoding/uuid/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
BSD 3-Clause License

Copyright (c) 2024, Feoramund

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 changes: 67 additions & 0 deletions core/encoding/uuid/definitions.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package uuid

// A RFC 4122 Universally Unique Identifier
Identifier :: distinct [16]u8

EXPECTED_LENGTH :: 8 + 4 + 4 + 4 + 12 + 4

VERSION_BYTE_INDEX :: 6
VARIANT_BYTE_INDEX :: 8

// The number of 100-nanosecond intervals between 1582-10-15 and 1970-01-01.
HNS_INTERVALS_BETWEEN_GREG_AND_UNIX :: 141427 * 24 * 60 * 60 * 1000 * 1000 * 10

VERSION_7_TIME_MASK :: 0xffffffff_ffff0000_00000000_00000000
VERSION_7_TIME_SHIFT :: 80
VERSION_7_COUNTER_MASK :: 0x00000000_00000fff_00000000_00000000
VERSION_7_COUNTER_SHIFT :: 64

@(private)
NO_CSPRNG_ERROR :: "The context random generator is not cryptographic. See the documentation for an example of how to set one up."
@(private)
BIG_CLOCK_ERROR :: "The clock sequence can only hold 14 bits of data, therefore no number greater than 16,383 (0x3FFF)."
@(private)
VERSION_7_BIG_COUNTER_ERROR :: "This implementation of the version 7 UUID counter can only hold 12 bits of data, therefore no number greater than 4,095 (0xFFF)."

Read_Error :: enum {
None,
Invalid_Length,
Invalid_Hexadecimal,
Invalid_Separator,
}

Variant_Type :: enum {
Unknown,
Reserved_Apollo_NCS, // 0b0xx
RFC_4122, // 0b10x
Reserved_Microsoft_COM, // 0b110
Reserved_Future, // 0b111
}

// Name string is a fully-qualified domain name.
@(rodata)
Namespace_DNS := Identifier {
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}

// Name string is a URL.
@(rodata)
Namespace_URL := Identifier {
Feoramund marked this conversation as resolved.
Show resolved Hide resolved
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}

// Name string is an ISO OID.
@(rodata)
Namespace_OID := Identifier {
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}

// Name string is an X.500 DN (in DER or a text output format).
@(rodata)
Namespace_X500 := Identifier {
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1,
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
}
46 changes: 46 additions & 0 deletions core/encoding/uuid/doc.odin
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
package uuid implements Universally Unique Identifiers according to the
standard originally outlined in RFC 4122 with additions from RFC 9562.

The UUIDs are textually represented and read in the following string format:
`00000000-0000-v000-V000-000000000000`

`v` is where the version bits reside, and `V` is where the variant bits reside.
The meaning of the other bits is version-dependent.

Outside of string representations, UUIDs are represented in memory by a 128-bit
structure organized as an array of 16 bytes.


Of the UUID versions which may make use of random number generation, a
requirement is placed upon them that the underlying generator be
cryptographically-secure, per RFC 9562's suggestion.

- Version 1 without a node argument.
- Version 4 in all cases.
- Version 6 without either a clock or node argument.
- Version 7 in all cases.

Here's an example of how to set up one:

import "core:crypto"
import "core:encoding/uuid"

main :: proc() {
my_uuid: uuid.Identifier

{
// This scope will have a CSPRNG.
context.random_generator = crypto.random_generator()
my_uuid = uuid.generate_v7()
}

// Back to the default random number generator.
}


For more information on the specifications, see here:
- https://www.rfc-editor.org/rfc/rfc4122.html
- https://www.rfc-editor.org/rfc/rfc9562.html
*/
package uuid
Loading