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