/
main.c
144 lines (119 loc) · 3.29 KB
/
main.c
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
# Copyright (c) 2016-.. #John
#
# Author: #John <pocolab.com@gmail.com>
# URL: http://www.pocolab.com
# Version: 1.0.0
# Commentary:
# xoror - xor two binary files
# License:
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Emacs; see the file COPYING. If not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
#include <stdio.h>
#include <stdlib.h>
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define CHUNK 1024
void print_help();
size_t file_size(FILE *f);
void open_files(const char *filename1, const char *filename2,
FILE **f_smaller, FILE **f_bigger,
size_t *f_s_s, size_t *f_b_s);
int main(int argc, char **argv){
if(argc!=3){
print_help();
exit(EXIT_FAILURE);
}
size_t f_s_s,f_b_s;
FILE *f_smaller;
FILE *f_bigger;
open_files(argv[1],argv[2],
&f_smaller,&f_bigger,
&f_s_s,&f_b_s);
size_t fl=f_s_s<CHUNK;
size_t len=fl?f_s_s:CHUNK;
size_t i,j;
unsigned char *b_s=malloc(len*sizeof(unsigned char));
unsigned char *b_b=malloc(len*sizeof(unsigned char));
unsigned char *r=malloc(len*sizeof(unsigned char));
size_t s_d=0,b_d=0;
size_t l_len=len;
int s,b;
FILE *f=fopen("x.zip","wb");
// TODO: add flexible size of buffer, smaller file.
while(b_d<f_b_s){
fseek(f_bigger, b_d, SEEK_SET);
fseek(f_smaller, b_d, SEEK_SET);
fread(b_s,sizeof(unsigned char),l_len,f_smaller);
fread(b_b,sizeof(unsigned char),l_len,f_bigger);
for(i=0;i<l_len;i++){
s=b_s[i];
b=b_b[i];
r[i]=s^b;
}
//fseek(f_smaller, 0, SEEK_SET);
if(b_d+l_len>f_b_s){
l_len=f_b_s-b_d;
}
fwrite(r,sizeof(char),l_len,f);
b_d+=len;
s_d+=len;
}
return(0);
}
void print_help(){
printf("\n Usage: xoror file1 file2");
}
size_t file_size(FILE *f){
size_t s;
fseek(f, 0, SEEK_END);
s = ftell(f);
fseek(f, 0, SEEK_SET);
return s;
}
void open_files(const char *filename1, const char *filename2,
FILE **f_smaller, FILE **f_bigger,
size_t *f_s_s, size_t *f_b_s
){
*f_smaller=fopen(filename1,"rb");
*f_bigger=fopen(filename2,"rb");
if(!*f_smaller){
fprintf(stderr,"Check input file: %s\n", filename1);
exit(EXIT_FAILURE);
}
if(!*f_bigger){
fprintf(stderr,"Check input file: %s\n", filename2);
exit(EXIT_FAILURE);
}
*f_s_s=file_size(*f_smaller);
*f_b_s=file_size(*f_bigger);
if(!*f_s_s){
fprintf(stderr,"Input file is empty: %s\n", filename1);
exit(EXIT_FAILURE);
}
if(!*f_b_s){
fprintf(stderr,"Input file is empty: %s\n", filename2);
exit(EXIT_FAILURE);
}
if (*f_s_s>*f_b_s) {
FILE *f;
size_t n;
f=*f_smaller;
*f_smaller=*f_bigger;
*f_bigger=f;
n=*f_s_s;
*f_s_s=*f_b_s;
*f_b_s=n;
}
}