diff options
-rw-r--r-- | src/library_uuid.js | 121 | ||||
-rw-r--r-- | src/modules.js | 2 | ||||
-rw-r--r-- | src/struct_info.json | 12 | ||||
-rw-r--r-- | system/include/uuid/uuid.h | 35 | ||||
-rw-r--r-- | tests/test_other.py | 12 | ||||
-rw-r--r-- | tests/uuid/test.c | 61 | ||||
-rw-r--r-- | tools/shared.py | 2 |
7 files changed, 243 insertions, 2 deletions
diff --git a/src/library_uuid.js b/src/library_uuid.js new file mode 100644 index 00000000..0c1bdf91 --- /dev/null +++ b/src/library_uuid.js @@ -0,0 +1,121 @@ +// Implementation of libuuid creating RFC4122 version 4 random UUIDs. + +mergeInto(LibraryManager.library, { + // Clear a 'compact' UUID. + uuid_clear: function(uu) { + // void uuid_clear(uuid_t uu); + _memset(uu, 0, 16); + }, + + // Compare whether or not two 'compact' UUIDs are the same. + // Returns an integer less than, equal to, or greater than zero if uu1 is found, respectively, to be + // lexigraphically less than, equal, or greater than uu2. + uuid_compare__deps: ['memcmp'], + uuid_compare: function(uu1, uu2) { + // int uuid_compare(const uuid_t uu1, const uuid_t uu2); + return _memcmp(uu1, uu2, 16); + }, + + // Copies the 'compact' UUID variable from src to dst. + uuid_copy: function(dst, src) { + // void uuid_copy(uuid_t dst, const uuid_t src); + _memcpy(dst, src, 16); + }, + + // Write a RFC4122 version 4 compliant UUID largely based on the method found in + // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript + // tweaked slightly in order to use the 'compact' UUID form used by libuuid. + uuid_generate: function(out) { + // void uuid_generate(uuid_t out); + var uuid = new Array(16); + var d = new Date().getTime(); + for (var i = 0; i < 16; i++) { + var r = (d + Math.random()*256)%256 | 0; + d = Math.floor(d/256); + uuid[i] = r; + } + + uuid[6] = (uuid[6] & 0x0F) | 0x40; + uuid[8] = (uuid[8] & 0x7F) | 0x80; + writeArrayToMemory(uuid, out); + }, + + // Compares the value of the supplied 'compact' UUID variable uu to the NULL value. + // If the value is equal to the NULL UUID, 1 is returned, otherwise 0 is returned. + uuid_is_null: function(uu) { + // int uuid_is_null(const uuid_t uu); + for (var i = 0; i < 4; i++, uu = (uu+4)|0) { + var val = {{{ makeGetValue('uu', 0, 'i32') }}}; + if (val) { + return 0; + } + } + return 1; + }, + + // converts the UUID string given by inp into the binary representation. The input UUID is a string of + // the form "%08x-%04x-%04x-%04x-%012x" 36 bytes plus the trailing '\0'. + // Upon successfully parsing the input string, 0 is returned, and the UUID is stored in the location + // pointed to by uu, otherwise -1 is returned. + uuid_parse: function(inp, uu) { + // int uuid_parse(const char *in, uuid_t uu); + var inp = Pointer_stringify(inp); + if (inp.length === 36) { + var i = 0; + var uuid = new Array(16); + inp.toLowerCase().replace(/[0-9a-f]{2}/g, function(byte) { + if (i < 16) { + uuid[i++] = parseInt(byte, 16); + } + }); + + if (i < 16) { + return -1; + } else { + writeArrayToMemory(uuid, uu); + return 0; + } + } else { + return -1; + } + }, + + // Convert a 'compact' form UUID to a string, if the upper parameter is supplied make the string upper case. + uuid_unparse: function(uu, out, upper) { + // void uuid_unparse(const uuid_t uu, char *out); + var i = 0; + var uuid = 'xxxx-xx-xx-xx-xxxxxx'.replace(/[x]/g, function(c) { + var r = upper ? ({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16).toUpperCase() : + ({{{ makeGetValue('uu', 'i', 'i8', 0, 1) }}}).toString(16); + r = (r.length === 1) ? '0' + r : r; // Zero pad single digit hex values + i++; + return r; + }); + writeStringToMemory(uuid, out); + }, + + // Convert a 'compact' form UUID to a lower case string. + uuid_unparse_lower__deps: ['uuid_unparse'], + uuid_unparse_lower: function(uu, out) { + // void uuid_unparse_lower(const uuid_t uu, char *out); + _uuid_unparse(uu, out); + }, + + // Convert a 'compact' form UUID to an upper case string. + uuid_unparse_upper__deps: ['uuid_unparse'], + uuid_unparse_upper: function(uu, out) { + // void uuid_unparse_upper(const uuid_t uu, char *out); + _uuid_unparse(uu, out, true); + }, + + uuid_type: function(uu) { + // int uuid_type(const uuid_t uu); + return {{{ cDefine('UUID_TYPE_DCE_RANDOM') }}}; + }, + + uuid_variant: function(uu) { + // int uuid_variant(const uuid_t uu); + return {{{ cDefine('UUID_VARIANT_DCE') }}}; + } +}); + diff --git a/src/modules.js b/src/modules.js index e2d3433f..c0b98f6f 100644 --- a/src/modules.js +++ b/src/modules.js @@ -424,7 +424,7 @@ var LibraryManager = { load: function() { if (this.library) return; - var libraries = ['library.js', 'library_path.js', 'library_fs.js', 'library_idbfs.js', 'library_memfs.js', 'library_nodefs.js', 'library_sockfs.js', 'library_tty.js', 'library_browser.js', 'library_sdl.js', 'library_gl.js', 'library_glut.js', 'library_xlib.js', 'library_egl.js', 'library_gc.js', 'library_jansson.js', 'library_openal.js', 'library_glfw.js'].concat(additionalLibraries); + var libraries = ['library.js', 'library_path.js', 'library_fs.js', 'library_idbfs.js', 'library_memfs.js', 'library_nodefs.js', 'library_sockfs.js', 'library_tty.js', 'library_browser.js', 'library_sdl.js', 'library_gl.js', 'library_glut.js', 'library_xlib.js', 'library_egl.js', 'library_gc.js', 'library_jansson.js', 'library_openal.js', 'library_glfw.js', 'library_uuid.js'].concat(additionalLibraries); for (var i = 0; i < libraries.length; i++) { eval(processMacros(preprocess(read(libraries[i])))); } diff --git a/src/struct_info.json b/src/struct_info.json index c136cc8b..a22851b6 100644 --- a/src/struct_info.json +++ b/src/struct_info.json @@ -1061,5 +1061,17 @@ "patch" ] } + }, + { + "file": "uuid/uuid.h", + "defines": [ + "UUID_VARIANT_NCS", + "UUID_VARIANT_DCE", + "UUID_VARIANT_MICROSOFT", + "UUID_VARIANT_OTHER", + "UUID_TYPE_DCE_TIME", + "UUID_TYPE_DCE_RANDOM" + ], + "structs": {} } ] diff --git a/system/include/uuid/uuid.h b/system/include/uuid/uuid.h new file mode 100644 index 00000000..0a84e02c --- /dev/null +++ b/system/include/uuid/uuid.h @@ -0,0 +1,35 @@ + +#ifndef _UUID_H +#define _UUID_H + +typedef unsigned char uuid_t[16]; + +#define UUID_VARIANT_NCS 0 +#define UUID_VARIANT_DCE 1 +#define UUID_VARIANT_MICROSOFT 2 +#define UUID_VARIANT_OTHER 3 + +#define UUID_TYPE_DCE_TIME 1 +#define UUID_TYPE_DCE_RANDOM 4 + +#ifdef __cplusplus +extern "C" { +#endif + +void uuid_clear(uuid_t uu); +int uuid_compare(const uuid_t uu1, const uuid_t uu2); +void uuid_copy(uuid_t dst, const uuid_t src); +void uuid_generate(uuid_t out); +int uuid_is_null(const uuid_t uu); +int uuid_parse(const char *in, uuid_t uu); +void uuid_unparse(const uuid_t uu, char *out); +void uuid_unparse_lower(const uuid_t uu, char *out); +void uuid_unparse_upper(const uuid_t uu, char *out); +int uuid_type(const uuid_t uu); +int uuid_variant(const uuid_t uu); + +#ifdef __cplusplus +} +#endif + +#endif /* _UUID_H */ diff --git a/tests/test_other.py b/tests/test_other.py index 43bd3025..f229fef1 100644 --- a/tests/test_other.py +++ b/tests/test_other.py @@ -2196,3 +2196,15 @@ mergeInto(LibraryManager.library, { process = Popen([PYTHON, EMCC, '-c', path_from_root('tests', 'hello_world.c'), '-o', outdir, '--default-obj-ext', 'obj'], stdout=PIPE, stderr=PIPE) process.communicate() assert(os.path.isfile(outdir + 'hello_world.obj')) + + def test_uuid(self): + # Run with ./runner.py other.test_uuid + Popen([PYTHON, EMCC, path_from_root('tests', 'uuid', 'test.c'), '-o', path_from_root('tests', 'uuid', 'test.js')], stdout=PIPE, stderr=PIPE).communicate() + + out = run_js(path_from_root('tests', 'uuid', 'test.js'), full_output=True) + print out + + # Tidy up files that might have been created by this test. + try_delete(path_from_root('tests', 'uuid', 'test.js')) + try_delete(path_from_root('tests', 'uuid', 'test.js.map')) + diff --git a/tests/uuid/test.c b/tests/uuid/test.c new file mode 100644 index 00000000..ea3b116c --- /dev/null +++ b/tests/uuid/test.c @@ -0,0 +1,61 @@ + +#include <uuid/uuid.h> +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> + +int isUUID(char* p, int upper) { + char* p1 = p; + do { + if (!(isxdigit(*p1) || (*p1 == '-')) || (upper && islower(*p1)) || (!upper && isupper(*p1))) { + return 0; + } else { + } + } while (*++p1 != 0); + + if ((p[8] == '-') && (p[13] == '-') && (p[18] == '-') && (p[23] == '-')) { + return 1; + } else { + return 0; + } +} + +int main() { + uuid_t uuid; + uuid_t uuid1; + uuid_t uuid2; + uuid_t empty_uuid = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + uuid_generate(uuid); + + assert(uuid_is_null(uuid) == 0); + assert(uuid_type(uuid) == UUID_TYPE_DCE_RANDOM); + assert(uuid_variant(uuid) == UUID_VARIANT_DCE); + + char *generated = (char *)malloc(37*sizeof(char)); + uuid_unparse(uuid, generated); + assert(isUUID(generated, 0) == 1); // Check it's a valid lower case UUID string. + printf("\nuuid = %s\n", generated); + + assert(uuid_parse(generated, uuid1) == 0); // Check the generated UUID parses correctly into a compact UUID. + assert(uuid_compare(uuid1, uuid) == 0); // Compare the parsed UUID with the original. + + uuid_unparse_lower(uuid, generated); + assert(isUUID(generated, 0) == 1); // Check it's a valid lower case UUID string. + printf("uuid = %s\n", generated); + + uuid_unparse_upper(uuid, generated); + assert(isUUID(generated, 1) == 1); // Check it's a valid upper case UUID string. + printf("uuid = %s\n", generated); + + + uuid_copy(uuid2, uuid); + assert(uuid_compare(uuid2, uuid) == 0); + + uuid_clear(uuid); + assert(uuid_compare(empty_uuid, uuid) == 0); + + assert(uuid_is_null(uuid) == 1); + + return 0; +} + diff --git a/tools/shared.py b/tools/shared.py index eb1c63be..5b8d43aa 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -322,7 +322,7 @@ def find_temp_directory(): # we re-check sanity when the settings are changed) # We also re-check sanity and clear the cache when the version changes -EMSCRIPTEN_VERSION = '1.8.0' +EMSCRIPTEN_VERSION = '1.8.1' def generate_sanity(): return EMSCRIPTEN_VERSION + '|' + get_llvm_target() + '|' + LLVM_ROOT |