Added -v flag. Some changes trying to merge a XML file.
[mx3r.git] / mx3r.c
blob0cf6bc335dde427eaa8b8a424fc408e8d6e5be74
1 /*
2 Prueba de concepto de buscador de diferencias por bloques.
3 */
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
9 // CLIG - Command Line Interpreter Generator:
10 #include "cmdline.h"
11 #include "util.h"
13 extern int basesz;
14 extern char *basef;
15 extern int localsz;
16 extern char *localf;
17 extern int remotesz;
18 extern char *remotef;
20 int debug=0;
22 void showUsage()
24 printf("Usage: \n"); usage();
28 hashblock cb_hash[2][512];
29 int cb_nhash[2]={0,0};
30 int cb_blocks[2]={0,0};
31 int cb_deleted[2]={0,0};
32 int cb_added[2]={0,0};
34 void calcBytes(int nfile,int bFrom,int bTo, int lFrom, int lTo)
36 char *baseFrom=&basef[bFrom];
37 char *baseTo=&basef[bTo];
38 char *localFrom=0;
39 char *localTo=0;
40 if (nfile==0)
42 localFrom=&localf[lFrom];
43 localTo=&localf[lTo];
45 if (nfile==1)
47 localFrom=&remotef[lFrom];
48 localTo=&remotef[lTo];
52 hashblock *hbchar=cb_hash[nfile]+cb_nhash[nfile];
53 int totalsize=(baseTo-baseFrom)+(localTo-localFrom);
55 if (debug) printf("trying %d-%d %d-%d\n",bFrom,bTo,lFrom,lTo);
57 int nhbchar = compare2bytevectors(
58 baseFrom, baseTo-baseFrom,
59 localFrom, localTo-localFrom,
60 16,totalsize/2,
61 hbchar, 512-cb_nhash[nfile]);
63 int totalsize1=0;
64 int j;
65 for (j=0;j<nhbchar;j++)
67 hbchar[j].line1+=bFrom;
68 hbchar[j].line2+=lFrom;
69 totalsize1+=hbchar[j].size;
70 if (debug) printf("%d-%d %d-%d\n",hbchar[j].line1,hbchar[j].line1+hbchar[j].size,hbchar[j].line2,hbchar[j].line2+hbchar[j].size);
73 /* if (debug) printf("-%d +%d (%d)*\n",
74 baseTo-baseFrom-totalsize1,
75 localTo-localFrom-totalsize1,nhbchar);*/
77 cb_nhash[nfile]+=nhbchar;
79 // Estadísticas:
80 cb_blocks[nfile]+=nhbchar;
81 cb_deleted[nfile]+=baseTo-baseFrom-totalsize1;
82 cb_added[nfile]+=localTo-localFrom-totalsize1;
87 int doBLRMerge(FILE *out, char *base, char *local, char *remote)
89 BLRintA split=splitBLR(base, local, remote);
91 if (debug) fprintf( stderr, "Base File: %d blocks found\n",split.basesz);
92 if (debug) fprintf( stderr, "Local File: %d blocks found\n",split.localsz);
93 if (debug) fprintf( stderr, "Remote File: %d blocks found\n",split.remotesz);
95 BLRintA hash=hashBLR();
97 hashblock bloque_BR[64];
98 int nbloquesBR = compare2hashvectors(
99 hash.base, hash.basesz,
100 hash.remote, hash.remotesz,
101 4, 256,
102 bloque_BR, 64);
104 hashblock bloque_BL[64];
105 int nbloquesBL = compare2hashvectors(
106 hash.base, hash.basesz,
107 hash.local, hash.localsz,
108 4, 256,
109 bloque_BL, 64);
111 int i;
112 for (i=0;i<nbloquesBL+1;i++)
114 if (i==nbloquesBL)
116 bloque_BL[i].line1=split.basesz;
117 bloque_BL[i].line2=split.localsz;
118 bloque_BL[i].size=0;
120 if (i>0)
122 int b1Start=bloque_BL[i-1].line1+bloque_BL[i-1].size;
123 int b2Start=bloque_BL[i-1].line2+bloque_BL[i-1].size;
124 int offset1=bloque_BL[i].line1-b1Start;
125 int offset2=bloque_BL[i].line2-b2Start;
127 if (debug) printf("Orphan:");
128 calcBytes(0,
129 split.base[b1Start],
130 split.base[b1Start+offset1]-1,
131 split.local[b2Start],
132 split.local[b2Start+offset2]-1);
135 if (i<nbloquesBL)
138 calcBytes(0,
139 split.base[bloque_BL[i].line1],
140 split.base[bloque_BL[i].line1+bloque_BL[i].size]-1,
142 split.local[bloque_BL[i].line2],
143 split.local[bloque_BL[i].line2+bloque_BL[i].size]-1
149 if (debug) printf("Total bytes deleted: %d\n", cb_deleted[0]);
150 if (debug) printf("Total bytes added: %d\n", cb_added[0]);
151 if (debug) printf("Total blocks used: %d\n", cb_blocks[0]);
153 if (debug) printf("\n\n");
156 for (i=0;i<nbloquesBR+1;i++)
158 if (i==nbloquesBR)
160 bloque_BR[i].line1=split.basesz;
161 bloque_BR[i].line2=split.remotesz;
162 bloque_BR[i].size=0;
164 if (i>0)
166 int b1Start=bloque_BR[i-1].line1+bloque_BR[i-1].size;
167 int b2Start=bloque_BR[i-1].line2+bloque_BR[i-1].size;
168 int offset1=bloque_BR[i].line1-b1Start;
169 int offset2=bloque_BR[i].line2-b2Start;
170 if (debug) printf("Orphan:");
171 calcBytes(1,
172 split.base[b1Start],
173 split.base[b1Start+offset1]-1,
175 split.remote[b2Start],
176 split.remote[b2Start+offset2]-1
180 if (i<nbloquesBR)
184 calcBytes(1,
185 split.base[bloque_BR[i].line1],
186 split.base[bloque_BR[i].line1+bloque_BR[i].size]-1,
188 split.remote[bloque_BR[i].line2],
189 split.remote[bloque_BR[i].line2+bloque_BR[i].size]-1
195 if (debug) printf("Total bytes deleted: %d\n", cb_deleted[1]);
196 if (debug) printf("Total bytes added: %d\n", cb_added[1]);
197 if (debug) printf("Total blocks used: %d\n", cb_blocks[1]);
199 if (debug) printf("\n\n");
201 int j;
202 for (j=0;j<cb_nhash[0];j++) // Search blocks in local for that code:
204 int Bfrom0=cb_hash[0][j].line1;
205 int Bto0=cb_hash[0][j].line1+cb_hash[0][j].size-1;
207 printf("%d->%d\n",Bfrom0,Bto0);
211 // Sort remote blocks by remote byte count:
213 int j,p;
214 int nbloques=cb_nhash[1];
215 hashblock auxbloque[nbloques];
216 int minline=0, min_j=0;
217 for (p=0;p<nbloques;p++)
219 minline=remotesz;
220 for (j=0;j<nbloques;j++)
222 if (cb_hash[1][j].line2<minline)
224 minline=cb_hash[1][j].line2;
225 min_j=j;
228 auxbloque[p]=cb_hash[1][min_j];
229 cb_hash[1][min_j].line2=remotesz;
231 memcpy(cb_hash[1],auxbloque,nbloques*sizeof(hashblock));
234 int fcursor=0,cursorB=0,cursorA=0;
235 // fwrite(localf,sizeof(char),localsz,out);
236 // fprintf(out,"\n<<<<< LOCAL <<<< \n");
238 for (i=0;i<cb_nhash[1];i++)
240 int from=cb_hash[1][i].line2;
241 int to=cb_hash[1][i].line2+cb_hash[1][i].size-1;
242 if (fcursor<from)
244 int Bfrom=cb_hash[1][i].line1;
245 if (Bfrom<cursorA+15)
247 // Modified data, only on remote. Copy from remote.
248 // fprintf(out,"\n<<<<< REMOTE <<<< %d bytes\n",from-fcursor);
249 if (debug) fprintf(out,"|C%d>",fcursor);
250 fwrite(&remotef[fcursor],sizeof(char),from-fcursor,out);
251 if (debug) fprintf(out,"<C%d|",from-1);
252 // fprintf(out,"\n>>>>> REMOTE >>>>>>>>>>>\n");
254 else
257 if (debug)
259 fprintf(stdout,"\n<C%d<%d$",Bfrom,cursorA);
260 fwrite(&remotef[fcursor],sizeof(char),from-fcursor,stdout);
261 fprintf(stdout,"$C>\n");
265 fcursor=from;
267 if (fcursor>=from)
269 // Original data: read from base, and translate to local.
270 // fprintf(out,"/*");
271 int Bfrom=cb_hash[1][i].line1;
272 int Bto=cb_hash[1][i].line1+cb_hash[1][i].size-1;
273 int j;
274 // printf("**** %d %d->%d\n",i,Bfrom,Bto);
275 int prev=0;
276 for (j=0;j<cb_nhash[0];j++) // Search blocks in local for that code:
278 int Bfrom0=cb_hash[0][j].line1;
279 int Bto0=cb_hash[0][j].line1+cb_hash[0][j].size-1;
281 int f=(Bfrom0>Bfrom)? Bfrom0:Bfrom;
282 int t=(Bto0<Bto)? Bto0:Bto;
284 if (t>f)
286 if (!prev && f>Bfrom)
288 if (debug) fprintf(out,"{Bf%d}",Bfrom);
289 int From2;
290 if (j>0)
292 From2=cb_hash[0][j-1].line2+cb_hash[0][j-1].size;
294 else From2=0;
296 int To2=cb_hash[0][j].line2;
298 if (cursorB>From2) From2=cursorB;
299 if (debug) fprintf(out,"|B%d>",From2);
300 fwrite(&localf[From2],sizeof(char),To2-From2,out);
301 if (debug) fprintf(out,"<B%d|",To2);
302 cursorB=To2;
305 prev=1;
307 if (debug) fprintf(out,"|A%d>",f);
308 fwrite(&basef[f],sizeof(char),t-f,out);
309 if (debug) fprintf(out,"<A%d|",t);
310 cursorA=t;
312 if (t==Bto0 && t<Bto)
314 int From2=cb_hash[0][j].line2+cb_hash[0][j].size-1;
315 int To2=cb_hash[0][j+1].line2;
318 if (cursorB>From2) From2=cursorB;
319 //if (To2-From2>Bto-t) To2=From2+Bto-t;
320 if (debug) fprintf(out,"|b%d>",From2);
321 fwrite(&localf[From2],sizeof(char),To2-From2,out);
322 if (debug) fprintf(out,"<b%d|",To2);
323 cursorB=To2;
326 } else prev=0;
329 // fprintf(out,"*/");
331 fprintf(out,"|C2>");
332 fwrite(&remotef[fcursor],sizeof(char),to-fcursor,out);
333 fprintf(out,"<C2|");*/
334 fcursor=to;
339 // fwrite(ptr,sizeof(char),size,out);
341 return 0;
345 int main(int argc, char **argv) {
346 // Parse the command line args.
347 Cmdline *cmd = parseCmdline(argc, argv);
349 if (cmd->show_helpP) showUsage();
351 if (cmd->be_verboseP) debug=1;
353 if (cmd->do_blrmergeP)
355 FILE *of1;
356 int result;
357 if (cmd->output_fileP)
359 of1=fopen(cmd->output_file,"w");
361 else of1=stdout;
363 result= doBLRMerge( of1,
364 cmd->do_blrmerge[0],
365 cmd->do_blrmerge[1],
366 cmd->do_blrmerge[2]);
368 if (cmd->output_fileP)
370 fclose(of1);
372 return result;
374 if (cmd->test_pluginP)
376 printf("\n Intentando cargar el plugin... \n");
377 if (loadsplitplugin())
379 printf("\n - El plugin se cargó correctamente -\n");
380 return 0;
381 } else
383 printf("\n ** ERROR \n");
384 return 1;
387 if (cmd->do_hashcmpP)
389 int i,b;
390 int nhashes=cmd->do_hashcmpC-1;
391 int block_size=4;
392 unsigned int *int_hash=malloc((nhashes)*sizeof(unsigned int));
393 fprintf( stderr, "nhashes: %d\n",nhashes );
394 for (i=1;i<cmd->do_hashcmpC;i++)
396 int_hash[i-1]=ihash(cmd->do_hashcmp[i]);
399 for (i=0;i<nhashes-block_size+1;i++)
401 for (b=0;b<block_size;b++)
403 printf( "%08X", int_hash[i+b]);
405 printf( "\n");
407 free(int_hash);
408 return 0;
411 printf("* Nothing to do.\n");
412 showUsage();
413 return 1;