Skip to content

Commit 2867723

Browse files
committed
squash me: update to latest upstream
1 parent 1194811 commit 2867723

File tree

2 files changed

+185
-19
lines changed

2 files changed

+185
-19
lines changed

src/include/switch_uuidv7.h

Lines changed: 137 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,44 @@
11
/*
2-
* switch_uuidv7.h uuidv7
2+
* switch_uuidv7.h UUIDv7 generation functions, copied AS IS from https://github.com/LiosK/uuidv7-h
3+
* with minor fixes to make it compile with FreeSWITCH.
34
*/
4-
#include <switch.h>
55

6-
#undef _POSIX_C_SOURCE
7-
#define _POSIX_C_SOURCE 199309L
6+
/**
7+
* @file
8+
*
9+
* uuidv7.h - Single-file C/C++ UUIDv7 Library
10+
*
11+
* @version v0.1.6
12+
* @author LiosK
13+
* @copyright Licensed under the Apache License, Version 2.0
14+
* @see https://github.com/LiosK/uuidv7-h
15+
*/
16+
/*
17+
* Copyright 2022 LiosK
18+
*
19+
* Licensed under the Apache License, Version 2.0 (the "License");
20+
* you may not use this file except in compliance with the License.
21+
* You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*/
31+
#ifndef UUIDV7_H_BAEDKYFQ
32+
#define UUIDV7_H_BAEDKYFQ
833

934
#include <stddef.h>
1035
#include <stdint.h>
1136

37+
/**
38+
* @name Status codes returned by uuidv7_generate()
39+
*
40+
* @{
41+
*/
1242

1343
/**
1444
* Indicates that the `unix_ts_ms` passed was used because no preceding UUID was
@@ -49,11 +79,17 @@
4979
*/
5080
#define UUIDV7_STATUS_ERR_TIMESTAMP_OVERFLOW (-2)
5181

82+
/** @} */
5283

5384
#ifdef __cplusplus
5485
extern "C" {
5586
#endif
5687

88+
/**
89+
* @name Low-level primitives
90+
*
91+
* @{
92+
*/
5793

5894
/**
5995
* Generates a new UUIDv7 from the given Unix time, random bytes, and previous
@@ -78,18 +114,19 @@ extern "C" {
78114
* monotonic order of UUIDs or fine-tune the generation
79115
* process.
80116
*/
117+
static inline int8_t uuidv7_generate(uint8_t *uuid_out, uint64_t unix_ts_ms,
118+
const uint8_t *rand_bytes,
119+
const uint8_t *uuid_prev) {
120+
static const uint64_t MAX_TIMESTAMP = ((uint64_t)1 << 48) - 1;
121+
static const uint64_t MAX_COUNTER = ((uint64_t)1 << 42) - 1;
81122

82-
static inline int8_t uuidv7_generate(uint8_t *uuid_out, uint64_t unix_ts_ms,const uint8_t *rand_bytes,const uint8_t *uuid_prev) {
83123
int8_t status;
84124
uint64_t timestamp = 0;
85-
static const uint64_t MAX_TIMESTAMP = ((uint64_t)1 << 48) - 1;
86-
static const uint64_t MAX_COUNTER = ((uint64_t)1 << 42) - 1;
87125

88126
if (unix_ts_ms > MAX_TIMESTAMP) {
89127
return UUIDV7_STATUS_ERR_TIMESTAMP;
90128
}
91129

92-
93130
if (uuid_prev == NULL) {
94131
status = UUIDV7_STATUS_UNPRECEDENTED;
95132
timestamp = unix_ts_ms;
@@ -162,9 +199,71 @@ static inline int uuidv7_status_n_rand_consumed(int8_t status) {
162199
return status == UUIDV7_STATUS_COUNTER_INC ? 4 : 10;
163200
}
164201

202+
/**
203+
* Encodes a UUID in the 8-4-4-4-12 hexadecimal string representation.
204+
*
205+
* @param uuid 16-byte byte array representing the UUID to encode.
206+
* @param string_out Character array where the encoded string is stored. Its
207+
* length must be 37 (36 digits + NUL) or longer.
208+
*/
209+
static inline void uuidv7_to_string(const uint8_t *uuid, char *string_out) {
210+
static const char DIGITS[] = "0123456789abcdef";
211+
for (int i = 0; i < 16; i++) {
212+
uint_fast8_t e = uuid[i];
213+
*string_out++ = DIGITS[e >> 4];
214+
*string_out++ = DIGITS[e & 15];
215+
if (i == 3 || i == 5 || i == 7 || i == 9) {
216+
*string_out++ = '-';
217+
}
218+
}
219+
*string_out = '\0';
220+
}
221+
222+
/**
223+
* Decodes the 8-4-4-4-12 hexadecimal string representation of a UUID.
224+
*
225+
* @param string 37-byte (36 digits + NUL) character array representing the
226+
* 8-4-4-4-12 hexadecimal string representation.
227+
* @param uuid_out 16-byte byte array where the decoded UUID is stored.
228+
* @return Zero on success or non-zero integer on failure.
229+
*/
230+
static inline int uuidv7_from_string(const char *string, uint8_t *uuid_out) {
231+
for (int i = 0; i < 32; i++) {
232+
char c = *string++;
233+
// clang-format off
234+
uint8_t x = c == '0' ? 0 : c == '1' ? 1 : c == '2' ? 2 : c == '3' ? 3
235+
: c == '4' ? 4 : c == '5' ? 5 : c == '6' ? 6 : c == '7' ? 7
236+
: c == '8' ? 8 : c == '9' ? 9 : c == 'a' ? 10 : c == 'b' ? 11
237+
: c == 'c' ? 12 : c == 'd' ? 13 : c == 'e' ? 14 : c == 'f' ? 15
238+
: c == 'A' ? 10 : c == 'B' ? 11 : c == 'C' ? 12 : c == 'D' ? 13
239+
: c == 'E' ? 14 : c == 'F' ? 15 : 0xff;
240+
// clang-format on
241+
if (x == 0xff) {
242+
return -1; // invalid digit
243+
}
244+
245+
if ((i & 1) == 0) {
246+
uuid_out[i >> 1] = x << 4; // even i => hi 4 bits
247+
} else {
248+
uuid_out[i >> 1] |= x; // odd i => lo 4 bits
249+
}
250+
251+
if ((i == 7 || i == 11 || i == 15 || i == 19) && (*string++ != '-')) {
252+
return -1; // invalid format
253+
}
254+
}
255+
if (*string != '\0') {
256+
return -1; // invalid length
257+
}
258+
return 0; // success
259+
}
260+
261+
/** @} */
165262

166263
/**
167264
* @name High-level APIs that require platform integration
265+
*
266+
* @{
168267
*/
169268

170269
/**
@@ -187,10 +286,39 @@ static inline int uuidv7_status_n_rand_consumed(int8_t status) {
187286
* process. The implementation-dependent code must be out of
188287
* the range of `int8_t` and negative if it reports an error.
189288
*/
289+
int uuidv7_new(uint8_t *uuid_out);
190290

191-
SWITCH_DECLARE(int) uuidv7_new(uint8_t *uuid_out);
291+
/**
292+
* Generates an 8-4-4-4-12 hexadecimal string representation of new UUIDv7.
293+
*
294+
* @param string_out Character array where the encoded string is stored. Its
295+
* length must be 37 (36 digits + NUL) or longer.
296+
* @return Return value of `uuidv7_new()`.
297+
* @note Provide a concrete `uuidv7_new()` implementation to enable
298+
* this function.
299+
*/
300+
static inline int uuidv7_new_string(char *string_out) {
301+
uint8_t uuid[16];
302+
int result = uuidv7_new(uuid);
303+
uuidv7_to_string(uuid, string_out);
304+
return result;
305+
}
192306

307+
/** @} */
193308

194309
#ifdef __cplusplus
195310
} /* extern "C" { */
196311
#endif
312+
313+
#endif /* #ifndef UUIDV7_H_BAEDKYFQ */
314+
315+
/* For Emacs:
316+
* Local Variables:
317+
* mode:c
318+
* indent-tabs-mode:t
319+
* tab-width:4
320+
* c-basic-offset:4
321+
* End:
322+
* For VIM:
323+
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
324+
*/

src/switch_uuidv7.c

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,35 @@
1+
/*
2+
* FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
3+
* Copyright (C) 2025-2025, Anthony Minessale II <anthm@freeswitch.org>
4+
*
5+
* Version: MPL 1.1
6+
*
7+
* The contents of this file are subject to the Mozilla Public License Version
8+
* 1.1 (the "License"); you may not use this file except in compliance with
9+
* the License. You may obtain a copy of the License at
10+
* http://www.mozilla.org/MPL/
11+
*
12+
* Software distributed under the License is distributed on an "AS IS" basis,
13+
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14+
* for the specific language governing rights and limitations under the
15+
* License.
16+
*
17+
* The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
18+
*
19+
* The Initial Developer of the Original Code is
20+
* Seven Du <dujinfang@gmail.com>
21+
* Portions created by the Initial Developer are Copyright (C)
22+
* the Initial Developer. All Rights Reserved.
23+
*
24+
* Contributor(s):
25+
*
26+
* Seven Du <dujinfang@gmail.com>
27+
*
28+
* switch_uuidv7.c -- UUIDv7 generation functions
29+
*
30+
*/
131

2-
32+
#include <switch.h>
333
#include "switch_uuidv7.h"
434

535
// #include <assert.h>
@@ -15,26 +45,34 @@ SWITCH_DECLARE(int) uuidv7_new(uint8_t *uuid_out)
1545
{
1646
int8_t status;
1747
// struct timespec tp;
18-
static uint8_t uuid_prev[16] = {0};
19-
static uint8_t rand_bytes[256] = {0};
20-
static size_t n_rand_consumed = sizeof(rand_bytes);
48+
uint8_t uuid_prev[16] = {0};
49+
uint8_t rand_bytes[256] = {0};
50+
size_t n_rand_consumed = sizeof(rand_bytes);
2151

2252
uint64_t unix_ts_ms ;
2353
// clock_gettime(CLOCK_REALTIME, &tp);
2454
// unix_ts_ms = (uint64_t)tp.tv_sec * 1000 + tp.tv_nsec / 1000000;
2555
unix_ts_ms = switch_time_now() / 1000;
2656

27-
if (n_rand_consumed > sizeof(rand_bytes) - 10)
28-
{
57+
if (n_rand_consumed > sizeof(rand_bytes) - 10) {
2958
getentropy(rand_bytes, n_rand_consumed);
3059
n_rand_consumed = 0;
3160
}
3261

33-
status = uuidv7_generate(uuid_prev, unix_ts_ms,
34-
&rand_bytes[n_rand_consumed], uuid_prev);
62+
status = uuidv7_generate(uuid_prev, unix_ts_ms, &rand_bytes[n_rand_consumed], uuid_prev);
3563
n_rand_consumed += uuidv7_status_n_rand_consumed(status);
36-
3764
memcpy(uuid_out, uuid_prev, 16);
38-
return status;
3965

66+
return status;
4067
}
68+
69+
/* For Emacs:
70+
* Local Variables:
71+
* mode:c
72+
* indent-tabs-mode:t
73+
* tab-width:4
74+
* c-basic-offset:4
75+
* End:
76+
* For VIM:
77+
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
78+
*/

0 commit comments

Comments
 (0)