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 <rene.scharfe@lsrfire.ath.cx> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Sep 18, 2012 at 21:46 UTC
88182bab001a9b9e9b0acd4b888693fa7c28ff4c
1 file changed
+21
-1
archive-zip.c
+21
-1
@@ -4,6 +4,7 @@
4
#include "cache.h"
5
#include "archive.h"
6
#include "streaming.h"
7
+#include "utf8.h"
8
9
static int zip_date;
10
static int zip_time;
@@ -16,7 +17,8 @@ static unsigned int zip_dir_offset;
17
static unsigned int zip_dir_entries;
18
19
#define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
19
-#define ZIP_STREAM (8)
20
+#define ZIP_STREAM (1 << 3)
21
+#define ZIP_UTF8 (1 << 11)
22
23
struct zip_local_header {
24
unsigned char magic[4];
@@ -164,6 +166,17 @@ static void set_zip_header_data_desc(struct zip_local_header *header,
166
copy_le32(header->size, size);
167
}
168
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
+
180
#define STREAM_BUFFER_SIZE (1024 * 16)
181
182
static int write_zip_entry(struct archiver_args *args,
@@ -187,6 +200,13 @@ static int write_zip_entry(struct archiver_args *args,
200
201
crc = crc32(0, NULL, 0);
202
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
+
210
if (pathlen > 0xffff) {
211
return error("path too long (%d chars, SHA1: %s): %s",
212
(int)pathlen, sha1_to_hex(sha1), path);