ssn-installer/src/main.c

156 lines
4.9 KiB
C
Raw Normal View History

#include <errno.h>
2018-07-24 19:56:21 +02:00
#include <stdbool.h>
#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"
#include "parseArguments.h"
#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
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
}
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
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
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
}
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
}
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
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);
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);
}
//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;
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;
while (remainingBytes > 0 || !hasReachedEnd) {
if (needToRead && remainingBytes > 0) {
chunkSize = min(BUFFER_SIZE, remainingBytes);
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;
hasReachedEnd = inflateResult.hasReachedEnd;
2018-07-31 13:18:18 +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()
uint8_t* dataBuffer = uncompressedChunk + uncompressedPosition;
unsigned long numBytes = inflateResult.numBytesWrittenToOutput;
2018-09-14 04:11:44 +02:00
//Optionally perform xdelta3
if (state.prevFile) {
xdelta3AddInput(dataBuffer, numBytes, remainingBytes == 0 && hasReachedEnd, targetFile);
} else { //otherwise write to target file or stdout
consumeCapacity(numBytes);
if (state.target) {
fwrite(dataBuffer, 1, numBytes, targetFile);
} else {
write(1, dataBuffer, numBytes);
}
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;
}