-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from microsoft/add-ntstatus-errors
Add NTStatus errors
- Loading branch information
Showing
5 changed files
with
167 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package bcrypt | ||
|
||
import ( | ||
"fmt" | ||
"syscall" | ||
"unicode/utf16" | ||
) | ||
|
||
const ( | ||
FORMAT_MESSAGE_FROM_HMODULE = 2048 | ||
FORMAT_MESSAGE_FROM_SYSTEM = 4096 | ||
FORMAT_MESSAGE_ARGUMENT_ARRAY = 8192 | ||
|
||
LANG_ENGLISH = 0x09 | ||
SUBLANG_ENGLISH_US = 0x01 | ||
) | ||
|
||
type NTStatus uint32 | ||
|
||
func (s NTStatus) Errno() syscall.Errno { | ||
return rtlNtStatusToDosErrorNoTeb(s) | ||
} | ||
|
||
func langID(pri, sub uint16) uint32 { return uint32(sub)<<10 | uint32(pri) } | ||
|
||
func (s NTStatus) Error() string { | ||
b := make([]uint16, 300) | ||
n, err := formatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_FROM_HMODULE|FORMAT_MESSAGE_ARGUMENT_ARRAY, modntdll.Handle(), uint32(s), langID(LANG_ENGLISH, SUBLANG_ENGLISH_US), b, nil) | ||
if err != nil { | ||
return fmt.Sprintf("NTSTATUS 0x%08x", uint32(s)) | ||
} | ||
// trim terminating \r and \n | ||
for ; n > 0 && (b[n-1] == '\n' || b[n-1] == '\r'); n-- { | ||
} | ||
return string(utf16.Decode(b[:n])) | ||
} | ||
|
||
// NT Native APIs | ||
//sys rtlNtStatusToDosErrorNoTeb(ntstatus NTStatus) (ret syscall.Errno) = ntdll.RtlNtStatusToDosErrorNoTeb | ||
|
||
// windows api calls | ||
//sys formatMessage(flags uint32, msgsrc uintptr, msgid uint32, langid uint32, buf []uint16, args *byte) (n uint32, err error) = FormatMessageW |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package bcrypt | ||
|
||
import "testing" | ||
|
||
func TestNTStatusErrMessage(t *testing.T) { | ||
|
||
StatusWait1 := NTStatus(0x00000001) | ||
StatusWait2 := NTStatus(0x00000002) | ||
|
||
if StatusWait1.Error() != "STATUS_WAIT_1" { | ||
t.Errorf("Expected STATUS_WAIT_2, got %s", StatusWait2.Error()) | ||
} | ||
|
||
if StatusWait2.Error() != "STATUS_WAIT_2" { | ||
t.Errorf("Expected STATUS_WAIT_2, got %s", StatusWait2.Error()) | ||
} | ||
} |
Oops, something went wrong.