Debug output disabled by default.
[mx3r.git] / mx3r.c
blobcce1b6d917b65322c22a8c91a804ee1ed6976cef
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 32,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;
72 if (debug) printf("-%d +%d (%d)*\n",
73 baseTo-baseFrom-totalsize1,
74 localTo-localFrom-totalsize1,nhbchar);
76 cb_nhash[nfile]+=nhbchar;
78 // Estadísticas:
79 cb_blocks[nfile]+=nhbchar;
80 cb_deleted[nfile]+=baseTo-baseFrom-totalsize1;
81 cb_added[nfile]+=localTo-localFrom-totalsize1;
86 int doBLRMerge(FILE *out, char *base, char *local, char *remote)
88 BLRintA split=splitBLR(base, local, remote);
90 if (debug) fprintf( stderr, "Base File: %d blocks found\n",split.basesz);
91 if (debug) fprintf( stderr, "Local File: %d blocks found\n",split.localsz);
92 if (debug) fprintf( stderr, "Remote File: %d blocks found\n",split.remotesz);
94 BLRintA hash=hashBLR();
96 hashblock bloque_BR[64];
97 int nbloquesBR = compare2hashvectors(
98 hash.base, hash.basesz,
99 hash.remote, hash.remotesz,
100 4, 256,
101 bloque_BR, 64);
103 hashblock bloque_BL[64];
104 int nbloquesBL = compare2hashvectors(
105 hash.base, hash.basesz,
106 hash.local, hash.localsz,
107 4, 256,
108 bloque_BL, 64);
110 int i;
111 for (i=0;i<nbloquesBL+1;i++)
113 if (i==nbloquesBL)
115 bloque_BL[i].line1=split.basesz;
116 bloque_BL[i].line2=split.localsz;
117 bloque_BL[i].size=0;
119 if (i>0)
121 int b1Start=bloque_BL[i-1].line1+bloque_BL[i-1].size;
122 int b2Start=bloque_BL[i-1].line2+bloque_BL[i-1].size;
123 int offset1=bloque_BL[i].line1-b1Start;
124 int offset2=bloque_BL[i].line2-b2Start;
126 calcBytes(0,
127 split.base[b1Start],
128 split.base[b1Start+offset1-1],
129 split.local[b2Start],
130 split.local[b2Start+offset2-1]);
133 if (i<nbloquesBL)
136 calcBytes(0,
137 split.base[bloque_BL[i].line1],
138 split.base[bloque_BL[i].line1+bloque_BL[i].size-1],
140 split.local[bloque_BL[i].line2],
141 split.local[bloque_BL[i].line2+bloque_BL[i].size-1]
147 if (debug) printf("Total bytes deleted: %d\n", cb_deleted[0]);
148 if (debug) printf("Total bytes added: %d\n", cb_added[0]);
149 if (debug) printf("Total blocks used: %d\n", cb_blocks[0]);
151 if (debug) printf("\n\n");
154 for (i=0;i<nbloquesBR+1;i++)
156 if (i==nbloquesBR)
158 bloque_BR[i].line1=split.basesz;
159 bloque_BR[i].line2=split.remotesz;
160 bloque_BR[i].size=0;
162 if (i>0)
164 int b1Start=bloque_BR[i-1].line1+bloque_BR[i-1].size;
165 int b2Start=bloque_BR[i-1].line2+bloque_BR[i-1].size;
166 int offset1=bloque_BR[i].line1-b1Start;
167 int offset2=bloque_BR[i].line2-b2Start;
168 if (debug) printf("\n Orphan:");
169 calcBytes(1,
170 split.base[b1Start],
171 split.base[b1Start+offset1-1],
173 split.remote[b2Start],
174 split.remote[b2Start+offset2-1]
178 if (i<nbloquesBR)
182 calcBytes(1,
183 split.base[bloque_BR[i].line1],
184 split.base[bloque_BR[i].line1+bloque_BR[i].size-1],
186 split.remote[bloque_BR[i].line2],
187 split.remote[bloque_BR[i].line2+bloque_BR[i].size-1]
193 if (debug) printf("Total bytes deleted: %d\n", cb_deleted[1]);
194 if (debug) printf("Total bytes added: %d\n", cb_added[1]);
195 if (debug) printf("Total blocks used: %d\n", cb_blocks[1]);
197 if (debug) printf("\n\n");
199 int j;
200 for (j=0;j<cb_nhash[0];j++) // Search blocks in local for that code:
202 int Bfrom0=cb_hash[0][j].line1;
203 int Bto0=cb_hash[0][j].line1+cb_hash[0][j].size-1;
205 printf("%d->%d\n",Bfrom0,Bto0);
209 // Sort remote blocks by remote byte count:
211 int j,p;
212 int nbloques=cb_nhash[1];
213 hashblock auxbloque[nbloques];
214 int minline=0, min_j=0;
215 for (p=0;p<nbloques;p++)
217 minline=remotesz;
218 for (j=0;j<nbloques;j++)
220 if (cb_hash[1][j].line2<minline)
222 minline=cb_hash[1][j].line2;
223 min_j=j;
226 auxbloque[p]=cb_hash[1][min_j];
227 cb_hash[1][min_j].line2=remotesz;
229 memcpy(cb_hash[1],auxbloque,nbloques*sizeof(hashblock));
232 int fcursor=0;
233 // fwrite(localf,sizeof(char),localsz,out);
234 // fprintf(out,"\n<<<<< LOCAL <<<< \n");
236 for (i=0;i<cb_nhash[1];i++)
238 int from=cb_hash[1][i].line2;
239 int to=cb_hash[1][i].line2+cb_hash[1][i].size-1;
240 if (fcursor<from)
242 // Modified data, only on remote. Copy from remote.
243 // fprintf(out,"\n<<<<< REMOTE <<<< %d bytes\n",from-fcursor);
244 if (debug) fprintf(out,"|C>");
245 fwrite(&remotef[fcursor],sizeof(char),from-fcursor,out);
246 if (debug) fprintf(out,"<C|");
247 // fprintf(out,"\n>>>>> REMOTE >>>>>>>>>>>\n");
249 fcursor=from;
251 if (fcursor>=from)
253 // Original data: read from base, and translate to local.
254 // fprintf(out,"/*");
255 int Bfrom=cb_hash[1][i].line1;
256 int Bto=cb_hash[1][i].line1+cb_hash[1][i].size-1;
257 int j;
258 // printf("**** %d %d->%d\n",i,Bfrom,Bto);
259 for (j=0;j<cb_nhash[0];j++) // Search blocks in local for that code:
261 int Bfrom0=cb_hash[0][j].line1;
262 int Bto0=cb_hash[0][j].line1+cb_hash[0][j].size-1;
264 int f=(Bfrom0>Bfrom)? Bfrom0:Bfrom;
265 int t=(Bto0<Bto)? Bto0:Bto;
267 if (t>f)
269 if (debug) fprintf(out,"|A>");
270 fwrite(&basef[f],sizeof(char),t-f,out);
271 if (debug) fprintf(out,"<A|");
273 if (t==Bto0 && t<Bto)
275 int From2=cb_hash[0][j].line2+cb_hash[0][j].size-1;
276 int To2=cb_hash[0][j+1].line2;
278 if (debug) fprintf(out,"|B>");
279 fwrite(&localf[From2],sizeof(char),To2-From2,out);
280 if (debug) fprintf(out,"<B|");
286 // fprintf(out,"*/");
288 fprintf(out,"|C2>");
289 fwrite(&remotef[fcursor],sizeof(char),to-fcursor,out);
290 fprintf(out,"<C2|");*/
292 fcursor=to;
297 // fwrite(ptr,sizeof(char),size,out);
299 return 0;
303 int main(int argc, char **argv) {
304 // Parse the command line args.
305 Cmdline *cmd = parseCmdline(argc, argv);
307 if (cmd->show_helpP) showUsage();
309 if (cmd->do_blrmergeP)
311 FILE *of1;
312 int result;
313 if (cmd->output_fileP)
315 of1=fopen(cmd->output_file,"w");
317 else of1=stdout;
319 result= doBLRMerge( of1,
320 cmd->do_blrmerge[0],
321 cmd->do_blrmerge[1],
322 cmd->do_blrmerge[2]);
324 if (cmd->output_fileP)
326 fclose(of1);
328 return result;
330 if (cmd->test_pluginP)
332 printf("\n Intentando cargar el plugin... \n");
333 if (loadsplitplugin())
335 printf("\n - El plugin se cargó correctamente -\n");
336 return 0;
337 } else
339 printf("\n ** ERROR \n");
340 return 1;
343 if (cmd->do_hashcmpP)
345 int i,b;
346 int nhashes=cmd->do_hashcmpC-1;
347 int block_size=4;
348 unsigned int *int_hash=malloc((nhashes)*sizeof(unsigned int));
349 fprintf( stderr, "nhashes: %d\n",nhashes );
350 for (i=1;i<cmd->do_hashcmpC;i++)
352 int_hash[i-1]=ihash(cmd->do_hashcmp[i]);
355 for (i=0;i<nhashes-block_size+1;i++)
357 for (b=0;b<block_size;b++)
359 printf( "%08X", int_hash[i+b]);
361 printf( "\n");
363 free(int_hash);
364 return 0;
367 printf("* Nothing to do.\n");
368 showUsage();
369 return 1;