ndr: allow ndr_print to print DATA_BLOB
authorAndrew Tridgell <tridge@samba.org>
Mon, 9 Aug 2010 06:37:52 +0000 (16:37 +1000)
committerAndrew Bartlett <abartlet@samba.org>
Sat, 14 Aug 2010 01:58:13 +0000 (11:58 +1000)
this prints DATA_BLOB structures using the ndr->print() calls

Signed-off-by: Andrew Bartlett <abartlet@samba.org>
librpc/ndr/libndr.h
librpc/ndr/ndr.c
librpc/ndr/ndr_basic.c

index d5091a6619b4d9054bba14804593d01d702dd5d3..9134efa17498b99760dfa9d8abc08e2a99b00a2a 100644 (file)
@@ -105,6 +105,7 @@ struct ndr_print {
        struct ndr_token_list *switch_list;
        void (*print)(struct ndr_print *, const char *, ...) PRINTF_ATTRIBUTE(2,3);
        void *private_data;
+       bool no_newline;
 };
 
 #define LIBNDR_FLAG_BIGENDIAN  (1<<0)
index 1600d51c1c0791b07be13d536fbd493192e22eb5..3f553a7cbc8a2800836406ad805cb30a6a4adac3 100644 (file)
@@ -176,6 +176,12 @@ _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format,
                return;
        }
 
+       if (ndr->no_newline) {
+               DEBUGADD(1,("%s", s));
+               free(s);
+               return;
+       }
+
        for (i=0;i<ndr->depth;i++) {
                DEBUGADD(1,("    "));
        }
@@ -189,17 +195,21 @@ _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format,
        va_list ap;
        int i;
 
-       for (i=0;i<ndr->depth;i++) {
-               ndr->private_data = talloc_asprintf_append_buffer(
-                                       (char *)ndr->private_data, "    ");
+       if (!ndr->no_newline) {
+               for (i=0;i<ndr->depth;i++) {
+                       ndr->private_data = talloc_asprintf_append_buffer(
+                               (char *)ndr->private_data, "    ");
+               }
        }
 
        va_start(ap, format);
        ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data, 
                                                    format, ap);
        va_end(ap);
-       ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data, 
-                                                  "\n");
+       if (!ndr->no_newline) {
+               ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
+                                                                 "\n");
+       }
 }
 
 /*
index d0d58b088448b6b83a3ae73d0bab55dbb7ce7111..0becf38f7ba14a3bacb70131b9a94e1efe539c71 100644 (file)
@@ -1021,11 +1021,58 @@ _PUBLIC_ void ndr_print_array_uint8(struct ndr_print *ndr, const char *name,
        ndr->depth--;   
 }
 
+static void ndr_print_asc(struct ndr_print *ndr, const uint8_t *buf, int len)
+{
+       int i;
+       for (i=0;i<len;i++)
+               ndr->print(ndr, "%c", isprint(buf[i])?buf[i]:'.');
+}
+
+/*
+  ndr_print version of dump_data()
+ */
+static void ndr_dump_data(struct ndr_print *ndr, const uint8_t *buf, int len)
+{
+       int i=0;
+
+       ndr->no_newline = true;
+
+       for (i=0;i<len;) {
+               if (i%16 == 0 && i<len) {
+                       ndr->print(ndr, "[%04X] ",i);
+               }
+
+               ndr->print(ndr, "%02X ",(int)buf[i]);
+               i++;
+               if (i%8 == 0) ndr->print(ndr,"  ");
+               if (i%16 == 0) {
+                       ndr_print_asc(ndr,&buf[i-16],8); ndr->print(ndr," ");
+                       ndr_print_asc(ndr,&buf[i-8],8); ndr->print(ndr, "\n");
+               }
+       }
+
+       if (i%16) {
+               int n;
+               n = 16 - (i%16);
+               ndr->print(ndr, " ");
+               if (n>8) ndr->print(ndr," ");
+               while (n--) ndr->print(ndr,"   ");
+               n = MIN(8,i%16);
+               ndr_print_asc(ndr,&buf[i-(i%16)],n); ndr->print(ndr, " ");
+               n = (i%16) - n;
+               if (n>0) ndr_print_asc(ndr,&buf[i-n],n);
+               ndr->print(ndr,"\n");
+       }
+
+       ndr->no_newline = false;
+}
+
+
 _PUBLIC_ void ndr_print_DATA_BLOB(struct ndr_print *ndr, const char *name, DATA_BLOB r)
 {
        ndr->print(ndr, "%-25s: DATA_BLOB length=%u", name, (unsigned)r.length);
        if (r.length) {
-               dump_data(10, r.data, r.length);
+               ndr_dump_data(ndr, r.data, r.length);
        }
 }