Skip to content

Commit 4824050

Browse files
authored
Merge pull request #3792 from Feoramund/core-uuid
Add `core:encoding/uuid`
2 parents 35651cf + ca58d77 commit 4824050

File tree

11 files changed

+1526
-0
lines changed

11 files changed

+1526
-0
lines changed

core/encoding/uuid/LICENSE

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
BSD 3-Clause License
2+
3+
Copyright (c) 2024, Feoramund
4+
5+
Redistribution and use in source and binary forms, with or without
6+
modification, are permitted provided that the following conditions are met:
7+
8+
1. Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
2. Redistributions in binary form must reproduce the above copyright notice,
12+
this list of conditions and the following disclaimer in the documentation
13+
and/or other materials provided with the distribution.
14+
15+
3. Neither the name of the copyright holder nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

core/encoding/uuid/definitions.odin

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package uuid
2+
3+
// A RFC 4122 Universally Unique Identifier
4+
Identifier :: distinct [16]u8
5+
6+
EXPECTED_LENGTH :: 8 + 4 + 4 + 4 + 12 + 4
7+
8+
VERSION_BYTE_INDEX :: 6
9+
VARIANT_BYTE_INDEX :: 8
10+
11+
// The number of 100-nanosecond intervals between 1582-10-15 and 1970-01-01.
12+
HNS_INTERVALS_BETWEEN_GREG_AND_UNIX :: 141427 * 24 * 60 * 60 * 1000 * 1000 * 10
13+
14+
VERSION_7_TIME_MASK :: 0xffffffff_ffff0000_00000000_00000000
15+
VERSION_7_TIME_SHIFT :: 80
16+
VERSION_7_COUNTER_MASK :: 0x00000000_00000fff_00000000_00000000
17+
VERSION_7_COUNTER_SHIFT :: 64
18+
19+
@(private)
20+
NO_CSPRNG_ERROR :: "The context random generator is not cryptographic. See the documentation for an example of how to set one up."
21+
@(private)
22+
BIG_CLOCK_ERROR :: "The clock sequence can only hold 14 bits of data, therefore no number greater than 16,383 (0x3FFF)."
23+
@(private)
24+
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)."
25+
26+
Read_Error :: enum {
27+
None,
28+
Invalid_Length,
29+
Invalid_Hexadecimal,
30+
Invalid_Separator,
31+
}
32+
33+
Variant_Type :: enum {
34+
Unknown,
35+
Reserved_Apollo_NCS, // 0b0xx
36+
RFC_4122, // 0b10x
37+
Reserved_Microsoft_COM, // 0b110
38+
Reserved_Future, // 0b111
39+
}
40+
41+
// Name string is a fully-qualified domain name.
42+
@(rodata)
43+
Namespace_DNS := Identifier {
44+
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1,
45+
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
46+
}
47+
48+
// Name string is a URL.
49+
@(rodata)
50+
Namespace_URL := Identifier {
51+
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1,
52+
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
53+
}
54+
55+
// Name string is an ISO OID.
56+
@(rodata)
57+
Namespace_OID := Identifier {
58+
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1,
59+
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
60+
}
61+
62+
// Name string is an X.500 DN (in DER or a text output format).
63+
@(rodata)
64+
Namespace_X500 := Identifier {
65+
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1,
66+
0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8,
67+
}

core/encoding/uuid/doc.odin

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
package uuid implements Universally Unique Identifiers according to the
3+
standard originally outlined in RFC 4122 with additions from RFC 9562.
4+
5+
The UUIDs are textually represented and read in the following string format:
6+
`00000000-0000-v000-V000-000000000000`
7+
8+
`v` is where the version bits reside, and `V` is where the variant bits reside.
9+
The meaning of the other bits is version-dependent.
10+
11+
Outside of string representations, UUIDs are represented in memory by a 128-bit
12+
structure organized as an array of 16 bytes.
13+
14+
15+
Of the UUID versions which may make use of random number generation, a
16+
requirement is placed upon them that the underlying generator be
17+
cryptographically-secure, per RFC 9562's suggestion.
18+
19+
- Version 1 without a node argument.
20+
- Version 4 in all cases.
21+
- Version 6 without either a clock or node argument.
22+
- Version 7 in all cases.
23+
24+
Here's an example of how to set up one:
25+
26+
import "core:crypto"
27+
import "core:encoding/uuid"
28+
29+
main :: proc() {
30+
my_uuid: uuid.Identifier
31+
32+
{
33+
// This scope will have a CSPRNG.
34+
context.random_generator = crypto.random_generator()
35+
my_uuid = uuid.generate_v7()
36+
}
37+
38+
// Back to the default random number generator.
39+
}
40+
41+
42+
For more information on the specifications, see here:
43+
- https://www.rfc-editor.org/rfc/rfc4122.html
44+
- https://www.rfc-editor.org/rfc/rfc9562.html
45+
*/
46+
package uuid

0 commit comments

Comments
 (0)