View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2012-2024, VU University Amsterdam
    7                              SWI-Prolog Solutions b.v.
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(uuid,
   37          [ uuid/1,                     % -UUID
   38            uuid/2,                     % -UUID, +Options
   39            is_uuid/1,                  % @UUID
   40            uuid_property/2             % +UUID, ?Property
   41          ]).   42:- autoload(library(apply), [maplist/2]).   43
   44/** <module> Universally Unique Identifier (UUID) Library
   45
   46The library provides operations on UUIDs.   Please consult other sources
   47for understanding UUIDs and  the  implications   of  the  different UUID
   48versions.  Some typical calls are given below:
   49
   50  ==
   51  ?- uuid(X).
   52  X = 'ea6589fa-19dd-11e2-8a49-001d92e1879d'.
   53
   54  ?- uuid(X, [url('http://www.swi-prolog.org')]).
   55  X = '73a07870-6a90-3f2e-ae2b-ffa538dc7c2c'.
   56  ==
   57
   58@tbd Compare UUIDs, extract time and version from UUIDs
   59@see http://www.ossp.org/pkg/lib/uuid/
   60@see https://en.wikipedia.org/wiki/Universally_unique_identifier
   61*/
   62
   63link_uuid :-
   64    catch(load_foreign_library(foreign(uuid)), _, true).
   65
   66:- initialization(link_uuid, now).   67
   68%!  uuid(-UUID) is det.
   69%
   70%   UUID is an atom representing a  new   UUID.  This is the same as
   71%   calling uuid(UUID, []).  See uuid/2 for options.
   72
   73uuid(UUID) :-
   74    uuid(UUID, []).
   75
   76%!  uuid(-UUID, +Options) is det.
   77%
   78%   Create a new UUID according to   Options.  The following options
   79%   are defined:
   80%
   81%     * version(+Versions)
   82%     Integer in the range 1..5, which specifies the UUID version
   83%     that is created.  Default is 1.
   84%
   85%     * dns(DNS)
   86%     * url(URL)
   87%     * oid(OID)
   88%     * x500(X500)
   89%     Provide additional context information for UUIDs using version
   90%     3 or 5.  If there is no explicit version option, UUID version
   91%     3 is used.
   92%
   93%     * format(+Format)
   94%     Representation of the UUID.  Default is =atom=, yielding atoms
   95%     such as =|8304efdd-bd6e-5b7c-a27f-83f3f05c64e0|=. The
   96%     alternative is =integer=, returning a large integer that
   97%     represents the 128 bits of the UUID.
   98%
   99%   If SWI-Prolog was not built with the  OSSP UUID dependency library a
  100%   simple Prolog alternative that  only   implements  version  4 random
  101%   UUIDs is provided. In this case  the   default  version is 4 and the
  102%   only admissible options are version(4) and format(Format).
  103
  104:- if(current_predicate(ossp_uuid/2)).  105uuid(UUID, Options) :-
  106    ossp_uuid(UUID, Options).
  107
  108:- else.  109
  110uuid(UUID, []) :-
  111    !,
  112    random_uuid(UUID).
  113uuid(UUID, Options) :-
  114    option(version(4), Options, 4),
  115    option(format(Format), Options, atom),
  116    (   Format == atom
  117    ->  !, random_uuid(UUID)
  118    ;   Format == integer
  119    ->  !, random_int_uuid(UUID)
  120    ).
  121uuid(_UUID, Options) :-
  122    domain_error(uuid_options, Options).
  123
  124random_uuid(UUID) :-
  125    Version = 4,
  126    A is random(0xffffffff),
  127    B is random(0xffff),
  128    C is random(0x0fff) \/ Version<<12,
  129    D is random(0x3fff) \/ 0x8000,
  130    E is random(0xffffffffffff),
  131    format(atom(UUID),
  132           '~`0t~16r~8+-~|\c
  133            ~`0t~16r~4+-~|\c
  134            ~`0t~16r~4+-~|\c
  135            ~`0t~16r~4+-~|\c
  136            ~`0t~16r~12+', [A,B,C,D,E]).
  137
  138random_int_uuid(UUID) :-
  139    Version = 4,
  140    A is random(0xffffffff),
  141    B is random(0xffff),
  142    C is random(0x0fff) \/ Version<<12,
  143    D is random(0x3fff) \/ 0x8000,
  144    E is random(0xffffffffffff),
  145    UUID is (A<<96)+(B<<80)+(C<<84)+(D<<48)+E.
  146
  147:- endif.  148
  149%!  uuid_property(+UUID, ?Property)
  150%
  151%   True when UUID is a property of the given UUID. Supported properties
  152%   are:
  153%
  154%     - version(V)
  155%       Return the version of the UUID (1..5)
  156%     - time(-Stamp)
  157%       Time using SWI-Prolog's time stamp (float with seconds
  158%       since January 1, 1970, UTC).  Only for version 1 and 2
  159%       UUIDs
  160%
  161%   @tbd Implement more properties.
  162
  163uuid_property(UUID, P) :-
  164    property_uuid(P, UUID).
  165
  166property_uuid(version(V), UUID) :-
  167    split_string(UUID, "-", "", [_A,_B,C,_D,_E]),
  168    sub_string(C, 0, 1, _, F),
  169    char_type(F, xdigit(V)).
  170property_uuid(time(Time), UUID) :-
  171    split_string(UUID, "-", "", [A,B,C,_D,_E]),
  172    sub_atom(C, 0, 1, _, F),
  173    has_time(F),
  174    sub_string(C, 1, _, 0, T),
  175    atomics_to_string(["0x",T,B,A], Hex),
  176    number_string(Nanos, Hex),
  177    Offset is 24*60*60*141427*10 000 000,
  178    Time is (Nanos-Offset)/10000000.0.
  179
  180has_time('1').
  181has_time('2').
  182
  183%!  is_uuid(@UUID) is semidet.
  184%
  185%   True when UUID is a UUID represented as an atom. It merely validates
  186%   that the length is 36 characters, there   are `-` at the right place
  187%   and all other characters are hexadecimal.
  188
  189is_uuid(UUID) :-
  190    atom(UUID),
  191    atom_length(UUID, 36),
  192    atom_codes(UUID, Codes),
  193    Codes = [ _,_,_,_,_,_,_,_, 0'-,
  194              _,_,_,_, 0'-,
  195              _,_,_,_, 0'-,
  196              _,_,_,_, 0'-,
  197              _,_,_,_,_,_,_,_,_,_,_,_
  198            ],
  199    maplist(hex_or_minus, Codes).
  200
  201hex_or_minus(0'-) :-
  202    !.
  203hex_or_minus(Code) :-
  204    code_type(Code, xdigit(_))