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, "");
148 as = talloc_strndup(ndr->current_mem_ctx,
149 ndr->data + ndr->offset,
151 } else if (!convert_string_talloc(ndr->current_mem_ctx, chset,
152 CH_UNIX, ndr->data + ndr->offset,
153 conv_src_len * byte_mul,
154 (void **)(void *)&as,
156 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
157 "Bad character conversion with flags 0x%x", flags);
161 /* this is a way of detecting if a string is sent with the wrong
163 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
164 if (strlen(as) < conv_src_len) {
165 DEBUG(6,("short string '%s'\n", as));
168 if (strlen(as) == conv_src_len) {
169 DEBUG(6,("long string '%s'\n", as));
173 NDR_CHECK(ndr_pull_advance(ndr, conv_src_len * byte_mul));
176 return NDR_ERR_SUCCESS;
181 push a general string onto the wire
183 _PUBLIC_ enum ndr_err_code ndr_push_string(struct ndr_push *ndr, int ndr_flags, const char *s)
185 ssize_t s_len, c_len;
187 int do_convert = 1, chset = CH_UTF16;
188 unsigned flags = ndr->flags;
189 unsigned byte_mul = 2;
190 uint8_t *dest = NULL;
192 if (!(ndr_flags & NDR_SCALARS)) {
193 return NDR_ERR_SUCCESS;
200 s_len = s?strlen(s):0;
202 if (flags & LIBNDR_FLAG_STR_ASCII) {
205 flags &= ~LIBNDR_FLAG_STR_ASCII;
208 if (flags & LIBNDR_FLAG_STR_UTF8) {
211 flags &= ~LIBNDR_FLAG_STR_UTF8;
214 if (flags & LIBNDR_FLAG_STR_RAW8) {
217 flags &= ~LIBNDR_FLAG_STR_RAW8;
220 flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
222 if (!(flags & LIBNDR_FLAG_STR_NOTERM)) {
228 dest = talloc_strndup(ndr, s, s_len);
229 } else if (!convert_string_talloc(ndr, CH_UNIX, chset, s, s_len,
230 (void **)(void *)&dest, &d_len))
232 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
233 "Bad character push conversion with flags 0x%x", flags);
236 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
238 flags &= ~LIBNDR_FLAG_STR_BYTESIZE;
239 } else if (flags & LIBNDR_FLAG_STR_CHARLEN) {
240 c_len = (d_len / byte_mul)-1;
241 flags &= ~LIBNDR_FLAG_STR_CHARLEN;
243 c_len = d_len / byte_mul;
246 switch ((flags & LIBNDR_STRING_FLAGS) & ~LIBNDR_FLAG_STR_NOTERM) {
247 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
248 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
249 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
250 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
251 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
254 case LIBNDR_FLAG_STR_LEN4:
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_SIZE4:
261 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
262 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
265 case LIBNDR_FLAG_STR_SIZE2:
266 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, c_len));
267 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
270 case LIBNDR_FLAG_STR_NULLTERM:
271 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
275 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
276 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
280 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
281 ndr->flags & LIBNDR_STRING_FLAGS);
286 return NDR_ERR_SUCCESS;
290 push a general string onto the wire
292 _PUBLIC_ size_t ndr_string_array_size(struct ndr_push *ndr, const char *s)
295 unsigned flags = ndr->flags;
296 unsigned byte_mul = 2;
297 unsigned c_len_term = 1;
299 if (flags & LIBNDR_FLAG_STR_RAW8) {
300 c_len = s?strlen(s):0;
302 c_len = s?strlen_m(s):0;
305 if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_RAW8|LIBNDR_FLAG_STR_UTF8)) {
309 if (flags & LIBNDR_FLAG_STR_NOTERM) {
313 c_len = c_len + c_len_term;
315 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
316 c_len = c_len * byte_mul;
322 _PUBLIC_ void ndr_print_string(struct ndr_print *ndr, const char *name, const char *s)
325 ndr->print(ndr, "%-25s: '%s'", name, s);
327 ndr->print(ndr, "%-25s: NULL", name);
331 _PUBLIC_ uint32_t ndr_size_string(int ret, const char * const* string, int flags)
333 /* FIXME: Is this correct for all strings ? */
334 if(!(*string)) return ret;
335 return ret+strlen(*string)+1;
339 pull a general string array from the wire
341 _PUBLIC_ enum ndr_err_code ndr_pull_string_array(struct ndr_pull *ndr, int ndr_flags, const char ***_a)
343 const char **a = NULL;
345 unsigned flags = ndr->flags;
346 unsigned saved_flags = ndr->flags;
348 if (!(ndr_flags & NDR_SCALARS)) {
349 return NDR_ERR_SUCCESS;
352 switch (flags & (LIBNDR_FLAG_STR_NULLTERM|LIBNDR_FLAG_STR_NOTERM)) {
353 case LIBNDR_FLAG_STR_NULLTERM:
355 * here the strings are null terminated
356 * but also the array is null terminated if LIBNDR_FLAG_REMAINING
359 for (count = 0;; count++) {
361 const char *s = NULL;
362 a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
363 NDR_ERR_HAVE_NO_MEMORY(a);
367 tmp_ctx = ndr->current_mem_ctx;
368 ndr->current_mem_ctx = a;
369 NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
370 if ((ndr->data_size - ndr->offset) == 0 && ndr->flags & LIBNDR_FLAG_REMAINING)
375 ndr->current_mem_ctx = tmp_ctx;
376 if (strcmp("", s)==0) {
387 case LIBNDR_FLAG_STR_NOTERM:
388 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
389 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
390 ndr->flags & LIBNDR_STRING_FLAGS);
393 * here the strings are not null terminated
394 * but serarated by a null terminator
396 * which means the same as:
397 * Every string is null terminated exept the last
398 * string is terminated by the end of the buffer
400 * as LIBNDR_FLAG_STR_NULLTERM also end at the end
401 * of the buffer, we can pull each string with this flag
403 * The big difference with the case LIBNDR_FLAG_STR_NOTERM +
404 * LIBNDR_FLAG_REMAINING is that the last string will not be null terminated
406 ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
407 ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
409 for (count = 0; ((ndr->data_size - ndr->offset) > 0); count++) {
411 const char *s = NULL;
412 a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
413 NDR_ERR_HAVE_NO_MEMORY(a);
417 tmp_ctx = ndr->current_mem_ctx;
418 ndr->current_mem_ctx = a;
419 NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
420 ndr->current_mem_ctx = tmp_ctx;
428 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
429 ndr->flags & LIBNDR_STRING_FLAGS);
432 ndr->flags = saved_flags;
433 return NDR_ERR_SUCCESS;
437 push a general string array onto the wire
439 _PUBLIC_ enum ndr_err_code ndr_push_string_array(struct ndr_push *ndr, int ndr_flags, const char **a)
442 unsigned flags = ndr->flags;
443 unsigned saved_flags = ndr->flags;
445 if (!(ndr_flags & NDR_SCALARS)) {
446 return NDR_ERR_SUCCESS;
449 switch (flags & LIBNDR_STRING_FLAGS) {
450 case LIBNDR_FLAG_STR_NULLTERM:
451 for (count = 0; a && a[count]; count++) {
452 NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
454 /* If LIBNDR_FLAG_REMAINING then we do not add a null terminator to the array */
455 if (!(flags & LIBNDR_FLAG_REMAINING))
457 NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
461 case LIBNDR_FLAG_STR_NOTERM:
462 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
463 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
464 ndr->flags & LIBNDR_STRING_FLAGS);
467 for (count = 0; a && a[count]; count++) {
469 ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
470 ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
471 NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
472 ndr->flags = saved_flags;
474 NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
480 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
481 ndr->flags & LIBNDR_STRING_FLAGS);
484 ndr->flags = saved_flags;
485 return NDR_ERR_SUCCESS;
488 _PUBLIC_ void ndr_print_string_array(struct ndr_print *ndr, const char *name, const char **a)
493 for (count = 0; a && a[count]; count++) {}
495 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
497 for (i=0;i<count;i++) {
499 if (asprintf(&idx, "[%d]", i) != -1) {
500 ndr_print_string(ndr, idx, a[i]);
507 _PUBLIC_ size_t ndr_size_string_array(const char **a, uint32_t count, int flags)
513 if (flags & LIBNDR_FLAG_STR_RAW8) {
515 flags &= ~LIBNDR_FLAG_STR_RAW8;
518 switch (flags & LIBNDR_STRING_FLAGS) {
519 case LIBNDR_FLAG_STR_NULLTERM:
520 for (i = 0; i < count; i++) {
521 size += rawbytes?strlen(a[i]) + 1:strlen_m_term(a[i]);
524 case LIBNDR_FLAG_STR_NOTERM:
525 for (i = 0; i < count; i++) {
526 size += rawbytes?strlen(a[i]):strlen_m(a[i]);
537 * Return number of elements in a string including the last (zeroed) element
539 _PUBLIC_ uint32_t ndr_string_length(const void *_var, uint32_t element_size)
542 uint8_t zero[4] = {0,0,0,0};
543 const char *var = (const char *)_var;
545 for (i = 0; memcmp(var+i*element_size,zero,element_size) != 0; i++);
550 _PUBLIC_ enum ndr_err_code ndr_check_string_terminator(struct ndr_pull *ndr, uint32_t count, uint32_t element_size)
553 uint32_t save_offset;
555 save_offset = ndr->offset;
556 ndr_pull_advance(ndr, (count - 1) * element_size);
557 NDR_PULL_NEED_BYTES(ndr, element_size);
559 for (i = 0; i < element_size; i++) {
560 if (ndr->data[ndr->offset+i] != 0) {
561 ndr->offset = save_offset;
563 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, "String terminator not present or outside string boundaries");
567 ndr->offset = save_offset;
569 return NDR_ERR_SUCCESS;
572 _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)
574 size_t converted_size;
577 *var = talloc_strdup(ndr->current_mem_ctx, "");
578 return NDR_ERR_SUCCESS;
581 if (NDR_BE(ndr) && chset == CH_UTF16) {
585 NDR_PULL_NEED_BYTES(ndr, length*byte_mul);
587 if (!convert_string_talloc(ndr->current_mem_ctx, chset, CH_UNIX,
588 ndr->data+ndr->offset, length*byte_mul,
589 discard_const_p(void *, var),
592 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
593 "Bad character conversion");
595 NDR_CHECK(ndr_pull_advance(ndr, length*byte_mul));
597 return NDR_ERR_SUCCESS;
600 _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)
604 if (NDR_BE(ndr) && chset == CH_UTF16) {
608 required = byte_mul * length;
610 NDR_PUSH_NEED_BYTES(ndr, required);
614 if (!convert_string(CH_UNIX, chset,
616 ndr->data+ndr->offset, required, &size)) {
617 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
618 "Bad character conversion");
621 /* Make sure the remaining part of the string is filled with zeroes */
622 if (size < required) {
623 memset(ndr->data+ndr->offset+size, 0, required-size);
627 ndr->offset += required;
629 return NDR_ERR_SUCCESS;
632 /* Return number of elements in a string in the specified charset */
633 _PUBLIC_ uint32_t ndr_charset_length(const void *var, charset_t chset)
636 /* case CH_UTF16: this has the same value as CH_UTF16LE */
641 return strlen_m_ext_term((const char *)var, CH_UNIX, chset);
645 return strlen((const char *)var)+1;
648 /* Fallback, this should never happen */
649 return strlen((const char *)var)+1;