1 /* === S Y N F I G ========================================================= */
3 ** \brief Template File
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 "quick_rng.h"
37 #include <sys/types.h>
41 #include <ETL/stringf>
50 using namespace __gnu_cxx;
54 #include SUBTRACT_RNG_H
55 using namespace __gnu_cxx;
64 /* === U S I N G =========================================================== */
68 using namespace synfig;
70 /* === M A C R O S ========================================================= */
72 #define MANUAL_GUID_CALC
74 /* === G L O B A L S ======================================================= */
76 /* === P R O C E D U R E S ================================================= */
78 /* === M E T H O D S ======================================================= */
80 #define GUID_RNG quick_rng
81 //#define GUID_RNG subtractive_rng
85 #ifdef MANUAL_GUID_CALC
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)
94 synfig::info("Starting up GUID system...");
96 _a=GUID_RNG(time(0)+clock());
98 get_rand_long_long=_get_rand_long_long;
99 _get_rand_long_long(x);
102 static void _get_rand_long_long(uint64_t &x)
104 //subtractive_rng _c(clock());
105 unsigned short* data(reinterpret_cast<unsigned short *>(&x));
113 // Use OS-Dependent method
117 static void get_rand_long_long(uint64_t &x)
119 _GUID* guid(reinterpret_cast<_GUID*>(&x));
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)
132 synfig::info("Starting up GUID system...");
134 rand_fd=open("/dev/urandom",O_RDONLY);
135 get_rand_long_long=_get_rand_long_long;
136 _get_rand_long_long(x);
139 static void _get_rand_long_long(uint64_t &x){ read(rand_fd,&x,sizeof(x));}
147 synfig::GUID::make_unique()
149 get_rand_long_long(data.u_64.a);
150 get_rand_long_long(data.u_64.b);
154 synfig::GUID::hasher(const String& str)
157 hash<const char*> string_hash_;
158 const unsigned int seed(
164 const unsigned int seed(0x3B642879);
165 for(int i=0;i<str.size();i++)
167 seed^=(seed*str[i])*i
168 seed=(seed>>(32-(i%24)))^(seed<<(i%24))
172 GUID_RNG random(seed);
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);
182 synfig::GUID::hasher(int 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);
194 synfig::GUID::get_string()const
196 return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
199 synfig::GUID::GUID(const String &str)
201 strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);