Fix bugs in previous commit that caused FTBFS in synfig and ETL FTBFS with older...
[synfig.git] / synfig-core / tags / synfig_0_61_07_rc3 / 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 #define HASH_MAP_H <ext/hash_map>
27 #define SUBTRACT_RNG_H <ext/functional>
28
29 #ifdef USING_PCH
30 #       include "pch.h"
31 #else
32 #ifdef HAVE_CONFIG_H
33 #       include <config.h>
34 #endif
35
36 #include "guid.h"
37 #include "quick_rng.h"
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <ETL/stringf>
43 #include <functional>
44
45 #ifdef _DEBUG
46 #include "general.h"
47 #endif
48
49 #ifdef HASH_MAP_H
50 #include HASH_MAP_H
51 using namespace __gnu_cxx;
52 #endif
53
54 #ifdef SUBTRACT_RNG_H
55 #include SUBTRACT_RNG_H
56 using namespace __gnu_cxx;
57 #endif
58
59 #ifdef _WIN32
60 #include <windows.h>
61 #endif
62
63 #endif
64
65 /* === U S I N G =========================================================== */
66
67 using namespace std;
68 using namespace etl;
69 using namespace synfig;
70
71 /* === M A C R O S ========================================================= */
72
73 #define MANUAL_GUID_CALC
74
75 /* === G L O B A L S ======================================================= */
76
77 /* === P R O C E D U R E S ================================================= */
78
79 /* === M E T H O D S ======================================================= */
80
81 #define GUID_RNG quick_rng
82 //#define GUID_RNG subtractive_rng
83
84
85
86 #ifdef MANUAL_GUID_CALC
87 #include <time.h>
88 static GUID_RNG _a, _b;
89 static void _set_up_rand_long_long(uint64_t &x);
90 static void _get_rand_long_long(uint64_t &x);
91 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
92 static void _set_up_rand_long_long(uint64_t &x)
93 {
94 #ifdef _DEBUG
95         synfig::info("Starting up GUID system...");
96 #endif
97         _a=GUID_RNG(time(0)+clock());
98         _b=GUID_RNG(clock());
99         get_rand_long_long=_get_rand_long_long;
100         _get_rand_long_long(x);
101 }
102
103 static void _get_rand_long_long(uint64_t &x)
104 {
105         //subtractive_rng _c(clock());
106         unsigned short* data(reinterpret_cast<unsigned short *>(&x));
107         data[0]=_a(65536);
108         data[1]=_a(65536);
109         data[2]=_a(65536);
110         data[3]=_a(65536);
111 }
112
113 #else
114 // Use OS-Dependent method
115
116 #ifdef _WIN32
117 // Win32
118 static void get_rand_long_long(uint64_t &x)
119 {
120         _GUID* guid(reinterpret_cast<_GUID*>(&x));
121         CoCreateGuid(guid);
122 }
123
124 #else
125 // Unix
126 static int rand_fd;
127 static void _set_up_rand_long_long(uint64_t &x);
128 static void _get_rand_long_long(uint64_t &x);
129 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
130 static void _set_up_rand_long_long(uint64_t &x)
131 {
132 #ifdef _DEBUG
133         synfig::info("Starting up GUID system...");
134 #endif
135         rand_fd=open("/dev/urandom",O_RDONLY);
136         get_rand_long_long=_get_rand_long_long;
137         _get_rand_long_long(x);
138 }
139
140 static void _get_rand_long_long(uint64_t &x){   read(rand_fd,&x,sizeof(x));}
141
142 #endif
143 #endif
144
145
146
147 void
148 synfig::GUID::make_unique()
149 {
150         get_rand_long_long(data.u_64.a);
151         get_rand_long_long(data.u_64.b);
152 }
153
154 synfig::GUID
155 synfig::GUID::hasher(const String& str)
156 {
157 #ifdef HASH_MAP_H
158         hash<const char*> string_hash_;
159         const unsigned int seed(
160                 string_hash_(
161                         str.c_str()
162                 )
163         );
164 #else
165         const unsigned int seed(0x3B642879);
166         for(int i=0;i<str.size();i++)
167         {
168                 seed^=(seed*str[i])*i
169                 seed=(seed>>(32-(i%24)))^(seed<<(i%24))
170         }
171 #endif
172
173         GUID_RNG random(seed);
174         GUID ret(0);
175         ret.data.u_32.a=random(~(unsigned int)0);
176         ret.data.u_32.b=random(~(unsigned int)0);
177         ret.data.u_32.c=random(~(unsigned int)0);
178         ret.data.u_32.d=random(~(unsigned int)0);
179         return ret;
180 }
181
182 synfig::GUID
183 synfig::GUID::hasher(int i)
184 {
185         GUID ret(0);
186         GUID_RNG random(i);
187         ret.data.u_32.a=random(~(unsigned int)0);
188         ret.data.u_32.b=random(~(unsigned int)0);
189         ret.data.u_32.c=random(~(unsigned int)0);
190         ret.data.u_32.d=random(~(unsigned int)0);
191         return ret;
192 }
193
194 String
195 synfig::GUID::get_string()const
196 {
197         return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
198 }
199
200 synfig::GUID::GUID(const String &str)
201 {
202         strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);
203 }