update.
[elisp/flim.git] / unique-id.el
1 ;;; unique-id.el --- Compute DIGEST-MD5.
2
3 ;; Copyright (C) 1999 Kenichi OKADA
4
5 ;; Author: Katsumi Yamaoka  <yamaoka@jpl.org>
6
7 ;; This file is part of FLIM (Faithful Library about Internet Message).
8
9 ;;; Code:
10
11 ;;; Gnus 5.8.3: message.el
12
13 (defvar unique-id-m-char nil)
14
15 ;; If you ever change this function, make sure the new version
16 ;; cannot generate IDs that the old version could.
17 ;; You might for example insert a "." somewhere (not next to another dot
18 ;; or string boundary), or modify the suffix string (default to "fsf").
19 (defun unique-id-m (&optional suffix)
20   ;; Don't use microseconds from (current-time), they may be unsupported.
21   ;; Instead we use this randomly inited counter.
22   (setq unique-id-m-char
23         (% (1+ (or unique-id-m-char (logand (random t) (1- (lsh 1 20)))))
24            ;; (current-time) returns 16-bit ints,
25            ;; and 2^16*25 just fits into 4 digits i base 36.
26            (* 25 25)))
27   (let ((tm (current-time)))
28     (concat
29      (if (memq system-type '(ms-dos emx vax-vms))
30          (let ((user (downcase (user-login-name))))
31            (while (string-match "[^a-z0-9_]" user)
32              (aset user (match-beginning 0) ?_))
33            user)
34        (unique-id-m-number-base36 (user-uid) -1))
35      (unique-id-m-number-base36 (+ (car   tm)
36                                    (lsh (% unique-id-m-char 25) 16)) 4)
37      (unique-id-m-number-base36 (+ (nth 1 tm)
38                                    (lsh (/ unique-id-m-char 25) 16)) 4)
39      ;; Append the suffix, because while the generated ID is unique to
40      ;; the application, other applications might otherwise generate
41      ;; the same ID via another algorithm.
42      (or suffix ".fsf"))))
43
44 (defun unique-id-m-number-base36 (num len)
45   (if (if (< len 0)
46           (<= num 0)
47         (= len 0))
48       ""
49     (concat (unique-id-m-number-base36 (/ num 36) (1- len))
50             (char-to-string (aref "zyxwvutsrqponmlkjihgfedcba9876543210"
51                                   (% num 36))))))
52
53 \f
54 ;;; Wanderlust 1.0.3: wl-draft.el, wl-mule.el
55
56 (defun unique-id-w-random-alphabet ()
57   (let ((alphabet '(?A ?B ?C ?D ?E ?F ?G ?H ?I ?J ?K ?L ?M
58                        ?N ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X ?Y ?Z)))
59     (nth (abs (% (random) 26)) alphabet)))
60
61 (defun unique-id-w ()
62   (let ((time (current-time)))
63     (format "%d.%d.%d.%d%c"
64             (car time) (nth 1 time) (nth 2 time)
65             (random 100000)
66             (unique-id-w-random-alphabet))))
67
68 \f
69 ;;; VM 6.75: vm-misc.el
70
71 (defun unique-id-v ()
72   (let ((time (current-time)))
73     (format "%d.%d.%d.%d"
74             (car time) (nth 1 time) (nth 2 time)
75             (random 1000000))))
76
77 \f
78 ;;; X-PGP-Sig 1.3.5.1
79
80 (defun unique-id-x (&optional length)
81   (let ((i (or length 16))
82         s)
83     (while (> i 0)
84       (setq i (1- i)
85             s (concat s (char-to-string (+ (/ (* 94 (% (abs (random)) 100))
86                                               100) 33)))))
87     s))
88
89 (provide 'unique-id)
90
91 ;;; unique-id.el ends here
92