From: Michihiro NAKAJIMA Date: Tue, 3 Mar 2009 14:07:38 +0000 (-0500) Subject: On Windows, reduce memory allocation and character-set convert. X-Git-Tag: v2.7.0~206 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=04baa3545d76cbec8f0d3f6497803fb5a0dc194d;p=thirdparty%2Flibarchive.git On Windows, reduce memory allocation and character-set convert. SVN-Revision: 722 --- diff --git a/libarchive/archive_write_set_format_mtree.c b/libarchive/archive_write_set_format_mtree.c index 52997e48c..e12142997 100644 --- a/libarchive/archive_write_set_format_mtree.c +++ b/libarchive/archive_write_set_format_mtree.c @@ -344,10 +344,10 @@ dir_len(struct archive_entry *entry) static size_t dir_len(struct archive_entry *entry) { - const wchar_t *wp, *r; + wchar_t wc; const char *path; - const char *p; - size_t al, l; + const char *p, *rp; + size_t al, l, size; path = archive_entry_pathname(entry); al = l = -1; @@ -359,14 +359,18 @@ dir_len(struct archive_entry *entry) } if (l == -1) goto alen; - if ((wp = archive_entry_pathname_w(entry)) == NULL) - goto alen; - r = wp + wcslen(wp); - while (--r >= wp && *r != L'/' && *r != L'\\') - ; - l = wcstombs(NULL, ++r, 0); - if (l != -1) - return (p - path - l); + size = p - path; + rp = p = path; + while (*p != '\0') { + l = mbtowc(&wc, p, size); + if (l == -1) + goto alen; + if (l == 1 && (wc == L'/' || wc == L'\\')) + rp = p; + p += l; + size -= l; + } + return (rp - path + 1); alen: if (al == -1) return (0); diff --git a/tar/bsdtar_windows.c b/tar/bsdtar_windows.c index 25bdab34c..ef70b1aaf 100644 --- a/tar/bsdtar_windows.c +++ b/tar/bsdtar_windows.c @@ -1119,10 +1119,9 @@ bsdtar_is_privileged(struct bsdtar *bsdtar) static size_t dir_len_w(const char *path) { - const wchar_t *r; - wchar_t *wp; - const char *p; - size_t al, l; + wchar_t wc; + const char *p, *rp; + size_t al, l, size; al = l = -1; for (p = path; *p != '\0'; ++p) { @@ -1133,21 +1132,18 @@ dir_len_w(const char *path) } if (l == -1) goto alen; - l = p - path; - if ((wp = malloc((l + 1) * sizeof(wchar_t))) == NULL) - goto alen; - if ((l = mbstowcs(wp, path, l)) == -1) { - free(wp); - goto alen; + size = p - path; + rp = p = path; + while (*p != '\0') { + l = mbtowc(&wc, p, size); + if (l == -1) + goto alen; + if (l == 1 && (wc == L'/' || wc == L'\\')) + rp = p; + p += l; + size -= l; } - wp[l] = L'\0'; - r = wp + l; - while (--r >= wp && *r != L'/' && *r != L'\\') - ; - l = wcstombs(NULL, ++r, 0); - free(wp); - if (l != -1) - return (p - path - l); + return (rp - path + 1); alen: if (al == -1) return (0);