X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Fencoding.c;h=e588eabff6e60cd015999c6446ccf11e2344bf35;hb=07e37b8d03fb6cd8bd5905905dd52807774204cd;hp=752ab847bed3cfeb6d8f7a7ae8341ebc9c47e7d8;hpb=c94e4a479c2b77cc9046e7820c539f749fc15d6e;p=wimlib diff --git a/src/encoding.c b/src/encoding.c index 752ab847..e588eabf 100644 --- a/src/encoding.c +++ b/src/encoding.c @@ -39,6 +39,10 @@ #include #include +#ifdef HAVE_ALLOCA_H +# include +#endif + bool wimlib_mbs_is_utf8 = !TCHAR_IS_UTF16LE; /* List of iconv_t conversion descriptors for a specific character conversion. @@ -139,12 +143,25 @@ varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\ if (cd == NULL) \ return WIMLIB_ERR_ICONV_NOT_AVAILABLE; \ \ + chartype2 *buf; \ + size_t bufsize; \ + bool buf_onheap; \ + bufsize = (worst_case_len_expr) * sizeof(chartype2); \ /* Worst case length */ \ - chartype2 buf[worst_case_len_expr]; \ + if (bufsize <= STACK_MAX) { \ + buf = alloca(bufsize); \ + buf_onheap = false; \ + } else { \ + buf = MALLOC(bufsize); \ + if (!buf) \ + return WIMLIB_ERR_NOMEM; \ + buf_onheap = true; \ + } \ + \ char *inbuf = (char*)in; \ size_t inbytesleft = in_nbytes; \ char *outbuf = (char*)buf; \ - size_t outbytesleft = sizeof(buf); \ + size_t outbytesleft = bufsize; \ size_t len; \ int ret; \ \ @@ -157,10 +174,12 @@ varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\ } \ ret = err_return; \ } else { \ - *out_nbytes_ret = sizeof(buf) - outbytesleft; \ + *out_nbytes_ret = bufsize - outbytesleft; \ ret = 0; \ } \ put_iconv(cd); \ + if (buf_onheap) \ + FREE(buf); \ return ret; \ } \ \ @@ -400,7 +419,7 @@ static u16 upcase[65536]; * Copyright (c) 2008-2011 Jean-Pierre Andre * Copyright (c) 2008 Bernhard Kaindl * - * License GPLv2 or later. (Or may actually be LGPL since it's in libntfs-3g.) + * License GPLv2 or later. * * The expanded table itself is the same as that used by Windows Vista for * NTFS case insensitivity.