archive-zip.con commit archive: make zip compression level independent from core git (3a176c6)
   1/*
   2 * Copyright (c) 2006 Rene Scharfe
   3 */
   4#include "cache.h"
   5#include "commit.h"
   6#include "blob.h"
   7#include "tree.h"
   8#include "quote.h"
   9#include "builtin.h"
  10#include "archive.h"
  11
  12static int zip_date;
  13static int zip_time;
  14
  15static unsigned char *zip_dir;
  16static unsigned int zip_dir_size;
  17
  18static unsigned int zip_offset;
  19static unsigned int zip_dir_offset;
  20static unsigned int zip_dir_entries;
  21
  22#define ZIP_DIRECTORY_MIN_SIZE  (1024 * 1024)
  23
  24struct zip_local_header {
  25        unsigned char magic[4];
  26        unsigned char version[2];
  27        unsigned char flags[2];
  28        unsigned char compression_method[2];
  29        unsigned char mtime[2];
  30        unsigned char mdate[2];
  31        unsigned char crc32[4];
  32        unsigned char compressed_size[4];
  33        unsigned char size[4];
  34        unsigned char filename_length[2];
  35        unsigned char extra_length[2];
  36        unsigned char _end[1];
  37};
  38
  39struct zip_dir_header {
  40        unsigned char magic[4];
  41        unsigned char creator_version[2];
  42        unsigned char version[2];
  43        unsigned char flags[2];
  44        unsigned char compression_method[2];
  45        unsigned char mtime[2];
  46        unsigned char mdate[2];
  47        unsigned char crc32[4];
  48        unsigned char compressed_size[4];
  49        unsigned char size[4];
  50        unsigned char filename_length[2];
  51        unsigned char extra_length[2];
  52        unsigned char comment_length[2];
  53        unsigned char disk[2];
  54        unsigned char attr1[2];
  55        unsigned char attr2[4];
  56        unsigned char offset[4];
  57        unsigned char _end[1];
  58};
  59
  60struct zip_dir_trailer {
  61        unsigned char magic[4];
  62        unsigned char disk[2];
  63        unsigned char directory_start_disk[2];
  64        unsigned char entries_on_this_disk[2];
  65        unsigned char entries[2];
  66        unsigned char size[4];
  67        unsigned char offset[4];
  68        unsigned char comment_length[2];
  69        unsigned char _end[1];
  70};
  71
  72/*
  73 * On ARM, padding is added at the end of the struct, so a simple
  74 * sizeof(struct ...) reports two bytes more than the payload size
  75 * we're interested in.
  76 */
  77#define ZIP_LOCAL_HEADER_SIZE   offsetof(struct zip_local_header, _end)
  78#define ZIP_DIR_HEADER_SIZE     offsetof(struct zip_dir_header, _end)
  79#define ZIP_DIR_TRAILER_SIZE    offsetof(struct zip_dir_trailer, _end)
  80
  81static void copy_le16(unsigned char *dest, unsigned int n)
  82{
  83        dest[0] = 0xff & n;
  84        dest[1] = 0xff & (n >> 010);
  85}
  86
  87static void copy_le32(unsigned char *dest, unsigned int n)
  88{
  89        dest[0] = 0xff & n;
  90        dest[1] = 0xff & (n >> 010);
  91        dest[2] = 0xff & (n >> 020);
  92        dest[3] = 0xff & (n >> 030);
  93}
  94
  95static void *zlib_deflate(void *data, unsigned long size,
  96                int compression_level, unsigned long *compressed_size)
  97{
  98        z_stream stream;
  99        unsigned long maxsize;
 100        void *buffer;
 101        int result;
 102
 103        memset(&stream, 0, sizeof(stream));
 104        deflateInit(&stream, compression_level);
 105        maxsize = deflateBound(&stream, size);
 106        buffer = xmalloc(maxsize);
 107
 108        stream.next_in = data;
 109        stream.avail_in = size;
 110        stream.next_out = buffer;
 111        stream.avail_out = maxsize;
 112
 113        do {
 114                result = deflate(&stream, Z_FINISH);
 115        } while (result == Z_OK);
 116
 117        if (result != Z_STREAM_END) {
 118                free(buffer);
 119                return NULL;
 120        }
 121
 122        deflateEnd(&stream);
 123        *compressed_size = stream.total_out;
 124
 125        return buffer;
 126}
 127
 128static int write_zip_entry(struct archiver_args *args,
 129                const unsigned char *sha1, const char *path, size_t pathlen,
 130                unsigned int mode, void *buffer, unsigned long size)
 131{
 132        struct zip_local_header header;
 133        struct zip_dir_header dirent;
 134        unsigned long attr2;
 135        unsigned long compressed_size;
 136        unsigned long uncompressed_size;
 137        unsigned long crc;
 138        unsigned long direntsize;
 139        int method;
 140        unsigned char *out;
 141        void *deflated = NULL;
 142
 143        crc = crc32(0, NULL, 0);
 144
 145        if (pathlen > 0xffff) {
 146                return error("path too long (%d chars, SHA1: %s): %s",
 147                                (int)pathlen, sha1_to_hex(sha1), path);
 148        }
 149
 150        if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
 151                method = 0;
 152                attr2 = 16;
 153                out = NULL;
 154                uncompressed_size = 0;
 155                compressed_size = 0;
 156        } else if (S_ISREG(mode) || S_ISLNK(mode)) {
 157                method = 0;
 158                attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
 159                        (mode & 0111) ? ((mode) << 16) : 0;
 160                if (S_ISREG(mode) && args->compression_level != 0)
 161                        method = 8;
 162                crc = crc32(crc, buffer, size);
 163                out = buffer;
 164                uncompressed_size = size;
 165                compressed_size = size;
 166        } else {
 167                return error("unsupported file mode: 0%o (SHA1: %s)", mode,
 168                                sha1_to_hex(sha1));
 169        }
 170
 171        if (method == 8) {
 172                deflated = zlib_deflate(buffer, size, args->compression_level,
 173                                &compressed_size);
 174                if (deflated && compressed_size - 6 < size) {
 175                        /* ZLIB --> raw compressed data (see RFC 1950) */
 176                        /* CMF and FLG ... */
 177                        out = (unsigned char *)deflated + 2;
 178                        compressed_size -= 6;   /* ... and ADLER32 */
 179                } else {
 180                        method = 0;
 181                        compressed_size = size;
 182                }
 183        }
 184
 185        /* make sure we have enough free space in the dictionary */
 186        direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
 187        while (zip_dir_size < zip_dir_offset + direntsize) {
 188                zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
 189                zip_dir = xrealloc(zip_dir, zip_dir_size);
 190        }
 191
 192        copy_le32(dirent.magic, 0x02014b50);
 193        copy_le16(dirent.creator_version,
 194                S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
 195        copy_le16(dirent.version, 10);
 196        copy_le16(dirent.flags, 0);
 197        copy_le16(dirent.compression_method, method);
 198        copy_le16(dirent.mtime, zip_time);
 199        copy_le16(dirent.mdate, zip_date);
 200        copy_le32(dirent.crc32, crc);
 201        copy_le32(dirent.compressed_size, compressed_size);
 202        copy_le32(dirent.size, uncompressed_size);
 203        copy_le16(dirent.filename_length, pathlen);
 204        copy_le16(dirent.extra_length, 0);
 205        copy_le16(dirent.comment_length, 0);
 206        copy_le16(dirent.disk, 0);
 207        copy_le16(dirent.attr1, 0);
 208        copy_le32(dirent.attr2, attr2);
 209        copy_le32(dirent.offset, zip_offset);
 210        memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
 211        zip_dir_offset += ZIP_DIR_HEADER_SIZE;
 212        memcpy(zip_dir + zip_dir_offset, path, pathlen);
 213        zip_dir_offset += pathlen;
 214        zip_dir_entries++;
 215
 216        copy_le32(header.magic, 0x04034b50);
 217        copy_le16(header.version, 10);
 218        copy_le16(header.flags, 0);
 219        copy_le16(header.compression_method, method);
 220        copy_le16(header.mtime, zip_time);
 221        copy_le16(header.mdate, zip_date);
 222        copy_le32(header.crc32, crc);
 223        copy_le32(header.compressed_size, compressed_size);
 224        copy_le32(header.size, uncompressed_size);
 225        copy_le16(header.filename_length, pathlen);
 226        copy_le16(header.extra_length, 0);
 227        write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
 228        zip_offset += ZIP_LOCAL_HEADER_SIZE;
 229        write_or_die(1, path, pathlen);
 230        zip_offset += pathlen;
 231        if (compressed_size > 0) {
 232                write_or_die(1, out, compressed_size);
 233                zip_offset += compressed_size;
 234        }
 235
 236        free(deflated);
 237
 238        return 0;
 239}
 240
 241static void write_zip_trailer(const unsigned char *sha1)
 242{
 243        struct zip_dir_trailer trailer;
 244
 245        copy_le32(trailer.magic, 0x06054b50);
 246        copy_le16(trailer.disk, 0);
 247        copy_le16(trailer.directory_start_disk, 0);
 248        copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
 249        copy_le16(trailer.entries, zip_dir_entries);
 250        copy_le32(trailer.size, zip_dir_offset);
 251        copy_le32(trailer.offset, zip_offset);
 252        copy_le16(trailer.comment_length, sha1 ? 40 : 0);
 253
 254        write_or_die(1, zip_dir, zip_dir_offset);
 255        write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
 256        if (sha1)
 257                write_or_die(1, sha1_to_hex(sha1), 40);
 258}
 259
 260static void dos_time(time_t *time, int *dos_date, int *dos_time)
 261{
 262        struct tm *t = localtime(time);
 263
 264        *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
 265                    (t->tm_year + 1900 - 1980) * 512;
 266        *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
 267}
 268
 269int write_zip_archive(struct archiver_args *args)
 270{
 271        int err;
 272
 273        dos_time(&args->time, &zip_date, &zip_time);
 274
 275        zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
 276        zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
 277
 278        err = write_archive_entries(args, write_zip_entry);
 279        if (!err)
 280                write_zip_trailer(args->commit_sha1);
 281
 282        free(zip_dir);
 283
 284        return err;
 285}