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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "librpc/ndr/libndr.h"
27 pull a general string from the wire
29 _PUBLIC_ NTSTATUS ndr_pull_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
32 uint32_t len1, ofs, len2;
36 unsigned byte_mul = 2;
37 unsigned flags = ndr->flags;
38 unsigned c_len_term = 0;
40 if (!(ndr_flags & NDR_SCALARS)) {
48 if (flags & LIBNDR_FLAG_STR_ASCII) {
51 flags &= ~LIBNDR_FLAG_STR_ASCII;
54 if (flags & LIBNDR_FLAG_STR_UTF8) {
57 flags &= ~LIBNDR_FLAG_STR_UTF8;
60 flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
61 if (flags & LIBNDR_FLAG_STR_CHARLEN) {
63 flags &= ~LIBNDR_FLAG_STR_CHARLEN;
66 switch (flags & LIBNDR_STRING_FLAGS) {
67 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
68 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4|LIBNDR_FLAG_STR_NOTERM:
69 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
70 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &ofs));
72 return ndr_pull_error(ndr, NDR_ERR_STRING, "non-zero array offset with string flags 0x%x\n",
73 ndr->flags & LIBNDR_STRING_FLAGS);
75 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len2));
77 return ndr_pull_error(ndr, NDR_ERR_STRING,
78 "Bad string lengths len1=%u ofs=%u len2=%u\n",
81 NDR_PULL_NEED_BYTES(ndr, (len2 + c_len_term)*byte_mul);
83 as = talloc_strdup(ndr->current_mem_ctx, "");
85 ret = convert_string_talloc(ndr->current_mem_ctx,
87 ndr->data+ndr->offset,
88 (len2 + c_len_term)*byte_mul,
91 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
92 "Bad character conversion");
95 NDR_CHECK(ndr_pull_advance(ndr, (len2 + c_len_term)*byte_mul));
98 DEBUG(6,("len1[%u] != len2[%u] '%s'\n", len1, len2, as));
101 /* this is a way of detecting if a string is sent with the wrong
103 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
104 if (strlen(as) < (len2 + c_len_term)) {
105 DEBUG(6,("short string '%s'\n", as));
108 if (strlen(as) == (len2 + c_len_term)) {
109 DEBUG(6,("long string '%s'\n", as));
115 case LIBNDR_FLAG_STR_SIZE4:
116 case LIBNDR_FLAG_STR_SIZE4|LIBNDR_FLAG_STR_NOTERM:
117 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
118 NDR_PULL_NEED_BYTES(ndr, (len1 + c_len_term)*byte_mul);
120 as = talloc_strdup(ndr->current_mem_ctx, "");
122 ret = convert_string_talloc(ndr->current_mem_ctx,
124 ndr->data+ndr->offset,
125 (len1 + c_len_term)*byte_mul,
128 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
129 "Bad character conversion");
132 NDR_CHECK(ndr_pull_advance(ndr, (len1 + c_len_term)*byte_mul));
134 /* this is a way of detecting if a string is sent with the wrong
136 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
137 if (strlen(as) < (len1 + c_len_term)) {
138 DEBUG(6,("short string '%s'\n", as));
141 if (strlen(as) == (len1 + c_len_term)) {
142 DEBUG(6,("long string '%s'\n", as));
148 case LIBNDR_FLAG_STR_LEN4:
149 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_NOTERM:
150 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &ofs));
152 return ndr_pull_error(ndr, NDR_ERR_STRING, "non-zero array offset with string flags 0x%x\n",
153 ndr->flags & LIBNDR_STRING_FLAGS);
155 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
156 NDR_PULL_NEED_BYTES(ndr, (len1 + c_len_term)*byte_mul);
158 as = talloc_strdup(ndr->current_mem_ctx, "");
160 ret = convert_string_talloc(ndr->current_mem_ctx,
162 ndr->data+ndr->offset,
163 (len1 + c_len_term)*byte_mul,
166 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
167 "Bad character conversion");
170 NDR_CHECK(ndr_pull_advance(ndr, (len1 + c_len_term)*byte_mul));
172 /* this is a way of detecting if a string is sent with the wrong
174 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
175 if (strlen(as) < (len1 + c_len_term)) {
176 DEBUG(6,("short string '%s'\n", as));
179 if (strlen(as) == (len1 + c_len_term)) {
180 DEBUG(6,("long string '%s'\n", as));
187 case LIBNDR_FLAG_STR_SIZE2:
188 case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM:
189 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
190 NDR_PULL_NEED_BYTES(ndr, (len3 + c_len_term)*byte_mul);
192 as = talloc_strdup(ndr->current_mem_ctx, "");
194 ret = convert_string_talloc(ndr->current_mem_ctx,
196 ndr->data+ndr->offset,
197 (len3 + c_len_term)*byte_mul,
200 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
201 "Bad character conversion");
204 NDR_CHECK(ndr_pull_advance(ndr, (len3 + c_len_term)*byte_mul));
206 /* this is a way of detecting if a string is sent with the wrong
208 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
209 if (strlen(as) < (len3 + c_len_term)) {
210 DEBUG(6,("short string '%s'\n", as));
213 if (strlen(as) == (len3 + c_len_term)) {
214 DEBUG(6,("long string '%s'\n", as));
220 case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_STR_BYTESIZE:
221 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
222 NDR_PULL_NEED_BYTES(ndr, len3);
224 as = talloc_strdup(ndr->current_mem_ctx, "");
226 ret = convert_string_talloc(ndr->current_mem_ctx,
228 ndr->data+ndr->offset,
232 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
233 "Bad character conversion");
236 NDR_CHECK(ndr_pull_advance(ndr, len3));
240 case LIBNDR_FLAG_STR_NULLTERM:
242 len1 = ascii_len_n((const char *)(ndr->data+ndr->offset), ndr->data_size - ndr->offset);
244 len1 = utf16_len_n(ndr->data+ndr->offset, ndr->data_size - ndr->offset);
246 ret = convert_string_talloc(ndr->current_mem_ctx,
248 ndr->data+ndr->offset,
252 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
253 "Bad character conversion");
255 NDR_CHECK(ndr_pull_advance(ndr, len1));
259 case LIBNDR_FLAG_STR_FIXLEN15:
260 case LIBNDR_FLAG_STR_FIXLEN32:
261 len1 = (flags & LIBNDR_FLAG_STR_FIXLEN32)?32:15;
262 NDR_PULL_NEED_BYTES(ndr, len1*byte_mul);
263 ret = convert_string_talloc(ndr->current_mem_ctx,
265 ndr->data+ndr->offset,
269 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
270 "Bad character conversion");
272 NDR_CHECK(ndr_pull_advance(ndr, len1*byte_mul));
276 case LIBNDR_FLAG_STR_NOTERM:
277 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
278 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
279 ndr->flags & LIBNDR_STRING_FLAGS);
282 len1 = ndr->data_size - ndr->offset;
284 NDR_PULL_NEED_BYTES(ndr, len1);
286 as = talloc_strdup(ndr->current_mem_ctx, "");
288 ret = convert_string_talloc(ndr->current_mem_ctx,
290 ndr->data+ndr->offset,
294 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
295 "Bad character conversion");
298 NDR_CHECK(ndr_pull_advance(ndr, len1));
304 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
305 ndr->flags & LIBNDR_STRING_FLAGS);
313 push a general string onto the wire
315 _PUBLIC_ NTSTATUS ndr_push_string(struct ndr_push *ndr, int ndr_flags, const char *s)
317 ssize_t s_len, c_len, d_len;
318 int chset = CH_UTF16;
319 unsigned flags = ndr->flags;
320 unsigned byte_mul = 2;
321 uint8_t *dest = NULL;
323 if (!(ndr_flags & NDR_SCALARS)) {
331 s_len = s?strlen(s):0;
333 if (flags & LIBNDR_FLAG_STR_ASCII) {
336 flags &= ~LIBNDR_FLAG_STR_ASCII;
339 if (flags & LIBNDR_FLAG_STR_UTF8) {
342 flags &= ~LIBNDR_FLAG_STR_UTF8;
345 flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
348 (LIBNDR_FLAG_STR_NOTERM |
349 LIBNDR_FLAG_STR_FIXLEN15 |
350 LIBNDR_FLAG_STR_FIXLEN32))) {
353 d_len = convert_string_talloc(ndr, CH_UNIX, chset, s, s_len, (void **)&dest);
355 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
356 "Bad character conversion");
359 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
361 flags &= ~LIBNDR_FLAG_STR_BYTESIZE;
362 } else if (flags & LIBNDR_FLAG_STR_CHARLEN) {
363 c_len = (d_len / byte_mul)-1;
364 flags &= ~LIBNDR_FLAG_STR_CHARLEN;
366 c_len = d_len / byte_mul;
369 switch ((flags & LIBNDR_STRING_FLAGS) & ~LIBNDR_FLAG_STR_NOTERM) {
370 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
371 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
372 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
373 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
374 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
377 case LIBNDR_FLAG_STR_LEN4:
378 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
379 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
380 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
383 case LIBNDR_FLAG_STR_SIZE4:
384 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
385 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
388 case LIBNDR_FLAG_STR_SIZE2:
389 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, c_len));
390 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
393 case LIBNDR_FLAG_STR_NULLTERM:
394 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
397 case LIBNDR_FLAG_STR_FIXLEN15:
398 case LIBNDR_FLAG_STR_FIXLEN32: {
399 ssize_t fix_len = (flags & LIBNDR_FLAG_STR_FIXLEN32)?32:15;
400 uint32_t pad_len = fix_len - d_len;
401 if (d_len > fix_len) {
402 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
403 "Bad character conversion");
405 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
407 NDR_CHECK(ndr_push_zero(ndr, pad_len));
413 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
414 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
418 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
419 ndr->flags & LIBNDR_STRING_FLAGS);
428 push a general string onto the wire
430 _PUBLIC_ size_t ndr_string_array_size(struct ndr_push *ndr, const char *s)
433 unsigned flags = ndr->flags;
434 unsigned byte_mul = 2;
435 unsigned c_len_term = 1;
437 if (flags & LIBNDR_FLAG_STR_FIXLEN32) {
440 if (flags & LIBNDR_FLAG_STR_FIXLEN15) {
444 c_len = s?strlen_m(s):0;
446 if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_UTF8)) {
450 if (flags & LIBNDR_FLAG_STR_NOTERM) {
454 c_len = c_len + c_len_term;
456 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
457 c_len = c_len * byte_mul;
463 _PUBLIC_ void ndr_print_string(struct ndr_print *ndr, const char *name, const char *s)
466 ndr->print(ndr, "%-25s: '%s'", name, s);
468 ndr->print(ndr, "%-25s: NULL", name);
472 _PUBLIC_ uint32_t ndr_size_string(int ret, const char * const* string, int flags)
474 /* FIXME: Is this correct for all strings ? */
475 if(!(*string)) return ret;
476 return ret+strlen(*string)+1;
480 pull a general string array from the wire
482 _PUBLIC_ NTSTATUS ndr_pull_string_array(struct ndr_pull *ndr, int ndr_flags, const char ***_a)
484 const char **a = *_a;
486 unsigned flags = ndr->flags;
487 unsigned saved_flags = ndr->flags;
489 if (!(ndr_flags & NDR_SCALARS)) {
493 switch (flags & LIBNDR_STRING_FLAGS) {
494 case LIBNDR_FLAG_STR_NULLTERM:
496 * here the strings are null terminated
497 * but also the array is null terminated
499 for (count = 0;; count++) {
501 const char *s = NULL;
502 a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
503 NT_STATUS_HAVE_NO_MEMORY(a);
507 tmp_ctx = ndr->current_mem_ctx;
508 ndr->current_mem_ctx = a;
509 NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
510 ndr->current_mem_ctx = tmp_ctx;
511 if (strcmp("", s)==0) {
522 case LIBNDR_FLAG_STR_NOTERM:
523 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
524 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
525 ndr->flags & LIBNDR_STRING_FLAGS);
528 * here the strings are not null terminated
529 * but serarated by a null terminator
531 * which means the same as:
532 * very string is null terminated exept the last
533 * string is terminated by the end of the buffer
535 * as LIBNDR_FLAG_STR_NULLTERM also end at the end
536 * of the buffer, we can pull each string with this flag
538 ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
539 ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
541 for (count = 0; ((ndr->data_size - ndr->offset) > 0); count++) {
543 const char *s = NULL;
544 a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
545 NT_STATUS_HAVE_NO_MEMORY(a);
549 tmp_ctx = ndr->current_mem_ctx;
550 ndr->current_mem_ctx = a;
551 NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
552 ndr->current_mem_ctx = tmp_ctx;
560 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
561 ndr->flags & LIBNDR_STRING_FLAGS);
564 ndr->flags = saved_flags;
569 push a general string array onto the wire
571 _PUBLIC_ NTSTATUS ndr_push_string_array(struct ndr_push *ndr, int ndr_flags, const char **a)
574 unsigned flags = ndr->flags;
575 unsigned saved_flags = ndr->flags;
577 if (!(ndr_flags & NDR_SCALARS)) {
581 switch (flags & LIBNDR_STRING_FLAGS) {
582 case LIBNDR_FLAG_STR_NULLTERM:
583 for (count = 0; a && a[count]; count++) {
584 NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
587 NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
590 case LIBNDR_FLAG_STR_NOTERM:
591 if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
592 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
593 ndr->flags & LIBNDR_STRING_FLAGS);
596 for (count = 0; a && a[count]; count++) {
598 ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
599 ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
600 NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
601 ndr->flags = saved_flags;
603 NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
609 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
610 ndr->flags & LIBNDR_STRING_FLAGS);
613 ndr->flags = saved_flags;
617 _PUBLIC_ void ndr_print_string_array(struct ndr_print *ndr, const char *name, const char **a)
622 for (count = 0; a && a[count]; count++) {}
624 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
626 for (i=0;i<count;i++) {
628 asprintf(&idx, "[%d]", i);
630 ndr_print_string(ndr, idx, a[i]);
638 * Return number of elements in a string including the last (zeroed) element
640 _PUBLIC_ uint32_t ndr_string_length(const void *_var, uint32_t element_size)
643 uint8_t zero[4] = {0,0,0,0};
644 const char *var = _var;
646 for (i = 0; memcmp(var+i*element_size,zero,element_size) != 0; i++);
651 _PUBLIC_ NTSTATUS ndr_check_string_terminator(struct ndr_pull *ndr, uint32_t count, uint32_t element_size)
654 struct ndr_pull_save save_offset;
656 ndr_pull_save(ndr, &save_offset);
657 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_pull_restore(ndr, &save_offset);
664 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, "String terminator not present or outside string boundaries");
668 ndr_pull_restore(ndr, &save_offset);
673 _PUBLIC_ NTSTATUS ndr_pull_charset(struct ndr_pull *ndr, int ndr_flags, const char **var, uint32_t length, uint8_t byte_mul, charset_t chset)
677 *var = talloc_strdup(ndr->current_mem_ctx, "");
681 if (NDR_BE(ndr) && chset == CH_UTF16) {
685 NDR_PULL_NEED_BYTES(ndr, length*byte_mul);
687 ret = convert_string_talloc(ndr->current_mem_ctx,
689 ndr->data+ndr->offset,
691 discard_const_p(void *, var));
693 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
694 "Bad character conversion");
696 NDR_CHECK(ndr_pull_advance(ndr, length*byte_mul));
701 _PUBLIC_ NTSTATUS ndr_push_charset(struct ndr_push *ndr, int ndr_flags, const char *var, uint32_t length, uint8_t byte_mul, charset_t chset)
703 ssize_t ret, required;
705 if (NDR_BE(ndr) && chset == CH_UTF16) {
709 required = byte_mul * length;
711 NDR_PUSH_NEED_BYTES(ndr, required);
712 ret = convert_string(CH_UNIX, chset,
714 ndr->data+ndr->offset, required);
716 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
717 "Bad character conversion");
720 /* Make sure the remaining part of the string is filled with zeroes */
721 if (ret < required) {
722 memset(ndr->data+ndr->offset+ret, 0, required-ret);
725 ndr->offset += required;
730 /* Return number of elements in a string in the specified charset */
731 _PUBLIC_ uint32_t ndr_charset_length(const void *var, charset_t chset)
733 /* FIXME: Treat special chars special here, taking chset into account */
734 /* Also include 0 byte */
735 return strlen(var)+1;