xref: /aosp_15_r20/external/e2fsprogs/lib/uuid/gen_uuid_nt.c (revision 6a54128f25917bfc36a8a6e9d722c04a0b4641b6)
1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker  * gen_uuid_nt.c -- Use NT api to generate uuid
3*6a54128fSAndroid Build Coastguard Worker  *
4*6a54128fSAndroid Build Coastguard Worker  * Written by Andrey Shedel ([email protected])
5*6a54128fSAndroid Build Coastguard Worker  */
6*6a54128fSAndroid Build Coastguard Worker 
7*6a54128fSAndroid Build Coastguard Worker 
8*6a54128fSAndroid Build Coastguard Worker #include "config.h"
9*6a54128fSAndroid Build Coastguard Worker #include "uuidP.h"
10*6a54128fSAndroid Build Coastguard Worker 
11*6a54128fSAndroid Build Coastguard Worker #pragma warning(push,4)
12*6a54128fSAndroid Build Coastguard Worker 
13*6a54128fSAndroid Build Coastguard Worker #pragma comment(lib, "ntdll.lib")
14*6a54128fSAndroid Build Coastguard Worker 
15*6a54128fSAndroid Build Coastguard Worker //
16*6a54128fSAndroid Build Coastguard Worker // Here is a nice example why it's not a good idea
17*6a54128fSAndroid Build Coastguard Worker // to use native API in ordinary applications.
18*6a54128fSAndroid Build Coastguard Worker // Number of parameters in function below was changed from 3 to 4
19*6a54128fSAndroid Build Coastguard Worker // for NT5.
20*6a54128fSAndroid Build Coastguard Worker //
21*6a54128fSAndroid Build Coastguard Worker //
22*6a54128fSAndroid Build Coastguard Worker // NTSYSAPI
23*6a54128fSAndroid Build Coastguard Worker // NTSTATUS
24*6a54128fSAndroid Build Coastguard Worker // NTAPI
25*6a54128fSAndroid Build Coastguard Worker // NtAllocateUuids(
26*6a54128fSAndroid Build Coastguard Worker //     OUT PULONG p1,
27*6a54128fSAndroid Build Coastguard Worker //     OUT PULONG p2,
28*6a54128fSAndroid Build Coastguard Worker //     OUT PULONG p3,
29*6a54128fSAndroid Build Coastguard Worker //     OUT PUCHAR Seed // 6 bytes
30*6a54128fSAndroid Build Coastguard Worker //   );
31*6a54128fSAndroid Build Coastguard Worker //
32*6a54128fSAndroid Build Coastguard Worker //
33*6a54128fSAndroid Build Coastguard Worker 
34*6a54128fSAndroid Build Coastguard Worker unsigned long
35*6a54128fSAndroid Build Coastguard Worker __stdcall
36*6a54128fSAndroid Build Coastguard Worker NtAllocateUuids(
37*6a54128fSAndroid Build Coastguard Worker    void* p1,  // 8 bytes
38*6a54128fSAndroid Build Coastguard Worker    void* p2,  // 4 bytes
39*6a54128fSAndroid Build Coastguard Worker    void* p3   // 4 bytes
40*6a54128fSAndroid Build Coastguard Worker    );
41*6a54128fSAndroid Build Coastguard Worker 
42*6a54128fSAndroid Build Coastguard Worker typedef
43*6a54128fSAndroid Build Coastguard Worker unsigned long
44*6a54128fSAndroid Build Coastguard Worker (__stdcall*
45*6a54128fSAndroid Build Coastguard Worker NtAllocateUuids_2000)(
46*6a54128fSAndroid Build Coastguard Worker    void* p1,  // 8 bytes
47*6a54128fSAndroid Build Coastguard Worker    void* p2,  // 4 bytes
48*6a54128fSAndroid Build Coastguard Worker    void* p3,  // 4 bytes
49*6a54128fSAndroid Build Coastguard Worker    void* seed // 6 bytes
50*6a54128fSAndroid Build Coastguard Worker    );
51*6a54128fSAndroid Build Coastguard Worker 
52*6a54128fSAndroid Build Coastguard Worker 
53*6a54128fSAndroid Build Coastguard Worker 
54*6a54128fSAndroid Build Coastguard Worker //
55*6a54128fSAndroid Build Coastguard Worker // Nice, but instead of including ntddk.h ot winnt.h
56*6a54128fSAndroid Build Coastguard Worker // I should define it here because they MISSED __stdcall in those headers.
57*6a54128fSAndroid Build Coastguard Worker //
58*6a54128fSAndroid Build Coastguard Worker 
59*6a54128fSAndroid Build Coastguard Worker __declspec(dllimport)
60*6a54128fSAndroid Build Coastguard Worker struct _TEB*
61*6a54128fSAndroid Build Coastguard Worker __stdcall
62*6a54128fSAndroid Build Coastguard Worker NtCurrentTeb(void);
63*6a54128fSAndroid Build Coastguard Worker 
64*6a54128fSAndroid Build Coastguard Worker 
65*6a54128fSAndroid Build Coastguard Worker //
66*6a54128fSAndroid Build Coastguard Worker // The only way to get version information from the system is to examine
67*6a54128fSAndroid Build Coastguard Worker // one stored in PEB. But it's pretty dangerous because this value could
68*6a54128fSAndroid Build Coastguard Worker // be altered in image header.
69*6a54128fSAndroid Build Coastguard Worker //
70*6a54128fSAndroid Build Coastguard Worker 
71*6a54128fSAndroid Build Coastguard Worker static
72*6a54128fSAndroid Build Coastguard Worker int
Nt5(void)73*6a54128fSAndroid Build Coastguard Worker Nt5(void)
74*6a54128fSAndroid Build Coastguard Worker {
75*6a54128fSAndroid Build Coastguard Worker 	//return NtCuttentTeb()->Peb->OSMajorVersion >= 5;
76*6a54128fSAndroid Build Coastguard Worker 	return (int)*(int*)((char*)(int)(*(int*)((char*)NtCurrentTeb() + 0x30)) + 0xA4) >= 5;
77*6a54128fSAndroid Build Coastguard Worker }
78*6a54128fSAndroid Build Coastguard Worker 
79*6a54128fSAndroid Build Coastguard Worker 
80*6a54128fSAndroid Build Coastguard Worker 
81*6a54128fSAndroid Build Coastguard Worker 
uuid_generate(uuid_t out)82*6a54128fSAndroid Build Coastguard Worker void uuid_generate(uuid_t out)
83*6a54128fSAndroid Build Coastguard Worker {
84*6a54128fSAndroid Build Coastguard Worker 	if(Nt5())
85*6a54128fSAndroid Build Coastguard Worker 	{
86*6a54128fSAndroid Build Coastguard Worker 		unsigned char seed[6];
87*6a54128fSAndroid Build Coastguard Worker 		((NtAllocateUuids_2000)NtAllocateUuids)(out, ((char*)out)+8, ((char*)out)+12, &seed[0] );
88*6a54128fSAndroid Build Coastguard Worker 	}
89*6a54128fSAndroid Build Coastguard Worker 	else
90*6a54128fSAndroid Build Coastguard Worker 	{
91*6a54128fSAndroid Build Coastguard Worker 		NtAllocateUuids(out, ((char*)out)+8, ((char*)out)+12);
92*6a54128fSAndroid Build Coastguard Worker 	}
93*6a54128fSAndroid Build Coastguard Worker }
94