LCOV - code coverage report
Current view: top level - librpc/ndr - ndr.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 681 914 74.5 %
Date: 2023-11-21 12:31:41 Functions: 69 77 89.6 %

          Line data    Source code
       1             : /*
       2             :    Unix SMB/CIFS implementation.
       3             : 
       4             :    libndr interface
       5             : 
       6             :    Copyright (C) Andrew Tridgell 2003
       7             :    Copyright (C) Jelmer Vernooij 2005-2008
       8             : 
       9             :    This program is free software; you can redistribute it and/or modify
      10             :    it under the terms of the GNU General Public License as published by
      11             :    the Free Software Foundation; either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    This program is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU General Public License
      20             :    along with this program.  If not, see <http://www.gnu.org/licenses/>.
      21             : */
      22             : 
      23             : /*
      24             :   this provides the core routines for NDR parsing functions
      25             : 
      26             :   see http://www.opengroup.org/onlinepubs/9629399/chap14.htm for details
      27             :   of NDR encoding rules
      28             : */
      29             : 
      30             : #include "includes.h"
      31             : #include "librpc/ndr/libndr.h"
      32             : #include "librpc/ndr/ndr_private.h"
      33             : #include "../lib/util/dlinklist.h"
      34             : 
      35             : #undef DBGC_CLASS
      36             : #define DBGC_CLASS DBGC_RPC_PARSE
      37             : 
      38             : #define NDR_BASE_MARSHALL_SIZE 1024
      39             : 
      40             : /*
      41             :  * This value is arbitrary, but designed to reduce the memory a client
      42             :  * can allocate and the work the client can force in processing a
      43             :  * malicious packet.
      44             :  *
      45             :  * In an ideal world this would be controlled by range() restrictions
      46             :  * on array sizes and careful IDL construction to avoid arbitrary
      47             :  * linked lists, but this is a backstop for now.
      48             :  */
      49             : #define NDR_TOKEN_MAX_LIST_SIZE 65535
      50             : 
      51           1 : size_t ndr_token_max_list_size(void) {
      52           1 :         return NDR_TOKEN_MAX_LIST_SIZE;
      53             : };
      54             : 
      55             : /* this guid indicates NDR encoding in a protocol tower */
      56             : const struct ndr_syntax_id ndr_transfer_syntax_ndr = {
      57             :   { 0x8a885d04, 0x1ceb, 0x11c9, {0x9f, 0xe8}, {0x08,0x00,0x2b,0x10,0x48,0x60} },
      58             :   2
      59             : };
      60             : 
      61             : const struct ndr_syntax_id ndr_transfer_syntax_ndr64 = {
      62             :   { 0x71710533, 0xbeba, 0x4937, {0x83, 0x19}, {0xb5,0xdb,0xef,0x9c,0xcc,0x36} },
      63             :   1
      64             : };
      65             : 
      66             : const struct ndr_syntax_id ndr_syntax_id_null = {
      67             :   { 0, 0, 0, { 0, 0 }, { 0, 0, 0, 0, 0, 0 } },
      68             :   0
      69             : };
      70             : 
      71             : /*
      72             :   work out the number of bytes needed to align on a n byte boundary
      73             : */
      74    19985029 : _PUBLIC_ size_t ndr_align_size(uint32_t offset, size_t n)
      75             : {
      76    19985029 :         if ((offset & (n-1)) == 0) return 0;
      77      533584 :         return n - (offset & (n-1));
      78             : }
      79             : 
      80             : /*
      81             :   initialise a ndr parse structure from a data blob
      82             : */
      83   109180220 : _PUBLIC_ struct ndr_pull *ndr_pull_init_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx)
      84             : {
      85     1096959 :         struct ndr_pull *ndr;
      86             : 
      87   109180220 :         ndr = talloc_zero(mem_ctx, struct ndr_pull);
      88   109180220 :         if (!ndr) return NULL;
      89   109180220 :         ndr->current_mem_ctx = mem_ctx;
      90             : 
      91   109180220 :         ndr->data = blob->data;
      92   109180220 :         ndr->data_size = blob->length;
      93             : 
      94   109180220 :         return ndr;
      95             : }
      96             : 
      97           0 : _PUBLIC_ enum ndr_err_code ndr_pull_append(struct ndr_pull *ndr, DATA_BLOB *blob)
      98             : {
      99           0 :         enum ndr_err_code ndr_err;
     100           0 :         DATA_BLOB b;
     101           0 :         uint32_t append = 0;
     102           0 :         bool ok;
     103             : 
     104           0 :         if (blob->length == 0) {
     105           0 :                 return NDR_ERR_SUCCESS;
     106             :         }
     107             : 
     108           0 :         ndr_err = ndr_token_retrieve(&ndr->array_size_list, ndr, &append);
     109           0 :         if (ndr_err == NDR_ERR_TOKEN) {
     110           0 :                 append = 0;
     111           0 :                 ndr_err = NDR_ERR_SUCCESS;
     112             :         }
     113           0 :         NDR_CHECK(ndr_err);
     114             : 
     115           0 :         if (ndr->data_size == 0) {
     116           0 :                 ndr->data = NULL;
     117           0 :                 append = UINT32_MAX;
     118             :         }
     119             : 
     120           0 :         if (append == UINT32_MAX) {
     121             :                 /*
     122             :                  * append == UINT32_MAX means that
     123             :                  * ndr->data is either NULL or a valid
     124             :                  * talloc child of ndr, which means
     125             :                  * we can use data_blob_append() without
     126             :                  * data_blob_talloc() of the existing callers data
     127             :                  */
     128           0 :                 b = data_blob_const(ndr->data, ndr->data_size);
     129             :         } else {
     130           0 :                 b = data_blob_talloc(ndr, ndr->data, ndr->data_size);
     131           0 :                 if (b.data == NULL) {
     132           0 :                         return ndr_pull_error(ndr, NDR_ERR_ALLOC, "%s", __location__);
     133             :                 }
     134             :         }
     135             : 
     136           0 :         ok = data_blob_append(ndr, &b, blob->data, blob->length);
     137           0 :         if (!ok) {
     138           0 :                 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "%s", __location__);
     139             :         }
     140             : 
     141           0 :         ndr->data = b.data;
     142           0 :         ndr->data_size = b.length;
     143             : 
     144           0 :         return ndr_token_store(ndr, &ndr->array_size_list, ndr, UINT32_MAX);
     145             : }
     146             : 
     147           0 : _PUBLIC_ enum ndr_err_code ndr_pull_pop(struct ndr_pull *ndr)
     148             : {
     149           0 :         uint32_t skip = 0;
     150           0 :         uint32_t append = 0;
     151           0 :         enum ndr_err_code ndr_err;
     152             : 
     153           0 :         if (ndr->relative_base_offset != 0) {
     154           0 :                 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
     155             :                                       "%s", __location__);
     156             :         }
     157           0 :         if (ndr->relative_highest_offset != 0) {
     158           0 :                 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
     159             :                                       "%s", __location__);
     160             :         }
     161           0 :         if (ndr->relative_list.count != 0) {
     162           0 :                 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
     163             :                                       "%s", __location__);
     164             :         }
     165           0 :         if (ndr->relative_base_list.count != 0) {
     166           0 :                 return ndr_pull_error(ndr, NDR_ERR_RELATIVE,
     167             :                                       "%s", __location__);
     168             :         }
     169             : 
     170             :         /*
     171             :          * we need to keep up to 7 bytes
     172             :          * in order to get the alignment right.
     173             :          */
     174           0 :         skip = ndr->offset & 0xFFFFFFF8;
     175             : 
     176           0 :         if (skip == 0) {
     177           0 :                 return NDR_ERR_SUCCESS;
     178             :         }
     179             : 
     180           0 :         ndr->offset -= skip;
     181           0 :         ndr->data_size -= skip;
     182             : 
     183           0 :         ndr_err = ndr_token_peek(&ndr->array_size_list, ndr, &append);
     184           0 :         if (ndr_err == NDR_ERR_TOKEN) {
     185             :                 /*
     186             :                  * here we assume, that ndr->data is not a
     187             :                  * talloc child of ndr.
     188             :                  */
     189           0 :                 ndr->data += skip;
     190           0 :                 return NDR_ERR_SUCCESS;
     191             :         }
     192             : 
     193           0 :         memmove(ndr->data, ndr->data + skip, ndr->data_size);
     194             : 
     195           0 :         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->data_size);
     196           0 :         if (ndr->data_size != 0 && ndr->data == NULL) {
     197           0 :                 return ndr_pull_error(ndr, NDR_ERR_ALLOC, "%s", __location__);
     198             :         }
     199             : 
     200           0 :         return NDR_ERR_SUCCESS;
     201             : }
     202             : 
     203             : /*
     204             :   advance by 'size' bytes
     205             : */
     206   336106707 : _PUBLIC_ enum ndr_err_code ndr_pull_advance(struct ndr_pull *ndr, uint32_t size)
     207             : {
     208   336106707 :         NDR_PULL_NEED_BYTES(ndr, size);
     209   336106706 :         ndr->offset += size;
     210   336106706 :         return NDR_ERR_SUCCESS;
     211             : }
     212             : 
     213             : /*
     214             :   set the parse offset to 'ofs'
     215             : */
     216    46052751 : static enum ndr_err_code ndr_pull_set_offset(struct ndr_pull *ndr, uint32_t ofs)
     217             : {
     218    46052751 :         ndr->offset = ofs;
     219    46052751 :         if (ndr->offset > ndr->data_size) {
     220           0 :                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
     221             :                                       "ndr_pull_set_offset %"PRIu32" failed",
     222             :                                       ofs);
     223             :         }
     224    44094303 :         return NDR_ERR_SUCCESS;
     225             : }
     226             : 
     227             : /* create a ndr_push structure, ready for some marshalling */
     228   147129244 : _PUBLIC_ struct ndr_push *ndr_push_init_ctx(TALLOC_CTX *mem_ctx)
     229             : {
     230     2107520 :         struct ndr_push *ndr;
     231             : 
     232   147129244 :         ndr = talloc_zero(mem_ctx, struct ndr_push);
     233   147129244 :         if (!ndr) {
     234           0 :                 return NULL;
     235             :         }
     236             : 
     237   147129244 :         ndr->flags = 0;
     238   147129244 :         ndr->alloc_size = NDR_BASE_MARSHALL_SIZE;
     239   147129244 :         ndr->data = talloc_array(ndr, uint8_t, ndr->alloc_size);
     240   147129244 :         if (!ndr->data) {
     241           0 :                 talloc_free(ndr);
     242           0 :                 return NULL;
     243             :         }
     244             : 
     245   145021724 :         return ndr;
     246             : }
     247             : 
     248             : /* return a DATA_BLOB structure for the current ndr_push marshalled data */
     249    82788518 : _PUBLIC_ DATA_BLOB ndr_push_blob(struct ndr_push *ndr)
     250             : {
     251      510793 :         DATA_BLOB blob;
     252    82788518 :         blob = data_blob_const(ndr->data, ndr->offset);
     253    82788518 :         if (ndr->alloc_size > ndr->offset) {
     254    82788518 :                 ndr->data[ndr->offset] = 0;
     255             :         }
     256    82788518 :         return blob;
     257             : }
     258             : 
     259             : 
     260             : /*
     261             :   expand the available space in the buffer to ndr->offset + extra_size
     262             : */
     263  5328025860 : _PUBLIC_ enum ndr_err_code ndr_push_expand(struct ndr_push *ndr, uint32_t extra_size)
     264             : {
     265  5328025860 :         uint32_t size = extra_size + ndr->offset;
     266             : 
     267  5328025860 :         if (size < ndr->offset) {
     268             :                 /* extra_size overflowed the offset */
     269           0 :                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand to %"PRIu32,
     270             :                                       size);
     271             :         }
     272             : 
     273  5328025860 :         if (ndr->fixed_buf_size) {
     274   799964342 :                 if (ndr->alloc_size >= size) {
     275   790631316 :                         return NDR_ERR_SUCCESS;
     276             :                 }
     277           2 :                 return ndr_push_error(ndr,
     278             :                                       NDR_ERR_BUFSIZE,
     279             :                                       "Overflow of fixed buffer in "
     280             :                                       "push_expand to %"PRIu32,
     281             :                                       size);
     282             :         }
     283             : 
     284  4528061518 :         if (ndr->alloc_size > size) {
     285  4460283698 :                 return NDR_ERR_SUCCESS;
     286             :         }
     287             : 
     288    16357863 :         ndr->alloc_size += NDR_BASE_MARSHALL_SIZE;
     289    16357863 :         if (size == UINT32_MAX) {
     290           0 :                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE, "Overflow in push_expand");
     291             :         }
     292    16357863 :         if (size+1 > ndr->alloc_size) {
     293     1829538 :                 ndr->alloc_size = size+1;
     294             :         }
     295    16357863 :         ndr->data = talloc_realloc(ndr, ndr->data, uint8_t, ndr->alloc_size);
     296    16357863 :         if (!ndr->data) {
     297           0 :                 return ndr_push_error(ndr, NDR_ERR_ALLOC, "Failed to push_expand to %"PRIu32,
     298             :                                       ndr->alloc_size);
     299             :         }
     300             : 
     301    16294603 :         return NDR_ERR_SUCCESS;
     302             : }
     303             : 
     304           0 : _PUBLIC_ void ndr_print_debugc_helper(struct ndr_print *ndr, const char *format, ...)
     305             : {
     306           0 :         va_list ap;
     307           0 :         char *s = NULL;
     308           0 :         uint32_t i;
     309           0 :         int ret;
     310           0 :         int dbgc_class;
     311             : 
     312           0 :         va_start(ap, format);
     313           0 :         ret = vasprintf(&s, format, ap);
     314           0 :         va_end(ap);
     315             : 
     316           0 :         if (ret == -1) {
     317           0 :                 return;
     318             :         }
     319             : 
     320           0 :         dbgc_class = *(int *)ndr->private_data;
     321             : 
     322           0 :         if (ndr->no_newline) {
     323           0 :                 DEBUGADDC(dbgc_class, 1,("%s", s));
     324           0 :                 free(s);
     325           0 :                 return;
     326             :         }
     327             : 
     328           0 :         for (i=0;i<ndr->depth;i++) {
     329           0 :                 DEBUGADDC(dbgc_class, 1,("    "));
     330             :         }
     331             : 
     332           0 :         DEBUGADDC(dbgc_class, 1,("%s\n", s));
     333           0 :         free(s);
     334             : }
     335             : 
     336      260458 : _PUBLIC_ void ndr_print_debug_helper(struct ndr_print *ndr, const char *format, ...)
     337             : {
     338       45005 :         va_list ap;
     339      260458 :         char *s = NULL;
     340       45005 :         uint32_t i;
     341       45005 :         int ret;
     342             : 
     343      260458 :         va_start(ap, format);
     344      260458 :         ret = vasprintf(&s, format, ap);
     345      260458 :         va_end(ap);
     346             : 
     347      260458 :         if (ret == -1) {
     348       67987 :                 return;
     349             :         }
     350             : 
     351      260458 :         if (ndr->no_newline) {
     352       67987 :                 DEBUGADD(1,("%s", s));
     353       67987 :                 free(s);
     354       67987 :                 return;
     355             :         }
     356             : 
     357     1022500 :         for (i=0;i<ndr->depth;i++) {
     358      830029 :                 DEBUGADD(1,("    "));
     359             :         }
     360             : 
     361      192471 :         DEBUGADD(1,("%s\n", s));
     362      192471 :         free(s);
     363             : }
     364             : 
     365       46694 : _PUBLIC_ void ndr_print_printf_helper(struct ndr_print *ndr, const char *format, ...)
     366             : {
     367       46122 :         va_list ap;
     368       46122 :         uint32_t i;
     369             : 
     370       46694 :         if (!ndr->no_newline) {
     371       20857 :                 for (i=0;i<ndr->depth;i++) {
     372       18292 :                         printf("    ");
     373             :                 }
     374             :         }
     375             : 
     376       46694 :         va_start(ap, format);
     377       46694 :         vprintf(format, ap);
     378       46694 :         va_end(ap);
     379       46694 :         if (!ndr->no_newline) {
     380        2565 :                 printf("\n");
     381             :         }
     382       46694 : }
     383             : 
     384    38417291 : _PUBLIC_ void ndr_print_string_helper(struct ndr_print *ndr, const char *format, ...)
     385             : {
     386     1559814 :         va_list ap;
     387     1559814 :         uint32_t i;
     388             : 
     389    38417291 :         if (!ndr->no_newline) {
     390    62697798 :                 for (i=0;i<ndr->depth;i++) {
     391    57182631 :                         ndr->private_data = talloc_asprintf_append_buffer(
     392    57182631 :                                 (char *)ndr->private_data, "    ");
     393             :                 }
     394             :         }
     395             : 
     396    38417291 :         va_start(ap, format);
     397    38417291 :         ndr->private_data = talloc_vasprintf_append_buffer((char *)ndr->private_data,
     398             :                                                     format, ap);
     399    38417291 :         va_end(ap);
     400    38417291 :         if (!ndr->no_newline) {
     401     5515167 :                 ndr->private_data = talloc_asprintf_append_buffer((char *)ndr->private_data,
     402             :                                                                   "\n");
     403             :         }
     404    38417291 : }
     405             : 
     406             : /*
     407             :   a useful helper function for printing idl structures via DEBUGC()
     408             : */
     409           0 : _PUBLIC_ void ndr_print_debugc(int dbgc_class, ndr_print_fn_t fn, const char *name, void *ptr)
     410             : {
     411           0 :         struct ndr_print *ndr;
     412             : 
     413           0 :         DEBUGC(dbgc_class, 1,(" "));
     414             : 
     415           0 :         ndr = talloc_zero(NULL, struct ndr_print);
     416           0 :         if (!ndr) return;
     417           0 :         ndr->private_data = &dbgc_class;
     418           0 :         ndr->print = ndr_print_debugc_helper;
     419           0 :         ndr->depth = 1;
     420           0 :         ndr->flags = 0;
     421             : #ifdef DEBUG_PASSWORD
     422           0 :         if (CHECK_DEBUGLVL(100)) {
     423           0 :                 ndr->print_secrets = true;
     424             :         }
     425             : #endif
     426             : 
     427           0 :         fn(ndr, name, ptr);
     428           0 :         talloc_free(ndr);
     429             : }
     430             : 
     431             : /*
     432             :   a useful helper function for printing idl structures via DEBUG()
     433             : */
     434        3776 : _PUBLIC_ bool ndr_print_debug(int level,
     435             :                               ndr_print_fn_t fn,
     436             :                               const char *name,
     437             :                               void *ptr,
     438             :                               const char *location,
     439             :                               const char *function)
     440             : {
     441           0 :         struct ndr_print *ndr;
     442             : 
     443        3776 :         DEBUGLF(level, (" "), location, function);
     444             : 
     445        3776 :         ndr = talloc_zero(NULL, struct ndr_print);
     446        3776 :         if (!ndr) return false;
     447        3776 :         ndr->print = ndr_print_debug_helper;
     448        3776 :         ndr->depth = 1;
     449        3776 :         ndr->flags = 0;
     450             : #ifdef DEBUG_PASSWORD
     451        3776 :         if (CHECK_DEBUGLVL(100)) {
     452           0 :                 ndr->print_secrets = true;
     453             :         }
     454             : #endif
     455             : 
     456        3776 :         fn(ndr, name, ptr);
     457        3776 :         talloc_free(ndr);
     458        3776 :         return true;
     459             : }
     460             : 
     461             : /*
     462             :   a useful helper function for printing idl unions via DEBUG()
     463             : */
     464           0 : _PUBLIC_ void ndr_print_union_debug(ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
     465             : {
     466           0 :         struct ndr_print *ndr;
     467             : 
     468           0 :         DEBUG(1,(" "));
     469             : 
     470           0 :         ndr = talloc_zero(NULL, struct ndr_print);
     471           0 :         if (!ndr) return;
     472           0 :         ndr->print = ndr_print_debug_helper;
     473           0 :         ndr->depth = 1;
     474           0 :         ndr->flags = 0;
     475             : #ifdef DEBUG_PASSWORD
     476           0 :         if (CHECK_DEBUGLVL(100)) {
     477           0 :                 ndr->print_secrets = true;
     478             :         }
     479             : #endif
     480             : 
     481           0 :         ndr_print_set_switch_value(ndr, ptr, level);
     482           0 :         fn(ndr, name, ptr);
     483           0 :         talloc_free(ndr);
     484             : }
     485             : 
     486             : /*
     487             :   a useful helper function for printing idl function calls via DEBUG()
     488             : */
     489         212 : _PUBLIC_ void ndr_print_function_debug(ndr_print_function_t fn, const char *name, ndr_flags_type flags, void *ptr)
     490             : {
     491           0 :         struct ndr_print *ndr;
     492             : 
     493         212 :         DEBUG(1,(" "));
     494             : 
     495         212 :         ndr = talloc_zero(NULL, struct ndr_print);
     496         212 :         if (!ndr) return;
     497         212 :         ndr->print = ndr_print_debug_helper;
     498         212 :         ndr->depth = 1;
     499         212 :         ndr->flags = 0;
     500             : #ifdef DEBUG_PASSWORD
     501         212 :         if (CHECK_DEBUGLVL(100)) {
     502           0 :                 ndr->print_secrets = true;
     503             :         }
     504             : #endif
     505             : 
     506         212 :         fn(ndr, name, flags, ptr);
     507         212 :         talloc_free(ndr);
     508             : }
     509             : 
     510             : /*
     511             :   a useful helper function for printing idl structures to a string
     512             : */
     513         448 : _PUBLIC_ char *ndr_print_struct_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, void *ptr)
     514             : {
     515          33 :         struct ndr_print *ndr;
     516         448 :         char *ret = NULL;
     517             : 
     518         448 :         ndr = talloc_zero(mem_ctx, struct ndr_print);
     519         448 :         if (!ndr) return NULL;
     520         448 :         ndr->private_data = talloc_strdup(ndr, "");
     521         448 :         if (!ndr->private_data) {
     522           0 :                 goto failed;
     523             :         }
     524         448 :         ndr->print = ndr_print_string_helper;
     525         448 :         ndr->depth = 1;
     526         448 :         ndr->flags = 0;
     527             : 
     528         448 :         fn(ndr, name, ptr);
     529         448 :         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
     530         448 : failed:
     531         448 :         talloc_free(ndr);
     532         448 :         return ret;
     533             : }
     534             : 
     535             : /*
     536             :   a useful helper function for printing idl unions to a string
     537             : */
     538           0 : _PUBLIC_ char *ndr_print_union_string(TALLOC_CTX *mem_ctx, ndr_print_fn_t fn, const char *name, uint32_t level, void *ptr)
     539             : {
     540           0 :         struct ndr_print *ndr;
     541           0 :         char *ret = NULL;
     542             : 
     543           0 :         ndr = talloc_zero(mem_ctx, struct ndr_print);
     544           0 :         if (!ndr) return NULL;
     545           0 :         ndr->private_data = talloc_strdup(ndr, "");
     546           0 :         if (!ndr->private_data) {
     547           0 :                 goto failed;
     548             :         }
     549           0 :         ndr->print = ndr_print_string_helper;
     550           0 :         ndr->depth = 1;
     551           0 :         ndr->flags = 0;
     552           0 :         ndr_print_set_switch_value(ndr, ptr, level);
     553           0 :         fn(ndr, name, ptr);
     554           0 :         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
     555           0 : failed:
     556           0 :         talloc_free(ndr);
     557           0 :         return ret;
     558             : }
     559             : 
     560             : /*
     561             :   a useful helper function for printing idl function calls to a string
     562             : */
     563       46248 : _PUBLIC_ char *ndr_print_function_string(TALLOC_CTX *mem_ctx,
     564             :                                 ndr_print_function_t fn, const char *name,
     565             :                                 ndr_flags_type flags, void *ptr)
     566             : {
     567        2990 :         struct ndr_print *ndr;
     568       46248 :         char *ret = NULL;
     569             : 
     570       46248 :         ndr = talloc_zero(mem_ctx, struct ndr_print);
     571       46248 :         if (!ndr) return NULL;
     572       46248 :         ndr->private_data = talloc_strdup(ndr, "");
     573       46248 :         if (!ndr->private_data) {
     574           0 :                 goto failed;
     575             :         }
     576       46248 :         ndr->print = ndr_print_string_helper;
     577       46248 :         ndr->depth = 1;
     578       46248 :         ndr->flags = 0;
     579       46248 :         fn(ndr, name, flags, ptr);
     580       46248 :         ret = talloc_steal(mem_ctx, (char *)ndr->private_data);
     581       46248 : failed:
     582       46248 :         talloc_free(ndr);
     583       46248 :         return ret;
     584             : }
     585             : 
     586   624783224 : _PUBLIC_ void ndr_set_flags(libndr_flags *pflags, libndr_flags new_flags)
     587             : {
     588             :         /* the big/little endian flags are inter-dependent */
     589   624783224 :         if (new_flags & LIBNDR_FLAG_LITTLE_ENDIAN) {
     590    17773571 :                 (*pflags) &= ~LIBNDR_FLAG_BIGENDIAN;
     591    17773571 :                 (*pflags) &= ~LIBNDR_FLAG_NDR64;
     592             :         }
     593   624783224 :         if (new_flags & LIBNDR_FLAG_BIGENDIAN) {
     594     2074061 :                 (*pflags) &= ~LIBNDR_FLAG_LITTLE_ENDIAN;
     595     2074061 :                 (*pflags) &= ~LIBNDR_FLAG_NDR64;
     596             :         }
     597   624783224 :         if (new_flags & LIBNDR_ALIGN_FLAGS) {
     598             :                 /* Ensure we only have the passed-in
     599             :                    align flag set in the new_flags,
     600             :                    remove any old align flag. */
     601   288405684 :                 (*pflags) &= ~LIBNDR_ALIGN_FLAGS;
     602             :         }
     603   624783224 :         if (new_flags & LIBNDR_FLAG_NO_RELATIVE_REVERSE) {
     604        2864 :                 (*pflags) &= ~LIBNDR_FLAG_RELATIVE_REVERSE;
     605             :         }
     606   624783224 :         (*pflags) |= new_flags;
     607   624783224 : }
     608             : 
     609             : /*
     610             :   return and possibly log an NDR error
     611             : */
     612         739 : _PUBLIC_ enum ndr_err_code _ndr_pull_error(struct ndr_pull *ndr,
     613             :                                            enum ndr_err_code ndr_err,
     614             :                                            const char *function,
     615             :                                            const char *location,
     616             :                                            const char *format, ...)
     617             : {
     618         739 :         char *s=NULL;
     619          54 :         va_list ap;
     620          54 :         int ret;
     621             : 
     622         739 :         if (ndr->flags & LIBNDR_FLAG_INCOMPLETE_BUFFER) {
     623           0 :                 switch (ndr_err) {
     624           0 :                 case NDR_ERR_BUFSIZE:
     625           0 :                         return NDR_ERR_INCOMPLETE_BUFFER;
     626           0 :                 default:
     627           0 :                         break;
     628             :                 }
     629             :         }
     630             : 
     631         739 :         va_start(ap, format);
     632         739 :         ret = vasprintf(&s, format, ap);
     633         739 :         va_end(ap);
     634             : 
     635         739 :         if (ret == -1) {
     636           0 :                 return NDR_ERR_ALLOC;
     637             :         }
     638             : 
     639         739 :         D_WARNING("%s: ndr_pull_error(%s): %s at %s\n",
     640             :                   function,
     641             :                   ndr_map_error2string(ndr_err),
     642             :                   s,
     643             :                   location);
     644             : 
     645         739 :         free(s);
     646             : 
     647         739 :         return ndr_err;
     648             : }
     649             : 
     650             : /*
     651             :   return and possibly log an NDR error
     652             : */
     653         376 : _PUBLIC_ enum ndr_err_code _ndr_push_error(struct ndr_push *ndr,
     654             :                                            enum ndr_err_code ndr_err,
     655             :                                            const char *function,
     656             :                                            const char *location,
     657             :                                            const char *format, ...)
     658             : {
     659         376 :         char *s=NULL;
     660          14 :         va_list ap;
     661          14 :         int ret;
     662             : 
     663         376 :         va_start(ap, format);
     664         376 :         ret = vasprintf(&s, format, ap);
     665         376 :         va_end(ap);
     666             : 
     667         376 :         if (ret == -1) {
     668           0 :                 return NDR_ERR_ALLOC;
     669             :         }
     670             : 
     671         376 :         D_WARNING("%s: ndr_push_error(%s): %s at %s\n",
     672             :                   function,
     673             :                   ndr_map_error2string(ndr_err),
     674             :                   s,
     675             :                   location);
     676             : 
     677         376 :         free(s);
     678             : 
     679         376 :         return ndr_err;
     680             : }
     681             : 
     682             : /*
     683             :   handle subcontext buffers, which in midl land are user-marshalled, but
     684             :   we use magic in pidl to make them easier to cope with
     685             : */
     686   205203485 : _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_start(struct ndr_pull *ndr,
     687             :                                    struct ndr_pull **_subndr,
     688             :                                    size_t header_size,
     689             :                                    ssize_t size_is)
     690             : {
     691     6576971 :         struct ndr_pull *subndr;
     692     6576971 :         uint32_t r_content_size;
     693   205203485 :         bool force_le = false;
     694   205203485 :         bool force_be = false;
     695             : 
     696   205203485 :         switch (header_size) {
     697   204177312 :         case 0: {
     698   204177312 :                 uint32_t content_size = ndr->data_size - ndr->offset;
     699   204177312 :                 if (size_is >= 0) {
     700   204084284 :                         content_size = size_is;
     701             :                 }
     702   197628272 :                 r_content_size = content_size;
     703   197628272 :                 break;
     704             :         }
     705             : 
     706      768798 :         case 2: {
     707        3728 :                 uint16_t content_size;
     708      768798 :                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &content_size));
     709      768798 :                 if (size_is >= 0 && size_is != content_size) {
     710           0 :                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%zd) (0x%04zx) mismatch content_size %"PRIu16" (0x%04"PRIx16")",
     711             :                                                 size_is, size_is,
     712             :                                                 content_size,
     713             :                                                 content_size);
     714             :                 }
     715      768798 :                 r_content_size = content_size;
     716      768798 :                 break;
     717             :         }
     718             : 
     719      119625 :         case 4: {
     720       21582 :                 uint32_t content_size;
     721      119625 :                 NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &content_size));
     722      119625 :                 if (size_is >= 0 && size_is != content_size) {
     723           0 :                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%zd) (0x%08zx) mismatch content_size %"PRIu32" (0x%08"PRIx32")",
     724             :                                                 size_is, size_is,
     725             :                                                 content_size,
     726             :                                                 content_size);
     727             :                 }
     728      119625 :                 r_content_size = content_size;
     729      119625 :                 break;
     730             :         }
     731      137750 :         case 0xFFFFFC01: {
     732             :                 /*
     733             :                  * Common Type Header for the Serialization Stream
     734             :                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
     735             :                  */
     736        2621 :                 uint8_t version;
     737        2621 :                 uint8_t drep;
     738        2621 :                 uint16_t hdrlen;
     739        2621 :                 uint32_t filler;
     740        2621 :                 uint32_t content_size;
     741        2621 :                 uint32_t reserved;
     742             : 
     743             :                 /* version */
     744      137750 :                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &version));
     745             : 
     746      137750 :                 if (version != 1) {
     747           0 :                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
     748             :                                               "Bad subcontext (PULL) Common Type Header version %"PRIu8" != 1",
     749             :                                               version);
     750             :                 }
     751             : 
     752             :                 /*
     753             :                  * 0x10 little endian
     754             :                  * 0x00 big endian
     755             :                  */
     756      137750 :                 NDR_CHECK(ndr_pull_uint8(ndr, NDR_SCALARS, &drep));
     757      137750 :                 if (drep == 0x10) {
     758      135129 :                         force_le = true;
     759           0 :                 } else if (drep == 0x00) {
     760           0 :                         force_be = true;
     761             :                 } else {
     762           0 :                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
     763             :                                               "Bad subcontext (PULL) Common Type Header invalid drep 0x%02"PRIX8,
     764             :                                               drep);
     765             :                 }
     766             : 
     767             :                 /* length of the "Private Header for Constructed Type" */
     768      137750 :                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &hdrlen));
     769      137750 :                 if (hdrlen != 8) {
     770           0 :                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
     771             :                                               "Bad subcontext (PULL) Common Type Header length %"PRIu16" != 8",
     772             :                                               hdrlen);
     773             :                 }
     774             : 
     775             :                 /* filler should be ignored */
     776      137750 :                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &filler));
     777             : 
     778             :                 /*
     779             :                  * Private Header for Constructed Type
     780             :                  */
     781             :                 /* length - will be updated later */
     782      137750 :                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &content_size));
     783      137750 :                 if (size_is >= 0 && size_is != content_size) {
     784           0 :                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) size_is(%zd) mismatch content_size %"PRIu32,
     785             :                                               size_is, content_size);
     786             :                 }
     787             :                 /* the content size must be a multiple of 8 */
     788      137750 :                 if ((content_size % 8) != 0) {
     789           0 :                         return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT,
     790             :                                               "Bad subcontext (PULL) size_is(%zd) not padded to 8 content_size %"PRIu32,
     791             :                                               size_is, content_size);
     792             :                 }
     793      137750 :                 r_content_size = content_size;
     794             : 
     795             :                 /* reserved */
     796      137750 :                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &reserved));
     797      137750 :                 break;
     798             :         }
     799           0 :         case 0xFFFFFFFF:
     800             :                 /*
     801             :                  * a shallow copy like subcontext
     802             :                  * useful for DCERPC pipe chunks.
     803             :                  */
     804           0 :                 subndr = talloc_zero(ndr, struct ndr_pull);
     805           0 :                 NDR_ERR_HAVE_NO_MEMORY(subndr);
     806             : 
     807           0 :                 subndr->flags                = ndr->flags;
     808           0 :                 subndr->current_mem_ctx      = ndr->current_mem_ctx;
     809           0 :                 subndr->data         = ndr->data;
     810           0 :                 subndr->offset               = ndr->offset;
     811           0 :                 subndr->data_size    = ndr->data_size;
     812             : 
     813           0 :                 *_subndr = subndr;
     814           0 :                 return NDR_ERR_SUCCESS;
     815             : 
     816           0 :         default:
     817           0 :                 return ndr_pull_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PULL) header_size %zu",
     818             :                                       header_size);
     819             :         }
     820             : 
     821   205203485 :         NDR_PULL_NEED_BYTES(ndr, r_content_size);
     822             : 
     823   205203473 :         subndr = talloc_zero(ndr, struct ndr_pull);
     824   205203473 :         NDR_ERR_HAVE_NO_MEMORY(subndr);
     825   205203473 :         subndr->flags                = ndr->flags & ~LIBNDR_FLAG_NDR64;
     826   205203473 :         subndr->current_mem_ctx      = ndr->current_mem_ctx;
     827             : 
     828   205203473 :         subndr->data = ndr->data + ndr->offset;
     829   205203473 :         subndr->offset = 0;
     830   205203473 :         subndr->data_size = r_content_size;
     831             : 
     832   205203473 :         if (force_le) {
     833      137750 :                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_LITTLE_ENDIAN);
     834   205065723 :         } else if (force_be) {
     835           0 :                 ndr_set_flags(&ndr->flags, LIBNDR_FLAG_BIGENDIAN);
     836             :         }
     837             : 
     838   205203473 :         *_subndr = subndr;
     839   205203473 :         return NDR_ERR_SUCCESS;
     840             : }
     841             : 
     842   205203460 : _PUBLIC_ enum ndr_err_code ndr_pull_subcontext_end(struct ndr_pull *ndr,
     843             :                                  struct ndr_pull *subndr,
     844             :                                  size_t header_size,
     845             :                                  ssize_t size_is)
     846             : {
     847     6576966 :         uint32_t advance;
     848     6576966 :         uint32_t highest_ofs;
     849             : 
     850   205203460 :         if (header_size == 0xFFFFFFFF) {
     851           0 :                 advance = subndr->offset - ndr->offset;
     852   205203460 :         } else if (size_is >= 0) {
     853   204090218 :                 advance = size_is;
     854     1113242 :         } else if (header_size > 0) {
     855     1020226 :                 advance = subndr->data_size;
     856             :         } else {
     857       93016 :                 advance = subndr->offset;
     858             :         }
     859             : 
     860   205203460 :         if (subndr->offset > ndr->relative_highest_offset) {
     861     2734453 :                 highest_ofs = subndr->offset;
     862             :         } else {
     863   202411703 :                 highest_ofs = subndr->relative_highest_offset;
     864             :         }
     865   205203460 :         if (!(subndr->flags & LIBNDR_FLAG_SUBCONTEXT_NO_UNREAD_BYTES)) {
     866             :                 /*
     867             :                  * avoid an error unless SUBCONTEXT_NO_UNREAD_BYTES is specified
     868             :                  */
     869   205020438 :                 highest_ofs = advance;
     870             :         }
     871   205203460 :         if (highest_ofs < advance) {
     872           0 :                 return ndr_pull_error(subndr, NDR_ERR_UNREAD_BYTES,
     873             :                                       "not all bytes consumed ofs[%"PRIu32"] advance[%"PRIu32"]",
     874             :                                       highest_ofs, advance);
     875             :         }
     876             : 
     877   205203460 :         NDR_CHECK(ndr_pull_advance(ndr, advance));
     878   198626494 :         return NDR_ERR_SUCCESS;
     879             : }
     880             : 
     881    56074709 : _PUBLIC_ enum ndr_err_code ndr_push_subcontext_start(struct ndr_push *ndr,
     882             :                                    struct ndr_push **_subndr,
     883             :                                    size_t header_size,
     884             :                                    ssize_t size_is)
     885             : {
     886     1584013 :         struct ndr_push *subndr;
     887             : 
     888    56074709 :         subndr = ndr_push_init_ctx(ndr);
     889    56074709 :         NDR_ERR_HAVE_NO_MEMORY(subndr);
     890    56074709 :         subndr->flags        = ndr->flags & ~LIBNDR_FLAG_NDR64;
     891             : 
     892    56074709 :         if (size_is > 0) {
     893        9678 :                 enum ndr_err_code status;
     894             : 
     895     1352741 :                 status = ndr_push_zero(subndr, size_is);
     896     1352741 :                 if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
     897           0 :                         talloc_free(subndr);
     898           0 :                         return status;
     899             :                 }
     900     1352741 :                 subndr->offset = 0;
     901     1352741 :                 subndr->relative_end_offset = size_is;
     902             :         }
     903             : 
     904    56074709 :         *_subndr = subndr;
     905    56074709 :         return NDR_ERR_SUCCESS;
     906             : }
     907             : 
     908             : /*
     909             :   push a subcontext header
     910             : */
     911    56074621 : _PUBLIC_ enum ndr_err_code ndr_push_subcontext_end(struct ndr_push *ndr,
     912             :                                  struct ndr_push *subndr,
     913             :                                  size_t header_size,
     914             :                                  ssize_t size_is)
     915             : {
     916     1584013 :         ssize_t padding_len;
     917             : 
     918    56074621 :         if (size_is >= 0) {
     919    54654285 :                 padding_len = size_is - subndr->offset;
     920    54654285 :                 if (padding_len < 0) {
     921           0 :                         return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext (PUSH) content_size %"PRIu32" is larger than size_is(%zd)",
     922             :                                               subndr->offset, size_is);
     923             :                 }
     924    54654285 :                 subndr->offset = size_is;
     925             :         }
     926             : 
     927    56074621 :         switch (header_size) {
     928    53095840 :         case 0:
     929    53095840 :                 break;
     930             : 
     931     1180125 :         case 2:
     932     1180125 :                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, subndr->offset));
     933     1173938 :                 break;
     934             : 
     935      149720 :         case 4:
     936      149720 :                 NDR_CHECK(ndr_push_uint3264(ndr, NDR_SCALARS, subndr->offset));
     937      127760 :                 break;
     938             : 
     939       96020 :         case 0xFFFFFC01:
     940             :                 /*
     941             :                  * Common Type Header for the Serialization Stream
     942             :                  * See [MS-RPCE] 2.2.6 Type Serialization Version 1
     943             :                  */
     944       96020 :                 padding_len = NDR_ROUND(subndr->offset, 8) - subndr->offset;
     945       96020 :                 if (padding_len > 0) {
     946       67408 :                         NDR_CHECK(ndr_push_zero(subndr, padding_len));
     947             :                 }
     948             : 
     949             :                 /* version */
     950       96020 :                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, 1));
     951             : 
     952             :                 /*
     953             :                  * 0x10 little endian
     954             :                  * 0x00 big endian
     955             :                  */
     956       98970 :                 NDR_CHECK(ndr_push_uint8(ndr, NDR_SCALARS, NDR_BE(ndr)?0x00:0x10));
     957             : 
     958             :                 /* length of the "Private Header for Constructed Type" */
     959       96020 :                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 8));
     960             : 
     961             :                 /* filler */
     962       96020 :                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0xCCCCCCCC));
     963             : 
     964             :                 /*
     965             :                  * Private Header for Constructed Type
     966             :                  */
     967             :                 /* length - will be updated later */
     968       96020 :                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, subndr->offset));
     969             : 
     970             :                 /* reserved */
     971       96020 :                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
     972       93070 :                 break;
     973             : 
     974           0 :         default:
     975           0 :                 return ndr_push_error(ndr, NDR_ERR_SUBCONTEXT, "Bad subcontext header size %zu",
     976             :                                       header_size);
     977             :         }
     978             : 
     979    56074621 :         NDR_CHECK(ndr_push_bytes(ndr, subndr->data, subndr->offset));
     980    54490608 :         return NDR_ERR_SUCCESS;
     981             : }
     982             : 
     983             : 
     984             : struct ndr_token {
     985             :         const void *key;
     986             :         uint32_t value;
     987             : };
     988             : 
     989             : /*
     990             :   store a token in the ndr context, for later retrieval
     991             : */
     992  1791790646 : _PUBLIC_ enum ndr_err_code ndr_token_store(TALLOC_CTX *mem_ctx,
     993             :                          struct ndr_token_list *list,
     994             :                          const void *key,
     995             :                          uint32_t value)
     996             : {
     997  1791790646 :         if (list->tokens == NULL) {
     998   371504643 :                 list->tokens = talloc_array(mem_ctx, struct ndr_token, 10);
     999   371504643 :                 if (list->tokens == NULL) {
    1000           0 :                         NDR_ERR_HAVE_NO_MEMORY(list->tokens);
    1001             :                 }
    1002             :         } else {
    1003  1420286003 :                 struct ndr_token *new_tokens = NULL;
    1004  1420286003 :                 uint32_t alloc_count = talloc_array_length(list->tokens);
    1005             : 
    1006             :                 /*
    1007             :                  * Check every time we have not allocated too many
    1008             :                  * tokens.  This ensures developer sanity when
    1009             :                  * debugging the boundary condition
    1010             :                  */
    1011  1420286003 :                 if (list->count >= NDR_TOKEN_MAX_LIST_SIZE) {
    1012           0 :                         return NDR_ERR_RANGE;
    1013             :                 }
    1014  1420286003 :                 if (list->count == alloc_count) {
    1015      461849 :                         uint32_t new_alloc;
    1016             :                         /*
    1017             :                          * Double the list, until we start in chunks
    1018             :                          * of 1000
    1019             :                          */
    1020    16761218 :                         uint32_t increment = MIN(list->count, 1000);
    1021    16761218 :                         new_alloc = alloc_count + increment;
    1022    16761218 :                         if (new_alloc < alloc_count) {
    1023           0 :                                 return NDR_ERR_RANGE;
    1024             :                         }
    1025    16761218 :                         new_tokens = talloc_realloc(mem_ctx, list->tokens,
    1026             :                                                     struct ndr_token, new_alloc);
    1027    16761218 :                         NDR_ERR_HAVE_NO_MEMORY(new_tokens);
    1028    16761218 :                         list->tokens = new_tokens;
    1029             :                 }
    1030             :         }
    1031  1791790646 :         list->tokens[list->count].key = key;
    1032  1791790646 :         list->tokens[list->count].value = value;
    1033  1791790646 :         list->count++;
    1034  1791790646 :         return NDR_ERR_SUCCESS;
    1035             : }
    1036             : 
    1037             : /*
    1038             :   retrieve a token from a ndr context, using cmp_fn to match the tokens
    1039             : */
    1040  1843139495 : _PUBLIC_ enum ndr_err_code ndr_token_retrieve_cmp_fn(struct ndr_token_list *list,
    1041             :                                                      const void *key, uint32_t *v,
    1042             :                                                      comparison_fn_t _cmp_fn,
    1043             :                                                      bool erase)
    1044             : {
    1045  1843139495 :         struct ndr_token *tokens = list->tokens;
    1046    36058326 :         unsigned i;
    1047  1843139495 :         if (_cmp_fn) {
    1048      693796 :                 for (i = list->count - 1; i < list->count; i--) {
    1049      573740 :                         if (_cmp_fn(tokens[i].key, key) == 0) {
    1050       32853 :                                 goto found;
    1051             :                         }
    1052             :                 }
    1053             :         } else {
    1054  2477000650 :                 for (i = list->count - 1; i < list->count; i--) {
    1055  2476932776 :                         if (tokens[i].key == key) {
    1056  1842918712 :                                 goto found;
    1057             :                         }
    1058             :                 }
    1059             :         }
    1060      186892 :         return NDR_ERR_TOKEN;
    1061  1842951565 : found:
    1062  1842951565 :         *v = tokens[i].value;
    1063  1842951565 :         if (erase) {
    1064  1335084753 :                 if (i != list->count - 1) {
    1065   115634558 :                         tokens[i] = tokens[list->count - 1];
    1066             :                 }
    1067  1335084753 :                 list->count--;
    1068             :         }
    1069  1806894277 :         return NDR_ERR_SUCCESS;
    1070             : }
    1071             : 
    1072             : /*
    1073             :   retrieve a token from a ndr context
    1074             : */
    1075  1335084753 : _PUBLIC_ enum ndr_err_code ndr_token_retrieve(struct ndr_token_list *list,
    1076             :                                               const void *key, uint32_t *v)
    1077             : {
    1078  1335084753 :         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, true);
    1079             : }
    1080             : 
    1081             : /*
    1082             :   peek at but don't removed a token from a ndr context
    1083             : */
    1084   507901833 : _PUBLIC_ enum ndr_err_code ndr_token_peek(struct ndr_token_list *list,
    1085             :                                           const void *key, uint32_t *v)
    1086             : {
    1087   507901833 :         return ndr_token_retrieve_cmp_fn(list, key, v, NULL, false);
    1088             : }
    1089             : 
    1090             : /*
    1091             :   pull an array size field and add it to the array_size_list token list
    1092             : */
    1093   243942270 : _PUBLIC_ enum ndr_err_code ndr_pull_array_size(struct ndr_pull *ndr, const void *p)
    1094             : {
    1095      323397 :         enum ndr_err_code ret;
    1096      323397 :         uint32_t size;
    1097   243942270 :         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &size));
    1098   243942246 :         ret = ndr_token_store(ndr, &ndr->array_size_list, p, size);
    1099   243942246 :         if (ret == NDR_ERR_RANGE) {
    1100           0 :                 return ndr_pull_error(ndr, ret,
    1101             :                                       "More than %d NDR tokens stored for array_size",
    1102             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1103             :         }
    1104   243618849 :         return ret;
    1105             : }
    1106             : 
    1107             : /*
    1108             :   get the stored array size field
    1109             : */
    1110   504221421 : _PUBLIC_ enum ndr_err_code ndr_get_array_size(struct ndr_pull *ndr, const void *p, uint32_t *size)
    1111             : {
    1112   504221421 :         return ndr_token_peek(&ndr->array_size_list, p, size);
    1113             : }
    1114             : 
    1115             : /*
    1116             :   get and remove from the stored list the stored array size field
    1117             : */
    1118   105070162 : _PUBLIC_ enum ndr_err_code ndr_steal_array_size(struct ndr_pull *ndr, const void *p, uint32_t *size)
    1119             : {
    1120   105070162 :         return ndr_token_retrieve(&ndr->array_size_list, p, size);
    1121             : }
    1122             : 
    1123             : /*
    1124             :  * check the stored array size field and remove from the stored list
    1125             :  * (the array_size NDR token list).  We try to remove when possible to
    1126             :  * avoid the list growing towards the bounds check
    1127             :  */
    1128    95531767 : _PUBLIC_ enum ndr_err_code ndr_check_steal_array_size(struct ndr_pull *ndr, const void *p, uint32_t size)
    1129             : {
    1130      140456 :         uint32_t stored;
    1131    95531767 :         NDR_CHECK(ndr_steal_array_size(ndr, p, &stored));
    1132    95531767 :         if (stored != size) {
    1133           0 :                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
    1134             :                                       "Bad array size - got %u expected %u\n",
    1135             :                                       stored, size);
    1136             :         }
    1137    95391311 :         return NDR_ERR_SUCCESS;
    1138             : }
    1139             : 
    1140             : /*
    1141             :  * check the stored array size field (leaving it on the array_size
    1142             :  * token list)
    1143             :  */
    1144   138872044 : _PUBLIC_ enum ndr_err_code ndr_check_array_size(struct ndr_pull *ndr, const void *p, uint32_t size)
    1145             : {
    1146       43852 :         uint32_t stored;
    1147   138872044 :         NDR_CHECK(ndr_get_array_size(ndr, p, &stored));
    1148   138872044 :         if (stored != size) {
    1149           0 :                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
    1150             :                                       "Bad array size - got %"PRIu32" expected %"PRIu32"\n",
    1151             :                                       stored, size);
    1152             :         }
    1153   138828192 :         return NDR_ERR_SUCCESS;
    1154             : }
    1155             : 
    1156             : /*
    1157             :   pull an array length field and add it to the array_length_list token list
    1158             : */
    1159    13190725 : _PUBLIC_ enum ndr_err_code ndr_pull_array_length(struct ndr_pull *ndr, const void *p)
    1160             : {
    1161      196551 :         enum ndr_err_code ret;
    1162      196551 :         uint32_t length, offset;
    1163    13190725 :         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &offset));
    1164    13190725 :         if (offset != 0) {
    1165           0 :                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
    1166             :                                       "non-zero array offset %"PRIu32"\n", offset);
    1167             :         }
    1168    13190725 :         NDR_CHECK(ndr_pull_uint3264(ndr, NDR_SCALARS, &length));
    1169    13190725 :         ret = ndr_token_store(ndr, &ndr->array_length_list, p, length);
    1170    13190725 :         if (ret == NDR_ERR_RANGE) {
    1171           0 :                 return ndr_pull_error(ndr, ret,
    1172             :                                       "More than %d NDR tokens stored for array_length_list",
    1173             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1174             :         }
    1175    12994174 :         return ret;
    1176             : }
    1177             : 
    1178             : /*
    1179             :   get the stored array length field
    1180             : */
    1181     3612538 : _PUBLIC_ enum ndr_err_code ndr_get_array_length(struct ndr_pull *ndr, const void *p, uint32_t *length)
    1182             : {
    1183     3612538 :         return ndr_token_peek(&ndr->array_length_list, p, length);
    1184             : }
    1185             : 
    1186             : /*
    1187             :  * check the stored array length field and remove from the stored list
    1188             :  * (the array_size NDR token list).  We try to remove when possible to
    1189             :  * avoid the list growing towards the bounds check
    1190             :  */
    1191    13190725 : _PUBLIC_ enum ndr_err_code ndr_steal_array_length(struct ndr_pull *ndr, const void *p, uint32_t *length)
    1192             : {
    1193    13190725 :         return ndr_token_retrieve(&ndr->array_length_list, p, length);
    1194             : }
    1195             : /*
    1196             :   check the stored array length field, removing it from the list
    1197             : */
    1198     3612538 : _PUBLIC_ enum ndr_err_code ndr_check_steal_array_length(struct ndr_pull *ndr, const void *p, uint32_t length)
    1199             : {
    1200       57462 :         uint32_t stored;
    1201     3612538 :         NDR_CHECK(ndr_steal_array_length(ndr, p, &stored));
    1202     3612538 :         if (stored != length) {
    1203           0 :                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
    1204             :                                       "Bad array length: got %"PRIu32" expected %"PRIu32"\n",
    1205             :                                       stored, length);
    1206             :         }
    1207     3555076 :         return NDR_ERR_SUCCESS;
    1208             : }
    1209             : 
    1210           0 : _PUBLIC_ enum ndr_err_code ndr_push_pipe_chunk_trailer(struct ndr_push *ndr, ndr_flags_type ndr_flags, uint32_t count)
    1211             : {
    1212           0 :         if (ndr->flags & LIBNDR_FLAG_NDR64) {
    1213           0 :                 int64_t tmp = 0 - (int64_t)count;
    1214           0 :                 uint64_t ncount = tmp;
    1215             : 
    1216           0 :                 NDR_CHECK(ndr_push_hyper(ndr, ndr_flags, ncount));
    1217             :         }
    1218             : 
    1219           0 :         return NDR_ERR_SUCCESS;
    1220             : }
    1221             : 
    1222           0 : _PUBLIC_ enum ndr_err_code ndr_check_pipe_chunk_trailer(struct ndr_pull *ndr, ndr_flags_type ndr_flags, uint32_t count)
    1223             : {
    1224           0 :         if (ndr->flags & LIBNDR_FLAG_NDR64) {
    1225           0 :                 int64_t tmp = 0 - (int64_t)count;
    1226           0 :                 uint64_t ncount1 = tmp;
    1227           0 :                 uint64_t ncount2;
    1228             : 
    1229           0 :                 NDR_CHECK(ndr_pull_hyper(ndr, ndr_flags, &ncount2));
    1230           0 :                 if (ncount1 == ncount2) {
    1231           0 :                         return NDR_ERR_SUCCESS;
    1232             :                 }
    1233             : 
    1234           0 :                 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE,
    1235             :                         "Bad pipe trailer[%"PRIu64" should be %"PRIu64"] size was %"PRIu32"\"",
    1236             :                         ncount2,
    1237             :                         ncount1,
    1238             :                         count);
    1239             :         }
    1240             : 
    1241           0 :         return NDR_ERR_SUCCESS;
    1242             : }
    1243             : 
    1244             : /*
    1245             :   store a switch value
    1246             :  */
    1247   302039080 : _PUBLIC_ enum ndr_err_code ndr_push_set_switch_value(struct ndr_push *ndr, const void *p, uint32_t val)
    1248             : {
    1249     7043023 :         enum ndr_err_code ret =
    1250   302039080 :                 ndr_token_store(ndr, &ndr->switch_list, p, val);
    1251   302039080 :         if (ret == NDR_ERR_RANGE) {
    1252           0 :                 return ndr_push_error(ndr, ret,
    1253             :                                       "More than %d NDR tokens stored for switch_list",
    1254             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1255             :         }
    1256   294996057 :         return ret;
    1257             : }
    1258             : 
    1259  1166561387 : _PUBLIC_ enum ndr_err_code ndr_pull_set_switch_value(struct ndr_pull *ndr, const void *p, uint32_t val)
    1260             : {
    1261             : 
    1262    33469619 :         enum ndr_err_code ret =
    1263  1166561387 :                 ndr_token_store(ndr, &ndr->switch_list, p, val);
    1264  1166561387 :         if (ret == NDR_ERR_RANGE) {
    1265           0 :                 return ndr_pull_error(ndr, ret,
    1266             :                                       "More than %d NDR tokens stored for switch_list",
    1267             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1268             :         }
    1269  1133091768 :         return ret;
    1270             : }
    1271             : 
    1272       28686 : _PUBLIC_ enum ndr_err_code ndr_print_set_switch_value(struct ndr_print *ndr, const void *p, uint32_t val)
    1273             : {
    1274       28686 :         return ndr_token_store(ndr, &ndr->switch_list, p, val);
    1275             : }
    1276             : 
    1277             : /* retrieve a switch value (for push) and remove it from the list */
    1278   247698075 : _PUBLIC_ enum ndr_err_code ndr_push_steal_switch_value(struct ndr_push *ndr,
    1279             :                                                        const void *p,
    1280             :                                                        uint32_t *v)
    1281             : {
    1282   247698075 :         return ndr_token_retrieve(&ndr->switch_list, p, v);
    1283             : }
    1284             : 
    1285             : /* retrieve a switch value and remove it from the list */
    1286       28686 : _PUBLIC_ uint32_t ndr_print_steal_switch_value(struct ndr_print *ndr, const void *p)
    1287             : {
    1288        3864 :         enum ndr_err_code status;
    1289        3864 :         uint32_t v;
    1290             : 
    1291       28686 :         status = ndr_token_retrieve(&ndr->switch_list, p, &v);
    1292       28686 :         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
    1293           0 :                 return 0;
    1294             :         }
    1295             : 
    1296       28686 :         return v;
    1297             : }
    1298             : 
    1299             : /* retrieve a switch value and remove it from the list */
    1300   903449146 : _PUBLIC_ enum ndr_err_code ndr_pull_steal_switch_value(struct ndr_pull *ndr,
    1301             :                                                        const void *p,
    1302             :                                                        uint32_t *v)
    1303             : {
    1304   903449146 :         return ndr_token_retrieve(&ndr->switch_list, p, v);
    1305             : }
    1306             : 
    1307             : /*
    1308             :   pull a struct from a blob using NDR
    1309             : */
    1310    86659908 : _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
    1311             :                               ndr_pull_flags_fn_t fn)
    1312             : {
    1313      667118 :         struct ndr_pull *ndr;
    1314    86659908 :         ndr = ndr_pull_init_blob(blob, mem_ctx);
    1315    86659908 :         NDR_ERR_HAVE_NO_MEMORY(ndr);
    1316    86659908 :         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1317    86659819 :         talloc_free(ndr);
    1318    86659819 :         return NDR_ERR_SUCCESS;
    1319             : }
    1320             : 
    1321             : /*
    1322             :   pull a struct from a blob using NDR - failing if all bytes are not consumed
    1323             : */
    1324     9830100 : _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
    1325             :                                                     void *p, ndr_pull_flags_fn_t fn)
    1326             : {
    1327      335060 :         struct ndr_pull *ndr;
    1328      335060 :         uint32_t highest_ofs;
    1329     9830100 :         ndr = ndr_pull_init_blob(blob, mem_ctx);
    1330     9830100 :         NDR_ERR_HAVE_NO_MEMORY(ndr);
    1331     9830100 :         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1332     9830090 :         if (ndr->offset > ndr->relative_highest_offset) {
    1333     8945860 :                 highest_ofs = ndr->offset;
    1334             :         } else {
    1335      549171 :                 highest_ofs = ndr->relative_highest_offset;
    1336             :         }
    1337     9830090 :         if (highest_ofs < ndr->data_size) {
    1338          10 :                 enum ndr_err_code ret;
    1339         165 :                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
    1340             :                                      "not all bytes consumed ofs[%"PRIu32"] size[%"PRIu32"]",
    1341             :                                      highest_ofs, ndr->data_size);
    1342         165 :                 talloc_free(ndr);
    1343         165 :                 return ret;
    1344             :         }
    1345     9829925 :         talloc_free(ndr);
    1346     9829925 :         return NDR_ERR_SUCCESS;
    1347             : }
    1348             : 
    1349             : /*
    1350             :  * pull a struct from a blob using NDR
    1351             :  *
    1352             :  * This only works for structures with NO allocated memory, like
    1353             :  * objectSID and GUID.  This helps because we parse these a lot.
    1354             :  */
    1355   252795487 : _PUBLIC_ enum ndr_err_code ndr_pull_struct_blob_noalloc(const uint8_t *buf,
    1356             :                                                         size_t buflen,
    1357             :                                                         void *p,
    1358             :                                                         ndr_pull_flags_fn_t fn,
    1359             :                                                         size_t *consumed)
    1360             : {
    1361             :         /*
    1362             :          * We init this structure on the stack here, to avoid a
    1363             :          * talloc() as otherwise this call to the fn() is assured not
    1364             :          * to be doing any allocation, eg SIDs and GUIDs.
    1365             :          *
    1366             :          * This allows us to keep the safety of the PIDL-generated
    1367             :          * code without the talloc() overhead.
    1368             :          */
    1369   252795487 :         struct ndr_pull ndr = {
    1370             :                 .data = discard_const_p(uint8_t, buf),
    1371             :                 .data_size = buflen,
    1372             :                 .current_mem_ctx = (void *)-1,
    1373             :         };
    1374             : 
    1375   252795487 :         NDR_CHECK(fn(&ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1376   252795050 :         *consumed = MAX(ndr.offset, ndr.relative_highest_offset);
    1377             : 
    1378   252795050 :         return NDR_ERR_SUCCESS;
    1379             : }
    1380             : 
    1381             : /*
    1382             :   pull a struct from a blob using NDR - failing if all bytes are not consumed
    1383             : 
    1384             :   This only works for structures with NO allocated memory, like
    1385             :   objectSID and GUID.  This helps because we parse these a lot.
    1386             : */
    1387             : _PUBLIC_ enum ndr_err_code
    1388   252794056 : ndr_pull_struct_blob_all_noalloc(const DATA_BLOB *blob,
    1389             :                                  void *p,
    1390             :                                  ndr_pull_flags_fn_t fn)
    1391             : {
    1392     5680248 :         size_t consumed;
    1393     5680248 :         enum ndr_err_code ndr_err;
    1394             : 
    1395   258474304 :         ndr_err = ndr_pull_struct_blob_noalloc(blob->data,
    1396   252794056 :                                                blob->length,
    1397             :                                                p,
    1398             :                                                fn,
    1399             :                                                &consumed);
    1400   252794056 :         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
    1401         434 :                 return ndr_err;
    1402             :         }
    1403             : 
    1404   252793619 :         if (consumed < blob->length) {
    1405           0 :                 D_WARNING("not all bytes consumed ofs[%zu] size[%zu]",
    1406             :                           consumed,
    1407             :                           blob->length);
    1408           0 :                 return NDR_ERR_UNREAD_BYTES;
    1409             :         }
    1410             : 
    1411   247113374 :         return NDR_ERR_SUCCESS;
    1412             : }
    1413             : 
    1414             : /*
    1415             :   pull a union from a blob using NDR, given the union discriminator
    1416             : */
    1417      405553 : _PUBLIC_ enum ndr_err_code ndr_pull_union_blob(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
    1418             :                                                void *p,
    1419             :                              uint32_t level, ndr_pull_flags_fn_t fn)
    1420             : {
    1421        7389 :         struct ndr_pull *ndr;
    1422      405553 :         ndr = ndr_pull_init_blob(blob, mem_ctx);
    1423      405553 :         NDR_ERR_HAVE_NO_MEMORY(ndr);
    1424      405553 :         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
    1425      405553 :         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1426      405553 :         talloc_free(ndr);
    1427      405553 :         return NDR_ERR_SUCCESS;
    1428             : }
    1429             : 
    1430             : /*
    1431             :   pull a union from a blob using NDR, given the union discriminator,
    1432             :   failing if all bytes are not consumed
    1433             : */
    1434           3 : _PUBLIC_ enum ndr_err_code ndr_pull_union_blob_all(const DATA_BLOB *blob, TALLOC_CTX *mem_ctx,
    1435             :                                                    void *p,
    1436             :                              uint32_t level, ndr_pull_flags_fn_t fn)
    1437             : {
    1438           0 :         struct ndr_pull *ndr;
    1439           0 :         uint32_t highest_ofs;
    1440           3 :         ndr = ndr_pull_init_blob(blob, mem_ctx);
    1441           3 :         NDR_ERR_HAVE_NO_MEMORY(ndr);
    1442           3 :         NDR_CHECK_FREE(ndr_pull_set_switch_value(ndr, p, level));
    1443           3 :         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1444           3 :         if (ndr->offset > ndr->relative_highest_offset) {
    1445           3 :                 highest_ofs = ndr->offset;
    1446             :         } else {
    1447           0 :                 highest_ofs = ndr->relative_highest_offset;
    1448             :         }
    1449           3 :         if (highest_ofs < ndr->data_size) {
    1450           0 :                 enum ndr_err_code ret;
    1451           0 :                 ret = ndr_pull_error(ndr, NDR_ERR_UNREAD_BYTES,
    1452             :                                      "not all bytes consumed ofs[%"PRIu32"] size[%"PRIu32"]",
    1453             :                                      highest_ofs, ndr->data_size);
    1454           0 :                 talloc_free(ndr);
    1455           0 :                 return ret;
    1456             :         }
    1457           3 :         talloc_free(ndr);
    1458           3 :         return NDR_ERR_SUCCESS;
    1459             : }
    1460             : 
    1461             : /*
    1462             :   push a struct to a blob using NDR
    1463             : */
    1464    59818495 : _PUBLIC_ enum ndr_err_code ndr_push_struct_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, const void *p, ndr_push_flags_fn_t fn)
    1465             : {
    1466      404728 :         struct ndr_push *ndr;
    1467    59818495 :         ndr = ndr_push_init_ctx(mem_ctx);
    1468    59818495 :         NDR_ERR_HAVE_NO_MEMORY(ndr);
    1469             : 
    1470    59818495 :         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1471             : 
    1472    59818483 :         *blob = ndr_push_blob(ndr);
    1473    59818483 :         talloc_steal(mem_ctx, blob->data);
    1474    59818483 :         talloc_free(ndr);
    1475             : 
    1476    59818483 :         return NDR_ERR_SUCCESS;
    1477             : }
    1478             : 
    1479             : /*
    1480             :   push a struct into a provided blob using NDR.
    1481             : 
    1482             :   We error because we want to have the performance issue (extra
    1483             :   talloc() calls) show up as an error, not just slower code.  This is
    1484             :   used for things like GUIDs, which we expect to be a fixed size, and
    1485             :   SIDs that we can pre-calculate the size for.
    1486             : */
    1487   150356014 : _PUBLIC_ enum ndr_err_code ndr_push_struct_into_fixed_blob(
    1488             :         DATA_BLOB *blob, const void *p, ndr_push_flags_fn_t fn)
    1489             : {
    1490   150356014 :         struct ndr_push ndr = {
    1491   150356014 :                 .data = blob->data,
    1492   150356014 :                 .alloc_size = blob->length,
    1493             :                 .fixed_buf_size = true
    1494             :         };
    1495             : 
    1496   150356014 :         NDR_CHECK(fn(&ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1497             : 
    1498   150356012 :         if (ndr.offset != blob->length) {
    1499           2 :                 return ndr_push_error(&ndr, NDR_ERR_BUFSIZE,
    1500             :                                       "buffer was either too large or small "
    1501             :                                       "ofs[%"PRIu32"] size[%zu]",
    1502             :                                       ndr.offset, blob->length);
    1503             :         }
    1504             : 
    1505   148597446 :         return NDR_ERR_SUCCESS;
    1506             : }
    1507             : 
    1508             : /*
    1509             :   push a union to a blob using NDR
    1510             : */
    1511      268349 : _PUBLIC_ enum ndr_err_code ndr_push_union_blob(DATA_BLOB *blob, TALLOC_CTX *mem_ctx, void *p,
    1512             :                              uint32_t level, ndr_push_flags_fn_t fn)
    1513             : {
    1514        8888 :         struct ndr_push *ndr;
    1515      268349 :         ndr = ndr_push_init_ctx(mem_ctx);
    1516      268349 :         NDR_ERR_HAVE_NO_MEMORY(ndr);
    1517             : 
    1518      268349 :         NDR_CHECK_FREE(ndr_push_set_switch_value(ndr, p, level));
    1519      268349 :         NDR_CHECK_FREE(fn(ndr, NDR_SCALARS|NDR_BUFFERS, p));
    1520             : 
    1521      268349 :         *blob = ndr_push_blob(ndr);
    1522      268349 :         talloc_steal(mem_ctx, blob->data);
    1523      268349 :         talloc_free(ndr);
    1524             : 
    1525      268349 :         return NDR_ERR_SUCCESS;
    1526             : }
    1527             : 
    1528             : /*
    1529             :   generic ndr_size_*() handler for structures
    1530             : */
    1531    13877487 : _PUBLIC_ size_t ndr_size_struct(const void *p, libndr_flags flags, ndr_push_flags_fn_t push)
    1532             : {
    1533        4397 :         struct ndr_push *ndr;
    1534        4397 :         enum ndr_err_code status;
    1535        4397 :         size_t ret;
    1536             : 
    1537             :         /* avoid recursion */
    1538    13877487 :         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
    1539             : 
    1540             :         /* Avoid following a NULL pointer */
    1541     5876502 :         if (p == NULL) {
    1542           0 :                 return 0;
    1543             :         }
    1544             : 
    1545     5876500 :         ndr = ndr_push_init_ctx(NULL);
    1546     5876500 :         if (!ndr) return 0;
    1547     5876500 :         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
    1548     5876500 :         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, discard_const(p));
    1549     5876500 :         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
    1550           0 :                 talloc_free(ndr);
    1551           0 :                 return 0;
    1552             :         }
    1553     5876500 :         ret = ndr->offset;
    1554     5876500 :         talloc_free(ndr);
    1555     5876500 :         return ret;
    1556             : }
    1557             : 
    1558             : /*
    1559             :   generic ndr_size_*() handler for unions
    1560             : */
    1561     2388602 : _PUBLIC_ size_t ndr_size_union(const void *p, libndr_flags flags, uint32_t level, ndr_push_flags_fn_t push)
    1562             : {
    1563        9706 :         struct ndr_push *ndr;
    1564        9706 :         enum ndr_err_code status;
    1565        9706 :         size_t ret;
    1566             : 
    1567             :         /* avoid recursion */
    1568     2388602 :         if (flags & LIBNDR_FLAG_NO_NDR_SIZE) return 0;
    1569             : 
    1570             :         /* Avoid following a NULL pointer */
    1571     2388602 :         if (p == NULL) {
    1572           0 :                 return 0;
    1573             :         }
    1574             : 
    1575     2388601 :         ndr = ndr_push_init_ctx(NULL);
    1576     2388601 :         if (!ndr) return 0;
    1577     2388601 :         ndr->flags |= flags | LIBNDR_FLAG_NO_NDR_SIZE;
    1578             : 
    1579     2388601 :         status = ndr_push_set_switch_value(ndr, p, level);
    1580     2388601 :         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
    1581           0 :                 talloc_free(ndr);
    1582           0 :                 return 0;
    1583             :         }
    1584     2388601 :         status = push(ndr, NDR_SCALARS|NDR_BUFFERS, p);
    1585     2388601 :         if (!NDR_ERR_CODE_IS_SUCCESS(status)) {
    1586         264 :                 talloc_free(ndr);
    1587         264 :                 return 0;
    1588             :         }
    1589     2388337 :         ret = ndr->offset;
    1590     2388337 :         talloc_free(ndr);
    1591     2388337 :         return ret;
    1592             : }
    1593             : 
    1594             : /*
    1595             :   get the current base for relative pointers for the push
    1596             : */
    1597      239231 : _PUBLIC_ uint32_t ndr_push_get_relative_base_offset(struct ndr_push *ndr)
    1598             : {
    1599      239231 :         return ndr->relative_base_offset;
    1600             : }
    1601             : 
    1602             : /*
    1603             :   restore the old base for relative pointers for the push
    1604             : */
    1605      239231 : _PUBLIC_ void ndr_push_restore_relative_base_offset(struct ndr_push *ndr, uint32_t offset)
    1606             : {
    1607      239231 :         ndr->relative_base_offset = offset;
    1608      239231 : }
    1609             : 
    1610             : /*
    1611             :   setup the current base for relative pointers for the push
    1612             :   called in the NDR_SCALAR stage
    1613             : */
    1614      126598 : _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset1(struct ndr_push *ndr, const void *p, uint32_t offset)
    1615             : {
    1616          12 :         enum ndr_err_code ret;
    1617      126598 :         ndr->relative_base_offset = offset;
    1618      126598 :         ret = ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
    1619      126598 :         if (ret == NDR_ERR_RANGE) {
    1620           0 :                 return ndr_push_error(ndr, ret,
    1621             :                                       "More than %d NDR tokens stored for relative_base_list",
    1622             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1623             :         }
    1624      126586 :         return ret;
    1625             : }
    1626             : 
    1627             : /*
    1628             :   setup the current base for relative pointers for the push
    1629             :   called in the NDR_BUFFERS stage
    1630             : */
    1631      126286 : _PUBLIC_ enum ndr_err_code ndr_push_setup_relative_base_offset2(struct ndr_push *ndr, const void *p)
    1632             : {
    1633      126286 :         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
    1634             : }
    1635             : 
    1636             : /*
    1637             :   push a relative object - stage1
    1638             :   this is called during SCALARS processing
    1639             : */
    1640    21659757 : _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr1(struct ndr_push *ndr, const void *p)
    1641             : {
    1642      838910 :         enum ndr_err_code ret;
    1643    21659757 :         if (p == NULL) {
    1644     2375632 :                 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
    1645     2375632 :                 return NDR_ERR_SUCCESS;
    1646             :         }
    1647    19284125 :         NDR_CHECK(ndr_push_align(ndr, 4));
    1648    19284125 :         ret = ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset);
    1649    19284125 :         if (ret == NDR_ERR_RANGE) {
    1650           0 :                 return ndr_push_error(ndr, ret,
    1651             :                                       "More than %d NDR tokens stored for relative_list",
    1652             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1653             :         }
    1654    19284125 :         NDR_CHECK(ret);
    1655    19284125 :         return ndr_push_uint32(ndr, NDR_SCALARS, 0xFFFFFFFF);
    1656             : }
    1657             : 
    1658             : /*
    1659             :   push a short relative object - stage1
    1660             :   this is called during SCALARS processing
    1661             : */
    1662      190358 : _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr1(struct ndr_push *ndr, const void *p)
    1663             : {
    1664        4762 :         enum ndr_err_code ret;
    1665      190358 :         if (p == NULL) {
    1666          54 :                 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, 0));
    1667          54 :                 return NDR_ERR_SUCCESS;
    1668             :         }
    1669      190304 :         NDR_CHECK(ndr_push_align(ndr, 2));
    1670      190304 :         ret = ndr_token_store(ndr, &ndr->relative_list, p, ndr->offset);
    1671      190304 :         if (ret == NDR_ERR_RANGE) {
    1672           0 :                 return ndr_push_error(ndr, ret,
    1673             :                                       "More than %d NDR tokens stored for relative_list",
    1674             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1675             :         }
    1676      190304 :         NDR_CHECK(ret);
    1677      190304 :         return ndr_push_uint16(ndr, NDR_SCALARS, 0xFFFF);
    1678             : }
    1679             : /*
    1680             :   push a relative object - stage2
    1681             :   this is called during buffers processing
    1682             : */
    1683    19192581 : static enum ndr_err_code ndr_push_relative_ptr2(struct ndr_push *ndr, const void *p)
    1684             : {
    1685      721124 :         uint32_t save_offset;
    1686    19192581 :         uint32_t ptr_offset = 0xFFFFFFFF;
    1687    19192581 :         if (p == NULL) {
    1688           0 :                 return NDR_ERR_SUCCESS;
    1689             :         }
    1690    19192581 :         save_offset = ndr->offset;
    1691    19192581 :         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
    1692    19192581 :         if (ptr_offset > ndr->offset) {
    1693           0 :                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
    1694             :                                       "ndr_push_relative_ptr2 ptr_offset(%"PRIu32") > ndr->offset(%"PRIu32")",
    1695             :                                       ptr_offset, ndr->offset);
    1696             :         }
    1697    19192581 :         ndr->offset = ptr_offset;
    1698    19192581 :         if (save_offset < ndr->relative_base_offset) {
    1699           0 :                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
    1700             :                                       "ndr_push_relative_ptr2 save_offset(%"PRIu32") < ndr->relative_base_offset(%"PRIu32")",
    1701             :                                       save_offset, ndr->relative_base_offset);
    1702             :         }
    1703    19192581 :         NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, save_offset - ndr->relative_base_offset));
    1704    19192581 :         ndr->offset = save_offset;
    1705    19192581 :         return NDR_ERR_SUCCESS;
    1706             : }
    1707             : /*
    1708             :   push a short relative object - stage2
    1709             :   this is called during buffers processing
    1710             : */
    1711      190304 : _PUBLIC_ enum ndr_err_code ndr_push_short_relative_ptr2(struct ndr_push *ndr, const void *p)
    1712             : {
    1713        4756 :         uint32_t save_offset;
    1714      190304 :         uint32_t ptr_offset = 0xFFFF;
    1715        4756 :         uint32_t relative_offset;
    1716        4756 :         size_t pad;
    1717      190304 :         size_t align = 1;
    1718             : 
    1719      190304 :         if (p == NULL) {
    1720           0 :                 return NDR_ERR_SUCCESS;
    1721             :         }
    1722             : 
    1723      190304 :         if (ndr->offset < ndr->relative_base_offset) {
    1724           0 :                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
    1725             :                                       "ndr_push_relative_ptr2 ndr->offset(%"PRIu32") < ndr->relative_base_offset(%"PRIu32")",
    1726             :                                       ndr->offset, ndr->relative_base_offset);
    1727             :         }
    1728             : 
    1729      190304 :         relative_offset = ndr->offset - ndr->relative_base_offset;
    1730             : 
    1731      190304 :         if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
    1732          12 :                 align = 1;
    1733      190280 :         } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
    1734       29742 :                 align = 2;
    1735      160536 :         } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
    1736           0 :                 align = 4;
    1737      160536 :         } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
    1738      130284 :                 align = 8;
    1739             :         }
    1740             : 
    1741      190304 :         pad = ndr_align_size(relative_offset, align);
    1742      190304 :         if (pad != 0) {
    1743      122428 :                 NDR_CHECK(ndr_push_zero(ndr, pad));
    1744             :         }
    1745             : 
    1746      190304 :         relative_offset = ndr->offset - ndr->relative_base_offset;
    1747      190304 :         if (relative_offset > UINT16_MAX) {
    1748           0 :                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
    1749             :                                       "ndr_push_relative_ptr2 relative_offset(%"PRIu32") > UINT16_MAX",
    1750             :                                       relative_offset);
    1751             :         }
    1752             : 
    1753      190304 :         save_offset = ndr->offset;
    1754      190304 :         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &ptr_offset));
    1755      190304 :         if (ptr_offset > ndr->offset) {
    1756           0 :                 return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
    1757             :                                       "ndr_push_short_relative_ptr2 ptr_offset(%"PRIu32") > ndr->offset(%"PRIu32")",
    1758             :                                       ptr_offset, ndr->offset);
    1759             :         }
    1760      190304 :         ndr->offset = ptr_offset;
    1761      190304 :         NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, relative_offset));
    1762      190304 :         ndr->offset = save_offset;
    1763      190304 :         return NDR_ERR_SUCCESS;
    1764             : }
    1765             : 
    1766             : /*
    1767             :   push a relative object - stage2 start
    1768             :   this is called during buffers processing
    1769             : */
    1770    19284125 : _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_start(struct ndr_push *ndr, const void *p)
    1771             : {
    1772      721124 :         enum ndr_err_code ret;
    1773    19284125 :         if (p == NULL) {
    1774           0 :                 return NDR_ERR_SUCCESS;
    1775             :         }
    1776    19284125 :         if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
    1777      721124 :                 uint32_t relative_offset;
    1778      721124 :                 size_t pad;
    1779    19147084 :                 size_t align = 1;
    1780             : 
    1781    19147084 :                 if (ndr->offset < ndr->relative_base_offset) {
    1782           0 :                         return ndr_push_error(ndr, NDR_ERR_BUFSIZE,
    1783             :                                       "ndr_push_relative_ptr2_start ndr->offset(%"PRIu32") < ndr->relative_base_offset(%"PRIu32")",
    1784             :                                       ndr->offset, ndr->relative_base_offset);
    1785             :                 }
    1786             : 
    1787    19147084 :                 relative_offset = ndr->offset - ndr->relative_base_offset;
    1788             : 
    1789    19147084 :                 if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
    1790           0 :                         align = 1;
    1791    19144679 :                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
    1792        8820 :                         align = 2;
    1793    19135345 :                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
    1794        6996 :                         align = 4;
    1795    19128349 :                 } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
    1796      156732 :                         align = 8;
    1797             :                 }
    1798             : 
    1799    19147084 :                 pad = ndr_align_size(relative_offset, align);
    1800    19147084 :                 if (pad) {
    1801         680 :                         NDR_CHECK(ndr_push_zero(ndr, pad));
    1802             :                 }
    1803             : 
    1804    19147084 :                 return ndr_push_relative_ptr2(ndr, p);
    1805             :         }
    1806      137041 :         if (ndr->relative_end_offset == -1) {
    1807           0 :                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
    1808             :                               "ndr_push_relative_ptr2_start RELATIVE_REVERSE flag set and relative_end_offset %"PRIu32,
    1809             :                               ndr->relative_end_offset);
    1810             :         }
    1811      137041 :         ret = ndr_token_store(ndr,
    1812             :                               &ndr->relative_begin_list,
    1813             :                               p,
    1814             :                               ndr->offset);
    1815      137041 :         if (ret == NDR_ERR_RANGE) {
    1816           0 :                 return ndr_push_error(ndr, ret,
    1817             :                                       "More than %d NDR tokens stored for array_size",
    1818             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1819             :         }
    1820      137041 :         return ret;
    1821             : }
    1822             : 
    1823             : /*
    1824             :   push a relative object - stage2 end
    1825             :   this is called during buffers processing
    1826             : */
    1827    19284125 : _PUBLIC_ enum ndr_err_code ndr_push_relative_ptr2_end(struct ndr_push *ndr, const void *p)
    1828             : {
    1829    19284125 :         uint32_t begin_offset = 0xFFFFFFFF;
    1830      721124 :         ssize_t len;
    1831    19284125 :         uint32_t correct_offset = 0;
    1832    19284125 :         uint32_t align = 1;
    1833    19284125 :         uint32_t pad = 0;
    1834             : 
    1835    19284125 :         if (p == NULL) {
    1836           0 :                 return NDR_ERR_SUCCESS;
    1837             :         }
    1838             : 
    1839    19284125 :         if (!(ndr->flags & LIBNDR_FLAG_RELATIVE_REVERSE)) {
    1840    18425960 :                 return NDR_ERR_SUCCESS;
    1841             :         }
    1842             : 
    1843      137041 :         if (ndr->flags & LIBNDR_FLAG_NO_NDR_SIZE) {
    1844             :                 /* better say more than calculation a too small buffer */
    1845      363232 :                 NDR_PUSH_ALIGN(ndr, 8);
    1846       91544 :                 return NDR_ERR_SUCCESS;
    1847             :         }
    1848             : 
    1849       45497 :         if (ndr->relative_end_offset < ndr->offset) {
    1850           0 :                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
    1851             :                                       "ndr_push_relative_ptr2_end:"
    1852             :                                       "relative_end_offset %"PRIu32" < offset %"PRIu32,
    1853             :                                       ndr->relative_end_offset, ndr->offset);
    1854             :         }
    1855             : 
    1856       45497 :         NDR_CHECK(ndr_token_retrieve(&ndr->relative_begin_list, p, &begin_offset));
    1857             : 
    1858             :         /* we have marshalled a buffer, see how long it was */
    1859       45497 :         len = ndr->offset - begin_offset;
    1860             : 
    1861       45497 :         if (len < 0) {
    1862           0 :                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
    1863             :                                       "ndr_push_relative_ptr2_end:"
    1864             :                                       "offset %"PRIu32" - begin_offset %"PRIu32" < 0",
    1865             :                                       ndr->offset, begin_offset);
    1866             :         }
    1867             : 
    1868       45497 :         if (ndr->relative_end_offset < len) {
    1869           0 :                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
    1870             :                                       "ndr_push_relative_ptr2_end:"
    1871             :                                       "relative_end_offset %"PRIu32" < len %zd",
    1872             :                                       ndr->offset, len);
    1873             :         }
    1874             : 
    1875             :         /* the reversed offset is at the end of the main buffer */
    1876       45497 :         correct_offset = ndr->relative_end_offset - len;
    1877             : 
    1878       45497 :         if (ndr->flags & LIBNDR_FLAG_NOALIGN) {
    1879           0 :                 align = 1;
    1880       45497 :         } else if (ndr->flags & LIBNDR_FLAG_ALIGN2) {
    1881       36946 :                 align = 2;
    1882        8551 :         } else if (ndr->flags & LIBNDR_FLAG_ALIGN4) {
    1883        2139 :                 align = 4;
    1884        6412 :         } else if (ndr->flags & LIBNDR_FLAG_ALIGN8) {
    1885           0 :                 align = 8;
    1886             :         }
    1887             : 
    1888       45497 :         pad = ndr_align_size(correct_offset, align);
    1889       45497 :         if (pad) {
    1890        1076 :                 correct_offset += pad;
    1891        1076 :                 correct_offset -= align;
    1892             :         }
    1893             : 
    1894       45497 :         if (correct_offset < begin_offset) {
    1895           0 :                 return ndr_push_error(ndr, NDR_ERR_RELATIVE,
    1896             :                                       "ndr_push_relative_ptr2_end: "
    1897             :                                       "correct_offset %"PRIu32" < begin_offset %"PRIu32,
    1898             :                                       correct_offset, begin_offset);
    1899             :         }
    1900             : 
    1901       45497 :         if (len > 0) {
    1902       45497 :                 uint32_t clear_size = correct_offset - begin_offset;
    1903             : 
    1904       45497 :                 clear_size = MIN(clear_size, len);
    1905             : 
    1906             :                 /* now move the marshalled buffer to the end of the main buffer */
    1907       45497 :                 memmove(ndr->data + correct_offset, ndr->data + begin_offset, len);
    1908             : 
    1909       45497 :                 if (clear_size) {
    1910             :                         /* and wipe out old buffer within the main buffer */
    1911       43837 :                         memset(ndr->data + begin_offset, '\0', clear_size);
    1912             :                 }
    1913             :         }
    1914             : 
    1915             :         /* and set the end offset for the next buffer */
    1916       45497 :         ndr->relative_end_offset = correct_offset;
    1917             : 
    1918             :         /* finally write the offset to the main buffer */
    1919       45497 :         ndr->offset = correct_offset;
    1920       45497 :         NDR_CHECK(ndr_push_relative_ptr2(ndr, p));
    1921             : 
    1922             :         /* restore to where we were in the main buffer */
    1923       45497 :         ndr->offset = begin_offset;
    1924             : 
    1925       45497 :         return NDR_ERR_SUCCESS;
    1926             : }
    1927             : 
    1928             : /*
    1929             :   get the current base for relative pointers for the pull
    1930             : */
    1931       73385 : _PUBLIC_ uint32_t ndr_pull_get_relative_base_offset(struct ndr_pull *ndr)
    1932             : {
    1933       73385 :         return ndr->relative_base_offset;
    1934             : }
    1935             : 
    1936             : /*
    1937             :   restore the old base for relative pointers for the pull
    1938             : */
    1939       73385 : _PUBLIC_ void ndr_pull_restore_relative_base_offset(struct ndr_pull *ndr, uint32_t offset)
    1940             : {
    1941       73385 :         ndr->relative_base_offset = offset;
    1942       73385 : }
    1943             : 
    1944             : /*
    1945             :   setup the current base for relative pointers for the pull
    1946             :   called in the NDR_SCALAR stage
    1947             : */
    1948       38774 : _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset1(struct ndr_pull *ndr, const void *p, uint32_t offset)
    1949             : {
    1950          65 :         enum ndr_err_code ret;
    1951       38774 :         ndr->relative_base_offset = offset;
    1952       38774 :         ret = ndr_token_store(ndr, &ndr->relative_base_list, p, offset);
    1953       38774 :         if (ret == NDR_ERR_RANGE) {
    1954           0 :                 return ndr_pull_error(ndr, ret,
    1955             :                                       "More than %d NDR tokens stored for relative_base_list",
    1956             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1957             :         }
    1958       38709 :         return ret;
    1959             : }
    1960             : 
    1961             : /*
    1962             :   setup the current base for relative pointers for the pull
    1963             :   called in the NDR_BUFFERS stage
    1964             : */
    1965       38668 : _PUBLIC_ enum ndr_err_code ndr_pull_setup_relative_base_offset2(struct ndr_pull *ndr, const void *p)
    1966             : {
    1967       38668 :         return ndr_token_retrieve(&ndr->relative_base_list, p, &ndr->relative_base_offset);
    1968             : }
    1969             : 
    1970             : /*
    1971             :   pull a relative object - stage1
    1972             :   called during SCALARS processing
    1973             : */
    1974    46052751 : _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr1(struct ndr_pull *ndr, const void *p, uint32_t rel_offset)
    1975             : {
    1976     1958448 :         enum ndr_err_code ret;
    1977    46052751 :         rel_offset += ndr->relative_base_offset;
    1978    46052751 :         if (rel_offset > ndr->data_size) {
    1979           0 :                 return ndr_pull_error(ndr, NDR_ERR_BUFSIZE,
    1980             :                                       "ndr_pull_relative_ptr1 rel_offset(%"PRIu32") > ndr->data_size(%"PRIu32")",
    1981             :                                       rel_offset, ndr->data_size);
    1982             :         }
    1983    46052751 :         ret = ndr_token_store(ndr, &ndr->relative_list, p, rel_offset);
    1984    46052751 :         if (ret == NDR_ERR_RANGE) {
    1985           0 :                 return ndr_pull_error(ndr, ret,
    1986             :                                       "More than %d NDR tokens stored for relative_list",
    1987             :                                       NDR_TOKEN_MAX_LIST_SIZE);
    1988             :         }
    1989    44094303 :         return ret;
    1990             : }
    1991             : 
    1992             : /*
    1993             :   pull a relative object - stage2
    1994             :   called during BUFFERS processing
    1995             : */
    1996    46052751 : _PUBLIC_ enum ndr_err_code ndr_pull_relative_ptr2(struct ndr_pull *ndr, const void *p)
    1997             : {
    1998     1958448 :         uint32_t rel_offset;
    1999    46052751 :         NDR_CHECK(ndr_token_retrieve(&ndr->relative_list, p, &rel_offset));
    2000    46052751 :         return ndr_pull_set_offset(ndr, rel_offset);
    2001             : }
    2002             : 
    2003             : static const struct {
    2004             :         enum ndr_err_code err;
    2005             :         const char *string;
    2006             : } ndr_err_code_strings[] = {
    2007             :         { NDR_ERR_SUCCESS, "Success" },
    2008             :         { NDR_ERR_ARRAY_SIZE, "Bad Array Size" },
    2009             :         { NDR_ERR_BAD_SWITCH, "Bad Switch" },
    2010             :         { NDR_ERR_OFFSET, "Offset Error" },
    2011             :         { NDR_ERR_RELATIVE, "Relative Pointer Error" },
    2012             :         { NDR_ERR_CHARCNV, "Character Conversion Error" },
    2013             :         { NDR_ERR_LENGTH, "Length Error" },
    2014             :         { NDR_ERR_SUBCONTEXT, "Subcontext Error" },
    2015             :         { NDR_ERR_COMPRESSION, "Compression Error" },
    2016             :         { NDR_ERR_STRING, "String Error" },
    2017             :         { NDR_ERR_VALIDATE, "Validate Error" },
    2018             :         { NDR_ERR_BUFSIZE, "Buffer Size Error" },
    2019             :         { NDR_ERR_ALLOC, "Allocation Error" },
    2020             :         { NDR_ERR_RANGE, "Range Error" },
    2021             :         { NDR_ERR_TOKEN, "Token Error" },
    2022             :         { NDR_ERR_IPV4ADDRESS, "IPv4 Address Error" },
    2023             :         { NDR_ERR_INVALID_POINTER, "Invalid Pointer" },
    2024             :         { NDR_ERR_UNREAD_BYTES, "Unread Bytes" },
    2025             :         { NDR_ERR_NDR64, "NDR64 assertion error" },
    2026             :         { NDR_ERR_INCOMPLETE_BUFFER, "Incomplete Buffer" },
    2027             :         { NDR_ERR_MAX_RECURSION_EXCEEDED, "Maximum Recursion Exceeded" },
    2028             :         { NDR_ERR_UNDERFLOW, "Underflow" },
    2029             :         { 0, NULL }
    2030             : };
    2031             : 
    2032        1336 : _PUBLIC_ const char *ndr_map_error2string(enum ndr_err_code ndr_err)
    2033             : {
    2034         101 :         int i;
    2035       20553 :         for (i = 0; ndr_err_code_strings[i].string != NULL; i++) {
    2036       20289 :                 if (ndr_err_code_strings[i].err == ndr_err)
    2037        1072 :                         return ndr_err_code_strings[i].string;
    2038             :         }
    2039         264 :         return "Unknown error";
    2040             : }

Generated by: LCOV version 1.14