-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.cpp
155 lines (136 loc) · 3.8 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// Created by lenovo on 2019/12/10.
//
#include "cstring"
#include "gdelta.h"
#include <cstdio>
#include <cstdlib>
#include <ctype.h>
#include <fcntl.h>
#include <stdint.h>
#ifdef _MSC_VER
#include <compat/msvc.h>
#include <compat/getopt.h>
#else
#include <unistd.h>
#endif
int load_file_to_memory(const char *filename, uint8_t **result) {
FILE *f = fopen(filename, "rb");
if (f == NULL) {
*result = NULL;
return -1; // -1 means file opening fail
}
fseek(f, 0, SEEK_END);
size_t size = ftell(f);
fseek(f, 0, SEEK_SET);
*result = (uint8_t *)malloc(size + 1);
if (size != fread(*result, sizeof(char), size, f)) {
free(*result);
return -2; // -2 means file reading fail
}
fclose(f);
(*result)[size] = 0;
return size;
}
int main(int argc, char *argv[]) {
uint8_t edflags = 0;
int c;
char *cvalue = nullptr;
char *basefp = nullptr;
char *targetfp = nullptr;
while ((c = getopt(argc, argv, "edo:c")) != -1) {
switch (c) {
case 'd':
edflags |= 0b01;
break;
case 'e':
edflags |= 0b10;
break;
case 'o':
cvalue = optarg;
break;
case '?':
if (optopt == 'o')
fprintf(stderr, "Option -%o requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option `-%o'.\n", optopt);
else
fprintf(stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
abort();
}
}
if (edflags > 2 || edflags == 0) {
usage:
fprintf(stderr, "Usage: gdelta [-d|-e] [-o <outputfile>] <basefile> "
"<delta|target-file> \n");
return 1;
}
for (int i = 0; optind < argc; i++, optind++) {
switch (i) {
case 0:
basefp = argv[optind];
break;
case 1:
targetfp = argv[optind];
break;
default:
goto usage;
}
}
// printf("Args: base:%s target/delta:%s encode:%d\n", basefp, targetfp, edflags & 0b10);
if (basefp == nullptr || targetfp == nullptr)
goto usage;
// Set output filedescriptor (stdout or file)
int output_fd = fileno(stdout);
if (cvalue != nullptr) {
#ifdef _WIN32
output_fd = open(cvalue, O_RDWR | O_TRUNC | O_CREAT);
#else
output_fd = open(cvalue, O_RDWR | O_TRUNC | O_CREAT,
S_IRGRP | S_IWGRP | S_IWUSR | S_IRUSR);
#endif
if (output_fd < 0) {
printf("Failed to open output file (%d)\n", output_fd);
return 1;
}
}
uint8_t *target_delta;
uint64_t target_delta_size = load_file_to_memory(targetfp, &target_delta);
uint8_t *origin;
uint64_t origin_size = load_file_to_memory(basefp, &origin);
if (edflags & 0b10) {
// Encode target, origin -> delta
// Maximum size for delta is the target state (since it's only useful if
// it's less than that)
uint32_t delta_size = target_delta_size;
uint8_t *delta = (uint8_t *)malloc(delta_size);
int status = gencode(target_delta, target_delta_size, origin, origin_size,
&delta, &delta_size);
if (write(output_fd, delta, delta_size) < 0) {
printf("Failed to write output file (%d)\n", output_fd);
return 1;
}
}
if (edflags & 0b01) {
// Decode origin, delta -> target
// Allocate slightly more than the origin and delta combined
// TODO: handle status and increase buffer if too small
uint32_t target_size = target_delta_size + origin_size * 11 / 10;
uint8_t *target = (uint8_t *)malloc(target_size);
int status = gdecode(target_delta, target_delta_size, origin, origin_size,
&target, &target_size);
if (write(output_fd, target, target_size) < 0) {
printf("Failed to write output file (%d)\n", output_fd);
return 1;
}
free(target);
}
free(target_delta);
free(origin);
if (output_fd != fileno(stdout)) {
close(output_fd);
}
return 0;
}