1
// Implementation of libuuid creating RFC4122 version 4 random UUIDs.
3
mergeInto(LibraryManager.library, {
4
// Clear a 'compact' UUID.
5
uuid_clear: function(uu) {
6
// void uuid_clear(uuid_t uu);
10
// Compare whether or not two 'compact' UUIDs are the same.
11
// Returns an integer less than, equal to, or greater than zero if uu1 is found, respectively, to be
12
// lexigraphically less than, equal, or greater than uu2.
13
uuid_compare__deps: ['memcmp'],
14
uuid_compare: function(uu1, uu2) {
15
// int uuid_compare(const uuid_t uu1, const uuid_t uu2);
16
return _memcmp(uu1, uu2, 16);
19
// Copies the 'compact' UUID variable from src to dst.
20
uuid_copy: function(dst, src) {
21
// void uuid_copy(uuid_t dst, const uuid_t src);
22
_memcpy(dst, src, 16);
25
// Write a RFC4122 version 4 compliant UUID largely based on the method found in
26
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
27
// tweaked slightly in order to use the 'compact' UUID form used by libuuid.
28
uuid_generate: function(out) {
29
// void uuid_generate(uuid_t out);
32
if (ENVIRONMENT_IS_NODE) {
33
// If Node.js try to use crypto.randomBytes
35
var rb = require('crypto').randomBytes;
38
} else if (ENVIRONMENT_IS_WEB &&
39
typeof(window.crypto) !== 'undefined' &&
40
typeof(window.crypto.getRandomValues) !== 'undefined') {
41
// If crypto.getRandomValues is available try to use it.
42
uuid = new Uint8Array(16);
43
window.crypto.getRandomValues(uuid);
46
// Fall back to Math.random if a higher quality random number generator is not available.
49
var d = new Date().getTime();
50
for (var i = 0; i < 16; i++) {
51
var r = (d + Math.random()*256)%256 | 0;
52
d = Math.floor(d/256);
57
uuid[6] = (uuid[6] & 0x0F) | 0x40;
58
uuid[8] = (uuid[8] & 0x7F) | 0x80;
59
writeArrayToMemory(uuid, out);
62
// Compares the value of the supplied 'compact' UUID variable uu to the NULL value.
63
// If the value is equal to the NULL UUID, 1 is returned, otherwise 0 is returned.
64
uuid_is_null: function(uu) {
65
// int uuid_is_null(const uuid_t uu);
66
for (var i = 0; i < 4; i++, uu = (uu+4)|0) {
67
var val = {{{ makeGetValue('uu', 0, 'i32') }}};
75
// converts the UUID string given by inp into the binary representation. The input UUID is a string of
76
// the form "%08x-%04x-%04x-%04x-%012x" 36 bytes plus the trailing '\0'.
77
// Upon successfully parsing the input string, 0 is returned, and the UUID is stored in the location
78
// pointed to by uu, otherwise -1 is returned.
79
uuid_parse: function(inp, uu) {
80
// int uuid_parse(const char *in, uuid_t uu);
81
var inp = Pointer_stringify(inp);
82
if (inp.length === 36) {
84
var uuid = new Array(16);
85
inp.toLowerCase().replace(/[0-9a-f]{2}/g, function(byte) {
87
uuid[i++] = parseInt(byte, 16);
94
writeArrayToMemory(uuid, uu);
102
// Convert a 'compact' form UUID to a string, if the upper parameter is supplied make the string upper case.
103
uuid_unparse: function(uu, out, upper) {
104
// void uuid_unparse(const uuid_t uu, char *out);
106
var uuid = 'xxxx-xx-xx-xx-xxxxxx'.replace(/[x]/g, function(c) {
107
var r = upper ? ({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16).toUpperCase() :
108
({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16);
109
r = (r.length === 1) ? '0' + r : r; // Zero pad single digit hex values
113
writeStringToMemory(uuid, out);
116
// Convert a 'compact' form UUID to a lower case string.
117
uuid_unparse_lower__deps: ['uuid_unparse'],
118
uuid_unparse_lower: function(uu, out) {
119
// void uuid_unparse_lower(const uuid_t uu, char *out);
120
_uuid_unparse(uu, out);
123
// Convert a 'compact' form UUID to an upper case string.
124
uuid_unparse_upper__deps: ['uuid_unparse'],
125
uuid_unparse_upper: function(uu, out) {
126
// void uuid_unparse_upper(const uuid_t uu, char *out);
127
_uuid_unparse(uu, out, true);
130
uuid_type: function(uu) {
131
// int uuid_type(const uuid_t uu);
132
return {{{ cDefine('UUID_TYPE_DCE_RANDOM') }}};
135
uuid_variant: function(uu) {
136
// int uuid_variant(const uuid_t uu);
137
return {{{ cDefine('UUID_VARIANT_DCE') }}};