1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
5 ** $Id: guid.cpp,v 1.4 2005/01/13 06:48:39 darco Exp $
8 ** Copyright (c) 2002-2005 Robert B. Quattlebaum Jr., Adrian Bentley
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.
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.
21 /* ========================================================================= */
23 /* === H E A D E R S ======================================================= */
25 #define HASH_MAP_H <ext/hash_map>
26 #define SUBTRACT_RNG_H <ext/functional>
36 #include <sys/types.h>
40 #include <ETL/stringf>
49 using namespace __gnu_cxx;
53 #include SUBTRACT_RNG_H
54 using namespace __gnu_cxx;
63 /* === U S I N G =========================================================== */
67 using namespace synfig;
69 /* === M A C R O S ========================================================= */
71 #define MANUAL_GUID_CALC
73 /* === G L O B A L S ======================================================= */
75 /* === P R O C E D U R E S ================================================= */
77 /* === M E T H O D S ======================================================= */
79 // A fast 32-bit linear congruential random number generator
84 quick_rng(unsigned long seed=0):next(seed) { }
86 void set_seed(unsigned long x)
93 static const unsigned long a(1664525);
94 static const unsigned long c(1013904223);
106 static const float m(int(65535));
108 return float(i16())/m;
111 unsigned long operator()(const unsigned long& m)
123 #define GUID_RNG quick_rng
124 //#define GUID_RNG subtractive_rng
128 #ifdef MANUAL_GUID_CALC
130 static GUID_RNG _a, _b;
131 static void _set_up_rand_long_long(uint64_t &x);
132 static void _get_rand_long_long(uint64_t &x);
133 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
134 static void _set_up_rand_long_long(uint64_t &x)
137 synfig::info("Starting up GUID system...");
139 _a=GUID_RNG(time(0)+clock());
140 _b=GUID_RNG(clock());
141 get_rand_long_long=_get_rand_long_long;
142 _get_rand_long_long(x);
145 static void _get_rand_long_long(uint64_t &x)
147 //subtractive_rng _c(clock());
148 unsigned short* data(reinterpret_cast<unsigned short *>(&x));
156 // Use OS-Dependent method
160 static void get_rand_long_long(uint64_t &x)
162 _GUID* guid(reinterpret_cast<_GUID*>(&x));
169 static void _set_up_rand_long_long(uint64_t &x);
170 static void _get_rand_long_long(uint64_t &x);
171 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
172 static void _set_up_rand_long_long(uint64_t &x)
175 synfig::info("Starting up GUID system...");
177 rand_fd=open("/dev/urandom",O_RDONLY);
178 get_rand_long_long=_get_rand_long_long;
179 _get_rand_long_long(x);
182 static void _get_rand_long_long(uint64_t &x){ read(rand_fd,&x,sizeof(x));}
190 synfig::GUID::make_unique()
192 get_rand_long_long(data.u_64.a);
193 get_rand_long_long(data.u_64.b);
197 synfig::GUID::hasher(const String& str)
200 hash<const char*> string_hash_;
201 const unsigned int seed(
207 const unsigned int seed(0x3B642879);
208 for(int i=0;i<str.size();i++)
210 seed^=(seed*str[i])*i
211 seed=(seed>>(32-(i%24)))^(seed<<(i%24))
215 GUID_RNG random(seed);
217 ret.data.u_32.a=random(~(unsigned int)0);
218 ret.data.u_32.b=random(~(unsigned int)0);
219 ret.data.u_32.c=random(~(unsigned int)0);
220 ret.data.u_32.d=random(~(unsigned int)0);
225 synfig::GUID::hasher(int i)
229 ret.data.u_32.a=random(~(unsigned int)0);
230 ret.data.u_32.b=random(~(unsigned int)0);
231 ret.data.u_32.c=random(~(unsigned int)0);
232 ret.data.u_32.d=random(~(unsigned int)0);
237 synfig::GUID::get_string()const
239 return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
242 synfig::GUID::GUID(const String &str)
244 strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);