summaryrefslogtreecommitdiff
path: root/source3/lib/netapi
diff options
context:
space:
mode:
authorGünther Deschner <gd@samba.org>2021-02-18 16:10:01 +0100
committerGünther Deschner <gd@samba.org>2021-07-14 16:49:30 +0000
commitfc51b38ed8b7ff239db82d4d2d52f6332910ca97 (patch)
treee9d8aea64977327a211a5441e73c92eb47e8f446 /source3/lib/netapi
parent3cfe663651f353f69833ffc46687d9602c3cc900 (diff)
downloadsamba-fc51b38ed8b7ff239db82d4d2d52f6332910ca97.tar.gz
s3-libnetapi: add netapi_save_file_ucs2() to example code
Guenther Signed-off-by: Guenther Deschner <gd@samba.org> Reviewed-by: Alexander Bokovoy <ab@samba.org>
Diffstat (limited to 'source3/lib/netapi')
-rw-r--r--source3/lib/netapi/examples/common.c58
-rw-r--r--source3/lib/netapi/examples/common.h1
2 files changed, 59 insertions, 0 deletions
diff --git a/source3/lib/netapi/examples/common.c b/source3/lib/netapi/examples/common.c
index c7f59684363..a1a491e60c2 100644
--- a/source3/lib/netapi/examples/common.c
+++ b/source3/lib/netapi/examples/common.c
@@ -10,6 +10,8 @@
#include <fcntl.h>
#include <unistd.h>
+#include <iconv.h>
+
#ifndef MIN
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
@@ -182,3 +184,59 @@ int netapi_save_file(const char *fname, void *ppacket, size_t length)
close(fd);
return 0;
}
+
+int netapi_save_file_ucs2(const char *fname, const char *str)
+{
+ char *str_p = NULL;
+ char *ucs2_str = NULL;
+ size_t str_len = 0;
+ size_t ucs2_str_len = 0;
+ iconv_t cd;
+ int ret;
+ char *start;
+ size_t start_len;
+ char *p;
+
+ str_len = strlen(str) + 1;
+ ucs2_str_len = 2 * str_len; /* room for ucs2 */
+ ucs2_str_len += 2;
+
+ ucs2_str = calloc(ucs2_str_len, sizeof(char));
+ if (ucs2_str == NULL) {
+ return -1;
+ }
+ p = ucs2_str; /* store for free */
+
+ ucs2_str[0] = 0xff;
+ ucs2_str[1] = 0xfe;
+
+ start = ucs2_str;
+ start_len = ucs2_str_len;
+
+ ucs2_str += 2;
+ ucs2_str_len -= 2;
+
+ cd = iconv_open("UTF-16LE", "ASCII");
+ if (cd == (iconv_t)-1) {
+ free(p);
+ return -1;
+ }
+
+ str_p = (void *)((uintptr_t)str);
+
+ ret = iconv(cd,
+ &str_p,
+ &str_len,
+ &ucs2_str,
+ &ucs2_str_len);
+ if (ret == -1) {
+ free(p);
+ return -1;
+ }
+ iconv_close(cd);
+
+ ret = netapi_save_file(fname, start, start_len);
+ free(p);
+
+ return ret;
+}
diff --git a/source3/lib/netapi/examples/common.h b/source3/lib/netapi/examples/common.h
index e852e817f7c..df7f1768bf8 100644
--- a/source3/lib/netapi/examples/common.h
+++ b/source3/lib/netapi/examples/common.h
@@ -15,3 +15,4 @@ extern struct poptOption popt_common_netapi_examples[];
char *netapi_read_file(const char *filename, uint32_t *psize);
int netapi_save_file(const char *fname, void *ppacket, size_t length);
+int netapi_save_file_ucs2(const char *fname, const char *str);