2 Unix SMB/CIFS implementation.
4 routines for marshalling/unmarshalling basic types
6 Copyright (C) Andrew Tridgell 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define NDR_PULL_NEED_BYTES(ndr, n) do { \
26 if ((n) > ndr->data_size || ndr->offset + (n) > ndr->data_size) { \
27 return NT_STATUS_BUFFER_TOO_SMALL; \
31 #define NDR_PULL_ALIGN(ndr, n) do { \
32 ndr->offset = (ndr->offset + (n-1)) & ~(n-1); \
33 if (ndr->offset >= ndr->data_size) { \
34 return NT_STATUS_BUFFER_TOO_SMALL; \
41 NTSTATUS ndr_pull_uint8(struct ndr_pull *ndr, uint8 *v)
43 NDR_PULL_NEED_BYTES(ndr, 1);
44 *v = CVAL(ndr->data, ndr->offset);
53 NTSTATUS ndr_pull_uint16(struct ndr_pull *ndr, uint16 *v)
55 NDR_PULL_ALIGN(ndr, 2);
56 NDR_PULL_NEED_BYTES(ndr, 2);
57 if (ndr->flags & LIBNDR_FLAG_BIGENDIAN) {
58 *v = RSVAL(ndr->data, ndr->offset);
60 *v = SVAL(ndr->data, ndr->offset);
70 NTSTATUS ndr_pull_uint32(struct ndr_pull *ndr, uint32 *v)
72 NDR_PULL_ALIGN(ndr, 4);
73 NDR_PULL_NEED_BYTES(ndr, 4);
74 if (ndr->flags & LIBNDR_FLAG_BIGENDIAN) {
75 *v = RIVAL(ndr->data, ndr->offset);
77 *v = IVAL(ndr->data, ndr->offset);
86 NTSTATUS ndr_pull_NTSTATUS(struct ndr_pull *ndr, NTSTATUS *status)
89 NDR_CHECK(ndr_pull_uint32(ndr, &v));
90 *status = NT_STATUS(v);
97 NTSTATUS ndr_pull_bytes(struct ndr_pull *ndr, char *data, uint32 n)
99 NDR_PULL_NEED_BYTES(ndr, n);
100 memcpy(data, ndr->data + ndr->offset, n);
106 pull an array of uint8
108 NTSTATUS ndr_pull_array_uint8(struct ndr_pull *ndr, char *data, uint32 n)
111 NDR_CHECK(ndr_pull_uint32(ndr, &len));
113 return NT_STATUS_INVALID_PARAMETER;
115 return ndr_pull_bytes(ndr, data, len);
122 NTSTATUS ndr_pull_guid(struct ndr_pull *ndr, GUID *guid)
125 NDR_PULL_NEED_BYTES(ndr, GUID_SIZE);
126 for (i=0;i<GUID_SIZE;i++) {
127 guid->info[i] = CVAL(ndr->data, ndr->offset + i);
134 #define NDR_PUSH_NEED_BYTES(ndr, n) NDR_CHECK(ndr_push_expand(ndr, ndr->offset+(n)))
136 #define NDR_PUSH_ALIGN(ndr, n) do { \
137 uint32 _pad = (ndr->offset & (n-1)); \
138 while (_pad--) NDR_CHECK(ndr_push_uint8(ndr, 0)); \
144 NTSTATUS ndr_push_uint8(struct ndr_push *ndr, uint8 v)
146 NDR_PUSH_NEED_BYTES(ndr, 1);
147 SCVAL(ndr->data, ndr->offset, v);
155 NTSTATUS ndr_push_uint16(struct ndr_push *ndr, uint16 v)
157 NDR_PUSH_ALIGN(ndr, 2);
158 NDR_PUSH_NEED_BYTES(ndr, 2);
159 SSVAL(ndr->data, ndr->offset, v);
167 NTSTATUS ndr_push_uint32(struct ndr_push *ndr, uint32 v)
169 NDR_PUSH_ALIGN(ndr, 4);
170 NDR_PUSH_NEED_BYTES(ndr, 4);
171 SIVAL(ndr->data, ndr->offset, v);
179 NTSTATUS ndr_push_align_uint32(struct ndr_push *ndr)
181 NDR_PUSH_ALIGN(ndr, 4);
188 NTSTATUS ndr_push_bytes(struct ndr_push *ndr, const char *data, uint32 n)
190 NDR_PUSH_NEED_BYTES(ndr, n);
191 memcpy(ndr->data + ndr->offset, data, n);
197 push an array of uint8
199 NTSTATUS ndr_push_array_uint8(struct ndr_push *ndr, const char *data, uint32 n)
201 NDR_CHECK(ndr_push_uint32(ndr, n));
202 return ndr_push_bytes(ndr, data, n);
206 save the current position
208 void ndr_push_save(struct ndr_push *ndr, struct ndr_push_save *save)
210 save->offset = ndr->offset;
216 void ndr_push_restore(struct ndr_push *ndr, struct ndr_push_save *save)
218 ndr->offset = save->offset;
222 this is used when a packet has a 4 byte length field. We remember the start position
223 and come back to it later to fill in the size
225 NTSTATUS ndr_push_length4_start(struct ndr_push *ndr, struct ndr_push_save *save)
227 NDR_PUSH_ALIGN(ndr, 4);
228 ndr_push_save(ndr, save);
229 return ndr_push_uint32(ndr, 0);
232 NTSTATUS ndr_push_length4_end(struct ndr_push *ndr, struct ndr_push_save *save)
234 struct ndr_push_save save2;
235 ndr_push_save(ndr, &save2);
236 ndr_push_restore(ndr, save);
237 NDR_CHECK(ndr_push_uint32(ndr, save2.offset - ndr->offset));
238 ndr_push_restore(ndr, &save2);
243 push a 1 if a pointer is non-NULL, otherwise 0
245 NTSTATUS ndr_push_ptr(struct ndr_push *ndr, const void *p)
247 return ndr_push_uint32(ndr, p?0xaabbccdd:0);
251 push a comformant, variable ucs2 string onto the wire from a C string
253 NTSTATUS ndr_push_unistr(struct ndr_push *ndr, const char *s)
257 len = push_ucs2_talloc(ndr->mem_ctx, (smb_ucs2_t **)&ws, s);
259 return NT_STATUS_INVALID_PARAMETER;
261 NDR_CHECK(ndr_push_uint32(ndr, len/2));
262 NDR_CHECK(ndr_push_uint32(ndr, 0));
263 NDR_CHECK(ndr_push_uint32(ndr, len/2));
264 NDR_CHECK(ndr_push_bytes(ndr, ws, len));
269 push a comformant, variable ucs2 string onto the wire from a C string
272 NTSTATUS ndr_push_unistr_noterm(struct ndr_push *ndr, const char *s)
276 len = push_ucs2_talloc(ndr->mem_ctx, (smb_ucs2_t **)&ws, s);
278 return NT_STATUS_INVALID_PARAMETER;
280 NDR_CHECK(ndr_push_uint32(ndr, len/2 - 1));
281 NDR_CHECK(ndr_push_uint32(ndr, 0));
282 NDR_CHECK(ndr_push_uint32(ndr, len/2 - 1));
283 NDR_CHECK(ndr_push_bytes(ndr, ws, len - 2));
288 pull a comformant, variable ucs2 string from the wire into a C string
290 NTSTATUS ndr_pull_unistr(struct ndr_pull *ndr, const char **s)
293 uint32 len1, ofs, len2;
295 NDR_CHECK(ndr_pull_uint32(ndr, &len1));
296 NDR_CHECK(ndr_pull_uint32(ndr, &ofs));
297 NDR_CHECK(ndr_pull_uint32(ndr, &len2));
299 return NT_STATUS_INVALID_PARAMETER;
301 NDR_ALLOC_N(ndr, ws, (len1+1)*2);
302 NDR_CHECK(ndr_pull_bytes(ndr, ws, len2*2));
303 SSVAL(ws, len1*2, 0);
304 SSVAL(ws, len2*2, 0);
305 pull_ucs2_talloc(ndr->mem_ctx, &as, (const smb_ucs2_t *)ws);
307 return NT_STATUS_INVALID_PARAMETER;
314 pull a comformant, variable ucs2 string from the wire into a C string
316 NTSTATUS ndr_pull_unistr_noterm(struct ndr_pull *ndr, const char **s)
318 return ndr_pull_unistr(ndr, s);
322 push a 4 byte offset pointer, remembering where we are so we can later fill
325 NTSTATUS ndr_push_offset(struct ndr_push *ndr, struct ndr_push_save *ofs)
327 NDR_PUSH_ALIGN(ndr, 4);
328 ndr_push_save(ndr, ofs);
329 return ndr_push_uint32(ndr, 0);
333 fill in the correct offset in a saved offset pointer
334 the offset is taken relative to 'save'
336 NTSTATUS ndr_push_offset_ptr(struct ndr_push *ndr,
337 struct ndr_push_save *ofs,
338 struct ndr_push_save *save)
340 struct ndr_push_save save2;
341 ndr_push_save(ndr, &save2);
342 ndr_push_restore(ndr, ofs);
343 NDR_CHECK(ndr_push_uint32(ndr, save2.offset - save->offset));
344 ndr_push_restore(ndr, &save2);
352 NTSTATUS ndr_push_guid(struct ndr_push *ndr, GUID *guid)
354 return ndr_push_bytes(ndr, guid->info, GUID_SIZE);
360 NTSTATUS ndr_push_NTTIME(struct ndr_push *ndr, NTTIME t)
362 NDR_CHECK(ndr_push_uint32(ndr, t.low));
363 NDR_CHECK(ndr_push_uint32(ndr, t.high));
370 NTSTATUS ndr_pull_NTTIME(struct ndr_pull *ndr, NTTIME *t)
372 NDR_CHECK(ndr_pull_uint32(ndr, &t->low));
373 NDR_CHECK(ndr_pull_uint32(ndr, &t->high));
378 void ndr_print_struct(struct ndr_print *ndr, const char *name, const char *type)
380 ndr->print(ndr, "%s: struct %s", name, type);
383 void ndr_print_uint8(struct ndr_print *ndr, const char *name, uint8 v)
385 ndr->print(ndr, "%-25s: 0x%02x (%u)", name, v, v);
388 void ndr_print_uint16(struct ndr_print *ndr, const char *name, uint16 v)
390 ndr->print(ndr, "%-25s: 0x%04x (%u)", name, v, v);
393 void ndr_print_uint32(struct ndr_print *ndr, const char *name, uint32 v)
395 ndr->print(ndr, "%-25s: 0x%08x (%u)", name, v, v);
398 void ndr_print_ptr(struct ndr_print *ndr, const char *name, const void *p)
401 ndr->print(ndr, "%-25s: *", name);
403 ndr->print(ndr, "%-25s: NULL", name);
407 void ndr_print_unistr_noterm(struct ndr_print *ndr, const char *name, const char *s)
409 ndr->print(ndr, "%-25s: '%s'", name, s);
412 void ndr_print_unistr(struct ndr_print *ndr, const char *name, const char *s)
414 ndr->print(ndr, "%-25s: '%s'", name, s);
417 void ndr_print_NTTIME(struct ndr_print *ndr, const char *name, NTTIME t)
419 ndr->print(ndr, "%-25s: %s", name, nt_time_string(ndr->mem_ctx, &t));
422 void ndr_print_union(struct ndr_print *ndr, const char *name, uint16 level, const char *type)
424 ndr->print(ndr, "%-25s: union %s(case %u)", name, type, level);
427 void ndr_print_bad_level(struct ndr_print *ndr, const char *name, uint16 level)
429 ndr->print(ndr, "UNKNOWN LEVEL %u", level);