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;
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 flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
60 if (flags & LIBNDR_FLAG_STR_CHARLEN) {
62 flags &= ~LIBNDR_FLAG_STR_CHARLEN;
65 switch (flags & LIBNDR_STRING_FLAGS) {
66 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
67 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4|LIBNDR_FLAG_STR_NOTERM:
68 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
69 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &ofs));
71 return ndr_pull_error(ndr, NDR_ERR_STRING, "non-zero array offset with string flags 0x%x\n",
72 ndr->flags & LIBNDR_STRING_FLAGS);
74 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len2));
76 return ndr_pull_error(ndr, NDR_ERR_STRING,
77 "Bad string lengths len1=%u ofs=%u len2=%u\n",
79 } else if (len1 != len2) {
80 DEBUG(6,("len1[%u] != len2[%u] '%s'\n", len1, len2, as));
82 conv_src_len = len2 + c_len_term;
85 case LIBNDR_FLAG_STR_SIZE4:
86 case LIBNDR_FLAG_STR_SIZE4|LIBNDR_FLAG_STR_NOTERM:
87 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
88 conv_src_len = len1 + c_len_term;
91 case LIBNDR_FLAG_STR_LEN4:
92 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_NOTERM:
93 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &ofs));
95 return ndr_pull_error(ndr, NDR_ERR_STRING, "non-zero array offset with string flags 0x%x\n",
96 ndr->flags & LIBNDR_STRING_FLAGS);
98 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
99 conv_src_len = len1 + c_len_term;
102 case LIBNDR_FLAG_STR_SIZE2:
103 case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM:
104 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
105 conv_src_len = len3 + c_len_term;
108 case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_STR_BYTESIZE:
109 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
111 byte_mul = 1; /* the length is now absolute */
114 case LIBNDR_FLAG_STR_NULLTERM:
116 conv_src_len = ascii_len_n((const char *)(ndr->data+ndr->offset), ndr->data_size - ndr->offset);
118 conv_src_len = utf16_len_n(ndr->data+ndr->offset, ndr->data_size - ndr->offset);
120 byte_mul = 1; /* the length is now absolute */
123 case LIBNDR_FLAG_STR_NOTERM:
124 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
125 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
126 ndr->flags & LIBNDR_STRING_FLAGS);
128 conv_src_len = ndr->data_size - ndr->offset;
129 byte_mul = 1; /* the length is now absolute */
133 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
134 ndr->flags & LIBNDR_STRING_FLAGS);
137 NDR_PULL_NEED_BYTES(ndr, conv_src_len * byte_mul);
138 if (conv_src_len == 0) {
139 as = talloc_strdup(ndr->current_mem_ctx, "");
141 if (!convert_string_talloc(ndr->current_mem_ctx, chset,
142 CH_UNIX, ndr->data + ndr->offset,
143 conv_src_len * byte_mul,
144 (void **)(void *)&as,
146 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
147 "Bad character conversion with flags 0x%x", flags);
151 /* this is a way of detecting if a string is sent with the wrong
153 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
154 if (strlen(as) < conv_src_len) {
155 DEBUG(6,("short string '%s'\n", as));
158 if (strlen(as) == conv_src_len) {
159 DEBUG(6,("long string '%s'\n", as));
163 NDR_CHECK(ndr_pull_advance(ndr, conv_src_len * byte_mul));
166 return NDR_ERR_SUCCESS;
171 push a general string onto the wire
173 _PUBLIC_ enum ndr_err_code ndr_push_string(struct ndr_push *ndr, int ndr_flags, const char *s)
175 ssize_t s_len, c_len;
177 int chset = CH_UTF16;
178 unsigned flags = ndr->flags;
179 unsigned byte_mul = 2;
180 uint8_t *dest = NULL;
182 if (!(ndr_flags & NDR_SCALARS)) {
183 return NDR_ERR_SUCCESS;
190 s_len = s?strlen(s):0;
192 if (flags & LIBNDR_FLAG_STR_ASCII) {
195 flags &= ~LIBNDR_FLAG_STR_ASCII;
198 if (flags & LIBNDR_FLAG_STR_UTF8) {
201 flags &= ~LIBNDR_FLAG_STR_UTF8;
204 flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
206 if (!(flags & LIBNDR_FLAG_STR_NOTERM)) {
209 if (!convert_string_talloc(ndr, CH_UNIX, chset, s, s_len,
210 (void **)(void *)&dest, &d_len))
212 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
213 "Bad character push conversion with flags 0x%x", flags);
216 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
218 flags &= ~LIBNDR_FLAG_STR_BYTESIZE;
219 } else if (flags & LIBNDR_FLAG_STR_CHARLEN) {
220 c_len = (d_len / byte_mul)-1;
221 flags &= ~LIBNDR_FLAG_STR_CHARLEN;
223 c_len = d_len / byte_mul;
226 switch ((flags & LIBNDR_STRING_FLAGS) & ~LIBNDR_FLAG_STR_NOTERM) {
227 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
228 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
229 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
230 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
231 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
234 case LIBNDR_FLAG_STR_LEN4:
235 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
236 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
237 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
240 case LIBNDR_FLAG_STR_SIZE4:
241 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
242 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
245 case LIBNDR_FLAG_STR_SIZE2:
246 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, c_len));
247 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
250 case LIBNDR_FLAG_STR_NULLTERM:
251 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
255 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
256 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
260 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
261 ndr->flags & LIBNDR_STRING_FLAGS);
266 return NDR_ERR_SUCCESS;
270 push a general string onto the wire
272 _PUBLIC_ size_t ndr_string_array_size(struct ndr_push *ndr, const char *s)
275 unsigned flags = ndr->flags;
276 unsigned byte_mul = 2;
277 unsigned c_len_term = 1;
279 c_len = s?strlen_m(s):0;
281 if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_UTF8)) {
285 if (flags & LIBNDR_FLAG_STR_NOTERM) {
289 c_len = c_len + c_len_term;
291 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
292 c_len = c_len * byte_mul;
298 _PUBLIC_ void ndr_print_string(struct ndr_print *ndr, const char *name, const char *s)
301 ndr->print(ndr, "%-25s: '%s'", name, s);
303 ndr->print(ndr, "%-25s: NULL", name);
307 _PUBLIC_ uint32_t ndr_size_string(int ret, const char * const* string, int flags)
309 /* FIXME: Is this correct for all strings ? */
310 if(!(*string)) return ret;
311 return ret+strlen(*string)+1;
315 pull a general string array from the wire
317 _PUBLIC_ enum ndr_err_code ndr_pull_string_array(struct ndr_pull *ndr, int ndr_flags, const char ***_a)
319 const char **a = NULL;
321 unsigned flags = ndr->flags;
322 unsigned saved_flags = ndr->flags;
324 if (!(ndr_flags & NDR_SCALARS)) {
325 return NDR_ERR_SUCCESS;
328 switch (flags & (LIBNDR_FLAG_STR_NULLTERM|LIBNDR_FLAG_STR_NOTERM)) {
329 case LIBNDR_FLAG_STR_NULLTERM:
331 * here the strings are null terminated
332 * but also the array is null terminated if LIBNDR_FLAG_REMAINING
335 for (count = 0;; count++) {
337 const char *s = NULL;
338 a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
339 NDR_ERR_HAVE_NO_MEMORY(a);
343 tmp_ctx = ndr->current_mem_ctx;
344 ndr->current_mem_ctx = a;
345 NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
346 if ((ndr->data_size - ndr->offset) == 0 && ndr->flags & LIBNDR_FLAG_REMAINING)
351 ndr->current_mem_ctx = tmp_ctx;
352 if (strcmp("", s)==0) {
363 case LIBNDR_FLAG_STR_NOTERM:
364 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
365 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
366 ndr->flags & LIBNDR_STRING_FLAGS);
369 * here the strings are not null terminated
370 * but serarated by a null terminator
372 * which means the same as:
373 * Every string is null terminated exept the last
374 * string is terminated by the end of the buffer
376 * as LIBNDR_FLAG_STR_NULLTERM also end at the end
377 * of the buffer, we can pull each string with this flag
379 * The big difference with the case LIBNDR_FLAG_STR_NOTERM +
380 * LIBNDR_FLAG_REMAINING is that the last string will not be null terminated
382 ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
383 ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
385 for (count = 0; ((ndr->data_size - ndr->offset) > 0); count++) {
387 const char *s = NULL;
388 a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
389 NDR_ERR_HAVE_NO_MEMORY(a);
393 tmp_ctx = ndr->current_mem_ctx;
394 ndr->current_mem_ctx = a;
395 NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
396 ndr->current_mem_ctx = tmp_ctx;
404 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
405 ndr->flags & LIBNDR_STRING_FLAGS);
408 ndr->flags = saved_flags;
409 return NDR_ERR_SUCCESS;
413 push a general string array onto the wire
415 _PUBLIC_ enum ndr_err_code ndr_push_string_array(struct ndr_push *ndr, int ndr_flags, const char **a)
418 unsigned flags = ndr->flags;
419 unsigned saved_flags = ndr->flags;
421 if (!(ndr_flags & NDR_SCALARS)) {
422 return NDR_ERR_SUCCESS;
425 switch (flags & LIBNDR_STRING_FLAGS) {
426 case LIBNDR_FLAG_STR_NULLTERM:
427 for (count = 0; a && a[count]; count++) {
428 NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
430 /* If LIBNDR_FLAG_REMAINING then we do not add a null terminator to the array */
431 if (!(flags & LIBNDR_FLAG_REMAINING))
433 NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
437 case LIBNDR_FLAG_STR_NOTERM:
438 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
439 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
440 ndr->flags & LIBNDR_STRING_FLAGS);
443 for (count = 0; a && a[count]; count++) {
445 ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
446 ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
447 NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
448 ndr->flags = saved_flags;
450 NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
456 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
457 ndr->flags & LIBNDR_STRING_FLAGS);
460 ndr->flags = saved_flags;
461 return NDR_ERR_SUCCESS;
464 _PUBLIC_ void ndr_print_string_array(struct ndr_print *ndr, const char *name, const char **a)
469 for (count = 0; a && a[count]; count++) {}
471 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
473 for (i=0;i<count;i++) {
475 if (asprintf(&idx, "[%d]", i) != -1) {
476 ndr_print_string(ndr, idx, a[i]);
483 _PUBLIC_ size_t ndr_size_string_array(const char **a, uint32_t count, int flags)
488 switch (flags & LIBNDR_STRING_FLAGS) {
489 case LIBNDR_FLAG_STR_NULLTERM:
490 for (i = 0; i < count; i++) {
491 size += strlen_m_term(a[i]);
494 case LIBNDR_FLAG_STR_NOTERM:
495 for (i = 0; i < count; i++) {
496 size += strlen_m(a[i]);
507 * Return number of elements in a string including the last (zeroed) element
509 _PUBLIC_ uint32_t ndr_string_length(const void *_var, uint32_t element_size)
512 uint8_t zero[4] = {0,0,0,0};
513 const char *var = (const char *)_var;
515 for (i = 0; memcmp(var+i*element_size,zero,element_size) != 0; i++);
520 _PUBLIC_ enum ndr_err_code ndr_check_string_terminator(struct ndr_pull *ndr, uint32_t count, uint32_t element_size)
523 uint32_t save_offset;
525 save_offset = ndr->offset;
526 ndr_pull_advance(ndr, (count - 1) * element_size);
527 NDR_PULL_NEED_BYTES(ndr, element_size);
529 for (i = 0; i < element_size; i++) {
530 if (ndr->data[ndr->offset+i] != 0) {
531 ndr->offset = save_offset;
533 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, "String terminator not present or outside string boundaries");
537 ndr->offset = save_offset;
539 return NDR_ERR_SUCCESS;
542 _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)
544 size_t converted_size;
547 *var = talloc_strdup(ndr->current_mem_ctx, "");
548 return NDR_ERR_SUCCESS;
551 if (NDR_BE(ndr) && chset == CH_UTF16) {
555 NDR_PULL_NEED_BYTES(ndr, length*byte_mul);
557 if (!convert_string_talloc(ndr->current_mem_ctx, chset, CH_UNIX,
558 ndr->data+ndr->offset, length*byte_mul,
559 discard_const_p(void *, var),
562 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
563 "Bad character conversion");
565 NDR_CHECK(ndr_pull_advance(ndr, length*byte_mul));
567 return NDR_ERR_SUCCESS;
570 _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)
574 if (NDR_BE(ndr) && chset == CH_UTF16) {
578 required = byte_mul * length;
580 NDR_PUSH_NEED_BYTES(ndr, required);
584 if (!convert_string(CH_UNIX, chset,
586 ndr->data+ndr->offset, required, &size)) {
587 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
588 "Bad character conversion");
591 /* Make sure the remaining part of the string is filled with zeroes */
592 if (size < required) {
593 memset(ndr->data+ndr->offset+size, 0, required-size);
597 ndr->offset += required;
599 return NDR_ERR_SUCCESS;
602 /* Return number of elements in a string in the specified charset */
603 _PUBLIC_ uint32_t ndr_charset_length(const void *var, charset_t chset)
606 /* case CH_UTF16: this has the same value as CH_UTF16LE */
611 return strlen_m_ext_term((const char *)var, CH_UNIX, chset);
615 return strlen((const char *)var)+1;
618 /* Fallback, this should never happen */
619 return strlen((const char *)var)+1;