VirtualBox

source: vbox/trunk/src/VBox/Runtime/win/errmsgwin.cpp@ 84009

Last change on this file since 84009 was 83984, checked in by vboxsync, 5 years ago

IPRT: Reduce the amoung of strings in errmsg and errmsgwin in static builds (IN_RT_STATIC). The windows ones are almost never used anywhere anyways.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 6.2 KB
Line 
1/* $Id: errmsgwin.cpp 83984 2020-04-27 00:17:19Z vboxsync $ */
2/** @file
3 * IPRT - Status code messages, Windows.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include <iprt/win/windows.h>
32
33#include <iprt/errcore.h>
34#include <iprt/asm.h>
35#include <iprt/string.h>
36
37
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41#if defined(IPRT_ERRMSG_DEFINES_ONLY) || defined(IN_RT_STATIC) /* No message text in static builds to save space. */
42# define ENTRY(a_pszMsg, a_pszDefine, a_iCode) \
43 { a_pszDefine, a_pszDefine, a_iCode }
44#else
45# define ENTRY(a_pszMsg, a_pszDefine, a_iCode) \
46 { a_pszMsg, a_pszDefine, a_iCode }
47#endif
48
49
50/*********************************************************************************************************************************
51* Global Variables *
52*********************************************************************************************************************************/
53/** Array of messages.
54 * The data is generated by sed scripts and sorted by errmsgwin-sorter.cpp.
55 */
56static const RTWINERRMSG g_aStatusMsgs[] =
57{
58#if !defined(IPRT_NO_ERROR_DATA) && !defined(DOXYGEN_RUNNING)
59# include "errmsgwindata-sorted.h"
60#else
61 { "Success.", "ERROR_SUCCESS", 0 },
62#endif
63};
64
65
66/** Temporary buffers to format unknown messages in.
67 * @{
68 */
69static char g_aszUnknownStr[8][64];
70static RTWINERRMSG g_aUnknownMsgs[8] =
71{
72 { &g_aszUnknownStr[0][0], &g_aszUnknownStr[0][0], 0 },
73 { &g_aszUnknownStr[1][0], &g_aszUnknownStr[1][0], 0 },
74 { &g_aszUnknownStr[2][0], &g_aszUnknownStr[2][0], 0 },
75 { &g_aszUnknownStr[3][0], &g_aszUnknownStr[3][0], 0 },
76 { &g_aszUnknownStr[4][0], &g_aszUnknownStr[4][0], 0 },
77 { &g_aszUnknownStr[5][0], &g_aszUnknownStr[5][0], 0 },
78 { &g_aszUnknownStr[6][0], &g_aszUnknownStr[6][0], 0 },
79 { &g_aszUnknownStr[7][0], &g_aszUnknownStr[7][0], 0 },
80};
81/** Last used index in g_aUnknownMsgs. */
82static volatile uint32_t g_iUnknownMsgs;
83/** @} */
84
85
86/**
87 * Get the message corresponding to a given status code.
88 *
89 * @returns Pointer to read-only message description.
90 * @param rc The status code.
91 */
92RTDECL(PCRTWINERRMSG) RTErrWinGet(long rc)
93{
94 /*
95 * Perform binary search (duplicate code in RTErrGet).
96 */
97 size_t iStart = 0;
98 size_t iEnd = RT_ELEMENTS(g_aStatusMsgs);
99 for (;;)
100 {
101 size_t i = iStart + (iEnd - iStart) / 2;
102 long const iCode = g_aStatusMsgs[i].iCode;
103 if (rc < iCode)
104 {
105 if (iStart < i)
106 iEnd = i;
107 else
108 break;
109 }
110 else if (rc > iCode)
111 {
112 i++;
113 if (i < iEnd)
114 iStart = i;
115 else
116 break;
117 }
118 else
119 return &g_aStatusMsgs[i];
120 }
121
122#ifdef RT_STRICT
123 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
124 Assert(g_aStatusMsgs[i].iCode != rc);
125#endif
126
127 /*
128 * If FACILITY_WIN32 kind of status, look up the win32 code.
129 */
130 if (SCODE_FACILITY(rc) == FACILITY_WIN32)
131 {
132 long const rcWin32 = HRESULT_CODE(rc);
133 iStart = 0;
134 iEnd = RT_ELEMENTS(g_aStatusMsgs);
135 for (;;)
136 {
137 size_t i = iStart + (iEnd - iStart) / 2;
138 long const iCode = g_aStatusMsgs[i].iCode;
139 if (rcWin32 < iCode)
140 {
141 if (iStart < i)
142 iEnd = i;
143 else
144 break;
145 }
146 else if (rcWin32 > iCode)
147 {
148 i++;
149 if (i < iEnd)
150 iStart = i;
151 else
152 break;
153 }
154 else
155 {
156 /* Append the incoming rc, so we know it's not a regular WIN32 status: */
157 int32_t iMsg = (ASMAtomicIncU32(&g_iUnknownMsgs) - 1) % RT_ELEMENTS(g_aUnknownMsgs);
158 RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "%s/0x%x", g_aStatusMsgs[i].pszDefine, rc);
159 return &g_aUnknownMsgs[iMsg];
160 }
161 }
162
163#ifdef RT_STRICT
164 for (size_t i = 0; i < RT_ELEMENTS(g_aStatusMsgs); i++)
165 Assert(g_aStatusMsgs[i].iCode != rcWin32);
166#endif
167 }
168
169 /*
170 * Need to use the temporary stuff.
171 */
172 int32_t iMsg = (ASMAtomicIncU32(&g_iUnknownMsgs) - 1) % RT_ELEMENTS(g_aUnknownMsgs);
173 RTStrPrintf(&g_aszUnknownStr[iMsg][0], sizeof(g_aszUnknownStr[iMsg]), "Unknown Status 0x%X", rc);
174 return &g_aUnknownMsgs[iMsg];
175}
176
177
178RTDECL(PCRTCOMERRMSG) RTErrCOMGet(uint32_t rc)
179{
180 return RTErrWinGet((long)rc);
181}
182
Note: See TracBrowser for help on using the repository browser.

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