1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | routines for marshalling/unmarshalling string types
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 2003
|
---|
7 |
|
---|
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.
|
---|
12 |
|
---|
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.
|
---|
17 |
|
---|
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/>.
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "includes.h"
|
---|
23 | #include "librpc/ndr/libndr.h"
|
---|
24 |
|
---|
25 | /**
|
---|
26 | pull a general string from the wire
|
---|
27 | */
|
---|
28 | _PUBLIC_ enum ndr_err_code ndr_pull_string(struct ndr_pull *ndr, int ndr_flags, const char **s)
|
---|
29 | {
|
---|
30 | char *as=NULL;
|
---|
31 | uint32_t len1, ofs, len2;
|
---|
32 | uint16_t len3;
|
---|
33 | int ret;
|
---|
34 | int chset = CH_UTF16;
|
---|
35 | unsigned byte_mul = 2;
|
---|
36 | unsigned flags = ndr->flags;
|
---|
37 | unsigned c_len_term = 0;
|
---|
38 |
|
---|
39 | if (!(ndr_flags & NDR_SCALARS)) {
|
---|
40 | return NDR_ERR_SUCCESS;
|
---|
41 | }
|
---|
42 |
|
---|
43 | if (NDR_BE(ndr)) {
|
---|
44 | chset = CH_UTF16BE;
|
---|
45 | }
|
---|
46 |
|
---|
47 | if (flags & LIBNDR_FLAG_STR_ASCII) {
|
---|
48 | chset = CH_DOS;
|
---|
49 | byte_mul = 1;
|
---|
50 | flags &= ~LIBNDR_FLAG_STR_ASCII;
|
---|
51 | }
|
---|
52 |
|
---|
53 | if (flags & LIBNDR_FLAG_STR_UTF8) {
|
---|
54 | chset = CH_UTF8;
|
---|
55 | byte_mul = 1;
|
---|
56 | flags &= ~LIBNDR_FLAG_STR_UTF8;
|
---|
57 | }
|
---|
58 |
|
---|
59 | flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
|
---|
60 | if (flags & LIBNDR_FLAG_STR_CHARLEN) {
|
---|
61 | c_len_term = 1;
|
---|
62 | flags &= ~LIBNDR_FLAG_STR_CHARLEN;
|
---|
63 | }
|
---|
64 |
|
---|
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));
|
---|
70 | if (ofs != 0) {
|
---|
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);
|
---|
73 | }
|
---|
74 | NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len2));
|
---|
75 | if (len2 > len1) {
|
---|
76 | return ndr_pull_error(ndr, NDR_ERR_STRING,
|
---|
77 | "Bad string lengths len1=%u ofs=%u len2=%u\n",
|
---|
78 | len1, ofs, len2);
|
---|
79 | }
|
---|
80 | NDR_PULL_NEED_BYTES(ndr, (len2 + c_len_term)*byte_mul);
|
---|
81 | if (len2 == 0) {
|
---|
82 | as = talloc_strdup(ndr->current_mem_ctx, "");
|
---|
83 | } else {
|
---|
84 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
85 | chset, CH_UNIX,
|
---|
86 | ndr->data+ndr->offset,
|
---|
87 | (len2 + c_len_term)*byte_mul,
|
---|
88 | (void **)(void *)&as,
|
---|
89 | false);
|
---|
90 | if (ret == -1) {
|
---|
91 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
92 | "Bad character conversion");
|
---|
93 | }
|
---|
94 | }
|
---|
95 | NDR_CHECK(ndr_pull_advance(ndr, (len2 + c_len_term)*byte_mul));
|
---|
96 |
|
---|
97 | if (len1 != len2) {
|
---|
98 | DEBUG(6,("len1[%u] != len2[%u] '%s'\n", len1, len2, as));
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* this is a way of detecting if a string is sent with the wrong
|
---|
102 | termination */
|
---|
103 | if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
|
---|
104 | if (strlen(as) < (len2 + c_len_term)) {
|
---|
105 | DEBUG(6,("short string '%s'\n", as));
|
---|
106 | }
|
---|
107 | } else {
|
---|
108 | if (strlen(as) == (len2 + c_len_term)) {
|
---|
109 | DEBUG(6,("long string '%s'\n", as));
|
---|
110 | }
|
---|
111 | }
|
---|
112 | *s = as;
|
---|
113 | break;
|
---|
114 |
|
---|
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);
|
---|
119 | if (len1 == 0) {
|
---|
120 | as = talloc_strdup(ndr->current_mem_ctx, "");
|
---|
121 | } else {
|
---|
122 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
123 | chset, CH_UNIX,
|
---|
124 | ndr->data+ndr->offset,
|
---|
125 | (len1 + c_len_term)*byte_mul,
|
---|
126 | (void **)(void *)&as,
|
---|
127 | false);
|
---|
128 | if (ret == -1) {
|
---|
129 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
130 | "Bad character conversion");
|
---|
131 | }
|
---|
132 | }
|
---|
133 | NDR_CHECK(ndr_pull_advance(ndr, (len1 + c_len_term)*byte_mul));
|
---|
134 |
|
---|
135 | /* this is a way of detecting if a string is sent with the wrong
|
---|
136 | termination */
|
---|
137 | if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
|
---|
138 | if (strlen(as) < (len1 + c_len_term)) {
|
---|
139 | DEBUG(6,("short string '%s'\n", as));
|
---|
140 | }
|
---|
141 | } else {
|
---|
142 | if (strlen(as) == (len1 + c_len_term)) {
|
---|
143 | DEBUG(6,("long string '%s'\n", as));
|
---|
144 | }
|
---|
145 | }
|
---|
146 | *s = as;
|
---|
147 | break;
|
---|
148 |
|
---|
149 | case LIBNDR_FLAG_STR_LEN4:
|
---|
150 | case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_NOTERM:
|
---|
151 | NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &ofs));
|
---|
152 | if (ofs != 0) {
|
---|
153 | return ndr_pull_error(ndr, NDR_ERR_STRING, "non-zero array offset with string flags 0x%x\n",
|
---|
154 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
155 | }
|
---|
156 | NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
|
---|
157 | NDR_PULL_NEED_BYTES(ndr, (len1 + c_len_term)*byte_mul);
|
---|
158 | if (len1 == 0) {
|
---|
159 | as = talloc_strdup(ndr->current_mem_ctx, "");
|
---|
160 | } else {
|
---|
161 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
162 | chset, CH_UNIX,
|
---|
163 | ndr->data+ndr->offset,
|
---|
164 | (len1 + c_len_term)*byte_mul,
|
---|
165 | (void **)(void *)&as,
|
---|
166 | false);
|
---|
167 | if (ret == -1) {
|
---|
168 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
169 | "Bad character conversion");
|
---|
170 | }
|
---|
171 | }
|
---|
172 | NDR_CHECK(ndr_pull_advance(ndr, (len1 + c_len_term)*byte_mul));
|
---|
173 |
|
---|
174 | /* this is a way of detecting if a string is sent with the wrong
|
---|
175 | termination */
|
---|
176 | if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
|
---|
177 | if (strlen(as) < (len1 + c_len_term)) {
|
---|
178 | DEBUG(6,("short string '%s'\n", as));
|
---|
179 | }
|
---|
180 | } else {
|
---|
181 | if (strlen(as) == (len1 + c_len_term)) {
|
---|
182 | DEBUG(6,("long string '%s'\n", as));
|
---|
183 | }
|
---|
184 | }
|
---|
185 | *s = as;
|
---|
186 | break;
|
---|
187 |
|
---|
188 |
|
---|
189 | case LIBNDR_FLAG_STR_SIZE2:
|
---|
190 | case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM:
|
---|
191 | NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
|
---|
192 | NDR_PULL_NEED_BYTES(ndr, (len3 + c_len_term)*byte_mul);
|
---|
193 | if (len3 == 0) {
|
---|
194 | as = talloc_strdup(ndr->current_mem_ctx, "");
|
---|
195 | } else {
|
---|
196 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
197 | chset, CH_UNIX,
|
---|
198 | ndr->data+ndr->offset,
|
---|
199 | (len3 + c_len_term)*byte_mul,
|
---|
200 | (void **)(void *)&as,
|
---|
201 | false);
|
---|
202 | if (ret == -1) {
|
---|
203 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
204 | "Bad character conversion");
|
---|
205 | }
|
---|
206 | }
|
---|
207 | NDR_CHECK(ndr_pull_advance(ndr, (len3 + c_len_term)*byte_mul));
|
---|
208 |
|
---|
209 | /* this is a way of detecting if a string is sent with the wrong
|
---|
210 | termination */
|
---|
211 | if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
|
---|
212 | if (strlen(as) < (len3 + c_len_term)) {
|
---|
213 | DEBUG(6,("short string '%s'\n", as));
|
---|
214 | }
|
---|
215 | } else {
|
---|
216 | if (strlen(as) == (len3 + c_len_term)) {
|
---|
217 | DEBUG(6,("long string '%s'\n", as));
|
---|
218 | }
|
---|
219 | }
|
---|
220 | *s = as;
|
---|
221 | break;
|
---|
222 |
|
---|
223 | case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_STR_BYTESIZE:
|
---|
224 | NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
|
---|
225 | NDR_PULL_NEED_BYTES(ndr, len3);
|
---|
226 | if (len3 == 0) {
|
---|
227 | as = talloc_strdup(ndr->current_mem_ctx, "");
|
---|
228 | } else {
|
---|
229 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
230 | chset, CH_UNIX,
|
---|
231 | ndr->data+ndr->offset,
|
---|
232 | len3,
|
---|
233 | (void **)(void *)&as,
|
---|
234 | false);
|
---|
235 | if (ret == -1) {
|
---|
236 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
237 | "Bad character conversion");
|
---|
238 | }
|
---|
239 | }
|
---|
240 | NDR_CHECK(ndr_pull_advance(ndr, len3));
|
---|
241 | *s = as;
|
---|
242 | break;
|
---|
243 |
|
---|
244 | case LIBNDR_FLAG_STR_NULLTERM:
|
---|
245 | if (byte_mul == 1) {
|
---|
246 | len1 = ascii_len_n((const char *)(ndr->data+ndr->offset), ndr->data_size - ndr->offset);
|
---|
247 | } else {
|
---|
248 | len1 = utf16_len_n(ndr->data+ndr->offset, ndr->data_size - ndr->offset);
|
---|
249 | }
|
---|
250 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
251 | chset, CH_UNIX,
|
---|
252 | ndr->data+ndr->offset,
|
---|
253 | len1,
|
---|
254 | (void **)(void *)&as,
|
---|
255 | false);
|
---|
256 | if (ret == -1) {
|
---|
257 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
258 | "Bad character conversion");
|
---|
259 | }
|
---|
260 | NDR_CHECK(ndr_pull_advance(ndr, len1));
|
---|
261 | *s = as;
|
---|
262 | break;
|
---|
263 |
|
---|
264 | case LIBNDR_FLAG_STR_FIXLEN15:
|
---|
265 | case LIBNDR_FLAG_STR_FIXLEN32:
|
---|
266 | len1 = (flags & LIBNDR_FLAG_STR_FIXLEN32)?32:15;
|
---|
267 | NDR_PULL_NEED_BYTES(ndr, len1*byte_mul);
|
---|
268 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
269 | chset, CH_UNIX,
|
---|
270 | ndr->data+ndr->offset,
|
---|
271 | len1*byte_mul,
|
---|
272 | (void **)(void *)&as,
|
---|
273 | false);
|
---|
274 | if (ret == -1) {
|
---|
275 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
276 | "Bad character conversion");
|
---|
277 | }
|
---|
278 | NDR_CHECK(ndr_pull_advance(ndr, len1*byte_mul));
|
---|
279 | *s = as;
|
---|
280 | break;
|
---|
281 |
|
---|
282 | case LIBNDR_FLAG_STR_NOTERM:
|
---|
283 | if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
|
---|
284 | return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
|
---|
285 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
286 | }
|
---|
287 |
|
---|
288 | len1 = ndr->data_size - ndr->offset;
|
---|
289 |
|
---|
290 | NDR_PULL_NEED_BYTES(ndr, len1);
|
---|
291 | if (len1 == 0) {
|
---|
292 | as = talloc_strdup(ndr->current_mem_ctx, "");
|
---|
293 | } else {
|
---|
294 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
295 | chset, CH_UNIX,
|
---|
296 | ndr->data+ndr->offset,
|
---|
297 | len1,
|
---|
298 | (void **)(void *)&as,
|
---|
299 | false);
|
---|
300 | if (ret == -1) {
|
---|
301 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
302 | "Bad character conversion");
|
---|
303 | }
|
---|
304 | }
|
---|
305 | NDR_CHECK(ndr_pull_advance(ndr, len1));
|
---|
306 |
|
---|
307 | *s = as;
|
---|
308 | break;
|
---|
309 |
|
---|
310 | default:
|
---|
311 | return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
|
---|
312 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
313 | }
|
---|
314 |
|
---|
315 | return NDR_ERR_SUCCESS;
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | /**
|
---|
320 | push a general string onto the wire
|
---|
321 | */
|
---|
322 | _PUBLIC_ enum ndr_err_code ndr_push_string(struct ndr_push *ndr, int ndr_flags, const char *s)
|
---|
323 | {
|
---|
324 | ssize_t s_len, c_len, d_len;
|
---|
325 | int chset = CH_UTF16;
|
---|
326 | unsigned flags = ndr->flags;
|
---|
327 | unsigned byte_mul = 2;
|
---|
328 | uint8_t *dest = NULL;
|
---|
329 |
|
---|
330 | if (!(ndr_flags & NDR_SCALARS)) {
|
---|
331 | return NDR_ERR_SUCCESS;
|
---|
332 | }
|
---|
333 |
|
---|
334 | if (NDR_BE(ndr)) {
|
---|
335 | chset = CH_UTF16BE;
|
---|
336 | }
|
---|
337 |
|
---|
338 | s_len = s?strlen(s):0;
|
---|
339 |
|
---|
340 | if (flags & LIBNDR_FLAG_STR_ASCII) {
|
---|
341 | chset = CH_DOS;
|
---|
342 | byte_mul = 1;
|
---|
343 | flags &= ~LIBNDR_FLAG_STR_ASCII;
|
---|
344 | }
|
---|
345 |
|
---|
346 | if (flags & LIBNDR_FLAG_STR_UTF8) {
|
---|
347 | chset = CH_UTF8;
|
---|
348 | byte_mul = 1;
|
---|
349 | flags &= ~LIBNDR_FLAG_STR_UTF8;
|
---|
350 | }
|
---|
351 |
|
---|
352 | flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
|
---|
353 |
|
---|
354 | if (!(flags &
|
---|
355 | (LIBNDR_FLAG_STR_NOTERM |
|
---|
356 | LIBNDR_FLAG_STR_FIXLEN15 |
|
---|
357 | LIBNDR_FLAG_STR_FIXLEN32))) {
|
---|
358 | s_len++;
|
---|
359 | }
|
---|
360 | d_len = convert_string_talloc(ndr, CH_UNIX, chset, s, s_len,
|
---|
361 | (void **)(void *)&dest, false);
|
---|
362 | if (d_len == -1) {
|
---|
363 | return ndr_push_error(ndr, NDR_ERR_CHARCNV,
|
---|
364 | "Bad character conversion");
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
|
---|
368 | c_len = d_len;
|
---|
369 | flags &= ~LIBNDR_FLAG_STR_BYTESIZE;
|
---|
370 | } else if (flags & LIBNDR_FLAG_STR_CHARLEN) {
|
---|
371 | c_len = (d_len / byte_mul)-1;
|
---|
372 | flags &= ~LIBNDR_FLAG_STR_CHARLEN;
|
---|
373 | } else {
|
---|
374 | c_len = d_len / byte_mul;
|
---|
375 | }
|
---|
376 |
|
---|
377 | switch ((flags & LIBNDR_STRING_FLAGS) & ~LIBNDR_FLAG_STR_NOTERM) {
|
---|
378 | case LIBNDR_FLAG_STR_LEN4|LIBNDR_FLAG_STR_SIZE4:
|
---|
379 | NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
|
---|
380 | NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
|
---|
381 | NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
|
---|
382 | NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
|
---|
383 | break;
|
---|
384 |
|
---|
385 | case LIBNDR_FLAG_STR_LEN4:
|
---|
386 | NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, 0));
|
---|
387 | NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
|
---|
388 | NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
|
---|
389 | break;
|
---|
390 |
|
---|
391 | case LIBNDR_FLAG_STR_SIZE4:
|
---|
392 | NDR_CHECK(ndr_push_uint32(ndr, NDR_SCALARS, c_len));
|
---|
393 | NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
|
---|
394 | break;
|
---|
395 |
|
---|
396 | case LIBNDR_FLAG_STR_SIZE2:
|
---|
397 | NDR_CHECK(ndr_push_uint16(ndr, NDR_SCALARS, c_len));
|
---|
398 | NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
|
---|
399 | break;
|
---|
400 |
|
---|
401 | case LIBNDR_FLAG_STR_NULLTERM:
|
---|
402 | NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
|
---|
403 | break;
|
---|
404 |
|
---|
405 | case LIBNDR_FLAG_STR_FIXLEN15:
|
---|
406 | case LIBNDR_FLAG_STR_FIXLEN32: {
|
---|
407 | ssize_t fix_len = (flags & LIBNDR_FLAG_STR_FIXLEN32)?32:15;
|
---|
408 | uint32_t pad_len = fix_len - d_len;
|
---|
409 | if (d_len > fix_len) {
|
---|
410 | return ndr_push_error(ndr, NDR_ERR_CHARCNV,
|
---|
411 | "Bad character conversion");
|
---|
412 | }
|
---|
413 | NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
|
---|
414 | if (pad_len != 0) {
|
---|
415 | NDR_CHECK(ndr_push_zero(ndr, pad_len));
|
---|
416 | }
|
---|
417 | break;
|
---|
418 | }
|
---|
419 |
|
---|
420 | default:
|
---|
421 | if (ndr->flags & LIBNDR_FLAG_REMAINING) {
|
---|
422 | NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
|
---|
423 | break;
|
---|
424 | }
|
---|
425 |
|
---|
426 | return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
|
---|
427 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
428 | }
|
---|
429 |
|
---|
430 | talloc_free(dest);
|
---|
431 |
|
---|
432 | return NDR_ERR_SUCCESS;
|
---|
433 | }
|
---|
434 |
|
---|
435 | /**
|
---|
436 | push a general string onto the wire
|
---|
437 | */
|
---|
438 | _PUBLIC_ size_t ndr_string_array_size(struct ndr_push *ndr, const char *s)
|
---|
439 | {
|
---|
440 | size_t c_len;
|
---|
441 | unsigned flags = ndr->flags;
|
---|
442 | unsigned byte_mul = 2;
|
---|
443 | unsigned c_len_term = 1;
|
---|
444 |
|
---|
445 | if (flags & LIBNDR_FLAG_STR_FIXLEN32) {
|
---|
446 | return 32;
|
---|
447 | }
|
---|
448 | if (flags & LIBNDR_FLAG_STR_FIXLEN15) {
|
---|
449 | return 15;
|
---|
450 | }
|
---|
451 |
|
---|
452 | c_len = s?strlen_m(s):0;
|
---|
453 |
|
---|
454 | if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_UTF8)) {
|
---|
455 | byte_mul = 1;
|
---|
456 | }
|
---|
457 |
|
---|
458 | if (flags & LIBNDR_FLAG_STR_NOTERM) {
|
---|
459 | c_len_term = 0;
|
---|
460 | }
|
---|
461 |
|
---|
462 | c_len = c_len + c_len_term;
|
---|
463 |
|
---|
464 | if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
|
---|
465 | c_len = c_len * byte_mul;
|
---|
466 | }
|
---|
467 |
|
---|
468 | return c_len;
|
---|
469 | }
|
---|
470 |
|
---|
471 | _PUBLIC_ void ndr_print_string(struct ndr_print *ndr, const char *name, const char *s)
|
---|
472 | {
|
---|
473 | if (s) {
|
---|
474 | ndr->print(ndr, "%-25s: '%s'", name, s);
|
---|
475 | } else {
|
---|
476 | ndr->print(ndr, "%-25s: NULL", name);
|
---|
477 | }
|
---|
478 | }
|
---|
479 |
|
---|
480 | _PUBLIC_ uint32_t ndr_size_string(int ret, const char * const* string, int flags)
|
---|
481 | {
|
---|
482 | /* FIXME: Is this correct for all strings ? */
|
---|
483 | if(!(*string)) return ret;
|
---|
484 | return ret+strlen(*string)+1;
|
---|
485 | }
|
---|
486 |
|
---|
487 | /**
|
---|
488 | pull a general string array from the wire
|
---|
489 | */
|
---|
490 | _PUBLIC_ enum ndr_err_code ndr_pull_string_array(struct ndr_pull *ndr, int ndr_flags, const char ***_a)
|
---|
491 | {
|
---|
492 | const char **a = *_a;
|
---|
493 | uint32_t count;
|
---|
494 | unsigned flags = ndr->flags;
|
---|
495 | unsigned saved_flags = ndr->flags;
|
---|
496 |
|
---|
497 | if (!(ndr_flags & NDR_SCALARS)) {
|
---|
498 | return NDR_ERR_SUCCESS;
|
---|
499 | }
|
---|
500 |
|
---|
501 | switch (flags & LIBNDR_STRING_FLAGS) {
|
---|
502 | case LIBNDR_FLAG_STR_NULLTERM:
|
---|
503 | /*
|
---|
504 | * here the strings are null terminated
|
---|
505 | * but also the array is null terminated
|
---|
506 | */
|
---|
507 | for (count = 0;; count++) {
|
---|
508 | TALLOC_CTX *tmp_ctx;
|
---|
509 | const char *s = NULL;
|
---|
510 | a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
|
---|
511 | NDR_ERR_HAVE_NO_MEMORY(a);
|
---|
512 | a[count] = NULL;
|
---|
513 | a[count+1] = NULL;
|
---|
514 |
|
---|
515 | tmp_ctx = ndr->current_mem_ctx;
|
---|
516 | ndr->current_mem_ctx = a;
|
---|
517 | NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
|
---|
518 | ndr->current_mem_ctx = tmp_ctx;
|
---|
519 | if (strcmp("", s)==0) {
|
---|
520 | a[count] = NULL;
|
---|
521 | break;
|
---|
522 | } else {
|
---|
523 | a[count] = s;
|
---|
524 | }
|
---|
525 | }
|
---|
526 |
|
---|
527 | *_a =a;
|
---|
528 | break;
|
---|
529 |
|
---|
530 | case LIBNDR_FLAG_STR_NOTERM:
|
---|
531 | if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
|
---|
532 | return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
|
---|
533 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
534 | }
|
---|
535 | /*
|
---|
536 | * here the strings are not null terminated
|
---|
537 | * but serarated by a null terminator
|
---|
538 | *
|
---|
539 | * which means the same as:
|
---|
540 | * very string is null terminated exept the last
|
---|
541 | * string is terminated by the end of the buffer
|
---|
542 | *
|
---|
543 | * as LIBNDR_FLAG_STR_NULLTERM also end at the end
|
---|
544 | * of the buffer, we can pull each string with this flag
|
---|
545 | */
|
---|
546 | ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
|
---|
547 | ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
|
---|
548 |
|
---|
549 | for (count = 0; ((ndr->data_size - ndr->offset) > 0); count++) {
|
---|
550 | TALLOC_CTX *tmp_ctx;
|
---|
551 | const char *s = NULL;
|
---|
552 | a = talloc_realloc(ndr->current_mem_ctx, a, const char *, count + 2);
|
---|
553 | NDR_ERR_HAVE_NO_MEMORY(a);
|
---|
554 | a[count] = NULL;
|
---|
555 | a[count+1] = NULL;
|
---|
556 |
|
---|
557 | tmp_ctx = ndr->current_mem_ctx;
|
---|
558 | ndr->current_mem_ctx = a;
|
---|
559 | NDR_CHECK(ndr_pull_string(ndr, ndr_flags, &s));
|
---|
560 | ndr->current_mem_ctx = tmp_ctx;
|
---|
561 | a[count] = s;
|
---|
562 | }
|
---|
563 |
|
---|
564 | *_a =a;
|
---|
565 | break;
|
---|
566 |
|
---|
567 | default:
|
---|
568 | return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
|
---|
569 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
570 | }
|
---|
571 |
|
---|
572 | ndr->flags = saved_flags;
|
---|
573 | return NDR_ERR_SUCCESS;
|
---|
574 | }
|
---|
575 |
|
---|
576 | /**
|
---|
577 | push a general string array onto the wire
|
---|
578 | */
|
---|
579 | _PUBLIC_ enum ndr_err_code ndr_push_string_array(struct ndr_push *ndr, int ndr_flags, const char **a)
|
---|
580 | {
|
---|
581 | uint32_t count;
|
---|
582 | unsigned flags = ndr->flags;
|
---|
583 | unsigned saved_flags = ndr->flags;
|
---|
584 |
|
---|
585 | if (!(ndr_flags & NDR_SCALARS)) {
|
---|
586 | return NDR_ERR_SUCCESS;
|
---|
587 | }
|
---|
588 |
|
---|
589 | switch (flags & LIBNDR_STRING_FLAGS) {
|
---|
590 | case LIBNDR_FLAG_STR_NULLTERM:
|
---|
591 | for (count = 0; a && a[count]; count++) {
|
---|
592 | NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
|
---|
593 | }
|
---|
594 |
|
---|
595 | NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
|
---|
596 | break;
|
---|
597 |
|
---|
598 | case LIBNDR_FLAG_STR_NOTERM:
|
---|
599 | if (!(ndr->flags & LIBNDR_FLAG_REMAINING)) {
|
---|
600 | return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x (missing NDR_REMAINING)\n",
|
---|
601 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
602 | }
|
---|
603 |
|
---|
604 | for (count = 0; a && a[count]; count++) {
|
---|
605 | if (count > 0) {
|
---|
606 | ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
|
---|
607 | ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
|
---|
608 | NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
|
---|
609 | ndr->flags = saved_flags;
|
---|
610 | }
|
---|
611 | NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
|
---|
612 | }
|
---|
613 |
|
---|
614 | break;
|
---|
615 |
|
---|
616 | default:
|
---|
617 | return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
|
---|
618 | ndr->flags & LIBNDR_STRING_FLAGS);
|
---|
619 | }
|
---|
620 |
|
---|
621 | ndr->flags = saved_flags;
|
---|
622 | return NDR_ERR_SUCCESS;
|
---|
623 | }
|
---|
624 |
|
---|
625 | _PUBLIC_ void ndr_print_string_array(struct ndr_print *ndr, const char *name, const char **a)
|
---|
626 | {
|
---|
627 | uint32_t count;
|
---|
628 | uint32_t i;
|
---|
629 |
|
---|
630 | for (count = 0; a && a[count]; count++) {}
|
---|
631 |
|
---|
632 | ndr->print(ndr, "%s: ARRAY(%d)", name, count);
|
---|
633 | ndr->depth++;
|
---|
634 | for (i=0;i<count;i++) {
|
---|
635 | char *idx=NULL;
|
---|
636 | if (asprintf(&idx, "[%d]", i) != -1) {
|
---|
637 | ndr_print_string(ndr, idx, a[i]);
|
---|
638 | free(idx);
|
---|
639 | }
|
---|
640 | }
|
---|
641 | ndr->depth--;
|
---|
642 | }
|
---|
643 |
|
---|
644 | /**
|
---|
645 | * Return number of elements in a string including the last (zeroed) element
|
---|
646 | */
|
---|
647 | _PUBLIC_ uint32_t ndr_string_length(const void *_var, uint32_t element_size)
|
---|
648 | {
|
---|
649 | uint32_t i;
|
---|
650 | uint8_t zero[4] = {0,0,0,0};
|
---|
651 | const char *var = (const char *)_var;
|
---|
652 |
|
---|
653 | for (i = 0; memcmp(var+i*element_size,zero,element_size) != 0; i++);
|
---|
654 |
|
---|
655 | return i+1;
|
---|
656 | }
|
---|
657 |
|
---|
658 | _PUBLIC_ enum ndr_err_code ndr_check_string_terminator(struct ndr_pull *ndr, uint32_t count, uint32_t element_size)
|
---|
659 | {
|
---|
660 | uint32_t i;
|
---|
661 | struct ndr_pull_save save_offset;
|
---|
662 |
|
---|
663 | ndr_pull_save(ndr, &save_offset);
|
---|
664 | ndr_pull_advance(ndr, (count - 1) * element_size);
|
---|
665 | NDR_PULL_NEED_BYTES(ndr, element_size);
|
---|
666 |
|
---|
667 | for (i = 0; i < element_size; i++) {
|
---|
668 | if (ndr->data[ndr->offset+i] != 0) {
|
---|
669 | ndr_pull_restore(ndr, &save_offset);
|
---|
670 |
|
---|
671 | return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, "String terminator not present or outside string boundaries");
|
---|
672 | }
|
---|
673 | }
|
---|
674 |
|
---|
675 | ndr_pull_restore(ndr, &save_offset);
|
---|
676 |
|
---|
677 | return NDR_ERR_SUCCESS;
|
---|
678 | }
|
---|
679 |
|
---|
680 | _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)
|
---|
681 | {
|
---|
682 | int ret;
|
---|
683 | if (length == 0) {
|
---|
684 | *var = talloc_strdup(ndr->current_mem_ctx, "");
|
---|
685 | return NDR_ERR_SUCCESS;
|
---|
686 | }
|
---|
687 |
|
---|
688 | if (NDR_BE(ndr) && chset == CH_UTF16) {
|
---|
689 | chset = CH_UTF16BE;
|
---|
690 | }
|
---|
691 |
|
---|
692 | NDR_PULL_NEED_BYTES(ndr, length*byte_mul);
|
---|
693 |
|
---|
694 | ret = convert_string_talloc(ndr->current_mem_ctx,
|
---|
695 | chset, CH_UNIX,
|
---|
696 | ndr->data+ndr->offset,
|
---|
697 | length*byte_mul,
|
---|
698 | discard_const_p(void *, var), false);
|
---|
699 | if (ret == -1) {
|
---|
700 | return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
|
---|
701 | "Bad character conversion");
|
---|
702 | }
|
---|
703 | NDR_CHECK(ndr_pull_advance(ndr, length*byte_mul));
|
---|
704 |
|
---|
705 | return NDR_ERR_SUCCESS;
|
---|
706 | }
|
---|
707 |
|
---|
708 | _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)
|
---|
709 | {
|
---|
710 | ssize_t ret, required;
|
---|
711 |
|
---|
712 | if (NDR_BE(ndr) && chset == CH_UTF16) {
|
---|
713 | chset = CH_UTF16BE;
|
---|
714 | }
|
---|
715 |
|
---|
716 | required = byte_mul * length;
|
---|
717 |
|
---|
718 | NDR_PUSH_NEED_BYTES(ndr, required);
|
---|
719 | ret = convert_string(CH_UNIX, chset,
|
---|
720 | var, strlen(var),
|
---|
721 | ndr->data+ndr->offset, required, false);
|
---|
722 | if (ret == -1) {
|
---|
723 | return ndr_push_error(ndr, NDR_ERR_CHARCNV,
|
---|
724 | "Bad character conversion");
|
---|
725 | }
|
---|
726 |
|
---|
727 | /* Make sure the remaining part of the string is filled with zeroes */
|
---|
728 | if (ret < required) {
|
---|
729 | memset(ndr->data+ndr->offset+ret, 0, required-ret);
|
---|
730 | }
|
---|
731 |
|
---|
732 | ndr->offset += required;
|
---|
733 |
|
---|
734 | return NDR_ERR_SUCCESS;
|
---|
735 | }
|
---|
736 |
|
---|
737 | /* Return number of elements in a string in the specified charset */
|
---|
738 | _PUBLIC_ uint32_t ndr_charset_length(const void *var, charset_t chset)
|
---|
739 | {
|
---|
740 | /* FIXME: Treat special chars special here, taking chset into account */
|
---|
741 | /* Also include 0 byte */
|
---|
742 | return strlen((const char *)var)+1;
|
---|
743 | }
|
---|