4d083106d3ece7e357967829e51c08c3fdfdde60
[jra/samba/.git] / source3 / include / nt_status.h
1 /* 
2    Unix SMB/CIFS implementation.
3    SMB parameters and setup, plus a whole lot more.
4    
5    Copyright (C) Andrew Tridgell              2001
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #ifndef _NT_STATUS_H
23 #define _NT_STATUS_H
24
25 /* The Splint code analysis tool doesn't like immediate structures. */
26
27 #ifdef _SPLINT_                      /* http://www.splint.org */
28 #undef HAVE_IMMEDIATE_STRUCTURES
29 #endif
30
31 /* the following rather strange looking definitions of NTSTATUS and WERROR
32    and there in order to catch common coding errors where different error types
33    are mixed up. This is especially important as we slowly convert Samba
34    from using BOOL for internal functions 
35 */
36
37 #if defined(HAVE_IMMEDIATE_STRUCTURES)
38 typedef struct {uint32 v;} NTSTATUS;
39 #define NT_STATUS(x) ((NTSTATUS) { x })
40 #define NT_STATUS_V(x) ((x).v)
41 #else
42 typedef uint32 NTSTATUS;
43 #define NT_STATUS(x) (x)
44 #define NT_STATUS_V(x) (x)
45 #endif
46
47 #if defined(HAVE_IMMEDIATE_STRUCTURES)
48 typedef struct {uint32 w;} WERROR;
49 #define W_ERROR(x) ((WERROR) { x })
50 #define W_ERROR_V(x) ((x).w)
51 #else
52 typedef uint32 WERROR;
53 #define W_ERROR(x) (x)
54 #define W_ERROR_V(x) (x)
55 #endif
56
57 #define NT_STATUS_IS_OK(x) (NT_STATUS_V(x) == 0)
58 #define NT_STATUS_IS_ERR(x) ((NT_STATUS_V(x) & 0xc0000000) == 0xc0000000)
59 #define NT_STATUS_EQUAL(x,y) (NT_STATUS_V(x) == NT_STATUS_V(y))
60 #define W_ERROR_IS_OK(x) (W_ERROR_V(x) == 0)
61 #define W_ERROR_EQUAL(x,y) (W_ERROR_V(x) == W_ERROR_V(y))
62
63 #define NT_STATUS_HAVE_NO_MEMORY(x) do { \
64         if (!(x)) {\
65                 return NT_STATUS_NO_MEMORY;\
66         }\
67 } while (0)
68
69 #define NT_STATUS_NOT_OK_RETURN(x) do { \
70         if (!NT_STATUS_IS_OK(x)) {\
71                 return x;\
72         }\
73 } while (0)
74
75 #define W_ERROR_NOT_OK_RETURN(x) do { \
76         if (!W_ERROR_IS_OK(x)) {\
77                 return x;\
78         }\
79 } while (0)
80
81 /* The top byte in an NTSTATUS code is used as a type field.
82  * Windows only uses value 0xC0 as an indicator for an NT error
83  * and 0x00 for success.
84  * So we can use the type field to store other types of error codes
85  * inside the three lower bytes. 
86  * NB: The system error codes (errno) are not integrated via a type of
87  *     their own but are mapped to genuine NT error codes via 
88  *     map_nt_error_from_unix() */
89
90 #define NT_STATUS_TYPE(status) ((NT_STATUS_V(status) & 0xFF000000) >> 24)
91
92 #define NT_STATUS_TYPE_DOS  0xF1
93 #define NT_STATUS_TYPE_LDAP 0xF2
94
95 /* this defines special NTSTATUS codes to represent DOS errors.  I
96    have chosen this macro to produce status codes in the invalid
97    NTSTATUS range */
98 #define NT_STATUS_DOS_MASK (NT_STATUS_TYPE_DOS << 24)
99 #define NT_STATUS_DOS(class, code) NT_STATUS(NT_STATUS_DOS_MASK | ((class)<<16) | code)
100 #define NT_STATUS_IS_DOS(status) ((NT_STATUS_V(status) & 0xFF000000) == NT_STATUS_DOS_MASK)
101 #define NT_STATUS_DOS_CLASS(status) ((NT_STATUS_V(status) >> 16) & 0xFF)
102 #define NT_STATUS_DOS_CODE(status) (NT_STATUS_V(status) & 0xFFFF)
103
104 /* define ldap error codes as NTSTATUS codes */
105 #define NT_STATUS_LDAP_MASK (NT_STATUS_TYPE_LDAP << 24)
106 #define NT_STATUS_LDAP(code) NT_STATUS(NT_STATUS_LDAP_MASK | code)
107 #define NT_STATUS_IS_LDAP(status) ((NT_STATUS_V(status) & 0xFF000000) == NT_STATUS_LDAP_MASK)
108 #define NT_STATUS_LDAP_CODE(status) (NT_STATUS_V(status) & ~0xFF000000)
109
110 #endif