summaryrefslogtreecommitdiff
path: root/ebus-racket/3rdparty/zitterbewegung/uuid/uuid-v4.ss
blob: 9aa6aa4d161e767b6f867fbfc5814e79cacdeb7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#lang scheme

(require srfi/27)

;;From Gambit Scheme Released under the LGPL
;; UUID generation
;; See: http://www.ietf.org/rfc/rfc4122.txt
;;
;; Version 4 UUID, see section 4.4
(provide make-uuid
         urn)
(define random-integer-65536
  (let* ((rs (make-random-source))
         (ri (random-source-make-integers rs)))
    (random-source-randomize! rs)
	(lambda ()
	  (ri 65536))))

(define (make-uuid)
  (define hex
    '#(#\0 #\1 #\2 #\3 #\4 #\5 #\6 #\7 #\8 #\9 #\A #\B #\C #\D #\E #\F))
  (let ((n1 (random-integer-65536))
        (n2 (random-integer-65536))
        (n3 (random-integer-65536))
        (n4 (random-integer-65536))
        (n5 (random-integer-65536))
        (n6 (random-integer-65536))
        (n7 (random-integer-65536))
        (n8 (random-integer-65536)))
    (string->symbol
     (string
      ;; time_lo
      (vector-ref hex (extract-bit-field 4 12 n1))
      (vector-ref hex (extract-bit-field 4  8 n1))
      (vector-ref hex (extract-bit-field 4  4 n1))
      (vector-ref hex (extract-bit-field 4  0 n1))
      (vector-ref hex (extract-bit-field 4 12 n2))
      (vector-ref hex (extract-bit-field 4  8 n2))
      (vector-ref hex (extract-bit-field 4  4 n2))
      (vector-ref hex (extract-bit-field 4  0 n2))
      #\-
      ;; time_mid
      (vector-ref hex (extract-bit-field 4 12 n3))
      (vector-ref hex (extract-bit-field 4  8 n3))
      (vector-ref hex (extract-bit-field 4  4 n3))
      (vector-ref hex (extract-bit-field 4  0 n3))
      #\-
      ;; time_hi_and_version
      (vector-ref hex #b0100)
      (vector-ref hex (extract-bit-field 4  8 n4))
      (vector-ref hex (extract-bit-field 4  4 n4))
      (vector-ref hex (extract-bit-field 4  0 n4))
      #\-
      ;; clock_seq_hi_and_reserved
      (vector-ref hex (bitwise-ior (extract-bit-field 2 12 n5) #b1000))
      (vector-ref hex (extract-bit-field 4  8 n5))
      ;; clock_seq_low
      (vector-ref hex (extract-bit-field 4  4 n5))
      (vector-ref hex (extract-bit-field 4  0 n5))
      #\-
      ;; node
      (vector-ref hex (extract-bit-field 4 12 n6))
      (vector-ref hex (extract-bit-field 4  8 n6))
      (vector-ref hex (extract-bit-field 4  4 n6))
      (vector-ref hex (extract-bit-field 4  0 n6))
      (vector-ref hex (extract-bit-field 4 12 n7))
      (vector-ref hex (extract-bit-field 4  8 n7))
      (vector-ref hex (extract-bit-field 4  4 n7))
      (vector-ref hex (extract-bit-field 4  0 n7))
      (vector-ref hex (extract-bit-field 4 12 n8))
      (vector-ref hex (extract-bit-field 4  8 n8))
      (vector-ref hex (extract-bit-field 4  4 n8))
      (vector-ref hex (extract-bit-field 4  0 n8))))))

(define (extract-bit-field size position n)
  (bitwise-and (bitwise-not (arithmetic-shift -1 size))
               (arithmetic-shift n (- position))))

(define (urn)
  (string-append "urn:uuid:"
                 (symbol->string (make-uuid))))