2006-04-12 01:59:08 +02:00
|
|
|
/**
|
|
|
|
* =========================================================================
|
|
|
|
* File : zip.cpp
|
|
|
|
* Project : 0 A.D.
|
|
|
|
* Description : archive backend for Zip files.
|
|
|
|
*
|
|
|
|
* @author Jan.Wassenberg@stud.uni-karlsruhe.de
|
|
|
|
* =========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2003-2006 Jan Wassenberg
|
|
|
|
*
|
|
|
|
* Redistribution and/or modification are also permitted under the
|
|
|
|
* terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation (version 2 or later, at your option).
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "zip.h"
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
#include <time.h>
|
2006-04-19 07:30:02 +02:00
|
|
|
#include <limits>
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-06-02 04:10:27 +02:00
|
|
|
#include "lib/lib.h"
|
|
|
|
#include "lib/byte_order.h"
|
|
|
|
#include "lib/allocators.h"
|
|
|
|
#include "lib/timer.h"
|
2006-09-22 15:19:40 +02:00
|
|
|
#include "lib/res/res.h"
|
2007-01-01 22:25:47 +01:00
|
|
|
#include "file_internal.h"
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
// safe downcasters: cast from any integral type to u32 or u16;
|
|
|
|
// issues warning if larger than would fit in the target type.
|
|
|
|
//
|
|
|
|
// these are generally useful but included here (instead of e.g. lib.h) for
|
|
|
|
// several reasons:
|
|
|
|
// - including implementation in lib.h doesn't work because the definition
|
|
|
|
// of debug_assert in turn requires lib.h's STMT.
|
|
|
|
// - separate compilation of templates via export isn't supported by
|
|
|
|
// most compilers.
|
|
|
|
|
|
|
|
template<typename T> u32 u32_from_larger(T x)
|
|
|
|
{
|
2006-04-20 06:25:14 +02:00
|
|
|
const u32 max = std::numeric_limits<u32>::max();
|
|
|
|
debug_assert((u64)x <= (u64)max);
|
2006-03-15 06:18:32 +01:00
|
|
|
return (u32)(x & max);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T> u16 u16_from_larger(T x)
|
|
|
|
{
|
2006-04-20 06:25:14 +02:00
|
|
|
const u16 max = std::numeric_limits<u16>::max();
|
|
|
|
debug_assert((u64)x <= (u64)max);
|
2006-03-15 06:18:32 +01:00
|
|
|
return (u16)(x & max);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// timestamp conversion: DOS FAT <-> Unix time_t
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2006-06-08 21:03:43 +02:00
|
|
|
// must not be static because these are tested by unit test
|
|
|
|
|
|
|
|
time_t time_t_from_FAT(u32 fat_timedate)
|
2006-03-15 06:18:32 +01:00
|
|
|
{
|
|
|
|
const uint fat_time = bits(fat_timedate, 0, 15);
|
|
|
|
const uint fat_date = bits(fat_timedate, 16, 31);
|
|
|
|
|
|
|
|
struct tm t; // struct tm format:
|
|
|
|
t.tm_sec = bits(fat_time, 0,4) * 2; // [0,59]
|
|
|
|
t.tm_min = bits(fat_time, 5,10); // [0,59]
|
|
|
|
t.tm_hour = bits(fat_time, 11,15); // [0,23]
|
|
|
|
t.tm_mday = bits(fat_date, 0,4); // [1,31]
|
|
|
|
t.tm_mon = bits(fat_date, 5,8) - 1; // [0,11]
|
|
|
|
t.tm_year = bits(fat_date, 9,15) + 80; // since 1900
|
|
|
|
t.tm_isdst = -1; // unknown - let libc determine
|
|
|
|
|
|
|
|
// otherwise: totally bogus, and at the limit of 32-bit time_t
|
|
|
|
debug_assert(t.tm_year < 138);
|
|
|
|
|
|
|
|
time_t ret = mktime(&t);
|
|
|
|
if(ret == (time_t)-1)
|
|
|
|
debug_warn("mktime failed");
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-08 21:03:43 +02:00
|
|
|
u32 FAT_from_time_t(time_t time)
|
2006-03-15 06:18:32 +01:00
|
|
|
{
|
|
|
|
// (values are adjusted for DST)
|
|
|
|
struct tm* t = localtime(&time);
|
|
|
|
|
|
|
|
u16 fat_time = 0;
|
|
|
|
fat_time |= (t->tm_sec/2); // 5
|
|
|
|
fat_time |= (t->tm_min) << 5; // 6
|
|
|
|
fat_time |= (t->tm_hour) << 11; // 5
|
|
|
|
|
|
|
|
u16 fat_date = 0;
|
|
|
|
fat_date |= (t->tm_mday); // 5
|
|
|
|
fat_date |= (t->tm_mon+1) << 5; // 4
|
|
|
|
fat_date |= (t->tm_year-80) << 9; // 7
|
|
|
|
|
|
|
|
u32 fat_timedate = u32_from_u16(fat_date, fat_time);
|
|
|
|
return fat_timedate;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2004-08-10 17:56:04 +02:00
|
|
|
// Zip file data structures and signatures
|
2006-03-15 06:18:32 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
enum ZipCompressionMethod
|
|
|
|
{
|
2006-01-23 21:05:09 +01:00
|
|
|
ZIP_CM_NONE = 0,
|
|
|
|
ZIP_CM_DEFLATE = 8
|
2005-12-28 21:29:22 +01:00
|
|
|
};
|
|
|
|
|
2006-04-20 06:25:14 +02:00
|
|
|
// translate ArchiveEntry.method to zip_method.
|
2006-03-15 06:18:32 +01:00
|
|
|
static ZipCompressionMethod zip_method_for(CompressionMethod method)
|
|
|
|
{
|
|
|
|
switch(method)
|
|
|
|
{
|
|
|
|
case CM_NONE:
|
|
|
|
return ZIP_CM_NONE;
|
|
|
|
case CM_DEFLATE:
|
|
|
|
return ZIP_CM_DEFLATE;
|
|
|
|
default:
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_ERR(ERR::COMPRESSION_UNKNOWN_METHOD);
|
2006-03-15 06:18:32 +01:00
|
|
|
return ZIP_CM_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate to (not Zip-specific) CompressionMethod for use in ArchiveEntry.
|
|
|
|
static CompressionMethod method_for_zip_method(ZipCompressionMethod zip_method)
|
|
|
|
{
|
|
|
|
switch(zip_method)
|
|
|
|
{
|
|
|
|
case ZIP_CM_NONE:
|
|
|
|
return CM_NONE;
|
|
|
|
case ZIP_CM_DEFLATE:
|
|
|
|
return CM_DEFLATE;
|
|
|
|
default:
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_ERR(ERR::COMPRESSION_UNKNOWN_METHOD);
|
2006-03-15 06:18:32 +01:00
|
|
|
return CM_UNSUPPORTED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static const u32 cdfh_magic = FOURCC_LE('P','K','\1','\2');
|
|
|
|
static const u32 lfh_magic = FOURCC_LE('P','K','\3','\4');
|
|
|
|
static const u32 ecdr_magic = FOURCC_LE('P','K','\5','\6');
|
|
|
|
|
2006-01-23 21:05:09 +01:00
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
struct LFH
|
|
|
|
{
|
|
|
|
u32 magic;
|
|
|
|
u16 x1; // version needed
|
|
|
|
u16 flags;
|
|
|
|
u16 method;
|
2006-01-23 21:05:09 +01:00
|
|
|
u32 fat_mtime; // last modified time (DOS FAT format)
|
2005-12-28 21:29:22 +01:00
|
|
|
u32 crc;
|
|
|
|
u32 csize;
|
|
|
|
u32 ucsize;
|
|
|
|
u16 fn_len;
|
2006-01-23 21:05:09 +01:00
|
|
|
u16 e_len;
|
2005-12-28 21:29:22 +01:00
|
|
|
};
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
const size_t LFH_SIZE = sizeof(LFH);
|
|
|
|
cassert(LFH_SIZE == 30);
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
// convenience (allows writing out LFH and fn in 1 IO).
|
|
|
|
// must be declared here to avoid any struct padding.
|
|
|
|
struct LFH_Package
|
|
|
|
{
|
|
|
|
LFH lfh;
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
};
|
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
|
|
|
|
struct CDFH
|
|
|
|
{
|
|
|
|
u32 magic;
|
|
|
|
u32 x1; // versions
|
|
|
|
u16 flags;
|
|
|
|
u16 method;
|
2006-01-23 21:05:09 +01:00
|
|
|
u32 fat_mtime; // last modified time (DOS FAT format)
|
2005-12-28 21:29:22 +01:00
|
|
|
u32 crc;
|
|
|
|
u32 csize;
|
|
|
|
u32 ucsize;
|
|
|
|
u16 fn_len;
|
|
|
|
u16 e_len;
|
|
|
|
u16 c_len;
|
|
|
|
u32 x2; // spanning
|
|
|
|
u32 x3; // attributes
|
|
|
|
u32 lfh_ofs;
|
|
|
|
};
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
const size_t CDFH_SIZE = sizeof(CDFH);
|
|
|
|
cassert(CDFH_SIZE == 46);
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
// convenience (avoids need for pointer arithmetic)
|
|
|
|
// must be declared here to avoid any struct padding.
|
|
|
|
struct CDFH_Package
|
|
|
|
{
|
|
|
|
CDFH cdfh;
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
};
|
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
|
|
|
|
struct ECDR
|
|
|
|
{
|
|
|
|
u32 magic;
|
|
|
|
u8 x1[6]; // multiple-disk support
|
|
|
|
u16 cd_entries;
|
|
|
|
u32 cd_size;
|
|
|
|
u32 cd_ofs;
|
|
|
|
u16 comment_len;
|
|
|
|
};
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
const size_t ECDR_SIZE = sizeof(ECDR);
|
|
|
|
cassert(ECDR_SIZE == 22);
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-01-23 21:05:09 +01:00
|
|
|
#pragma pack(pop)
|
2005-12-28 21:29:22 +01:00
|
|
|
|
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
static off_t lfh_total_size(const LFH* lfh_le)
|
|
|
|
{
|
|
|
|
debug_assert(lfh_le->magic == lfh_magic);
|
|
|
|
const size_t fn_len = read_le16(&lfh_le->fn_len);
|
|
|
|
const size_t e_len = read_le16(&lfh_le->e_len);
|
|
|
|
// note: LFH doesn't have a comment field!
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
return (off_t)(LFH_SIZE + fn_len + e_len);
|
|
|
|
}
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
static void lfh_assemble(LFH* lfh_le,
|
|
|
|
CompressionMethod method, time_t mtime, u32 crc,
|
|
|
|
off_t csize, off_t ucsize, size_t fn_len)
|
2006-01-23 21:05:09 +01:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
const ZipCompressionMethod zip_method = zip_method_for(method);
|
|
|
|
const u32 fat_mtime = FAT_from_time_t(mtime);
|
|
|
|
|
|
|
|
lfh_le->magic = lfh_magic;
|
|
|
|
lfh_le->x1 = to_le16(0);
|
|
|
|
lfh_le->flags = to_le16(0);
|
|
|
|
lfh_le->method = to_le16(zip_method);
|
|
|
|
lfh_le->fat_mtime = to_le32(fat_mtime);
|
|
|
|
lfh_le->crc = to_le32(crc);
|
|
|
|
lfh_le->csize = to_le32(u32_from_larger(csize));
|
|
|
|
lfh_le->ucsize = to_le32(u32_from_larger(ucsize));
|
|
|
|
lfh_le->fn_len = to_le16(u16_from_larger(fn_len));
|
|
|
|
lfh_le->e_len = to_le16(0);
|
|
|
|
}
|
2005-12-28 21:29:22 +01:00
|
|
|
|
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
static void cdfh_decompose(const CDFH* cdfh_le,
|
|
|
|
CompressionMethod& method, time_t& mtime, off_t& csize, off_t& ucsize,
|
|
|
|
const char*& fn, off_t& lfh_ofs, size_t& total_size)
|
|
|
|
{
|
|
|
|
const u16 zip_method = read_le16(&cdfh_le->method);
|
|
|
|
const u32 fat_mtime = read_le32(&cdfh_le->fat_mtime);
|
|
|
|
csize = (off_t)read_le32(&cdfh_le->csize);
|
|
|
|
ucsize = (off_t)read_le32(&cdfh_le->ucsize);
|
|
|
|
const u16 fn_len = read_le16(&cdfh_le->fn_len);
|
|
|
|
const u16 e_len = read_le16(&cdfh_le->e_len);
|
|
|
|
const u16 c_len = read_le16(&cdfh_le->c_len);
|
|
|
|
lfh_ofs = (off_t)read_le32(&cdfh_le->lfh_ofs);
|
|
|
|
|
|
|
|
method = method_for_zip_method((ZipCompressionMethod)zip_method);
|
|
|
|
mtime = time_t_from_FAT(fat_mtime);
|
|
|
|
|
|
|
|
// return 0-terminated copy of filename
|
|
|
|
const char* fn_src = (const char*)cdfh_le+CDFH_SIZE; // not 0-terminated!
|
|
|
|
char fn_buf[PATH_MAX];
|
|
|
|
memcpy2(fn_buf, fn_src, fn_len*sizeof(char));
|
|
|
|
fn_buf[fn_len] = '\0';
|
|
|
|
fn = file_make_unique_fn_copy(fn_buf);
|
|
|
|
|
|
|
|
total_size = CDFH_SIZE + fn_len + e_len + c_len;
|
2006-01-23 21:05:09 +01:00
|
|
|
}
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
static void cdfh_assemble(CDFH* dst_cdfh_le,
|
|
|
|
CompressionMethod method, time_t mtime, u32 crc,
|
|
|
|
size_t csize, size_t ucsize, size_t fn_len, size_t slack, u32 lfh_ofs)
|
2004-08-10 17:56:04 +02:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
const ZipCompressionMethod zip_method = zip_method_for(method);
|
|
|
|
const u32 fat_mtime = FAT_from_time_t(mtime);
|
|
|
|
|
|
|
|
dst_cdfh_le->magic = cdfh_magic;
|
|
|
|
dst_cdfh_le->x1 = to_le32(0);
|
|
|
|
dst_cdfh_le->flags = to_le16(0);
|
|
|
|
dst_cdfh_le->method = to_le16(zip_method);
|
|
|
|
dst_cdfh_le->fat_mtime = to_le32(fat_mtime);
|
|
|
|
dst_cdfh_le->crc = to_le32(crc);
|
|
|
|
dst_cdfh_le->csize = to_le32(u32_from_larger(csize));
|
|
|
|
dst_cdfh_le->ucsize = to_le32(u32_from_larger(ucsize));
|
|
|
|
dst_cdfh_le->fn_len = to_le16(u16_from_larger(fn_len));
|
|
|
|
dst_cdfh_le->e_len = to_le16(0);
|
|
|
|
dst_cdfh_le->c_len = to_le16(u16_from_larger(slack));
|
|
|
|
dst_cdfh_le->x2 = to_le32(0);
|
|
|
|
dst_cdfh_le->x3 = to_le32(0);
|
|
|
|
dst_cdfh_le->lfh_ofs = to_le32(lfh_ofs);
|
|
|
|
}
|
2006-01-23 21:05:09 +01:00
|
|
|
|
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
static void ecdr_decompose(ECDR* ecdr_le,
|
|
|
|
uint& cd_entries, off_t& cd_ofs, size_t& cd_size)
|
|
|
|
{
|
|
|
|
cd_entries = (uint)read_le16(&ecdr_le->cd_entries);
|
|
|
|
cd_ofs = (off_t)read_le32(&ecdr_le->cd_ofs);
|
|
|
|
cd_size = (size_t)read_le32(&ecdr_le->cd_size);
|
|
|
|
}
|
2004-05-13 15:52:48 +02:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
static void ecdr_assemble(ECDR* dst_ecdr_le, uint cd_entries, off_t cd_ofs, size_t cd_size)
|
|
|
|
{
|
|
|
|
dst_ecdr_le->magic = ecdr_magic;
|
|
|
|
memset(dst_ecdr_le->x1, 0, sizeof(dst_ecdr_le->x1));
|
|
|
|
dst_ecdr_le->cd_entries = to_le16(u16_from_larger(cd_entries));
|
|
|
|
dst_ecdr_le->cd_size = to_le32(u32_from_larger(cd_size));
|
|
|
|
dst_ecdr_le->cd_ofs = to_le32(u32_from_larger(cd_ofs));
|
|
|
|
dst_ecdr_le->comment_len = to_le16(0);
|
2004-05-13 15:52:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2004-12-18 15:45:04 +01:00
|
|
|
// scan for and return a pointer to a Zip record, or 0 if not found.
|
|
|
|
// <start> is the expected position; we scan from there until EOF for
|
2006-02-11 23:49:09 +01:00
|
|
|
// the given ID (fourcc). <record_size> includes ID field) bytes must
|
2004-12-18 15:45:04 +01:00
|
|
|
// remain before EOF - this makes sure the record is completely in the file.
|
|
|
|
// used by z_find_ecdr and z_extract_cdfh.
|
2006-01-23 21:05:09 +01:00
|
|
|
static const u8* za_find_id(const u8* buf, size_t size, const void* start, u32 magic, size_t record_size)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2006-01-23 21:05:09 +01:00
|
|
|
ssize_t bytes_left = (ssize_t)((buf+size) - (u8*)start - record_size);
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2006-01-23 21:05:09 +01:00
|
|
|
const u8* p = (const u8*)start;
|
2004-12-18 15:45:04 +01:00
|
|
|
// don't increment function argument directly,
|
|
|
|
// so we can warn the user if we had to scan.
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-12-18 15:45:04 +01:00
|
|
|
while(bytes_left-- >= 0)
|
2004-05-06 19:14:30 +02:00
|
|
|
{
|
2004-12-18 15:45:04 +01:00
|
|
|
// found it
|
2005-12-28 21:29:22 +01:00
|
|
|
if(*(u32*)p == magic)
|
2004-12-18 15:45:04 +01:00
|
|
|
{
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if(p != start)
|
2005-10-19 08:29:55 +02:00
|
|
|
debug_warn("archive damaged, but still found next record.");
|
2004-12-18 15:45:04 +01:00
|
|
|
#endif
|
|
|
|
return p;
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-12-18 15:45:04 +01:00
|
|
|
p++;
|
|
|
|
// be careful not to increment before comparison;
|
2005-12-28 21:29:22 +01:00
|
|
|
// magic may already be found at <start>.
|
2004-05-18 02:38:39 +02:00
|
|
|
}
|
|
|
|
|
2004-12-18 15:45:04 +01:00
|
|
|
// passed EOF, didn't find it.
|
2006-01-23 21:05:09 +01:00
|
|
|
// note: do not warn - this happens in the initial ECDR search at
|
|
|
|
// EOF if the archive contains a comment field.
|
2004-03-03 00:56:51 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// search for ECDR in the last <max_scan_amount> bytes of the file.
|
2006-03-15 06:18:32 +01:00
|
|
|
// if found, fill <dst_ecdr> with a copy of the (little-endian) ECDR and
|
2006-09-22 15:19:40 +02:00
|
|
|
// return INFO::OK, otherwise IO error or ERR::CORRUPTED.
|
2006-03-15 06:18:32 +01:00
|
|
|
static LibError za_find_ecdr(File* f, size_t max_scan_amount, ECDR* dst_ecdr_le)
|
2004-11-08 00:00:32 +01:00
|
|
|
{
|
2006-02-11 23:49:09 +01:00
|
|
|
// don't scan more than the entire file
|
2006-04-19 17:19:04 +02:00
|
|
|
const size_t file_size = f->size;
|
2006-02-11 23:49:09 +01:00
|
|
|
const size_t scan_amount = MIN(max_scan_amount, file_size);
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// read desired chunk of file into memory
|
2006-01-23 21:05:09 +01:00
|
|
|
const off_t ofs = (off_t)(file_size - scan_amount);
|
|
|
|
FileIOBuf buf = FILE_BUF_ALLOC;
|
2006-02-11 23:49:09 +01:00
|
|
|
ssize_t bytes_read = file_io(f, ofs, scan_amount, &buf);
|
|
|
|
RETURN_ERR(bytes_read);
|
|
|
|
debug_assert(bytes_read == (ssize_t)scan_amount);
|
2004-12-07 02:19:10 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// look for ECDR in buffer
|
2006-09-22 15:19:40 +02:00
|
|
|
LibError ret = ERR::CORRUPTED;
|
2006-01-23 21:05:09 +01:00
|
|
|
const u8* start = (const u8*)buf;
|
2006-03-15 06:18:32 +01:00
|
|
|
const ECDR* ecdr_le = (const ECDR*)za_find_id(start, bytes_read, start, ecdr_magic, ECDR_SIZE);
|
|
|
|
if(ecdr_le)
|
2006-01-23 21:05:09 +01:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
*dst_ecdr_le = *ecdr_le;
|
2006-09-22 15:19:40 +02:00
|
|
|
ret = INFO::OK;
|
2006-01-23 21:05:09 +01:00
|
|
|
}
|
2004-12-18 15:45:04 +01:00
|
|
|
|
2006-01-23 21:05:09 +01:00
|
|
|
file_buf_free(buf);
|
2004-12-07 02:19:10 +01:00
|
|
|
return ret;
|
2004-11-08 00:00:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
static LibError za_find_cd(File* f, uint& cd_entries, off_t& cd_ofs, size_t& cd_size)
|
2005-12-28 21:29:22 +01:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
// sanity check: file size must be > header size.
|
|
|
|
// (this speeds up determining if the file is a Zip file at all)
|
2006-04-19 17:19:04 +02:00
|
|
|
const size_t file_size = f->size;
|
2006-03-15 06:18:32 +01:00
|
|
|
if(file_size < LFH_SIZE+CDFH_SIZE+ECDR_SIZE)
|
2006-01-31 04:47:52 +01:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
completely_bogus:
|
|
|
|
// this file is definitely not a valid Zip file.
|
|
|
|
// note: the VFS blindly opens files when mounting; it needs to open
|
|
|
|
// all archives, but doesn't know their extension (e.g. ".pk3").
|
|
|
|
// therefore, do not warn user.
|
2006-09-22 15:19:40 +02:00
|
|
|
return ERR::RES_UNKNOWN_FORMAT; // NOWARN
|
2006-01-31 04:47:52 +01:00
|
|
|
}
|
2006-02-23 08:55:32 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
ECDR ecdr_le;
|
|
|
|
// expected case: ECDR at EOF; no file comment (=> we only need to
|
|
|
|
// read 512 bytes)
|
|
|
|
LibError ret = za_find_ecdr(f, ECDR_SIZE, &ecdr_le);
|
2006-09-22 15:19:40 +02:00
|
|
|
if(ret == INFO::OK)
|
2006-03-15 06:18:32 +01:00
|
|
|
{
|
|
|
|
have_ecdr:
|
|
|
|
ecdr_decompose(&ecdr_le, cd_entries, cd_ofs, cd_size);
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2006-03-15 06:18:32 +01:00
|
|
|
}
|
|
|
|
// last resort: scan last 66000 bytes of file
|
|
|
|
// (the Zip archive comment field - up to 64k - may follow ECDR).
|
|
|
|
// if the zip file is < 66000 bytes, scan the whole file.
|
|
|
|
ret = za_find_ecdr(f, 66000u, &ecdr_le);
|
2006-09-22 15:19:40 +02:00
|
|
|
if(ret == INFO::OK)
|
2006-03-15 06:18:32 +01:00
|
|
|
goto have_ecdr;
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
// both ECDR scans failed - this is not a valid Zip file.
|
|
|
|
// now see if the beginning of the file holds a valid LFH:
|
|
|
|
const off_t ofs = 0; const size_t scan_amount = LFH_SIZE;
|
|
|
|
FileIOBuf buf = FILE_BUF_ALLOC;
|
|
|
|
ssize_t bytes_read = file_io(f, ofs, scan_amount, &buf);
|
|
|
|
RETURN_ERR(bytes_read);
|
|
|
|
debug_assert(bytes_read == (ssize_t)scan_amount);
|
|
|
|
const bool has_LFH = (za_find_id(buf, scan_amount, buf, lfh_magic, LFH_SIZE) != 0);
|
|
|
|
file_buf_free(buf);
|
|
|
|
if(!has_LFH)
|
|
|
|
goto completely_bogus;
|
|
|
|
// the Zip file is mostly valid but lacking an ECDR. (can happen if
|
|
|
|
// user hard-exits while building an archive)
|
|
|
|
// notes:
|
2006-09-22 15:19:40 +02:00
|
|
|
// - return ERR::CORRUPTED so VFS will not include this file.
|
2006-03-15 06:18:32 +01:00
|
|
|
// - we could work around this by scanning all LFHs, but won't bother
|
|
|
|
// because it'd be slow.
|
|
|
|
// - do not warn - the corrupt archive will be deleted on next
|
|
|
|
// successful archive builder run anyway.
|
2006-09-22 15:19:40 +02:00
|
|
|
return ERR::CORRUPTED; // NOWARN
|
2005-12-28 21:29:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// analyse an opened Zip file; call back into archive.cpp to
|
|
|
|
// populate the Archive object with a list of the files it contains.
|
2006-09-22 15:19:40 +02:00
|
|
|
// returns INFO::OK on success, ERR::CORRUPTED if file is recognizable as
|
|
|
|
// a Zip file but invalid, otherwise ERR::RES_UNKNOWN_FORMAT or IO error.
|
2006-01-28 23:19:42 +01:00
|
|
|
//
|
2006-02-11 23:49:09 +01:00
|
|
|
// fairly slow - must read Central Directory from disk
|
|
|
|
// (size ~= 60 bytes*num_files); observed time ~= 80ms.
|
|
|
|
LibError zip_populate_archive(File* f, Archive* a)
|
2006-01-23 21:05:09 +01:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
uint cd_entries; off_t cd_ofs; size_t cd_size;
|
|
|
|
RETURN_ERR(za_find_cd(f, cd_entries, cd_ofs, cd_size));
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2004-08-24 19:29:54 +02:00
|
|
|
// call back with number of entries in archives (an upper bound
|
|
|
|
// for valid files; we're not interested in the directory entries).
|
|
|
|
// we'd have to scan through the central dir to count them out; we'll
|
|
|
|
// just skip them and waste a bit of preallocated memory.
|
2006-01-23 21:05:09 +01:00
|
|
|
RETURN_ERR(archive_allocate_entries(a, cd_entries));
|
|
|
|
|
|
|
|
FileIOBuf buf = FILE_BUF_ALLOC;
|
|
|
|
RETURN_ERR(file_io(f, cd_ofs, cd_size, &buf));
|
2006-03-15 06:18:32 +01:00
|
|
|
|
|
|
|
// iterate through Central Directory
|
2006-09-22 15:19:40 +02:00
|
|
|
LibError ret = INFO::OK;
|
2006-01-23 21:05:09 +01:00
|
|
|
const CDFH* cdfh = (const CDFH*)buf;
|
|
|
|
size_t ofs_to_next_cdfh = 0;
|
|
|
|
for(uint i = 0; i < cd_entries; i++)
|
2004-12-18 15:45:04 +01:00
|
|
|
{
|
2005-12-28 21:29:22 +01:00
|
|
|
// scan for next CDFH (at or beyond current cdfh position)
|
2006-01-23 21:05:09 +01:00
|
|
|
cdfh = (const CDFH*)((u8*)cdfh + ofs_to_next_cdfh);
|
|
|
|
cdfh = (CDFH*)za_find_id((const u8*)buf, cd_size, (const u8*)cdfh, cdfh_magic, CDFH_SIZE);
|
|
|
|
if(!cdfh) // no (further) CDFH found:
|
2004-08-24 19:29:54 +02:00
|
|
|
{
|
2006-09-22 15:19:40 +02:00
|
|
|
ret = ERR::CORRUPTED;
|
2006-01-23 21:05:09 +01:00
|
|
|
break;
|
2004-08-24 19:29:54 +02:00
|
|
|
}
|
2005-12-28 21:29:22 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
// copy translated fields from CDFH into ArchiveEntry.
|
|
|
|
ArchiveEntry ae;
|
|
|
|
cdfh_decompose(cdfh, ae.method, ae.mtime, ae.csize, ae.ucsize, ae.atom_fn, ae.ofs, ofs_to_next_cdfh);
|
|
|
|
ae.flags = ZIP_LFH_FIXUP_NEEDED;
|
|
|
|
|
|
|
|
// if file (we don't care about directories):
|
|
|
|
if(ae.csize && ae.ucsize)
|
2006-01-23 21:05:09 +01:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
ret = archive_add_file(a, &ae);
|
2006-09-22 15:19:40 +02:00
|
|
|
if(ret != INFO::OK)
|
2006-01-23 21:05:09 +01:00
|
|
|
break;
|
|
|
|
}
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
file_buf_free(buf);
|
2006-01-23 21:05:09 +01:00
|
|
|
return ret;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// this code grabs an LFH struct from file block(s) that are
|
|
|
|
// passed to the callback. usually, one call copies the whole thing,
|
|
|
|
// but the LFH may straddle a block boundary.
|
|
|
|
//
|
|
|
|
// rationale: this allows using temp buffers for zip_fixup_lfh,
|
|
|
|
// which avoids involving the file buffer manager and thus
|
|
|
|
// unclutters the trace and cache contents.
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
struct LFH_Copier
|
|
|
|
{
|
|
|
|
u8* lfh_dst;
|
|
|
|
size_t lfh_bytes_remaining;
|
|
|
|
};
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
static LibError lfh_copier_cb(uintptr_t ctx, const void* block, size_t size, size_t* bytes_processed)
|
|
|
|
{
|
|
|
|
LFH_Copier* p = (LFH_Copier*)ctx;
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
debug_assert(size <= p->lfh_bytes_remaining);
|
|
|
|
memcpy2(p->lfh_dst, block, size);
|
|
|
|
p->lfh_dst += size;
|
|
|
|
p->lfh_bytes_remaining -= size;
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
*bytes_processed = size;
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::CB_CONTINUE;
|
2006-02-11 23:49:09 +01:00
|
|
|
}
|
2006-01-23 21:05:09 +01:00
|
|
|
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// ensures <ent.ofs> points to the actual file contents; it is initially
|
|
|
|
// the offset of the LFH. we cannot use CDFH filename and extra field
|
|
|
|
// lengths to skip past LFH since that may not mirror CDFH (has happened).
|
|
|
|
//
|
|
|
|
// this is called at file-open time instead of while mounting to
|
|
|
|
// reduce seeks: since reading the file will typically follow, the
|
|
|
|
// block cache entirely absorbs the IO cost.
|
|
|
|
void zip_fixup_lfh(File* f, ArchiveEntry* ent)
|
|
|
|
{
|
|
|
|
// already fixed up - done.
|
|
|
|
if(!(ent->flags & ZIP_LFH_FIXUP_NEEDED))
|
|
|
|
return;
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// performance note: this ends up reading one file block, which is
|
|
|
|
// only in the block cache if the file starts in the same block as a
|
|
|
|
// previously read file (i.e. both are small).
|
|
|
|
LFH lfh;
|
|
|
|
LFH_Copier params = { (u8*)&lfh, sizeof(LFH) };
|
|
|
|
ssize_t ret = file_io(f, ent->ofs, LFH_SIZE, FILE_BUF_TEMP, lfh_copier_cb, (uintptr_t)¶ms);
|
|
|
|
debug_assert(ret == sizeof(LFH));
|
2006-01-23 21:05:09 +01:00
|
|
|
|
2006-03-15 06:18:32 +01:00
|
|
|
ent->ofs += lfh_total_size(&lfh);
|
2006-02-11 23:49:09 +01:00
|
|
|
ent->flags &= ~ZIP_LFH_FIXUP_NEEDED;
|
|
|
|
}
|
2006-01-23 21:05:09 +01:00
|
|
|
|
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// archive builder backend
|
|
|
|
//-----------------------------------------------------------------------------
|
2004-05-06 19:14:30 +02:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// rationale: don't support partial adding, i.e. updating archive with
|
|
|
|
// only one file. this would require overwriting parts of the Zip archive,
|
|
|
|
// which is annoying and slow. also, archives are usually built in
|
|
|
|
// seek-optimal order, which would break if we start inserting files.
|
|
|
|
// while testing, loose files can be used, so there's no loss.
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2006-02-11 23:49:09 +01:00
|
|
|
// we don't want to expose ZipArchive to callers,
|
|
|
|
// (would require defining File, Pool and CDFH)
|
|
|
|
// so allocate the storage here and return opaque pointer.
|
2005-12-28 21:29:22 +01:00
|
|
|
struct ZipArchive
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2005-12-28 21:29:22 +01:00
|
|
|
File f;
|
|
|
|
off_t cur_file_size;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
Pool cdfhs;
|
|
|
|
uint cd_entries;
|
2006-01-31 04:47:52 +01:00
|
|
|
CDFH* prev_cdfh;
|
2004-03-03 00:56:51 +01:00
|
|
|
};
|
|
|
|
|
2006-01-28 23:19:42 +01:00
|
|
|
static SingleAllocator<ZipArchive> za_mgr;
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2006-02-15 04:03:17 +01:00
|
|
|
// create a new Zip archive and return a pointer for use in subsequent
|
|
|
|
// zip_archive_add_file calls. previous archive file is overwritten.
|
2006-01-28 23:19:42 +01:00
|
|
|
LibError zip_archive_create(const char* zip_filename, ZipArchive** pza)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2006-01-28 23:19:42 +01:00
|
|
|
// local za_copy simplifies things - if something fails, no cleanup is
|
|
|
|
// needed. upon success, we copy into the newly allocated real za.
|
|
|
|
ZipArchive za_copy;
|
2006-01-31 04:47:52 +01:00
|
|
|
za_copy.cur_file_size = 0;
|
|
|
|
za_copy.cd_entries = 0;
|
|
|
|
za_copy.prev_cdfh = 0;
|
|
|
|
|
|
|
|
RETURN_ERR(file_open(zip_filename, FILE_WRITE|FILE_NO_AIO, &za_copy.f));
|
2006-01-28 23:19:42 +01:00
|
|
|
RETURN_ERR(pool_create(&za_copy.cdfhs, 10*MiB, 0));
|
|
|
|
|
2006-04-27 00:38:25 +02:00
|
|
|
ZipArchive* za = za_mgr.alloc();
|
2006-01-28 23:19:42 +01:00
|
|
|
if(!za)
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::NO_MEM);
|
2006-01-28 23:19:42 +01:00
|
|
|
*za = za_copy;
|
|
|
|
*pza = za;
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-15 04:03:17 +01:00
|
|
|
// add a file (described by ArchiveEntry) to the archive. file_contents
|
|
|
|
// is the actual file data; its compression method is given in ae->method and
|
|
|
|
// can be CM_NONE.
|
|
|
|
// IO cost: writes out <file_contents> to disk (we don't currently attempt
|
|
|
|
// any sort of write-buffering).
|
2006-01-31 04:47:52 +01:00
|
|
|
LibError zip_archive_add_file(ZipArchive* za, const ArchiveEntry* ae, void* file_contents)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2006-03-15 06:18:32 +01:00
|
|
|
const size_t fn_len = strlen(ae->atom_fn);
|
2005-12-28 21:29:22 +01:00
|
|
|
|
|
|
|
// write (LFH, filename, file contents) to archive
|
2006-03-15 06:18:32 +01:00
|
|
|
// .. put LFH and filename into one 'package'
|
|
|
|
LFH_Package header;
|
|
|
|
lfh_assemble(&header.lfh, ae->method, ae->mtime, ae->crc, ae->csize, ae->ucsize, fn_len);
|
|
|
|
strcpy_s(header.fn, ARRAY_SIZE(header.fn), ae->atom_fn);
|
|
|
|
// .. write that out in 1 IO
|
|
|
|
const off_t lfh_ofs = za->cur_file_size;
|
2006-01-28 23:19:42 +01:00
|
|
|
FileIOBuf buf;
|
2006-03-15 06:18:32 +01:00
|
|
|
buf = (FileIOBuf)&header;
|
|
|
|
file_io(&za->f, lfh_ofs, LFH_SIZE+fn_len, &buf);
|
|
|
|
// .. write out file contents
|
2006-01-28 23:19:42 +01:00
|
|
|
buf = (FileIOBuf)file_contents;
|
2006-03-15 06:18:32 +01:00
|
|
|
file_io(&za->f, lfh_ofs+(off_t)(LFH_SIZE+fn_len), ae->csize, &buf);
|
|
|
|
za->cur_file_size += (off_t)(LFH_SIZE+fn_len+ae->csize);
|
2005-12-28 21:29:22 +01:00
|
|
|
|
|
|
|
// append a CDFH to the central dir (in memory)
|
2006-01-31 04:47:52 +01:00
|
|
|
// .. note: pool_alloc may round size up for padding purposes.
|
|
|
|
const size_t prev_pos = za->cdfhs.da.pos;
|
2006-03-15 06:18:32 +01:00
|
|
|
CDFH_Package* p = (CDFH_Package*)pool_alloc(&za->cdfhs, CDFH_SIZE+fn_len);
|
|
|
|
if(!p)
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::NO_MEM);
|
2006-01-31 04:47:52 +01:00
|
|
|
const size_t slack = za->cdfhs.da.pos-prev_pos - (CDFH_SIZE+fn_len);
|
2006-03-15 06:18:32 +01:00
|
|
|
cdfh_assemble(&p->cdfh, ae->method, ae->mtime, ae->crc, ae->csize, ae->ucsize, fn_len, slack, lfh_ofs);
|
|
|
|
memcpy2(p->fn, ae->atom_fn, fn_len);
|
2006-01-31 04:47:52 +01:00
|
|
|
|
|
|
|
za->cd_entries++;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-15 04:03:17 +01:00
|
|
|
// write out the archive to disk; only hereafter is it valid.
|
|
|
|
// frees the ZipArchive instance.
|
|
|
|
// IO cost: writes out Central Directory to disk (about 70 bytes per file).
|
2005-12-28 21:29:22 +01:00
|
|
|
LibError zip_archive_finish(ZipArchive* za)
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2005-12-28 21:29:22 +01:00
|
|
|
const size_t cd_size = za->cdfhs.da.pos;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
// append an ECDR to the CDFH list (this allows us to
|
|
|
|
// write out both to the archive file in one burst)
|
2006-01-31 04:47:52 +01:00
|
|
|
ECDR* ecdr = (ECDR*)pool_alloc(&za->cdfhs, ECDR_SIZE);
|
2005-12-28 21:29:22 +01:00
|
|
|
if(!ecdr)
|
2006-09-22 15:19:40 +02:00
|
|
|
WARN_RETURN(ERR::NO_MEM);
|
2006-03-15 06:18:32 +01:00
|
|
|
ecdr_assemble(ecdr, za->cd_entries, za->cur_file_size, cd_size);
|
2004-08-24 19:29:54 +02:00
|
|
|
|
2006-01-23 21:05:09 +01:00
|
|
|
FileIOBuf buf = za->cdfhs.da.base;
|
2006-01-31 04:47:52 +01:00
|
|
|
file_io(&za->f, za->cur_file_size, cd_size+ECDR_SIZE, &buf);
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-12-28 21:29:22 +01:00
|
|
|
(void)file_close(&za->f);
|
|
|
|
(void)pool_destroy(&za->cdfhs);
|
2006-03-15 06:18:32 +01:00
|
|
|
za_mgr.release(za);
|
2006-09-22 15:19:40 +02:00
|
|
|
return INFO::OK;
|
2004-05-06 19:14:30 +02:00
|
|
|
}
|