Skip to content

Commit

Permalink
Default to older file header unless there is a .bmp
Browse files Browse the repository at this point in the history
Co-authored-by: Adrien Bertrand <bertrand.adrien@gmail.com>
Co-authored-by: Fabian Vogt <fabian@ritter-vogt.de>
  • Loading branch information
3 people committed Oct 2, 2022
1 parent 84d64c3 commit af0c31d
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 48 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Then you can just run `make`.

## History

2018-10-03: v2.1
* FIX: luna now defaults to an older file header version unless one of the input files is a .bmp

2016-12-28: v2.0
* NEW: No OpenSSL library dependency anymore, just use the relevant DES code.
* FIX: Fix crash when parsing deeply nested XML documents
Expand Down
35 changes: 21 additions & 14 deletions luna.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <zlib.h>
#include "minizip-1.1/zip.h"

#define LUNA_VER "2.0"
#define LUNA_VER "2.1"

#ifndef min
#define min(a,b) (((a) < (b)) ? (a) : (b))
Expand Down Expand Up @@ -432,7 +432,7 @@ int doccrypt(uint8_t *inout, long in_size) {
static zipFile zipF = 0;

// stateful, keep the zipFile opened
int add_processed_file_to_tns(const char *infile_name, void const *in_buf, long in_size, const char *outfile_path) {
int add_processed_file_to_tns(const char *infile_name, void const *in_buf, long in_size, const char *outfile_path, unsigned tiversion) {
zip_fileinfo zi;
if (!zipF && !(zipF = zipOpen(outfile_path, 0))) {
puts("can't open zip-TNS file for writing");
Expand All @@ -450,7 +450,7 @@ int add_processed_file_to_tns(const char *infile_name, void const *in_buf, long
method = Z_DEFLATED; // just deflated
level = Z_DEFAULT_COMPRESSION;
}
if (zipOpenNewFileInZip2(zipF, infile_name, &zi, NULL, 0, NULL, 0, NULL, method, level, 0) != ZIP_OK) {
if (zipOpenNewFileInZip2(zipF, infile_name, &zi, NULL, 0, NULL, 0, NULL, method, level, 0, tiversion) != ZIP_OK) {
puts("can't open file in zip-TNS file for writing");
close_quit:
zipClose(zipF, NULL);
Expand Down Expand Up @@ -501,7 +501,7 @@ long deflate_compressed_xml(void *def_buf, size_t def_size, void *xmlc_buf, size
return zstream.total_out;
}

int add_default_document_to_tns(const char *tnsfile_path) {
int add_default_document_to_tns(const char *tnsfile_path, unsigned tiversion) {
static const char default_processed_document_xml[] =
"\x0F\xCE\xD8\xD2\x81\x06\x86\x5B\x4A\x4A\xC5\xCE\xA9\x16\xF2\xD5\x1D\xA8\x2F\x6E"
"\x00\x22\xF2\xF0\xC1\xA6\x06\x77\x4D\x7E\xA6\xC0\x3A\xF0\x5C\x74\xBA\xAA\x44\x60"
Expand All @@ -519,10 +519,10 @@ int add_default_document_to_tns(const char *tnsfile_path) {
"\x2D\xFA\x69\x9F\x11\xD2\x20\x12\xE0\x79\x14\x04\x4E\x62\x8F\x0A\x2A\x18\x72\x5A"
"\x8B\x80\xB3\x3C\x9B\xD5\x67\x59\x4B\x51\x4D\xE0\xC3\x38\x28\xC3\xDC\xCD\x39\x22"
"\x12\x8C\x40\x55";
return add_processed_file_to_tns("Document.xml", default_processed_document_xml, sizeof(default_processed_document_xml) - 1, tnsfile_path);
return add_processed_file_to_tns("Document.xml", default_processed_document_xml, sizeof(default_processed_document_xml) - 1, tnsfile_path, tiversion);
}

int add_infile_to_tns(const char *infile_path, const char *tnsfile_path) {
int add_infile_to_tns(const char *infile_path, const char *tnsfile_path, unsigned tiversion) {
size_t xmlc_buf_size;
void *xmlc_buf = read_file_and_xml_compress(infile_path, &xmlc_buf_size);
if (!xmlc_buf)
Expand All @@ -548,11 +548,11 @@ int add_infile_to_tns(const char *infile_path, const char *tnsfile_path) {
if (doccrypt(def_buf, deflated_size))
goto add_infile_err;
memcpy(header_and_deflated_buf, tien_crypted_header, header_size);
if (add_processed_file_to_tns(gnu_basename(infile_path), header_and_deflated_buf, header_size + deflated_size, tnsfile_path))
if (add_processed_file_to_tns(gnu_basename(infile_path), header_and_deflated_buf, header_size + deflated_size, tnsfile_path, tiversion))
goto add_infile_err;
}
else { // don't crypt, don't deflate: will be deflated by minizip
if (add_processed_file_to_tns(gnu_basename(infile_path), xmlc_buf, xmlc_buf_size, tnsfile_path))
if (add_processed_file_to_tns(gnu_basename(infile_path), xmlc_buf, xmlc_buf_size, tnsfile_path, tiversion))
goto add_infile_err;
}
free(header_and_deflated_buf);
Expand All @@ -570,30 +570,37 @@ int main(int argc, char *argv[]) {
);
return 0;
}
unsigned tiversion = 0x0500; // default to document version 5
for (int i = 1; i <= argc - 2; i++) { // infiles: the args except the last one
if (has_ext(argv[i], ".bmp")) {
tiversion = 0x0700; // bitmap files require the document type to be bumped up to 7
break;
}
}

char *outfile_path = argv[argc - 1];
unlink(outfile_path);

zipF = 0; // Only useful for emscripten (possible multiple main() calls)

// Document.xml must be added first to the TNS
int has_processed_documentxml = 0;
int i;
for (i = 1; i <= argc - 2; i++) { // infiles: the args except the last one
for (int i = 1; i <= argc - 2; i++) { // infiles: the args except the last one
if (!strcmp("Document.xml", gnu_basename(argv[i]))) {
printf("processing '%s'...\n", argv[i]);
int ret = add_infile_to_tns(argv[i], outfile_path);
int ret = add_infile_to_tns(argv[i], outfile_path, tiversion);
if (ret) return ret;
has_processed_documentxml = 1;
}
}
if (!has_processed_documentxml) {
int ret = add_default_document_to_tns(outfile_path);
int ret = add_default_document_to_tns(outfile_path, tiversion);
if (ret) return ret;
}

// Then add all the other files
int is_converting_lua = 0;
for (i = 1; i <= argc - 2; i++) { // infiles: the args except the last one
for (int i = 1; i <= argc - 2; i++) { // infiles: the args except the last one
if (!strcmp("Document.xml", gnu_basename(argv[i])))
continue;
printf("processing '%s'...\n", argv[i]);
Expand All @@ -604,7 +611,7 @@ int main(int argc, char *argv[]) {
}
is_converting_lua = 1;
}
int ret = add_infile_to_tns(argv[i], outfile_path);
int ret = add_infile_to_tns(argv[i], outfile_path, tiversion);
if (ret) return ret;
}

Expand Down
70 changes: 38 additions & 32 deletions minizip-1.1/zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
*/

/* Changes by Olivier ARMAND for luna:
/* Changes for luna:
* - Remove value check for parameter 'method' of zipOpenNewFileInZip4_64()
* - Change LOCALHEADERMAGIC to LOCALHEADERMAGIC1/2/3 (first entry) and STDLOCALHEADERMAGIC (next entries)
* - zipCloseFileInZipRaw64(): fix offsets due to new LOCALHEADERMAGIC* length
* - Change ENDHEADERMAGIC
* - Modify zipOpenNewFileInZip* to add tiversion to be passed through when generating file headers
*/


Expand Down Expand Up @@ -108,9 +109,9 @@ const char zip_copyright[] =" zip 1.01 Copyright 1998-2004 Gilles Vollant - http
#define SIZEDATA_INDATABLOCK (4096-(4*4))

#define STDLOCALHEADERMAGIC (0x04034b50)
#define LOCALHEADERMAGIC1 (0x4D49542A)
#define LOCALHEADERMAGIC2 (0x3730504C)
#define LOCALHEADERMAGIC3 (0x3030)
#define LOCALHEADERMAGIC1 (0x4D49542A) /* MIT* */
#define LOCALHEADERMAGIC2 (0x504C) /* PL */
#define TIVERSION_DEFAULT (0x0500)
#define CENTRALHEADERMAGIC (0x02014b50)
#define ENDHEADERMAGIC (0x44504954)
#define ZIP64ENDHEADERMAGIC (0x6064b50)
Expand Down Expand Up @@ -965,29 +966,34 @@ extern zipFile ZEXPORT zipOpen64 (const void* pathname, int append)
return zipOpen3(pathname,append,NULL,NULL);
}

int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local)
int Write_LocalFileHeader(zip64_internal* zi, const char* filename, uInt size_extrafield_local, const void* extrafield_local, uInt tiversion)
{
/* write the local header */
int err;
uInt size_filename = (uInt)strlen(filename);
uInt size_extrafield = size_extrafield_local;

if (!zi->number_entry)
{
err = zip64local_putValue(&zi->z_filefunc,zi->filestream, LOCALHEADERMAGIC1, 4);
if (err==ZIP_OK)
{
err = zip64local_putValue(&zi->z_filefunc,zi->filestream, LOCALHEADERMAGIC2, 4);
}
if (err==ZIP_OK)
{
err = zip64local_putValue(&zi->z_filefunc,zi->filestream, LOCALHEADERMAGIC3, 2);
}
}
else
{
err = zip64local_putValue(&zi->z_filefunc,zi->filestream, STDLOCALHEADERMAGIC, 4);
}
if (!zi->number_entry)
{
err = zip64local_putValue(&zi->z_filefunc,zi->filestream, LOCALHEADERMAGIC1, 4);
if (err==ZIP_OK)
{
err = zip64local_putValue(&zi->z_filefunc,zi->filestream, LOCALHEADERMAGIC2, 2);
}
if (err==ZIP_OK)
{
char buf[5] = {0};
snprintf(buf, 5, "%04X", tiversion);
if (ZWRITE64(zi->z_filefunc,zi->filestream, buf, 4) != 4)
{
err = ZIP_ERRNO;
}
}
}
else
{
err = zip64local_putValue(&zi->z_filefunc,zi->filestream, STDLOCALHEADERMAGIC, 4);
}

if (err==ZIP_OK)
{
Expand Down Expand Up @@ -1081,9 +1087,9 @@ extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename,
const void* extrafield_local, uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
const char* comment, int method, int level, int raw,
int windowBits,int memLevel, int strategy,
int windowBits, int memLevel, int strategy,
const char* password, uLong crcForCrypting,
uLong versionMadeBy, uLong flagBase, int zip64)
uLong versionMadeBy, uLong flagBase, int zip64, uInt tiversion)
{
zip64_internal* zi;
uInt size_filename;
Expand Down Expand Up @@ -1200,7 +1206,7 @@ extern int ZEXPORT zipOpenNewFileInZip4_64 (zipFile file, const char* filename,
zi->ci.totalUncompressedData = 0;
zi->ci.pos_zip64extrainfo = 0;

err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local);
err = Write_LocalFileHeader(zi, filename, size_extrafield_local, extrafield_local, tiversion);

#ifdef HAVE_BZIP2
zi->ci.bstream.avail_in = (uInt)0;
Expand Down Expand Up @@ -1291,7 +1297,7 @@ extern int ZEXPORT zipOpenNewFileInZip4 (zipFile file, const char* filename, con
extrafield_global, size_extrafield_global,
comment, method, level, raw,
windowBits, memLevel, strategy,
password, crcForCrypting, versionMadeBy, flagBase, 0);
password, crcForCrypting, versionMadeBy, flagBase, 0, TIVERSION_DEFAULT);
}

extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
Expand All @@ -1306,7 +1312,7 @@ extern int ZEXPORT zipOpenNewFileInZip3 (zipFile file, const char* filename, con
extrafield_global, size_extrafield_global,
comment, method, level, raw,
windowBits, memLevel, strategy,
password, crcForCrypting, VERSIONMADEBY, 0, 0);
password, crcForCrypting, VERSIONMADEBY, 0, 0, TIVERSION_DEFAULT);
}

extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
Expand All @@ -1321,20 +1327,20 @@ extern int ZEXPORT zipOpenNewFileInZip3_64(zipFile file, const char* filename, c
extrafield_global, size_extrafield_global,
comment, method, level, raw,
windowBits, memLevel, strategy,
password, crcForCrypting, VERSIONMADEBY, 0, zip64);
password, crcForCrypting, VERSIONMADEBY, 0, zip64, TIVERSION_DEFAULT);
}

extern int ZEXPORT zipOpenNewFileInZip2(zipFile file, const char* filename, const zip_fileinfo* zipfi,
const void* extrafield_local, uInt size_extrafield_local,
const void* extrafield_global, uInt size_extrafield_global,
const char* comment, int method, int level, int raw)
const char* comment, int method, int level, int raw, uInt tiversion)
{
return zipOpenNewFileInZip4_64 (file, filename, zipfi,
extrafield_local, size_extrafield_local,
extrafield_global, size_extrafield_global,
comment, method, level, raw,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, VERSIONMADEBY, 0, 0);
NULL, 0, VERSIONMADEBY, 0, 0, tiversion);
}

extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, const zip_fileinfo* zipfi,
Expand All @@ -1347,7 +1353,7 @@ extern int ZEXPORT zipOpenNewFileInZip2_64(zipFile file, const char* filename, c
extrafield_global, size_extrafield_global,
comment, method, level, raw,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, VERSIONMADEBY, 0, zip64);
NULL, 0, VERSIONMADEBY, 0, zip64, TIVERSION_DEFAULT);
}

extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, const zip_fileinfo* zipfi,
Expand All @@ -1360,7 +1366,7 @@ extern int ZEXPORT zipOpenNewFileInZip64 (zipFile file, const char* filename, co
extrafield_global, size_extrafield_global,
comment, method, level, 0,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, VERSIONMADEBY, 0, zip64);
NULL, 0, VERSIONMADEBY, 0, zip64, TIVERSION_DEFAULT);
}

extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, const zip_fileinfo* zipfi,
Expand All @@ -1373,7 +1379,7 @@ extern int ZEXPORT zipOpenNewFileInZip (zipFile file, const char* filename, cons
extrafield_global, size_extrafield_global,
comment, method, level, 0,
-MAX_WBITS, DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY,
NULL, 0, VERSIONMADEBY, 0, 0);
NULL, 0, VERSIONMADEBY, 0, 0, TIVERSION_DEFAULT);
}

local int zip64FlushWriteBuffer(zip64_internal* zi)
Expand Down
6 changes: 4 additions & 2 deletions minizip-1.1/zip.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ extern int ZEXPORT zipOpenNewFileInZip2 OF((zipFile file,
const char* comment,
int method,
int level,
int raw));
int raw,
uInt tiversion));


extern int ZEXPORT zipOpenNewFileInZip2_64 OF((zipFile file,
Expand Down Expand Up @@ -295,7 +296,8 @@ extern int ZEXPORT zipOpenNewFileInZip4_64 OF((zipFile file,
uLong crcForCrypting,
uLong versionMadeBy,
uLong flagBase,
int zip64
int zip64,
uInt tiversion
));
/*
Same than zipOpenNewFileInZip4, except
Expand Down

0 comments on commit af0c31d

Please sign in to comment.