2 Prueba de concepto de buscador de diferencias por bloques.
9 // CLIG - Command Line Interpreter Generator:
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
];
42 localFrom
=&localf
[lFrom
];
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
,
61 hbchar
, 512-cb_nhash
[nfile
]);
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
;
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
,
104 hashblock bloque_BL
[64];
105 int nbloquesBL
= compare2hashvectors(
106 hash
.base
, hash
.basesz
,
107 hash
.local
, hash
.localsz
,
112 for (i
=0;i
<nbloquesBL
+1;i
++)
116 bloque_BL
[i
].line1
=split
.basesz
;
117 bloque_BL
[i
].line2
=split
.localsz
;
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:");
130 split
.base
[b1Start
+offset1
]-1,
131 split
.local
[b2Start
],
132 split
.local
[b2Start
+offset2
]-1);
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
++)
160 bloque_BR
[i
].line1
=split
.basesz
;
161 bloque_BR
[i
].line2
=split
.remotesz
;
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:");
173 split
.base
[b1Start
+offset1
]-1,
175 split
.remote
[b2Start
],
176 split
.remote
[b2Start
+offset2
]-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");
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:
214 int nbloques
=cb_nhash
[1];
215 hashblock auxbloque
[nbloques
];
216 int minline
=0, min_j
=0;
217 for (p
=0;p
<nbloques
;p
++)
220 for (j
=0;j
<nbloques
;j
++)
222 if (cb_hash
[1][j
].line2
<minline
)
224 minline
=cb_hash
[1][j
].line2
;
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;
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");
259 fprintf(stdout
,"\n<C%d<%d$",Bfrom
,cursorA
);
260 fwrite(&remotef
[fcursor
],sizeof(char),from
-fcursor
,stdout
);
261 fprintf(stdout
,"$C>\n");
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;
274 // printf("**** %d %d->%d\n",i,Bfrom,Bto);
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
;
286 if (!prev
&& f
>Bfrom
)
288 if (debug
) fprintf(out
,"{Bf%d}",Bfrom
);
292 From2
=cb_hash
[0][j
-1].line2
+cb_hash
[0][j
-1].size
;
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
);
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
);
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
);
329 // fprintf(out,"*/");
332 fwrite(&remotef[fcursor],sizeof(char),to-fcursor,out);
333 fprintf(out,"<C2|");*/
339 // fwrite(ptr,sizeof(char),size,out);
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
)
357 if (cmd
->output_fileP
)
359 of1
=fopen(cmd
->output_file
,"w");
363 result
= doBLRMerge( of1
,
366 cmd
->do_blrmerge
[2]);
368 if (cmd
->output_fileP
)
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");
383 printf("\n ** ERROR \n");
387 if (cmd
->do_hashcmpP
)
390 int nhashes
=cmd
->do_hashcmpC
-1;
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
]);
411 printf("* Nothing to do.\n");