1 | /* $Id: errmsgwin.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Status code messages, Windows.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-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 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/win/windows.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 | #include <iprt/bldprog-strtab.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Global Variables *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | #if defined(IPRT_NO_ERROR_DATA) || defined(IPRT_NO_WIN_ERROR_DATA)
|
---|
54 | /* Cook data just for VINF_SUCCESS so that code below compiles fine. */
|
---|
55 | static const char g_achWinStrTabData[] = { "ERROR_SUCCESS" };
|
---|
56 | static const RTBLDPROGSTRTAB g_WinMsgStrTab = { g_achWinStrTabData, sizeof(g_achWinStrTabData) - 1, 0, NULL };
|
---|
57 | static const struct
|
---|
58 | {
|
---|
59 | int16_t iCode;
|
---|
60 | uint8_t offDefine;
|
---|
61 | uint8_t cchDefine;
|
---|
62 | uint8_t offMsgFull;
|
---|
63 | uint8_t cchMsgFull;
|
---|
64 | } g_aWinMsgs[] =
|
---|
65 | {
|
---|
66 | { 0, 0, 13, 0, 13, },
|
---|
67 | };
|
---|
68 | #else
|
---|
69 | # include "errmsgwindata-only-defines.h"
|
---|
70 | #endif
|
---|
71 |
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Looks up the message table entry for @a rc.
|
---|
75 | *
|
---|
76 | * @returns index into g_aWinMsgs on success, ~(size_t)0 if not found.
|
---|
77 | * @param rc The status code to locate the entry for.
|
---|
78 | */
|
---|
79 | static size_t rtErrWinLookup(long rc)
|
---|
80 | {
|
---|
81 | /*
|
---|
82 | * Perform binary search (duplicate code in rtErrLookup).
|
---|
83 | */
|
---|
84 | size_t iStart = 0;
|
---|
85 | size_t iEnd = RT_ELEMENTS(g_aWinMsgs);
|
---|
86 | for (;;)
|
---|
87 | {
|
---|
88 | size_t i = iStart + (iEnd - iStart) / 2;
|
---|
89 | long const iCode = g_aWinMsgs[i].iCode;
|
---|
90 | if (rc < iCode)
|
---|
91 | {
|
---|
92 | if (iStart < i)
|
---|
93 | iEnd = i;
|
---|
94 | else
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | else if (rc > iCode)
|
---|
98 | {
|
---|
99 | i++;
|
---|
100 | if (i < iEnd)
|
---|
101 | iStart = i;
|
---|
102 | else
|
---|
103 | break;
|
---|
104 | }
|
---|
105 | else
|
---|
106 | return i;
|
---|
107 | }
|
---|
108 |
|
---|
109 | #ifdef RT_STRICT
|
---|
110 | for (size_t i = 0; i < RT_ELEMENTS(g_aWinMsgs); i++)
|
---|
111 | Assert(g_aWinMsgs[i].iCode != rc);
|
---|
112 | #endif
|
---|
113 |
|
---|
114 | return ~(size_t)0;
|
---|
115 | }
|
---|
116 |
|
---|
117 |
|
---|
118 | RTDECL(bool) RTErrWinIsKnown(long rc)
|
---|
119 | {
|
---|
120 | if (rtErrWinLookup(rc) != ~(size_t)0)
|
---|
121 | return true;
|
---|
122 | if (SCODE_FACILITY(rc) == FACILITY_WIN32)
|
---|
123 | {
|
---|
124 | if (rtErrWinLookup(HRESULT_CODE(rc)) != ~(size_t)0)
|
---|
125 | return true;
|
---|
126 | }
|
---|
127 | return false;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(ssize_t) RTErrWinQueryDefine(long rc, char *pszBuf, size_t cbBuf, bool fFailIfUnknown)
|
---|
132 | {
|
---|
133 | size_t idx = rtErrWinLookup(rc);
|
---|
134 | if (idx != ~(size_t)0)
|
---|
135 | return RTBldProgStrTabQueryString(&g_WinMsgStrTab,
|
---|
136 | g_aWinMsgs[idx].offDefine, g_aWinMsgs[idx].cchDefine,
|
---|
137 | pszBuf, cbBuf);
|
---|
138 |
|
---|
139 | /*
|
---|
140 | * If FACILITY_WIN32 kind of status, look up the win32 code.
|
---|
141 | */
|
---|
142 | if ( SCODE_FACILITY(rc) == FACILITY_WIN32
|
---|
143 | && (idx = rtErrWinLookup(HRESULT_CODE(rc))) != ~(size_t)0)
|
---|
144 | {
|
---|
145 | /* Append the incoming rc, so we know it's not a regular WIN32 status: */
|
---|
146 | ssize_t cchRet = RTBldProgStrTabQueryString(&g_WinMsgStrTab,
|
---|
147 | g_aWinMsgs[idx].offDefine, g_aWinMsgs[idx].cchDefine,
|
---|
148 | pszBuf, cbBuf);
|
---|
149 | if (cchRet > 0)
|
---|
150 | {
|
---|
151 | pszBuf[cchRet++] = '/';
|
---|
152 | return RTStrFormatU32(pszBuf + cchRet, cbBuf - cchRet, rc, 16, 0, 0, RTSTR_F_SPECIAL);
|
---|
153 | }
|
---|
154 | return VERR_BUFFER_OVERFLOW;
|
---|
155 | }
|
---|
156 |
|
---|
157 | if (fFailIfUnknown)
|
---|
158 | return VERR_NOT_FOUND;
|
---|
159 | return RTStrFormatU32(pszBuf, cbBuf, rc, 16, 0, 0, RTSTR_F_SPECIAL);
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | RTDECL(size_t) RTErrWinFormatDefine(long rc, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, char *pszTmp, size_t cbTmp)
|
---|
164 | {
|
---|
165 | RT_NOREF(pszTmp, cbTmp);
|
---|
166 | size_t idx = rtErrWinLookup(rc);
|
---|
167 | if (idx != ~(size_t)0)
|
---|
168 | return RTBldProgStrTabQueryOutput(&g_WinMsgStrTab,
|
---|
169 | g_aWinMsgs[idx].offDefine, g_aWinMsgs[idx].cchDefine,
|
---|
170 | pfnOutput, pvArgOutput);
|
---|
171 |
|
---|
172 | /*
|
---|
173 | * If FACILITY_WIN32 kind of status, look up the win32 code.
|
---|
174 | */
|
---|
175 | size_t cchRet = 0;
|
---|
176 | if ( SCODE_FACILITY(rc) == FACILITY_WIN32
|
---|
177 | && (idx = rtErrWinLookup(HRESULT_CODE(rc))) != ~(size_t)0)
|
---|
178 | {
|
---|
179 | /* Append the incoming rc, so we know it's not a regular WIN32 status: */
|
---|
180 | cchRet = RTBldProgStrTabQueryOutput(&g_WinMsgStrTab,
|
---|
181 | g_aWinMsgs[idx].offDefine, g_aWinMsgs[idx].cchDefine,
|
---|
182 | pfnOutput, pvArgOutput);
|
---|
183 | cchRet += pfnOutput(pvArgOutput, RT_STR_TUPLE("/"));
|
---|
184 | }
|
---|
185 |
|
---|
186 | ssize_t cchValue = RTStrFormatU32(pszTmp, cbTmp, rc, 16, 0, 0, RTSTR_F_SPECIAL);
|
---|
187 | Assert(cchValue > 0);
|
---|
188 | cchRet += pfnOutput(pvArgOutput, pszTmp, cchValue);
|
---|
189 | return cchRet;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | RTDECL(size_t) RTErrWinFormatMsg(long rc, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, char *pszTmp, size_t cbTmp)
|
---|
194 | {
|
---|
195 | return RTErrWinFormatDefine(rc, pfnOutput, pvArgOutput, pszTmp, cbTmp);
|
---|
196 | }
|
---|
197 |
|
---|
198 |
|
---|
199 | RTDECL(size_t) RTErrWinFormatMsgAll(long rc, PFNRTSTROUTPUT pfnOutput, void *pvArgOutput, char *pszTmp, size_t cbTmp)
|
---|
200 | {
|
---|
201 | RT_NOREF(pszTmp, cbTmp);
|
---|
202 | size_t cchRet;
|
---|
203 | size_t idx = rtErrWinLookup(rc);
|
---|
204 | if ( idx != ~(size_t)0
|
---|
205 | || ( SCODE_FACILITY(rc) == FACILITY_WIN32
|
---|
206 | && (idx = rtErrWinLookup(HRESULT_CODE(rc))) != ~(size_t)0))
|
---|
207 | {
|
---|
208 | cchRet = RTBldProgStrTabQueryOutput(&g_WinMsgStrTab,
|
---|
209 | g_aWinMsgs[idx].offDefine, g_aWinMsgs[idx].cchDefine,
|
---|
210 | pfnOutput, pvArgOutput);
|
---|
211 | cchRet += pfnOutput(pvArgOutput, RT_STR_TUPLE(" ("));
|
---|
212 | }
|
---|
213 | else
|
---|
214 | cchRet = pfnOutput(pvArgOutput, RT_STR_TUPLE("Unknown Status "));
|
---|
215 |
|
---|
216 | ssize_t cchValue = RTStrFormatU32(pszTmp, cbTmp, rc, 16, 0, 0, RTSTR_F_SPECIAL);
|
---|
217 | Assert(cchValue > 0);
|
---|
218 | cchRet += pfnOutput(pvArgOutput, pszTmp, cchValue);
|
---|
219 |
|
---|
220 | if (idx != ~(size_t)0)
|
---|
221 | cchRet += pfnOutput(pvArgOutput, RT_STR_TUPLE(")"));
|
---|
222 |
|
---|
223 | return cchRet;
|
---|
224 | }
|
---|
225 |
|
---|