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 SUBTRACT_RNG_H <ext/functional>
36 #include "quick_rng.h"
37 #include <sys/types.h>
41 #include <ETL/stringf>
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 #define GUID_RNG quick_rng
80 //#define GUID_RNG subtractive_rng
84 #ifdef MANUAL_GUID_CALC
86 static GUID_RNG _a, _b;
87 static void _set_up_rand_long_long(uint64_t &x);
88 static void _get_rand_long_long(uint64_t &x);
89 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
90 static void _set_up_rand_long_long(uint64_t &x)
93 // synfig::info("Starting up GUID system...");
95 _a=GUID_RNG(time(0)+clock());
97 get_rand_long_long=_get_rand_long_long;
98 _get_rand_long_long(x);
101 static void _get_rand_long_long(uint64_t &x)
103 //subtractive_rng _c(clock());
104 unsigned short* data(reinterpret_cast<unsigned short *>(&x));
112 // Use OS-Dependent method
116 static void get_rand_long_long(uint64_t &x)
118 _GUID* guid(reinterpret_cast<_GUID*>(&x));
125 static void _set_up_rand_long_long(uint64_t &x);
126 static void _get_rand_long_long(uint64_t &x);
127 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
128 static void _set_up_rand_long_long(uint64_t &x)
131 // synfig::info("Starting up GUID system...");
133 rand_fd=open("/dev/urandom",O_RDONLY);
134 get_rand_long_long=_get_rand_long_long;
135 _get_rand_long_long(x);
138 static void _get_rand_long_long(uint64_t &x){ read(rand_fd,&x,sizeof(x));}
146 synfig::GUID::make_unique()
148 get_rand_long_long(data.u_64.a);
149 get_rand_long_long(data.u_64.b);
153 synfig::GUID::hasher(const String& str)
156 HASH_MAP_NAMESPACE::hash<const char*> string_hash_;
157 const unsigned int seed(
163 const unsigned int seed(0x3B642879);
164 for(int i=0;i<str.size();i++)
166 seed^=(seed*str[i])*i
167 seed=(seed>>(32-(i%24)))^(seed<<(i%24))
171 GUID_RNG random(seed);
173 ret.data.u_32.a=random(~(unsigned int)0);
174 ret.data.u_32.b=random(~(unsigned int)0);
175 ret.data.u_32.c=random(~(unsigned int)0);
176 ret.data.u_32.d=random(~(unsigned int)0);
181 synfig::GUID::hasher(int i)
185 ret.data.u_32.a=random(~(unsigned int)0);
186 ret.data.u_32.b=random(~(unsigned int)0);
187 ret.data.u_32.c=random(~(unsigned int)0);
188 ret.data.u_32.d=random(~(unsigned int)0);
193 synfig::GUID::get_string()const
195 return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
198 synfig::GUID::GUID(const String &str)
200 strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);