#include "archive_platform.h"
__FBSDID("$FreeBSD: head/lib/libarchive/archive_string.c 201095 2009-12-28 02:33:22Z kientzle $");
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif
#if defined(_WIN32) && !defined(__CYGWIN__)
#include <windows.h>
#endif
#include "archive_private.h"
#include "archive_string.h"
struct archive_string *
__archive_string_append(struct archive_string *as, const char *p, size_t s)
{
if (__archive_string_ensure(as, as->length + s + 1) == NULL)
__archive_errx(1, "Out of memory");
memcpy(as->s + as->length, p, s);
as->s[as->length + s] = 0;
as->length += s;
return (as);
}
void
__archive_string_copy(struct archive_string *dest, struct archive_string *src)
{
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;
}
}
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;
}
}
void
__archive_string_free(struct archive_string *as)
{
as->length = 0;
as->buffer_length = 0;
if (as->s != NULL) {
free(as->s);
as->s = NULL;
}
}
struct archive_string *
__archive_string_ensure(struct archive_string *as, size_t s)
{
if (as->s && (s <= as->buffer_length))
return (as);
if (as->buffer_length < 32)
as->buffer_length = 32;
else if (as->buffer_length < 8192)
as->buffer_length += as->buffer_length;
else {
size_t old_length = as->buffer_length;
as->buffer_length += as->buffer_length / 4;
if (as->buffer_length < old_length) {
free(as->s);
as->s = NULL;
return (NULL);
}
}
if (as->buffer_length < s)
as->buffer_length = s;
as->s = (char *)realloc(as->s, as->buffer_length);
if (as->s == NULL)
return (NULL);
return (as);
}
struct archive_string *
__archive_strncat(struct archive_string *as, const void *_p, size_t n)
{
size_t s;
const char *p, *pp;
p = (const char *)_p;
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));
}
struct archive_string *
__archive_strappend_w_utf8(struct archive_string *as, const wchar_t *w)
{
char *p;
unsigned wc;
char buff[256];
struct archive_string *return_val = as;
p = buff;
while (*w != L'\0') {
if ((size_t)(p - buff) >= (size_t)(sizeof(buff) - 16)) {
*p = '\0';
archive_strcat(as, buff);
p = buff;
}
wc = *w++;
if (wc >= 0xD800 && wc <= 0xDBff
&& *w >= 0xDC00 && *w <= 0xDFFF) {
wc -= 0xD800;
wc *= 0x400;
wc += (*w - 0xDC00);
wc += 0x10000;
++w;
}
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 {
*p++ = '?';
return_val = NULL;
}
}
*p = '\0';
archive_strcat(as, buff);
return (return_val);
}
static int
utf8_to_unicode(int *pwc, const char *s, size_t n)
{
int ch;
ch = (unsigned char)*s;
if (ch == 0) {
return (0);
}
if ((ch & 0x80) == 0) {
*pwc = ch & 0x7f;
return (1);
}
if ((ch & 0xe0) == 0xc0) {
if (n < 2)
return (-1);
if ((s[1] & 0xc0) != 0x80) return (-1);
*pwc = ((ch & 0x1f) << 6) | (s[1] & 0x3f);
return (2);
}
if ((ch & 0xf0) == 0xe0) {
if (n < 3)
return (-1);
if ((s[1] & 0xc0) != 0x80) return (-1);
if ((s[2] & 0xc0) != 0x80) return (-1);
*pwc = ((ch & 0x0f) << 12)
| ((s[1] & 0x3f) << 6)
| (s[2] & 0x3f);
return (3);
}
if ((ch & 0xf8) == 0xf0) {
if (n < 4)
return (-1);
if ((s[1] & 0xc0) != 0x80) return (-1);
if ((s[2] & 0xc0) != 0x80) return (-1);
if ((s[3] & 0xc0) != 0x80) return (-1);
*pwc = ((ch & 0x07) << 18)
| ((s[1] & 0x3f) << 12)
| ((s[2] & 0x3f) << 6)
| (s[3] & 0x3f);
return (4);
}
return (-1);
}
wchar_t *
__archive_string_utf8_w(struct archive_string *as)
{
wchar_t *ws, *dest;
int wc, wc2;
const char *src;
int n;
ws = (wchar_t *)malloc((as->length + 1) * sizeof(wchar_t));
if (ws == NULL)
__archive_errx(1, "Out of memory");
dest = ws;
src = as->s;
while (*src != '\0') {
n = utf8_to_unicode(&wc, src, 8);
if (n == 0)
break;
if (n < 0) {
free(ws);
return (NULL);
}
src += n;
if (wc >= 0xDC00 && wc <= 0xDBFF) {
n = utf8_to_unicode(&wc2, src, 8);
if (n < 0) {
free(ws);
return (NULL);
}
if (n == 0)
break;
if (wc2 < 0xDC00 || wc2 > 0xDFFF) {
free(ws);
return (NULL);
} else {
src += n;
wc -= 0xD800;
wc *= 0x400;
wc += wc2 - 0xDC00;
wc += 0x10000;
}
}
if ((sizeof(wchar_t) < 4) && (wc > 0xffff)) {
wc -= 0x10000;
*dest++ = ((wc >> 10) & 0x3ff) + 0xD800;
*dest++ = (wc & 0x3ff) + 0xDC00;
} else
*dest++ = wc;
}
*dest = L'\0';
return (ws);
}
#if defined(_WIN32) && !defined(__CYGWIN__)
struct archive_string *
__archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
{
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");
l = WideCharToMultiByte(CP_ACP, 0, w, wl, p, l, NULL, &useDefaultChar);
if (l == 0) {
free(p);
return (NULL);
}
__archive_string_append(as, p, l);
free(p);
return (as);
}
#else
struct archive_string *
__archive_strappend_w_mbs(struct archive_string *as, const wchar_t *w)
{
#if !defined(HAVE_WCTOMB) && !defined(HAVE_WCRTOMB)
return __archive_strappend_w_utf8(as, w);
#else
int n;
char *p;
char buff[256];
#if HAVE_WCRTOMB
mbstate_t shift_state;
memset(&shift_state, 0, sizeof(shift_state));
#else
wctomb(NULL, L'\0');
#endif
p = buff;
while (*w != L'\0') {
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;
}
*p = '\0';
archive_strcat(as, buff);
return (as);
#endif
}
#endif