|
@@ -16,28 +16,61 @@
|
|
|
// published by Sam Hocevar. See the COPYING file for more details.
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
+#include <assert.h>
|
|
|
+#include <iconv.h>
|
|
|
+#include <langinfo.h>
|
|
|
+#include <stdio.h>
|
|
|
#include <stdlib.h>
|
|
|
#include <string.h>
|
|
|
-#include <assert.h>
|
|
|
|
|
|
#include "bdsm/smb_utils.h"
|
|
|
|
|
|
-size_t smb_to_utf16(const char *src_enc, const char *src,
|
|
|
- size_t src_len, char **dst)
|
|
|
+static const char *current_encoding()
|
|
|
+{
|
|
|
+ return (nl_langinfo(CODESET));
|
|
|
+}
|
|
|
+
|
|
|
+static size_t smb_iconv(const char *src, size_t src_len, char **dst,
|
|
|
+ size_t dst_len, const char *src_enc,
|
|
|
+ const char *dst_enc)
|
|
|
{
|
|
|
- size_t res = src_len * 2;
|
|
|
+ iconv_t ic;
|
|
|
char *out;
|
|
|
+ size_t outleft;
|
|
|
|
|
|
- out = malloc(res);
|
|
|
- assert(out != NULL);
|
|
|
- *dst = (char *)out;
|
|
|
+ assert(src != NULL && dst != NULL && src_enc != NULL && dst_enc != NULL);
|
|
|
+
|
|
|
+ if (!src_len)
|
|
|
+ {
|
|
|
+ *dst = NULL;
|
|
|
+ return (0);
|
|
|
+ }
|
|
|
|
|
|
- for (size_t i = 0; i < src_len; i++)
|
|
|
+ if ((ic = iconv_open(dst_enc, src_enc)) < 0)
|
|
|
{
|
|
|
- out[2 * i] = src[i];
|
|
|
- out[2 * i + 1] = 0;
|
|
|
+ fprintf(stderr, "Unable to open iconv to convert from %s to %s\n",
|
|
|
+ src_enc, dst_enc);
|
|
|
+ return (0);
|
|
|
}
|
|
|
|
|
|
- return (res);
|
|
|
+ outleft = dst_len; // The utf-16 str is at most 2x bigger than the utf-8 one. (i think ?)
|
|
|
+ out = *dst = malloc(outleft);
|
|
|
|
|
|
+ assert(out != NULL);
|
|
|
+ iconv(ic, (char **)&src, &src_len, &out, &outleft);
|
|
|
+ assert(src_len == 0);
|
|
|
+
|
|
|
+ return (dst_len - outleft);
|
|
|
+}
|
|
|
+
|
|
|
+size_t smb_to_utf16(const char *src, size_t src_len, char **dst)
|
|
|
+{
|
|
|
+ return(smb_iconv(src, src_len, dst, src_len * 2,
|
|
|
+ current_encoding(), "UCS-2LE"));
|
|
|
+}
|
|
|
+
|
|
|
+size_t smb_from_utf16(const char *src, size_t src_len, char **dst)
|
|
|
+{
|
|
|
+ return(smb_iconv(src, src_len, dst, src_len,
|
|
|
+ "UCS-2LE", current_encoding()));
|
|
|
}
|