ctdb/docs: Include ceph rados namespace support in man page
[samba.git] / source3 / lib / fstring.c
index cd5317c23037cc26a08e6f25bbfcb89f9899b9a4..3ed1db1f24c74cfde53b56673d66ba2501cf02e9 100644 (file)
@@ -31,41 +31,26 @@ size_t push_ascii_fstring(void *dest, const char *src)
 }
 
 /********************************************************************
- Push an nstring - ensure null terminated. Written by
- moriyama@miraclelinux.com (MORIYAMA Masayuki).
+ Push an nstring (a netbios string)
+ this function uses convert_string_error() to avoid common debug
+ warnings where is unable to convert strings to CH_DOS. The target
+ string is truncated at the first character that cannot be converted
+ The target is always null terminated.
 ********************************************************************/
 
 size_t push_ascii_nstring(void *dest, const char *src)
 {
-       size_t i, buffer_len, dest_len;
-       smb_ucs2_t *buffer;
-
-       conv_silent = True;
-       if (!push_ucs2_talloc(talloc_tos(), &buffer, src, &buffer_len)) {
-               smb_panic("failed to create UCS2 buffer");
-       }
-
-       /* We're using buffer_len below to count ucs2 characters, not bytes. */
-       buffer_len /= sizeof(smb_ucs2_t);
-
-       dest_len = 0;
-       for (i = 0; buffer[i] != 0 && (i < buffer_len); i++) {
-               unsigned char mb[10];
-               /* Convert one smb_ucs2_t character at a time. */
-               size_t mb_len = convert_string(CH_UTF16LE, CH_DOS, buffer+i, sizeof(smb_ucs2_t), mb, sizeof(mb));
-               if ((mb_len != (size_t)-1) && (dest_len + mb_len <= MAX_NETBIOSNAME_LEN - 1)) {
-                       memcpy((char *)dest + dest_len, mb, mb_len);
-                       dest_len += mb_len;
-               } else {
-                       errno = E2BIG;
-                       break;
-               }
+       size_t converted_size = 0;
+       bool ret;
+
+       errno = 0;
+       ret = convert_string_error(CH_UNIX, CH_DOS, src, -1, dest, sizeof(nstring), &converted_size);
+       if (ret || errno == E2BIG) {
+               SCVAL(dest, sizeof(nstring)-1, 0);
+       } else {
+               SCVAL(dest, 0, 0);
        }
-       ((char *)dest)[dest_len] = '\0';
-
-       conv_silent = False;
-       TALLOC_FREE(buffer);
-       return dest_len;
+       return ret ? converted_size : (size_t)-1;
 }
 
 size_t pull_ascii_fstring(char *dest, const void *src)
@@ -77,55 +62,6 @@ size_t pull_ascii_fstring(char *dest, const void *src)
 
 size_t pull_ascii_nstring(char *dest, size_t dest_len, const void *src)
 {
-       return pull_ascii(dest, src, dest_len, sizeof(nstring)-1, STR_TERMINATE);
+       return pull_ascii(dest, src, dest_len, sizeof(nstring), STR_TERMINATE);
 }
 
-/**
- Copy a string from a char* src to a UTF-8 destination.
- Return the number of bytes occupied by the string in the destination
- Flags can have:
-  STR_TERMINATE means include the null termination
-  STR_UPPER     means uppercase in the destination
- dest_len is the maximum length allowed in the destination. If dest_len
- is -1 then no maxiumum is used.
-**/
-
-static size_t push_utf8(void *dest, const char *src, size_t dest_len, int flags)
-{
-       size_t src_len = 0;
-       size_t ret;
-       char *tmpbuf = NULL;
-
-       if (dest_len == (size_t)-1) {
-               /* No longer allow dest_len of -1. */
-               smb_panic("push_utf8 - invalid dest_len of -1");
-       }
-
-       if (flags & STR_UPPER) {
-               tmpbuf = strupper_talloc(talloc_tos(), src);
-               if (!tmpbuf) {
-                       return (size_t)-1;
-               }
-               src = tmpbuf;
-               src_len = strlen(src);
-       }
-
-       src_len = strlen(src);
-       if (flags & STR_TERMINATE) {
-               src_len++;
-       }
-
-       ret = convert_string(CH_UNIX, CH_UTF8, src, src_len, dest, dest_len);
-       TALLOC_FREE(tmpbuf);
-       return ret;
-}
-
-size_t push_utf8_fstring(void *dest, const char *src)
-{
-       return push_utf8(dest, src, sizeof(fstring), STR_TERMINATE);
-}
-
-size_t pull_ucs2_fstring(char *dest, const void *src)
-{
-       return pull_ucs2(NULL, dest, src, sizeof(fstring), -1, STR_TERMINATE);
-}