d0d8303240918b3de1e2740eed6a89d6147cc524
[samba.git] / librpc / ndr / ndr_string.c
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         size_t conv_src_len = 0, converted_size;
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                 } else if (len1 != len2) {
80                         DEBUG(6,("len1[%u] != len2[%u] '%s'\n", len1, len2, as));
81                 }
82                 conv_src_len = len2 + c_len_term;
83                 break;
84
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;
89                 break;
90
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));
94                 if (ofs != 0) {
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);
97                 }
98                 NDR_CHECK(ndr_pull_uint32(ndr, NDR_SCALARS, &len1));
99                 conv_src_len = len1 + c_len_term;
100                 break;
101
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;
106                 break;
107
108         case LIBNDR_FLAG_STR_SIZE2|LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_STR_BYTESIZE:
109                 NDR_CHECK(ndr_pull_uint16(ndr, NDR_SCALARS, &len3));
110                 conv_src_len = len3;
111                 byte_mul = 1; /* the length is now absolute */
112                 break;
113
114         case LIBNDR_FLAG_STR_NULLTERM:
115                 if (byte_mul == 1) {
116                         conv_src_len = ascii_len_n((const char *)(ndr->data+ndr->offset), ndr->data_size - ndr->offset);
117                 } else {
118                         conv_src_len = utf16_len_n(ndr->data+ndr->offset, ndr->data_size - ndr->offset);
119                 }
120                 byte_mul = 1; /* the length is now absolute */
121                 break;
122
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);
127                 }
128                 conv_src_len = ndr->data_size - ndr->offset;
129                 byte_mul = 1; /* the length is now absolute */
130                 break;
131
132         default:
133                 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
134                                       ndr->flags & LIBNDR_STRING_FLAGS);
135         }
136
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, "");
140         } else {
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,
145                                            &converted_size)) {
146                         return ndr_pull_error(ndr, NDR_ERR_CHARCNV,
147                                               "Bad character conversion with flags 0x%x", flags);
148                 }
149         }
150
151         /* this is a way of detecting if a string is sent with the wrong
152            termination */
153         if (ndr->flags & LIBNDR_FLAG_STR_NOTERM) {
154                 if (strlen(as) < conv_src_len) {
155                         DEBUG(6,("short string '%s'\n", as));
156                 }
157         } else {
158                 if (strlen(as) == conv_src_len) {
159                         DEBUG(6,("long string '%s'\n", as));
160                 }
161         }
162
163         NDR_CHECK(ndr_pull_advance(ndr, conv_src_len * byte_mul));
164         *s = as;
165
166         return NDR_ERR_SUCCESS;
167 }
168
169
170 /**
171   push a general string onto the wire
172 */
173 _PUBLIC_ enum ndr_err_code ndr_push_string(struct ndr_push *ndr, int ndr_flags, const char *s)
174 {
175         ssize_t s_len, c_len;
176         size_t d_len;
177         int chset = CH_UTF16;
178         unsigned flags = ndr->flags;
179         unsigned byte_mul = 2;
180         uint8_t *dest = NULL;
181
182         if (!(ndr_flags & NDR_SCALARS)) {
183                 return NDR_ERR_SUCCESS;
184         }
185
186         if (NDR_BE(ndr)) {
187                 chset = CH_UTF16BE;
188         }
189         
190         s_len = s?strlen(s):0;
191
192         if (flags & LIBNDR_FLAG_STR_ASCII) {
193                 chset = CH_DOS;
194                 byte_mul = 1;
195                 flags &= ~LIBNDR_FLAG_STR_ASCII;
196         }
197
198         if (flags & LIBNDR_FLAG_STR_UTF8) {
199                 chset = CH_UTF8;
200                 byte_mul = 1;
201                 flags &= ~LIBNDR_FLAG_STR_UTF8;
202         }
203
204         flags &= ~LIBNDR_FLAG_STR_CONFORMANT;
205
206         if (!(flags & LIBNDR_FLAG_STR_NOTERM)) {
207                 s_len++;
208         }
209         if (!convert_string_talloc(ndr, CH_UNIX, chset, s, s_len,
210                                    (void **)(void *)&dest, &d_len))
211         {
212                 return ndr_push_error(ndr, NDR_ERR_CHARCNV, 
213                                       "Bad character push conversion with flags 0x%x", flags);
214         }
215
216         if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
217                 c_len = d_len;
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;
222         } else {
223                 c_len = d_len / byte_mul;
224         }
225
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));
232                 break;
233
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));
238                 break;
239
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));
243                 break;
244
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));
248                 break;
249
250         case LIBNDR_FLAG_STR_NULLTERM:
251                 NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
252                 break;
253
254         default:
255                 if (ndr->flags & LIBNDR_FLAG_REMAINING) {
256                         NDR_CHECK(ndr_push_bytes(ndr, dest, d_len));
257                         break;          
258                 }
259
260                 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
261                                       ndr->flags & LIBNDR_STRING_FLAGS);
262         }
263
264         talloc_free(dest);
265
266         return NDR_ERR_SUCCESS;
267 }
268
269 /**
270   push a general string onto the wire
271 */
272 _PUBLIC_ size_t ndr_string_array_size(struct ndr_push *ndr, const char *s)
273 {
274         size_t c_len;
275         unsigned flags = ndr->flags;
276         unsigned byte_mul = 2;
277         unsigned c_len_term = 1;
278
279         c_len = s?strlen_m(s):0;
280
281         if (flags & (LIBNDR_FLAG_STR_ASCII|LIBNDR_FLAG_STR_UTF8)) {
282                 byte_mul = 1;
283         }
284
285         if (flags & LIBNDR_FLAG_STR_NOTERM) {
286                 c_len_term = 0;
287         }
288
289         c_len = c_len + c_len_term;
290
291         if (flags & LIBNDR_FLAG_STR_BYTESIZE) {
292                 c_len = c_len * byte_mul;
293         }
294
295         return c_len;
296 }
297
298 _PUBLIC_ void ndr_print_string(struct ndr_print *ndr, const char *name, const char *s)
299 {
300         if (s) {
301                 ndr->print(ndr, "%-25s: '%s'", name, s);
302         } else {
303                 ndr->print(ndr, "%-25s: NULL", name);
304         }
305 }
306
307 _PUBLIC_ uint32_t ndr_size_string(int ret, const char * const* string, int flags) 
308 {
309         /* FIXME: Is this correct for all strings ? */
310         if(!(*string)) return ret;
311         return ret+strlen(*string)+1;
312 }
313
314 /**
315   pull a general string array from the wire
316 */
317 _PUBLIC_ enum ndr_err_code ndr_pull_string_array(struct ndr_pull *ndr, int ndr_flags, const char ***_a)
318 {
319         const char **a = NULL;
320         uint32_t count;
321         unsigned flags = ndr->flags;
322         unsigned saved_flags = ndr->flags;
323
324         if (!(ndr_flags & NDR_SCALARS)) {
325                 return NDR_ERR_SUCCESS;
326         }
327
328         switch (flags & (LIBNDR_FLAG_STR_NULLTERM|LIBNDR_FLAG_STR_NOTERM)) {
329         case LIBNDR_FLAG_STR_NULLTERM:
330                 /* 
331                  * here the strings are null terminated
332                  * but also the array is null terminated if LIBNDR_FLAG_REMAINING
333                  * is specified
334                  */
335                 for (count = 0;; count++) {
336                         TALLOC_CTX *tmp_ctx;
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);
340                         a[count]   = NULL;
341                         a[count+1]   = NULL;
342
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)
347                         {
348                                 a[count] = s;
349                                 break;
350                         }
351                         ndr->current_mem_ctx = tmp_ctx;
352                         if (strcmp("", s)==0) {
353                                 a[count] = NULL;
354                                 break;
355                         } else {
356                                 a[count] = s;
357                         }
358                 }
359
360                 *_a =a;
361                 break;
362
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);
367                 }
368                 /*
369                  * here the strings are not null terminated
370                  * but serarated by a null terminator
371                  *
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
375                  *
376                  * as LIBNDR_FLAG_STR_NULLTERM also end at the end
377                  * of the buffer, we can pull each string with this flag
378                  *
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
381                  */
382                 ndr->flags &= ~(LIBNDR_FLAG_STR_NOTERM|LIBNDR_FLAG_REMAINING);
383                 ndr->flags |= LIBNDR_FLAG_STR_NULLTERM;
384
385                 for (count = 0; ((ndr->data_size - ndr->offset) > 0); count++) {
386                         TALLOC_CTX *tmp_ctx;
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);
390                         a[count]   = NULL;
391                         a[count+1]   = NULL;
392
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;
397                         a[count] = s;
398                 }
399
400                 *_a =a;
401                 break;
402
403         default:
404                 return ndr_pull_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
405                                       ndr->flags & LIBNDR_STRING_FLAGS);
406         }
407
408         ndr->flags = saved_flags;
409         return NDR_ERR_SUCCESS;
410 }
411
412 /**
413   push a general string array onto the wire
414 */
415 _PUBLIC_ enum ndr_err_code ndr_push_string_array(struct ndr_push *ndr, int ndr_flags, const char **a)
416 {
417         uint32_t count;
418         unsigned flags = ndr->flags;
419         unsigned saved_flags = ndr->flags;
420
421         if (!(ndr_flags & NDR_SCALARS)) {
422                 return NDR_ERR_SUCCESS;
423         }
424
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]));
429                 }
430                 /* If LIBNDR_FLAG_REMAINING then we do not add a null terminator to the array */
431                 if (!(flags & LIBNDR_FLAG_REMAINING))
432                 {
433                         NDR_CHECK(ndr_push_string(ndr, ndr_flags, ""));
434                 }
435                 break;
436
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);
441                 }
442
443                 for (count = 0; a && a[count]; count++) {
444                         if (count > 0) {
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;
449                         }
450                         NDR_CHECK(ndr_push_string(ndr, ndr_flags, a[count]));
451                 }
452
453                 break;
454
455         default:
456                 return ndr_push_error(ndr, NDR_ERR_STRING, "Bad string flags 0x%x\n",
457                                       ndr->flags & LIBNDR_STRING_FLAGS);
458         }
459         
460         ndr->flags = saved_flags;
461         return NDR_ERR_SUCCESS;
462 }
463
464 _PUBLIC_ void ndr_print_string_array(struct ndr_print *ndr, const char *name, const char **a)
465 {
466         uint32_t count;
467         uint32_t i;
468
469         for (count = 0; a && a[count]; count++) {}
470
471         ndr->print(ndr, "%s: ARRAY(%d)", name, count);
472         ndr->depth++;
473         for (i=0;i<count;i++) {
474                 char *idx=NULL;
475                 if (asprintf(&idx, "[%d]", i) != -1) {
476                         ndr_print_string(ndr, idx, a[i]);
477                         free(idx);
478                 }
479         }
480         ndr->depth--;
481 }
482
483 _PUBLIC_ size_t ndr_size_string_array(const char **a, uint32_t count, int flags)
484 {
485         uint32_t i;
486         size_t size = 0;
487
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]);
492                 }
493                 break;
494         case LIBNDR_FLAG_STR_NOTERM:
495                 for (i = 0; i < count; i++) {
496                         size += strlen_m(a[i]);
497                 }
498                 break;
499         default:
500                 return 0;
501         }
502
503         return size;
504 }
505
506 /**
507  * Return number of elements in a string including the last (zeroed) element 
508  */
509 _PUBLIC_ uint32_t ndr_string_length(const void *_var, uint32_t element_size)
510 {
511         uint32_t i;
512         uint8_t zero[4] = {0,0,0,0};
513         const char *var = (const char *)_var;
514
515         for (i = 0; memcmp(var+i*element_size,zero,element_size) != 0; i++);
516
517         return i+1;
518 }
519
520 _PUBLIC_ enum ndr_err_code ndr_check_string_terminator(struct ndr_pull *ndr, uint32_t count, uint32_t element_size)
521 {
522         uint32_t i;
523         uint32_t save_offset;
524
525         save_offset = ndr->offset;
526         ndr_pull_advance(ndr, (count - 1) * element_size);
527         NDR_PULL_NEED_BYTES(ndr, element_size);
528
529         for (i = 0; i < element_size; i++) {
530                  if (ndr->data[ndr->offset+i] != 0) {
531                         ndr->offset = save_offset;
532
533                         return ndr_pull_error(ndr, NDR_ERR_ARRAY_SIZE, "String terminator not present or outside string boundaries");
534                  }
535         }
536
537         ndr->offset = save_offset;
538
539         return NDR_ERR_SUCCESS;
540 }
541
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)
543 {
544         size_t converted_size;
545
546         if (length == 0) {
547                 *var = talloc_strdup(ndr->current_mem_ctx, "");
548                 return NDR_ERR_SUCCESS;
549         }
550
551         if (NDR_BE(ndr) && chset == CH_UTF16) {
552                 chset = CH_UTF16BE;
553         }
554
555         NDR_PULL_NEED_BYTES(ndr, length*byte_mul);
556
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),
560                                    &converted_size))
561         {
562                 return ndr_pull_error(ndr, NDR_ERR_CHARCNV, 
563                                       "Bad character conversion");
564         }
565         NDR_CHECK(ndr_pull_advance(ndr, length*byte_mul));
566
567         return NDR_ERR_SUCCESS;
568 }
569
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)
571 {
572         ssize_t required;
573
574         if (NDR_BE(ndr) && chset == CH_UTF16) {
575                 chset = CH_UTF16BE;
576         }
577
578         required = byte_mul * length;
579         
580         NDR_PUSH_NEED_BYTES(ndr, required);
581
582         if (required) {
583                 size_t size = 0;
584                 if (!convert_string(CH_UNIX, chset,
585                              var, strlen(var),
586                              ndr->data+ndr->offset, required, &size)) {
587                         return ndr_push_error(ndr, NDR_ERR_CHARCNV, 
588                                       "Bad character conversion");
589                 }
590
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);
594                 }
595         }
596
597         ndr->offset += required;
598
599         return NDR_ERR_SUCCESS;
600 }
601
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)
604 {
605         switch (chset) {
606         /* case CH_UTF16: this has the same value as CH_UTF16LE */
607         case CH_UTF16LE:
608         case CH_UTF16BE:
609         case CH_UTF16MUNGED:
610         case CH_UTF8:
611                 return strlen_m_ext_term((const char *)var, CH_UNIX, chset);
612         case CH_DISPLAY:
613         case CH_DOS:
614         case CH_UNIX:
615                 return strlen((const char *)var)+1;
616         }
617
618         /* Fallback, this should never happen */
619         return strlen((const char *)var)+1;
620 }