1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
9 ** Copyright (c) 2007 Chris Moore
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.
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.
22 /* ========================================================================= */
24 /* === H E A D E R S ======================================================= */
26 #define HASH_MAP_H <ext/hash_map>
27 #define SUBTRACT_RNG_H <ext/functional>
37 #include "quick_rng.h"
38 #include <sys/types.h>
42 #include <ETL/stringf>
51 using namespace __gnu_cxx;
55 #include SUBTRACT_RNG_H
56 using namespace __gnu_cxx;
65 /* === U S I N G =========================================================== */
69 using namespace synfig;
71 /* === M A C R O S ========================================================= */
73 #define MANUAL_GUID_CALC
75 /* === G L O B A L S ======================================================= */
77 /* === P R O C E D U R E S ================================================= */
79 /* === M E T H O D S ======================================================= */
81 #define GUID_RNG quick_rng
82 //#define GUID_RNG subtractive_rng
86 #ifdef MANUAL_GUID_CALC
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)
95 synfig::info("Starting up GUID system...");
97 _a=GUID_RNG(time(0)+clock());
99 get_rand_long_long=_get_rand_long_long;
100 _get_rand_long_long(x);
103 static void _get_rand_long_long(uint64_t &x)
105 //subtractive_rng _c(clock());
106 unsigned short* data(reinterpret_cast<unsigned short *>(&x));
114 // Use OS-Dependent method
118 static void get_rand_long_long(uint64_t &x)
120 _GUID* guid(reinterpret_cast<_GUID*>(&x));
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)
133 synfig::info("Starting up GUID system...");
135 rand_fd=open("/dev/urandom",O_RDONLY);
136 get_rand_long_long=_get_rand_long_long;
137 _get_rand_long_long(x);
140 static void _get_rand_long_long(uint64_t &x){ read(rand_fd,&x,sizeof(x));}
148 synfig::GUID::make_unique()
150 get_rand_long_long(data.u_64.a);
151 get_rand_long_long(data.u_64.b);
155 synfig::GUID::hasher(const String& str)
158 hash<const char*> string_hash_;
159 const unsigned int seed(
165 const unsigned int seed(0x3B642879);
166 for(int i=0;i<str.size();i++)
168 seed^=(seed*str[i])*i
169 seed=(seed>>(32-(i%24)))^(seed<<(i%24))
173 GUID_RNG random(seed);
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);
183 synfig::GUID::hasher(int 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);
195 synfig::GUID::get_string()const
197 return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
200 synfig::GUID::GUID(const String &str)
202 strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);