~ubuntu-branches/ubuntu/vivid/emscripten/vivid-proposed

« back to all changes in this revision

Viewing changes to src/library_uuid.js

  • Committer: Package Import Robot
  • Author(s): Sylvestre Ledru
  • Date: 2014-01-19 14:12:40 UTC
  • mfrom: (4.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20140119141240-nfiw0p8033oitpfz
Tags: 1.9.0~20140119~7dc8c2f-1
* New snapshot release (Closes: #733714)
* Provide sources for javascript and flash. Done in orig-tar.sh
  Available in third_party/websockify/include/web-socket-js/src/
  (Closes: #735903)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Implementation of libuuid creating RFC4122 version 4 random UUIDs.
 
2
 
 
3
mergeInto(LibraryManager.library, {
 
4
  // Clear a 'compact' UUID.
 
5
  uuid_clear: function(uu) {
 
6
    // void uuid_clear(uuid_t uu);
 
7
    _memset(uu, 0, 16);
 
8
  },
 
9
 
 
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);
 
17
  },
 
18
 
 
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);
 
23
  },
 
24
 
 
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);
 
30
    var uuid = null;
 
31
 
 
32
    if (ENVIRONMENT_IS_NODE) {
 
33
      // If Node.js try to use crypto.randomBytes
 
34
      try {
 
35
        var rb = require('crypto').randomBytes;
 
36
        uuid = rb(16);
 
37
      } catch(e) {}
 
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);
 
44
    }
 
45
 
 
46
    // Fall back to Math.random if a higher quality random number generator is not available.
 
47
    if (!uuid) {
 
48
      uuid = new Array(16);
 
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);
 
53
        uuid[i] = r;
 
54
      }
 
55
    }
 
56
 
 
57
    uuid[6] = (uuid[6] & 0x0F) | 0x40;
 
58
    uuid[8] = (uuid[8] & 0x7F) | 0x80;
 
59
    writeArrayToMemory(uuid, out);
 
60
  },
 
61
 
 
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') }}};
 
68
      if (val) {
 
69
        return 0;
 
70
      }
 
71
    }
 
72
    return 1;
 
73
  },
 
74
 
 
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) {
 
83
      var i = 0;
 
84
      var uuid = new Array(16);
 
85
      inp.toLowerCase().replace(/[0-9a-f]{2}/g, function(byte) {
 
86
        if (i < 16) {
 
87
          uuid[i++] = parseInt(byte, 16);
 
88
        }
 
89
      });
 
90
 
 
91
      if (i < 16) {
 
92
        return -1;
 
93
      } else {
 
94
        writeArrayToMemory(uuid, uu);
 
95
        return 0;
 
96
      }
 
97
    } else {
 
98
      return -1;
 
99
    }
 
100
  },
 
101
 
 
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);
 
105
    var i = 0;
 
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
 
110
      i++;
 
111
      return r;
 
112
    });
 
113
    writeStringToMemory(uuid, out);
 
114
  },
 
115
 
 
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);
 
121
  },
 
122
 
 
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);
 
128
  },
 
129
 
 
130
  uuid_type: function(uu) {
 
131
    // int uuid_type(const uuid_t uu);
 
132
    return {{{ cDefine('UUID_TYPE_DCE_RANDOM') }}};
 
133
  },
 
134
 
 
135
  uuid_variant: function(uu) {
 
136
    // int uuid_variant(const uuid_t uu);
 
137
    return {{{ cDefine('UUID_VARIANT_DCE') }}};
 
138
  }
 
139
});
 
140