#include <sys/time.h>
#include <getopt.h>
-#include <Alloc.h>
-#include <7zFile.h>
-#include <7zVersion.h>
-#include <LzmaDec.h>
-#include <LzmaEnc.h>
#include <brotli/encode.h>
#define SUFSORT_MOD // Change suffix sorting algorithm from Qsufsort to Divsufsort
FILE * pfbz2;
};
-enum compression_method {
- CM_LZMA,
- CM_BROTLI,
-};
-
struct bsdiff_info {
const char *old_file;
const char *new_file;
const char *patch_file;
- enum compression_method comp_method;
};
struct data_thread data;
const char *kCantAllocateMessage = "Can not allocate memory";
const char *kDataErrorMessage = "Data error";
-static void *SzAlloc(void *p, size_t size)
-{
- p = p;
- return MyAlloc(size);
-}
-static void SzFree(void *p, void *address)
-{
- p = p;
- MyFree(address);
-}
-static ISzAlloc g_Alloc = { SzAlloc, SzFree };
-
int PrintError(char *buffer, const char *message, int buf_size)
{
snprintf(buffer + strlen(buffer), buf_size - strlen(buffer),
return 1;
}
-int PrintErrorNumber(char *buffer, SRes val, int buf_size)
-{
- snprintf(buffer + strlen(buffer), buf_size - strlen(buffer),
- "\nError code: %x\n", (unsigned)val);
- return 1;
-}
-
int PrintUserError(char *buffer, int buf_size)
{
return PrintError(buffer, "Incorrect command", buf_size);
#define IN_BUF_SIZE (1 << 16)
#define OUT_BUF_SIZE (1 << 16)
-
-static SRes lzma_encode(ISeqOutStream *outStream, ISeqInStream *inStream, UInt64 fileSize, char *rs)
-{
- CLzmaEncHandle enc;
- SRes res;
- CLzmaEncProps props;
-
- rs = rs;
-
- enc = LzmaEnc_Create(&g_Alloc);
- if (enc == 0)
- return SZ_ERROR_MEM;
-
- LzmaEncProps_Init(&props);
- res = LzmaEnc_SetProps(enc, &props);
-
- if (res == SZ_OK) {
- Byte header[LZMA_PROPS_SIZE + 8];
- size_t headerSize = LZMA_PROPS_SIZE;
- int i;
-
- res = LzmaEnc_WriteProperties(enc, header, &headerSize);
- for (i = 0; i < 8; i++)
- header[headerSize++] = (Byte)(fileSize >> (8 * i));
- if (outStream->Write(outStream, header, headerSize) != headerSize)
- res = SZ_ERROR_WRITE;
- else {
- if (res == SZ_OK)
- res = LzmaEnc_Encode(enc, outStream, inStream, NULL, &g_Alloc, &g_Alloc);
- }
- }
- LzmaEnc_Destroy(enc, &g_Alloc, &g_Alloc);
- return res;
-}
-
-int lzma_compress(const char *input_file, const char *output_file, char *rs, int rs_size)
-{
- assert(input_file);
- assert(output_file);
- assert(rs);
-
- CFileSeqInStream inStream;
- CFileOutStream outStream;
- int res;
-
- FileSeqInStream_CreateVTable(&inStream);
- File_Construct(&inStream.file);
-
- FileOutStream_CreateVTable(&outStream);
- File_Construct(&outStream.file);
-
- size_t t4 = sizeof(UInt32);
- size_t t8 = sizeof(UInt64);
- if (t4 != 4 || t8 != 8)
- return PrintError(rs, "Incorrect UInt32 or UInt64", rs_size);
-
- if (InFile_Open(&inStream.file, input_file) != 0)
- return PrintError(rs, "Can not open input file", rs_size);
-
-
- if (OutFile_Open(&outStream.file, output_file) != 0)
- return PrintError(rs, "Can not open output file", rs_size);
-
-
- UInt64 fileSize;
- File_GetLength(&inStream.file, &fileSize);
- res = lzma_encode(&outStream.s, &inStream.s, fileSize, rs);
-
- File_Close(&outStream.file);
- File_Close(&inStream.file);
-
- if (res != SZ_OK) {
- if (res == SZ_ERROR_MEM)
- return PrintError(rs, kCantAllocateMessage, rs_size);
- else if (res == SZ_ERROR_DATA)
- return PrintError(rs, kDataErrorMessage, rs_size);
- else if (res == SZ_ERROR_WRITE)
- return PrintError(rs, kCantWriteMessage, rs_size);
- else if (res == SZ_ERROR_READ)
- return PrintError(rs, kCantReadMessage, rs_size);
- return PrintErrorNumber(rs, res, rs_size);
- }
- return 0;
-}
-
int brotli_compress_internal(int input_fd, int output_fd, int quality)
{
int res = -1;
void print_help(const char *arg0)
{
assert(arg0);
- errx(1, "ss_bsdiff Version 5.0\nUsage: %s [-c <lzma|brotli>] oldfile newfile patchfile\n", arg0);
+ errx(1, "ss_bsdiff\nUsage: %s oldfile newfile patchfile\n", arg0);
}
int parse_args(struct bsdiff_info *info, int argc, char *argv[])
assert(info);
assert(argv);
- info->comp_method = CM_LZMA; // default compression method
-
- struct option long_options[] = {
- {"compression", optional_argument, NULL, 'c'},
- {0, 0 , 0, 0}
- };
-
- int opt;
-
- while ((opt = getopt_long(argc, argv, "c:", long_options, NULL)) != -1) {
- switch (opt) {
- case 'c':
- if (strcmp("lzma", optarg) == 0)
- info->comp_method = CM_LZMA;
- else if (strcmp("brotli", optarg) == 0)
- info->comp_method = CM_BROTLI;
- else {
- err(1, "Unknown compression method: %s", optarg);
- return -1;
- }
- }
- }
-
- if (optind + 2 >= argc) {
+ if (3 >= argc) {
err(1, "Not enough parameters");
print_help(argv[0]);
return -1;
}
- info->old_file = argv[optind];
- info->new_file = argv[optind+1];
- info->patch_file = argv[optind+2];
+ info->old_file = argv[1];
+ info->new_file = argv[2];
+ info->patch_file = argv[3];
return 0;
}
-int MY_CDECL main(int argc, char *argv[])
+int main(int argc, char *argv[])
{
char rs[800] = { 0 };
err(1, "bsdiff fails to create delta within timelimit");
}
#endif
- int res = 0;
- switch(info.comp_method) {
- case CM_LZMA:
- res = lzma_compress(TEMP_PATCH_NAME, info.patch_file, rs, sizeof(rs));
- break;
- case CM_BROTLI:
- res = brotli_compress(TEMP_PATCH_NAME, info.patch_file, BROTLI_COMPRESSION_QUALITY);
- break;
- default:
- printf("Unknown compression method\n");
- res = -1;
- break;
- }
+ int res = brotli_compress(TEMP_PATCH_NAME, info.patch_file, BROTLI_COMPRESSION_QUALITY);
if (remove(TEMP_PATCH_NAME) < 0)
printf("Failed to remove %s\n", TEMP_PATCH_NAME);