80025d71f07d0f1022e4a1bf5b27095de3a2183c
[abartlet/samba.git/.git] / librpc / ndr / ndr.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    libndr interface
5
6    Copyright (C) Andrew Tridgell 2003
7    
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 3 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23   this provides the core routines for NDR parsing functions
24
25   see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
26   of NDR encoding rules
27 */
28
29 #include "includes.h"
30 #include "librpc/ndr/libndr.h"
31 #include "../lib/util/dlinklist.h"
32 #if _SAMBA_BUILD_ == 4
33 #include "param/param.h"
34 #endif
35
36 #define NDR_BASE_MARSHALL_SIZE 1024
37
38 /* this guid indicates NDR encoding in a protocol tower */
39 const struct ndr_syntax_id ndr_transfer_syntax = {
40   { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
41   2
42 };
43
44 const struct ndr_syntax_id ndr64_transfer_syntax = {
45   { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
46   1
47 };
48
49 /*
50   work out the number of bytes needed to align on a n byte boundary
51 */
52 _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
53 {
54         if ((offset & (n-1)) == 0) return 0;
55         return n - (offset & (n-1));
56 }
57
58 /*
59   initialise a ndr parse structure from a data blob
60 */
61 _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
62 {
63         struct ndr_pull *ndr;
64
65         ndr = talloc_zero(mem_ctx, struct ndr_pull);
66         if (!ndr) return NULL;
67         ndr->current_mem_ctx = mem_ctx;
68
69         ndr->data = blob->data;
70         ndr->data_size = blob->length;
71         ndr->iconv_convenience = talloc_reference(ndr, iconv_convenience);
72
73         return ndr;
74 }
75
76 /*
77   advance by 'size' bytes
78 */
79 _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
80 {
81         ndr->offset += size;
82         if (ndr->offset > ndr->data_size) {
83                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
84                                       "ndr_pull_advance by %u failed",
85                                       size);
86         }
87         return NDR_ERR_SUCCESS;
88 }
89
90 /*
91   set the parse offset to 'ofs'
92 */
93 static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
94 {
95         ndr->offset = ofs;
96         if (ndr->offset > ndr->data_size) {
97                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
98                                       "ndr_pull_set_offset %u failed",
99                                       ofs);
100         }
101         return NDR_ERR_SUCCESS;
102 }
103
104 /* create a ndr_push structure, ready for some marshalling */
105 _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience)
106 {
107         struct ndr_push *ndr;
108
109         ndr = talloc_zero(mem_ctx, struct ndr_push);
110         if (!ndr) {
111                 return NULL;
112         }
113
114         ndr->flags = 0;
115         ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
116         ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
117         if (!ndr->data) {
118                 return NULL;
119         }
120         ndr->iconv_convenience = talloc_reference(ndr, iconv_convenience);
121
122         return ndr;
123 }
124
125 /* return a DATA_BLOB structure for the current ndr_push marshalled data */
126 _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
127 {
128         DATA_BLOB blob;
129         blob = data_blob_const(ndr->data, ndr->offset);
130         if (ndr->alloc_size > ndr->offset) {
131                 ndr->data[ndr->offset] = 0;
132         }
133         return blob;
134 }
135
136
137 /*
138   expand the available space in the buffer to ndr->offset + extra_size
139 */
140 _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
141 {
142         uint32_t size = extra_size + ndr->offset;
143
144         if (size < ndr->offset) {
145                 /* extra_size overflowed the offset */
146                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %u",
147                                       size);
148         }
149
150         if (ndr->alloc_size > size) {
151                 return NDR_ERR_SUCCESS;
152         }
153
154         ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
155         if (size+1 > ndr->alloc_size) {
156                 ndr->alloc_size = size+1;
157         }
158         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
159         if (!ndr->data) {
160                 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %u",
161                                       ndr->alloc_size);
162         }
163
164         return NDR_ERR_SUCCESS;
165 }
166
167 _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...) 
168 {
169         va_list ap;
170         char *s = NULL;
171         int i, ret;
172
173         va_start(ap, format);
174         ret = vasprintf(&s, format, ap);
175         va_end(ap);
176
177         if (ret == -1) {
178                 return;
179         }
180
181         for (i=0;i<ndr->depth;i++) {
182                 DEBUGADD(1,("    "));
183         }
184
185         DEBUGADD(1,("%s\n", s));
186         free(s);
187 }
188
189 _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
190 {
191         va_list ap;
192         int i;
193
194         for (i=0;i<ndr->depth;i++) {
195                 ndr->private_data = talloc_asprintf_append_buffer(
196                                         (char *)ndr->private_data, "    ");
197         }
198
199         va_start(ap, format);
200         ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data, 
201                                                     format, ap);
202         va_end(ap);
203         ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data, 
204                                                    "\n");
205 }
206
207 /*
208   a useful helper function for printing idl structures via DEBUG()
209 */
210 _PUBLIC_ void ndr_print_debug(ndr_print_fn_t fn, const char *name, void *ptr)
211 {
212         struct ndr_print *ndr;
213
214         DEBUG(1,(" "));
215
216         ndr = talloc_zero(NULL, struct ndr_print);
217         if (!ndr) return;
218         ndr->print = ndr_print_debug_helper;
219         ndr->depth = 1;
220         ndr->flags = 0;
221         fn(ndr, name, ptr);
222         talloc_free(ndr);
223 }
224
225 /*
226   a useful helper function for printing idl unions via DEBUG()
227 */
228 _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
229 {
230         struct ndr_print *ndr;
231
232         DEBUG(1,(" "));
233
234         ndr = talloc_zero(NULL, struct ndr_print);
235         if (!ndr) return;
236         ndr->print = ndr_print_debug_helper;
237         ndr->depth = 1;
238         ndr->flags = 0;
239         ndr_print_set_switch_value(ndr, ptr, level);
240         fn(ndr, name, ptr);
241         talloc_free(ndr);
242 }
243
244 /*
245   a useful helper function for printing idl function calls via DEBUG()
246 */
247 _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, int flags, void *ptr)
248 {
249         struct ndr_print *ndr;
250
251         DEBUG(1,(" "));
252
253         ndr = talloc_zero(NULL, struct ndr_print);
254         if (!ndr) return;
255         ndr->print = ndr_print_debug_helper;
256         ndr->depth = 1;
257         ndr->flags = 0;
258
259         /* this is a s4 hack until we build up the courage to pass
260          * this all the way down 
261          */
262 #if _SAMBA_BUILD_ == 4
263         ndr->iconv_convenience = smb_iconv_convenience_init(talloc_autofree_context(), "ASCII", "UTF-8", true);
264 #endif
265
266         fn(ndr, name, flags, ptr);
267         talloc_free(ndr);
268 }
269
270 /*
271   a useful helper function for printing idl structures to a string
272 */
273 _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
274 {
275         struct ndr_print *ndr;
276         char *ret = NULL;
277
278         ndr = talloc_zero(mem_ctx, struct ndr_print);
279         if (!ndr) return NULL;
280         ndr->private_data = talloc_strdup(ndr, "");
281         if (!ndr->private_data) {
282                 goto failed;
283         }
284         ndr->print = ndr_print_string_helper;
285         ndr->depth = 1;
286         ndr->flags = 0;
287
288         /* this is a s4 hack until we build up the courage to pass
289          * this all the way down 
290          */
291 #if _SAMBA_BUILD_ == 4
292         ndr->iconv_convenience = smb_iconv_convenience_init(talloc_autofree_context(), "ASCII", "UTF-8", true);
293 #endif
294
295         fn(ndr, name, ptr);
296         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
297 failed:
298         talloc_free(ndr);
299         return ret;
300 }
301
302 /*
303   a useful helper function for printing idl unions to a string
304 */
305 _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
306 {
307         struct ndr_print *ndr;
308         char *ret = NULL;
309
310         ndr = talloc_zero(mem_ctx, struct ndr_print);
311         if (!ndr) return NULL;
312         ndr->private_data = talloc_strdup(ndr, "");
313         if (!ndr->private_data) {
314                 goto failed;
315         }
316         ndr->print = ndr_print_string_helper;
317         ndr->depth = 1;
318         ndr->flags = 0;
319         ndr_print_set_switch_value(ndr, ptr, level);
320         fn(ndr, name, ptr);
321         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
322 failed:
323         talloc_free(ndr);
324         return ret;
325 }
326
327 /*
328   a useful helper function for printing idl function calls to a string
329 */
330 _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
331                                 ndr_print_function_t fn, const char *name, 
332                                 int flags, void *ptr)
333 {
334         struct ndr_print *ndr;
335         char *ret = NULL;
336
337         ndr = talloc_zero(mem_ctx, struct ndr_print);
338         if (!ndr) return NULL;
339         ndr->private_data = talloc_strdup(ndr, "");
340         if (!ndr->private_data) {
341                 goto failed;
342         }
343         ndr->print = ndr_print_string_helper;
344         ndr->depth = 1;
345         ndr->flags = 0;
346         fn(ndr, name, flags, ptr);
347         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
348 failed:
349         talloc_free(ndr);
350         return ret;
351 }
352
353 _PUBLIC_ void ndr_set_flags(uint32_t *pflags, uint32_t new_flags)
354 {
355         /* the big/little endian flags are inter-dependent */
356         if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
357                 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
358                 (*pflags) &= ~LIBNDR_FLAG_NDR64;
359         }
360         if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
361                 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
362                 (*pflags) &= ~LIBNDR_FLAG_NDR64;
363         }
364         if (new_flags & LIBNDR_FLAG_REMAINING) {
365                 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
366         }
367         if (new_flags & LIBNDR_ALIGN_FLAGS) {
368                 (*pflags) &= ~LIBNDR_FLAG_REMAINING;
369         }
370         (*pflags) |= new_flags;
371 }
372
373 /*
374   return and possibly log an NDR error
375 */
376 _PUBLIC_ enum ndr_err_code ndr_pull_error(struct ndr_pull *ndr,
377                                  enum ndr_err_code ndr_err,
378                                  const char *format, ...)
379 {
380         char *s=NULL;
381         va_list ap;
382         int ret;
383
384         va_start(ap, format);
385         ret = vasprintf(&s, format, ap);
386         va_end(ap);
387
388         if (ret == -1) {
389                 return NDR_ERR_ALLOC;
390         }
391
392         DEBUG(1,("ndr_pull_error(%u): %s\n", ndr_err, s));
393
394         free(s);
395
396         return ndr_err;
397 }
398
399 /*
400   return and possibly log an NDR error
401 */
402 _PUBLIC_ enum ndr_err_code ndr_push_error(struct ndr_push *ndr,
403                                  enum ndr_err_code ndr_err,
404                                  const char *format, ...) 
405 {
406         char *s=NULL;
407         va_list ap;
408         int ret;
409
410         va_start(ap, format);
411         ret = vasprintf(&s, format, ap);
412         va_end(ap);
413
414         if (ret == -1) {
415                 return NDR_ERR_ALLOC;
416         }
417
418         DEBUG(1,("ndr_push_error(%u): %s\n", ndr_err, s));
419
420         free(s);
421
422         return ndr_err;
423 }
424
425 /*
426   handle subcontext buffers, which in midl land are user-marshalled, but
427   we use magic in pidl to make them easier to cope with
428 */
429 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
430                                    struct ndr_pull **_subndr,
431                                    size_t header_size,
432                                    ssize_t size_is)
433 {
434         struct ndr_pull *subndr;
435         uint32_t r_content_size;
436         bool force_le = false;
437         bool force_be = false;
438
439         switch (header_size) {
440         case 0: {
441                 uint32_t content_size = ndr->data_size - ndr->offset;
442                 if (size_is >= 0) {
443                         content_size = size_is;
444                 }
445                 r_content_size = content_size;
446                 break;
447         }
448
449         case 2: {
450                 uint16_t content_size;
451                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
452                 if (size_is >= 0 && size_is != content_size) {
453                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
454                                                 (int)size_is, (int)content_size);
455                 }
456                 r_content_size = content_size;
457                 break;
458         }
459
460         case 4: {
461                 uint32_t content_size;
462                 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
463                 if (size_is >= 0 && size_is != content_size) {
464                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d", 
465                                                 (int)size_is, (int)content_size);
466                 }
467                 r_content_size = content_size;
468                 break;
469         }
470         case 0xFFFFFC01: {
471                 /*
472                  * Common Type Header for the Serialization Stream
473                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
474                  */
475                 uint8_t version;
476                 uint8_t drep;
477                 uint16_t hdrlen;
478                 uint32_t filler;
479                 uint32_t content_size;
480                 uint32_t reserved;
481
482                 /* version */
483                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
484
485                 if (version != 1) {
486                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
487                                               "Bad subcontext (PULL) Common Type Header version %d != 1",
488                                               (int)version);
489                 }
490
491                 /*
492                  * 0x10 little endian
493                  * 0x00 big endian
494                  */
495                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
496                 if (drep == 0x10) {
497                         force_le = true;
498                 } else if (drep == 0x00) {
499                         force_be = true;
500                 } else {
501                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
502                                               "Bad subcontext (PULL) Common Type Header invalid drep 0x%02X",
503                                               (unsigned int)drep);
504                 }
505
506                 /* length of the "Private Header for Constructed Type" */
507                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
508                 if (hdrlen != 8) {
509                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
510                                               "Bad subcontext (PULL) Common Type Header length %d != 8",
511                                               (int)hdrlen);
512                 }
513
514                 /* filler should be ignored */
515                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
516
517                 /*
518                  * Private Header for Constructed Type
519                  */
520                 /* length - will be updated latter */
521                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
522                 if (size_is >= 0 && size_is != content_size) {
523                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%d) mismatch content_size %d",
524                                               (int)size_is, (int)content_size);
525                 }
526                 /* the content size must be a multiple of 8 */
527                 if ((content_size % 8) != 0) {
528                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
529                                               "Bad subcontext (PULL) size_is(%d) not padded to 8 content_size %d",
530                                               (int)size_is, (int)content_size);
531                 }
532                 r_content_size = content_size;
533
534                 /* reserved */
535                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
536                 break;
537         }
538         default:
539                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %d", 
540                                       (int)header_size);
541         }
542
543         NDR_PULL_NEED_BYTES(ndr, r_content_size);
544
545         subndr = talloc_zero(ndr, struct ndr_pull);
546         NDR_ERR_HAVE_NO_MEMORY(subndr);
547         subndr->flags           = ndr->flags & ~LIBNDR_FLAG_NDR64;
548         subndr->current_mem_ctx = ndr->current_mem_ctx;
549
550         subndr->data = ndr->data + ndr->offset;
551         subndr->offset = 0;
552         subndr->data_size = r_content_size;
553         subndr->iconv_convenience = talloc_reference(subndr, ndr->iconv_convenience);
554
555         if (force_le) {
556                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
557         } else if (force_be) {
558                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
559         }
560
561         *_subndr = subndr;
562         return NDR_ERR_SUCCESS;
563 }
564
565 _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
566                                  struct ndr_pull *subndr,
567                                  size_t header_size,
568                                  ssize_t size_is)
569 {
570         uint32_t advance;
571         if (size_is >= 0) {
572                 advance = size_is;
573         } else if (header_size > 0) {
574                 advance = subndr->data_size;
575         } else {
576                 advance = subndr->offset;
577         }
578         NDR_CHECK(ndr_pull_advance(ndr, advance));
579         return NDR_ERR_SUCCESS;
580 }
581
582 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
583                                    struct ndr_push **_subndr,
584                                    size_t header_size,
585                                    ssize_t size_is)
586 {
587         struct ndr_push *subndr;
588
589         subndr = ndr_push_init_ctx(ndr, ndr->iconv_convenience);
590         NDR_ERR_HAVE_NO_MEMORY(subndr);
591         subndr->flags   = ndr->flags & ~LIBNDR_FLAG_NDR64;
592
593         if (size_is > 0) {
594                 NDR_CHECK(ndr_push_zero(subndr, size_is));
595                 subndr->offset = 0;
596         }
597
598         *_subndr = subndr;
599         return NDR_ERR_SUCCESS;
600 }
601
602 /*
603   push a subcontext header 
604 */
605 _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
606                                  struct ndr_push *subndr,
607                                  size_t header_size,
608                                  ssize_t size_is)
609 {
610         ssize_t padding_len;
611
612         if (size_is >= 0) {
613                 padding_len = size_is - subndr->offset;
614                 if (padding_len < 0) {
615                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %d is larger than size_is(%d)",
616                                               (int)subndr->offset, (int)size_is);
617                 }
618                 subndr->offset = size_is;
619         }
620
621         switch (header_size) {
622         case 0: 
623                 break;
624
625         case 2: 
626                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
627                 break;
628
629         case 4: 
630                 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
631                 break;
632
633         case 0xFFFFFC01:
634                 /*
635                  * Common Type Header for the Serialization Stream
636                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
637                  */
638                 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
639                 if (padding_len > 0) {
640                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
641                 }
642
643                 /* version */
644                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
645
646                 /*
647                  * 0x10 little endian
648                  * 0x00 big endian
649                  */
650                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
651
652                 /* length of the "Private Header for Constructed Type" */
653                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
654
655                 /* filler */
656                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
657
658                 /*
659                  * Private Header for Constructed Type
660                  */
661                 /* length - will be updated latter */
662                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
663
664                 /* reserved */
665                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
666                 break;
667
668         default:
669                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %d", 
670                                       (int)header_size);
671         }
672
673         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
674         return NDR_ERR_SUCCESS;
675 }
676
677 /*
678   store a token in the ndr context, for later retrieval
679 */
680 _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
681                          struct ndr_token_list **list, 
682                          const void *key, 
683                          uint32_t value)
684 {
685         struct ndr_token_list *tok;
686         tok = talloc(mem_ctx, struct ndr_token_list);
687         NDR_ERR_HAVE_NO_MEMORY(tok);
688         tok->key = key;
689         tok->value = value;
690         DLIST_ADD((*list), tok);
691         return NDR_ERR_SUCCESS;
692 }
693
694 /*
695   retrieve a token from a ndr context, using cmp_fn to match the tokens
696 */
697 _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list **list, const void *key, uint32_t *v,
698                                    comparison_fn_t _cmp_fn, bool _remove_tok)
699 {
700         struct ndr_token_list *tok;
701         for (tok=*list;tok;tok=tok->next) {
702                 if (_cmp_fn && _cmp_fn(tok->key,key)==0) goto found;
703                 else if (!_cmp_fn && tok->key == key) goto found;
704         }
705         return NDR_ERR_TOKEN;
706 found:
707         *v = tok->value;
708         if (_remove_tok) {
709                 DLIST_REMOVE((*list), tok);
710                 talloc_free(tok);
711         }
712         return NDR_ERR_SUCCESS;
713 }
714
715 /*
716   retrieve a token from a ndr context
717 */
718 _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list **list, const void *key, uint32_t *v)
719 {
720         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
721 }
722
723 /*
724   peek at but don't removed a token from a ndr context
725 */
726 _PUBLIC_ uint32_t ndr_token_peek(struct ndr_token_list **list, const void *key)
727 {
728         enum ndr_err_code status;
729         uint32_t v;
730
731         status = ndr_token_retrieve_cmp_fn(list, key, &v, NULL, false);
732         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
733                 return 0;
734         }
735
736         return v;
737 }
738
739 /*
740   pull an array size field and add it to the array_size_list token list
741 */
742 _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
743 {
744         uint32_t size;
745         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
746         return ndr_token_store(ndr, &ndr->array_size_list, p, size);
747 }
748
749 /*
750   get the stored array size field
751 */
752 _PUBLIC_ uint32_t ndr_get_array_size(struct ndr_pull *ndr, const void *p)
753 {
754         return ndr_token_peek(&ndr->array_size_list, p);
755 }
756
757 /*
758   check the stored array size field
759 */
760 _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, void *p, uint32_t size)
761 {
762         uint32_t stored;
763         stored = ndr_token_peek(&ndr->array_size_list, p);
764         if (stored != size) {
765                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
766                                       "Bad array size - got %u expected %u\n",
767                                       stored, size);
768         }
769         return NDR_ERR_SUCCESS;
770 }
771
772 /*
773   pull an array length field and add it to the array_length_list token list
774 */
775 _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
776 {
777         uint32_t length, offset;
778         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
779         if (offset != 0) {
780                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
781                                       "non-zero array offset %u\n", offset);
782         }
783         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
784         return ndr_token_store(ndr, &ndr->array_length_list, p, length);
785 }
786
787 /*
788   get the stored array length field
789 */
790 _PUBLIC_ uint32_t ndr_get_array_length(struct ndr_pull *ndr, const void *p)
791 {
792         return ndr_token_peek(&ndr->array_length_list, p);
793 }
794
795 /*
796   check the stored array length field
797 */
798 _PUBLIC_ enum ndr_err_code ndr_check_array_length(struct ndr_pull *ndr, void *p, uint32_t length)
799 {
800         uint32_t stored;
801         stored = ndr_token_peek(&ndr->array_length_list, p);
802         if (stored != length) {
803                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, 
804                                       "Bad array length - got %u expected %u\n",
805                                       stored, length);
806         }
807         return NDR_ERR_SUCCESS;
808 }
809
810 /*
811   store a switch value
812  */
813 _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
814 {
815         return ndr_token_store(ndr, &ndr->switch_list, p, val);
816 }
817
818 _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
819 {
820         return ndr_token_store(ndr, &ndr->switch_list, p, val);
821 }
822
823 _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
824 {
825         return ndr_token_store(ndr, &ndr->switch_list, p, val);
826 }
827
828 /*
829   retrieve a switch value
830  */
831 _PUBLIC_ uint32_t ndr_push_get_switch_value(struct ndr_push *ndr, const void *p)
832 {
833         return ndr_token_peek(&ndr->switch_list, p);
834 }
835
836 _PUBLIC_ uint32_t ndr_pull_get_switch_value(struct ndr_pull *ndr, const void *p)
837 {
838         return ndr_token_peek(&ndr->switch_list, p);
839 }
840
841 _PUBLIC_ uint32_t ndr_print_get_switch_value(struct ndr_print *ndr, const void *p)
842 {
843         return ndr_token_peek(&ndr->switch_list, p);
844 }
845
846 /*
847   pull a struct from a blob using NDR
848 */
849 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
850                               ndr_pull_flags_fn_t fn)
851 {
852         struct ndr_pull *ndr;
853         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
854         NDR_ERR_HAVE_NO_MEMORY(ndr);
855         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
856         talloc_free(ndr);
857         return NDR_ERR_SUCCESS;
858 }
859
860 /*
861   pull a struct from a blob using NDR - failing if all bytes are not consumed
862 */
863 _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
864                                                     struct smb_iconv_convenience *iconv_convenience,
865                                                     void *p, ndr_pull_flags_fn_t fn)
866 {
867         struct ndr_pull *ndr;
868         uint32_t highest_ofs;
869         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
870         NDR_ERR_HAVE_NO_MEMORY(ndr);
871         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
872         if (ndr->offset > ndr->relative_highest_offset) {
873                 highest_ofs = ndr->offset;
874         } else {
875                 highest_ofs = ndr->relative_highest_offset;
876         }
877         if (highest_ofs < ndr->data_size) {
878                 enum ndr_err_code ret;
879                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
880                                      "not all bytes consumed ofs[%u] size[%u]",
881                                      highest_ofs, ndr->data_size);
882                 talloc_free(ndr);
883                 return ret;
884         }
885         talloc_free(ndr);
886         return NDR_ERR_SUCCESS;
887 }
888
889 /*
890   pull a union from a blob using NDR, given the union discriminator
891 */
892 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
893                                                struct smb_iconv_convenience *iconv_convenience, void *p,
894                              uint32_t level, ndr_pull_flags_fn_t fn)
895 {
896         struct ndr_pull *ndr;
897         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
898         NDR_ERR_HAVE_NO_MEMORY(ndr);
899         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
900         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
901         talloc_free(ndr);
902         return NDR_ERR_SUCCESS;
903 }
904
905 /*
906   pull a union from a blob using NDR, given the union discriminator,
907   failing if all bytes are not consumed
908 */
909 _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, 
910                                                    struct smb_iconv_convenience *iconv_convenience, void *p,
911                              uint32_t level, ndr_pull_flags_fn_t fn)
912 {
913         struct ndr_pull *ndr;
914         uint32_t highest_ofs;
915         ndr = ndr_pull_init_blob(blob, mem_ctx, iconv_convenience);
916         NDR_ERR_HAVE_NO_MEMORY(ndr);
917         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
918         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
919         if (ndr->offset > ndr->relative_highest_offset) {
920                 highest_ofs = ndr->offset;
921         } else {
922                 highest_ofs = ndr->relative_highest_offset;
923         }
924         if (highest_ofs < ndr->data_size) {
925                 enum ndr_err_code ret;
926                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
927                                      "not all bytes consumed ofs[%u] size[%u]",
928                                      highest_ofs, ndr->data_size);
929                 talloc_free(ndr);
930                 return ret;
931         }
932         talloc_free(ndr);
933         return NDR_ERR_SUCCESS;
934 }
935
936 /*
937   push a struct to a blob using NDR
938 */
939 _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, const void *p, ndr_push_flags_fn_t fn)
940 {
941         struct ndr_push *ndr;
942         ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
943         NDR_ERR_HAVE_NO_MEMORY(ndr);
944
945         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
946
947         *blob = ndr_push_blob(ndr);
948         talloc_steal(mem_ctx, blob->data);
949         talloc_free(ndr);
950
951         return NDR_ERR_SUCCESS;
952 }
953
954 /*
955   push a union to a blob using NDR
956 */
957 _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, struct smb_iconv_convenience *iconv_convenience, void *p,
958                              uint32_t level, ndr_push_flags_fn_t fn)
959 {
960         struct ndr_push *ndr;
961         ndr = ndr_push_init_ctx(mem_ctx, iconv_convenience);
962         NDR_ERR_HAVE_NO_MEMORY(ndr);
963
964         NDR_CHECK(ndr_push_set_switch_value(ndr, p, level));
965         NDR_CHECK(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
966
967         *blob = ndr_push_blob(ndr);
968         talloc_steal(mem_ctx, blob->data);
969         talloc_free(ndr);
970
971         return NDR_ERR_SUCCESS;
972 }
973
974 /*
975   generic ndr_size_*() handler for structures
976 */
977 _PUBLIC_ size_t ndr_size_struct(const void *p, int flags, ndr_push_flags_fn_t push, struct smb_iconv_convenience *iconv_convenience)
978 {
979         struct ndr_push *ndr;
980         enum ndr_err_code status;
981         size_t ret;
982
983         /* avoid recursion */
984         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
985
986         ndr = ndr_push_init_ctx(NULL, iconv_convenience);
987         if (!ndr) return 0;
988         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
989         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
990         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
991                 talloc_free(ndr);
992                 return 0;
993         }
994         ret = ndr->offset;
995         talloc_free(ndr);
996         return ret;
997 }
998
999 /*
1000   generic ndr_size_*() handler for unions
1001 */
1002 _PUBLIC_ size_t ndr_size_union(const void *p, int flags, uint32_t level, ndr_push_flags_fn_t push, struct smb_iconv_convenience *iconv_convenience)
1003 {
1004         struct ndr_push *ndr;
1005         enum ndr_err_code status;
1006         size_t ret;
1007
1008         /* avoid recursion */
1009         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
1010
1011         ndr = ndr_push_init_ctx(NULL, iconv_convenience);
1012         if (!ndr) return 0;
1013         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
1014
1015         status = ndr_push_set_switch_value(ndr, p, level);
1016         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1017                 talloc_free(ndr);
1018                 return 0;
1019         }
1020         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
1021         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
1022                 talloc_free(ndr);
1023                 return 0;
1024         }
1025         ret = ndr->offset;
1026         talloc_free(ndr);
1027         return ret;
1028 }
1029
1030 /*
1031   get the current base for relative pointers for the push
1032 */
1033 _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
1034 {
1035         return ndr->relative_base_offset;
1036 }
1037
1038 /*
1039   restore the old base for relative pointers for the push
1040 */
1041 _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
1042 {
1043         ndr->relative_base_offset = offset;
1044 }
1045
1046 /*
1047   setup the current base for relative pointers for the push
1048   called in the NDR_SCALAR stage
1049 */
1050 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
1051 {
1052         ndr->relative_base_offset = offset;
1053         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1054 }
1055
1056 /*
1057   setup the current base for relative pointers for the push
1058   called in the NDR_BUFFERS stage
1059 */
1060 _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
1061 {
1062         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1063 }
1064
1065 /*
1066   push a relative object - stage1
1067   this is called during SCALARS processing
1068 */
1069 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
1070 {
1071         if (p == NULL) {
1072                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
1073                 return NDR_ERR_SUCCESS;
1074         }
1075         NDR_CHECK(ndr_push_align(ndr, 4));
1076         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1077         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
1078 }
1079
1080 /*
1081   push a short relative object - stage1
1082   this is called during SCALARS processing
1083 */
1084 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
1085 {
1086         if (p == NULL) {
1087                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
1088                 return NDR_ERR_SUCCESS;
1089         }
1090         NDR_CHECK(ndr_push_align(ndr, 2));
1091         NDR_CHECK(ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset));
1092         return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
1093 }
1094 /*
1095   push a relative object - stage2
1096   this is called during buffers processing
1097 */
1098 static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
1099 {
1100         uint32_t save_offset;
1101         uint32_t ptr_offset = 0xFFFFFFFF;
1102         if (p == NULL) {
1103                 return NDR_ERR_SUCCESS;
1104         }
1105         save_offset = ndr->offset;
1106         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1107         if (ptr_offset > ndr->offset) {
1108                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1109                                       "ndr_push_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1110                                       ptr_offset, ndr->offset);
1111         }
1112         ndr->offset = ptr_offset;
1113         if (save_offset < ndr->relative_base_offset) {
1114                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, 
1115                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1116                                       save_offset, ndr->relative_base_offset);
1117         }       
1118         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1119         ndr->offset = save_offset;
1120         return NDR_ERR_SUCCESS;
1121 }
1122 /*
1123   push a short relative object - stage2
1124   this is called during buffers processing
1125 */
1126 _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
1127 {
1128         uint32_t save_offset;
1129         uint32_t ptr_offset = 0xFFFF;
1130         if (p == NULL) {
1131                 return NDR_ERR_SUCCESS;
1132         }
1133         save_offset = ndr->offset;
1134         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
1135         if (ptr_offset > ndr->offset) {
1136                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1137                                       "ndr_push_short_relative_ptr2 ptr_offset(%u) > ndr->offset(%u)",
1138                                       ptr_offset, ndr->offset);
1139         }
1140         ndr->offset = ptr_offset;
1141         if (save_offset < ndr->relative_base_offset) {
1142                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
1143                                       "ndr_push_relative_ptr2 save_offset(%u) < ndr->relative_base_offset(%u)",
1144                                       save_offset, ndr->relative_base_offset);
1145         }
1146         NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
1147         ndr->offset = save_offset;
1148         return NDR_ERR_SUCCESS;
1149 }
1150
1151 /*
1152   push a relative object - stage2 start
1153   this is called during buffers processing
1154 */
1155 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
1156 {
1157         if (p == NULL) {
1158                 return NDR_ERR_SUCCESS;
1159         }
1160         return ndr_push_relative_ptr2(ndr, p);
1161 }
1162
1163 /*
1164   push a relative object - stage2 end
1165   this is called during buffers processing
1166 */
1167 _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
1168 {
1169         if (p == NULL) {
1170                 return NDR_ERR_SUCCESS;
1171         }
1172         return NDR_ERR_SUCCESS;
1173 }
1174
1175 /*
1176   get the current base for relative pointers for the pull
1177 */
1178 _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
1179 {
1180         return ndr->relative_base_offset;
1181 }
1182
1183 /*
1184   restore the old base for relative pointers for the pull
1185 */
1186 _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
1187 {
1188         ndr->relative_base_offset = offset;
1189 }
1190
1191 /*
1192   setup the current base for relative pointers for the pull
1193   called in the NDR_SCALAR stage
1194 */
1195 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
1196 {
1197         ndr->relative_base_offset = offset;
1198         return ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
1199 }
1200
1201 /*
1202   setup the current base for relative pointers for the pull
1203   called in the NDR_BUFFERS stage
1204 */
1205 _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
1206 {
1207         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
1208 }
1209
1210 /*
1211   pull a relative object - stage1
1212   called during SCALARS processing
1213 */
1214 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
1215 {
1216         rel_offset += ndr->relative_base_offset;
1217         if (rel_offset > ndr->data_size) {
1218                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE, 
1219                                       "ndr_pull_relative_ptr1 rel_offset(%u) > ndr->data_size(%u)",
1220                                       rel_offset, ndr->data_size);
1221         }
1222         return ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
1223 }
1224
1225 /*
1226   pull a relative object - stage2
1227   called during BUFFERS processing
1228 */
1229 _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
1230 {
1231         uint32_t rel_offset;
1232         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
1233         return ndr_pull_set_offset(ndr, rel_offset);
1234 }
1235
1236 const static struct {
1237         enum ndr_err_code err;
1238         const char *string;
1239 } ndr_err_code_strings[] = {
1240         { NDR_ERR_SUCCESS, "Success" },
1241         { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
1242         { NDR_ERR_BAD_SWITCH, "Bad Switch" },
1243         { NDR_ERR_OFFSET, "Offset Error" },
1244         { NDR_ERR_RELATIVE, "Relative Pointer Error" },
1245         { NDR_ERR_CHARCNV, "Character Conversion Error" },
1246         { NDR_ERR_LENGTH, "Length Error" },
1247         { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
1248         { NDR_ERR_COMPRESSION, "Compression Error" },
1249         { NDR_ERR_STRING, "String Error" },
1250         { NDR_ERR_VALIDATE, "Validate Error" },
1251         { NDR_ERR_BUFSIZE, "Buffer Size Error" },
1252         { NDR_ERR_ALLOC, "Allocation Error" },
1253         { NDR_ERR_RANGE, "Range Error" },
1254         { NDR_ERR_TOKEN, "Token Error" },
1255         { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
1256         { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
1257         { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
1258         { NDR_ERR_NDR64, "NDR64 assertion error" },
1259         { 0, NULL }
1260 };
1261
1262 _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
1263 {
1264         int i;
1265         for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
1266                 if (ndr_err_code_strings[i].err == ndr_err)
1267                         return ndr_err_code_strings[i].string;
1268         }
1269         return "Unknown error";
1270 }