Skip to content

Commit 88182ba

Browse files
René Scharfegitster
authored andcommitted
archive-zip: support UTF-8 paths
Set general purpose flag 11 if we encounter a path that contains non-ASCII characters. We assume that all paths are given as UTF-8; no conversion is done. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bb52d22 commit 88182ba

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

archive-zip.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "cache.h"
55
#include "archive.h"
66
#include "streaming.h"
7+
#include "utf8.h"
78

89
static int zip_date;
910
static int zip_time;
@@ -16,7 +17,8 @@ static unsigned int zip_dir_offset;
1617
static unsigned int zip_dir_entries;
1718

1819
#define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
19-
#define ZIP_STREAM (8)
20+
#define ZIP_STREAM (1 << 3)
21+
#define ZIP_UTF8 (1 << 11)
2022

2123
struct zip_local_header {
2224
unsigned char magic[4];
@@ -164,6 +166,17 @@ static void set_zip_header_data_desc(struct zip_local_header *header,
164166
copy_le32(header->size, size);
165167
}
166168

169+
static int has_only_ascii(const char *s)
170+
{
171+
for (;;) {
172+
int c = *s++;
173+
if (c == '\0')
174+
return 1;
175+
if (!isascii(c))
176+
return 0;
177+
}
178+
}
179+
167180
#define STREAM_BUFFER_SIZE (1024 * 16)
168181

169182
static int write_zip_entry(struct archiver_args *args,
@@ -187,6 +200,13 @@ static int write_zip_entry(struct archiver_args *args,
187200

188201
crc = crc32(0, NULL, 0);
189202

203+
if (!has_only_ascii(path)) {
204+
if (is_utf8(path))
205+
flags |= ZIP_UTF8;
206+
else
207+
warning("Path is not valid UTF-8: %s", path);
208+
}
209+
190210
if (pathlen > 0xffff) {
191211
return error("path too long (%d chars, SHA1: %s): %s",
192212
(int)pathlen, sha1_to_hex(sha1), path);

0 commit comments

Comments
 (0)