9a5f2c239bcb353ed4530eb2caebd6320e5b52af
[synfig.git] / synfig-core / trunk / src / synfig / guid.cpp
1 /* === S Y N F I G ========================================================= */
2 /*!     \file guid.cpp
3 **      \brief Template File
4 **
5 **      $Id$
6 **
7 **      \legal
8 **      Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 **      Copyright (c) 2007 Chris Moore
10 **
11 **      This package is free software; you can redistribute it and/or
12 **      modify it under the terms of the GNU General Public License as
13 **      published by the Free Software Foundation; either version 2 of
14 **      the License, or (at your option) any later version.
15 **
16 **      This package is distributed in the hope that it will be useful,
17 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
18 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 **      General Public License for more details.
20 **      \endlegal
21 */
22 /* ========================================================================= */
23
24 /* === H E A D E R S ======================================================= */
25
26 #ifdef USING_PCH
27 #       include "pch.h"
28 #else
29 #ifdef HAVE_CONFIG_H
30 #       include <config.h>
31 #endif
32
33 #include "guid.h"
34 #include "quick_rng.h"
35 #include <sys/types.h>
36 #include <sys/stat.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <ETL/stringf>
40 #include <functional>
41
42 #ifdef _DEBUG
43 #include "general.h"
44 #endif
45
46 #ifdef HASH_MAP_H
47 #include HASH_MAP_H
48 #include FUNCTIONAL_H
49 #endif
50
51 #ifdef _WIN32
52 #include <windows.h>
53 #endif
54
55 #endif
56
57 /* === U S I N G =========================================================== */
58
59 using namespace std;
60 using namespace etl;
61 using namespace synfig;
62
63 /* === M A C R O S ========================================================= */
64
65 #define MANUAL_GUID_CALC
66
67 /* === G L O B A L S ======================================================= */
68
69 /* === P R O C E D U R E S ================================================= */
70
71 /* === M E T H O D S ======================================================= */
72
73 #define GUID_RNG quick_rng
74 //#define GUID_RNG subtractive_rng
75
76
77
78 #ifdef MANUAL_GUID_CALC
79 #include <time.h>
80 static GUID_RNG _a, _b;
81 static void _set_up_rand_long_long(uint64_t &x);
82 static void _get_rand_long_long(uint64_t &x);
83 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
84 static void _set_up_rand_long_long(uint64_t &x)
85 {
86 #ifdef _DEBUG
87         // synfig::info("Starting up GUID system...");
88 #endif
89         _a=GUID_RNG(time(0)+clock());
90         _b=GUID_RNG(clock());
91         get_rand_long_long=_get_rand_long_long;
92         _get_rand_long_long(x);
93 }
94
95 static void _get_rand_long_long(uint64_t &x)
96 {
97         //subtractive_rng _c(clock());
98         unsigned short* data(reinterpret_cast<unsigned short *>(&x));
99         data[0]=_a(65536);
100         data[1]=_a(65536);
101         data[2]=_a(65536);
102         data[3]=_a(65536);
103 }
104
105 #else
106 // Use OS-Dependent method
107
108 #ifdef _WIN32
109 // Win32
110 static void get_rand_long_long(uint64_t &x)
111 {
112         _GUID* guid(reinterpret_cast<_GUID*>(&x));
113         CoCreateGuid(guid);
114 }
115
116 #else
117 // Unix
118 static int rand_fd;
119 static void _set_up_rand_long_long(uint64_t &x);
120 static void _get_rand_long_long(uint64_t &x);
121 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
122 static void _set_up_rand_long_long(uint64_t &x)
123 {
124 #ifdef _DEBUG
125         // synfig::info("Starting up GUID system...");
126 #endif
127         rand_fd=open("/dev/urandom",O_RDONLY);
128         get_rand_long_long=_get_rand_long_long;
129         _get_rand_long_long(x);
130 }
131
132 static void _get_rand_long_long(uint64_t &x){   read(rand_fd,&x,sizeof(x));}
133
134 #endif
135 #endif
136
137
138
139 void
140 synfig::GUID::make_unique()
141 {
142         get_rand_long_long(data.u_64.a);
143         get_rand_long_long(data.u_64.b);
144 }
145
146 synfig::GUID
147 synfig::GUID::hasher(const String& str)
148 {
149 #ifdef HASH_MAP_H
150         /* http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1456.html says:
151          *
152          *   "Some earlier hash table implementations gave char* special
153          *    treatment: it specialized the default hash function to look
154          *    at character array being pointed to, rather than the pointer
155          *    itself. This proposal removes that special treatment."
156          *
157          * Unfortunately, the older implementation doesn't seem to want to
158          * accept Strings, so we're left with this conditional compilation.
159          */
160 # ifdef FUNCTIONAL_HASH_ON_STRING
161         HASH_MAP_NAMESPACE::hash<String> string_hash_;
162         const unsigned int seed(string_hash_(str));
163 # else  // FUNCTIONAL_HASH_ON_STRING
164         HASH_MAP_NAMESPACE::hash<const char*> string_hash_;
165         const unsigned int seed(string_hash_(str.c_str()));
166 # endif  // FUNCTIONAL_HASH_ON_STRING
167 #else  // HASH_MAP_H
168         unsigned int seed(0x3B642879);
169         for(unsigned int i=0;i<str.size();i++)
170         {
171                 seed^=(seed*str[i])*i;
172                 seed=(seed>>(32-(i%24)))^(seed<<(i%24));
173         }
174 #endif  // HASH_MAP_H
175
176         GUID_RNG random(seed);
177         GUID ret(0);
178         ret.data.u_32.a=random(~(unsigned int)0);
179         ret.data.u_32.b=random(~(unsigned int)0);
180         ret.data.u_32.c=random(~(unsigned int)0);
181         ret.data.u_32.d=random(~(unsigned int)0);
182         return ret;
183 }
184
185 synfig::GUID
186 synfig::GUID::hasher(int i)
187 {
188         GUID ret(0);
189         GUID_RNG random(i);
190         ret.data.u_32.a=random(~(unsigned int)0);
191         ret.data.u_32.b=random(~(unsigned int)0);
192         ret.data.u_32.c=random(~(unsigned int)0);
193         ret.data.u_32.d=random(~(unsigned int)0);
194         return ret;
195 }
196
197 String
198 synfig::GUID::get_string()const
199 {
200         return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
201 }
202
203 synfig::GUID::GUID(const String &str)
204 {
205         strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);
206 }