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 * We ensure that conv_str_len cannot return 0 by
123 * requring that there be enough bytes for at least
124 * the NULL terminator
127 NDR_PULL_NEED_BYTES(ndr
, 1);
128 conv_src_len
= ascii_len_n((const char *)(ndr
->data
+ndr
->offset
), ndr
->data_size
- ndr
->offset
);
130 NDR_PULL_NEED_BYTES(ndr
, 2);
131 conv_src_len
= utf16_len_n(ndr
->data
+ndr
->offset
, ndr
->data_size
- ndr
->offset
);
133 byte_mul
= 1; /* the length is now absolute */
136 case LIBNDR_FLAG_STR_NOTERM
:
137 if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
138 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
139 ndr
->flags
& LIBNDR_STRING_FLAGS
);
141 conv_src_len
= ndr
->data_size
- ndr
->offset
;
142 byte_mul
= 1; /* the length is now absolute */
146 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
147 ndr
->flags
& LIBNDR_STRING_FLAGS
);
150 NDR_PULL_NEED_BYTES(ndr
, conv_src_len
* byte_mul
);
151 if (conv_src_len
== 0) {
152 as
= talloc_strdup(ndr
->current_mem_ctx
, "");
156 as
= talloc_strndup(ndr
->current_mem_ctx
,
157 (char *)ndr
->data
+ ndr
->offset
,
160 return ndr_pull_error(ndr
, NDR_ERR_ALLOC
,
161 "Failed to talloc_strndup() in RAW8 ndr_string_pull()");
163 converted_size
= MIN(strlen(as
)+1, conv_src_len
);
164 } else if (!convert_string_talloc(ndr
->current_mem_ctx
, chset
,
165 CH_UNIX
, ndr
->data
+ ndr
->offset
,
166 conv_src_len
* byte_mul
,
167 (void **)(void *)&as
,
169 return ndr_pull_error(ndr
, NDR_ERR_CHARCNV
,
170 "Bad character conversion with flags 0x%x", flags
);
174 /* this is a way of detecting if a string is sent with the wrong
176 if (ndr
->flags
& LIBNDR_FLAG_STR_NOTERM
) {
177 if (as
&& converted_size
> 0 && as
[converted_size
-1] == '\0') {
178 DEBUG(6,("short string '%s', sent with NULL termination despite NOTERM flag in IDL\n", as
));
181 if (as
&& converted_size
> 0 && as
[converted_size
-1] != '\0') {
182 DEBUG(6,("long string '%s', send without NULL termination (which was expected)\n", as
));
186 NDR_CHECK(ndr_pull_advance(ndr
, conv_src_len
* byte_mul
));
189 return NDR_ERR_SUCCESS
;
194 push a general string onto the wire
196 _PUBLIC_
enum ndr_err_code
ndr_push_string(struct ndr_push
*ndr
, int ndr_flags
, const char *s
)
198 ssize_t s_len
, c_len
;
200 int do_convert
= 1, chset
= CH_UTF16
;
201 unsigned flags
= ndr
->flags
;
202 unsigned byte_mul
= 2;
203 uint8_t *dest
= NULL
;
205 if (!(ndr_flags
& NDR_SCALARS
)) {
206 return NDR_ERR_SUCCESS
;
213 s_len
= s
?strlen(s
):0;
215 if (flags
& LIBNDR_FLAG_STR_ASCII
) {
218 flags
&= ~LIBNDR_FLAG_STR_ASCII
;
221 if (flags
& LIBNDR_FLAG_STR_UTF8
) {
224 flags
&= ~LIBNDR_FLAG_STR_UTF8
;
227 if (flags
& LIBNDR_FLAG_STR_RAW8
) {
230 flags
&= ~LIBNDR_FLAG_STR_RAW8
;
233 flags
&= ~LIBNDR_FLAG_STR_CONFORMANT
;
235 if (!(flags
& LIBNDR_FLAG_STR_NOTERM
)) {
241 dest
= (uint8_t *)talloc_strndup(ndr
, s
, s_len
);
242 } else if (!convert_string_talloc(ndr
, CH_UNIX
, chset
, s
, s_len
,
243 (void **)(void *)&dest
, &d_len
))
245 return ndr_push_error(ndr
, NDR_ERR_CHARCNV
,
246 "Bad character push conversion with flags 0x%x", flags
);
249 if (flags
& LIBNDR_FLAG_STR_BYTESIZE
) {
251 flags
&= ~LIBNDR_FLAG_STR_BYTESIZE
;
252 } else if (flags
& LIBNDR_FLAG_STR_CHARLEN
) {
253 c_len
= (d_len
/ byte_mul
)-1;
254 flags
&= ~LIBNDR_FLAG_STR_CHARLEN
;
256 c_len
= d_len
/ byte_mul
;
259 switch ((flags
& LIBNDR_STRING_FLAGS
) & ~LIBNDR_FLAG_STR_NOTERM
) {
260 case LIBNDR_FLAG_STR_LEN4
|LIBNDR_FLAG_STR_SIZE4
:
261 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
262 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
263 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
264 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
267 case LIBNDR_FLAG_STR_LEN4
:
268 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, 0));
269 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
270 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
273 case LIBNDR_FLAG_STR_SIZE4
:
274 NDR_CHECK(ndr_push_uint32(ndr
, NDR_SCALARS
, c_len
));
275 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
278 case LIBNDR_FLAG_STR_SIZE2
:
279 NDR_CHECK(ndr_push_uint16(ndr
, NDR_SCALARS
, c_len
));
280 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
283 case LIBNDR_FLAG_STR_NULLTERM
:
284 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
288 if (ndr
->flags
& LIBNDR_FLAG_REMAINING
) {
289 NDR_CHECK(ndr_push_bytes(ndr
, dest
, d_len
));
293 return ndr_push_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
294 ndr
->flags
& LIBNDR_STRING_FLAGS
);
299 return NDR_ERR_SUCCESS
;
303 push a general string onto the wire
305 _PUBLIC_
size_t ndr_string_array_size(struct ndr_push
*ndr
, const char *s
)
308 unsigned flags
= ndr
->flags
;
309 unsigned byte_mul
= 2;
310 unsigned c_len_term
= 1;
312 if (flags
& LIBNDR_FLAG_STR_RAW8
) {
313 c_len
= s
?strlen(s
):0;
315 c_len
= s
?strlen_m(s
):0;
318 if (flags
& (LIBNDR_FLAG_STR_ASCII
|LIBNDR_FLAG_STR_RAW8
|LIBNDR_FLAG_STR_UTF8
)) {
322 if (flags
& LIBNDR_FLAG_STR_NOTERM
) {
326 c_len
= c_len
+ c_len_term
;
328 if (flags
& LIBNDR_FLAG_STR_BYTESIZE
) {
329 c_len
= c_len
* byte_mul
;
335 _PUBLIC_
void ndr_print_string(struct ndr_print
*ndr
, const char *name
, const char *s
)
338 ndr
->print(ndr
, "%-25s: '%s'", name
, s
);
340 ndr
->print(ndr
, "%-25s: NULL", name
);
344 _PUBLIC_
uint32_t ndr_size_string(int ret
, const char * const* string
, int flags
)
346 /* FIXME: Is this correct for all strings ? */
347 if(!(*string
)) return ret
;
348 return ret
+strlen(*string
)+1;
351 static uint32_t guess_string_array_size(struct ndr_pull
*ndr
, int ndr_flags
)
354 * Here we could do something clever like count the number of zeros in
355 * the ndr data, but it is probably sufficient to pick a lowish number
356 * (compared to the overhead of the talloc header) and let the
357 * expontential resizing deal with longer arrays.
362 static enum ndr_err_code
extend_string_array(struct ndr_pull
*ndr
,
366 const char **a
= *_a
;
367 uint32_t inc
= *count
/ 4 + 3;
368 uint32_t alloc_size
= *count
+ inc
;
370 if (alloc_size
< *count
) {
372 return NDR_ERR_ALLOC
;
375 * We allocate and zero two more bytes than we report back, so that
376 * the string array will always be NULL terminated.
378 a
= talloc_realloc(ndr
->current_mem_ctx
, a
,
381 NDR_ERR_HAVE_NO_MEMORY(a
);
383 memset(a
+ *count
, 0, inc
* sizeof(a
[0]));
385 *count
= alloc_size
- 2;
386 return NDR_ERR_SUCCESS
;
390 pull a general string array from the wire
392 _PUBLIC_
enum ndr_err_code
ndr_pull_string_array(struct ndr_pull
*ndr
, int ndr_flags
, const char ***_a
)
394 const char **a
= NULL
;
396 unsigned flags
= ndr
->flags
;
397 unsigned saved_flags
= ndr
->flags
;
400 if (!(ndr_flags
& NDR_SCALARS
)) {
401 return NDR_ERR_SUCCESS
;
404 alloc_size
= guess_string_array_size(ndr
, ndr_flags
);
405 a
= talloc_zero_array(ndr
->current_mem_ctx
, const char *, alloc_size
+ 2);
406 NDR_ERR_HAVE_NO_MEMORY(a
);
408 switch (flags
& (LIBNDR_FLAG_STR_NULLTERM
|LIBNDR_FLAG_STR_NOTERM
)) {
409 case LIBNDR_FLAG_STR_NULLTERM
:
411 * here the strings are null terminated
412 * but also the array is null terminated if LIBNDR_FLAG_REMAINING
415 for (count
= 0;; count
++) {
417 const char *s
= NULL
;
418 if (count
== alloc_size
) {
419 NDR_CHECK(extend_string_array(ndr
,
424 tmp_ctx
= ndr
->current_mem_ctx
;
425 ndr
->current_mem_ctx
= a
;
426 NDR_CHECK(ndr_pull_string(ndr
, ndr_flags
, &s
));
427 ndr
->current_mem_ctx
= tmp_ctx
;
428 if ((ndr
->data_size
- ndr
->offset
) == 0 && ndr
->flags
& LIBNDR_FLAG_REMAINING
)
433 if (strcmp("", s
)==0) {
444 case LIBNDR_FLAG_STR_NOTERM
:
445 if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
446 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
447 ndr
->flags
& LIBNDR_STRING_FLAGS
);
450 * here the strings are not null terminated
451 * but separated by a null terminator
453 * which means the same as:
454 * Every string is null terminated exept the last
455 * string is terminated by the end of the buffer
457 * as LIBNDR_FLAG_STR_NULLTERM also end at the end
458 * of the buffer, we can pull each string with this flag
460 * The big difference with the case LIBNDR_FLAG_STR_NOTERM +
461 * LIBNDR_FLAG_REMAINING is that the last string will not be null terminated
463 ndr
->flags
&= ~(LIBNDR_FLAG_STR_NOTERM
|LIBNDR_FLAG_REMAINING
);
464 ndr
->flags
|= LIBNDR_FLAG_STR_NULLTERM
;
466 for (count
= 0; ((ndr
->data_size
- ndr
->offset
) > 0); count
++) {
468 const char *s
= NULL
;
469 if (count
== alloc_size
) {
470 NDR_CHECK(extend_string_array(ndr
,
475 tmp_ctx
= ndr
->current_mem_ctx
;
476 ndr
->current_mem_ctx
= a
;
477 NDR_CHECK(ndr_pull_string(ndr
, ndr_flags
, &s
));
478 ndr
->current_mem_ctx
= tmp_ctx
;
482 a
= talloc_realloc(ndr
->current_mem_ctx
, a
, const char *, count
+ 1);
483 NDR_ERR_HAVE_NO_MEMORY(a
);
488 return ndr_pull_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
489 ndr
->flags
& LIBNDR_STRING_FLAGS
);
492 ndr
->flags
= saved_flags
;
493 return NDR_ERR_SUCCESS
;
497 push a general string array onto the wire
499 _PUBLIC_
enum ndr_err_code
ndr_push_string_array(struct ndr_push
*ndr
, int ndr_flags
, const char **a
)
502 unsigned flags
= ndr
->flags
;
503 unsigned saved_flags
= ndr
->flags
;
505 if (!(ndr_flags
& NDR_SCALARS
)) {
506 return NDR_ERR_SUCCESS
;
509 switch (flags
& LIBNDR_STRING_FLAGS
) {
510 case LIBNDR_FLAG_STR_NULLTERM
:
511 for (count
= 0; a
&& a
[count
]; count
++) {
512 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, a
[count
]));
514 /* If LIBNDR_FLAG_REMAINING then we do not add a null terminator to the array */
515 if (!(flags
& LIBNDR_FLAG_REMAINING
))
517 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, ""));
521 case LIBNDR_FLAG_STR_NOTERM
:
522 if (!(ndr
->flags
& LIBNDR_FLAG_REMAINING
)) {
523 return ndr_push_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
524 ndr
->flags
& LIBNDR_STRING_FLAGS
);
527 for (count
= 0; a
&& a
[count
]; count
++) {
529 ndr
->flags
&= ~(LIBNDR_FLAG_STR_NOTERM
|LIBNDR_FLAG_REMAINING
);
530 ndr
->flags
|= LIBNDR_FLAG_STR_NULLTERM
;
531 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, ""));
532 ndr
->flags
= saved_flags
;
534 NDR_CHECK(ndr_push_string(ndr
, ndr_flags
, a
[count
]));
540 return ndr_push_error(ndr
, NDR_ERR_STRING
, "Bad string flags 0x%x\n",
541 ndr
->flags
& LIBNDR_STRING_FLAGS
);
544 ndr
->flags
= saved_flags
;
545 return NDR_ERR_SUCCESS
;
548 _PUBLIC_
void ndr_print_string_array(struct ndr_print
*ndr
, const char *name
, const char **a
)
553 for (count
= 0; a
&& a
[count
]; count
++) {}
555 ndr
->print(ndr
, "%s: ARRAY(%d)", name
, count
);
557 for (i
=0;i
<count
;i
++) {
559 if (asprintf(&idx
, "[%d]", i
) != -1) {
560 ndr_print_string(ndr
, idx
, a
[i
]);
567 _PUBLIC_
size_t ndr_size_string_array(const char **a
, uint32_t count
, int flags
)
573 if (flags
& LIBNDR_FLAG_STR_RAW8
) {
575 flags
&= ~LIBNDR_FLAG_STR_RAW8
;
578 switch (flags
& LIBNDR_STRING_FLAGS
) {
579 case LIBNDR_FLAG_STR_NULLTERM
:
580 for (i
= 0; i
< count
; i
++) {
581 size
+= rawbytes
?strlen(a
[i
]) + 1:strlen_m_term(a
[i
]);
584 case LIBNDR_FLAG_STR_NOTERM
:
585 for (i
= 0; i
< count
; i
++) {
586 size
+= rawbytes
?strlen(a
[i
]):strlen_m(a
[i
]);
597 * Return number of elements in a string including the last (zeroed) element
599 _PUBLIC_
uint32_t ndr_string_length(const void *_var
, uint32_t element_size
)
602 uint8_t zero
[4] = {0,0,0,0};
603 const char *var
= (const char *)_var
;
605 for (i
= 0; memcmp(var
+i
*element_size
,zero
,element_size
) != 0; i
++);
611 * @brief Get the string length including the null terminator if available.
613 * This checks the string length based on the elements. The returned number
614 * includes the terminating null byte(s) if found.
616 * @param[in] _var The string the calculate the length for.
618 * @param[in] length The length of the buffer passed by _var.
620 * @param[in] element_size The element_size of a string char in bytes.
622 * @return The length of the strings or 0.
624 static uint32_t ndr_string_n_length(const void *_var
,
626 uint32_t element_size
)
629 uint8_t zero
[4] = {0,0,0,0};
630 const char *var
= (const char *)_var
;
633 if (element_size
> 4) {
637 for (i
= 0; i
< length
; i
++, var
+= element_size
) {
638 cmp
= memcmp(var
, zero
, element_size
);
651 _PUBLIC_
enum ndr_err_code
ndr_check_string_terminator(struct ndr_pull
*ndr
, uint32_t count
, uint32_t element_size
)
654 uint32_t save_offset
;
656 save_offset
= ndr
->offset
;
657 NDR_CHECK(ndr_pull_advance(ndr
, (count
- 1) * element_size
));
658 NDR_PULL_NEED_BYTES(ndr
, element_size
);
660 for (i
= 0; i
< element_size
; i
++) {
661 if (ndr
->data
[ndr
->offset
+i
] != 0) {
662 ndr
->offset
= save_offset
;
664 return ndr_pull_error(ndr
, NDR_ERR_ARRAY_SIZE
, "String terminator not present or outside string boundaries");
668 ndr
->offset
= save_offset
;
670 return NDR_ERR_SUCCESS
;
673 _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
)
675 size_t converted_size
;
678 *var
= talloc_strdup(ndr
->current_mem_ctx
, "");
679 return NDR_ERR_SUCCESS
;
682 if (NDR_BE(ndr
) && chset
== CH_UTF16
) {
686 if ((byte_mul
!= 0) && (length
> UINT32_MAX
/byte_mul
)) {
687 return ndr_pull_error(ndr
, NDR_ERR_BUFSIZE
, "length overflow");
689 NDR_PULL_NEED_BYTES(ndr
, length
*byte_mul
);
691 if (!convert_string_talloc(ndr
->current_mem_ctx
, chset
, CH_UNIX
,
692 ndr
->data
+ndr
->offset
, length
*byte_mul
,
693 discard_const_p(void *, var
),
696 return ndr_pull_error(ndr
, NDR_ERR_CHARCNV
,
697 "Bad character conversion");
699 NDR_CHECK(ndr_pull_advance(ndr
, length
*byte_mul
));
701 return NDR_ERR_SUCCESS
;
704 _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
)
706 size_t converted_size
;
710 *var
= talloc_strdup(ndr
->current_mem_ctx
, "");
711 return NDR_ERR_SUCCESS
;
714 if (NDR_BE(ndr
) && chset
== CH_UTF16
) {
718 NDR_PULL_NEED_BYTES(ndr
, length
*byte_mul
);
720 str_len
= ndr_string_n_length(ndr
->data
+ndr
->offset
, length
, byte_mul
);
722 return ndr_pull_error(ndr
, NDR_ERR_LENGTH
,
726 if (!convert_string_talloc(ndr
->current_mem_ctx
, chset
, CH_UNIX
,
727 ndr
->data
+ndr
->offset
, str_len
*byte_mul
,
728 discard_const_p(void *, var
),
731 return ndr_pull_error(ndr
, NDR_ERR_CHARCNV
,
732 "Bad character conversion");
734 NDR_CHECK(ndr_pull_advance(ndr
, length
*byte_mul
));
736 return NDR_ERR_SUCCESS
;
739 _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
)
743 if (NDR_BE(ndr
) && chset
== CH_UTF16
) {
747 if ((byte_mul
!= 0) && (length
> SIZE_MAX
/byte_mul
)) {
748 return ndr_push_error(ndr
, NDR_ERR_LENGTH
, "length overflow");
750 required
= byte_mul
* length
;
752 NDR_PUSH_NEED_BYTES(ndr
, required
);
758 return ndr_push_error(ndr
, NDR_ERR_INVALID_POINTER
, "NULL [ref] pointer");
761 if (!convert_string(CH_UNIX
, chset
,
763 ndr
->data
+ndr
->offset
, required
, &size
)) {
764 return ndr_push_error(ndr
, NDR_ERR_CHARCNV
,
765 "Bad character conversion");
768 /* Make sure the remaining part of the string is filled with zeroes */
769 if (size
< required
) {
770 memset(ndr
->data
+ndr
->offset
+size
, 0, required
-size
);
774 ndr
->offset
+= required
;
776 return NDR_ERR_SUCCESS
;
779 _PUBLIC_
enum ndr_err_code
ndr_push_charset_to_null(struct ndr_push
*ndr
, int ndr_flags
, const char *var
, uint32_t length
, uint8_t byte_mul
, charset_t chset
)
781 const char *str
= var
;
784 str
= "\0"; /* i.e. two zero bytes, for UTF16 null word. */
788 return ndr_push_charset(ndr
, ndr_flags
, str
, length
, byte_mul
, chset
);
791 /* Return number of elements in a string in the specified charset */
792 _PUBLIC_
uint32_t ndr_charset_length(const void *var
, charset_t chset
)
795 /* case CH_UTF16: this has the same value as CH_UTF16LE */
800 return strlen_m_ext_term((const char *)var
, CH_UNIX
, chset
);
803 return strlen((const char *)var
)+1;
806 /* Fallback, this should never happen */
807 return strlen((const char *)var
)+1;