r6690: added ndr_pull_struct_blob_all(), which is like ndr_pull_struct_blob() but...
[samba.git] / source / librpc / ndr / ndr.c
index 3a7ad7f29e3b344d55f6a9a8ae6e2dae8ff87756..79af5967c754e7ac56186b6e04dfbc82e6f18234 100644 (file)
@@ -315,16 +315,12 @@ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
 {
        struct ndr_print *ndr;
 
-       ndr = talloc(NULL, struct ndr_print);
+       ndr = talloc_zero(NULL, struct ndr_print);
        if (!ndr) return;
        ndr->print = ndr_print_debug_helper;
        ndr->depth = 1;
        ndr->flags = 0;
-       ndr->switch_list = talloc(ndr, struct ndr_token_list);
-       if (!ndr->switch_list)
-               goto fail;
        fn(ndr, name, ptr);
-fail:
        talloc_free(ndr);
 }
 
@@ -335,7 +331,7 @@ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int fla
 {
        struct ndr_print *ndr;
 
-       ndr = talloc(NULL, struct ndr_print);
+       ndr = talloc_zero(NULL, struct ndr_print);
        if (!ndr) return;
        ndr->print = ndr_print_debug_helper;
        ndr->depth = 1;
@@ -664,6 +660,11 @@ NTSTATUS ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t
        return ndr_token_store(ndr, &ndr->switch_list, p, val);
 }
 
+NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
+{
+       return ndr_token_store(ndr, &ndr->switch_list, p, val);
+}
+
 /*
   retrieve a switch value
  */
@@ -677,14 +678,6 @@ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
        return ndr_token_peek(&ndr->switch_list, p);
 }
 
-NTSTATUS ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
-{
-       return ndr_token_store(ndr, &ndr->switch_list, p, val);
-}
-
-/*
-  retrieve a switch value
- */
 uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
 {
        return ndr_token_peek(&ndr->switch_list, p);
@@ -766,6 +759,27 @@ NTSTATUS ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *
        return fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
 }
 
+/*
+  pull a struct from a blob using NDR - failing if all bytes are not consumed
+*/
+NTSTATUS ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
+                                 ndr_pull_flags_fn_t fn)
+{
+       struct ndr_pull *ndr;
+       NTSTATUS status;
+
+       ndr = ndr_pull_init_blob(blob, mem_ctx);
+       if (!ndr) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       status = fn(ndr, NDR_SCALARS|NDR_BUFFERS, p);
+       if (!NT_STATUS_IS_OK(status)) return status;
+       if (ndr->offset != ndr->data_size) {
+               return NT_STATUS_BUFFER_TOO_SMALL;
+       }
+       return status;
+}
+
 /*
   push a struct to a blob using NDR
 */