1 /* === S I N F G =========================================================== */
3 ** \brief Template File
5 ** $Id: guid.cpp,v 1.4 2005/01/13 06:48:39 darco Exp $
8 ** Copyright (c) 2002 Robert B. Quattlebaum Jr.
10 ** This software and associated documentation
11 ** are CONFIDENTIAL and PROPRIETARY property of
12 ** the above-mentioned copyright holder.
14 ** You may not copy, print, publish, or in any
15 ** other way distribute this software without
16 ** a prior written agreement with
17 ** the copyright holder.
20 /* ========================================================================= */
22 /* === H E A D E R S ======================================================= */
24 #define HASH_MAP_H <ext/hash_map>
25 #define SUBTRACT_RNG_H <ext/functional>
35 #include <sys/types.h>
39 #include <ETL/stringf>
48 using namespace __gnu_cxx;
52 #include SUBTRACT_RNG_H
53 using namespace __gnu_cxx;
62 /* === U S I N G =========================================================== */
66 using namespace sinfg;
68 /* === M A C R O S ========================================================= */
70 #define MANUAL_GUID_CALC
72 /* === G L O B A L S ======================================================= */
74 /* === P R O C E D U R E S ================================================= */
76 /* === M E T H O D S ======================================================= */
78 // A fast 32-bit linear congruential random number generator
83 quick_rng(unsigned long seed=0):next(seed) { }
85 void set_seed(unsigned long x)
92 static const unsigned long a(1664525);
93 static const unsigned long c(1013904223);
105 static const float m(int(65535));
107 return float(i16())/m;
110 unsigned long operator()(const unsigned long& m)
122 #define GUID_RNG quick_rng
123 //#define GUID_RNG subtractive_rng
127 #ifdef MANUAL_GUID_CALC
129 static GUID_RNG _a, _b;
130 static void _set_up_rand_long_long(uint64_t &x);
131 static void _get_rand_long_long(uint64_t &x);
132 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
133 static void _set_up_rand_long_long(uint64_t &x)
136 sinfg::info("Starting up GUID system...");
138 _a=GUID_RNG(time(0)+clock());
139 _b=GUID_RNG(clock());
140 get_rand_long_long=_get_rand_long_long;
141 _get_rand_long_long(x);
144 static void _get_rand_long_long(uint64_t &x)
146 //subtractive_rng _c(clock());
147 unsigned short* data(reinterpret_cast<unsigned short *>(&x));
155 // Use OS-Dependent method
159 static void get_rand_long_long(uint64_t &x)
161 _GUID* guid(reinterpret_cast<_GUID*>(&x));
168 static void _set_up_rand_long_long(uint64_t &x);
169 static void _get_rand_long_long(uint64_t &x);
170 static void (*get_rand_long_long)(uint64_t&)=_set_up_rand_long_long;
171 static void _set_up_rand_long_long(uint64_t &x)
174 sinfg::info("Starting up GUID system...");
176 rand_fd=open("/dev/urandom",O_RDONLY);
177 get_rand_long_long=_get_rand_long_long;
178 _get_rand_long_long(x);
181 static void _get_rand_long_long(uint64_t &x){ read(rand_fd,&x,sizeof(x));}
189 sinfg::GUID::make_unique()
191 get_rand_long_long(data.u_64.a);
192 get_rand_long_long(data.u_64.b);
196 sinfg::GUID::hasher(const String& str)
199 hash<const char*> string_hash_;
200 const unsigned int seed(
206 const unsigned int seed(0x3B642879);
207 for(int i=0;i<str.size();i++)
209 seed^=(seed*str[i])*i
210 seed=(seed>>(32-(i%24)))^(seed<<(i%24))
214 GUID_RNG random(seed);
216 ret.data.u_32.a=random(~(unsigned int)0);
217 ret.data.u_32.b=random(~(unsigned int)0);
218 ret.data.u_32.c=random(~(unsigned int)0);
219 ret.data.u_32.d=random(~(unsigned int)0);
224 sinfg::GUID::hasher(int i)
228 ret.data.u_32.a=random(~(unsigned int)0);
229 ret.data.u_32.b=random(~(unsigned int)0);
230 ret.data.u_32.c=random(~(unsigned int)0);
231 ret.data.u_32.d=random(~(unsigned int)0);
236 sinfg::GUID::get_string()const
238 return strprintf("%08X%08X%08X%08X",data.u_32.a,data.u_32.b,data.u_32.c,data.u_32.d);
241 sinfg::GUID::GUID(const String &str)
243 strscanf(str,"%08X%08X%08X%08X",&data.u_32.a,&data.u_32.b,&data.u_32.c,&data.u_32.d);