aboutsummaryrefslogtreecommitdiff
path: root/src/api/burn/balutil/balutil.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/burn/balutil/balutil.cpp')
-rw-r--r--src/api/burn/balutil/balutil.cpp213
1 files changed, 205 insertions, 8 deletions
diff --git a/src/api/burn/balutil/balutil.cpp b/src/api/burn/balutil/balutil.cpp
index 7a638219..5671da4e 100644
--- a/src/api/burn/balutil/balutil.cpp
+++ b/src/api/burn/balutil/balutil.cpp
@@ -89,6 +89,66 @@ LExit:
89} 89}
90 90
91 91
92DAPI_(HRESULT) BalEscapeString(
93 __in_z LPCWSTR wzIn,
94 __inout LPWSTR* psczOut
95 )
96{
97 HRESULT hr = S_OK;
98
99 if (!vpEngine)
100 {
101 hr = E_POINTER;
102 ExitOnRootFailure(hr, "BalInitialize() must be called first.");
103 }
104
105 hr = BalEscapeStringFromEngine(vpEngine, wzIn, psczOut);
106
107LExit:
108 return hr;
109}
110
111
112DAPI_(HRESULT) BalEscapeStringFromEngine(
113 __in IBootstrapperEngine* pEngine,
114 __in_z LPCWSTR wzIn,
115 __inout LPWSTR* psczOut
116 )
117{
118 HRESULT hr = S_OK;
119 SIZE_T cch = 0;
120
121 if (*psczOut)
122 {
123 hr = StrMaxLength(*psczOut, &cch);
124 ExitOnFailure(hr, "Failed to determine length of value.");
125 }
126 else
127 {
128 hr = ::StringCchLengthW(wzIn, STRSAFE_MAX_LENGTH, reinterpret_cast<size_t*>(&cch));
129 ExitOnFailure(hr, "Failed to determine length of source.");
130
131 cch = min(STRSAFE_MAX_LENGTH, cch + VARIABLE_GROW_FACTOR);
132 hr = StrAlloc(psczOut, cch);
133 ExitOnFailure(hr, "Failed to pre-allocate value.");
134 }
135
136 hr = pEngine->EscapeString(wzIn, *psczOut, &cch);
137 if (E_MOREDATA == hr)
138 {
139 ++cch;
140
141 hr = StrAllocSecure(psczOut, cch);
142 ExitOnFailure(hr, "Failed to allocate value.");
143
144 hr = pEngine->EscapeString(wzIn, *psczOut, &cch);
145 }
146
147LExit:
148 return hr;
149}
150
151
92// The contents of psczOut may be sensitive, should keep encrypted and SecureZeroFree. 152// The contents of psczOut may be sensitive, should keep encrypted and SecureZeroFree.
93DAPI_(HRESULT) BalFormatString( 153DAPI_(HRESULT) BalFormatString(
94 __in_z LPCWSTR wzFormat, 154 __in_z LPCWSTR wzFormat,
@@ -96,7 +156,6 @@ DAPI_(HRESULT) BalFormatString(
96 ) 156 )
97{ 157{
98 HRESULT hr = S_OK; 158 HRESULT hr = S_OK;
99 SIZE_T cch = 0;
100 159
101 if (!vpEngine) 160 if (!vpEngine)
102 { 161 {
@@ -104,13 +163,39 @@ DAPI_(HRESULT) BalFormatString(
104 ExitOnRootFailure(hr, "BalInitialize() must be called first."); 163 ExitOnRootFailure(hr, "BalInitialize() must be called first.");
105 } 164 }
106 165
166 hr = BalFormatStringFromEngine(vpEngine, wzFormat, psczOut);
167
168LExit:
169 return hr;
170}
171
172
173// The contents of psczOut may be sensitive, should keep encrypted and SecureZeroFree.
174DAPI_(HRESULT) BalFormatStringFromEngine(
175 __in IBootstrapperEngine* pEngine,
176 __in_z LPCWSTR wzFormat,
177 __inout LPWSTR* psczOut
178 )
179{
180 HRESULT hr = S_OK;
181 SIZE_T cch = 0;
182
107 if (*psczOut) 183 if (*psczOut)
108 { 184 {
109 hr = StrMaxLength(*psczOut, &cch); 185 hr = StrMaxLength(*psczOut, &cch);
110 ExitOnFailure(hr, "Failed to determine length of value."); 186 ExitOnFailure(hr, "Failed to determine length of value.");
111 } 187 }
188 else
189 {
190 hr = ::StringCchLengthW(wzFormat, STRSAFE_MAX_LENGTH, reinterpret_cast<size_t*>(&cch));
191 ExitOnFailure(hr, "Failed to determine length of source.");
192
193 cch = min(STRSAFE_MAX_LENGTH, cch + VARIABLE_GROW_FACTOR);
194 hr = StrAlloc(psczOut, cch);
195 ExitOnFailure(hr, "Failed to pre-allocate value.");
196 }
112 197
113 hr = vpEngine->FormatString(wzFormat, *psczOut, &cch); 198 hr = pEngine->FormatString(wzFormat, *psczOut, &cch);
114 if (E_MOREDATA == hr) 199 if (E_MOREDATA == hr)
115 { 200 {
116 ++cch; 201 ++cch;
@@ -118,7 +203,7 @@ DAPI_(HRESULT) BalFormatString(
118 hr = StrAllocSecure(psczOut, cch); 203 hr = StrAllocSecure(psczOut, cch);
119 ExitOnFailure(hr, "Failed to allocate value."); 204 ExitOnFailure(hr, "Failed to allocate value.");
120 205
121 hr = vpEngine->FormatString(wzFormat, *psczOut, &cch); 206 hr = pEngine->FormatString(wzFormat, *psczOut, &cch);
122 } 207 }
123 208
124LExit: 209LExit:
@@ -172,7 +257,7 @@ DAPI_(BOOL) BalVariableExists(
172 ) 257 )
173{ 258{
174 HRESULT hr = S_OK; 259 HRESULT hr = S_OK;
175 SIZE_T cch = 0; 260 BOOL fExists = FALSE;
176 261
177 if (!vpEngine) 262 if (!vpEngine)
178 { 263 {
@@ -180,9 +265,23 @@ DAPI_(BOOL) BalVariableExists(
180 ExitOnRootFailure(hr, "BalInitialize() must be called first."); 265 ExitOnRootFailure(hr, "BalInitialize() must be called first.");
181 } 266 }
182 267
183 hr = vpEngine->GetVariableString(wzVariable, NULL, &cch); 268 fExists = BalVariableExistsFromEngine(vpEngine, wzVariable);
184 269
185LExit: 270LExit:
271 return fExists;
272}
273
274
275DAPI_(BOOL) BalVariableExistsFromEngine(
276 __in IBootstrapperEngine* pEngine,
277 __in_z LPCWSTR wzVariable
278 )
279{
280 HRESULT hr = S_OK;
281 SIZE_T cch = 0;
282
283 hr = pEngine->GetVariableString(wzVariable, NULL, &cch);
284
186 return E_NOTFOUND != hr; 285 return E_NOTFOUND != hr;
187} 286}
188 287
@@ -194,7 +293,6 @@ DAPI_(HRESULT) BalGetStringVariable(
194 ) 293 )
195{ 294{
196 HRESULT hr = S_OK; 295 HRESULT hr = S_OK;
197 SIZE_T cch = 0;
198 296
199 if (!vpEngine) 297 if (!vpEngine)
200 { 298 {
@@ -202,13 +300,36 @@ DAPI_(HRESULT) BalGetStringVariable(
202 ExitOnRootFailure(hr, "BalInitialize() must be called first."); 300 ExitOnRootFailure(hr, "BalInitialize() must be called first.");
203 } 301 }
204 302
303 hr = BalGetStringVariableFromEngine(vpEngine, wzVariable, psczValue);
304
305LExit:
306 return hr;
307}
308
309
310// The contents of psczValue may be sensitive, if variable is hidden should keep value encrypted and SecureZeroFree.
311DAPI_(HRESULT) BalGetStringVariableFromEngine(
312 __in IBootstrapperEngine* pEngine,
313 __in_z LPCWSTR wzVariable,
314 __inout LPWSTR* psczValue
315 )
316{
317 HRESULT hr = S_OK;
318 SIZE_T cch = 0;
319
205 if (*psczValue) 320 if (*psczValue)
206 { 321 {
207 hr = StrMaxLength(*psczValue, &cch); 322 hr = StrMaxLength(*psczValue, &cch);
208 ExitOnFailure(hr, "Failed to determine length of value."); 323 ExitOnFailure(hr, "Failed to determine length of value.");
209 } 324 }
325 else
326 {
327 cch = VARIABLE_GROW_FACTOR;
328 hr = StrAlloc(psczValue, cch);
329 ExitOnFailure(hr, "Failed to pre-allocate value.");
330 }
210 331
211 hr = vpEngine->GetVariableString(wzVariable, *psczValue, &cch); 332 hr = pEngine->GetVariableString(wzVariable, *psczValue, &cch);
212 if (E_MOREDATA == hr) 333 if (E_MOREDATA == hr)
213 { 334 {
214 ++cch; 335 ++cch;
@@ -216,7 +337,7 @@ DAPI_(HRESULT) BalGetStringVariable(
216 hr = StrAllocSecure(psczValue, cch); 337 hr = StrAllocSecure(psczValue, cch);
217 ExitOnFailure(hr, "Failed to allocate value."); 338 ExitOnFailure(hr, "Failed to allocate value.");
218 339
219 hr = vpEngine->GetVariableString(wzVariable, *psczValue, &cch); 340 hr = pEngine->GetVariableString(wzVariable, *psczValue, &cch);
220 } 341 }
221 342
222LExit: 343LExit:
@@ -244,6 +365,82 @@ LExit:
244} 365}
245 366
246 367
368DAPI_(HRESULT) BalGetVersionVariable(
369 __in_z LPCWSTR wzVariable,
370 __inout LPWSTR* psczValue
371 )
372{
373 HRESULT hr = S_OK;
374
375 if (!vpEngine)
376 {
377 hr = E_POINTER;
378 ExitOnRootFailure(hr, "BalInitialize() must be called first.");
379 }
380
381 hr = BalGetVersionVariableFromEngine(vpEngine, wzVariable, psczValue);
382
383LExit:
384 return hr;
385}
386
387
388DAPI_(HRESULT) BalGetVersionVariableFromEngine(
389 __in IBootstrapperEngine* pEngine,
390 __in_z LPCWSTR wzVariable,
391 __inout LPWSTR* psczValue
392 )
393{
394 HRESULT hr = S_OK;
395 SIZE_T cch = 0;
396
397 if (*psczValue)
398 {
399 hr = StrMaxLength(*psczValue, &cch);
400 ExitOnFailure(hr, "Failed to determine length of value.");
401 }
402 else
403 {
404 cch = VARIABLE_GROW_FACTOR;
405 hr = StrAlloc(psczValue, cch);
406 ExitOnFailure(hr, "Failed to pre-allocate value.");
407 }
408
409 hr = pEngine->GetVariableVersion(wzVariable, *psczValue, &cch);
410 if (E_MOREDATA == hr)
411 {
412 ++cch;
413
414 hr = StrAllocSecure(psczValue, cch);
415 ExitOnFailure(hr, "Failed to allocate value.");
416
417 hr = pEngine->GetVariableVersion(wzVariable, *psczValue, &cch);
418 }
419
420LExit:
421 return hr;
422}
423
424DAPI_(HRESULT) BalSetVersionVariable(
425 __in_z LPCWSTR wzVariable,
426 __in_z_opt LPCWSTR wzValue
427 )
428{
429 HRESULT hr = S_OK;
430
431 if (!vpEngine)
432 {
433 hr = E_POINTER;
434 ExitOnRootFailure(hr, "BalInitialize() must be called first.");
435 }
436
437 hr = vpEngine->SetVariableVersion(wzVariable, wzValue);
438
439LExit:
440 return hr;
441}
442
443
247DAPIV_(HRESULT) BalLog( 444DAPIV_(HRESULT) BalLog(
248 __in BOOTSTRAPPER_LOG_LEVEL level, 445 __in BOOTSTRAPPER_LOG_LEVEL level,
249 __in_z __format_string LPCSTR szFormat, 446 __in_z __format_string LPCSTR szFormat,