2 Unix SMB/CIFS implementation.
4 routines for marshalling/unmarshalling string types
6 Copyright (C) Andrew Tridgell 2003
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "librpc/ndr/libndr.h"
26 pull a general string from the wire
28 _PUBLIC_
enum ndr_err_code
ndr_pull_string(struct ndr_pull
*ndr
, int ndr_flags
, const char **s
)
31 uint32_t len1
, ofs
, len2
;
33 size_t conv_src_len
= 0, converted_size
;
34 int do_convert
= 1, chset
= CH_UTF16
;
35 unsigned byte_mul
= 2;
36 unsigned flags
= ndr
->flags
;
37 unsigned c_len_term
= 0;
39 if (!(ndr_flags
& NDR_SCALARS
)) {
40 return NDR_ERR_SUCCESS
;
47 if (flags
& LIBNDR_FLAG_STR_ASCII
) {
50 flags
&= ~LIBNDR_FLAG_STR_ASCII
;
53 if (flags
& LIBNDR_FLAG_STR_UTF8
) {
56 flags
&= ~LIBNDR_FLAG_STR_UTF8
;
59 if (flags
& LIBNDR_FLAG_STR_RAW8
) {
62 flags
&= ~LIBNDR_FLAG_STR_RAW8
;
65 flags
&= ~LIBNDR_FLAG_STR_CONFORMANT
;
66 if (flags
& LIBNDR_FLAG_STR_CHARLEN
) {
68 flags
&= ~LIBNDR_FLAG_STR_CHARLEN
;
71 switch (flags
& LIBNDR_STRING_FLAGS
) {
72 case LIBNDR_FLAG_STR_LEN4
|LIBNDR_FLAG_STR_SIZE4
:
73 case LIBNDR_FLAG_STR_LEN4
|LIBNDR_FLAG_STR_SIZE4
|LIBNDR_FLAG_STR_NOTERM
:
74 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &len1
));
75 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &ofs
));
77 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "non-zero array offset with string flags 0x%x\n",
78 ndr
->flags
& LIBNDR_STRING_FLAGS
);
80 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &len2
));
82 return ndr_pull_error(ndr
, NDR_ERR_STRING
,
83 "Bad string lengths len1=%u ofs=%u len2=%u\n",
85 } else if (len1
!= len2
) {
86 DEBUG(6,("len1[%u] != len2[%u] '%s'\n", len1
, len2
, as
));
88 conv_src_len
= len2
+ c_len_term
;
91 case LIBNDR_FLAG_STR_SIZE4
:
92 case LIBNDR_FLAG_STR_SIZE4
|LIBNDR_FLAG_STR_NOTERM
:
93 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &len1
));
94 conv_src_len
= len1
+ c_len_term
;
97 case LIBNDR_FLAG_STR_LEN4
:
98 case LIBNDR_FLAG_STR_LEN4
|LIBNDR_FLAG_STR_NOTERM
:
99 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &ofs
));
101 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "non-zero array offset with string flags 0x%x\n",
102 ndr
->flags
& LIBNDR_STRING_FLAGS
);
104 NDR_CHECK(ndr_pull_uint32(ndr
, NDR_SCALARS
, &len1
));
105 conv_src_len
= len1
+ c_len_term
;
108 case LIBNDR_FLAG_STR_SIZE2
:
109 case LIBNDR_FLAG_STR_SIZE2
|LIBNDR_FLAG_STR_NOTERM
:
110 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &len3
));
111 conv_src_len
= len3
+ c_len_term
;
114 case LIBNDR_FLAG_STR_SIZE2
|LIBNDR_FLAG_STR_NOTERM
|LIBNDR_FLAG_STR_BYTESIZE
:
115 NDR_CHECK(ndr_pull_uint16(ndr
, NDR_SCALARS
, &len3
));
117 byte_mul
= 1; /* the length is now absolute */
120 case LIBNDR_FLAG_STR_NULLTERM
:
122 conv_src_len
= ascii_len_n((const char *)(ndr
->data
+ndr
->offset
), ndr
->data_size
- ndr
->offset
);
124 conv_src_len
= utf16_len_n(ndr
->data
+ndr
->offset
, ndr
->data_size
- ndr
->offset
);
126 byte_mul
= 1; /* the length is now absolute */
129 case LIBNDR_FLAG_STR_NOTERM
:
130 if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
131 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
132 ndr
->flags
& LIBNDR_STRING_FLAGS
);
134 conv_src_len
= ndr
->data_size
- ndr
->offset
;
135 byte_mul
= 1; /* the length is now absolute */
139 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
140 ndr
->flags
& LIBNDR_STRING_FLAGS
);
143 NDR_PULL_NEED_BYTES(ndr
, conv_src_len
* byte_mul
);
144 if (conv_src_len
== 0) {
145 as
= talloc_strdup(ndr
->current_mem_ctx
, "");
149 as
= talloc_strndup(ndr
->current_mem_ctx
,
150 (char *)ndr
->data
+ ndr
->offset
,
153 return ndr_pull_error(ndr
, NDR_ERR_ALLOC
,
154 "Failed to talloc_strndup() in RAW8 ndr_string_pull()");
156 converted_size
= MIN(strlen(as
)+1, conv_src_len
);
157 } else if (!convert_string_talloc(ndr
->current_mem_ctx
, chset
,
158 CH_UNIX
, ndr
->data
+ ndr
->offset
,
159 conv_src_len
* byte_mul
,
160 (void **)(void *)&as
,
162 return ndr_pull_error(ndr
, NDR_ERR_CHARCNV
,
163 "Bad character conversion with flags 0x%x", flags
);
167 /* this is a way of detecting if a string is sent with the wrong
169 if (ndr
->flags
& LIBNDR_FLAG_STR_NOTERM
) {
170 if (as
&& converted_size
> 0 && as
[converted_size
-1] == '\0') {
171 DEBUG(6,("short string '%s', sent with NULL termination despite NOTERM flag in IDL\n", as
));
174 if (as
&& converted_size
> 0 && as
[converted_size
-1] != '\0') {
175 DEBUG(6,("long string '%s', send without NULL termination (which was expected)\n", as
));
179 NDR_CHECK(ndr_pull_advance(ndr
, conv_src_len
* byte_mul
));
182 return NDR_ERR_SUCCESS
;
187 push a general string onto the wire
189 _PUBLIC_
enum ndr_err_code
ndr_push_string(struct ndr_push
*ndr
, int ndr_flags
, const char *s
)
191 ssize_t s_len
, c_len
;
193 int do_convert
= 1, chset
= CH_UTF16
;
194 unsigned flags
= ndr
->flags
;
195 unsigned byte_mul
= 2;
196 uint8_t *dest
= NULL
;
198 if (!(ndr_flags
& NDR_SCALARS
)) {
199 return NDR_ERR_SUCCESS
;
206 s_len
= s
?strlen(s
):0;
208 if (flags
& LIBNDR_FLAG_STR_ASCII
) {
211 flags
&= ~LIBNDR_FLAG_STR_ASCII
;
214 if (flags
& LIBNDR_FLAG_STR_UTF8
) {
217 flags
&= ~LIBNDR_FLAG_STR_UTF8
;
220 if (flags
& LIBNDR_FLAG_STR_RAW8
) {
223 flags
&= ~LIBNDR_FLAG_STR_RAW8
;
226 flags
&= ~LIBNDR_FLAG_STR_CONFORMANT
;
228 if (!(flags
& LIBNDR_FLAG_STR_NOTERM
)) {
234 dest
= (uint8_t *)talloc_strndup(ndr
, s
, s_len
);
235 } else if (!convert_string_talloc(ndr
, CH_UNIX
, chset
, s
, s_len
,
236 (void **)(void *)&dest
, &d_len
))
238 return ndr_push_error(ndr
, NDR_ERR_CHARCNV
,
239 "Bad character push conversion with flags 0x%x", flags
);
242 if (flags
& LIBNDR_FLAG_STR_BYTESIZE
) {
244 flags
&= ~LIBNDR_FLAG_STR_BYTESIZE
;
245 } else if (flags
& LIBNDR_FLAG_STR_CHARLEN
) {
246 c_len
= (d_len
/ byte_mul
)-1;
247 flags
&= ~LIBNDR_FLAG_STR_CHARLEN
;
249 c_len
= d_len
/ byte_mul
;
252 switch ((flags
& LIBNDR_STRING_FLAGS
) & ~LIBNDR_FLAG_STR_NOTERM
) {
253 case LIBNDR_FLAG_STR_LEN4
|LIBNDR_FLAG_STR_SIZE4
:
254 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
255 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
256 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
257 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
260 case LIBNDR_FLAG_STR_LEN4
:
261 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
262 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
263 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
266 case LIBNDR_FLAG_STR_SIZE4
:
267 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
268 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
271 case LIBNDR_FLAG_STR_SIZE2
:
272 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, c_len
));
273 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
276 case LIBNDR_FLAG_STR_NULLTERM
:
277 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
281 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
282 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
286 return ndr_push_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
287 ndr
->flags
& LIBNDR_STRING_FLAGS
);
292 return NDR_ERR_SUCCESS
;
296 push a general string onto the wire
298 _PUBLIC_
size_t ndr_string_array_size(struct ndr_push
*ndr
, const char *s
)
301 unsigned flags
= ndr
->flags
;
302 unsigned byte_mul
= 2;
303 unsigned c_len_term
= 1;
305 if (flags
& LIBNDR_FLAG_STR_RAW8
) {
306 c_len
= s
?strlen(s
):0;
308 c_len
= s
?strlen_m(s
):0;
311 if (flags
& (LIBNDR_FLAG_STR_ASCII
|LIBNDR_FLAG_STR_RAW8
|LIBNDR_FLAG_STR_UTF8
)) {
315 if (flags
& LIBNDR_FLAG_STR_NOTERM
) {
319 c_len
= c_len
+ c_len_term
;
321 if (flags
& LIBNDR_FLAG_STR_BYTESIZE
) {
322 c_len
= c_len
* byte_mul
;
328 _PUBLIC_
void ndr_print_string(struct ndr_print
*ndr
, const char *name
, const char *s
)
331 ndr
->print(ndr
, "%-25s: '%s'", name
, s
);
333 ndr
->print(ndr
, "%-25s: NULL", name
);
337 _PUBLIC_
uint32_t ndr_size_string(int ret
, const char * const* string
, int flags
)
339 /* FIXME: Is this correct for all strings ? */
340 if(!(*string
)) return ret
;
341 return ret
+strlen(*string
)+1;
345 pull a general string array from the wire
347 _PUBLIC_
enum ndr_err_code
ndr_pull_string_array(struct ndr_pull
*ndr
, int ndr_flags
, const char ***_a
)
349 const char **a
= NULL
;
351 unsigned flags
= ndr
->flags
;
352 unsigned saved_flags
= ndr
->flags
;
354 if (!(ndr_flags
& NDR_SCALARS
)) {
355 return NDR_ERR_SUCCESS
;
358 switch (flags
& (LIBNDR_FLAG_STR_NULLTERM
|LIBNDR_FLAG_STR_NOTERM
)) {
359 case LIBNDR_FLAG_STR_NULLTERM
:
361 * here the strings are null terminated
362 * but also the array is null terminated if LIBNDR_FLAG_REMAINING
365 for (count
= 0;; count
++) {
367 const char *s
= NULL
;
368 a
= talloc_realloc(ndr
->current_mem_ctx
, a
, const char *, count
+ 2);
369 NDR_ERR_HAVE_NO_MEMORY(a
);
373 tmp_ctx
= ndr
->current_mem_ctx
;
374 ndr
->current_mem_ctx
= a
;
375 NDR_CHECK(ndr_pull_string(ndr
, ndr_flags
, &s
));
376 if ((ndr
->data_size
- ndr
->offset
) == 0 && ndr
->flags
& LIBNDR_FLAG_REMAINING
)
381 ndr
->current_mem_ctx
= tmp_ctx
;
382 if (strcmp("", s
)==0) {
393 case LIBNDR_FLAG_STR_NOTERM
:
394 if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
395 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
396 ndr
->flags
& LIBNDR_STRING_FLAGS
);
399 * here the strings are not null terminated
400 * but serarated by a null terminator
402 * which means the same as:
403 * Every string is null terminated exept the last
404 * string is terminated by the end of the buffer
406 * as LIBNDR_FLAG_STR_NULLTERM also end at the end
407 * of the buffer, we can pull each string with this flag
409 * The big difference with the case LIBNDR_FLAG_STR_NOTERM +
410 * LIBNDR_FLAG_REMAINING is that the last string will not be null terminated
412 ndr
->flags
&= ~(LIBNDR_FLAG_STR_NOTERM
|LIBNDR_FLAG_REMAINING
);
413 ndr
->flags
|= LIBNDR_FLAG_STR_NULLTERM
;
415 for (count
= 0; ((ndr
->data_size
- ndr
->offset
) > 0); count
++) {
417 const char *s
= NULL
;
418 a
= talloc_realloc(ndr
->current_mem_ctx
, a
, const char *, count
+ 2);
419 NDR_ERR_HAVE_NO_MEMORY(a
);
423 tmp_ctx
= ndr
->current_mem_ctx
;
424 ndr
->current_mem_ctx
= a
;
425 NDR_CHECK(ndr_pull_string(ndr
, ndr_flags
, &s
));
426 ndr
->current_mem_ctx
= tmp_ctx
;
434 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
435 ndr
->flags
& LIBNDR_STRING_FLAGS
);
438 ndr
->flags
= saved_flags
;
439 return NDR_ERR_SUCCESS
;
443 push a general string array onto the wire
445 _PUBLIC_
enum ndr_err_code
ndr_push_string_array(struct ndr_push
*ndr
, int ndr_flags
, const char **a
)
448 unsigned flags
= ndr
->flags
;
449 unsigned saved_flags
= ndr
->flags
;
451 if (!(ndr_flags
& NDR_SCALARS
)) {
452 return NDR_ERR_SUCCESS
;
455 switch (flags
& LIBNDR_STRING_FLAGS
) {
456 case LIBNDR_FLAG_STR_NULLTERM
:
457 for (count
= 0; a
&& a
[count
]; count
++) {
458 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, a
[count
]));
460 /* If LIBNDR_FLAG_REMAINING then we do not add a null terminator to the array */
461 if (!(flags
& LIBNDR_FLAG_REMAINING
))
463 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, ""));
467 case LIBNDR_FLAG_STR_NOTERM
:
468 if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
469 return ndr_push_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
470 ndr
->flags
& LIBNDR_STRING_FLAGS
);
473 for (count
= 0; a
&& a
[count
]; count
++) {
475 ndr
->flags
&= ~(LIBNDR_FLAG_STR_NOTERM
|LIBNDR_FLAG_REMAINING
);
476 ndr
->flags
|= LIBNDR_FLAG_STR_NULLTERM
;
477 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, ""));
478 ndr
->flags
= saved_flags
;
480 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, a
[count
]));
486 return ndr_push_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
487 ndr
->flags
& LIBNDR_STRING_FLAGS
);
490 ndr
->flags
= saved_flags
;
491 return NDR_ERR_SUCCESS
;
494 _PUBLIC_
void ndr_print_string_array(struct ndr_print
*ndr
, const char *name
, const char **a
)
499 for (count
= 0; a
&& a
[count
]; count
++) {}
501 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
503 for (i
=0;i
<count
;i
++) {
505 if (asprintf(&idx
, "[%d]", i
) != -1) {
506 ndr_print_string(ndr
, idx
, a
[i
]);
513 _PUBLIC_
size_t ndr_size_string_array(const char **a
, uint32_t count
, int flags
)
519 if (flags
& LIBNDR_FLAG_STR_RAW8
) {
521 flags
&= ~LIBNDR_FLAG_STR_RAW8
;
524 switch (flags
& LIBNDR_STRING_FLAGS
) {
525 case LIBNDR_FLAG_STR_NULLTERM
:
526 for (i
= 0; i
< count
; i
++) {
527 size
+= rawbytes
?strlen(a
[i
]) + 1:strlen_m_term(a
[i
]);
530 case LIBNDR_FLAG_STR_NOTERM
:
531 for (i
= 0; i
< count
; i
++) {
532 size
+= rawbytes
?strlen(a
[i
]):strlen_m(a
[i
]);
543 * Return number of elements in a string including the last (zeroed) element
545 _PUBLIC_
uint32_t ndr_string_length(const void *_var
, uint32_t element_size
)
548 uint8_t zero
[4] = {0,0,0,0};
549 const char *var
= (const char *)_var
;
551 for (i
= 0; memcmp(var
+i
*element_size
,zero
,element_size
) != 0; i
++);
556 _PUBLIC_
enum ndr_err_code
ndr_check_string_terminator(struct ndr_pull
*ndr
, uint32_t count
, uint32_t element_size
)
559 uint32_t save_offset
;
561 save_offset
= ndr
->offset
;
562 NDR_CHECK(ndr_pull_advance(ndr
, (count
- 1) * element_size
));
563 NDR_PULL_NEED_BYTES(ndr
, element_size
);
565 for (i
= 0; i
< element_size
; i
++) {
566 if (ndr
->data
[ndr
->offset
+i
] != 0) {
567 ndr
->offset
= save_offset
;
569 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
, "String terminator not present or outside string boundaries");
573 ndr
->offset
= save_offset
;
575 return NDR_ERR_SUCCESS
;
578 _PUBLIC_
enum ndr_err_code
ndr_pull_charset(struct ndr_pull
*ndr
, int ndr_flags
, const char **var
, uint32_t length
, uint8_t byte_mul
, charset_t chset
)
580 size_t converted_size
;
583 *var
= talloc_strdup(ndr
->current_mem_ctx
, "");
584 return NDR_ERR_SUCCESS
;
587 if (NDR_BE(ndr
) && chset
== CH_UTF16
) {
591 NDR_PULL_NEED_BYTES(ndr
, length
*byte_mul
);
593 if (!convert_string_talloc(ndr
->current_mem_ctx
, chset
, CH_UNIX
,
594 ndr
->data
+ndr
->offset
, length
*byte_mul
,
595 discard_const_p(void *, var
),
598 return ndr_pull_error(ndr
, NDR_ERR_CHARCNV
,
599 "Bad character conversion");
601 NDR_CHECK(ndr_pull_advance(ndr
, length
*byte_mul
));
603 return NDR_ERR_SUCCESS
;
606 _PUBLIC_
enum ndr_err_code
ndr_pull_charset_to_null(struct ndr_pull
*ndr
, int ndr_flags
, const char **var
, uint32_t length
, uint8_t byte_mul
, charset_t chset
)
608 size_t converted_size
;
612 *var
= talloc_strdup(ndr
->current_mem_ctx
, "");
613 return NDR_ERR_SUCCESS
;
616 if (NDR_BE(ndr
) && chset
== CH_UTF16
) {
620 NDR_PULL_NEED_BYTES(ndr
, length
*byte_mul
);
622 str_len
= ndr_string_length(ndr
->data
+ndr
->offset
, byte_mul
);
623 str_len
= MIN(str_len
, length
); /* overrun protection */
624 if (!convert_string_talloc(ndr
->current_mem_ctx
, chset
, CH_UNIX
,
625 ndr
->data
+ndr
->offset
, str_len
*byte_mul
,
626 discard_const_p(void *, var
),
629 return ndr_pull_error(ndr
, NDR_ERR_CHARCNV
,
630 "Bad character conversion");
632 NDR_CHECK(ndr_pull_advance(ndr
, length
*byte_mul
));
634 return NDR_ERR_SUCCESS
;
637 _PUBLIC_
enum ndr_err_code
ndr_push_charset(struct ndr_push
*ndr
, int ndr_flags
, const char *var
, uint32_t length
, uint8_t byte_mul
, charset_t chset
)
641 if (NDR_BE(ndr
) && chset
== CH_UTF16
) {
645 required
= byte_mul
* length
;
647 NDR_PUSH_NEED_BYTES(ndr
, required
);
651 if (!convert_string(CH_UNIX
, chset
,
653 ndr
->data
+ndr
->offset
, required
, &size
)) {
654 return ndr_push_error(ndr
, NDR_ERR_CHARCNV
,
655 "Bad character conversion");
658 /* Make sure the remaining part of the string is filled with zeroes */
659 if (size
< required
) {
660 memset(ndr
->data
+ndr
->offset
+size
, 0, required
-size
);
664 ndr
->offset
+= required
;
666 return NDR_ERR_SUCCESS
;
669 /* Return number of elements in a string in the specified charset */
670 _PUBLIC_
uint32_t ndr_charset_length(const void *var
, charset_t chset
)
673 /* case CH_UTF16: this has the same value as CH_UTF16LE */
678 return strlen_m_ext_term((const char *)var
, CH_UNIX
, chset
);
681 return strlen((const char *)var
)+1;
684 /* Fallback, this should never happen */
685 return strlen((const char *)var
)+1;