ReactOS 0.4.16-dev-1990-gfa5cf28
misc.c
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS DiskPart
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/system/diskpart/misc.c
5 * PURPOSE: Manages all the partitions of the OS in an interactive way.
6 * PROGRAMMERS: Eric Kohl
7 */
8
9#include "diskpart.h"
10
11/* FUNCTIONS ******************************************************************/
12
13BOOL
15 _In_ PWSTR pszDecString)
16{
17 PWSTR ptr;
18
19 if ((pszDecString == NULL) || (*pszDecString == UNICODE_NULL))
20 return FALSE;
21
22 ptr = pszDecString;
23 while (*ptr != UNICODE_NULL)
24 {
25 if (!iswdigit(*ptr))
26 return FALSE;
27
28 ptr++;
29 }
30
31 return TRUE;
32}
33
34
35BOOL
37 _In_ PWSTR pszHexString)
38{
39 PWSTR ptr;
40
41 if ((pszHexString == NULL) || (*pszHexString == UNICODE_NULL))
42 return FALSE;
43
44 ptr = pszHexString;
45 while (*ptr != UNICODE_NULL)
46 {
47 if (!iswxdigit(*ptr))
48 return FALSE;
49
50 ptr++;
51 }
52
53 return TRUE;
54}
55
56
57BOOL
59 _In_ PWSTR pszString,
60 _In_ PWSTR pszPrefix,
61 _Out_opt_ PWSTR *ppszSuffix)
62{
63 INT nPrefixLength, ret;
64
65 nPrefixLength = wcslen(pszPrefix);
66 ret = _wcsnicmp(pszString, pszPrefix, nPrefixLength);
67 if ((ret == 0) && (ppszSuffix != NULL))
68 *ppszSuffix = &pszString[nPrefixLength];
69
70 return (ret == 0);
71}
72
73
76 _In_ ULONGLONG Dividend,
78{
79 return (Dividend + Divisor / 2) / Divisor;
80}
81
82
85 _In_ PWSTR pszInString)
86{
87 PWSTR pszOutString = NULL;
88 PWSTR pStart, pEnd;
90
91 if ((pszInString == NULL) || (pszInString[0] == UNICODE_NULL))
92 return NULL;
93
94 if (pszInString[0] == L'"')
95 {
96 if (pszInString[1] == UNICODE_NULL)
97 return NULL;
98
99 pStart = &pszInString[1];
100 pEnd = wcschr(pStart, '"');
101 if (pEnd == NULL)
102 {
103 nLength = wcslen(pStart);
104 }
105 else
106 {
107 nLength = (pEnd - pStart);
108 }
109 }
110 else
111 {
112 pStart = pszInString;
113 nLength = wcslen(pStart);
114 }
115
116 pszOutString = RtlAllocateHeap(RtlGetProcessHeap(),
118 (nLength + 1) * sizeof(WCHAR));
119 if (pszOutString == NULL)
120 return NULL;
121
122 wcsncpy(pszOutString, pStart, nLength);
123
124 return pszOutString;
125}
126
127
128PWSTR
130 _In_ PWSTR pszInString)
131{
132 PWSTR pszOutString = NULL;
133 INT nLength;
134
135 if ((pszInString == NULL) || (pszInString[0] == UNICODE_NULL))
136 return NULL;
137
138 nLength = wcslen(pszInString);
139 pszOutString = RtlAllocateHeap(RtlGetProcessHeap(),
141 (nLength + 1) * sizeof(WCHAR));
142 if (pszOutString == NULL)
143 return NULL;
144
145 wcscpy(pszOutString, pszInString);
146
147 return pszOutString;
148}
149
150
151VOID
153 _Out_ GUID *pGuid)
154{
155 RtlGenRandom(pGuid, sizeof(*pGuid));
156 /* Clear the version bits and set the version (4) */
157 pGuid->Data3 &= 0x0fff;
158 pGuid->Data3 |= (4 << 12);
159 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
160 * specified in RFC 4122, section 4.4.
161 */
162 pGuid->Data4[0] &= 0x3f;
163 pGuid->Data4[0] |= 0x80;
164}
165
166
167VOID
169 _Out_ PDWORD pSignature)
170{
171 LARGE_INTEGER SystemTime;
174
175 NtQuerySystemTime(&SystemTime);
176 RtlTimeToTimeFields(&SystemTime, &TimeFields);
177
178 Buffer = (PUCHAR)pSignature;
179 Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF);
180 Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF);
181 Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF);
182 Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF);
183}
184
185
186VOID
188 _Out_ PWSTR pszBuffer,
189 _In_ GUID *pGuid)
190{
191 swprintf(pszBuffer,
192 L"%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
193 pGuid->Data1,
194 pGuid->Data2,
195 pGuid->Data3,
196 pGuid->Data4[0],
197 pGuid->Data4[1],
198 pGuid->Data4[2],
199 pGuid->Data4[3],
200 pGuid->Data4[4],
201 pGuid->Data4[5],
202 pGuid->Data4[6],
203 pGuid->Data4[7]);
204}
205
206static
207BYTE
209 WCHAR Char)
210{
211 if (iswdigit(Char))
212 {
213 return (BYTE)((INT)Char - (INT)'0');
214 }
215 else if (iswalpha(Char))
216 {
217 if (iswupper(Char))
218 return (INT)Char - ((INT)'A' - 10);
219 else
220 return (INT)Char - ((INT)'a' - 10);
221 }
222 return 0;
223}
224
225
226BOOL
228 _Out_ GUID *pGuid,
229 _In_ PWSTR pszString)
230{
231 INT i;
232
233 if (pszString == NULL)
234 return FALSE;
235
236 pGuid->Data1 = 0;
237 for (i = 0; i < 8; i++)
238 {
239 if (!iswxdigit(pszString[i]))
240 return FALSE;
241
242 pGuid->Data1 = (pGuid->Data1 << 4) | CharToByte(pszString[i]);
243 }
244
245 if (pszString[8] != L'-')
246 return FALSE;
247
248 pGuid->Data2 = 0;
249 for (i = 9; i < 13; i++)
250 {
251 if (!iswxdigit(pszString[i]))
252 return FALSE;
253
254 pGuid->Data2 = (pGuid->Data2 << 4) | CharToByte(pszString[i]);
255 }
256
257 if (pszString[13] != L'-')
258 return FALSE;
259
260 pGuid->Data3 = 0;
261 for (i = 14; i < 18; i++)
262 {
263 if (!iswxdigit(pszString[i]))
264 return FALSE;
265
266 pGuid->Data3 = (pGuid->Data3 << 4) | CharToByte(pszString[i]);
267 }
268
269 if (pszString[18] != L'-')
270 return FALSE;
271
272 for (i = 19; i < 36; i += 2)
273 {
274 if (i == 23)
275 {
276 if (pszString[i] != L'-')
277 return FALSE;
278 i++;
279 }
280
281 if (!iswxdigit(pszString[i]) || !iswxdigit(pszString[i + 1]))
282 return FALSE;
283
284 pGuid->Data4[(i - 19) / 2] = CharToByte(pszString[i]) << 4 | CharToByte(pszString[i + 1]);
285 }
286
287 if (pszString[36] == L'\0')
288 return TRUE;
289
290 return FALSE;
291}
VOID CreateGUID(_Out_ GUID *pGuid)
Definition: misc.c:152
VOID PrintGUID(_Out_ PWSTR pszBuffer, _In_ GUID *pGuid)
Definition: misc.c:187
static BYTE CharToByte(WCHAR Char)
Definition: misc.c:208
BOOL HasPrefix(_In_ PWSTR pszString, _In_ PWSTR pszPrefix, _Out_opt_ PWSTR *ppszSuffix)
Definition: misc.c:58
BOOL IsDecString(_In_ PWSTR pszDecString)
Definition: misc.c:14
BOOL IsHexString(_In_ PWSTR pszHexString)
Definition: misc.c:36
ULONGLONG RoundingDivide(_In_ ULONGLONG Dividend, _In_ ULONGLONG Divisor)
Definition: misc.c:75
PWSTR DuplicateQuotedString(_In_ PWSTR pszInString)
Definition: misc.c:84
VOID CreateSignature(_Out_ PDWORD pSignature)
Definition: misc.c:168
BOOL StringToGUID(_Out_ GUID *pGuid, _In_ PWSTR pszString)
Definition: misc.c:227
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
Definition: bufpool.h:45
wcsncpy
wcscpy
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define wcschr
Definition: compat.h:17
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define swprintf
Definition: precomp.h:40
return ret
Definition: mutex.c:146
#define L(x)
Definition: resources.c:13
BOOLEAN RtlTimeToTimeFields(IN PLARGE_INTEGER Time, IN PTIME_FIELDS TimeFields)
unsigned int BOOL
Definition: ntddk_ex.h:94
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define iswupper(_c)
Definition: ctype.h:665
#define iswdigit(_c)
Definition: ctype.h:667
#define iswxdigit(_c)
Definition: ctype.h:668
#define iswalpha(_c)
Definition: ctype.h:664
_CRTIMP size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
static PVOID ptr
Definition: dispmode.c:27
static PTIME_FIELDS TimeFields
Definition: time.c:104
#define _Out_opt_
Definition: no_sal2.h:214
#define _Out_
Definition: no_sal2.h:160
#define _In_
Definition: no_sal2.h:158
#define UNICODE_NULL
NTSTATUS NTAPI NtQuerySystemTime(OUT PLARGE_INTEGER SystemTime)
Definition: time.c:569
#define RtlGenRandom
Definition: ntsecapi.h:691
DWORD * PDWORD
Definition: pedump.c:68
#define INT
Definition: polytest.cpp:20
_Check_return_ _CRTIMP int __cdecl _wcsnicmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
Definition: _wcsnicmp_nt.c:13
#define DuplicateString(x)
Definition: stringutils.h:45
USHORT Milliseconds
Definition: env_spec_w32.h:717
uint16_t * PWSTR
Definition: typedefs.h:56
int32_t INT
Definition: typedefs.h:58
unsigned char * PUCHAR
Definition: typedefs.h:53
uint64_t ULONGLONG
Definition: typedefs.h:67
WINBASEAPI _In_ DWORD nLength
Definition: wincon.h:682
_In_ LARGE_INTEGER Divisor
Definition: rtlfuncs.h:3061
unsigned char UCHAR
Definition: xmlstorage.h:181
__wchar_t WCHAR
Definition: xmlstorage.h:180
unsigned char BYTE
Definition: xxhash.c:193