diff options
| author | jakob <jakob@memeware.net> | 2017-05-10 20:42:36 -0400 |
|---|---|---|
| committer | jakob <jakob@memeware.net> | 2017-05-10 20:42:36 -0400 |
| commit | 1885dd37171b1cf95210e7a4850c2179f21a9e08 (patch) | |
| tree | a7be17e425fea7ec3ac7b1bb8044eb94d873dc4d /src/encoding.c | |
| parent | b38a284ffc56e292b79610bbd94ebaf086fb4421 (diff) | |
eliF entry creation for the archive table
Diffstat (limited to 'src/encoding.c')
| -rw-r--r-- | src/encoding.c | 21 |
1 files changed, 18 insertions, 3 deletions
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 <http://www.gnu.org/licenses/>. */ #include <iconv.h> +#include <string.h> /* 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); } |