r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[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 3 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, see <http://www.gnu.org/licenses/>.
19 */
20
21 #ifndef _NT_STATUS_H
22 #define _NT_STATUS_H
23
24 /* The Splint code analysis tool doesn't like immediate structures. */
25
26 #ifdef _SPLINT_                      /* http://www.splint.org */
27 #undef HAVE_IMMEDIATE_STRUCTURES
28 #endif
29
30 /* the following rather strange looking definitions of NTSTATUS and WERROR
31    and there in order to catch common coding errors where different error types
32    are mixed up. This is especially important as we slowly convert Samba
33    from using BOOL for internal functions 
34 */
35
36 #if defined(HAVE_IMMEDIATE_STRUCTURES)
37 typedef struct {uint32 v;} NTSTATUS;
38 #define NT_STATUS(x) ((NTSTATUS) { x })
39 #define NT_STATUS_V(x) ((x).v)
40 #else
41 typedef uint32 NTSTATUS;
42 #define NT_STATUS(x) (x)
43 #define NT_STATUS_V(x) (x)
44 #endif
45
46 #if defined(HAVE_IMMEDIATE_STRUCTURES)
47 typedef struct {uint32 w;} WERROR;
48 #define W_ERROR(x) ((WERROR) { x })
49 #define W_ERROR_V(x) ((x).w)
50 #else
51 typedef uint32 WERROR;
52 #define W_ERROR(x) (x)
53 #define W_ERROR_V(x) (x)
54 #endif
55
56 #define NT_STATUS_IS_OK(x) (NT_STATUS_V(x) == 0)
57 #define NT_STATUS_IS_ERR(x) ((NT_STATUS_V(x) & 0xc0000000) == 0xc0000000)
58 #define NT_STATUS_EQUAL(x,y) (NT_STATUS_V(x) == NT_STATUS_V(y))
59 #define W_ERROR_IS_OK(x) (W_ERROR_V(x) == 0)
60 #define W_ERROR_EQUAL(x,y) (W_ERROR_V(x) == W_ERROR_V(y))
61
62 #define NT_STATUS_HAVE_NO_MEMORY(x) do { \
63         if (!(x)) {\
64                 return NT_STATUS_NO_MEMORY;\
65         }\
66 } while (0)
67
68 #define NT_STATUS_NOT_OK_RETURN(x) do { \
69         if (!NT_STATUS_IS_OK(x)) {\
70                 return x;\
71         }\
72 } while (0)
73
74 #define W_ERROR_NOT_OK_RETURN(x) do { \
75         if (!W_ERROR_IS_OK(x)) {\
76                 return x;\
77         }\
78 } while (0)
79
80 /* The top byte in an NTSTATUS code is used as a type field.
81  * Windows only uses value 0xC0 as an indicator for an NT error
82  * and 0x00 for success.
83  * So we can use the type field to store other types of error codes
84  * inside the three lower bytes. 
85  * NB: The system error codes (errno) are not integrated via a type of
86  *     their own but are mapped to genuine NT error codes via 
87  *     map_nt_error_from_unix() */
88
89 #define NT_STATUS_TYPE(status) ((NT_STATUS_V(status) & 0xFF000000) >> 24)
90
91 #define NT_STATUS_TYPE_DOS  0xF1
92 #define NT_STATUS_TYPE_LDAP 0xF2
93
94 /* this defines special NTSTATUS codes to represent DOS errors.  I
95    have chosen this macro to produce status codes in the invalid
96    NTSTATUS range */
97 #define NT_STATUS_DOS_MASK (NT_STATUS_TYPE_DOS << 24)
98 #define NT_STATUS_DOS(class, code) NT_STATUS(NT_STATUS_DOS_MASK | ((class)<<16) | code)
99 #define NT_STATUS_IS_DOS(status) ((NT_STATUS_V(status) & 0xFF000000) == NT_STATUS_DOS_MASK)
100 #define NT_STATUS_DOS_CLASS(status) ((NT_STATUS_V(status) >> 16) & 0xFF)
101 #define NT_STATUS_DOS_CODE(status) (NT_STATUS_V(status) & 0xFFFF)
102
103 /* define ldap error codes as NTSTATUS codes */
104 #define NT_STATUS_LDAP_MASK (NT_STATUS_TYPE_LDAP << 24)
105 #define NT_STATUS_LDAP(code) NT_STATUS(NT_STATUS_LDAP_MASK | code)
106 #define NT_STATUS_IS_LDAP(status) ((NT_STATUS_V(status) & 0xFF000000) == NT_STATUS_LDAP_MASK)
107 #define NT_STATUS_LDAP_CODE(status) (NT_STATUS_V(status) & ~0xFF000000)
108
109 #endif