Fossil SCM

Add a function for generating a random (v4) UUID and a test command using it.

danield 2025-07-07 15:11 trunk
Commit 9472f708c99b4325bf4654b04f8c9a7abc08394277d921d02e3522deef2d5029
1 file changed +42
+42
--- src/util.c
+++ src/util.c
@@ -899,10 +899,52 @@
899899
}else{
900900
fossil_print("%s\n", zPassword);
901901
}
902902
fossil_free(zPassword);
903903
}
904
+
905
+/*
906
+** Generate a version 4 ("random"), variant 1 UUID (RFC 9562, Section 5.4).
907
+**
908
+** Format: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
909
+** where M=4 and N=8, 9, a, or b (this leaves 122 random bits)
910
+*/
911
+char* fossil_generate_uuid() {
912
+ static const char zDigits[] = "0123456789abcdef";
913
+ unsigned char aBlob[16];
914
+ unsigned char zStr[37];
915
+ unsigned char *p = zStr;
916
+ int i, k;
917
+
918
+ sqlite3_randomness(16, aBlob);
919
+ aBlob[6] = (aBlob[6]&0x0f) + 0x40; /* Version byte: 0100 xxxx */
920
+ aBlob[8] = (aBlob[8]&0x3f) + 0x80; /* Variant byte: 1000 xxxx */
921
+
922
+ for(i=0, k=0x550; i<16; i++, k=k>>1){
923
+ if( k&1 ){
924
+ *p++ = '-'; /* Add a dash after byte 4, 6, 8, and 12 */
925
+ }
926
+ *p++ = zDigits[aBlob[i]>>4];
927
+ *p++ = zDigits[aBlob[i]&0xf];
928
+ }
929
+ *p = 0;
930
+
931
+ return mprintf("%s", zStr);
932
+}
933
+
934
+/*
935
+** COMMAND: test-generate-uuid
936
+**
937
+** Usage: %fossil test-generate-uuid
938
+**
939
+** Generate a version 4 ("random"), variant 1 UUID (RFC 9562, Section 5.4):
940
+**
941
+** xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx - where M=4 and N=8, 9, a, or b
942
+*/
943
+void test_generate_uuid(void){
944
+ fossil_print("%s\n", fossil_generate_uuid());
945
+}
904946
905947
/*
906948
** Return the number of decimal digits in a nonnegative integer. This is useful
907949
** when formatting text.
908950
*/
909951
--- src/util.c
+++ src/util.c
@@ -899,10 +899,52 @@
899 }else{
900 fossil_print("%s\n", zPassword);
901 }
902 fossil_free(zPassword);
903 }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
904
905 /*
906 ** Return the number of decimal digits in a nonnegative integer. This is useful
907 ** when formatting text.
908 */
909
--- src/util.c
+++ src/util.c
@@ -899,10 +899,52 @@
899 }else{
900 fossil_print("%s\n", zPassword);
901 }
902 fossil_free(zPassword);
903 }
904
905 /*
906 ** Generate a version 4 ("random"), variant 1 UUID (RFC 9562, Section 5.4).
907 **
908 ** Format: xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
909 ** where M=4 and N=8, 9, a, or b (this leaves 122 random bits)
910 */
911 char* fossil_generate_uuid() {
912 static const char zDigits[] = "0123456789abcdef";
913 unsigned char aBlob[16];
914 unsigned char zStr[37];
915 unsigned char *p = zStr;
916 int i, k;
917
918 sqlite3_randomness(16, aBlob);
919 aBlob[6] = (aBlob[6]&0x0f) + 0x40; /* Version byte: 0100 xxxx */
920 aBlob[8] = (aBlob[8]&0x3f) + 0x80; /* Variant byte: 1000 xxxx */
921
922 for(i=0, k=0x550; i<16; i++, k=k>>1){
923 if( k&1 ){
924 *p++ = '-'; /* Add a dash after byte 4, 6, 8, and 12 */
925 }
926 *p++ = zDigits[aBlob[i]>>4];
927 *p++ = zDigits[aBlob[i]&0xf];
928 }
929 *p = 0;
930
931 return mprintf("%s", zStr);
932 }
933
934 /*
935 ** COMMAND: test-generate-uuid
936 **
937 ** Usage: %fossil test-generate-uuid
938 **
939 ** Generate a version 4 ("random"), variant 1 UUID (RFC 9562, Section 5.4):
940 **
941 ** xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx - where M=4 and N=8, 9, a, or b
942 */
943 void test_generate_uuid(void){
944 fossil_print("%s\n", fossil_generate_uuid());
945 }
946
947 /*
948 ** Return the number of decimal digits in a nonnegative integer. This is useful
949 ** when formatting text.
950 */
951

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button