06aeaa3dac0dbf0c2339038147b58e3ec4bb4ad1
[garming/samba-autobuild/.git] / source4 / libcli / util / 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 following rather strange looking definitions of NTSTATUS and WERROR
26    and there in order to catch common coding errors where different error types
27    are mixed up. This is especially important as we slowly convert Samba
28    from using BOOL for internal functions 
29 */
30
31 #if defined(HAVE_IMMEDIATE_STRUCTURES)
32 typedef struct {uint32_t v;} NTSTATUS;
33 #define NT_STATUS(x) ((NTSTATUS) { x })
34 #define NT_STATUS_V(x) ((x).v)
35 #else
36 typedef uint32_t NTSTATUS;
37 #define NT_STATUS(x) (x)
38 #define NT_STATUS_V(x) (x)
39 #endif
40
41 #if defined(HAVE_IMMEDIATE_STRUCTURES)
42 typedef struct {uint32_t v;} WERROR;
43 #define W_ERROR(x) ((WERROR) { x })
44 #define W_ERROR_V(x) ((x).v)
45 #else
46 typedef uint32_t WERROR;
47 #define W_ERROR(x) (x)
48 #define W_ERROR_V(x) (x)
49 #endif
50
51 #define NT_STATUS_IS_OK(x) (NT_STATUS_V(x) == 0)
52 #define NT_STATUS_IS_ERR(x) ((NT_STATUS_V(x) & 0xc0000000) == 0xc0000000)
53 /* checking for DOS error mapping here is ugly, but unfortunately the
54    alternative is a very intrusive rewrite of the torture code */
55 #define NT_STATUS_EQUAL(x,y) (NT_STATUS_IS_DOS(x)||NT_STATUS_IS_DOS(y)?ntstatus_dos_equal(x,y):NT_STATUS_V(x) == NT_STATUS_V(y))
56
57 #define NT_STATUS_HAVE_NO_MEMORY(x) do { \
58         if (!(x)) {\
59                 return NT_STATUS_NO_MEMORY;\
60         }\
61 } while (0)
62
63 #define NT_STATUS_IS_OK_RETURN(x) do { \
64         if (NT_STATUS_IS_OK(x)) {\
65                 return x;\
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 NT_STATUS_IS_ERR_RETURN(x) do { \
76         if (NT_STATUS_IS_ERR(x)) {\
77                 return x;\
78         }\
79 } while (0)
80
81 #define NT_STATUS_NOT_ERR_RETURN(x) do { \
82         if (!NT_STATUS_IS_ERR(x)) {\
83                 return x;\
84         }\
85 } while (0)
86
87 #define W_ERROR_IS_OK(x) (W_ERROR_V(x) == 0)
88 #define W_ERROR_EQUAL(x,y) (W_ERROR_V(x) == W_ERROR_V(y))
89
90 #define W_ERROR_HAVE_NO_MEMORY(x) do { \
91         if (!(x)) {\
92                 return WERR_NOMEM;\
93         }\
94 } while (0)
95
96 #define W_ERROR_IS_OK_RETURN(x) do { \
97         if (W_ERROR_IS_OK(x)) {\
98                 return x;\
99         }\
100 } while (0)
101
102 #define W_ERROR_NOT_OK_RETURN(x) do { \
103         if (!W_ERROR_IS_OK(x)) {\
104                 return x;\
105         }\
106 } while (0)
107
108 /* this defines special NTSTATUS codes to represent DOS errors.  I
109    have chosen this macro to produce status codes in the invalid
110    NTSTATUS range */
111 #define NT_STATUS_DOS(class, code) NT_STATUS(0xF1000000 | ((class)<<16) | code)
112 #define NT_STATUS_IS_DOS(status) ((NT_STATUS_V(status) & 0xFF000000) == 0xF1000000)
113 #define NT_STATUS_DOS_CLASS(status) ((NT_STATUS_V(status) >> 16) & 0xFF)
114 #define NT_STATUS_DOS_CODE(status) (NT_STATUS_V(status) & 0xFFFF)
115
116 /* define ldap error codes as NTSTATUS codes */
117 #define NT_STATUS_LDAP(code) NT_STATUS(0xF2000000 | code)
118 #define NT_STATUS_IS_LDAP(status) ((NT_STATUS_V(status) & 0xFF000000) == 0xF2000000)
119 #define NT_STATUS_LDAP_CODE(status) (NT_STATUS_V(status) & ~0xFF000000)
120
121 #endif