]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
On Windows, reduce memory allocation and character-set convert.
authorMichihiro NAKAJIMA <ggcueroad@gmail.com>
Tue, 3 Mar 2009 14:07:38 +0000 (09:07 -0500)
committerMichihiro NAKAJIMA <ggcueroad@gmail.com>
Tue, 3 Mar 2009 14:07:38 +0000 (09:07 -0500)
SVN-Revision: 722

libarchive/archive_write_set_format_mtree.c
tar/bsdtar_windows.c

index 52997e48c62222a3c0a8ecaf53ac132cebc3c4af..e12142997abc63e86f38c60f29ea179bbe25e97c 100644 (file)
@@ -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);
index 25bdab34c8386b0a24196e34198cc98553adc680..ef70b1aafa9bc0f5765527d731f5220ad8874118 100644 (file)
@@ -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);