1 //
2 //    Copyright (C) Microsoft.  All rights reserved.
3 //
4 #include "Mx.h"
5 
6 BOOL
ConvertWinErrorToNtstatus(__in ULONG WinError,__out NTSTATUS * Status)7 ConvertWinErrorToNtstatus(
8     __in    ULONG       WinError,
9     __out   NTSTATUS    * Status
10     )
11 {
12     ULONG index = 0;
13     BOOL found = FALSE;
14 
15     *Status = INVALID_STATUS;
16 
17     for ( ULONG i = 0; i < ARRAYSIZE(ErrorBucketTable); i++ )
18     {
19         if (WinError < ErrorBucketTable[i].BaseErrorCode)
20         {
21             //
22             // The error code falls between previous bucket end and current
23             // bucket begin, hence there is no mapping for it
24             //
25             break;
26         }
27 
28         if ( WinError < (ErrorBucketTable[i].BaseErrorCode +
29                                                 ErrorBucketTable[i].RunLength) )
30         {
31             //
32             // Index falls within the current bucket
33             //
34             index += (WinError - ErrorBucketTable[i].BaseErrorCode);
35             found = TRUE;
36             break;
37         }
38         else
39         {
40             //
41             // Index is beyond current bucket, continue search
42             //
43             index += ErrorBucketTable[i].RunLength;
44         }
45     }
46 
47     if (TRUE == found)
48     {
49         *Status = ErrorTable[index];
50         if (INVALID_STATUS == (*Status))
51         {
52             found = FALSE;
53         }
54     }
55 
56     return found;
57 }
58 
59 NTSTATUS
WinErrorToNtStatus(__in ULONG WinError)60 WinErrorToNtStatus(
61     __in ULONG WinError
62     )
63 {
64     NTSTATUS status;
65 
66     if (TRUE == ConvertWinErrorToNtstatus(WinError, &status))
67     {
68         return status;
69     }
70     else
71     {
72         return STATUS_UNSUCCESSFUL;
73     }
74 }
75