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