]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
librpr/ndr: split out ndr_print_generic_string()
authorStefan Metzmacher <metze@samba.org>
Fri, 22 Nov 2024 13:10:56 +0000 (14:10 +0100)
committerStefan Metzmacher <metze@samba.org>
Thu, 5 Dec 2024 16:46:37 +0000 (16:46 +0000)
Signed-off-by: Stefan Metzmacher <metze@samba.org>
Reviewed-by: Andreas Schneider <asn@samba.org>
librpc/ndr/ndr.c

index ece7f56a1a8394bb5f59ebb1ff542132eede6c7b..f73462480836d0fb054ea37bd25b3433183b13fd 100644 (file)
@@ -523,13 +523,13 @@ _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn,
        TALLOC_FREE(ndr);
 }
 
-/*
-  a useful helper function for printing idl structures to a string
-*/
-_PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx,
-                                      ndr_print_fn_t fn,
-                                      const char *name,
-                                      const void *ptr)
+static char *ndr_print_generic_string(TALLOC_CTX *mem_ctx,
+                                     ndr_print_function_t inout_fn,
+                                     ndr_flags_type inout_flags,
+                                     ndr_print_fn_t single_fn,
+                                     const char *name,
+                                     const uint32_t *level,
+                                     const void *ptr)
 {
        struct ndr_print *ndr;
        char *ret = NULL;
@@ -543,14 +543,37 @@ _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx,
        ndr->print = ndr_print_string_helper;
        ndr->depth = 1;
        ndr->flags = 0;
-
-       fn(ndr, name, ptr);
+       if (level != NULL) {
+               ndr_print_set_switch_value(ndr, ptr, *level);
+       }
+       if (inout_fn != NULL) {
+               inout_fn(ndr, name, inout_flags, ptr);
+       } else {
+               single_fn(ndr, name, ptr);
+       }
        ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
 failed:
        TALLOC_FREE(ndr);
        return ret;
 }
 
+/*
+  a useful helper function for printing idl structures to a string
+*/
+_PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx,
+                                      ndr_print_fn_t fn,
+                                      const char *name,
+                                      const void *ptr)
+{
+       return ndr_print_generic_string(mem_ctx,
+                                       NULL, /* inout_fn */
+                                       0,    /* inout_flags */
+                                       fn,   /* single_fn */
+                                       name,
+                                       NULL, /* level */
+                                       ptr);
+}
+
 /*
   a useful helper function for printing idl unions to a string
 */
@@ -560,24 +583,13 @@ _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx,
                                      uint32_t level,
                                      const void *ptr)
 {
-       struct ndr_print *ndr;
-       char *ret = NULL;
-
-       ndr = talloc_zero(mem_ctx, struct ndr_print);
-       if (!ndr) return NULL;
-       ndr->private_data = talloc_strdup(ndr, "");
-       if (!ndr->private_data) {
-               goto failed;
-       }
-       ndr->print = ndr_print_string_helper;
-       ndr->depth = 1;
-       ndr->flags = 0;
-       ndr_print_set_switch_value(ndr, ptr, level);
-       fn(ndr, name, ptr);
-       ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
-failed:
-       TALLOC_FREE(ndr);
-       return ret;
+       return ndr_print_generic_string(mem_ctx,
+                                       NULL, /* inout_fn */
+                                       0,    /* inout_flags */
+                                       fn,   /* single_fn */
+                                       name,
+                                       &level,
+                                       ptr);
 }
 
 /*
@@ -589,23 +601,13 @@ _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
                                         ndr_flags_type flags,
                                         const void *ptr)
 {
-       struct ndr_print *ndr;
-       char *ret = NULL;
-
-       ndr = talloc_zero(mem_ctx, struct ndr_print);
-       if (!ndr) return NULL;
-       ndr->private_data = talloc_strdup(ndr, "");
-       if (!ndr->private_data) {
-               goto failed;
-       }
-       ndr->print = ndr_print_string_helper;
-       ndr->depth = 1;
-       ndr->flags = 0;
-       fn(ndr, name, flags, ptr);
-       ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
-failed:
-       TALLOC_FREE(ndr);
-       return ret;
+       return ndr_print_generic_string(mem_ctx,
+                                       fn,    /* inout_fn */
+                                       flags, /* inout_flags */
+                                       NULL,  /* single_fn */
+                                       name,
+                                       NULL,  /* level */
+                                       ptr);
 }
 
 _PUBLIC_ void ndr_set_flags(libndr_flags *pflags, libndr_flags new_flags)