Coverage Report

Created: 2024-09-08 06:23

/src/git/archive-zip.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2006 Rene Scharfe
3
 */
4
5
#define USE_THE_REPOSITORY_VARIABLE
6
7
#include "git-compat-util.h"
8
#include "config.h"
9
#include "archive.h"
10
#include "gettext.h"
11
#include "git-zlib.h"
12
#include "hex.h"
13
#include "streaming.h"
14
#include "utf8.h"
15
#include "object-store-ll.h"
16
#include "strbuf.h"
17
#include "userdiff.h"
18
#include "write-or-die.h"
19
#include "xdiff-interface.h"
20
#include "date.h"
21
22
static int zip_date;
23
static int zip_time;
24
25
/* We only care about the "buf" part here. */
26
static struct strbuf zip_dir;
27
28
static uintmax_t zip_offset;
29
static uint64_t zip_dir_entries;
30
31
static unsigned int max_creator_version;
32
33
0
#define ZIP_STREAM  (1 <<  3)
34
0
#define ZIP_UTF8  (1 << 11)
35
36
enum zip_method {
37
  ZIP_METHOD_STORE = 0,
38
  ZIP_METHOD_DEFLATE = 8
39
};
40
41
struct zip_local_header {
42
  unsigned char magic[4];
43
  unsigned char version[2];
44
  unsigned char flags[2];
45
  unsigned char compression_method[2];
46
  unsigned char mtime[2];
47
  unsigned char mdate[2];
48
  unsigned char crc32[4];
49
  unsigned char compressed_size[4];
50
  unsigned char size[4];
51
  unsigned char filename_length[2];
52
  unsigned char extra_length[2];
53
  unsigned char _end[1];
54
};
55
56
struct zip_data_desc {
57
  unsigned char magic[4];
58
  unsigned char crc32[4];
59
  unsigned char compressed_size[4];
60
  unsigned char size[4];
61
  unsigned char _end[1];
62
};
63
64
struct zip64_data_desc {
65
  unsigned char magic[4];
66
  unsigned char crc32[4];
67
  unsigned char compressed_size[8];
68
  unsigned char size[8];
69
  unsigned char _end[1];
70
};
71
72
struct zip_dir_trailer {
73
  unsigned char magic[4];
74
  unsigned char disk[2];
75
  unsigned char directory_start_disk[2];
76
  unsigned char entries_on_this_disk[2];
77
  unsigned char entries[2];
78
  unsigned char size[4];
79
  unsigned char offset[4];
80
  unsigned char comment_length[2];
81
  unsigned char _end[1];
82
};
83
84
struct zip_extra_mtime {
85
  unsigned char magic[2];
86
  unsigned char extra_size[2];
87
  unsigned char flags[1];
88
  unsigned char mtime[4];
89
  unsigned char _end[1];
90
};
91
92
struct zip64_extra {
93
  unsigned char magic[2];
94
  unsigned char extra_size[2];
95
  unsigned char size[8];
96
  unsigned char compressed_size[8];
97
  unsigned char _end[1];
98
};
99
100
struct zip64_dir_trailer {
101
  unsigned char magic[4];
102
  unsigned char record_size[8];
103
  unsigned char creator_version[2];
104
  unsigned char version[2];
105
  unsigned char disk[4];
106
  unsigned char directory_start_disk[4];
107
  unsigned char entries_on_this_disk[8];
108
  unsigned char entries[8];
109
  unsigned char size[8];
110
  unsigned char offset[8];
111
  unsigned char _end[1];
112
};
113
114
struct zip64_dir_trailer_locator {
115
  unsigned char magic[4];
116
  unsigned char disk[4];
117
  unsigned char offset[8];
118
  unsigned char number_of_disks[4];
119
  unsigned char _end[1];
120
};
121
122
/*
123
 * On ARM, padding is added at the end of the struct, so a simple
124
 * sizeof(struct ...) reports two bytes more than the payload size
125
 * we're interested in.
126
 */
127
0
#define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
128
0
#define ZIP_DATA_DESC_SIZE  offsetof(struct zip_data_desc, _end)
129
0
#define ZIP64_DATA_DESC_SIZE  offsetof(struct zip64_data_desc, _end)
130
#define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
131
0
#define ZIP_DIR_TRAILER_SIZE  offsetof(struct zip_dir_trailer, _end)
132
0
#define ZIP_EXTRA_MTIME_SIZE  offsetof(struct zip_extra_mtime, _end)
133
#define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
134
0
  (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
135
0
#define ZIP64_EXTRA_SIZE  offsetof(struct zip64_extra, _end)
136
#define ZIP64_EXTRA_PAYLOAD_SIZE \
137
0
  (ZIP64_EXTRA_SIZE - offsetof(struct zip64_extra, size))
138
0
#define ZIP64_DIR_TRAILER_SIZE  offsetof(struct zip64_dir_trailer, _end)
139
#define ZIP64_DIR_TRAILER_RECORD_SIZE \
140
0
  (ZIP64_DIR_TRAILER_SIZE - \
141
0
   offsetof(struct zip64_dir_trailer, creator_version))
142
#define ZIP64_DIR_TRAILER_LOCATOR_SIZE \
143
0
  offsetof(struct zip64_dir_trailer_locator, _end)
144
145
static void copy_le16(unsigned char *dest, unsigned int n)
146
0
{
147
0
  dest[0] = 0xff & n;
148
0
  dest[1] = 0xff & (n >> 010);
149
0
}
150
151
static void copy_le32(unsigned char *dest, unsigned int n)
152
0
{
153
0
  dest[0] = 0xff & n;
154
0
  dest[1] = 0xff & (n >> 010);
155
0
  dest[2] = 0xff & (n >> 020);
156
0
  dest[3] = 0xff & (n >> 030);
157
0
}
158
159
static void copy_le64(unsigned char *dest, uint64_t n)
160
0
{
161
0
  dest[0] = 0xff & n;
162
0
  dest[1] = 0xff & (n >> 010);
163
0
  dest[2] = 0xff & (n >> 020);
164
0
  dest[3] = 0xff & (n >> 030);
165
0
  dest[4] = 0xff & (n >> 040);
166
0
  dest[5] = 0xff & (n >> 050);
167
0
  dest[6] = 0xff & (n >> 060);
168
0
  dest[7] = 0xff & (n >> 070);
169
0
}
170
171
static uint64_t clamp_max(uint64_t n, uint64_t max, int *clamped)
172
0
{
173
0
  if (n <= max)
174
0
    return n;
175
0
  *clamped = 1;
176
0
  return max;
177
0
}
178
179
static void copy_le16_clamp(unsigned char *dest, uint64_t n, int *clamped)
180
0
{
181
0
  copy_le16(dest, clamp_max(n, 0xffff, clamped));
182
0
}
183
184
static void copy_le32_clamp(unsigned char *dest, uint64_t n, int *clamped)
185
0
{
186
0
  copy_le32(dest, clamp_max(n, 0xffffffff, clamped));
187
0
}
188
189
static int strbuf_add_le(struct strbuf *sb, size_t size, uintmax_t n)
190
0
{
191
0
  while (size-- > 0) {
192
0
    strbuf_addch(sb, n & 0xff);
193
0
    n >>= 8;
194
0
  }
195
0
  return -!!n;
196
0
}
197
198
static uint32_t clamp32(uintmax_t n)
199
0
{
200
0
  const uintmax_t max = 0xffffffff;
201
0
  return (n < max) ? n : max;
202
0
}
203
204
static void *zlib_deflate_raw(void *data, unsigned long size,
205
            int compression_level,
206
            unsigned long *compressed_size)
207
0
{
208
0
  git_zstream stream;
209
0
  unsigned long maxsize;
210
0
  void *buffer;
211
0
  int result;
212
213
0
  git_deflate_init_raw(&stream, compression_level);
214
0
  maxsize = git_deflate_bound(&stream, size);
215
0
  buffer = xmalloc(maxsize);
216
217
0
  stream.next_in = data;
218
0
  stream.avail_in = size;
219
0
  stream.next_out = buffer;
220
0
  stream.avail_out = maxsize;
221
222
0
  do {
223
0
    result = git_deflate(&stream, Z_FINISH);
224
0
  } while (result == Z_OK);
225
226
0
  if (result != Z_STREAM_END) {
227
0
    free(buffer);
228
0
    return NULL;
229
0
  }
230
231
0
  git_deflate_end(&stream);
232
0
  *compressed_size = stream.total_out;
233
234
0
  return buffer;
235
0
}
236
237
static void write_zip_data_desc(unsigned long size,
238
        unsigned long compressed_size,
239
        unsigned long crc)
240
0
{
241
0
  if (size >= 0xffffffff || compressed_size >= 0xffffffff) {
242
0
    struct zip64_data_desc trailer;
243
0
    copy_le32(trailer.magic, 0x08074b50);
244
0
    copy_le32(trailer.crc32, crc);
245
0
    copy_le64(trailer.compressed_size, compressed_size);
246
0
    copy_le64(trailer.size, size);
247
0
    write_or_die(1, &trailer, ZIP64_DATA_DESC_SIZE);
248
0
    zip_offset += ZIP64_DATA_DESC_SIZE;
249
0
  } else {
250
0
    struct zip_data_desc trailer;
251
0
    copy_le32(trailer.magic, 0x08074b50);
252
0
    copy_le32(trailer.crc32, crc);
253
0
    copy_le32(trailer.compressed_size, compressed_size);
254
0
    copy_le32(trailer.size, size);
255
0
    write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
256
0
    zip_offset += ZIP_DATA_DESC_SIZE;
257
0
  }
258
0
}
259
260
static void set_zip_header_data_desc(struct zip_local_header *header,
261
             unsigned long size,
262
             unsigned long compressed_size,
263
             unsigned long crc)
264
0
{
265
0
  copy_le32(header->crc32, crc);
266
0
  copy_le32(header->compressed_size, compressed_size);
267
0
  copy_le32(header->size, size);
268
0
}
269
270
static int has_only_ascii(const char *s)
271
0
{
272
0
  for (;;) {
273
0
    int c = *s++;
274
0
    if (c == '\0')
275
0
      return 1;
276
0
    if (!isascii(c))
277
0
      return 0;
278
0
  }
279
0
}
280
281
static int entry_is_binary(struct index_state *istate, const char *path,
282
         const void *buffer, size_t size)
283
0
{
284
0
  struct userdiff_driver *driver = userdiff_find_by_path(istate, path);
285
0
  if (!driver)
286
0
    driver = userdiff_find_by_name("default");
287
0
  if (driver->binary != -1)
288
0
    return driver->binary;
289
0
  return buffer_is_binary(buffer, size);
290
0
}
291
292
#define STREAM_BUFFER_SIZE (1024 * 16)
293
294
static int write_zip_entry(struct archiver_args *args,
295
         const struct object_id *oid,
296
         const char *path, size_t pathlen,
297
         unsigned int mode,
298
         void *buffer, unsigned long size)
299
0
{
300
0
  struct zip_local_header header;
301
0
  uintmax_t offset = zip_offset;
302
0
  struct zip_extra_mtime extra;
303
0
  struct zip64_extra extra64;
304
0
  size_t header_extra_size = ZIP_EXTRA_MTIME_SIZE;
305
0
  int need_zip64_extra = 0;
306
0
  unsigned long attr2;
307
0
  unsigned long compressed_size;
308
0
  unsigned long crc;
309
0
  enum zip_method method;
310
0
  unsigned char *out;
311
0
  void *deflated = NULL;
312
0
  struct git_istream *stream = NULL;
313
0
  unsigned long flags = 0;
314
0
  int is_binary = -1;
315
0
  const char *path_without_prefix = path + args->baselen;
316
0
  unsigned int creator_version = 0;
317
0
  unsigned int version_needed = 10;
318
0
  size_t zip_dir_extra_size = ZIP_EXTRA_MTIME_SIZE;
319
0
  size_t zip64_dir_extra_payload_size = 0;
320
321
0
  crc = crc32(0, NULL, 0);
322
323
0
  if (!has_only_ascii(path)) {
324
0
    if (is_utf8(path))
325
0
      flags |= ZIP_UTF8;
326
0
    else
327
0
      warning(_("path is not valid UTF-8: %s"), path);
328
0
  }
329
330
0
  if (pathlen > 0xffff) {
331
0
    return error(_("path too long (%d chars, SHA1: %s): %s"),
332
0
        (int)pathlen, oid_to_hex(oid), path);
333
0
  }
334
335
0
  if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
336
0
    method = ZIP_METHOD_STORE;
337
0
    attr2 = 16;
338
0
    out = NULL;
339
0
    compressed_size = 0;
340
0
  } else if (S_ISREG(mode) || S_ISLNK(mode)) {
341
0
    method = ZIP_METHOD_STORE;
342
0
    attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
343
0
      (mode & 0111) ? ((mode) << 16) : 0;
344
0
    if (S_ISLNK(mode) || (mode & 0111))
345
0
      creator_version = 0x0317;
346
0
    if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
347
0
      method = ZIP_METHOD_DEFLATE;
348
349
0
    if (!buffer) {
350
0
      enum object_type type;
351
0
      stream = open_istream(args->repo, oid, &type, &size,
352
0
                NULL);
353
0
      if (!stream)
354
0
        return error(_("cannot stream blob %s"),
355
0
               oid_to_hex(oid));
356
0
      flags |= ZIP_STREAM;
357
0
      out = NULL;
358
0
    } else {
359
0
      crc = crc32(crc, buffer, size);
360
0
      is_binary = entry_is_binary(args->repo->index,
361
0
                path_without_prefix,
362
0
                buffer, size);
363
0
      out = buffer;
364
0
    }
365
0
    compressed_size = (method == ZIP_METHOD_STORE) ? size : 0;
366
0
  } else {
367
0
    return error(_("unsupported file mode: 0%o (SHA1: %s)"), mode,
368
0
        oid_to_hex(oid));
369
0
  }
370
371
0
  if (creator_version > max_creator_version)
372
0
    max_creator_version = creator_version;
373
374
0
  if (buffer && method == ZIP_METHOD_DEFLATE) {
375
0
    out = deflated = zlib_deflate_raw(buffer, size,
376
0
              args->compression_level,
377
0
              &compressed_size);
378
0
    if (!out || compressed_size >= size) {
379
0
      out = buffer;
380
0
      method = ZIP_METHOD_STORE;
381
0
      compressed_size = size;
382
0
    }
383
0
  }
384
385
0
  copy_le16(extra.magic, 0x5455);
386
0
  copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
387
0
  extra.flags[0] = 1; /* just mtime */
388
0
  copy_le32(extra.mtime, args->time);
389
390
0
  if (size > 0xffffffff || compressed_size > 0xffffffff)
391
0
    need_zip64_extra = 1;
392
0
  if (stream && size > 0x7fffffff)
393
0
    need_zip64_extra = 1;
394
395
0
  if (need_zip64_extra)
396
0
    version_needed = 45;
397
398
0
  copy_le32(header.magic, 0x04034b50);
399
0
  copy_le16(header.version, version_needed);
400
0
  copy_le16(header.flags, flags);
401
0
  copy_le16(header.compression_method, method);
402
0
  copy_le16(header.mtime, zip_time);
403
0
  copy_le16(header.mdate, zip_date);
404
0
  if (need_zip64_extra) {
405
0
    set_zip_header_data_desc(&header, 0xffffffff, 0xffffffff, crc);
406
0
    header_extra_size += ZIP64_EXTRA_SIZE;
407
0
  } else {
408
0
    set_zip_header_data_desc(&header, size, compressed_size, crc);
409
0
  }
410
0
  copy_le16(header.filename_length, pathlen);
411
0
  copy_le16(header.extra_length, header_extra_size);
412
0
  write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
413
0
  zip_offset += ZIP_LOCAL_HEADER_SIZE;
414
0
  write_or_die(1, path, pathlen);
415
0
  zip_offset += pathlen;
416
0
  write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
417
0
  zip_offset += ZIP_EXTRA_MTIME_SIZE;
418
0
  if (need_zip64_extra) {
419
0
    copy_le16(extra64.magic, 0x0001);
420
0
    copy_le16(extra64.extra_size, ZIP64_EXTRA_PAYLOAD_SIZE);
421
0
    copy_le64(extra64.size, size);
422
0
    copy_le64(extra64.compressed_size, compressed_size);
423
0
    write_or_die(1, &extra64, ZIP64_EXTRA_SIZE);
424
0
    zip_offset += ZIP64_EXTRA_SIZE;
425
0
  }
426
427
0
  if (stream && method == ZIP_METHOD_STORE) {
428
0
    unsigned char buf[STREAM_BUFFER_SIZE];
429
0
    ssize_t readlen;
430
431
0
    for (;;) {
432
0
      readlen = read_istream(stream, buf, sizeof(buf));
433
0
      if (readlen <= 0)
434
0
        break;
435
0
      crc = crc32(crc, buf, readlen);
436
0
      if (is_binary == -1)
437
0
        is_binary = entry_is_binary(args->repo->index,
438
0
                  path_without_prefix,
439
0
                  buf, readlen);
440
0
      write_or_die(1, buf, readlen);
441
0
    }
442
0
    close_istream(stream);
443
0
    if (readlen)
444
0
      return readlen;
445
446
0
    compressed_size = size;
447
0
    zip_offset += compressed_size;
448
449
0
    write_zip_data_desc(size, compressed_size, crc);
450
0
  } else if (stream && method == ZIP_METHOD_DEFLATE) {
451
0
    unsigned char buf[STREAM_BUFFER_SIZE];
452
0
    ssize_t readlen;
453
0
    git_zstream zstream;
454
0
    int result;
455
0
    size_t out_len;
456
0
    unsigned char compressed[STREAM_BUFFER_SIZE * 2];
457
458
0
    git_deflate_init_raw(&zstream, args->compression_level);
459
460
0
    compressed_size = 0;
461
0
    zstream.next_out = compressed;
462
0
    zstream.avail_out = sizeof(compressed);
463
464
0
    for (;;) {
465
0
      readlen = read_istream(stream, buf, sizeof(buf));
466
0
      if (readlen <= 0)
467
0
        break;
468
0
      crc = crc32(crc, buf, readlen);
469
0
      if (is_binary == -1)
470
0
        is_binary = entry_is_binary(args->repo->index,
471
0
                  path_without_prefix,
472
0
                  buf, readlen);
473
474
0
      zstream.next_in = buf;
475
0
      zstream.avail_in = readlen;
476
0
      result = git_deflate(&zstream, 0);
477
0
      if (result != Z_OK)
478
0
        die(_("deflate error (%d)"), result);
479
0
      out_len = zstream.next_out - compressed;
480
481
0
      if (out_len > 0) {
482
0
        write_or_die(1, compressed, out_len);
483
0
        compressed_size += out_len;
484
0
        zstream.next_out = compressed;
485
0
        zstream.avail_out = sizeof(compressed);
486
0
      }
487
488
0
    }
489
0
    close_istream(stream);
490
0
    if (readlen)
491
0
      return readlen;
492
493
0
    zstream.next_in = buf;
494
0
    zstream.avail_in = 0;
495
0
    result = git_deflate(&zstream, Z_FINISH);
496
0
    if (result != Z_STREAM_END)
497
0
      die("deflate error (%d)", result);
498
499
0
    git_deflate_end(&zstream);
500
0
    out_len = zstream.next_out - compressed;
501
0
    write_or_die(1, compressed, out_len);
502
0
    compressed_size += out_len;
503
0
    zip_offset += compressed_size;
504
505
0
    write_zip_data_desc(size, compressed_size, crc);
506
0
  } else if (compressed_size > 0) {
507
0
    write_or_die(1, out, compressed_size);
508
0
    zip_offset += compressed_size;
509
0
  }
510
511
0
  free(deflated);
512
513
0
  if (compressed_size > 0xffffffff || size > 0xffffffff ||
514
0
      offset > 0xffffffff) {
515
0
    if (compressed_size >= 0xffffffff)
516
0
      zip64_dir_extra_payload_size += 8;
517
0
    if (size >= 0xffffffff)
518
0
      zip64_dir_extra_payload_size += 8;
519
0
    if (offset >= 0xffffffff)
520
0
      zip64_dir_extra_payload_size += 8;
521
0
    zip_dir_extra_size += 2 + 2 + zip64_dir_extra_payload_size;
522
0
  }
523
524
0
  strbuf_add_le(&zip_dir, 4, 0x02014b50); /* magic */
525
0
  strbuf_add_le(&zip_dir, 2, creator_version);
526
0
  strbuf_add_le(&zip_dir, 2, version_needed);
527
0
  strbuf_add_le(&zip_dir, 2, flags);
528
0
  strbuf_add_le(&zip_dir, 2, method);
529
0
  strbuf_add_le(&zip_dir, 2, zip_time);
530
0
  strbuf_add_le(&zip_dir, 2, zip_date);
531
0
  strbuf_add_le(&zip_dir, 4, crc);
532
0
  strbuf_add_le(&zip_dir, 4, clamp32(compressed_size));
533
0
  strbuf_add_le(&zip_dir, 4, clamp32(size));
534
0
  strbuf_add_le(&zip_dir, 2, pathlen);
535
0
  strbuf_add_le(&zip_dir, 2, zip_dir_extra_size);
536
0
  strbuf_add_le(&zip_dir, 2, 0);    /* comment length */
537
0
  strbuf_add_le(&zip_dir, 2, 0);    /* disk */
538
0
  strbuf_add_le(&zip_dir, 2, !is_binary);
539
0
  strbuf_add_le(&zip_dir, 4, attr2);
540
0
  strbuf_add_le(&zip_dir, 4, clamp32(offset));
541
0
  strbuf_add(&zip_dir, path, pathlen);
542
0
  strbuf_add(&zip_dir, &extra, ZIP_EXTRA_MTIME_SIZE);
543
0
  if (zip64_dir_extra_payload_size) {
544
0
    strbuf_add_le(&zip_dir, 2, 0x0001); /* magic */
545
0
    strbuf_add_le(&zip_dir, 2, zip64_dir_extra_payload_size);
546
0
    if (size >= 0xffffffff)
547
0
      strbuf_add_le(&zip_dir, 8, size);
548
0
    if (compressed_size >= 0xffffffff)
549
0
      strbuf_add_le(&zip_dir, 8, compressed_size);
550
0
    if (offset >= 0xffffffff)
551
0
      strbuf_add_le(&zip_dir, 8, offset);
552
0
  }
553
0
  zip_dir_entries++;
554
555
0
  return 0;
556
0
}
557
558
static void write_zip64_trailer(void)
559
0
{
560
0
  struct zip64_dir_trailer trailer64;
561
0
  struct zip64_dir_trailer_locator locator64;
562
563
0
  copy_le32(trailer64.magic, 0x06064b50);
564
0
  copy_le64(trailer64.record_size, ZIP64_DIR_TRAILER_RECORD_SIZE);
565
0
  copy_le16(trailer64.creator_version, max_creator_version);
566
0
  copy_le16(trailer64.version, 45);
567
0
  copy_le32(trailer64.disk, 0);
568
0
  copy_le32(trailer64.directory_start_disk, 0);
569
0
  copy_le64(trailer64.entries_on_this_disk, zip_dir_entries);
570
0
  copy_le64(trailer64.entries, zip_dir_entries);
571
0
  copy_le64(trailer64.size, zip_dir.len);
572
0
  copy_le64(trailer64.offset, zip_offset);
573
574
0
  copy_le32(locator64.magic, 0x07064b50);
575
0
  copy_le32(locator64.disk, 0);
576
0
  copy_le64(locator64.offset, zip_offset + zip_dir.len);
577
0
  copy_le32(locator64.number_of_disks, 1);
578
579
0
  write_or_die(1, &trailer64, ZIP64_DIR_TRAILER_SIZE);
580
0
  write_or_die(1, &locator64, ZIP64_DIR_TRAILER_LOCATOR_SIZE);
581
0
}
582
583
static void write_zip_trailer(const struct object_id *oid)
584
0
{
585
0
  struct zip_dir_trailer trailer;
586
0
  int clamped = 0;
587
588
0
  copy_le32(trailer.magic, 0x06054b50);
589
0
  copy_le16(trailer.disk, 0);
590
0
  copy_le16(trailer.directory_start_disk, 0);
591
0
  copy_le16_clamp(trailer.entries_on_this_disk, zip_dir_entries,
592
0
      &clamped);
593
0
  copy_le16_clamp(trailer.entries, zip_dir_entries, &clamped);
594
0
  copy_le32(trailer.size, zip_dir.len);
595
0
  copy_le32_clamp(trailer.offset, zip_offset, &clamped);
596
0
  copy_le16(trailer.comment_length, oid ? the_hash_algo->hexsz : 0);
597
598
0
  write_or_die(1, zip_dir.buf, zip_dir.len);
599
0
  if (clamped)
600
0
    write_zip64_trailer();
601
0
  write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
602
0
  if (oid)
603
0
    write_or_die(1, oid_to_hex(oid), the_hash_algo->hexsz);
604
0
}
605
606
static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
607
0
{
608
0
  time_t time;
609
0
  struct tm tm;
610
611
0
  if (date_overflows(*timestamp))
612
0
    die(_("timestamp too large for this system: %"PRItime),
613
0
        *timestamp);
614
0
  time = (time_t)*timestamp;
615
0
  localtime_r(&time, &tm);
616
0
  *timestamp = time;
617
618
0
  *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 +
619
0
        (tm.tm_year + 1900 - 1980) * 512;
620
0
  *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048;
621
0
}
622
623
static int archive_zip_config(const char *var, const char *value,
624
            const struct config_context *ctx UNUSED,
625
            void *data UNUSED)
626
0
{
627
0
  return userdiff_config(var, value);
628
0
}
629
630
static int write_zip_archive(const struct archiver *ar UNUSED,
631
           struct archiver_args *args)
632
0
{
633
0
  int err;
634
635
0
  git_config(archive_zip_config, NULL);
636
637
0
  dos_time(&args->time, &zip_date, &zip_time);
638
639
0
  strbuf_init(&zip_dir, 0);
640
641
0
  err = write_archive_entries(args, write_zip_entry);
642
0
  if (!err)
643
0
    write_zip_trailer(args->commit_oid);
644
645
0
  strbuf_release(&zip_dir);
646
647
0
  return err;
648
0
}
649
650
static struct archiver zip_archiver = {
651
  .name = "zip",
652
  .write_archive = write_zip_archive,
653
  .flags = ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE,
654
};
655
656
void init_zip_archiver(void)
657
0
{
658
0
  register_archiver(&zip_archiver);
659
0
}