1#if !defined __UUID_H__
2#    define  __UUID_H__
3
4#include <string.h>
5#include <stdio.h>
6#include <stdlib.h>
7#ifndef _MSC_VER
8/* No unistd.h in MS VC */
9#include <unistd.h>
10#endif
11#include <time.h>
12
13#if !defined INT2PTR
14#if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
15#  define PTRV			UV
16#  define INT2PTR(any,d)	(any)(d)
17#else
18#  if PTRSIZE == LONGSIZE
19#    define PTRV		unsigned long
20#  else
21#    define PTRV		unsigned
22#  endif
23#  define INT2PTR(any,d)	(any)(PTRV)(d)
24#endif
25#define NUM2PTR(any,d)	(any)(PTRV)(d)
26#define PTR2IV(p)	INT2PTR(IV,p)
27#define PTR2UV(p)	INT2PTR(UV,p)
28#define PTR2NV(p)	NUM2PTR(NV,p)
29#if PTRSIZE == LONGSIZE
30#  define PTR2ul(p)	(unsigned long)(p)
31#else
32#  define PTR2ul(p)	INT2PTR(unsigned long,p)
33#endif
34#endif
35
36#if defined __cygwin__ || defined __mingw32__ || defined _MSC_VER
37#include <windows.h>
38#endif
39#if defined __darwin__
40#include <sys/file.h>
41#endif
42
43#ifdef _MSC_VER
44#include <process.h>
45#endif
46
47#if !defined _STDIR
48#    define  _STDIR			"/var/tmp"
49#endif
50#if !defined _DEFAULT_UMASK
51#    define  _DEFAULT_UMASK		0007
52#endif
53
54#define UUID_STATE			".UUID_STATE"
55#define UUID_NODEID			".UUID_NODEID"
56#if defined __mingw32__ || (defined _WIN32 && !defined(__cygwin__)) || defined _MSC_VER
57#define UUID_STATE_NV_STORE		_STDIR"\\"UUID_STATE
58#define UUID_NODEID_NV_STORE		_STDIR"\\"UUID_NODEID
59#else
60#define UUID_STATE_NV_STORE		_STDIR"/"UUID_STATE
61#define UUID_NODEID_NV_STORE		_STDIR"/"UUID_NODEID
62#endif
63
64#define UUIDS_PER_TICK 1024
65#ifdef _MSC_VER
66#define I64(C) C##i64
67#else
68#define I64(C) C##LL
69#endif
70
71#define F_BIN 0
72#define F_STR 1
73#define F_HEX 2
74#define F_B64 3
75
76#define CHECK(f1, f2) if (f1 != f2) RETVAL = f1 < f2 ? -1 : 1;
77
78typedef unsigned int       unsigned32;
79typedef unsigned short     unsigned16;
80typedef unsigned char      unsigned8;
81typedef unsigned char      byte;
82#ifndef _MSC_VER
83typedef unsigned long long unsigned64_t;
84# else
85typedef __int64 int64_t;
86typedef unsigned __int64 uint64_t;
87typedef __int32 int32_t;
88typedef unsigned __int32 uint32_t;
89typedef __int16 int16_t;
90typedef unsigned __int16 uint16_t;
91typedef __int8 int8_t;
92typedef unsigned __int8 uint8_t;
93
94typedef unsigned __int64 unsigned64_t;
95// http://msdn2.microsoft.com/en-us/library/296az74e.aspx - Integer Limits
96
97typedef int pid_t;
98#endif /* _MSC_VER */
99typedef unsigned64_t       perl_uuid_time_t;
100
101#if   defined __solaris__ || defined __linux__
102#     define LOCK(f)		lockf(fileno(f),F_LOCK,0);
103#     define UNLOCK(f)		lockf(fileno(f),F_ULOCK,0);
104#elif defined __darwin__
105#     define LOCK(f)		flock(fileno(f),LOCK_EX);
106#     define UNLOCK(f)		flock(fileno(f),LOCK_UN);
107#else
108#     define LOCK(f)
109#     define UNLOCK(f)
110#endif
111
112#undef perl_uuid_t
113
114typedef struct _uuid_node_t {
115   char nodeID[6];
116} uuid_node_t;
117
118typedef struct _perl_uuid_t {
119   unsigned32          time_low;
120   unsigned16          time_mid;
121   unsigned16          time_hi_and_version;
122   unsigned8           clock_seq_hi_and_reserved;
123   unsigned8           clock_seq_low;
124   byte                node[6];
125} perl_uuid_t;
126
127typedef struct _uuid_state_t {
128   perl_uuid_time_t ts;
129   uuid_node_t node;
130   unsigned16  cs;
131} uuid_state_t;
132
133typedef struct _uuid_context_t {
134   uuid_state_t state;
135   uuid_node_t  nodeid;
136   perl_uuid_time_t  next_save;
137} uuid_context_t;
138
139static void format_uuid_v1(
140   perl_uuid_t      *uuid,
141   unsigned16   clockseq,
142   perl_uuid_time_t  timestamp,
143   uuid_node_t  node
144);
145static void format_uuid_v3(
146   perl_uuid_t      *uuid,
147   unsigned     char hash[16]
148);
149static void       get_current_time(perl_uuid_time_t * timestamp);
150static unsigned16 true_random(void);
151static void       get_system_time(perl_uuid_time_t *perl_uuid_time);
152static void       get_random_info(unsigned char seed[16]);
153static SV*        make_ret(const perl_uuid_t u, int type);
154static SV*        MD5Init(void);
155static void       MD5Update(SV* ctx, SV* data);
156static void       MD5Final(unsigned char hash[16], SV* ctx);
157
158static const char base64[] =
159   "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
160
161static unsigned char index64[256] = {
162   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
163   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
164   255,255,255,255, 255,255,255,255, 255,255,255,62, 255,255,255,63,
165   52,53,54,55, 56,57,58,59, 60,61,255,255, 255,254,255,255,
166   255, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
167   15,16,17,18, 19,20,21,22, 23,24,25,255, 255,255,255,255,
168   255,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
169   41,42,43,44, 45,46,47,48, 49,50,51,255, 255,255,255,255,
170
171   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
172   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
173   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
174   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
175   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
176   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
177   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
178   255,255,255,255, 255,255,255,255, 255,255,255,255, 255,255,255,255,
179};
180#endif
181