use nstring and [relative] to support levels 1 and 2 of EnumPrinters
[bbaumbach/samba-autobuild/.git] / source4 / librpc / ndr / libndr.h
1 /* 
2    Unix SMB/CIFS implementation.
3    rpc interface definitions
4    Copyright (C) Andrew Tridgell 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 /*
22   this provides definitions for the libcli/rpc/ MSRPC library
23 */
24
25
26 /* this is the base structure passed to routines that 
27    parse MSRPC formatted data 
28
29    note that in Samba4 we use separate routines and structures for
30    MSRPC marshalling and unmarshalling. Also note that these routines
31    are being kept deliberately very simple, and are not tied to a
32    particular transport
33 */
34 struct ndr_pull {
35         uint32 flags; /* LIBNDR_FLAG_* */
36         char *data;
37         uint32 data_size;
38         uint32 offset;
39         TALLOC_CTX *mem_ctx;
40 };
41
42 struct ndr_pull_save {
43         uint32 data_size;
44         uint32 offset;
45 };
46
47
48 /* structure passed to functions that generate NDR formatted data */
49 struct ndr_push {
50         uint32 flags; /* LIBNDR_FLAG_* */
51         char *data;
52         uint32 alloc_size;
53         uint32 offset;
54         TALLOC_CTX *mem_ctx;
55
56         /* this list is used by the [relative] code to find the offsets */
57         struct ndr_push_save *relative_list;
58 };
59
60 struct ndr_push_save {
61         uint32 offset;
62         struct ndr_push_save *next;
63 };
64
65
66 /* structure passed to functions that print IDL structures */
67 struct ndr_print {
68         uint32 flags; /* LIBNDR_FLAG_* */
69         TALLOC_CTX *mem_ctx;
70         uint32 depth;
71         void (*print)(struct ndr_print *, const char *, ...);
72         void *private;
73 };
74
75 #define LIBNDR_FLAG_BIGENDIAN 1
76
77
78 /* useful macro for debugging */
79 #define NDR_PRINT_DEBUG(type, p) ndr_print_debug((ndr_print_fn_t)ndr_print_ ##type, #p, p)
80 #define NDR_PRINT_UNION_DEBUG(type, level, p) ndr_print_union_debug((ndr_print_union_fn_t)ndr_print_ ##type, #p, level, p)
81
82
83 enum ndr_err_code {
84         NDR_ERR_CONFORMANT_SIZE,
85         NDR_ERR_ARRAY_SIZE,
86         NDR_ERR_BAD_SWITCH,
87         NDR_ERR_OFFSET,
88         NDR_ERR_RELATIVE,
89         NDR_ERR_CHARCNV,
90         NDR_ERR_LENGTH
91 };
92
93 /*
94   flags passed to control parse flow
95 */
96 #define NDR_SCALARS 1
97 #define NDR_BUFFERS 2
98
99 #define NDR_PULL_NEED_BYTES(ndr, n) do { \
100         if ((n) > ndr->data_size || ndr->offset + (n) > ndr->data_size) { \
101                 return NT_STATUS_BUFFER_TOO_SMALL; \
102         } \
103 } while(0)
104
105 #define NDR_PULL_ALIGN(ndr, n) do { \
106         ndr->offset = (ndr->offset + (n-1)) & ~(n-1); \
107         if (ndr->offset >= ndr->data_size) { \
108                 return NT_STATUS_BUFFER_TOO_SMALL; \
109         } \
110 } while(0)
111
112 #define NDR_PUSH_NEED_BYTES(ndr, n) NDR_CHECK(ndr_push_expand(ndr, ndr->offset+(n)))
113
114 #define NDR_PUSH_ALIGN(ndr, n) do { \
115         uint32 _pad = (ndr->offset & (n-1)); \
116         while (_pad--) NDR_CHECK(ndr_push_uint8(ndr, 0)); \
117 } while(0)
118
119
120 /* these are used to make the error checking on each element in libndr
121    less tedious, hopefully making the code more readable */
122 #define NDR_CHECK(call) do { NTSTATUS _status; \
123                              _status = call; \
124                              if (!NT_STATUS_IS_OK(_status)) \
125                                 return _status; \
126                         } while (0)
127
128
129 #define NDR_ALLOC_SIZE(ndr, s, size) do { \
130                                (s) = talloc(ndr->mem_ctx, size); \
131                                if (!(s)) return NT_STATUS_NO_MEMORY; \
132                            } while (0)
133
134 #define NDR_ALLOC(ndr, s) NDR_ALLOC_SIZE(ndr, s, sizeof(*(s)))
135
136
137 #define NDR_ALLOC_N_SIZE(ndr, s, n, elsize) do { \
138                                 if ((n) == 0) { \
139                                         (s) = NULL; \
140                                 } else { \
141                                         (s) = talloc(ndr->mem_ctx, (n) * elsize); \
142                                         if (!(s)) return NT_STATUS_NO_MEMORY; \
143                                 } \
144                            } while (0)
145
146 #define NDR_ALLOC_N(ndr, s, n) NDR_ALLOC_N_SIZE(ndr, s, n, sizeof(*(s)))
147
148 /* these are used when generic fn pointers are needed for ndr push/pull fns */
149 typedef NTSTATUS (*ndr_push_fn_t)(struct ndr_push *, void *);
150 typedef NTSTATUS (*ndr_pull_fn_t)(struct ndr_pull *, void *);
151
152 typedef NTSTATUS (*ndr_push_flags_fn_t)(struct ndr_push *, int ndr_flags, void *);
153 typedef NTSTATUS (*ndr_push_const_fn_t)(struct ndr_push *, int ndr_flags, const void *);
154 typedef NTSTATUS (*ndr_pull_flags_fn_t)(struct ndr_pull *, int ndr_flags, void *);
155 typedef NTSTATUS (*ndr_push_union_fn_t)(struct ndr_push *, int ndr_flags, uint16, void *);
156 typedef NTSTATUS (*ndr_pull_union_fn_t)(struct ndr_pull *, int ndr_flags, uint16 *, void *);
157 typedef void (*ndr_print_fn_t)(struct ndr_print *, const char *, void *);
158 typedef void (*ndr_print_union_fn_t)(struct ndr_print *, const char *, uint16, void *);
159
160 /* now pull in the individual parsers */
161 #include "librpc/ndr/ndr_sec.h"
162 #include "librpc/ndr/ndr_misc.h"
163 #include "librpc/ndr/ndr_echo.h"
164 #include "librpc/ndr/ndr_lsa.h"
165 #include "librpc/ndr/ndr_dfs.h"
166 #include "librpc/ndr/ndr_spoolss.h"
167 #include "librpc/ndr/ndr_spoolss_buf.h"
168 #include "librpc/ndr/ndr_samr.h"
169