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/>.
25 pull a general string from the wire
27 NTSTATUS ndr_pull_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
30 uint32_t len1, ofs, len2;
33 charset_t chset = CH_UTF16LE;
34 unsigned byte_mul = 2;
35 unsigned flags = ndr->flags;
36 unsigned c_len_term = 0;
38 if (!(ndr_flags & NDR_SCALARS)) {
46 if (flags & LIBNDR_FLAG_STR_ASCII) {
49 flags &= ~LIBNDR_FLAG_STR_ASCII;
52 if (flags & LIBNDR_FLAG_STR_UTF8) {
55 flags &= ~LIBNDR_FLAG_STR_UTF8;
58 flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
59 if (flags & LIBNDR_FLAG_STR_CHARLEN) {
61 flags &= ~LIBNDR_FLAG_STR_CHARLEN;
64 switch (flags & LIBNDR_STRING_FLAGS) {
65 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
66 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4|LIBNDR_FLAG_STR_NOTERM:
67 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
68 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &ofs));
70 return ndr_pull_error(ndr, NDR_ERR_STRING, "non-zero array offset with string flags 0x%x\n",
71 ndr->flags & LIBNDR_STRING_FLAGS);
73 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len2));
75 return ndr_pull_error(ndr, NDR_ERR_STRING,
76 "Bad string lengths len1=%u ofs=%u len2=%u\n",
79 NDR_PULL_NEED_BYTES(ndr, (len2 + c_len_term)*byte_mul);
81 as = talloc_strdup(ndr->current_mem_ctx, "");
83 ret = convert_string_talloc(ndr->current_mem_ctx,
85 ndr->data+ndr->offset,
86 (len2 + c_len_term)*byte_mul,
89 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
90 "Bad character conversion");
93 NDR_CHECK(ndr_pull_advance(ndr, (len2 + c_len_term)*byte_mul));
96 DEBUG(6,("len1[%u] != len2[%u] '%s'\n", len1, len2, as));
99 /* this is a way of detecting if a string is sent with the wrong
101 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
102 if (strlen(as) < (len2 + c_len_term)) {
103 DEBUG(6,("short string '%s'\n", as));
106 if (strlen(as) == (len2 + c_len_term)) {
107 DEBUG(6,("long string '%s'\n", as));
113 case LIBNDR_FLAG_STR_SIZE4:
114 case LIBNDR_FLAG_STR_SIZE4|LIBNDR_FLAG_STR_NOTERM:
115 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
116 NDR_PULL_NEED_BYTES(ndr, (len1 + c_len_term)*byte_mul);
118 as = talloc_strdup(ndr->current_mem_ctx, "");
120 ret = convert_string_talloc(ndr->current_mem_ctx,
122 ndr->data+ndr->offset,
123 (len1 + c_len_term)*byte_mul,
126 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
127 "Bad character conversion");
130 NDR_CHECK(ndr_pull_advance(ndr, (len1 + c_len_term)*byte_mul));
132 /* this is a way of detecting if a string is sent with the wrong
134 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
135 if (strlen(as) < (len1 + c_len_term)) {
136 DEBUG(6,("short string '%s'\n", as));
139 if (strlen(as) == (len1 + c_len_term)) {
140 DEBUG(6,("long string '%s'\n", as));
146 case LIBNDR_FLAG_STR_LEN4:
147 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_NOTERM:
148 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &ofs));
150 return ndr_pull_error(ndr, NDR_ERR_STRING, "non-zero array offset with string flags 0x%x\n",
151 ndr->flags & LIBNDR_STRING_FLAGS);
153 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
154 NDR_PULL_NEED_BYTES(ndr, (len1 + c_len_term)*byte_mul);
156 as = talloc_strdup(ndr->current_mem_ctx, "");
158 ret = convert_string_talloc(ndr->current_mem_ctx,
160 ndr->data+ndr->offset,
161 (len1 + c_len_term)*byte_mul,
164 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
165 "Bad character conversion");
168 NDR_CHECK(ndr_pull_advance(ndr, (len1 + c_len_term)*byte_mul));
170 /* this is a way of detecting if a string is sent with the wrong
172 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
173 if (strlen(as) < (len1 + c_len_term)) {
174 DEBUG(6,("short string '%s'\n", as));
177 if (strlen(as) == (len1 + c_len_term)) {
178 DEBUG(6,("long string '%s'\n", as));
185 case LIBNDR_FLAG_STR_SIZE2:
186 case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM:
187 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
188 NDR_PULL_NEED_BYTES(ndr, (len3 + c_len_term)*byte_mul);
190 as = talloc_strdup(ndr->current_mem_ctx, "");
192 ret = convert_string_talloc(ndr->current_mem_ctx,
194 ndr->data+ndr->offset,
195 (len3 + c_len_term)*byte_mul,
198 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
199 "Bad character conversion");
202 NDR_CHECK(ndr_pull_advance(ndr, (len3 + c_len_term)*byte_mul));
204 /* this is a way of detecting if a string is sent with the wrong
206 if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
207 if (strlen(as) < (len3 + c_len_term)) {
208 DEBUG(6,("short string '%s'\n", as));
211 if (strlen(as) == (len3 + c_len_term)) {
212 DEBUG(6,("long string '%s'\n", as));
218 case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_STR_BYTESIZE:
219 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
220 NDR_PULL_NEED_BYTES(ndr, len3);
222 as = talloc_strdup(ndr->current_mem_ctx, "");
224 ret = convert_string_talloc(ndr->current_mem_ctx,
226 ndr->data+ndr->offset,
229 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
230 "Bad character conversion");
233 NDR_CHECK(ndr_pull_advance(ndr, len3));
237 case LIBNDR_FLAG_STR_NULLTERM:
239 len1 = ascii_len_n((const char *)(ndr->data+ndr->offset), ndr->data_size - ndr->offset);
241 len1 = utf16_len_n(ndr->data+ndr->offset, ndr->data_size - ndr->offset);
243 ret = convert_string_talloc(ndr->current_mem_ctx,
245 ndr->data+ndr->offset,
248 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
249 "Bad character conversion");
251 NDR_CHECK(ndr_pull_advance(ndr, len1));
255 case LIBNDR_FLAG_STR_FIXLEN15:
256 case LIBNDR_FLAG_STR_FIXLEN32:
257 len1 = (flags & LIBNDR_FLAG_STR_FIXLEN32)?32:15;
258 NDR_PULL_NEED_BYTES(ndr, len1*byte_mul);
259 ret = convert_string_talloc(ndr->current_mem_ctx,
261 ndr->data+ndr->offset,
262 len1*byte_mul, &as, False);
264 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
265 "Bad character conversion");
267 NDR_CHECK(ndr_pull_advance(ndr, len1*byte_mul));
272 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
273 ndr->flags & LIBNDR_STRING_FLAGS);
281 push a general string onto the wire
283 NTSTATUS ndr_push_string(struct ndr_push *ndr, int ndr_flags, const char *s)
285 ssize_t s_len, c_len, d_len;
286 charset_t chset = CH_UTF16LE;
287 unsigned flags = ndr->flags;
288 unsigned byte_mul = 2;
289 uint8_t *dest = NULL;
291 if (!(ndr_flags & NDR_SCALARS)) {
299 s_len = s?strlen(s):0;
301 if (flags & LIBNDR_FLAG_STR_ASCII) {
304 flags &= ~LIBNDR_FLAG_STR_ASCII;
307 if (flags & LIBNDR_FLAG_STR_UTF8) {
310 flags &= ~LIBNDR_FLAG_STR_UTF8;
313 flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
316 (LIBNDR_FLAG_STR_NOTERM |
317 LIBNDR_FLAG_STR_FIXLEN15 |
318 LIBNDR_FLAG_STR_FIXLEN32))) {
321 d_len = convert_string_talloc(ndr, CH_UNIX, chset, s, s_len, &dest,
324 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
325 "Bad character conversion");
328 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
330 flags &= ~LIBNDR_FLAG_STR_BYTESIZE;
331 } else if (flags & LIBNDR_FLAG_STR_CHARLEN) {
332 c_len = (d_len / byte_mul)-1;
333 flags &= ~LIBNDR_FLAG_STR_CHARLEN;
335 c_len = d_len / byte_mul;
338 switch ((flags & LIBNDR_STRING_FLAGS) & ~LIBNDR_FLAG_STR_NOTERM) {
339 case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
340 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
341 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
342 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
343 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
346 case LIBNDR_FLAG_STR_LEN4:
347 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
348 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
349 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
352 case LIBNDR_FLAG_STR_SIZE4:
353 NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
354 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
357 case LIBNDR_FLAG_STR_SIZE2:
358 NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, c_len));
359 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
362 case LIBNDR_FLAG_STR_NULLTERM:
363 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
366 case LIBNDR_FLAG_STR_FIXLEN15:
367 case LIBNDR_FLAG_STR_FIXLEN32: {
368 ssize_t fix_len = (flags & LIBNDR_FLAG_STR_FIXLEN32)?32:15;
369 uint32_t pad_len = fix_len - d_len;
370 if (d_len > fix_len) {
371 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
372 "Bad character conversion");
374 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
376 NDR_CHECK(ndr_push_zero(ndr, pad_len));
382 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
383 ndr->flags & LIBNDR_STRING_FLAGS);
392 push a general string onto the wire
394 size_t ndr_string_array_size(struct ndr_push *ndr, const char *s)
397 unsigned flags = ndr->flags;
398 unsigned byte_mul = 2;
399 unsigned c_len_term = 1;
401 if (flags & LIBNDR_FLAG_STR_FIXLEN32) {
404 if (flags & LIBNDR_FLAG_STR_FIXLEN15) {
408 c_len = s?strlen(s):0;
410 if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_UTF8)) {
414 if (flags & LIBNDR_FLAG_STR_NOTERM) {
418 c_len = c_len + c_len_term;
420 if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
421 c_len = c_len * byte_mul;
427 void ndr_print_string(struct ndr_print *ndr, const char *name, const char *s)
430 ndr->print(ndr, "%-25s: '%s'", name, s);
432 ndr->print(ndr, "%-25s: NULL", name);
436 uint32_t ndr_size_string(int ret, const char * const* string, int flags)
438 /* FIXME: Is this correct for all strings ? */
439 if(!(*string)) return ret;
440 return ret+strlen(*string)+1;
444 pull a general string array from the wire
446 NTSTATUS ndr_pull_string_array(struct ndr_pull *ndr, int ndr_flags, const char ***_a)
448 const char **a = *_a;
451 if (!(ndr_flags & NDR_SCALARS)) {
455 for (count = 0;; count++) {
457 const char *s = NULL;
458 a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
459 NT_STATUS_HAVE_NO_MEMORY(a);
463 tmp_ctx = ndr->current_mem_ctx;
464 ndr->current_mem_ctx = a;
465 NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
466 ndr->current_mem_ctx = tmp_ctx;
467 if (strcmp("", s)==0) {
480 push a general string array onto the wire
482 NTSTATUS ndr_push_string_array(struct ndr_push *ndr, int ndr_flags, const char **a)
486 if (!(ndr_flags & NDR_SCALARS)) {
490 for (count = 0; a && a[count]; count++) {
491 NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
494 NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
499 void ndr_print_string_array(struct ndr_print *ndr, const char *name, const char **a)
504 for (count = 0; a && a[count]; count++) {}
506 ndr->print(ndr, "%s: ARRAY(%d)", name, count);
508 for (i=0;i<count;i++) {
510 asprintf(&idx, "[%d]", i);
512 ndr_print_string(ndr, idx, a[i]);
520 * Return number of elements in a string including the last (zeroed) element
522 uint32_t ndr_string_length(const void *_var, uint32_t element_size)
525 uint8_t zero[4] = {0,0,0,0};
526 const char *var = (const char *)_var;
528 for (i = 0; memcmp(var+i*element_size,zero,element_size) != 0; i++);
533 NTSTATUS ndr_check_string_terminator(struct ndr_pull *ndr, uint32_t count, uint32_t element_size)
536 struct ndr_pull_save save_offset;
538 ndr_pull_save(ndr, &save_offset);
539 ndr_pull_advance(ndr, (count - 1) * element_size);
540 NDR_PULL_NEED_BYTES(ndr, element_size);
542 for (i = 0; i < element_size; i++) {
543 if (ndr->data[ndr->offset+i] != 0) {
544 ndr_pull_restore(ndr, &save_offset);
546 return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, "String terminator not present or outside string boundaries");
550 ndr_pull_restore(ndr, &save_offset);
555 NTSTATUS ndr_pull_charset(struct ndr_pull *ndr, int ndr_flags, const char **var, uint32_t length, uint8_t byte_mul, charset_t chset)
559 *var = talloc_strdup(ndr->current_mem_ctx, "");
563 if (NDR_BE(ndr) && chset == CH_UTF16) {
567 NDR_PULL_NEED_BYTES(ndr, length*byte_mul);
569 ret = convert_string_talloc(ndr->current_mem_ctx,
571 ndr->data+ndr->offset,
575 return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
576 "Bad character conversion");
578 NDR_CHECK(ndr_pull_advance(ndr, length*byte_mul));
583 NTSTATUS ndr_push_charset(struct ndr_push *ndr, int ndr_flags, const char *var,
584 uint32_t length, uint8_t byte_mul, charset_t chset)
586 ssize_t ret, required;
588 if (NDR_BE(ndr) && chset == CH_UTF16) {
592 required = byte_mul * length;
594 NDR_PUSH_NEED_BYTES(ndr, required);
595 ret = convert_string(CH_UNIX, chset,
597 ndr->data+ndr->offset, required, False);
599 return ndr_push_error(ndr, NDR_ERR_CHARCNV,
600 "Bad character conversion");
603 /* Make sure the remaining part of the string is filled with zeroes */
604 if (ret < required) {
605 memset(ndr->data+ndr->offset+ret, 0, required-ret);
608 ndr->offset += required;
613 /* Return number of elements in a string in the specified charset */
614 uint32_t ndr_charset_length(const void *var, int chset)
616 /* FIXME: Treat special chars special here, taking chset into account */
617 /* Also include 0 byte */
618 return strlen((const char *)var)+1;