2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
2007-01-09 08:05:56 +00:00
|
|
|
* Copyright (c) 2003-2007 Tim Kientzle
|
2004-02-09 23:22:54 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
2007-01-09 08:05:56 +00:00
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2004-02-09 23:22:54 +00:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Basic resizable string support, to simplify manipulating arbitrary-sized
|
|
|
|
* strings while minimizing heap activity.
|
|
|
|
*/
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2008-05-26 17:00:24 +00:00
|
|
|
#ifdef HAVE_WCHAR_H
|
|
|
|
#include <wchar.h>
|
|
|
|
#endif
|
2009-04-12 05:33:34 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2009-03-03 17:02:51 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
2008-05-26 17:00:24 +00:00
|
|
|
|
2004-08-14 03:45:45 +00:00
|
|
|
#include "archive_private.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
#include "archive_string.h"
|
|
|
|
|
|
|
|
struct archive_string *
|
|
|
|
__archive_string_append(struct archive_string *as, const char *p, size_t s)
|
|
|
|
{
|
2007-07-15 19:13:59 +00:00
|
|
|
if (__archive_string_ensure(as, as->length + s + 1) == NULL)
|
|
|
|
__archive_errx(1, "Out of memory");
|
2004-02-09 23:22:54 +00:00
|
|
|
memcpy(as->s + as->length, p, s);
|
|
|
|
as->s[as->length + s] = 0;
|
|
|
|
as->length += s;
|
|
|
|
return (as);
|
|
|
|
}
|
|
|
|
|
2007-05-29 01:00:21 +00:00
|
|
|
void
|
|
|
|
__archive_string_copy(struct archive_string *dest, struct archive_string *src)
|
|
|
|
{
|
2008-05-26 17:00:24 +00:00
|
|
|
if (src->length == 0)
|
|
|
|
dest->length = 0;
|
|
|
|
else {
|
|
|
|
if (__archive_string_ensure(dest, src->length + 1) == NULL)
|
|
|
|
__archive_errx(1, "Out of memory");
|
|
|
|
memcpy(dest->s, src->s, src->length);
|
|
|
|
dest->length = src->length;
|
|
|
|
dest->s[dest->length] = 0;
|
|
|
|
}
|
2007-05-29 01:00:21 +00:00
|
|
|
}
|
|
|
|
|
2008-12-06 05:56:43 +00:00
|
|
|
void
|
|
|
|
__archive_string_concat(struct archive_string *dest, struct archive_string *src)
|
|
|
|
{
|
|
|
|
if (src->length > 0) {
|
|
|
|
if (__archive_string_ensure(dest, dest->length + src->length + 1) == NULL)
|
|
|
|
__archive_errx(1, "Out of memory");
|
|
|
|
memcpy(dest->s + dest->length, src->s, src->length);
|
|
|
|
dest->length += src->length;
|
|
|
|
dest->s[dest->length] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
void
|
|
|
|
__archive_string_free(struct archive_string *as)
|
|
|
|
{
|
|
|
|
as->length = 0;
|
|
|
|
as->buffer_length = 0;
|
2008-05-26 17:00:24 +00:00
|
|
|
if (as->s != NULL) {
|
2004-02-09 23:22:54 +00:00
|
|
|
free(as->s);
|
2008-05-26 17:00:24 +00:00
|
|
|
as->s = NULL;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2007-07-15 19:13:59 +00:00
|
|
|
/* Returns NULL on any allocation failure. */
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_string *
|
|
|
|
__archive_string_ensure(struct archive_string *as, size_t s)
|
|
|
|
{
|
2008-05-26 17:00:24 +00:00
|
|
|
/* If buffer is already big enough, don't reallocate. */
|
2004-02-09 23:22:54 +00:00
|
|
|
if (as->s && (s <= as->buffer_length))
|
|
|
|
return (as);
|
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
/*
|
|
|
|
* Growing the buffer at least exponentially ensures that
|
|
|
|
* append operations are always linear in the number of
|
|
|
|
* characters appended. Using a smaller growth rate for
|
|
|
|
* larger buffers reduces memory waste somewhat at the cost of
|
|
|
|
* a larger constant factor.
|
|
|
|
*/
|
2004-02-09 23:22:54 +00:00
|
|
|
if (as->buffer_length < 32)
|
2008-05-26 17:00:24 +00:00
|
|
|
/* Start with a minimum 32-character buffer. */
|
2004-02-09 23:22:54 +00:00
|
|
|
as->buffer_length = 32;
|
2008-05-26 17:00:24 +00:00
|
|
|
else if (as->buffer_length < 8192)
|
|
|
|
/* Buffers under 8k are doubled for speed. */
|
2009-04-12 04:59:11 +00:00
|
|
|
as->buffer_length += as->buffer_length;
|
2008-05-26 17:00:24 +00:00
|
|
|
else {
|
|
|
|
/* Buffers 8k and over grow by at least 25% each time. */
|
|
|
|
size_t old_length = as->buffer_length;
|
2009-04-12 04:59:11 +00:00
|
|
|
as->buffer_length += as->buffer_length / 4;
|
2008-05-26 17:00:24 +00:00
|
|
|
/* Be safe: If size wraps, release buffer and return NULL. */
|
|
|
|
if (as->buffer_length < old_length) {
|
|
|
|
free(as->s);
|
|
|
|
as->s = NULL;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* The computation above is a lower limit to how much we'll
|
|
|
|
* grow the buffer. In any case, we have to grow it enough to
|
|
|
|
* hold the request.
|
|
|
|
*/
|
|
|
|
if (as->buffer_length < s)
|
|
|
|
as->buffer_length = s;
|
|
|
|
/* Now we can reallocate the buffer. */
|
2006-11-10 06:39:46 +00:00
|
|
|
as->s = (char *)realloc(as->s, as->buffer_length);
|
2004-02-09 23:22:54 +00:00
|
|
|
if (as->s == NULL)
|
2007-07-15 19:13:59 +00:00
|
|
|
return (NULL);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (as);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct archive_string *
|
2009-04-12 04:59:11 +00:00
|
|
|
__archive_strncat(struct archive_string *as, const void *_p, size_t n)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
size_t s;
|
2009-04-12 04:59:11 +00:00
|
|
|
const char *p, *pp;
|
|
|
|
|
|
|
|
p = (const char *)_p;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Like strlen(p), except won't examine positions beyond p[n]. */
|
|
|
|
s = 0;
|
|
|
|
pp = p;
|
|
|
|
while (*pp && s < n) {
|
|
|
|
pp++;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
return (__archive_string_append(as, p, s));
|
|
|
|
}
|
|
|
|
|
|
|
|
struct archive_string *
|
|
|
|
__archive_strappend_char(struct archive_string *as, char c)
|
|
|
|
{
|
|
|
|
return (__archive_string_append(as, &c, 1));
|
|
|
|
}
|
2004-12-22 06:12:40 +00:00
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
/*
|
2009-04-12 05:33:34 +00:00
|
|
|
* Translates a wide character string into UTF-8 and appends
|
|
|
|
* to the archive_string. Note: returns NULL if conversion fails,
|
|
|
|
* but still leaves a best-effort conversion in the argument as.
|
2008-05-26 17:00:24 +00:00
|
|
|
*/
|
2009-04-12 05:33:34 +00:00
|
|
|
struct archive_string *
|
|
|
|
__archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
|
2008-05-26 17:00:24 +00:00
|
|
|
{
|
|
|
|
char *p;
|
2009-04-12 05:33:34 +00:00
|
|
|
unsigned wc;
|
2008-05-26 17:00:24 +00:00
|
|
|
char buff[256];
|
2009-04-12 05:33:34 +00:00
|
|
|
struct archive_string *return_val = as;
|
2008-06-15 05:12:47 +00:00
|
|
|
|
2008-05-26 17:00:24 +00:00
|
|
|
/*
|
|
|
|
* Convert one wide char at a time into 'buff', whenever that
|
|
|
|
* fills, append it to the string.
|
|
|
|
*/
|
|
|
|
p = buff;
|
|
|
|
while (*w != L'\0') {
|
|
|
|
/* Flush the buffer when we have <=16 bytes free. */
|
|
|
|
/* (No encoding has a single character >16 bytes.) */
|
|
|
|
if ((size_t)(p - buff) >= (size_t)(sizeof(buff) - 16)) {
|
|
|
|
*p = '\0';
|
|
|
|
archive_strcat(as, buff);
|
|
|
|
p = buff;
|
|
|
|
}
|
2009-04-12 05:33:34 +00:00
|
|
|
wc = *w++;
|
|
|
|
/* If this is a surrogate pair, assemble the full code point.*/
|
|
|
|
/* Note: wc must not be wchar_t here, because the full code
|
|
|
|
* point can be more than 16 bits! */
|
|
|
|
if (wc >= 0xD800 && wc <= 0xDBff
|
|
|
|
&& *w >= 0xDC00 && *w <= 0xDFFF) {
|
|
|
|
wc -= 0xD800;
|
|
|
|
wc *= 0x400;
|
|
|
|
wc += (*w - 0xDC00);
|
|
|
|
wc += 0x10000;
|
|
|
|
++w;
|
|
|
|
}
|
|
|
|
/* Translate code point to UTF8 */
|
|
|
|
if (wc <= 0x7f) {
|
|
|
|
*p++ = (char)wc;
|
|
|
|
} else if (wc <= 0x7ff) {
|
|
|
|
*p++ = 0xc0 | ((wc >> 6) & 0x1f);
|
|
|
|
*p++ = 0x80 | (wc & 0x3f);
|
|
|
|
} else if (wc <= 0xffff) {
|
|
|
|
*p++ = 0xe0 | ((wc >> 12) & 0x0f);
|
|
|
|
*p++ = 0x80 | ((wc >> 6) & 0x3f);
|
|
|
|
*p++ = 0x80 | (wc & 0x3f);
|
|
|
|
} else if (wc <= 0x1fffff) {
|
|
|
|
*p++ = 0xf0 | ((wc >> 18) & 0x07);
|
|
|
|
*p++ = 0x80 | ((wc >> 12) & 0x3f);
|
|
|
|
*p++ = 0x80 | ((wc >> 6) & 0x3f);
|
|
|
|
*p++ = 0x80 | (wc & 0x3f);
|
|
|
|
} else {
|
|
|
|
/* Unicode has no codes larger than 0x1fffff. */
|
|
|
|
/* TODO: use \uXXXX escape here instead of ? */
|
|
|
|
*p++ = '?';
|
|
|
|
return_val = NULL;
|
|
|
|
}
|
2008-05-26 17:00:24 +00:00
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
archive_strcat(as, buff);
|
2009-04-12 05:33:34 +00:00
|
|
|
return (return_val);
|
2008-05-26 17:00:24 +00:00
|
|
|
}
|
|
|
|
|
2008-06-15 11:28:56 +00:00
|
|
|
static int
|
2009-04-12 05:33:34 +00:00
|
|
|
utf8_to_unicode(int *pwc, const char *s, size_t n)
|
2008-05-26 17:00:24 +00:00
|
|
|
{
|
|
|
|
int ch;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Decode 1-4 bytes depending on the value of the first byte.
|
|
|
|
*/
|
|
|
|
ch = (unsigned char)*s;
|
|
|
|
if (ch == 0) {
|
|
|
|
return (0); /* Standard: return 0 for end-of-string. */
|
|
|
|
}
|
|
|
|
if ((ch & 0x80) == 0) {
|
|
|
|
*pwc = ch & 0x7f;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if ((ch & 0xe0) == 0xc0) {
|
|
|
|
if (n < 2)
|
2008-06-15 11:28:56 +00:00
|
|
|
return (-1);
|
|
|
|
if ((s[1] & 0xc0) != 0x80) return (-1);
|
2008-05-26 17:00:24 +00:00
|
|
|
*pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
|
|
|
|
return (2);
|
|
|
|
}
|
|
|
|
if ((ch & 0xf0) == 0xe0) {
|
|
|
|
if (n < 3)
|
2008-06-15 11:28:56 +00:00
|
|
|
return (-1);
|
|
|
|
if ((s[1] & 0xc0) != 0x80) return (-1);
|
|
|
|
if ((s[2] & 0xc0) != 0x80) return (-1);
|
2008-05-26 17:00:24 +00:00
|
|
|
*pwc = ((ch & 0x0f) << 12)
|
|
|
|
| ((s[1] & 0x3f) << 6)
|
|
|
|
| (s[2] & 0x3f);
|
|
|
|
return (3);
|
|
|
|
}
|
|
|
|
if ((ch & 0xf8) == 0xf0) {
|
|
|
|
if (n < 4)
|
2008-06-15 11:28:56 +00:00
|
|
|
return (-1);
|
|
|
|
if ((s[1] & 0xc0) != 0x80) return (-1);
|
|
|
|
if ((s[2] & 0xc0) != 0x80) return (-1);
|
|
|
|
if ((s[3] & 0xc0) != 0x80) return (-1);
|
2008-05-26 17:00:24 +00:00
|
|
|
*pwc = ((ch & 0x07) << 18)
|
|
|
|
| ((s[1] & 0x3f) << 12)
|
|
|
|
| ((s[2] & 0x3f) << 6)
|
|
|
|
| (s[3] & 0x3f);
|
|
|
|
return (4);
|
|
|
|
}
|
|
|
|
/* Invalid first byte. */
|
2008-06-15 11:28:56 +00:00
|
|
|
return (-1);
|
2008-05-26 17:00:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-04-12 05:33:34 +00:00
|
|
|
* Return a wide-character Unicode string by converting this archive_string
|
|
|
|
* from UTF-8. We assume that systems with 16-bit wchar_t always use
|
|
|
|
* UTF16 and systems with 32-bit wchar_t can accept UCS4.
|
2008-05-26 17:00:24 +00:00
|
|
|
*/
|
|
|
|
wchar_t *
|
|
|
|
__archive_string_utf8_w(struct archive_string *as)
|
|
|
|
{
|
|
|
|
wchar_t *ws, *dest;
|
2009-04-12 05:33:34 +00:00
|
|
|
int wc, wc2;/* Must be large enough for a 21-bit Unicode code point. */
|
2008-05-26 17:00:24 +00:00
|
|
|
const char *src;
|
2008-06-15 11:28:56 +00:00
|
|
|
int n;
|
2008-05-26 17:00:24 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
ws = (wchar_t *)malloc((as->length + 1) * sizeof(wchar_t));
|
|
|
|
if (ws == NULL)
|
|
|
|
__archive_errx(1, "Out of memory");
|
|
|
|
err = 0;
|
|
|
|
dest = ws;
|
|
|
|
src = as->s;
|
|
|
|
while (*src != '\0') {
|
2009-04-12 05:33:34 +00:00
|
|
|
n = utf8_to_unicode(&wc, src, 8);
|
2008-05-26 17:00:24 +00:00
|
|
|
if (n == 0)
|
|
|
|
break;
|
2008-06-15 11:28:56 +00:00
|
|
|
if (n < 0) {
|
2008-05-26 17:00:24 +00:00
|
|
|
free(ws);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
src += n;
|
2009-04-12 05:33:34 +00:00
|
|
|
if (wc >= 0xDC00 && wc <= 0xDBFF) {
|
|
|
|
/* This is a leading surrogate; some idiot
|
|
|
|
* has translated UTF16 to UTF8 without combining
|
|
|
|
* surrogates; rebuild the full code point before
|
|
|
|
* continuing. */
|
|
|
|
n = utf8_to_unicode(&wc2, src, 8);
|
|
|
|
if (n < 0) {
|
|
|
|
free(ws);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
if (n == 0) /* Ignore the leading surrogate */
|
|
|
|
break;
|
|
|
|
if (wc2 < 0xDC00 || wc2 > 0xDFFF) {
|
|
|
|
/* If the second character isn't a
|
|
|
|
* trailing surrogate, then someone
|
|
|
|
* has really screwed up and this is
|
|
|
|
* invalid. */
|
|
|
|
free(ws);
|
|
|
|
return (NULL);
|
|
|
|
} else {
|
|
|
|
src += n;
|
|
|
|
wc -= 0xD800;
|
|
|
|
wc *= 0x400;
|
|
|
|
wc += wc2 - 0xDC00;
|
|
|
|
wc += 0x10000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((sizeof(wchar_t) < 4) && (wc > 0xffff)) {
|
|
|
|
/* We have a code point that won't fit into a
|
|
|
|
* wchar_t; convert it to a surrogate pair. */
|
|
|
|
wc -= 0x10000;
|
|
|
|
*dest++ = ((wc >> 10) & 0x3ff) + 0xD800;
|
|
|
|
*dest++ = (wc & 0x3ff) + 0xDC00;
|
|
|
|
} else
|
|
|
|
*dest++ = wc;
|
2008-05-26 17:00:24 +00:00
|
|
|
}
|
|
|
|
*dest++ = L'\0';
|
|
|
|
return (ws);
|
|
|
|
}
|
2009-03-03 17:02:51 +00:00
|
|
|
|
2009-04-12 05:33:34 +00:00
|
|
|
#if defined(_WIN32) && !defined(__CYGWIN__)
|
2009-03-03 17:02:51 +00:00
|
|
|
|
2009-04-12 05:33:34 +00:00
|
|
|
/*
|
|
|
|
* Translates a wide character string into current locale character set
|
|
|
|
* and appends to the archive_string. Note: returns NULL if conversion
|
|
|
|
* fails.
|
|
|
|
*
|
|
|
|
* Win32 builds use WideCharToMultiByte from the Windows API.
|
|
|
|
* (Maybe Cygwin should too? WideCharToMultiByte will know a
|
|
|
|
* lot more about local character encodings than the wcrtomb()
|
|
|
|
* wrapper is going to know.)
|
|
|
|
*/
|
|
|
|
struct archive_string *
|
|
|
|
__archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
|
2009-03-03 17:02:51 +00:00
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int l, wl;
|
|
|
|
BOOL useDefaultChar = FALSE;
|
|
|
|
|
|
|
|
wl = (int)wcslen(w);
|
|
|
|
l = wl * 4 + 4;
|
|
|
|
p = malloc(l);
|
|
|
|
if (p == NULL)
|
|
|
|
__archive_errx(1, "Out of memory");
|
|
|
|
/* To check a useDefaultChar is to simulate error handling of
|
|
|
|
* the my_wcstombs() which is running on non Windows system with
|
|
|
|
* wctomb().
|
|
|
|
* And to set NULL for last argument is necessary when a codepage
|
|
|
|
* is not CP_ACP(current locale).
|
|
|
|
*/
|
2009-04-12 05:33:34 +00:00
|
|
|
l = WideCharToMultiByte(CP_ACP, 0, w, wl, p, l, NULL, &useDefaultChar);
|
|
|
|
if (l == 0) {
|
2009-03-03 17:02:51 +00:00
|
|
|
free(p);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
__archive_string_append(as, p, l);
|
|
|
|
free(p);
|
|
|
|
return (as);
|
|
|
|
}
|
|
|
|
|
2009-04-12 05:33:34 +00:00
|
|
|
#else
|
2009-03-03 17:02:51 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Translates a wide character string into current locale character set
|
|
|
|
* and appends to the archive_string. Note: returns NULL if conversion
|
|
|
|
* fails.
|
2009-04-12 05:33:34 +00:00
|
|
|
*
|
|
|
|
* Non-Windows uses ISO C wcrtomb() or wctomb() to perform the conversion
|
|
|
|
* one character at a time. If a non-Windows platform doesn't have
|
|
|
|
* either of these, fall back to the built-in UTF8 conversion.
|
2009-03-03 17:02:51 +00:00
|
|
|
*/
|
|
|
|
struct archive_string *
|
|
|
|
__archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
|
|
|
|
{
|
2009-04-12 05:33:34 +00:00
|
|
|
#if !defined(HAVE_WCTOMB) && !defined(HAVE_WCRTOMB)
|
|
|
|
/* If there's no built-in locale support, fall back to UTF8 always. */
|
|
|
|
return __archive_strappend_w_utf8(as, w);
|
|
|
|
#else
|
|
|
|
/* We cannot use the standard wcstombs() here because it
|
|
|
|
* cannot tell us how big the output buffer should be. So
|
|
|
|
* I've built a loop around wcrtomb() or wctomb() that
|
|
|
|
* converts a character at a time and resizes the string as
|
|
|
|
* needed. We prefer wcrtomb() when it's available because
|
|
|
|
* it's thread-safe. */
|
2009-03-03 17:02:51 +00:00
|
|
|
int n;
|
2009-04-12 05:33:34 +00:00
|
|
|
char *p;
|
|
|
|
char buff[256];
|
|
|
|
#if HAVE_WCRTOMB
|
|
|
|
mbstate_t shift_state;
|
2009-03-03 17:02:51 +00:00
|
|
|
|
2009-04-12 05:33:34 +00:00
|
|
|
memset(&shift_state, 0, sizeof(shift_state));
|
|
|
|
#else
|
|
|
|
/* Clear the shift state before starting. */
|
|
|
|
wctomb(NULL, L'\0');
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convert one wide char at a time into 'buff', whenever that
|
|
|
|
* fills, append it to the string.
|
|
|
|
*/
|
|
|
|
p = buff;
|
|
|
|
while (*w != L'\0') {
|
|
|
|
/* Flush the buffer when we have <=16 bytes free. */
|
|
|
|
/* (No encoding has a single character >16 bytes.) */
|
|
|
|
if ((size_t)(p - buff) >= (size_t)(sizeof(buff) - MB_CUR_MAX)) {
|
|
|
|
*p = '\0';
|
|
|
|
archive_strcat(as, buff);
|
|
|
|
p = buff;
|
|
|
|
}
|
|
|
|
#if HAVE_WCRTOMB
|
|
|
|
n = wcrtomb(p, *w++, &shift_state);
|
|
|
|
#else
|
|
|
|
n = wctomb(p, *w++);
|
|
|
|
#endif
|
|
|
|
if (n == -1)
|
|
|
|
return (NULL);
|
|
|
|
p += n;
|
2009-03-03 17:02:51 +00:00
|
|
|
}
|
2009-04-12 05:33:34 +00:00
|
|
|
*p = '\0';
|
|
|
|
archive_strcat(as, buff);
|
|
|
|
return (as);
|
|
|
|
#endif
|
2009-03-03 17:02:51 +00:00
|
|
|
}
|
|
|
|
|
2009-04-12 05:33:34 +00:00
|
|
|
#endif /* _WIN32 && ! __CYGWIN__ */
|