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
13 BOOL
IsDecString(_In_ PWSTR pszDecString)14 IsDecString(
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
35 BOOL
IsHexString(_In_ PWSTR pszHexString)36 IsHexString(
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
57 BOOL
HasPrefix(_In_ PWSTR pszString,_In_ PWSTR pszPrefix,_Out_opt_ PWSTR * ppszSuffix)58 HasPrefix(
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
74 ULONGLONG
RoundingDivide(_In_ ULONGLONG Dividend,_In_ ULONGLONG Divisor)75 RoundingDivide(
76 _In_ ULONGLONG Dividend,
77 _In_ ULONGLONG Divisor)
78 {
79 return (Dividend + Divisor / 2) / Divisor;
80 }
81
82
83 PWSTR
DuplicateQuotedString(_In_ PWSTR pszInString)84 DuplicateQuotedString(
85 _In_ PWSTR pszInString)
86 {
87 PWSTR pszOutString = NULL;
88 PWSTR pStart, pEnd;
89 INT nLength;
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(),
117 HEAP_ZERO_MEMORY,
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
128 PWSTR
DuplicateString(_In_ PWSTR pszInString)129 DuplicateString(
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(),
140 HEAP_ZERO_MEMORY,
141 (nLength + 1) * sizeof(WCHAR));
142 if (pszOutString == NULL)
143 return NULL;
144
145 wcscpy(pszOutString, pszInString);
146
147 return pszOutString;
148 }
149