2018-09-16 02:33:30 +02:00
|
|
|
#include <errno.h>
|
2018-07-24 19:56:21 +02:00
|
|
|
#include <stdbool.h>
|
2018-07-24 17:11:24 +02:00
|
|
|
#include <stdio.h>
|
2018-07-24 19:56:21 +02:00
|
|
|
#include <stdlib.h>
|
2018-07-17 14:05:22 +02:00
|
|
|
#include <string.h>
|
2018-07-31 13:18:18 +02:00
|
|
|
#include <unistd.h>
|
2018-07-17 14:05:22 +02:00
|
|
|
|
|
|
|
//Import our code
|
2018-09-14 04:11:44 +02:00
|
|
|
#include "utils/min.h"
|
2018-07-17 14:05:22 +02:00
|
|
|
#include "decrypt.h"
|
2018-09-14 01:41:35 +02:00
|
|
|
#include "errorAndExit.h"
|
2018-07-17 17:19:13 +02:00
|
|
|
#include "fileReader.h"
|
2018-07-31 13:18:18 +02:00
|
|
|
#include "inflate.h"
|
2018-09-14 01:02:12 +02:00
|
|
|
#include "parseArguments.h"
|
2018-09-30 16:48:41 +02:00
|
|
|
#include "rateLimiter.h"
|
2018-09-14 04:11:44 +02:00
|
|
|
#include "xdelta3.h"
|
2018-07-17 14:05:22 +02:00
|
|
|
|
2018-09-14 01:41:35 +02:00
|
|
|
//The size of the buffers where the compressed and uncompressed data is stored
|
2018-08-09 16:20:21 +02:00
|
|
|
#define BUFFER_SIZE 512UL * 1024UL //512 KiB
|
2018-09-14 01:41:35 +02:00
|
|
|
//If a file is encrypted, there are 12 random bytes before the start of the file contents
|
2018-07-24 19:56:21 +02:00
|
|
|
#define ENCRYPTION_HEADER_LENGTH 12UL
|
2018-09-14 01:41:35 +02:00
|
|
|
//Each local file header in a .zip file has the magic bytes "50 4b 03 04" at the beginning
|
2018-07-25 02:34:08 +02:00
|
|
|
#define LOCAL_FILE_HEADER_MAGIC (uint32_t)0x04034b50
|
2018-07-24 19:56:21 +02:00
|
|
|
|
2018-09-14 01:41:35 +02:00
|
|
|
//Convenience functions for reading integers from a char array
|
2018-08-07 15:22:07 +02:00
|
|
|
uint16_t getUint16(uint8_t* buffer) {
|
2018-09-14 01:41:35 +02:00
|
|
|
return ((uint16_t)buffer[0]) | \
|
|
|
|
((uint16_t)buffer[1]) << 8;
|
2018-07-24 19:56:21 +02:00
|
|
|
}
|
2018-08-07 15:22:07 +02:00
|
|
|
uint32_t getUint32(uint8_t* buffer) {
|
2018-09-14 01:41:35 +02:00
|
|
|
return ((uint32_t)buffer[0]) | \
|
|
|
|
((uint32_t)buffer[1]) << 8 | \
|
|
|
|
((uint32_t)buffer[2]) << 16 | \
|
|
|
|
((uint32_t)buffer[3]) << 24;
|
2018-07-24 19:56:21 +02:00
|
|
|
}
|
|
|
|
|
2018-09-14 00:48:53 +02:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
//Parse command line arguments
|
2018-09-14 01:02:12 +02:00
|
|
|
struct arguments state = parseArguments(argc, argv);
|
2018-09-14 00:48:53 +02:00
|
|
|
|
2018-07-24 19:56:21 +02:00
|
|
|
//-------------------------------------------------
|
2018-07-17 14:05:22 +02:00
|
|
|
|
2018-08-07 15:22:07 +02:00
|
|
|
uint8_t* compressedChunk = malloc(BUFFER_SIZE);
|
2018-07-31 13:18:18 +02:00
|
|
|
if (compressedChunk == NULL) {
|
|
|
|
fprintf(stderr, "Could not allocate %lu bytes for compressed buffer.\n", BUFFER_SIZE);
|
2018-09-14 01:41:35 +02:00
|
|
|
errorAndExit();
|
2018-07-31 13:18:18 +02:00
|
|
|
}
|
|
|
|
|
2018-08-07 15:22:07 +02:00
|
|
|
uint8_t* uncompressedChunk = malloc(BUFFER_SIZE);
|
2018-07-31 13:18:18 +02:00
|
|
|
if (uncompressedChunk == NULL) {
|
|
|
|
fprintf(stderr, "Could not allocate %lu bytes for uncompressed buffer.\n", BUFFER_SIZE);
|
2018-09-14 01:41:35 +02:00
|
|
|
errorAndExit();
|
2018-07-31 13:18:18 +02:00
|
|
|
}
|
2018-08-07 15:22:07 +02:00
|
|
|
memset(uncompressedChunk, (uint8_t)0, BUFFER_SIZE);
|
2018-07-31 13:18:18 +02:00
|
|
|
|
|
|
|
//-------------------------------------------------
|
|
|
|
|
2018-07-25 02:34:08 +02:00
|
|
|
//Initialize file reader
|
2018-09-14 00:48:53 +02:00
|
|
|
initFileReader(state.diskName, state.diskOffset);
|
2018-07-17 15:05:57 +02:00
|
|
|
|
2018-07-24 19:56:21 +02:00
|
|
|
//Skip local file header (30 bytes + additional length)
|
2018-09-14 04:54:11 +02:00
|
|
|
getBytes(compressedChunk, 30UL, false);
|
2018-07-24 19:56:21 +02:00
|
|
|
//Check that header is correct
|
2018-07-31 13:18:18 +02:00
|
|
|
const uint32_t magic = getUint32(compressedChunk);
|
2018-07-25 02:34:08 +02:00
|
|
|
if (magic != LOCAL_FILE_HEADER_MAGIC) {
|
2018-09-14 01:41:35 +02:00
|
|
|
fprintf(stderr, "Wrong magic in local file header, expected %#010x but found %#010x.\n", LOCAL_FILE_HEADER_MAGIC, magic);
|
|
|
|
errorAndExit();
|
2018-07-24 19:56:21 +02:00
|
|
|
}
|
|
|
|
//Read additional length
|
2018-07-31 13:18:18 +02:00
|
|
|
const unsigned long additionalLength = getUint16(compressedChunk + 26) + getUint16(compressedChunk + 28);
|
2018-09-14 00:48:53 +02:00
|
|
|
if (additionalLength > 0UL) {
|
2018-09-14 04:54:11 +02:00
|
|
|
getBytes(NULL, additionalLength, false);
|
2018-09-14 00:48:53 +02:00
|
|
|
}
|
2018-07-17 15:05:57 +02:00
|
|
|
|
2018-09-14 05:40:03 +02:00
|
|
|
unsigned long remainingBytes = state.fileSize;
|
|
|
|
|
2018-07-24 19:56:21 +02:00
|
|
|
//If file is encrypted, skip 12-byte encryption header
|
2018-09-14 00:48:53 +02:00
|
|
|
if (state.isEncrypted) {
|
2018-09-14 04:54:11 +02:00
|
|
|
getBytes(compressedChunk, ENCRYPTION_HEADER_LENGTH, false);
|
2018-07-31 13:18:18 +02:00
|
|
|
decrypt(compressedChunk, ENCRYPTION_HEADER_LENGTH);
|
2018-09-14 05:40:03 +02:00
|
|
|
remainingBytes -= 12;
|
2018-07-24 19:56:21 +02:00
|
|
|
}
|
2018-07-17 15:05:57 +02:00
|
|
|
|
2018-09-14 04:11:44 +02:00
|
|
|
//Initialize xdelta3
|
|
|
|
if (state.prevFile) {
|
|
|
|
xdelta3Init(state.prevFile);
|
|
|
|
}
|
|
|
|
|
2018-09-16 02:33:30 +02:00
|
|
|
//If a target path is specified, create a file write stream
|
|
|
|
FILE* targetFile;
|
|
|
|
if (state.target) {
|
|
|
|
targetFile = fopen(state.target, "wb");
|
|
|
|
if (!targetFile) {
|
|
|
|
fprintf(stderr, "Could not open the target file \"%s\" for writing: %s\n", state.target, strerror(errno));
|
|
|
|
errorAndExit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 19:56:21 +02:00
|
|
|
//-------------------------------------------------
|
2018-07-17 15:05:57 +02:00
|
|
|
|
2018-07-31 13:18:18 +02:00
|
|
|
struct InflateOutput inflateResult;
|
|
|
|
inflateInit(compressedChunk, uncompressedChunk, BUFFER_SIZE);
|
|
|
|
|
2018-07-24 18:10:07 +02:00
|
|
|
//Read actual file
|
2018-07-31 13:18:18 +02:00
|
|
|
bool needToRead = true;
|
2018-07-31 13:38:21 +02:00
|
|
|
bool hasReachedEnd = false;
|
2018-07-31 13:18:18 +02:00
|
|
|
unsigned long chunkSize;
|
2018-08-09 16:20:21 +02:00
|
|
|
unsigned long uncompressedPosition = 0UL;
|
2018-07-31 13:38:21 +02:00
|
|
|
while (remainingBytes > 0 || !hasReachedEnd) {
|
2018-09-14 05:40:03 +02:00
|
|
|
if (needToRead && remainingBytes > 0) {
|
|
|
|
chunkSize = min(BUFFER_SIZE, remainingBytes);
|
2018-09-14 04:52:18 +02:00
|
|
|
getBytes(compressedChunk, chunkSize, chunkSize == remainingBytes);
|
2018-07-31 13:18:18 +02:00
|
|
|
remainingBytes -= chunkSize;
|
|
|
|
|
|
|
|
//Decrypt file if it is encrypted
|
2018-09-14 00:48:53 +02:00
|
|
|
if (state.isEncrypted) {
|
2018-07-31 13:18:18 +02:00
|
|
|
decrypt(compressedChunk, chunkSize);
|
|
|
|
}
|
2018-07-24 19:56:21 +02:00
|
|
|
}
|
2018-07-24 18:10:07 +02:00
|
|
|
|
|
|
|
//Decompress file
|
2018-07-31 13:18:18 +02:00
|
|
|
//bytes are contained in uncompressedChunk from [0, inflateResult.numBytesWrittenToOutput - 1]
|
|
|
|
inflateResult = inflateInflate(needToRead ? chunkSize : 0, remainingBytes > 0);
|
|
|
|
needToRead = inflateResult.needMoreInput;
|
2018-07-31 13:38:21 +02:00
|
|
|
hasReachedEnd = inflateResult.hasReachedEnd;
|
2018-07-31 13:18:18 +02:00
|
|
|
|
2018-08-07 15:22:07 +02:00
|
|
|
//important: we must not modify uncompressedChunk since miniz may use it as dictionary and read from it during the next invocation of inflateInflate()
|
|
|
|
|
2018-09-30 16:48:41 +02:00
|
|
|
uint8_t* dataBuffer = uncompressedChunk + uncompressedPosition;
|
|
|
|
unsigned long numBytes = inflateResult.numBytesWrittenToOutput;
|
|
|
|
|
2018-09-14 04:11:44 +02:00
|
|
|
//Optionally perform xdelta3
|
|
|
|
if (state.prevFile) {
|
2018-09-30 16:48:41 +02:00
|
|
|
xdelta3AddInput(dataBuffer, numBytes, remainingBytes == 0 && hasReachedEnd, targetFile);
|
2018-09-16 02:33:30 +02:00
|
|
|
} else { //otherwise write to target file or stdout
|
2018-09-30 16:48:41 +02:00
|
|
|
consumeCapacity(numBytes);
|
2018-09-16 02:33:30 +02:00
|
|
|
if (state.target) {
|
2018-09-30 16:48:41 +02:00
|
|
|
fwrite(dataBuffer, 1, numBytes, targetFile);
|
2018-09-16 02:33:30 +02:00
|
|
|
} else {
|
2018-09-30 16:48:41 +02:00
|
|
|
write(1, dataBuffer, numBytes);
|
2018-09-16 02:33:30 +02:00
|
|
|
}
|
2018-09-14 04:11:44 +02:00
|
|
|
}
|
|
|
|
|
2018-08-09 16:20:21 +02:00
|
|
|
uncompressedPosition += inflateResult.numBytesWrittenToOutput;
|
|
|
|
while (uncompressedPosition >= BUFFER_SIZE) {
|
|
|
|
uncompressedPosition -= BUFFER_SIZE;
|
|
|
|
}
|
2018-07-24 18:10:07 +02:00
|
|
|
}
|
2018-07-17 15:05:57 +02:00
|
|
|
|
2018-07-31 13:18:18 +02:00
|
|
|
//release memory
|
|
|
|
free(compressedChunk);
|
|
|
|
free(uncompressedChunk);
|
|
|
|
|
2018-07-17 14:05:22 +02:00
|
|
|
return 0;
|
|
|
|
}
|