1 /**
2 * \file os_uuid.c
3 * \brief Create a new UUID.
4 * \author Copyright (c) 2002-2012 Jason Perkins and the Premake project
5 */
6
7 #include "premake.h"
8
9 #if PLATFORM_WINDOWS
10 #include <Objbase.h>
11 #endif
12
13
14 /*
15 * Pull off the four lowest bytes of a value and add them to my array,
16 * without the help of the determinately sized C99 data types that
17 * are not yet universally supported.
18 */
add(unsigned char * bytes,int offset,uint32_t value)19 static void add(unsigned char* bytes, int offset, uint32_t value)
20 {
21 int i;
22 for (i = 0; i < 4; ++i)
23 {
24 bytes[offset++] = (unsigned char)(value & 0xff);
25 value >>= 8;
26 }
27 }
28
29
os_uuid(lua_State * L)30 int os_uuid(lua_State* L)
31 {
32 char uuid[38];
33 unsigned char bytes[16];
34
35 /* If a name argument is supplied, build the UUID from that. For speed we
36 * are using a simple DBJ2 hashing function; if this isn't sufficient we
37 * can switch to a full RFC 4122 §4.3 implementation later. */
38 const char* name = luaL_optstring(L, 1, NULL);
39 if (name != NULL)
40 {
41 add(bytes, 0, do_hash(name, 0));
42 add(bytes, 4, do_hash(name, 'L'));
43 add(bytes, 8, do_hash(name, 'u'));
44 add(bytes, 12, do_hash(name, 'a'));
45 }
46
47 /* If no name is supplied, try to build one properly */
48 else
49 {
50 #if PLATFORM_WINDOWS
51 CoCreateGuid((GUID*)bytes);
52 #else
53 int result;
54
55 /* not sure how to get a UUID here, so I fake it */
56 FILE* rnd = fopen("/dev/urandom", "rb");
57 result = fread(bytes, 16, 1, rnd);
58 fclose(rnd);
59 if (!result)
60 {
61 return 0;
62 }
63 #endif
64 }
65
66 sprintf(uuid, "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X",
67 bytes[0], bytes[1], bytes[2], bytes[3],
68 bytes[4], bytes[5],
69 bytes[6], bytes[7],
70 bytes[8], bytes[9],
71 bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]);
72
73 lua_pushstring(L, uuid);
74 return 1;
75 }
76