CreatePatch.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*-
  2. * Parts of this code are copyright 2003-2005 Colin Percival
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "MemoryCompressor.h"
  27. #if 0
  28. __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bsdiff/bsdiff.c,v 1.1 2005/08/06 01:59:05 cperciva Exp $");
  29. #endif
  30. #include <sys/types.h>
  31. #include "bzlib.h"
  32. #ifndef _WIN32
  33. #include <err.h>
  34. #include <unistd.h>
  35. #else
  36. // KevinJ - Windows compatibility
  37. typedef int ssize_t;
  38. typedef unsigned char u_char;
  39. typedef long off_t;
  40. #include <wchar.h>
  41. #include <io.h>
  42. #define fseeko fseek
  43. #define ftello ftell
  44. static void err(int i, ...)
  45. {
  46. exit(i);
  47. }
  48. static void errx(int i, ...)
  49. {
  50. exit(i);
  51. }
  52. #endif
  53. #include <fcntl.h>
  54. #include <stdio.h>
  55. #include <stdlib.h>
  56. #include <string.h>
  57. #ifndef MIN
  58. #define MIN(x,y) (((x)<(y)) ? (x) : (y))
  59. #endif
  60. #ifndef _O_BINARY
  61. #define _O_BINARY 0
  62. #endif
  63. #ifndef O_BINARY
  64. #define O_BINARY _O_BINARY
  65. #endif
  66. static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h)
  67. {
  68. off_t i,j,k,x,tmp,jj,kk;
  69. if(len<16) {
  70. for(k=start;k<start+len;k+=j) {
  71. j=1;x=V[I[k]+h];
  72. for(i=1;k+i<start+len;i++) {
  73. if(V[I[k+i]+h]<x) {
  74. x=V[I[k+i]+h];
  75. j=0;
  76. };
  77. if(V[I[k+i]+h]==x) {
  78. tmp=I[k+j];I[k+j]=I[k+i];I[k+i]=tmp;
  79. j++;
  80. };
  81. };
  82. for(i=0;i<j;i++) V[I[k+i]]=k+j-1;
  83. if(j==1) I[k]=-1;
  84. };
  85. return;
  86. };
  87. x=V[I[start+len/2]+h];
  88. jj=0;kk=0;
  89. for(i=start;i<start+len;i++) {
  90. if(V[I[i]+h]<x) jj++;
  91. if(V[I[i]+h]==x) kk++;
  92. };
  93. jj+=start;kk+=jj;
  94. i=start;j=0;k=0;
  95. while(i<jj) {
  96. if(V[I[i]+h]<x) {
  97. i++;
  98. } else if(V[I[i]+h]==x) {
  99. tmp=I[i];I[i]=I[jj+j];I[jj+j]=tmp;
  100. j++;
  101. } else {
  102. tmp=I[i];I[i]=I[kk+k];I[kk+k]=tmp;
  103. k++;
  104. };
  105. };
  106. while(jj+j<kk) {
  107. if(V[I[jj+j]+h]==x) {
  108. j++;
  109. } else {
  110. tmp=I[jj+j];I[jj+j]=I[kk+k];I[kk+k]=tmp;
  111. k++;
  112. };
  113. };
  114. if(jj>start) split(I,V,start,jj-start,h);
  115. for(i=0;i<kk-jj;i++) V[I[jj+i]]=kk-1;
  116. if(jj==kk-1) I[jj]=-1;
  117. if(start+len>kk) split(I,V,kk,start+len-kk,h);
  118. }
  119. static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize)
  120. {
  121. off_t buckets[256];
  122. off_t i,h,len;
  123. //for(i=0;i<256;i++) buckets[i]=0;
  124. memset(buckets, 0, sizeof(buckets));
  125. for(i=0;i<oldsize;i++) buckets[old[i]]++;
  126. for(i=1;i<256;i++) buckets[i]+=buckets[i-1];
  127. for(i=255;i>0;i--) buckets[i]=buckets[i-1];
  128. buckets[0]=0;
  129. for(i=0;i<oldsize;i++) I[++buckets[old[i]]]=i;
  130. I[0]=oldsize;
  131. for(i=0;i<oldsize;i++) V[i]=buckets[old[i]];
  132. V[oldsize]=0;
  133. for(i=1;i<256;i++) if(buckets[i]==buckets[i-1]+1) I[buckets[i]]=-1;
  134. I[0]=-1;
  135. for(h=1;I[0]!=-(oldsize+1);h+=h) {
  136. len=0;
  137. for(i=0;i<oldsize+1;) {
  138. if(I[i]<0) {
  139. len-=I[i];
  140. i-=I[i];
  141. } else {
  142. if(len) I[i-len]=-len;
  143. len=V[I[i]]+1-i;
  144. split(I,V,i,len,h);
  145. i+=len;
  146. len=0;
  147. };
  148. };
  149. if(len) I[i-len]=-len;
  150. };
  151. for(i=0;i<oldsize+1;i++) I[V[i]]=i;
  152. }
  153. static off_t matchlen(u_char *old,off_t oldsize,u_char *_new,off_t newsize)
  154. {
  155. off_t i;
  156. for(i=0;(i<oldsize)&&(i<newsize);i++)
  157. if(old[i]!=_new[i]) break;
  158. return i;
  159. }
  160. static off_t search(off_t *I,u_char *old,off_t oldsize,
  161. u_char *_new,off_t newsize,off_t st,off_t en,off_t *pos)
  162. {
  163. off_t x,y;
  164. if(en-st<2) {
  165. x=matchlen(old+I[st],oldsize-I[st],_new,newsize);
  166. y=matchlen(old+I[en],oldsize-I[en],_new,newsize);
  167. if(x>y) {
  168. *pos=I[st];
  169. return x;
  170. } else {
  171. *pos=I[en];
  172. return y;
  173. }
  174. };
  175. x=st+(en-st)/2;
  176. if(memcmp(old+I[x],_new,MIN(oldsize-I[x],newsize))<0) {
  177. return search(I,old,oldsize,_new,newsize,x,en,pos);
  178. } else {
  179. return search(I,old,oldsize,_new,newsize,st,x,pos);
  180. };
  181. }
  182. static void offtout(off_t x,u_char *buf)
  183. {
  184. off_t y;
  185. if(x<0) y=-x; else y=x;
  186. /*
  187. buf[0]=y%256;y-=buf[0];
  188. y=y/256;buf[1]=y%256;y-=buf[1];
  189. y=y/256;buf[2]=y%256;y-=buf[2];
  190. y=y/256;buf[3]=y%256;y-=buf[3];
  191. y=y/256;buf[4]=y%256;y-=buf[4];
  192. y=y/256;buf[5]=y%256;y-=buf[5];
  193. y=y/256;buf[6]=y%256;y-=buf[6];
  194. y=y/256;buf[7]=y%256;
  195. */
  196. // Thanks to Oliver Smith for pointing out this optimization
  197. buf[0] = (u_char)(y&(off_t)0x000000ff); y >>= 8 ;
  198. buf[1] = (u_char)(y&(off_t)0x000000ff); y >>= 8 ;
  199. buf[2] = (u_char)(y&(off_t)0x000000ff); y >>= 8 ;
  200. buf[3] = (u_char)(y&(off_t)0x000000ff); y >>= 8 ;
  201. buf[4] = (u_char)(y&(off_t)0x000000ff); y >>= 8 ;
  202. buf[5] = (u_char)(y&(off_t)0x000000ff); y >>= 8 ;
  203. buf[6] = (u_char)(y&(off_t)0x000000ff); y >>= 8 ;
  204. buf[7] = (u_char)(y&(off_t)0x000000ff);// y >>= 8 ;
  205. if(x<0) buf[7]|=0x80;
  206. }
  207. // This function modifies the main() function included in bsdiff.c of bsdiff-4.3 found at http://www.daemonology.net/bsdiff/
  208. // It is changed to be a standalone function, to work entirely in memory, and to use my class MemoryCompressor as an interface to BZip
  209. // Up to the caller to delete out
  210. bool CreatePatch(const char *old, unsigned oldsize, char *_new, unsigned int newsize, char **out, unsigned *outSize)
  211. {
  212. // int fd;
  213. // u_char *old,*new;
  214. // off_t oldsize,newsize;
  215. off_t *I,*V;
  216. off_t scan,pos,len;
  217. off_t lastscan,lastpos,lastoffset;
  218. off_t oldscore,scsc;
  219. off_t s,Sf,lenf,Sb,lenb;
  220. off_t overlap,Ss,lens;
  221. off_t i;
  222. off_t dblen,eblen;
  223. u_char *db,*eb;
  224. u_char buf[8];
  225. u_char header[32];
  226. MemoryCompressor patch;
  227. // unsigned outWriteOffset;
  228. // FILE * pf;
  229. // BZFILE * pfbz2;
  230. // int bz2err;
  231. // if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
  232. /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
  233. that we never try to malloc(0) and get a NULL pointer */
  234. /*
  235. if(((fd=open(argv[1],O_RDONLY | _O_BINARY ,0))<0) ||
  236. ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  237. ((old=malloc(oldsize+1))==NULL) ||
  238. (lseek(fd,0,SEEK_SET)!=0) ||
  239. (read(fd,old,oldsize)!=oldsize) ||
  240. (close(fd)==-1)) err(1,"%s",argv[1]);
  241. */
  242. if(((I=(off_t*)malloc((oldsize+1)*sizeof(off_t)))==NULL) ||
  243. ((V=(off_t*)malloc((oldsize+1)*sizeof(off_t)))==NULL))
  244. // err(1,NULL);
  245. return false;
  246. qsufsort(I,V,(u_char*)old,oldsize);
  247. free(V);
  248. /* Allocate newsize+1 bytes instead of newsize bytes to ensure
  249. that we never try to malloc(0) and get a NULL pointer */
  250. /*
  251. if(((fd=open(argv[2],O_RDONLY | _O_BINARY ,0))<0) ||
  252. ((newsize=lseek(fd,0,SEEK_END))==-1) ||
  253. ((new=malloc(newsize+1))==NULL) ||
  254. (lseek(fd,0,SEEK_SET)!=0) ||
  255. (read(fd,new,newsize)!=newsize) ||
  256. (close(fd)==-1)) err(1,"%s",argv[2]);
  257. */
  258. if(((db=(u_char*)malloc(newsize+1))==NULL) ||
  259. ((eb=(u_char*)malloc(newsize+1))==NULL))
  260. // err(1,NULL);
  261. {
  262. free(I);
  263. return false;
  264. }
  265. dblen=0;
  266. eblen=0;
  267. /* Create the patch file */
  268. // if ((pf = fopen(argv[3], "wb")) == NULL)
  269. // err(1, "%s", argv[3]);
  270. /* Header is
  271. 0 8 "BSDIFF40"
  272. 8 8 length of bzip2ed ctrl block
  273. 16 8 length of bzip2ed diff block
  274. 24 8 length of new file */
  275. /* File is
  276. 0 32 Header
  277. 32 ?? Bzip2ed ctrl block
  278. ?? ?? Bzip2ed diff block
  279. ?? ?? Bzip2ed extra block */
  280. memcpy(header,"BSDIFF40",8);
  281. offtout(0, header + 8);
  282. offtout(0, header + 16);
  283. offtout(newsize, header + 24);
  284. // if (fwrite(header, 32, 1, pf) != 1)
  285. // err(1, "fwrite(%s)", argv[3]);
  286. // Allocate enough to hold any output
  287. // *out = (char*) malloc(oldsize+newsize);
  288. // Copy out the header
  289. // memcpy(*out, header, 32);
  290. //outWriteOffset=32;
  291. /* Compute the differences, writing ctrl as we go */
  292. // if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
  293. // errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
  294. scan=0;len=0;
  295. lastscan=0;lastpos=0;lastoffset=0;
  296. while(scan<newsize) {
  297. oldscore=0;
  298. for(scsc=scan+=len;scan<newsize;scan++) {
  299. len=search(I,(u_char*)old,oldsize,(u_char*)_new+scan,newsize-scan,
  300. 0,oldsize,&pos);
  301. for(;scsc<scan+len;scsc++)
  302. if((scsc+lastoffset<oldsize) &&
  303. (old[scsc+lastoffset] == _new[scsc]))
  304. oldscore++;
  305. if(((len==oldscore) && (len!=0)) ||
  306. (len>oldscore+8)) break;
  307. if((scan+lastoffset<oldsize) &&
  308. (old[scan+lastoffset] == _new[scan]))
  309. oldscore--;
  310. };
  311. if((len!=oldscore) || (scan==newsize)) {
  312. s=0;Sf=0;lenf=0;
  313. for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
  314. if(old[lastpos+i]==_new[lastscan+i]) s++;
  315. i++;
  316. if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
  317. };
  318. lenb=0;
  319. if(scan<newsize) {
  320. s=0;Sb=0;
  321. for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
  322. if(old[pos-i]==_new[scan-i]) s++;
  323. if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
  324. };
  325. };
  326. if(lastscan+lenf>scan-lenb) {
  327. overlap=(lastscan+lenf)-(scan-lenb);
  328. s=0;Ss=0;lens=0;
  329. for(i=0;i<overlap;i++) {
  330. if(_new[lastscan+lenf-overlap+i]==
  331. old[lastpos+lenf-overlap+i]) s++;
  332. if(_new[scan-lenb+i]==
  333. old[pos-lenb+i]) s--;
  334. if(s>Ss) { Ss=s; lens=i+1; };
  335. };
  336. lenf+=lens-overlap;
  337. lenb-=lens;
  338. };
  339. for(i=0;i<lenf;i++)
  340. db[dblen+i]=_new[lastscan+i]-old[lastpos+i];
  341. for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
  342. eb[eblen+i]=_new[lastscan+lenf+i];
  343. dblen+=lenf;
  344. eblen+=(scan-lenb)-(lastscan+lenf);
  345. offtout(lenf,buf);
  346. if (patch.Compress((char*)buf, 8, false)==false)
  347. {
  348. free(db);
  349. free(eb);
  350. free(I);
  351. return false;
  352. }
  353. //BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
  354. //if (bz2err != BZ_OK)
  355. // errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  356. offtout((scan-lenb)-(lastscan+lenf),buf);
  357. if (patch.Compress((char*)buf, 8, false)==false)
  358. {
  359. free(db);
  360. free(eb);
  361. free(I);
  362. return false;
  363. }
  364. //BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
  365. //if (bz2err != BZ_OK)
  366. // errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  367. offtout((pos-lenb)-(lastpos+lenf),buf);
  368. if (patch.Compress((char*)buf, 8, false)==false)
  369. {
  370. free(db);
  371. free(eb);
  372. free(I);
  373. return false;
  374. }
  375. //BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
  376. //if (bz2err != BZ_OK)
  377. // errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  378. lastscan=scan-lenb;
  379. lastpos=pos-lenb;
  380. lastoffset=pos-scan;
  381. };
  382. };
  383. // BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
  384. // if (bz2err != BZ_OK)
  385. // errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
  386. /* Compute size of compressed ctrl data */
  387. // if ((len = ftello(pf)) == -1)
  388. // err(1, "ftello");
  389. if (patch.Compress(0,0,true)==false)
  390. {
  391. free(db);
  392. free(eb);
  393. free(I);
  394. return false;
  395. }
  396. len=patch.GetTotalOutputSize()+32; // test: len should be 188
  397. offtout(len-32, header + 8);
  398. //memcpy(*out+outWriteOffset, patch.GetOutput(), patch.GetTotalOutputSize());
  399. //outWriteOffset+=patch.GetTotalOutputSize();
  400. //patch.Clear(_FILE_AND_LINE_);
  401. /* Write compressed diff data */
  402. // if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
  403. // errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
  404. // BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
  405. if (patch.Compress((char*)db,dblen,true)==false)
  406. {
  407. free(db);
  408. free(eb);
  409. free(I);
  410. return false;
  411. }
  412. // memcpy(*out+outWriteOffset, patch.GetOutput(), patch.GetTotalOutputSize());
  413. // outWriteOffset+=patch.GetTotalOutputSize();
  414. // patch.Clear(_FILE_AND_LINE_);
  415. // if (bz2err != BZ_OK)
  416. // errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  417. // BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
  418. // if (bz2err != BZ_OK)
  419. // errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
  420. /* Compute size of compressed diff data */
  421. // if ((newsize = ftello(pf)) == -1)
  422. // err(1, "ftello");
  423. newsize=32+patch.GetTotalOutputSize();
  424. offtout(newsize - len, header + 16);
  425. // memcpy(*out+outWriteOffset, patch.GetOutput(), patch.GetTotalOutputSize());
  426. // outWriteOffset+=patch.GetTotalOutputSize();
  427. // patch.Clear(_FILE_AND_LINE_);
  428. /* Write compressed extra data */
  429. // if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
  430. // errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
  431. // BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
  432. if (patch.Compress((char*)eb,eblen,true)==false)
  433. {
  434. free(db);
  435. free(eb);
  436. free(I);
  437. return false;
  438. }
  439. // memcpy(*out+outWriteOffset, patch.GetOutput(), patch.GetTotalOutputSize());
  440. // outWriteOffset+=patch.GetTotalOutputSize();
  441. // patch.Clear(_FILE_AND_LINE_);
  442. // if (bz2err != BZ_OK)
  443. // errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  444. // BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
  445. // if (bz2err != BZ_OK)
  446. // errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
  447. /* Seek to the beginning, write the header, and close the file */
  448. // if (fseeko(pf, 0, SEEK_SET))
  449. // err(1, "fseeko");
  450. // if (fwrite(header, 32, 1, pf) != 1)
  451. // err(1, "fwrite(%s)", argv[3]);
  452. // if (fclose(pf))
  453. // err(1, "fclose");
  454. // memcpy(*out,header,32);
  455. // *out=(char*) realloc(*out, outWriteOffset);
  456. // *outSize=outWriteOffset;
  457. *outSize=patch.GetTotalOutputSize()+32;
  458. *out = new char [*outSize];
  459. memcpy(*out, header, 32);
  460. memcpy(*out+32, patch.GetOutput(), patch.GetTotalOutputSize());
  461. /* Free the memory we used */
  462. free(db);
  463. free(eb);
  464. free(I);
  465. //free(old);
  466. //free(new);
  467. return true;
  468. }
  469. int TestDiffInMemory(int argc,char *argv[])
  470. {
  471. char *old;
  472. off_t oldsize;
  473. char *_new;
  474. off_t newsize;
  475. char *out;
  476. unsigned outSize;
  477. if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
  478. int fd;
  479. FILE * pf;
  480. if(((fd=open(argv[1],O_RDONLY | _O_BINARY ,0))<0) ||
  481. ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  482. ((old=(char*)malloc(oldsize+1))==NULL) ||
  483. (lseek(fd,0,SEEK_SET)!=0) ||
  484. (read(fd,old,oldsize)!=oldsize) ||
  485. (close(fd)==-1)) err(1,"%s",argv[1]);
  486. if(((fd=open(argv[2],O_RDONLY | _O_BINARY ,0))<0) ||
  487. ((newsize=lseek(fd,0,SEEK_END))==-1) ||
  488. ((_new=(char*)malloc(newsize+1))==NULL) ||
  489. (lseek(fd,0,SEEK_SET)!=0) ||
  490. (read(fd,_new,newsize)!=newsize) ||
  491. (close(fd)==-1)) err(1,"%s",argv[2]);
  492. int res = CreatePatch(old, oldsize, _new, newsize, &out, &outSize);
  493. if ((pf = fopen(argv[3], "wb")) == NULL)
  494. err(1, "%s", argv[3]);
  495. fwrite(out,outSize,1,pf);
  496. fclose(pf);
  497. delete[] out;
  498. return res;
  499. }
  500. int DIFF_main(int argc,char *argv[])
  501. {
  502. int fd;
  503. u_char *old,*_new;
  504. off_t oldsize,newsize;
  505. off_t *I,*V;
  506. off_t scan,pos,len;
  507. off_t lastscan,lastpos,lastoffset;
  508. off_t oldscore,scsc;
  509. off_t s,Sf,lenf,Sb,lenb;
  510. off_t overlap,Ss,lens;
  511. off_t i;
  512. off_t dblen,eblen;
  513. u_char *db,*eb;
  514. u_char buf[8];
  515. u_char header[32];
  516. FILE * pf;
  517. BZFILE * pfbz2;
  518. int bz2err;
  519. int bytesWritten=0;
  520. if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
  521. /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
  522. that we never try to malloc(0) and get a NULL pointer */
  523. if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) ||
  524. ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  525. ((old=(u_char*)malloc(oldsize+1))==NULL) ||
  526. (lseek(fd,0,SEEK_SET)!=0) ||
  527. (read(fd,old,oldsize)!=oldsize) ||
  528. (close(fd)==-1)) err(1,"%s",argv[1]);
  529. if(((I=(off_t*)malloc((oldsize+1)*sizeof(off_t)))==NULL) ||
  530. ((V=(off_t*)malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL);
  531. qsufsort(I,V,old,oldsize);
  532. free(V);
  533. /* Allocate newsize+1 bytes instead of newsize bytes to ensure
  534. that we never try to malloc(0) and get a NULL pointer */
  535. if(((fd=open(argv[2],O_RDONLY|O_BINARY,0))<0) ||
  536. ((newsize=lseek(fd,0,SEEK_END))==-1) ||
  537. ((_new=(u_char*)malloc(newsize+1))==NULL) ||
  538. (lseek(fd,0,SEEK_SET)!=0) ||
  539. (read(fd,_new,newsize)!=newsize) ||
  540. (close(fd)==-1)) err(1,"%s",argv[2]);
  541. if(((db=(u_char*)malloc(newsize+1))==NULL) ||
  542. ((eb=(u_char*)malloc(newsize+1))==NULL)) err(1,NULL);
  543. dblen=0;
  544. eblen=0;
  545. /* Create the patch file */
  546. if ((pf = fopen(argv[3], "wb")) == NULL)
  547. err(1, "%s", argv[3]);
  548. /* Header is
  549. 0 8 "BSDIFF40"
  550. 8 8 length of bzip2ed ctrl block
  551. 16 8 length of bzip2ed diff block
  552. 24 8 length of new file */
  553. /* File is
  554. 0 32 Header
  555. 32 ?? Bzip2ed ctrl block
  556. ?? ?? Bzip2ed diff block
  557. ?? ?? Bzip2ed extra block */
  558. memcpy(header,"BSDIFF40",8);
  559. offtout(0, header + 8);
  560. offtout(0, header + 16);
  561. offtout(newsize, header + 24);
  562. if (fwrite(header, 32, 1, pf) != 1)
  563. err(1, "fwrite(%s)", argv[3]);
  564. /* Compute the differences, writing ctrl as we go */
  565. if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
  566. errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
  567. scan=0;len=0;
  568. lastscan=0;lastpos=0;lastoffset=0;
  569. while(scan<newsize) {
  570. oldscore=0;
  571. for(scsc=scan+=len;scan<newsize;scan++) {
  572. len=search(I,old,oldsize,_new+scan,newsize-scan,
  573. 0,oldsize,&pos);
  574. for(;scsc<scan+len;scsc++)
  575. if((scsc+lastoffset<oldsize) &&
  576. (old[scsc+lastoffset] == _new[scsc]))
  577. oldscore++;
  578. if(((len==oldscore) && (len!=0)) ||
  579. (len>oldscore+8)) break;
  580. if((scan+lastoffset<oldsize) &&
  581. (old[scan+lastoffset] == _new[scan]))
  582. oldscore--;
  583. };
  584. if((len!=oldscore) || (scan==newsize)) {
  585. s=0;Sf=0;lenf=0;
  586. for(i=0;(lastscan+i<scan)&&(lastpos+i<oldsize);) {
  587. if(old[lastpos+i]==_new[lastscan+i]) s++;
  588. i++;
  589. if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; };
  590. };
  591. lenb=0;
  592. if(scan<newsize) {
  593. s=0;Sb=0;
  594. for(i=1;(scan>=lastscan+i)&&(pos>=i);i++) {
  595. if(old[pos-i]==_new[scan-i]) s++;
  596. if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; };
  597. };
  598. };
  599. if(lastscan+lenf>scan-lenb) {
  600. overlap=(lastscan+lenf)-(scan-lenb);
  601. s=0;Ss=0;lens=0;
  602. for(i=0;i<overlap;i++) {
  603. if(_new[lastscan+lenf-overlap+i]==
  604. old[lastpos+lenf-overlap+i]) s++;
  605. if(_new[scan-lenb+i]==
  606. old[pos-lenb+i]) s--;
  607. if(s>Ss) { Ss=s; lens=i+1; };
  608. };
  609. lenf+=lens-overlap;
  610. lenb-=lens;
  611. };
  612. for(i=0;i<lenf;i++)
  613. db[dblen+i]=_new[lastscan+i]-old[lastpos+i];
  614. for(i=0;i<(scan-lenb)-(lastscan+lenf);i++)
  615. eb[eblen+i]=_new[lastscan+lenf+i];
  616. dblen+=lenf;
  617. eblen+=(scan-lenb)-(lastscan+lenf);
  618. offtout(lenf,buf);
  619. BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
  620. if (bz2err != BZ_OK)
  621. errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  622. bytesWritten+=8;
  623. // printf("bz2err 8 %i\n", bytesWritten);
  624. offtout((scan-lenb)-(lastscan+lenf),buf);
  625. BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
  626. if (bz2err != BZ_OK)
  627. errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  628. bytesWritten+=8;
  629. // printf("bz2err 8 %i\n", bytesWritten);
  630. offtout((pos-lenb)-(lastpos+lenf),buf);
  631. BZ2_bzWrite(&bz2err, pfbz2, buf, 8);
  632. if (bz2err != BZ_OK)
  633. errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  634. bytesWritten+=8;
  635. // printf("bz2err 8 %i\n", bytesWritten);
  636. lastscan=scan-lenb;
  637. lastpos=pos-lenb;
  638. lastoffset=pos-scan;
  639. };
  640. };
  641. BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
  642. if (bz2err != BZ_OK)
  643. errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
  644. /* Compute size of compressed ctrl data */
  645. if ((len = ftello(pf)) == -1)
  646. err(1, "ftello");
  647. offtout(len-32, header + 8);
  648. /* Write compressed diff data */
  649. if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
  650. errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
  651. BZ2_bzWrite(&bz2err, pfbz2, db, dblen);
  652. bytesWritten+=dblen;
  653. // printf("bz2err dblen %i %i\n", dblen, bytesWritten);
  654. if (bz2err != BZ_OK)
  655. errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  656. BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
  657. if (bz2err != BZ_OK)
  658. errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
  659. /* Compute size of compressed diff data */
  660. if ((newsize = ftello(pf)) == -1)
  661. err(1, "ftello");
  662. offtout(newsize - len, header + 16);
  663. /* Write compressed extra data */
  664. if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
  665. errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err);
  666. BZ2_bzWrite(&bz2err, pfbz2, eb, eblen);
  667. if (bz2err != BZ_OK)
  668. errx(1, "BZ2_bzWrite, bz2err = %d", bz2err);
  669. bytesWritten+=eblen;
  670. //printf("bz2err eblen %i %i\n", eblen, bytesWritten);
  671. BZ2_bzWriteClose(&bz2err, pfbz2, 0, NULL, NULL);
  672. if (bz2err != BZ_OK)
  673. errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
  674. // REMOVEME
  675. // if ((newsize = ftello(pf)) == -1)
  676. // err(1, "ftello");
  677. /* Seek to the beginning, write the header, and close the file */
  678. if (fseeko(pf, 0, SEEK_SET))
  679. err(1, "fseeko");
  680. if (fwrite(header, 32, 1, pf) != 1)
  681. err(1, "fwrite(%s)", argv[3]);
  682. if (fclose(pf))
  683. err(1, "fclose");
  684. /* Free the memory we used */
  685. free(db);
  686. free(eb);
  687. free(I);
  688. free(old);
  689. free(_new);
  690. return 0;
  691. }
粤ICP备19079148号