VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/include/NoCrtOutput.h@ 106240

Last change on this file since 106240 was 106240, checked in by vboxsync, 7 weeks ago

Additions/NT/Installer: Check that what installer selector is about to launch looks like an installer and is correctly signed with the build certificate. This needs more testing. bugref:10771

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.9 KB
Line 
1/* $Id: NoCrtOutput.h 106240 2024-10-08 14:47:39Z vboxsync $ */
2/** @file
3 * NoCrtOutput - ErrorMsgXxx and PrintXxx functions for small EXEs.
4 */
5
6/*
7 * Copyright (C) 2022-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#ifndef GA_INCLUDED_WINNT_NoCrtOutput_h
29#define GA_INCLUDED_WINNT_NoCrtOutput_h
30#ifndef RT_WITHOUT_PRAGMA_ONCE
31# pragma once
32#endif
33
34#include <iprt/win/windows.h>
35#include <iprt/string.h>
36#include <iprt/utf16.h>
37
38
39/** @name Output helpers
40 *
41 * The general ASSUMPTION here is that all strings are restricted to 7-bit
42 * ASCII, with the exception of wchar_t ones.
43 *
44 * @note We don't use printf, RTPrintf or similar not for masochistic reasons
45 * but to keep the binary small and make it easier to switch between CRT
46 * and IPRT w/ no-CRT.
47 *
48 * @{
49 */
50
51DECLINLINE(void) OutputWStr(HANDLE hDst, const wchar_t *pwszStr)
52{
53 DWORD cbIgn;
54 if (GetConsoleMode(hDst, &cbIgn))
55 WriteConsoleW(hDst, pwszStr, (DWORD)RTUtf16Len(pwszStr), &cbIgn, NULL);
56 else
57 {
58 char *pszTmp;
59 int rc = RTUtf16ToUtf8(pwszStr, &pszTmp);
60 if (RT_SUCCESS(rc))
61 {
62 char *pszInCodepage;
63 rc = RTStrUtf8ToCurrentCP(&pszInCodepage, pszTmp);
64 if (RT_SUCCESS(rc))
65 {
66 WriteFile(hDst, pszInCodepage, (DWORD)strlen(pszInCodepage), &cbIgn, NULL);
67 RTStrFree(pszInCodepage);
68 }
69 else
70 WriteFile(hDst, RT_STR_TUPLE("<RTStrUtf8ToCurrentCP error>"), &cbIgn, NULL);
71 RTStrFree(pszTmp);
72 }
73 else
74 WriteFile(hDst, RT_STR_TUPLE("<RTUtf16ToUtf8 error>"), &cbIgn, NULL);
75 }
76}
77
78
79DECLINLINE(void) ErrorMsgBegin(const char *pszMsg)
80{
81 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
82 DWORD cbIgn;
83 WriteFile(hStdErr, RT_STR_TUPLE("error: "), &cbIgn, NULL);
84 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
85}
86
87
88DECLINLINE(void) ErrorMsgStr(const char *pszMsg)
89{
90 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
91 DWORD cbIgn;
92 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
93}
94
95
96DECLINLINE(void) ErrorMsgWStr(const wchar_t *pwszStr)
97{
98 OutputWStr(GetStdHandle(STD_ERROR_HANDLE), pwszStr);
99}
100
101
102DECLINLINE(int) ErrorMsgEnd(const char *pszMsg)
103{
104 HANDLE const hStdErr = GetStdHandle(STD_ERROR_HANDLE);
105 DWORD cbIgn;
106 if (pszMsg)
107 WriteFile(hStdErr, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
108 WriteFile(hStdErr, RT_STR_TUPLE("\r\n"), &cbIgn, NULL);
109#ifdef EXIT_FAIL /* VBoxDrvInst.cpp speciality */
110 return EXIT_FAIL;
111#else
112 return RTEXITCODE_FAILURE;
113#endif
114}
115
116
117DECLINLINE(void) ErrorMsgU64(uint64_t uValue, bool fSigned = false)
118{
119 char szVal[128];
120 RTStrFormatU64(szVal, sizeof(szVal), uValue, 10, 0, 0, fSigned ? RTSTR_F_VALSIGNED : 0);
121 ErrorMsgStr(szVal);
122}
123
124
125DECLINLINE(void) ErrorMsgX64(uint64_t uValue)
126{
127 char szVal[128];
128 RTStrFormatU64(szVal, sizeof(szVal), uValue, 16, 0, 0, 0);
129 ErrorMsgStr(szVal);
130}
131
132
133DECLINLINE(int) ErrorMsg(const char *pszMsg)
134{
135 ErrorMsgBegin(pszMsg);
136 return ErrorMsgEnd(NULL);
137}
138
139
140DECLINLINE(int) ErrorMsgSU(const char *pszMsg1, uint64_t uValue1)
141{
142 ErrorMsgBegin(pszMsg1);
143 ErrorMsgU64(uValue1);
144 return ErrorMsgEnd(NULL);
145}
146
147
148DECLINLINE(int) ErrorMsgSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
149{
150 ErrorMsgBegin(pszMsg1);
151 ErrorMsgWStr(pwszMsg2);
152 return ErrorMsgEnd(pszMsg3);
153}
154
155
156DECLINLINE(int) ErrorMsgSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
157 const wchar_t *pwszMsg4, const char *pszMsg5)
158{
159 ErrorMsgBegin(pszMsg1);
160 ErrorMsgWStr(pwszMsg2);
161 ErrorMsgStr(pszMsg3);
162 ErrorMsgWStr(pwszMsg4);
163 return ErrorMsgEnd(pszMsg5);
164}
165
166
167DECLINLINE(int) ErrorMsgSUSUS(const char *pszMsg1, uint64_t uValue1, const char *pszMsg2, uint64_t uValue2, const char *pszMsg3)
168{
169 ErrorMsgBegin(pszMsg1);
170 ErrorMsgU64(uValue1);
171 ErrorMsgStr(pszMsg2);
172 ErrorMsgU64(uValue2);
173 return ErrorMsgEnd(pszMsg3);
174}
175
176
177DECLINLINE(void) ErrorMsgErrVal(uint32_t uErrVal, bool fSigned)
178{
179 char szVal[128];
180 ssize_t cchVal = RTStrFormatU32(szVal, sizeof(szVal) - 1, uErrVal, 10, 0, 0, fSigned ? RTSTR_F_VALSIGNED : 0);
181 szVal[cchVal++] = '/';
182 szVal[cchVal] = '\0';
183 ErrorMsgStr(szVal);
184
185 RTStrFormatU32(szVal, sizeof(szVal) - 1, uErrVal, 16, 0, 0, RTSTR_F_SPECIAL);
186 ErrorMsgStr(szVal);
187}
188
189
190DECLINLINE(int) ErrorMsgErr(const char *pszMsg, uint32_t uErrVal, const char *pszErrIntro, bool fSigned)
191{
192 ErrorMsgBegin(pszMsg);
193 ErrorMsgStr(pszErrIntro);
194 ErrorMsgErrVal(uErrVal, fSigned);
195 return ErrorMsgEnd(")");
196}
197
198
199DECLINLINE(int) ErrorMsgRc(int rcExit, const char *pszMsg)
200{
201 ErrorMsgBegin(pszMsg);
202 ErrorMsgEnd(NULL);
203 return rcExit;
204}
205
206
207DECLINLINE(int) ErrorMsgRcSU(int rcExit, const char *pszMsg1, uint64_t uValue)
208{
209 ErrorMsgBegin(pszMsg1);
210 ErrorMsgU64(uValue);
211 ErrorMsgEnd(NULL);
212 return rcExit;
213}
214
215
216DECLINLINE(int) ErrorMsgRcSX(int rcExit, const char *pszMsg1, uint64_t uValue)
217{
218 ErrorMsgBegin(pszMsg1);
219 ErrorMsgX64(uValue);
220 ErrorMsgEnd(NULL);
221 return rcExit;
222}
223
224
225DECLINLINE(int) ErrorMsgRcSUS(int rcExit, const char *pszMsg1, uint64_t uValue, const char *pszMsg2)
226{
227 ErrorMsgBegin(pszMsg1);
228 ErrorMsgU64(uValue);
229 ErrorMsgEnd(pszMsg2);
230 return rcExit;
231}
232
233
234DECLINLINE(int) ErrorMsgRcSW(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2)
235{
236 ErrorMsgBegin(pszMsg1);
237 ErrorMsgWStr(pwszMsg2);
238 ErrorMsgEnd(NULL);
239 return rcExit;
240}
241
242
243DECLINLINE(int) ErrorMsgRcSWS(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
244{
245 ErrorMsgBegin(pszMsg1);
246 ErrorMsgWStr(pwszMsg2);
247 ErrorMsgEnd(pszMsg3);
248 return rcExit;
249}
250
251
252DECLINLINE(int) ErrorMsgRcSWSS(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const char *pszMsg4)
253{
254 ErrorMsgBegin(pszMsg1);
255 ErrorMsgWStr(pwszMsg2);
256 ErrorMsgStr(pszMsg3);
257 ErrorMsgEnd(pszMsg4);
258 return rcExit;
259}
260
261
262DECLINLINE(int) ErrorMsgRcSWSU(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, uint64_t uValue4)
263{
264 ErrorMsgBegin(pszMsg1);
265 ErrorMsgWStr(pwszMsg2);
266 ErrorMsgStr(pszMsg3);
267 ErrorMsgU64(uValue4);
268 ErrorMsgEnd(NULL);
269 return rcExit;
270}
271
272
273DECLINLINE(int) ErrorMsgRcSWSX(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, uint64_t uValue4)
274{
275 ErrorMsgBegin(pszMsg1);
276 ErrorMsgWStr(pwszMsg2);
277 ErrorMsgStr(pszMsg3);
278 ErrorMsgX64(uValue4);
279 ErrorMsgEnd(NULL);
280 return rcExit;
281}
282
283
284DECLINLINE(int) ErrorMsgVBoxErr(const char *pszMsg, int rc)
285{
286 return ErrorMsgErr(pszMsg, rc, " (", true);
287}
288
289
290DECLINLINE(int) ErrorMsgLastErr(const char *pszMsg)
291{
292 return ErrorMsgErr(pszMsg, GetLastError(), " (last error ", false);
293}
294
295
296DECLINLINE(int) ErrorMsgLastErrSUR(const char *pszMsg1, uint64_t uValue)
297{
298 DWORD dwErr = GetLastError();
299 ErrorMsgBegin(pszMsg1);
300 ErrorMsgU64(uValue);
301 ErrorMsgStr(" (last error ");
302 ErrorMsgErrVal(dwErr, false);
303 return ErrorMsgEnd(")");
304}
305
306
307DECLINLINE(int) ErrorMsgLastErrSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
308{
309 DWORD dwErr = GetLastError();
310 ErrorMsgBegin(pszMsg1);
311 ErrorMsgWStr(pwszMsg2);
312 ErrorMsgStr(pszMsg3);
313 ErrorMsgStr(" (last error ");
314 ErrorMsgErrVal(dwErr, false);
315 return ErrorMsgEnd(")");
316}
317
318
319DECLINLINE(int) ErrorMsgLastErrSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
320 const wchar_t *pwszMsg4, const char *pszMsg5)
321{
322 DWORD dwErr = GetLastError();
323 ErrorMsgBegin(pszMsg1);
324 ErrorMsgWStr(pwszMsg2);
325 ErrorMsgStr(pszMsg3);
326 ErrorMsgWStr(pwszMsg4);
327 ErrorMsgStr(pszMsg5);
328 ErrorMsgStr(" (last error ");
329 ErrorMsgErrVal(dwErr, false);
330 return ErrorMsgEnd(")");
331}
332
333
334DECLINLINE(int) ErrorMsgLastErrSWSRSUS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const char *pszMsg4,
335 uint64_t uValue, const char *pszMsg5)
336{
337 DWORD dwErr = GetLastError();
338 ErrorMsgBegin(pszMsg1);
339 ErrorMsgWStr(pwszMsg2);
340 ErrorMsgStr(pszMsg3);
341 ErrorMsgStr(" (last error ");
342 ErrorMsgErrVal(dwErr, false);
343 ErrorMsgStr(")");
344 ErrorMsgStr(pszMsg4);
345 ErrorMsgU64(uValue);
346 return ErrorMsgEnd(pszMsg5);
347}
348
349
350DECLINLINE(int) ErrorMsgLastErrSSS(const char *pszMsg1, const char *pszMsg2, const char *pszMsg3)
351{
352 DWORD dwErr = GetLastError();
353 ErrorMsgBegin(pszMsg1);
354 ErrorMsgStr(pszMsg2);
355 ErrorMsgStr(pszMsg3);
356 ErrorMsgStr(" (last error ");
357 ErrorMsgErrVal(dwErr, false);
358 return ErrorMsgEnd(")");
359}
360
361
362DECLINLINE(int) ErrorMsgRcLastErr(int rcExit, const char *pszMsg)
363{
364 ErrorMsgErr(pszMsg, GetLastError(), " (last error ", false);
365 return rcExit;
366}
367
368
369DECLINLINE(int) ErrorMsgRcLastErrSUR(int rcExit, const char *pszMsg1, uint64_t uValue)
370{
371 ErrorMsgLastErrSUR(pszMsg1, uValue);
372 return rcExit;
373}
374
375
376static int ErrorMsgRcLastErrSWSR(int rcExit, const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
377{
378 DWORD dwErr = GetLastError();
379 ErrorMsgBegin(pszMsg1);
380 ErrorMsgWStr(pwszMsg2);
381 ErrorMsgStr(pszMsg3);
382 ErrorMsgStr(" (last error ");
383 ErrorMsgErrVal(dwErr, false);
384 ErrorMsgEnd(")");
385 return rcExit;
386}
387
388
389
390DECLINLINE(int) ErrorMsgLStatus(const char *pszMsg, LSTATUS lrc)
391{
392 return ErrorMsgErr(pszMsg, (DWORD)lrc, " (", true);
393}
394
395
396DECLINLINE(int) ErrorMsgLStatusSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
397 LSTATUS lrc, const char *pszMsg4)
398{
399 ErrorMsgBegin(pszMsg1);
400 ErrorMsgWStr(pwszMsg2);
401 ErrorMsgStr(pszMsg3);
402 ErrorMsgErrVal((DWORD)lrc, true);
403 return ErrorMsgEnd(pszMsg4);
404}
405
406
407DECLINLINE(int) ErrorMsgLStatusSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
408 const char *pszMsg5, LSTATUS lrc, const char *pszMsg6)
409{
410 ErrorMsgBegin(pszMsg1);
411 ErrorMsgWStr(pwszMsg2);
412 ErrorMsgStr(pszMsg3);
413 ErrorMsgWStr(pwszMsg4);
414 ErrorMsgStr(pszMsg5);
415 ErrorMsgErrVal((DWORD)lrc, true);
416 return ErrorMsgEnd(pszMsg6);
417}
418
419
420DECLINLINE(int) ErrorMsgLStatusSWSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
421 const wchar_t *pwszMsg4, const char *pszMsg5, const wchar_t *pwszMsg6,
422 const char *pszMsg7, LSTATUS lrc, const char *pszMsg8)
423{
424 ErrorMsgBegin(pszMsg1);
425 ErrorMsgWStr(pwszMsg2);
426 ErrorMsgStr(pszMsg3);
427 ErrorMsgWStr(pwszMsg4);
428 ErrorMsgStr(pszMsg5);
429 ErrorMsgWStr(pwszMsg6);
430 ErrorMsgStr(pszMsg7);
431 ErrorMsgErrVal((DWORD)lrc, true);
432 return ErrorMsgEnd(pszMsg8);
433}
434
435
436DECLINLINE(int) ErrorMsgLStatusSWSWSWSWSRS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
437 const wchar_t *pwszMsg4, const char *pszMsg5, const wchar_t *pwszMsg6,
438 const char *pszMsg7, const wchar_t *pwszMsg8, const char *pszMsg9, LSTATUS lrc,
439 const char *pszMsg10)
440{
441 ErrorMsgBegin(pszMsg1);
442 ErrorMsgWStr(pwszMsg2);
443 ErrorMsgStr(pszMsg3);
444 ErrorMsgWStr(pwszMsg4);
445 ErrorMsgStr(pszMsg5);
446 ErrorMsgWStr(pwszMsg6);
447 ErrorMsgStr(pszMsg7);
448 ErrorMsgWStr(pwszMsg8);
449 ErrorMsgStr(pszMsg9);
450 ErrorMsgErrVal((DWORD)lrc, true);
451 return ErrorMsgEnd(pszMsg10);
452}
453
454
455DECLINLINE(int) ErrorBadArg(const char *pszName, wchar_t const *pwszArg, const char *pszValues = NULL)
456{
457 ErrorMsgBegin("Bad argument '");
458 ErrorMsgStr(pszName);
459 ErrorMsgStr("': ");
460 ErrorMsgWStr(pwszArg);
461 if (pszValues)
462 ErrorMsgStr(", expected: ");
463 return ErrorMsgEnd(pszValues);
464}
465
466
467/** Simple fputs(stdout) replacement. */
468DECLINLINE(void) PrintStr(const char *pszMsg)
469{
470 HANDLE const hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
471 DWORD cbIgn;
472 WriteFile(hStdOut, pszMsg, (DWORD)strlen(pszMsg), &cbIgn, NULL);
473}
474
475
476/** Simple fputs(stdout) replacement. */
477DECLINLINE(void) PrintWStr(const wchar_t *pwszStr)
478{
479 OutputWStr(GetStdHandle(STD_OUTPUT_HANDLE), pwszStr);
480}
481
482
483DECLINLINE(void) PrintX64(uint64_t uValue)
484{
485 char szVal[128];
486 RTStrFormatU64(szVal, sizeof(szVal), uValue, 16, 0, 0, RTSTR_F_64BIT | RTSTR_F_SPECIAL);
487 PrintStr(szVal);
488}
489
490
491DECLINLINE(void) PrintSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3)
492{
493 PrintStr(pszMsg1);
494 PrintWStr(pwszMsg2);
495 PrintStr(pszMsg3);
496}
497
498
499DECLINLINE(void) PrintSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3,
500 const wchar_t *pwszMsg4, const char *pszMsg5)
501{
502 PrintStr(pszMsg1);
503 PrintWStr(pwszMsg2);
504 PrintStr(pszMsg3);
505 PrintWStr(pwszMsg4);
506 PrintStr(pszMsg5);
507}
508
509
510DECLINLINE(void) PrintSWSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
511 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7)
512{
513 PrintStr(pszMsg1);
514 PrintWStr(pwszMsg2);
515 PrintStr(pszMsg3);
516 PrintWStr(pwszMsg4);
517 PrintStr(pszMsg5);
518 PrintWStr(pwszMsg6);
519 PrintStr(pszMsg7);
520}
521
522
523DECLINLINE(void) PrintSWSWSWSWS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
524 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7, const wchar_t *pwszMsg8,
525 const char *pszMsg9)
526{
527 PrintStr(pszMsg1);
528 PrintWStr(pwszMsg2);
529 PrintStr(pszMsg3);
530 PrintWStr(pwszMsg4);
531 PrintStr(pszMsg5);
532 PrintWStr(pwszMsg6);
533 PrintStr(pszMsg7);
534 PrintWStr(pwszMsg8);
535 PrintStr(pszMsg9);
536}
537
538
539DECLINLINE(void) PrintSXS(const char *pszMsg1, uint64_t uValue, const char *pszMsg2)
540{
541 PrintStr(pszMsg1);
542 PrintX64(uValue);
543 PrintStr(pszMsg2);
544}
545
546
547DECLINLINE(void) PrintSWSWSWSXS(const char *pszMsg1, const wchar_t *pwszMsg2, const char *pszMsg3, const wchar_t *pwszMsg4,
548 const char *pszMsg5, const wchar_t *pwszMsg6, const char *pszMsg7, uint64_t uValue,
549 const char *pszMsg8)
550{
551 PrintStr(pszMsg1);
552 PrintWStr(pwszMsg2);
553 PrintStr(pszMsg3);
554 PrintWStr(pwszMsg4);
555 PrintStr(pszMsg5);
556 PrintWStr(pwszMsg6);
557 PrintStr(pszMsg7);
558 PrintX64(uValue);
559 PrintStr(pszMsg8);
560}
561
562/** @} */
563
564#endif /* !GA_INCLUDED_WINNT_NoCrtOutput_h */
565
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette