1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 /* 27 * Network Data Representation (NDR) is a compatible subset of the DCE RPC 28 * and MSRPC NDR. NDR is used to move parameters consisting of 29 * complicated trees of data constructs between an RPC client and server. 30 */ 31 32 #include <sys/byteorder.h> 33 #include <strings.h> 34 #include <assert.h> 35 #include <string.h> 36 #include <stdlib.h> 37 38 #include <smbsrv/libsmb.h> 39 #include <smbsrv/string.h> 40 #include <smbsrv/libmlrpc.h> 41 42 #define NDR_STRING_MAX 4096 43 44 #define NDR_IS_UNION(T) \ 45 (((T)->type_flags & NDR_F_TYPEOP_MASK) == NDR_F_UNION) 46 #define NDR_IS_STRING(T) \ 47 (((T)->type_flags & NDR_F_TYPEOP_MASK) == NDR_F_STRING) 48 49 extern ndr_typeinfo_t ndt_s_wchar; 50 51 /* 52 * The following synopsis describes the terms TOP-MOST, OUTER and INNER. 53 * 54 * Each parameter (call arguments and return values) is a TOP-MOST item. 55 * A TOP-MOST item consists of one or more OUTER items. An OUTER item 56 * consists of one or more INNER items. There are important differences 57 * between each kind, which, primarily, have to do with the allocation 58 * of memory to contain data structures and the order of processing. 59 * 60 * This is most easily demonstrated with a short example. 61 * Consider these structures: 62 * 63 * struct top_param { 64 * long level; 65 * struct list * head; 66 * long count; 67 * }; 68 * 69 * struct list { 70 * struct list * next; 71 * char * str; // a string 72 * }; 73 * 74 * Now, consider an instance tree like this: 75 * 76 * +---------+ +-------+ +-------+ 77 * |top_param| +--->|list #1| +--->|list #2| 78 * +---------+ | +-------+ | +-------+ 79 * | level | | | next ----+ | next --->(NULL) 80 * | head ----+ | str -->"foo" | str -->"bar" 81 * | count | | flag | | flag | 82 * +---------+ +-------+ +-------+ 83 * 84 * The DCE(MS)/RPC Stub Data encoding for the tree is the following. 85 * The vertical bars (|) indicate OUTER construct boundaries. 86 * 87 * +-----+----------------------+----------------------+-----+-----+-----+ 88 * |level|#1.next #1.str #1.flag|#2.next #2.str #2.flag|"bar"|"foo"|count| 89 * +-----+----------------------+----------------------+-----+-----+-----+ 90 * level |<----------------------- head -------------------------->|count 91 * TOP TOP TOP 92 * 93 * Here's what to notice: 94 * 95 * - The members of the TOP-MOST construct are scattered through the Stub 96 * Data in the order they occur. This example shows a TOP-MOST construct 97 * consisting of atomic types (pointers and integers). A construct 98 * (struct) within the TOP-MOST construct would be contiguous and not 99 * scattered. 100 * 101 * - The members of OUTER constructs are contiguous, which allows for 102 * non-copied relocated (fixed-up) data structures at the packet's 103 * destination. We don't do fix-ups here. The pointers within the 104 * OUTER constructs are processed depth-first in the order that they 105 * occur. If they were processed breadth first, the sequence would 106 * be #1,"foo",#2,"bar". This is tricky because OUTER constructs may 107 * be variable length, and pointers are often encountered before the 108 * size(s) is known. 109 * 110 * - The INNER constructs are simply the members of an OUTER construct. 111 * 112 * For comparison, consider how ONC RPC would handle the same tree of 113 * data. ONC requires very little buffering, while DCE requires enough 114 * buffer space for the entire message. ONC does atom-by-atom depth-first 115 * (de)serialization and copy, while DCE allows for constructs to be 116 * "fixed-up" (relocated) in place at the destination. The packet data 117 * for the same tree processed by ONC RPC would look like this: 118 * 119 * +---------------------------------------------------------------------+ 120 * |level #1.next #2.next #2.str "bar" #2.flag #1.str "foo" #1.flag count| 121 * +---------------------------------------------------------------------+ 122 * TOP #1 #2 #2 bar #2 #1 foo #1 TOP 123 * 124 * More details about each TOP-MOST, OUTER, and INNER constructs appear 125 * throughout this source file near where such constructs are processed. 126 * 127 * NDR_REFERENCE 128 * 129 * The primary object for NDR is the ndr_ref_t. 130 * 131 * An ndr reference indicates the local datum (i.e. native "C" data 132 * format), and the element within the Stub Data (contained within the 133 * RPC PDU (protocol data unit). An ndr reference also indicates, 134 * largely as a debugging aid, something about the type of the 135 * element/datum, and the enclosing construct for the element. The 136 * ndr reference's are typically allocated on the stack as locals, 137 * and the chain of ndr-reference.enclosing references is in reverse 138 * order of the call graph. 139 * 140 * The ndr-reference.datum is a pointer to the local memory that 141 * contains/receives the value. The ndr-reference.pdu_offset indicates 142 * where in the Stub Data the value is to be stored/retrieved. 143 * 144 * The ndr-reference also contains various parameters to the NDR 145 * process, such as ndr-reference.size_is, which indicates the size 146 * of variable length data, or ndr-reference.switch_is, which 147 * indicates the arm of a union to use. 148 * 149 * QUEUE OF OUTER REFERENCES 150 * 151 * Some OUTER constructs are variable size. Sometimes (often) we don't 152 * know the size of the OUTER construct until after pointers have been 153 * encountered. Hence, we can not begin processing the referent of the 154 * pointer until after the referring OUTER construct is completely 155 * processed, i.e. we don't know where to find/put the referent in the 156 * Stub Data until we know the size of all its predecessors. 157 * 158 * This is managed using the queue of OUTER references. The queue is 159 * anchored in ndr_stream.outer_queue_head. At any time, 160 * ndr_stream.outer_queue_tailp indicates where to put the 161 * ndr-reference for the next encountered pointer. 162 * 163 * Refer to the example above as we illustrate the queue here. In these 164 * illustrations, the queue entries are not the data structures themselves. 165 * Rather, they are ndr-reference entries which **refer** to the data 166 * structures in both the PDU and local memory. 167 * 168 * During some point in the processing, the queue looks like this: 169 * 170 * outer_current -------v 171 * outer_queue_head --> list#1 --0 172 * outer_queue_tailp ---------& 173 * 174 * When the pointer #1.next is encountered, and entry is added to the 175 * queue, 176 * 177 * outer_current -------v 178 * outer_queue_head --> list#1 --> list#2 --0 179 * outer_queue_tailp --------------------& 180 * 181 * and the members of #1 continue to be processed, which encounters 182 * #1.str: 183 * 184 * outer_current -------v 185 * outer_queue_head --> list#1 --> list#2 --> "foo" --0 186 * outer_queue_tailp ------------------------------& 187 * 188 * Upon the completion of list#1, the processing continues by moving to 189 * ndr_stream.outer_current->next, and the tail is set to this outer member: 190 * 191 * outer_current ------------------v 192 * outer_queue_head --> list#1 --> list#2 --> "foo" --0 193 * outer_queue_tailp --------------------& 194 * 195 * Space for list#2 is allocated, either in the Stub Data or of local 196 * memory. When #2.next is encountered, it is found to be the null 197 * pointer and no reference is added to the queue. When #2.str is 198 * encountered, it is found to be valid, and a reference is added: 199 * 200 * outer_current ------------------v 201 * outer_queue_head --> list#1 --> list#2 --> "bar" --> "foo" --0 202 * outer_queue_tailp ------------------------------& 203 * 204 * Processing continues in a similar fashion with the string "bar", 205 * which is variable-length. At this point, memory for "bar" may be 206 * malloc()ed during NDR_M_OP_UNMARSHALL: 207 * 208 * outer_current -----------------------------v 209 * outer_queue_head --> list#1 --> list#2 --> "bar" --> "foo" --0 210 * outer_queue_tailp ------------------------------& 211 * 212 * And finishes on string "foo". Notice that because "bar" is a 213 * variable length string, and we don't know the PDU offset for "foo" 214 * until we reach this point. 215 * 216 * When the queue is drained (current->next==0), processing continues 217 * with the next TOP-MOST member. 218 * 219 * The queue of OUTER constructs manages the variable-length semantics 220 * of OUTER constructs and satisfies the depth-first requirement. 221 * We allow the queue to linger until the entire TOP-MOST structure is 222 * processed as an aid to debugging. 223 */ 224 225 static ndr_ref_t *ndr_enter_outer_queue(ndr_ref_t *); 226 extern int ndr__ulong(ndr_ref_t *); 227 228 /* 229 * TOP-MOST ELEMENTS 230 * 231 * This is fundamentally the first OUTER construct of the parameter, 232 * possibly followed by more OUTER constructs due to pointers. The 233 * datum (local memory) for TOP-MOST constructs (structs) is allocated 234 * by the caller of NDR. 235 * 236 * After the element is transferred, the outer_queue is drained. 237 * 238 * All we have to do is add an entry to the outer_queue for this 239 * top-most member, and commence the outer_queue processing. 240 */ 241 int 242 ndo_process(ndr_stream_t *nds, ndr_typeinfo_t *ti, char *datum) 243 { 244 ndr_ref_t myref; 245 246 bzero(&myref, sizeof (myref)); 247 myref.stream = nds; 248 myref.datum = datum; 249 myref.name = "PROCESS"; 250 myref.ti = ti; 251 252 return (ndr_topmost(&myref)); 253 } 254 255 int 256 ndo_operation(ndr_stream_t *nds, ndr_typeinfo_t *ti, int opnum, char *datum) 257 { 258 ndr_ref_t myref; 259 260 bzero(&myref, sizeof (myref)); 261 myref.stream = nds; 262 myref.datum = datum; 263 myref.name = "OPERATION"; 264 myref.ti = ti; 265 myref.inner_flags = NDR_F_SWITCH_IS; 266 myref.switch_is = opnum; 267 268 if (ti->type_flags != NDR_F_INTERFACE) { 269 NDR_SET_ERROR(&myref, NDR_ERR_NOT_AN_INTERFACE); 270 return (0); 271 } 272 273 return ((*ti->ndr_func)(&myref)); 274 } 275 276 int 277 ndr_params(ndr_ref_t *params_ref) 278 { 279 ndr_typeinfo_t *ti = params_ref->ti; 280 281 if (ti->type_flags == NDR_F_OPERATION) 282 return (*ti->ndr_func) (params_ref); 283 else 284 return (ndr_topmost(params_ref)); 285 } 286 287 int 288 ndr_topmost(ndr_ref_t *top_ref) 289 { 290 ndr_stream_t *nds; 291 ndr_typeinfo_t *ti; 292 ndr_ref_t *outer_ref = 0; 293 int is_varlen; 294 int is_string; 295 int error; 296 int rc; 297 unsigned n_fixed; 298 int params; 299 300 assert(top_ref); 301 assert(top_ref->stream); 302 assert(top_ref->ti); 303 304 nds = top_ref->stream; 305 ti = top_ref->ti; 306 307 is_varlen = ti->pdu_size_variable_part; 308 is_string = NDR_IS_STRING(ti); 309 310 assert(nds->outer_queue_tailp && !*nds->outer_queue_tailp); 311 assert(!nds->outer_current); 312 313 params = top_ref->inner_flags & NDR_F_PARAMS_MASK; 314 315 switch (params) { 316 case NDR_F_NONE: 317 case NDR_F_SWITCH_IS: 318 if (is_string || is_varlen) { 319 error = NDR_ERR_TOPMOST_VARLEN_ILLEGAL; 320 NDR_SET_ERROR(outer_ref, error); 321 return (0); 322 } 323 n_fixed = ti->pdu_size_fixed_part; 324 break; 325 326 case NDR_F_SIZE_IS: 327 error = NDR_ERR_TOPMOST_VARLEN_ILLEGAL; 328 NDR_SET_ERROR(outer_ref, error); 329 return (0); 330 331 case NDR_F_DIMENSION_IS: 332 if (is_varlen) { 333 error = NDR_ERR_ARRAY_VARLEN_ILLEGAL; 334 NDR_SET_ERROR(outer_ref, error); 335 return (0); 336 } 337 n_fixed = ti->pdu_size_fixed_part * top_ref->dimension_is; 338 break; 339 340 case NDR_F_IS_POINTER: 341 case NDR_F_IS_POINTER+NDR_F_SIZE_IS: 342 n_fixed = 4; 343 break; 344 345 case NDR_F_IS_REFERENCE: 346 case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS: 347 n_fixed = 0; 348 break; 349 350 default: 351 error = NDR_ERR_OUTER_PARAMS_BAD; 352 NDR_SET_ERROR(outer_ref, error); 353 return (0); 354 } 355 356 outer_ref = ndr_enter_outer_queue(top_ref); 357 if (!outer_ref) 358 return (0); /* error already set */ 359 360 /* 361 * Hand-craft the first OUTER construct and directly call 362 * ndr_inner(). Then, run the outer_queue. We do this 363 * because ndr_outer() wants to malloc() memory for 364 * the construct, and we already have the memory. 365 */ 366 367 /* move the flags, etc, around again, undoes enter_outer_queue() */ 368 outer_ref->inner_flags = top_ref->inner_flags; 369 outer_ref->outer_flags = 0; 370 outer_ref->datum = top_ref->datum; 371 372 /* All outer constructs start on a mod4 (longword) boundary */ 373 if (!ndr_outer_align(outer_ref)) 374 return (0); /* error already set */ 375 376 /* Regardless of what it is, this is where it starts */ 377 outer_ref->pdu_offset = nds->pdu_scan_offset; 378 379 rc = ndr_outer_grow(outer_ref, n_fixed); 380 if (!rc) 381 return (0); /* error already set */ 382 383 outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_fixed; 384 385 /* set-up outer_current, as though run_outer_queue() was doing it */ 386 nds->outer_current = outer_ref; 387 nds->outer_queue_tailp = &nds->outer_current->next; 388 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 389 390 /* do the topmost member */ 391 rc = ndr_inner(outer_ref); 392 if (!rc) 393 return (0); /* error already set */ 394 395 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 396 397 /* advance, as though run_outer_queue() was doing it */ 398 nds->outer_current = nds->outer_current->next; 399 return (ndr_run_outer_queue(nds)); 400 } 401 402 static ndr_ref_t * 403 ndr_enter_outer_queue(ndr_ref_t *arg_ref) 404 { 405 ndr_stream_t *nds = arg_ref->stream; 406 ndr_ref_t *outer_ref; 407 408 /*LINTED E_BAD_PTR_CAST_ALIGN*/ 409 outer_ref = (ndr_ref_t *)NDS_MALLOC(nds, sizeof (*outer_ref), arg_ref); 410 if (!outer_ref) { 411 NDR_SET_ERROR(arg_ref, NDR_ERR_MALLOC_FAILED); 412 return (0); 413 } 414 415 *outer_ref = *arg_ref; 416 417 /* move advice in inner_flags to outer_flags */ 418 outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK; 419 outer_ref->inner_flags = 0; 420 outer_ref->enclosing = nds->outer_current; 421 outer_ref->backptr = 0; 422 outer_ref->datum = 0; 423 424 assert(nds->outer_queue_tailp); 425 426 outer_ref->next = *nds->outer_queue_tailp; 427 *nds->outer_queue_tailp = outer_ref; 428 nds->outer_queue_tailp = &outer_ref->next; 429 return (outer_ref); 430 } 431 432 int 433 ndr_run_outer_queue(ndr_stream_t *nds) 434 { 435 while (nds->outer_current) { 436 nds->outer_queue_tailp = &nds->outer_current->next; 437 438 if (!ndr_outer(nds->outer_current)) 439 return (0); 440 441 nds->outer_current = nds->outer_current->next; 442 } 443 444 return (1); 445 } 446 447 /* 448 * OUTER CONSTRUCTS 449 * 450 * OUTER constructs are where the real work is, which stems from the 451 * variable-length potential. 452 * 453 * DCE(MS)/RPC VARIABLE LENGTH -- CONFORMANT, VARYING, VARYING/CONFORMANT 454 * 455 * DCE(MS)/RPC provides for three forms of variable length: CONFORMANT, 456 * VARYING, and VARYING/CONFORMANT. 457 * 458 * What makes this so tough is that the variable-length array may be well 459 * encapsulated within the outer construct. Further, because DCE(MS)/RPC 460 * tries to keep the constructs contiguous in the data stream, the sizing 461 * information precedes the entire OUTER construct. The sizing information 462 * must be used at the appropriate time, which can be after many, many, 463 * many fixed-length elements. During IDL type analysis, we know in 464 * advance constructs that encapsulate variable-length constructs. So, 465 * we know when we have a sizing header and when we don't. The actual 466 * semantics of the header are largely deferred. 467 * 468 * Currently, VARYING constructs are not implemented but they are described 469 * here in case they have to be implemented in the future. Similarly, 470 * DCE(MS)/RPC provides for multi-dimensional arrays, which are currently 471 * not implemented. Only one-dimensional, variable-length arrays are 472 * supported. 473 * 474 * CONFORMANT CONSTRUCTS -- VARIABLE LENGTH ARRAYS START THE SHOW 475 * 476 * All variable-length values are arrays. These arrays may be embedded 477 * well within another construct. However, a variable-length construct 478 * may ONLY appear as the last member of an enclosing construct. Example: 479 * 480 * struct credentials { 481 * ulong uid, gid; 482 * ulong n_gids; 483 * [size_is(n_gids)] 484 * ulong gids[*]; // variable-length. 485 * }; 486 * 487 * CONFORMANT constructs have a dynamic size in local memory and in the 488 * PDU. The CONFORMANT quality is indicated by the [size_is()] advice. 489 * CONFORMANT constructs have the following header: 490 * 491 * struct conformant_header { 492 * ulong size_is; 493 * }; 494 * 495 * (Multi-dimensional CONFORMANT arrays have a similar header for each 496 * dimension - not implemented). 497 * 498 * Example CONFORMANT construct: 499 * 500 * struct user { 501 * char * name; 502 * struct credentials cred; // see above 503 * }; 504 * 505 * Consider the data tree: 506 * 507 * +--------+ 508 * | user | 509 * +--------+ 510 * | name ----> "fred" (the string is a different OUTER) 511 * | uid | 512 * | gid | 513 * | n_gids | for example, 3 514 * | gids[0]| 515 * | gids[1]| 516 * | gids[2]| 517 * +--------+ 518 * 519 * The OUTER construct in the Stub Data would be: 520 * 521 * +---+---------+---------------------------------------------+ 522 * |pad|size_is=3 name uid gid n_gids=3 gids[0] gids[1] gids[2]| 523 * +---+---------+---------------------------------------------+ 524 * szing hdr|user |<-------------- user.cred ------------>| 525 * |<--- fixed-size ---->|<----- conformant ---->| 526 * 527 * The ndr_typeinfo for struct user will have: 528 * pdu_fixed_size_part = 16 four long words (name uid gid n_gids) 529 * pdu_variable_size_part = 4 per element, sizeof gids[0] 530 * 531 * VARYING CONSTRUCTS -- NOT IMPLEMENTED 532 * 533 * VARYING constructs have the following header: 534 * 535 * struct varying_header { 536 * ulong first_is; 537 * ulong length_is; 538 * }; 539 * 540 * This indicates which interval of an array is significant. 541 * Non-intersecting elements of the array are undefined and usually 542 * zero-filled. The first_is parameter for C arrays is always 0 for 543 * the first element. 544 * 545 * N.B. Constructs may contain one CONFORMANT element, which is always 546 * last, but may contain many VARYING elements, which can be anywhere. 547 * 548 * VARYING CONFORMANT constructs have the sizing headers arranged like 549 * this: 550 * 551 * struct conformant_header all_conformant[N_CONFORMANT_DIM]; 552 * struct varying_header all_varying[N_VARYING_ELEMS_AND_DIMS]; 553 * 554 * The sizing header is immediately followed by the values for the 555 * construct. Again, we don't support more than one dimension and 556 * we don't support VARYING constructs at this time. 557 * 558 * A good example of a VARYING/CONFORMANT data structure is the UNIX 559 * directory entry: 560 * 561 * struct dirent { 562 * ushort reclen; 563 * ushort namlen; 564 * ulong inum; 565 * [size_is(reclen-8) length_is(namlen+1)] // -(2+2+4), +1 for NUL 566 * uchar name[*]; 567 * }; 568 * 569 * 570 * STRINGS ARE A SPECIAL CASE 571 * 572 * Strings are handled specially. MS/RPC uses VARYING/CONFORMANT structures 573 * for strings. This is a simple one-dimensional variable-length array, 574 * typically with its last element all zeroes. We handle strings with the 575 * header: 576 * 577 * struct string_header { 578 * ulong size_is; 579 * ulong first_is; // always 0 580 * ulong length_is; // always same as size_is 581 * }; 582 * 583 * If general support for VARYING and VARYING/CONFORMANT mechanisms is 584 * implemented, we probably won't need the strings special case. 585 */ 586 int 587 ndr_outer(ndr_ref_t *outer_ref) 588 { 589 ndr_stream_t *nds = outer_ref->stream; 590 ndr_typeinfo_t *ti = outer_ref->ti; 591 int is_varlen = ti->pdu_size_variable_part; 592 int is_union = NDR_IS_UNION(ti); 593 int is_string = NDR_IS_STRING(ti); 594 int error = NDR_ERR_OUTER_PARAMS_BAD; 595 int params; 596 597 params = outer_ref->outer_flags & NDR_F_PARAMS_MASK; 598 599 NDR_TATTLE(outer_ref, "--OUTER--"); 600 601 /* All outer constructs start on a mod4 (longword) boundary */ 602 if (!ndr_outer_align(outer_ref)) 603 return (0); /* error already set */ 604 605 /* Regardless of what it is, this is where it starts */ 606 outer_ref->pdu_offset = nds->pdu_scan_offset; 607 608 if (is_union) { 609 error = NDR_ERR_OUTER_UNION_ILLEGAL; 610 NDR_SET_ERROR(outer_ref, error); 611 return (0); 612 } 613 614 switch (params) { 615 case NDR_F_NONE: 616 if (is_string) 617 return (ndr_outer_string(outer_ref)); 618 if (is_varlen) 619 return (ndr_outer_conformant_construct(outer_ref)); 620 621 return (ndr_outer_fixed(outer_ref)); 622 break; 623 624 case NDR_F_SIZE_IS: 625 case NDR_F_DIMENSION_IS: 626 case NDR_F_IS_POINTER+NDR_F_SIZE_IS: 627 case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS: 628 if (is_varlen) { 629 error = NDR_ERR_ARRAY_VARLEN_ILLEGAL; 630 break; 631 } 632 633 if (params & NDR_F_SIZE_IS) 634 return (ndr_outer_conformant_array(outer_ref)); 635 else 636 return (ndr_outer_fixed_array(outer_ref)); 637 break; 638 639 default: 640 error = NDR_ERR_OUTER_PARAMS_BAD; 641 break; 642 } 643 644 /* 645 * If we get here, something is wrong. Most likely, 646 * the params flags do not match. 647 */ 648 NDR_SET_ERROR(outer_ref, error); 649 return (0); 650 } 651 652 int 653 ndr_outer_fixed(ndr_ref_t *outer_ref) 654 { 655 ndr_stream_t *nds = outer_ref->stream; 656 ndr_typeinfo_t *ti = outer_ref->ti; 657 ndr_ref_t myref; 658 char *valp = NULL; 659 int is_varlen = ti->pdu_size_variable_part; 660 int is_union = NDR_IS_UNION(ti); 661 int is_string = NDR_IS_STRING(ti); 662 int rc; 663 unsigned n_hdr; 664 unsigned n_fixed; 665 unsigned n_variable; 666 unsigned n_alloc; 667 unsigned n_pdu_total; 668 int params; 669 670 params = outer_ref->outer_flags & NDR_F_PARAMS_MASK; 671 672 assert(!is_varlen && !is_string && !is_union); 673 assert(params == NDR_F_NONE); 674 675 /* no header for this */ 676 n_hdr = 0; 677 678 /* fixed part -- exactly one of these */ 679 n_fixed = ti->pdu_size_fixed_part; 680 assert(n_fixed > 0); 681 682 /* variable part -- exactly none of these */ 683 n_variable = 0; 684 685 /* sum them up to determine the PDU space required */ 686 n_pdu_total = n_hdr + n_fixed + n_variable; 687 688 /* similar sum to determine how much local memory is required */ 689 n_alloc = n_fixed + n_variable; 690 691 rc = ndr_outer_grow(outer_ref, n_pdu_total); 692 if (!rc) 693 return (rc); /* error already set */ 694 695 switch (nds->m_op) { 696 case NDR_M_OP_MARSHALL: 697 valp = outer_ref->datum; 698 assert(valp); 699 if (outer_ref->backptr) 700 assert(valp == *outer_ref->backptr); 701 break; 702 703 case NDR_M_OP_UNMARSHALL: 704 valp = NDS_MALLOC(nds, n_alloc, outer_ref); 705 if (!valp) { 706 NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED); 707 return (0); 708 } 709 if (outer_ref->backptr) 710 *outer_ref->backptr = valp; 711 outer_ref->datum = valp; 712 break; 713 714 default: 715 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 716 return (0); 717 } 718 719 bzero(&myref, sizeof (myref)); 720 myref.stream = nds; 721 myref.enclosing = outer_ref; 722 myref.ti = outer_ref->ti; 723 myref.datum = outer_ref->datum; 724 myref.name = "FIXED-VALUE"; 725 myref.outer_flags = NDR_F_NONE; 726 myref.inner_flags = NDR_F_NONE; 727 728 myref.pdu_offset = outer_ref->pdu_offset; 729 outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total; 730 731 rc = ndr_inner(&myref); 732 if (!rc) 733 return (rc); /* error already set */ 734 735 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 736 return (1); 737 } 738 739 int 740 ndr_outer_fixed_array(ndr_ref_t *outer_ref) 741 { 742 ndr_stream_t *nds = outer_ref->stream; 743 ndr_typeinfo_t *ti = outer_ref->ti; 744 ndr_ref_t myref; 745 char *valp = NULL; 746 int is_varlen = ti->pdu_size_variable_part; 747 int is_union = NDR_IS_UNION(ti); 748 int is_string = NDR_IS_STRING(ti); 749 int rc; 750 unsigned n_hdr; 751 unsigned n_fixed; 752 unsigned n_variable; 753 unsigned n_alloc; 754 unsigned n_pdu_total; 755 int params; 756 757 params = outer_ref->outer_flags & NDR_F_PARAMS_MASK; 758 759 assert(!is_varlen && !is_string && !is_union); 760 assert(params == NDR_F_DIMENSION_IS); 761 762 /* no header for this */ 763 n_hdr = 0; 764 765 /* fixed part -- exactly dimension_is of these */ 766 n_fixed = ti->pdu_size_fixed_part * outer_ref->dimension_is; 767 assert(n_fixed > 0); 768 769 /* variable part -- exactly none of these */ 770 n_variable = 0; 771 772 /* sum them up to determine the PDU space required */ 773 n_pdu_total = n_hdr + n_fixed + n_variable; 774 775 /* similar sum to determine how much local memory is required */ 776 n_alloc = n_fixed + n_variable; 777 778 rc = ndr_outer_grow(outer_ref, n_pdu_total); 779 if (!rc) 780 return (rc); /* error already set */ 781 782 switch (nds->m_op) { 783 case NDR_M_OP_MARSHALL: 784 valp = outer_ref->datum; 785 assert(valp); 786 if (outer_ref->backptr) 787 assert(valp == *outer_ref->backptr); 788 break; 789 790 case NDR_M_OP_UNMARSHALL: 791 valp = NDS_MALLOC(nds, n_alloc, outer_ref); 792 if (!valp) { 793 NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED); 794 return (0); 795 } 796 if (outer_ref->backptr) 797 *outer_ref->backptr = valp; 798 outer_ref->datum = valp; 799 break; 800 801 default: 802 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 803 return (0); 804 } 805 806 bzero(&myref, sizeof (myref)); 807 myref.stream = nds; 808 myref.enclosing = outer_ref; 809 myref.ti = outer_ref->ti; 810 myref.datum = outer_ref->datum; 811 myref.name = "FIXED-ARRAY"; 812 myref.outer_flags = NDR_F_NONE; 813 myref.inner_flags = NDR_F_DIMENSION_IS; 814 myref.dimension_is = outer_ref->dimension_is; 815 816 myref.pdu_offset = outer_ref->pdu_offset; 817 outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total; 818 819 rc = ndr_inner(&myref); 820 if (!rc) 821 return (rc); /* error already set */ 822 823 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 824 return (1); 825 } 826 827 int 828 ndr_outer_conformant_array(ndr_ref_t *outer_ref) 829 { 830 ndr_stream_t *nds = outer_ref->stream; 831 ndr_typeinfo_t *ti = outer_ref->ti; 832 ndr_ref_t myref; 833 char *valp = NULL; 834 int is_varlen = ti->pdu_size_variable_part; 835 int is_union = NDR_IS_UNION(ti); 836 int is_string = NDR_IS_STRING(ti); 837 unsigned long size_is; 838 int rc; 839 unsigned n_hdr; 840 unsigned n_fixed; 841 unsigned n_variable; 842 unsigned n_alloc; 843 unsigned n_pdu_total; 844 unsigned n_ptr_offset; 845 int params; 846 847 params = outer_ref->outer_flags & NDR_F_PARAMS_MASK; 848 849 assert(!is_varlen && !is_string && !is_union); 850 assert(params & NDR_F_SIZE_IS); 851 852 /* conformant header for this */ 853 n_hdr = 4; 854 855 /* fixed part -- exactly none of these */ 856 n_fixed = 0; 857 858 /* variable part -- exactly size_of of these */ 859 /* notice that it is the **fixed** size of the ti */ 860 n_variable = ti->pdu_size_fixed_part * outer_ref->size_is; 861 862 /* sum them up to determine the PDU space required */ 863 n_pdu_total = n_hdr + n_fixed + n_variable; 864 865 /* similar sum to determine how much local memory is required */ 866 n_alloc = n_fixed + n_variable; 867 868 rc = ndr_outer_grow(outer_ref, n_pdu_total); 869 if (!rc) 870 return (rc); /* error already set */ 871 872 switch (nds->m_op) { 873 case NDR_M_OP_MARSHALL: 874 size_is = outer_ref->size_is; 875 rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is); 876 if (!rc) 877 return (0); /* error already set */ 878 879 valp = outer_ref->datum; 880 assert(valp); 881 if (outer_ref->backptr) 882 assert(valp == *outer_ref->backptr); 883 n_ptr_offset = 4; 884 break; 885 886 case NDR_M_OP_UNMARSHALL: 887 if (params & NDR_F_IS_REFERENCE) { 888 size_is = outer_ref->size_is; 889 n_ptr_offset = 0; 890 } else { 891 /* NDR_F_IS_POINTER */ 892 rc = ndr_outer_peek_sizing(outer_ref, 0, &size_is); 893 if (!rc) 894 return (0); /* error already set */ 895 896 if (size_is != outer_ref->size_is) { 897 NDR_SET_ERROR(outer_ref, 898 NDR_ERR_SIZE_IS_MISMATCH_PDU); 899 return (0); 900 } 901 902 n_ptr_offset = 4; 903 } 904 905 if (size_is > 0) { 906 valp = NDS_MALLOC(nds, n_alloc, outer_ref); 907 if (!valp) { 908 NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED); 909 return (0); 910 } 911 } 912 913 if (outer_ref->backptr) 914 *outer_ref->backptr = valp; 915 outer_ref->datum = valp; 916 break; 917 918 default: 919 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 920 return (0); 921 } 922 923 outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total; 924 outer_ref->type_flags = NDR_F_NONE; 925 outer_ref->inner_flags = NDR_F_NONE; 926 927 if (size_is > 0) { 928 bzero(&myref, sizeof (myref)); 929 myref.stream = nds; 930 myref.enclosing = outer_ref; 931 myref.ti = outer_ref->ti; 932 myref.datum = outer_ref->datum; 933 myref.name = "CONFORMANT-ARRAY"; 934 myref.outer_flags = NDR_F_NONE; 935 myref.inner_flags = NDR_F_SIZE_IS; 936 myref.size_is = outer_ref->size_is; 937 938 myref.inner_flags = NDR_F_DIMENSION_IS; /* convenient */ 939 myref.dimension_is = outer_ref->size_is; /* convenient */ 940 941 myref.pdu_offset = outer_ref->pdu_offset + n_ptr_offset; 942 943 rc = ndr_inner(&myref); 944 if (!rc) 945 return (rc); /* error already set */ 946 } 947 948 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 949 return (1); 950 } 951 952 int 953 ndr_outer_conformant_construct(ndr_ref_t *outer_ref) 954 { 955 ndr_stream_t *nds = outer_ref->stream; 956 ndr_typeinfo_t *ti = outer_ref->ti; 957 ndr_ref_t myref; 958 char *valp = NULL; 959 int is_varlen = ti->pdu_size_variable_part; 960 int is_union = NDR_IS_UNION(ti); 961 int is_string = NDR_IS_STRING(ti); 962 unsigned long size_is; 963 int rc; 964 unsigned n_hdr; 965 unsigned n_fixed; 966 unsigned n_variable; 967 unsigned n_alloc; 968 unsigned n_pdu_total; 969 int params; 970 971 params = outer_ref->outer_flags & NDR_F_PARAMS_MASK; 972 973 assert(is_varlen && !is_string && !is_union); 974 assert(params == NDR_F_NONE); 975 976 /* conformant header for this */ 977 n_hdr = 4; 978 979 /* fixed part -- exactly one of these */ 980 n_fixed = ti->pdu_size_fixed_part; 981 982 /* variable part -- exactly size_of of these */ 983 n_variable = 0; /* 0 for the moment */ 984 985 /* sum them up to determine the PDU space required */ 986 n_pdu_total = n_hdr + n_fixed + n_variable; 987 988 /* similar sum to determine how much local memory is required */ 989 n_alloc = n_fixed + n_variable; 990 991 /* For the moment, grow enough for the fixed-size part */ 992 rc = ndr_outer_grow(outer_ref, n_pdu_total); 993 if (!rc) 994 return (rc); /* error already set */ 995 996 switch (nds->m_op) { 997 case NDR_M_OP_MARSHALL: 998 /* 999 * We don't know the size yet. We have to wait for 1000 * it. Proceed with the fixed-size part, and await 1001 * the call to ndr_size_is(). 1002 */ 1003 size_is = 0; 1004 rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is); 1005 if (!rc) 1006 return (0); /* error already set */ 1007 1008 valp = outer_ref->datum; 1009 assert(valp); 1010 if (outer_ref->backptr) 1011 assert(valp == *outer_ref->backptr); 1012 break; 1013 1014 case NDR_M_OP_UNMARSHALL: 1015 /* 1016 * We know the size of the variable part because 1017 * of the CONFORMANT header. We will verify 1018 * the header against the [size_is(X)] advice 1019 * later when ndr_size_is() is called. 1020 */ 1021 rc = ndr_outer_peek_sizing(outer_ref, 0, &size_is); 1022 if (!rc) 1023 return (0); /* error already set */ 1024 1025 /* recalculate metrics */ 1026 n_variable = size_is * ti->pdu_size_variable_part; 1027 n_pdu_total = n_hdr + n_fixed + n_variable; 1028 n_alloc = n_fixed + n_variable; 1029 1030 rc = ndr_outer_grow(outer_ref, n_pdu_total); 1031 if (!rc) 1032 return (rc); /* error already set */ 1033 1034 outer_ref->size_is = size_is; /* verified later */ 1035 1036 valp = NDS_MALLOC(nds, n_alloc, outer_ref); 1037 if (!valp) { 1038 NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED); 1039 return (0); 1040 } 1041 if (outer_ref->backptr) 1042 *outer_ref->backptr = valp; 1043 outer_ref->datum = valp; 1044 break; 1045 1046 default: 1047 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 1048 return (0); 1049 } 1050 1051 outer_ref->pdu_end_offset = outer_ref->pdu_offset + n_pdu_total; 1052 outer_ref->type_flags = NDR_F_SIZE_IS; /* indicate pending */ 1053 outer_ref->inner_flags = NDR_F_NONE; /* indicate pending */ 1054 1055 bzero(&myref, sizeof (myref)); 1056 myref.stream = nds; 1057 myref.enclosing = outer_ref; 1058 myref.ti = outer_ref->ti; 1059 myref.datum = outer_ref->datum; 1060 myref.name = "CONFORMANT-CONSTRUCT"; 1061 myref.outer_flags = NDR_F_NONE; 1062 myref.inner_flags = NDR_F_NONE; 1063 myref.size_is = outer_ref->size_is; 1064 1065 myref.pdu_offset = outer_ref->pdu_offset + 4; 1066 1067 rc = ndr_inner(&myref); 1068 if (!rc) 1069 return (rc); /* error already set */ 1070 1071 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 1072 1073 if (outer_ref->inner_flags != NDR_F_SIZE_IS) { 1074 NDR_SET_ERROR(&myref, NDR_ERR_SIZE_IS_MISMATCH_AFTER); 1075 return (0); 1076 } 1077 1078 return (1); 1079 } 1080 1081 int 1082 ndr_size_is(ndr_ref_t *ref) 1083 { 1084 ndr_stream_t *nds = ref->stream; 1085 ndr_ref_t *outer_ref = nds->outer_current; 1086 ndr_typeinfo_t *ti = outer_ref->ti; 1087 unsigned long size_is; 1088 int rc; 1089 unsigned n_hdr; 1090 unsigned n_fixed; 1091 unsigned n_variable; 1092 unsigned n_pdu_total; 1093 1094 assert(ref->inner_flags & NDR_F_SIZE_IS); 1095 size_is = ref->size_is; 1096 1097 if (outer_ref->type_flags != NDR_F_SIZE_IS) { 1098 NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_UNEXPECTED); 1099 return (0); 1100 } 1101 1102 if (outer_ref->inner_flags & NDR_F_SIZE_IS) { 1103 NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_DUPLICATED); 1104 return (0); 1105 } 1106 1107 /* repeat metrics, see ndr_conformant_construct() above */ 1108 n_hdr = 4; 1109 n_fixed = ti->pdu_size_fixed_part; 1110 n_variable = size_is * ti->pdu_size_variable_part; 1111 n_pdu_total = n_hdr + n_fixed + n_variable; 1112 1113 rc = ndr_outer_grow(outer_ref, n_pdu_total); 1114 if (!rc) 1115 return (rc); /* error already set */ 1116 1117 switch (nds->m_op) { 1118 case NDR_M_OP_MARSHALL: 1119 /* 1120 * We have to set the sizing header and extend 1121 * the size of the PDU (already done). 1122 */ 1123 rc = ndr_outer_poke_sizing(outer_ref, 0, &size_is); 1124 if (!rc) 1125 return (0); /* error already set */ 1126 break; 1127 1128 case NDR_M_OP_UNMARSHALL: 1129 /* 1130 * Allocation done during ndr_conformant_construct(). 1131 * All we are doing here is verifying that the 1132 * intended size (ref->size_is) matches the sizing header. 1133 */ 1134 if (size_is != outer_ref->size_is) { 1135 NDR_SET_ERROR(ref, NDR_ERR_SIZE_IS_MISMATCH_PDU); 1136 return (0); 1137 } 1138 break; 1139 1140 default: 1141 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 1142 return (0); 1143 } 1144 1145 outer_ref->inner_flags |= NDR_F_SIZE_IS; 1146 outer_ref->size_is = ref->size_is; 1147 return (1); 1148 } 1149 1150 int 1151 ndr_outer_string(ndr_ref_t *outer_ref) 1152 { 1153 ndr_stream_t *nds = outer_ref->stream; 1154 ndr_typeinfo_t *ti = outer_ref->ti; 1155 ndr_ref_t myref; 1156 char *valp = NULL; 1157 unsigned is_varlen = ti->pdu_size_variable_part; 1158 int is_union = NDR_IS_UNION(ti); 1159 int is_string = NDR_IS_STRING(ti); 1160 int rc; 1161 unsigned n_zeroes; 1162 unsigned ix; 1163 unsigned long size_is; 1164 unsigned long first_is; 1165 unsigned long length_is; 1166 unsigned n_hdr; 1167 unsigned n_fixed; 1168 unsigned n_variable; 1169 unsigned n_alloc; 1170 unsigned n_pdu_total; 1171 int params; 1172 1173 params = outer_ref->outer_flags & NDR_F_PARAMS_MASK; 1174 1175 assert(is_varlen && is_string && !is_union); 1176 assert(params == NDR_F_NONE); 1177 1178 /* string header for this: size_is first_is length_is */ 1179 n_hdr = 12; 1180 1181 /* fixed part -- exactly none of these */ 1182 n_fixed = 0; 1183 1184 if (!ndr_outer_grow(outer_ref, n_hdr)) 1185 return (0); /* error already set */ 1186 1187 switch (nds->m_op) { 1188 case NDR_M_OP_MARSHALL: 1189 valp = outer_ref->datum; 1190 assert(valp); 1191 1192 if (outer_ref->backptr) 1193 assert(valp == *outer_ref->backptr); 1194 1195 if (ti == &ndt_s_wchar) { 1196 /* 1197 * size_is is the number of characters in the 1198 * (multibyte) string, including the null. 1199 */ 1200 size_is = smb_wcequiv_strlen(valp) / 1201 sizeof (smb_wchar_t); 1202 1203 if (!(nds->flags & NDS_F_NONULL)) 1204 ++size_is; 1205 1206 if (size_is > NDR_STRING_MAX) { 1207 NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN); 1208 return (0); 1209 } 1210 } else { 1211 valp = outer_ref->datum; 1212 n_zeroes = 0; 1213 for (ix = 0; ix < NDR_STRING_MAX; ix++) { 1214 if (valp[ix] == 0) { 1215 n_zeroes++; 1216 if (n_zeroes >= is_varlen && 1217 ix % is_varlen == 0) { 1218 break; 1219 } 1220 } else { 1221 n_zeroes = 0; 1222 } 1223 } 1224 if (ix >= NDR_STRING_MAX) { 1225 NDR_SET_ERROR(outer_ref, NDR_ERR_STRLEN); 1226 return (0); 1227 } 1228 size_is = ix+1; 1229 } 1230 1231 first_is = 0; 1232 1233 if (nds->flags & NDS_F_NOTERM) 1234 length_is = size_is - 1; 1235 else 1236 length_is = size_is; 1237 1238 if (!ndr_outer_poke_sizing(outer_ref, 0, &size_is) || 1239 !ndr_outer_poke_sizing(outer_ref, 4, &first_is) || 1240 !ndr_outer_poke_sizing(outer_ref, 8, &length_is)) 1241 return (0); /* error already set */ 1242 break; 1243 1244 case NDR_M_OP_UNMARSHALL: 1245 if (!ndr_outer_peek_sizing(outer_ref, 0, &size_is) || 1246 !ndr_outer_peek_sizing(outer_ref, 4, &first_is) || 1247 !ndr_outer_peek_sizing(outer_ref, 8, &length_is)) 1248 return (0); /* error already set */ 1249 1250 /* 1251 * In addition to the first_is check, we used to check that 1252 * size_is or size_is-1 was equal to length_is but Windows95 1253 * doesn't conform to this "rule" (see variable part below). 1254 * The srvmgr tool for Windows95 sent the following values 1255 * for a path string: 1256 * 1257 * size_is = 261 (0x105) 1258 * first_is = 0 1259 * length_is = 53 (0x35) 1260 * 1261 * The length_is was correct (for the given path) but the 1262 * size_is was the maximum path length rather than being 1263 * related to length_is. 1264 */ 1265 if (first_is != 0) { 1266 NDR_SET_ERROR(outer_ref, NDR_ERR_STRING_SIZING); 1267 return (0); 1268 } 1269 1270 if (ti == &ndt_s_wchar) { 1271 /* 1272 * Decoding Unicode to UTF-8; we need to allow 1273 * for the maximum possible char size. It would 1274 * be nice to use mbequiv_strlen but the string 1275 * may not be null terminated. 1276 */ 1277 n_alloc = (size_is + 1) * MTS_MB_CHAR_MAX; 1278 } else { 1279 n_alloc = (size_is + 1) * is_varlen; 1280 } 1281 1282 valp = NDS_MALLOC(nds, n_alloc, outer_ref); 1283 if (!valp) { 1284 NDR_SET_ERROR(outer_ref, NDR_ERR_MALLOC_FAILED); 1285 return (0); 1286 } 1287 1288 bzero(valp, (size_is+1) * is_varlen); 1289 1290 if (outer_ref->backptr) 1291 *outer_ref->backptr = valp; 1292 outer_ref->datum = valp; 1293 break; 1294 1295 default: 1296 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 1297 return (0); 1298 } 1299 1300 /* 1301 * Variable part - exactly length_is of these. 1302 * 1303 * Usually, length_is is same as size_is and includes nul. 1304 * Some protocols use length_is = size_is-1, and length_is does 1305 * not include the nul (which is more consistent with DCE spec). 1306 * If the length_is is 0, there is no data following the 1307 * sizing header, regardless of size_is. 1308 */ 1309 n_variable = length_is * is_varlen; 1310 1311 /* sum them up to determine the PDU space required */ 1312 n_pdu_total = n_hdr + n_fixed + n_variable; 1313 1314 /* similar sum to determine how much local memory is required */ 1315 n_alloc = n_fixed + n_variable; 1316 1317 rc = ndr_outer_grow(outer_ref, n_pdu_total); 1318 if (!rc) 1319 return (rc); /* error already set */ 1320 1321 if (length_is > 0) { 1322 bzero(&myref, sizeof (myref)); 1323 myref.stream = nds; 1324 myref.enclosing = outer_ref; 1325 myref.ti = outer_ref->ti; 1326 myref.datum = outer_ref->datum; 1327 myref.name = "OUTER-STRING"; 1328 myref.outer_flags = NDR_F_IS_STRING; 1329 myref.inner_flags = NDR_F_NONE; 1330 1331 /* 1332 * Set up size_is and strlen_is for ndr_s_wchar. 1333 */ 1334 myref.size_is = size_is; 1335 myref.strlen_is = length_is; 1336 } 1337 1338 myref.pdu_offset = outer_ref->pdu_offset + 12; 1339 1340 /* 1341 * Don't try to decode empty strings. 1342 */ 1343 if ((size_is == 0) && (first_is == 0) && (length_is == 0)) { 1344 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 1345 return (1); 1346 } 1347 1348 if ((size_is != 0) && (length_is != 0)) { 1349 rc = ndr_inner(&myref); 1350 if (!rc) 1351 return (rc); /* error already set */ 1352 } 1353 1354 nds->pdu_scan_offset = outer_ref->pdu_end_offset; 1355 return (1); 1356 } 1357 1358 int 1359 ndr_outer_peek_sizing(ndr_ref_t *outer_ref, unsigned offset, 1360 unsigned long *sizing_p) 1361 { 1362 ndr_stream_t *nds = outer_ref->stream; 1363 unsigned long pdu_offset; 1364 int rc; 1365 1366 pdu_offset = outer_ref->pdu_offset + offset; 1367 1368 if (pdu_offset < nds->outer_current->pdu_offset || 1369 pdu_offset > nds->outer_current->pdu_end_offset || 1370 pdu_offset+4 > nds->outer_current->pdu_end_offset) { 1371 NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK); 1372 return (0); 1373 } 1374 1375 switch (nds->m_op) { 1376 case NDR_M_OP_MARSHALL: 1377 NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED); 1378 return (0); 1379 1380 case NDR_M_OP_UNMARSHALL: 1381 rc = NDS_GET_PDU(nds, pdu_offset, 4, (char *)sizing_p, 1382 nds->swap, outer_ref); 1383 break; 1384 1385 default: 1386 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 1387 return (0); 1388 } 1389 1390 return (rc); 1391 } 1392 1393 int 1394 ndr_outer_poke_sizing(ndr_ref_t *outer_ref, unsigned offset, 1395 unsigned long *sizing_p) 1396 { 1397 ndr_stream_t *nds = outer_ref->stream; 1398 unsigned long pdu_offset; 1399 int rc; 1400 1401 pdu_offset = outer_ref->pdu_offset + offset; 1402 1403 if (pdu_offset < nds->outer_current->pdu_offset || 1404 pdu_offset > nds->outer_current->pdu_end_offset || 1405 pdu_offset+4 > nds->outer_current->pdu_end_offset) { 1406 NDR_SET_ERROR(outer_ref, NDR_ERR_BOUNDS_CHECK); 1407 return (0); 1408 } 1409 1410 switch (nds->m_op) { 1411 case NDR_M_OP_MARSHALL: 1412 rc = NDS_PUT_PDU(nds, pdu_offset, 4, (char *)sizing_p, 1413 nds->swap, outer_ref); 1414 break; 1415 1416 case NDR_M_OP_UNMARSHALL: 1417 NDR_SET_ERROR(outer_ref, NDR_ERR_UNIMPLEMENTED); 1418 return (0); 1419 1420 default: 1421 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 1422 return (0); 1423 } 1424 1425 return (rc); 1426 } 1427 1428 /* 1429 * All OUTER constructs begin on a mod4 (dword) boundary - except 1430 * for the ones that don't: some MSRPC calls appear to use word or 1431 * packed alignment. Strings appear to be dword aligned. 1432 */ 1433 int 1434 ndr_outer_align(ndr_ref_t *outer_ref) 1435 { 1436 ndr_stream_t *nds = outer_ref->stream; 1437 int rc; 1438 unsigned n_pad; 1439 unsigned align; 1440 1441 if (outer_ref->packed_alignment && outer_ref->ti != &ndt_s_wchar) { 1442 align = outer_ref->ti->alignment; 1443 n_pad = ((align + 1) - nds->pdu_scan_offset) & align; 1444 } else { 1445 n_pad = NDR_ALIGN4(nds->pdu_scan_offset); 1446 } 1447 1448 if (n_pad == 0) 1449 return (1); /* already aligned, often the case */ 1450 1451 if (!ndr_outer_grow(outer_ref, n_pad)) 1452 return (0); /* error already set */ 1453 1454 switch (nds->m_op) { 1455 case NDR_M_OP_MARSHALL: 1456 rc = NDS_PAD_PDU(nds, nds->pdu_scan_offset, n_pad, outer_ref); 1457 if (!rc) { 1458 NDR_SET_ERROR(outer_ref, NDR_ERR_PAD_FAILED); 1459 return (0); 1460 } 1461 break; 1462 1463 case NDR_M_OP_UNMARSHALL: 1464 break; 1465 1466 default: 1467 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 1468 return (0); 1469 } 1470 1471 nds->pdu_scan_offset += n_pad; 1472 return (1); 1473 } 1474 1475 int 1476 ndr_outer_grow(ndr_ref_t *outer_ref, unsigned n_total) 1477 { 1478 ndr_stream_t *nds = outer_ref->stream; 1479 unsigned long pdu_want_size; 1480 int rc, is_ok = 0; 1481 1482 pdu_want_size = nds->pdu_scan_offset + n_total; 1483 1484 if (pdu_want_size <= nds->pdu_max_size) { 1485 is_ok = 1; 1486 } 1487 1488 switch (nds->m_op) { 1489 case NDR_M_OP_MARSHALL: 1490 if (is_ok) 1491 break; 1492 rc = NDS_GROW_PDU(nds, pdu_want_size, outer_ref); 1493 if (!rc) { 1494 NDR_SET_ERROR(outer_ref, NDR_ERR_GROW_FAILED); 1495 return (0); 1496 } 1497 break; 1498 1499 case NDR_M_OP_UNMARSHALL: 1500 if (is_ok) 1501 break; 1502 NDR_SET_ERROR(outer_ref, NDR_ERR_UNDERFLOW); 1503 return (0); 1504 1505 default: 1506 NDR_SET_ERROR(outer_ref, NDR_ERR_M_OP_INVALID); 1507 return (0); 1508 } 1509 1510 if (nds->pdu_size < pdu_want_size) 1511 nds->pdu_size = pdu_want_size; 1512 1513 outer_ref->pdu_end_offset = pdu_want_size; 1514 return (1); 1515 } 1516 1517 /* 1518 * INNER ELEMENTS 1519 * 1520 * The local datum (arg_ref->datum) already exists, there is no need to 1521 * malloc() it. The datum should point at a member of a structure. 1522 * 1523 * For the most part, ndr_inner() and its helpers are just a sanity 1524 * check. The underlying ti->ndr_func() could be called immediately 1525 * for non-pointer elements. For the sake of robustness, we detect 1526 * run-time errors here. Most of the situations this protects against 1527 * have already been checked by the IDL compiler. This is also a 1528 * common point for processing of all data, and so is a convenient 1529 * place to work from for debugging. 1530 */ 1531 int 1532 ndr_inner(ndr_ref_t *arg_ref) 1533 { 1534 ndr_typeinfo_t *ti = arg_ref->ti; 1535 int is_varlen = ti->pdu_size_variable_part; 1536 int is_union = NDR_IS_UNION(ti); 1537 int error = NDR_ERR_INNER_PARAMS_BAD; 1538 int params; 1539 1540 params = arg_ref->inner_flags & NDR_F_PARAMS_MASK; 1541 1542 switch (params) { 1543 case NDR_F_NONE: 1544 if (is_union) { 1545 error = NDR_ERR_SWITCH_VALUE_MISSING; 1546 break; 1547 } 1548 return (*ti->ndr_func)(arg_ref); 1549 break; 1550 1551 case NDR_F_SIZE_IS: 1552 case NDR_F_DIMENSION_IS: 1553 case NDR_F_IS_POINTER+NDR_F_SIZE_IS: /* pointer to something */ 1554 case NDR_F_IS_REFERENCE+NDR_F_SIZE_IS: /* pointer to something */ 1555 if (is_varlen) { 1556 error = NDR_ERR_ARRAY_VARLEN_ILLEGAL; 1557 break; 1558 } 1559 if (is_union) { 1560 error = NDR_ERR_ARRAY_UNION_ILLEGAL; 1561 break; 1562 } 1563 if (params & NDR_F_IS_POINTER) 1564 return (ndr_inner_pointer(arg_ref)); 1565 else if (params & NDR_F_IS_REFERENCE) 1566 return (ndr_inner_reference(arg_ref)); 1567 else 1568 return (ndr_inner_array(arg_ref)); 1569 break; 1570 1571 case NDR_F_IS_POINTER: /* type is pointer to one something */ 1572 if (is_union) { 1573 error = NDR_ERR_ARRAY_UNION_ILLEGAL; 1574 break; 1575 } 1576 return (ndr_inner_pointer(arg_ref)); 1577 break; 1578 1579 case NDR_F_IS_REFERENCE: /* type is pointer to one something */ 1580 if (is_union) { 1581 error = NDR_ERR_ARRAY_UNION_ILLEGAL; 1582 break; 1583 } 1584 return (ndr_inner_reference(arg_ref)); 1585 break; 1586 1587 case NDR_F_SWITCH_IS: 1588 if (!is_union) { 1589 error = NDR_ERR_SWITCH_VALUE_ILLEGAL; 1590 break; 1591 } 1592 return (*ti->ndr_func)(arg_ref); 1593 break; 1594 1595 default: 1596 error = NDR_ERR_INNER_PARAMS_BAD; 1597 break; 1598 } 1599 1600 /* 1601 * If we get here, something is wrong. Most likely, 1602 * the params flags do not match 1603 */ 1604 NDR_SET_ERROR(arg_ref, error); 1605 return (0); 1606 } 1607 1608 int 1609 ndr_inner_pointer(ndr_ref_t *arg_ref) 1610 { 1611 ndr_stream_t *nds = arg_ref->stream; 1612 /*LINTED E_BAD_PTR_CAST_ALIGN*/ 1613 char **valpp = (char **)arg_ref->datum; 1614 ndr_ref_t *outer_ref; 1615 1616 if (!ndr__ulong(arg_ref)) 1617 return (0); /* error */ 1618 if (!*valpp) 1619 return (1); /* NULL pointer */ 1620 1621 outer_ref = ndr_enter_outer_queue(arg_ref); 1622 if (!outer_ref) 1623 return (0); /* error already set */ 1624 1625 /* 1626 * Move advice in inner_flags to outer_flags. 1627 * Retain pointer flag for conformant arrays. 1628 */ 1629 outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK; 1630 if ((outer_ref->outer_flags & NDR_F_SIZE_IS) == 0) 1631 outer_ref->outer_flags &= ~NDR_F_IS_POINTER; 1632 #ifdef NDR_INNER_PTR_NOT_YET 1633 outer_ref->outer_flags |= NDR_F_BACKPTR; 1634 if (outer_ref->outer_flags & NDR_F_SIZE_IS) { 1635 outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT; 1636 } 1637 #endif /* NDR_INNER_PTR_NOT_YET */ 1638 1639 outer_ref->backptr = valpp; 1640 1641 switch (nds->m_op) { 1642 case NDR_M_OP_MARSHALL: 1643 outer_ref->datum = *valpp; 1644 break; 1645 1646 case NDR_M_OP_UNMARSHALL: 1647 /* 1648 * This is probably wrong if the application allocated 1649 * memory in advance. Indicate no value for now. 1650 * ONC RPC handles this case. 1651 */ 1652 *valpp = 0; 1653 outer_ref->datum = 0; 1654 break; 1655 } 1656 1657 return (1); /* pointer dereference scheduled */ 1658 } 1659 1660 int 1661 ndr_inner_reference(ndr_ref_t *arg_ref) 1662 { 1663 ndr_stream_t *nds = arg_ref->stream; 1664 /*LINTED E_BAD_PTR_CAST_ALIGN*/ 1665 char **valpp = (char **)arg_ref->datum; 1666 ndr_ref_t *outer_ref; 1667 1668 outer_ref = ndr_enter_outer_queue(arg_ref); 1669 if (!outer_ref) 1670 return (0); /* error already set */ 1671 1672 /* 1673 * Move advice in inner_flags to outer_flags. 1674 * Retain reference flag for conformant arrays. 1675 */ 1676 outer_ref->outer_flags = arg_ref->inner_flags & NDR_F_PARAMS_MASK; 1677 if ((outer_ref->outer_flags & NDR_F_SIZE_IS) == 0) 1678 outer_ref->outer_flags &= ~NDR_F_IS_REFERENCE; 1679 #ifdef NDR_INNER_REF_NOT_YET 1680 outer_ref->outer_flags |= NDR_F_BACKPTR; 1681 if (outer_ref->outer_flags & NDR_F_SIZE_IS) { 1682 outer_ref->outer_flags |= NDR_F_ARRAY+NDR_F_CONFORMANT; 1683 } 1684 #endif /* NDR_INNER_REF_NOT_YET */ 1685 1686 outer_ref->backptr = valpp; 1687 1688 switch (nds->m_op) { 1689 case NDR_M_OP_MARSHALL: 1690 outer_ref->datum = *valpp; 1691 break; 1692 1693 case NDR_M_OP_UNMARSHALL: 1694 /* 1695 * This is probably wrong if the application allocated 1696 * memory in advance. Indicate no value for now. 1697 * ONC RPC handles this case. 1698 */ 1699 *valpp = 0; 1700 outer_ref->datum = 0; 1701 break; 1702 } 1703 1704 return (1); /* pointer dereference scheduled */ 1705 } 1706 1707 int 1708 ndr_inner_array(ndr_ref_t *encl_ref) 1709 { 1710 ndr_typeinfo_t *ti = encl_ref->ti; 1711 ndr_ref_t myref; 1712 unsigned long pdu_offset = encl_ref->pdu_offset; 1713 unsigned long n_elem; 1714 unsigned long i; 1715 char name[30]; 1716 1717 if (encl_ref->inner_flags & NDR_F_SIZE_IS) { 1718 /* now is the time to check/set size */ 1719 if (!ndr_size_is(encl_ref)) 1720 return (0); /* error already set */ 1721 n_elem = encl_ref->size_is; 1722 } else { 1723 assert(encl_ref->inner_flags & NDR_F_DIMENSION_IS); 1724 n_elem = encl_ref->dimension_is; 1725 } 1726 1727 bzero(&myref, sizeof (myref)); 1728 myref.enclosing = encl_ref; 1729 myref.stream = encl_ref->stream; 1730 myref.packed_alignment = 0; 1731 myref.ti = ti; 1732 myref.inner_flags = NDR_F_NONE; 1733 1734 for (i = 0; i < n_elem; i++) { 1735 (void) sprintf(name, "[%lu]", i); 1736 myref.name = name; 1737 myref.pdu_offset = pdu_offset + i * ti->pdu_size_fixed_part; 1738 myref.datum = encl_ref->datum + i * ti->c_size_fixed_part; 1739 1740 if (!ndr_inner(&myref)) 1741 return (0); 1742 } 1743 1744 return (1); 1745 } 1746 1747 1748 /* 1749 * BASIC TYPES 1750 */ 1751 #define MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \ 1752 extern int ndr_##TYPE(struct ndr_reference *encl_ref); \ 1753 ndr_typeinfo_t ndt_##TYPE = { \ 1754 1, /* NDR version */ \ 1755 (SIZE)-1, /* alignment */ \ 1756 NDR_F_NONE, /* flags */ \ 1757 ndr_##TYPE, /* ndr_func */ \ 1758 SIZE, /* pdu_size_fixed_part */ \ 1759 0, /* pdu_size_variable_part */ \ 1760 SIZE, /* c_size_fixed_part */ \ 1761 0, /* c_size_variable_part */ \ 1762 }; \ 1763 int ndr_##TYPE(struct ndr_reference *ref) { \ 1764 return (ndr_basic_integer(ref, SIZE)); \ 1765 } 1766 1767 #define MAKE_BASIC_TYPE_STRING(TYPE, SIZE) \ 1768 extern int ndr_s##TYPE(struct ndr_reference *encl_ref); \ 1769 ndr_typeinfo_t ndt_s##TYPE = { \ 1770 1, /* NDR version */ \ 1771 (SIZE)-1, /* alignment */ \ 1772 NDR_F_STRING, /* flags */ \ 1773 ndr_s##TYPE, /* ndr_func */ \ 1774 0, /* pdu_size_fixed_part */ \ 1775 SIZE, /* pdu_size_variable_part */ \ 1776 0, /* c_size_fixed_part */ \ 1777 SIZE, /* c_size_variable_part */ \ 1778 }; \ 1779 int ndr_s##TYPE(struct ndr_reference *ref) { \ 1780 return (ndr_string_basic_integer(ref, &ndt_##TYPE)); \ 1781 } 1782 1783 #define MAKE_BASIC_TYPE(TYPE, SIZE) \ 1784 MAKE_BASIC_TYPE_BASE(TYPE, SIZE) \ 1785 MAKE_BASIC_TYPE_STRING(TYPE, SIZE) 1786 1787 int ndr_basic_integer(ndr_ref_t *, unsigned); 1788 int ndr_string_basic_integer(ndr_ref_t *, ndr_typeinfo_t *); 1789 1790 1791 MAKE_BASIC_TYPE(_char, 1) 1792 MAKE_BASIC_TYPE(_uchar, 1) 1793 MAKE_BASIC_TYPE(_short, 2) 1794 MAKE_BASIC_TYPE(_ushort, 2) 1795 MAKE_BASIC_TYPE(_long, 4) 1796 MAKE_BASIC_TYPE(_ulong, 4) 1797 1798 MAKE_BASIC_TYPE_BASE(_wchar, 2) 1799 1800 int 1801 ndr_basic_integer(ndr_ref_t *ref, unsigned size) 1802 { 1803 ndr_stream_t *nds = ref->stream; 1804 char *valp = (char *)ref->datum; 1805 int rc; 1806 1807 switch (nds->m_op) { 1808 case NDR_M_OP_MARSHALL: 1809 rc = NDS_PUT_PDU(nds, ref->pdu_offset, size, 1810 valp, nds->swap, ref); 1811 break; 1812 1813 case NDR_M_OP_UNMARSHALL: 1814 rc = NDS_GET_PDU(nds, ref->pdu_offset, size, 1815 valp, nds->swap, ref); 1816 break; 1817 1818 default: 1819 NDR_SET_ERROR(ref, NDR_ERR_M_OP_INVALID); 1820 return (0); 1821 } 1822 1823 return (rc); 1824 } 1825 1826 int 1827 ndr_string_basic_integer(ndr_ref_t *encl_ref, ndr_typeinfo_t *type_under) 1828 { 1829 unsigned long pdu_offset = encl_ref->pdu_offset; 1830 unsigned size = type_under->pdu_size_fixed_part; 1831 char *valp; 1832 ndr_ref_t myref; 1833 unsigned long i; 1834 long sense = 0; 1835 char name[30]; 1836 1837 assert(size != 0); 1838 1839 bzero(&myref, sizeof (myref)); 1840 myref.enclosing = encl_ref; 1841 myref.stream = encl_ref->stream; 1842 myref.packed_alignment = 0; 1843 myref.ti = type_under; 1844 myref.inner_flags = NDR_F_NONE; 1845 myref.name = name; 1846 1847 for (i = 0; i < NDR_STRING_MAX; i++) { 1848 (void) sprintf(name, "[%lu]", i); 1849 myref.pdu_offset = pdu_offset + i * size; 1850 valp = encl_ref->datum + i * size; 1851 myref.datum = valp; 1852 1853 if (!ndr_inner(&myref)) 1854 return (0); 1855 1856 switch (size) { 1857 case 1: sense = *valp; break; 1858 /*LINTED E_BAD_PTR_CAST_ALIGN*/ 1859 case 2: sense = *(short *)valp; break; 1860 /*LINTED E_BAD_PTR_CAST_ALIGN*/ 1861 case 4: sense = *(long *)valp; break; 1862 } 1863 1864 if (!sense) 1865 break; 1866 } 1867 1868 return (1); 1869 } 1870 1871 1872 extern int ndr_s_wchar(ndr_ref_t *encl_ref); 1873 ndr_typeinfo_t ndt_s_wchar = { 1874 1, /* NDR version */ 1875 2-1, /* alignment */ 1876 NDR_F_STRING, /* flags */ 1877 ndr_s_wchar, /* ndr_func */ 1878 0, /* pdu_size_fixed_part */ 1879 2, /* pdu_size_variable_part */ 1880 0, /* c_size_fixed_part */ 1881 1, /* c_size_variable_part */ 1882 }; 1883 1884 1885 /* 1886 * Hand coded wchar function because all strings are transported 1887 * as wide characters. During NDR_M_OP_MARSHALL, we convert from 1888 * multi-byte to wide characters. During NDR_M_OP_UNMARSHALL, we 1889 * convert from wide characters to multi-byte. 1890 * 1891 * It appeared that NT would sometimes leave a spurious character 1892 * in the data stream before the null wide_char, which would get 1893 * included in the string decode because we processed until the 1894 * null character. It now looks like NT does not always terminate 1895 * RPC Unicode strings and the terminating null is a side effect 1896 * of field alignment. So now we rely on the strlen_is (set up in 1897 * ndr_outer_string) of the enclosing reference. This may or may 1898 * not include the null but it doesn't matter, the algorithm will 1899 * get it right. 1900 */ 1901 int 1902 ndr_s_wchar(ndr_ref_t *encl_ref) 1903 { 1904 ndr_stream_t *nds = encl_ref->stream; 1905 unsigned short wide_char; 1906 char *valp; 1907 ndr_ref_t myref; 1908 unsigned long i; 1909 char name[30]; 1910 int count; 1911 int char_count = 0; 1912 1913 if (nds->m_op == NDR_M_OP_UNMARSHALL) { 1914 /* 1915 * To avoid problems with zero length strings 1916 * we can just null terminate here and be done. 1917 */ 1918 if (encl_ref->strlen_is == 0) { 1919 encl_ref->datum[0] = '\0'; 1920 return (1); 1921 } 1922 } 1923 1924 bzero(&myref, sizeof (myref)); 1925 myref.enclosing = encl_ref; 1926 myref.stream = encl_ref->stream; 1927 myref.packed_alignment = 0; 1928 myref.ti = &ndt__wchar; 1929 myref.inner_flags = NDR_F_NONE; 1930 myref.datum = (char *)&wide_char; 1931 myref.name = name; 1932 myref.pdu_offset = encl_ref->pdu_offset; 1933 1934 valp = encl_ref->datum; 1935 count = 0; 1936 1937 for (i = 0; i < NDR_STRING_MAX; i++) { 1938 (void) sprintf(name, "[%lu]", i); 1939 1940 if (nds->m_op == NDR_M_OP_MARSHALL) { 1941 count = smb_mbtowc((smb_wchar_t *)&wide_char, valp, 1942 MTS_MB_CHAR_MAX); 1943 if (count < 0) { 1944 return (0); 1945 } else if (count == 0) { 1946 if (encl_ref->strlen_is != encl_ref->size_is) 1947 break; 1948 1949 /* 1950 * If the input char is 0, mbtowc 1951 * returns 0 without setting wide_char. 1952 * Set wide_char to 0 and a count of 1. 1953 */ 1954 wide_char = *valp; 1955 count = 1; 1956 } 1957 } 1958 1959 if (!ndr_inner(&myref)) 1960 return (0); 1961 1962 if (nds->m_op == NDR_M_OP_UNMARSHALL) { 1963 count = smb_wctomb(valp, wide_char); 1964 1965 if ((++char_count) == encl_ref->strlen_is) { 1966 valp += count; 1967 *valp = '\0'; 1968 break; 1969 } 1970 } 1971 1972 if (!wide_char) 1973 break; 1974 1975 myref.pdu_offset += sizeof (wide_char); 1976 valp += count; 1977 } 1978 1979 return (1); 1980 } 1981 1982 /* 1983 * Converts a multibyte character string to a little-endian, wide-char 1984 * string. No more than nwchars wide characters are stored. 1985 * A terminating null wide character is appended if there is room. 1986 * 1987 * Returns the number of wide characters converted, not counting 1988 * any terminating null wide character. Returns -1 if an invalid 1989 * multibyte character is encountered. 1990 */ 1991 size_t 1992 ndr_mbstowcs(ndr_stream_t *nds, smb_wchar_t *wcs, const char *mbs, 1993 size_t nwchars) 1994 { 1995 smb_wchar_t *start = wcs; 1996 int nbytes; 1997 1998 while (nwchars--) { 1999 nbytes = ndr_mbtowc(nds, wcs, mbs, MTS_MB_CHAR_MAX); 2000 if (nbytes < 0) { 2001 *wcs = 0; 2002 return ((size_t)-1); 2003 } 2004 2005 if (*mbs == 0) 2006 break; 2007 2008 ++wcs; 2009 mbs += nbytes; 2010 } 2011 2012 return (wcs - start); 2013 } 2014 2015 /* 2016 * Converts a multibyte character to a little-endian, wide-char, which 2017 * is stored in wcharp. Up to nbytes bytes are examined. 2018 * 2019 * If mbchar is valid, returns the number of bytes processed in mbchar. 2020 * If mbchar is invalid, returns -1. See also smb_mbtowc(). 2021 */ 2022 /*ARGSUSED*/ 2023 int 2024 ndr_mbtowc(ndr_stream_t *nds, smb_wchar_t *wcharp, const char *mbchar, 2025 size_t nbytes) 2026 { 2027 int rc; 2028 2029 if ((rc = smb_mbtowc(wcharp, mbchar, nbytes)) < 0) 2030 return (rc); 2031 2032 #ifdef _BIG_ENDIAN 2033 if (nds == NULL || NDR_MODE_MATCH(nds, NDR_MODE_RETURN_SEND)) 2034 *wcharp = BSWAP_16(*wcharp); 2035 #endif 2036 2037 return (rc); 2038 } 2039