ReactOS 0.4.17-dev-1190-g4c812f4
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
58 _In_ PWSTR pszString)
59{
61
62 nLength = wcslen(pszString);
63 if ((nLength == 0) || (nLength > 2))
64 return UNICODE_NULL;
65
66 if (!iswalpha(pszString[0]))
67 return UNICODE_NULL;
68
69 if ((nLength > 1) && (pszString[1] != L':'))
70 return UNICODE_NULL;
71
72 return pszString[0];
73}
74
75BOOL
77 _In_ PWSTR pszString,
78 _In_ PWSTR pszPrefix,
79 _Out_opt_ PWSTR *ppszSuffix)
80{
81 INT nPrefixLength, ret;
82
83 nPrefixLength = wcslen(pszPrefix);
84 ret = _wcsnicmp(pszString, pszPrefix, nPrefixLength);
85 if ((ret == 0) && (ppszSuffix != NULL))
86 *ppszSuffix = &pszString[nPrefixLength];
87
88 return (ret == 0);
89}
90
91
94 _In_ ULONGLONG Dividend,
96{
97 return (Dividend + Divisor / 2) / Divisor;
98}
99
100
101PWSTR
103 _In_ PWSTR pszInString)
104{
105 PWSTR pszOutString = NULL;
106 PWSTR pStart, pEnd;
107 INT nLength;
108
109 if ((pszInString == NULL) || (pszInString[0] == UNICODE_NULL))
110 return NULL;
111
112 if (pszInString[0] == L'"')
113 {
114 if (pszInString[1] == UNICODE_NULL)
115 return NULL;
116
117 pStart = &pszInString[1];
118 pEnd = wcschr(pStart, '"');
119 if (pEnd == NULL)
120 {
121 nLength = wcslen(pStart);
122 }
123 else
124 {
125 nLength = (pEnd - pStart);
126 }
127 }
128 else
129 {
130 pStart = pszInString;
131 nLength = wcslen(pStart);
132 }
133
134 pszOutString = RtlAllocateHeap(RtlGetProcessHeap(),
136 (nLength + 1) * sizeof(WCHAR));
137 if (pszOutString == NULL)
138 return NULL;
139
140 wcsncpy(pszOutString, pStart, nLength);
141
142 return pszOutString;
143}
144
145
146PWSTR
148 _In_ PWSTR pszInString)
149{
150 PWSTR pszOutString = NULL;
151 INT nLength;
152
153 if ((pszInString == NULL) || (pszInString[0] == UNICODE_NULL))
154 return NULL;
155
156 nLength = wcslen(pszInString);
157 pszOutString = RtlAllocateHeap(RtlGetProcessHeap(),
159 (nLength + 1) * sizeof(WCHAR));
160 if (pszOutString == NULL)
161 return NULL;
162
163 wcscpy(pszOutString, pszInString);
164
165 return pszOutString;
166}
167
168
169VOID
171 _Out_ GUID *pGuid)
172{
173 RtlGenRandom(pGuid, sizeof(*pGuid));
174 /* Clear the version bits and set the version (4) */
175 pGuid->Data3 &= 0x0fff;
176 pGuid->Data3 |= (4 << 12);
177 /* Set the topmost bits of Data4 (clock_seq_hi_and_reserved) as
178 * specified in RFC 4122, section 4.4.
179 */
180 pGuid->Data4[0] &= 0x3f;
181 pGuid->Data4[0] |= 0x80;
182}
183
184
185VOID
187 _Out_ PDWORD pSignature)
188{
189 LARGE_INTEGER SystemTime;
192
193 NtQuerySystemTime(&SystemTime);
194 RtlTimeToTimeFields(&SystemTime, &TimeFields);
195
196 Buffer = (PUCHAR)pSignature;
197 Buffer[0] = (UCHAR)(TimeFields.Year & 0xFF) + (UCHAR)(TimeFields.Hour & 0xFF);
198 Buffer[1] = (UCHAR)(TimeFields.Year >> 8) + (UCHAR)(TimeFields.Minute & 0xFF);
199 Buffer[2] = (UCHAR)(TimeFields.Month & 0xFF) + (UCHAR)(TimeFields.Second & 0xFF);
200 Buffer[3] = (UCHAR)(TimeFields.Day & 0xFF) + (UCHAR)(TimeFields.Milliseconds & 0xFF);
201}
202
203
204VOID
206 _Out_ PWSTR pszBuffer,
207 _In_ GUID *pGuid)
208{
209 _swprintf(pszBuffer,
210 L"%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
211 pGuid->Data1,
212 pGuid->Data2,
213 pGuid->Data3,
214 pGuid->Data4[0],
215 pGuid->Data4[1],
216 pGuid->Data4[2],
217 pGuid->Data4[3],
218 pGuid->Data4[4],
219 pGuid->Data4[5],
220 pGuid->Data4[6],
221 pGuid->Data4[7]);
222}
223
224static
225BYTE
227 WCHAR Char)
228{
229 if (iswdigit(Char))
230 {
231 return (BYTE)((INT)Char - (INT)'0');
232 }
233 else if (iswalpha(Char))
234 {
235 if (iswupper(Char))
236 return (INT)Char - ((INT)'A' - 10);
237 else
238 return (INT)Char - ((INT)'a' - 10);
239 }
240 return 0;
241}
242
243
244BOOL
246 _Out_ GUID *pGuid,
247 _In_ PWSTR pszString)
248{
249 INT i;
250
251 if (pszString == NULL)
252 return FALSE;
253
254 pGuid->Data1 = 0;
255 for (i = 0; i < 8; i++)
256 {
257 if (!iswxdigit(pszString[i]))
258 return FALSE;
259
260 pGuid->Data1 = (pGuid->Data1 << 4) | CharToByte(pszString[i]);
261 }
262
263 if (pszString[8] != L'-')
264 return FALSE;
265
266 pGuid->Data2 = 0;
267 for (i = 9; i < 13; i++)
268 {
269 if (!iswxdigit(pszString[i]))
270 return FALSE;
271
272 pGuid->Data2 = (pGuid->Data2 << 4) | CharToByte(pszString[i]);
273 }
274
275 if (pszString[13] != L'-')
276 return FALSE;
277
278 pGuid->Data3 = 0;
279 for (i = 14; i < 18; i++)
280 {
281 if (!iswxdigit(pszString[i]))
282 return FALSE;
283
284 pGuid->Data3 = (pGuid->Data3 << 4) | CharToByte(pszString[i]);
285 }
286
287 if (pszString[18] != L'-')
288 return FALSE;
289
290 for (i = 19; i < 36; i += 2)
291 {
292 if (i == 23)
293 {
294 if (pszString[i] != L'-')
295 return FALSE;
296 i++;
297 }
298
299 if (!iswxdigit(pszString[i]) || !iswxdigit(pszString[i + 1]))
300 return FALSE;
301
302 pGuid->Data4[(i - 19) / 2] = CharToByte(pszString[i]) << 4 | CharToByte(pszString[i + 1]);
303 }
304
305 if (pszString[36] == L'\0')
306 return TRUE;
307
308 return FALSE;
309}
310
311
312VOID
314 _Out_ PWSTR pszBuffer,
315 _In_ INT cchBufferMax,
317{
318 if (BusType <= BusTypeMax)
321 pszBuffer,
322 cchBufferMax);
323 else
326 pszBuffer,
327 cchBufferMax);
328}
VOID CreateGUID(_Out_ GUID *pGuid)
Definition: misc.c:170
VOID PrintGUID(_Out_ PWSTR pszBuffer, _In_ GUID *pGuid)
Definition: misc.c:205
static BYTE CharToByte(WCHAR Char)
Definition: misc.c:226
WCHAR IsDriveLetter(_In_ PWSTR pszString)
Definition: misc.c:57
BOOL HasPrefix(_In_ PWSTR pszString, _In_ PWSTR pszPrefix, _Out_opt_ PWSTR *ppszSuffix)
Definition: misc.c:76
VOID PrintBusType(_Out_ PWSTR pszBuffer, _In_ INT cchBufferMax, _In_ STORAGE_BUS_TYPE BusType)
Definition: misc.c:313
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:93
PWSTR DuplicateQuotedString(_In_ PWSTR pszInString)
Definition: misc.c:102
VOID CreateSignature(_Out_ PDWORD pSignature)
Definition: misc.c:186
BOOL StringToGUID(_Out_ GUID *pGuid, _In_ PWSTR pszString)
Definition: misc.c:245
#define IDS_BUSTYPE_OTHER
Definition: resource.h:263
#define IDS_BUSTYPE_UNKNOWN
Definition: resource.h:242
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:616
Definition: bufpool.h:45
#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
_ACRTIMP size_t __cdecl wcslen(const wchar_t *)
Definition: wcs.c:2988
_ACRTIMP int __cdecl _wcsnicmp(const wchar_t *, const wchar_t *, size_t)
Definition: wcs.c:200
return ret
Definition: mutex.c:147
#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
static PVOID ptr
Definition: dispmode.c:27
#define _swprintf(buf, format,...)
Definition: sprintf.c:56
static PTIME_FIELDS TimeFields
Definition: time.c:36
#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:563
short WCHAR
Definition: pedump.c:58
DWORD * PDWORD
Definition: pedump.c:68
#define INT
Definition: polytest.cpp:20
@ BusTypeMax
Definition: ntddstor.h:454
enum _STORAGE_BUS_TYPE STORAGE_BUS_TYPE
#define RtlGenRandom
Definition: ntsecapi.h:691
#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
wcsncpy
wcscpy
#define LoadStringW
Definition: utils.h:64
#define DuplicateString(x)
Definition: stringutils.h:45
USHORT Milliseconds
Definition: env_spec_w32.h:717
uint16_t * PWSTR
Definition: typedefs.h:56
unsigned char UCHAR
Definition: typedefs.h:53
int32_t INT
Definition: typedefs.h:58
uint64_t ULONGLONG
Definition: typedefs.h:67
unsigned char * PUCHAR
Definition: typedefs.h:53
#define GetModuleHandle
Definition: winbase.h:3550
WINBASEAPI _In_ DWORD nLength
Definition: wincon.h:682
_In_opt_ PUNICODE_STRING _In_ PDRIVER_OBJECT _In_ PDEVICE_OBJECT _In_ INTERFACE_TYPE BusType
Definition: halfuncs.h:159
_In_ LARGE_INTEGER Divisor
Definition: rtlfuncs.h:3068
unsigned char BYTE
Definition: xxhash.c:193