2 * Copyright 2003-2005 Colin Percival
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted providing that the following conditions
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.
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.
28 __FBSDID("$FreeBSD: src/usr.bin/bsdiff/bspatch/bspatch.c,v 1.1 2005/08/06 01:59:06 cperciva Exp $");
39 static off_t offtin(u_char *buf)
57 int main(int argc,char * argv[])
59 FILE * f, * cpf, * dpf, * epf;
60 BZFILE * cpfbz2, * dpfbz2, * epfbz2;
61 int cbz2err, dbz2err, ebz2err;
63 ssize_t oldsize,newsize;
64 ssize_t bzctrllen,bzdatalen;
65 u_char header[32],buf[8];
72 if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
75 if ((f = fopen(argv[3], "r")) == NULL)
76 err(1, "fopen(%s)", argv[3]);
84 32 X bzip2(control block)
85 32+X Y bzip2(diff block)
86 32+X+Y ??? bzip2(extra block)
87 with control block a set of triples (x,y,z) meaning "add x bytes
88 from oldfile to x bytes from the diff block; copy y bytes from the
89 extra block; seek forwards in oldfile by z bytes".
93 if (fread(header, 1, 32, f) < 32) {
95 errx(1, "Corrupt patch\n");
96 err(1, "fread(%s)", argv[3]);
99 /* Check for appropriate magic */
100 if (memcmp(header, "BSDIFF40", 8) != 0)
101 errx(1, "Corrupt patch\n");
103 /* Read lengths from header */
104 bzctrllen=offtin(header+8);
105 bzdatalen=offtin(header+16);
106 newsize=offtin(header+24);
107 if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
108 errx(1,"Corrupt patch\n");
110 /* Close patch file and re-open it via libbzip2 at the right places */
112 err(1, "fclose(%s)", argv[3]);
113 if ((cpf = fopen(argv[3], "r")) == NULL)
114 err(1, "fopen(%s)", argv[3]);
115 if (fseeko(cpf, 32, SEEK_SET))
116 err(1, "fseeko(%s, %lld)", argv[3],
118 if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
119 errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
120 if ((dpf = fopen(argv[3], "r")) == NULL)
121 err(1, "fopen(%s)", argv[3]);
122 if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
123 err(1, "fseeko(%s, %lld)", argv[3],
124 (long long)(32 + bzctrllen));
125 if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
126 errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
127 if ((epf = fopen(argv[3], "r")) == NULL)
128 err(1, "fopen(%s)", argv[3]);
129 if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
130 err(1, "fseeko(%s, %lld)", argv[3],
131 (long long)(32 + bzctrllen + bzdatalen));
132 if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
133 errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
135 if(((fd=open(argv[1],O_RDONLY,0))<0) ||
136 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
137 ((old=malloc(oldsize+1))==NULL) ||
138 (lseek(fd,0,SEEK_SET)!=0) ||
139 (read(fd,old,oldsize)!=oldsize) ||
140 (close(fd)==-1)) err(1,"%s",argv[1]);
141 if((new=malloc(newsize+1))==NULL) err(1,NULL);
144 while(newpos<newsize) {
145 /* Read control data */
147 lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
148 if ((lenread < 8) || ((cbz2err != BZ_OK) &&
149 (cbz2err != BZ_STREAM_END)))
150 errx(1, "Corrupt patch\n");
155 if(newpos+ctrl[0]>newsize)
156 errx(1,"Corrupt patch\n");
158 /* Read diff string */
159 lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]);
160 if ((lenread < ctrl[0]) ||
161 ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
162 errx(1, "Corrupt patch\n");
164 /* Add old data to diff string */
165 for(i=0;i<ctrl[0];i++)
166 if((oldpos+i>=0) && (oldpos+i<oldsize))
167 new[newpos+i]+=old[oldpos+i];
169 /* Adjust pointers */
174 if(newpos+ctrl[1]>newsize)
175 errx(1,"Corrupt patch\n");
177 /* Read extra string */
178 lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]);
179 if ((lenread < ctrl[1]) ||
180 ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
181 errx(1, "Corrupt patch\n");
183 /* Adjust pointers */
188 /* Clean up the bzip2 reads */
189 BZ2_bzReadClose(&cbz2err, cpfbz2);
190 BZ2_bzReadClose(&dbz2err, dpfbz2);
191 BZ2_bzReadClose(&ebz2err, epfbz2);
192 if (fclose(cpf) || fclose(dpf) || fclose(epf))
193 err(1, "fclose(%s)", argv[3]);
195 /* Write the new file */
196 if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
197 (write(fd,new,newsize)!=newsize) || (close(fd)==-1))