From 1885dd37171b1cf95210e7a4850c2179f21a9e08 Mon Sep 17 00:00:00 2001 From: jakob Date: Wed, 10 May 2017 20:42:36 -0400 Subject: eliF entry creation for the archive table --- src/encoding.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'src/encoding.c') diff --git a/src/encoding.c b/src/encoding.c index e7af310..8b1b7dc 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -18,11 +18,13 @@ along with Nekopack. If not, see . */ #include +#include /* Wrapper for iconv, using the conversion specified by `conv`. */ -static void convert(char *in_buf, char *out_buf, size_t len, iconv_t conv) { - size_t in_size = len, out_size = len; +static void convert(char *in_buf, char *out_buf, size_t in_len, + size_t out_len, iconv_t conv) { + size_t in_size = in_len, out_size = out_len; char *in_start = in_buf, *out_start = out_buf; iconv(conv, &in_start, &in_size, &out_start, &out_size); } @@ -31,6 +33,19 @@ static void convert(char *in_buf, char *out_buf, size_t len, iconv_t conv) { /* Decodes the UTF-16LE string specified by `in_buf` into `out_buf`. */ void utf16le_decode(char *in_buf, char *out_buf, size_t len) { iconv_t conv = iconv_open("UTF-8", "UTF-16LE"); - convert(in_buf, out_buf, len, conv); + convert(in_buf, out_buf, len, len, conv); + iconv_close(conv); +} + + +/* Encodes the UTF-8 string specified by `in_buf` into a UTF-16LE string + stored in `out_buf`. */ +void utf16le_encode(char *in_buf, char *out_buf, size_t len) { + iconv_t conv = iconv_open("UTF-16LE", "UTF-8"); + /* This is to ensure that the null-terminator is included in the + encoded string. */ + if (strlen(in_buf) == len) + len++; + convert(in_buf, out_buf, len, len * 2 + 2, conv); iconv_close(conv); } -- cgit v1.3