From fc51b38ed8b7ff239db82d4d2d52f6332910ca97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Deschner?= Date: Thu, 18 Feb 2021 16:10:01 +0100 Subject: s3-libnetapi: add netapi_save_file_ucs2() to example code Guenther Signed-off-by: Guenther Deschner Reviewed-by: Alexander Bokovoy --- source3/lib/netapi/examples/common.c | 58 ++++++++++++++++++++++++++++++++++++ source3/lib/netapi/examples/common.h | 1 + 2 files changed, 59 insertions(+) (limited to 'source3/lib/netapi') 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 #include +#include + #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); -- cgit v1.2.1