Subversion Repositories nw_plus

Rev

Blame | Last modification | View Log | RSS feed

  1. /*-
  2.  * 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.  
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. //#include <err.h>
  30. #include "bzlib.h"
  31. #include <io.h>
  32. #include <fcntl.h>
  33.  
  34. #include <sys/types.h>
  35. typedef unsigned char u_char;
  36. typedef signed int ssize_t;
  37.  
  38. template<class T1, class T2>
  39. void err(int i, const char* str, T1 arg1, T2 arg2) {
  40.         char lastErrorTxt[1024];
  41.         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,lastErrorTxt,1024,NULL);
  42.         printf("%s",lastErrorTxt);
  43.         printf(str, arg1, arg2);
  44.         exit(i);
  45. }
  46. template<class T>
  47. void err(int i, const char* str, T arg) {
  48.         char lastErrorTxt[1024];
  49.         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,lastErrorTxt,1024,NULL);
  50.         printf("%s",lastErrorTxt);
  51.         printf(str, arg);
  52.         exit(i);
  53. }
  54. void err(int i, const char* str) {
  55.         char lastErrorTxt[1024];
  56.         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,NULL,GetLastError(),0,lastErrorTxt,1024,NULL);
  57.         printf("%s",lastErrorTxt);
  58.         if (str!=NULL) {
  59.                 printf("%s",str);
  60.         }
  61.         exit(i);
  62. }
  63. template<class T>
  64. void errx(int i, const char* str, T arg) {
  65.         printf(str, arg);
  66.         exit(i);
  67. }
  68. void errx(int i, const char* str) {
  69.         printf("%s",str);
  70.         exit(i);
  71. }
  72.  
  73.  
  74. static off_t offtin(u_char *buf)
  75. {
  76.         off_t y;
  77.  
  78.         y=buf[7]&0x7F;
  79.         y=y*256;y+=buf[6];
  80.         y=y*256;y+=buf[5];
  81.         y=y*256;y+=buf[4];
  82.         y=y*256;y+=buf[3];
  83.         y=y*256;y+=buf[2];
  84.         y=y*256;y+=buf[1];
  85.         y=y*256;y+=buf[0];
  86.  
  87.         if(buf[7]&0x80) y=-y;
  88.  
  89.         return y;
  90. }
  91.  
  92. int main(int argc,char * argv[])
  93. {
  94.         FILE * f, * cpf, * dpf, * epf;
  95.         BZFILE * cpfbz2, * dpfbz2, * epfbz2;
  96.         int cbz2err, dbz2err, ebz2err;
  97.         int fd;
  98.         ssize_t oldsize,newsize;
  99.         ssize_t bzctrllen,bzdatalen;
  100.         u_char header[32],buf[8];
  101.         u_char *old, *_new;
  102.         off_t oldpos,newpos;
  103.         off_t ctrl[3];
  104.         off_t lenread;
  105.         off_t i;
  106.  
  107.         if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
  108.  
  109.         /* Open patch file */
  110.         if ((f = fopen(argv[3], "rb")) == NULL)
  111.                 err(1, "fopen(%s)", argv[3]);
  112.  
  113.         /*
  114.         File format:
  115.                 0       8       "BSDIFF40"
  116.                 8       8       X
  117.                 16      8       Y
  118.                 24      8       sizeof(newfile)
  119.                 32      X       bzip2(control block)
  120.                 32+X    Y       bzip2(diff block)
  121.                 32+X+Y  ???     bzip2(extra block)
  122.         with control block a set of triples (x,y,z) meaning "add x bytes
  123.         from oldfile to x bytes from the diff block; copy y bytes from the
  124.         extra block; seek forwards in oldfile by z bytes".
  125.         */
  126.  
  127.         /* Read header */
  128.         if (fread(header, 1, 32, f) < 32) {
  129.                 if (feof(f))
  130.                         errx(1, "Corrupt patch\n");
  131.                 err(1, "fread(%s)", argv[3]);
  132.         }
  133.  
  134.         /* Check for appropriate magic */
  135.         if (memcmp(header, "BSDIFF40", 8) != 0)
  136.                 errx(1, "Corrupt patch\n");
  137.  
  138.         /* Read lengths from header */
  139.         bzctrllen=offtin(header+8);
  140.         bzdatalen=offtin(header+16);
  141.         newsize=offtin(header+24);
  142.         if((bzctrllen<0) || (bzdatalen<0) || (newsize<0))
  143.                 errx(1,"Corrupt patch\n");
  144.  
  145.         /* Close patch file and re-open it via libbzip2 at the right places */
  146.         if (fclose(f))
  147.                 err(1, "fclose(%s)", argv[3]);
  148.         if ((cpf = fopen(argv[3], "rb")) == NULL)
  149.                 err(1, "fopen(%s)", argv[3]);
  150.         if (fseek(cpf, 32, SEEK_SET))
  151.                 err(1, "fseeko(%s, %d)", argv[3], 32);
  152.         if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
  153.                 errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
  154.         if ((dpf = fopen(argv[3], "rb")) == NULL)
  155.                 err(1, "fopen(%s)", argv[3]);
  156.         if (fseek(dpf, 32 + bzctrllen, SEEK_SET))
  157.                 err(1, "fseeko(%s, %d)", argv[3],
  158.                     (32 + bzctrllen));
  159.         if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
  160.                 errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
  161.         if ((epf = fopen(argv[3], "rb")) == NULL)
  162.                 err(1, "fopen(%s)", argv[3]);
  163.         if (fseek(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
  164.                 err(1, "fseeko(%s, %d)", argv[3],
  165.                     (32 + bzctrllen + bzdatalen));
  166.         if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
  167.                 errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
  168.  
  169.         //org:
  170.         //if(((fd=open(argv[1],O_RDONLY,0))<0) ||
  171.         //      ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  172.         //      ((old=malloc(oldsize+1))==NULL) ||
  173.         //      (lseek(fd,0,SEEK_SET)!=0) ||
  174.         //      (read(fd,old,oldsize)!=oldsize) ||
  175.         //      (close(fd)==-1)) err(1,"%s",argv[1]);
  176.         //new:
  177.         //Read in chunks, don't rely on read always returns full data!
  178.         if(((fd=open(argv[1],O_RDONLY|O_BINARY|O_NOINHERIT,0))<0) ||
  179.                 ((oldsize=lseek(fd,0,SEEK_END))==-1) ||
  180.                 ((old=(u_char*)malloc(oldsize+1))==NULL) ||
  181.                 (lseek(fd,0,SEEK_SET)!=0))
  182.                                 err(1,"%s",argv[1]);
  183.         int r=oldsize;
  184.         while (r>0 && (i=read(fd,old+oldsize-r,r))>0) r-=i;
  185.         if (r>0 || close(fd)==-1) err(1,"%s",argv[1]);
  186.  
  187.         if((_new=(u_char*)malloc(newsize+1))==NULL) err(1,NULL);
  188.  
  189.         oldpos=0;newpos=0;
  190.         while(newpos<newsize) {
  191.                 /* Read control data */
  192.                 for(i=0;i<=2;i++) {
  193.                         lenread = BZ2_bzRead(&cbz2err, cpfbz2, buf, 8);
  194.                         if ((lenread < 8) || ((cbz2err != BZ_OK) &&
  195.                             (cbz2err != BZ_STREAM_END)))
  196.                                 errx(1, "Corrupt patch\n");
  197.                         ctrl[i]=offtin(buf);
  198.                 };
  199.  
  200.                 /* Sanity-check */
  201.                 if(newpos+ctrl[0]>newsize)
  202.                         errx(1,"Corrupt patch\n");
  203.  
  204.                 /* Read diff string */
  205.                 lenread = BZ2_bzRead(&dbz2err, dpfbz2, _new + newpos, ctrl[0]);
  206.                 if ((lenread < ctrl[0]) ||
  207.                     ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END)))
  208.                         errx(1, "Corrupt patch\n");
  209.  
  210.                 /* Add old data to diff string */
  211.                 for(i=0;i<ctrl[0];i++)
  212.                         if((oldpos+i>=0) && (oldpos+i<oldsize))
  213.                                 _new[newpos+i]+=old[oldpos+i];
  214.  
  215.                 /* Adjust pointers */
  216.                 newpos+=ctrl[0];
  217.                 oldpos+=ctrl[0];
  218.  
  219.                 /* Sanity-check */
  220.                 if(newpos+ctrl[1]>newsize)
  221.                         errx(1,"Corrupt patch\n");
  222.  
  223.                 /* Read extra string */
  224.                 lenread = BZ2_bzRead(&ebz2err, epfbz2, _new + newpos, ctrl[1]);
  225.                 if ((lenread < ctrl[1]) ||
  226.                     ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END)))
  227.                         errx(1, "Corrupt patch\n");
  228.  
  229.                 /* Adjust pointers */
  230.                         newpos+=ctrl[1];
  231.                         oldpos+=ctrl[2];
  232.                 };
  233.  
  234.         /* Clean up the bzip2 reads */
  235.         BZ2_bzReadClose(&cbz2err, cpfbz2);
  236.         BZ2_bzReadClose(&dbz2err, dpfbz2);
  237.         BZ2_bzReadClose(&ebz2err, epfbz2);
  238.         if (fclose(cpf) || fclose(dpf) || fclose(epf))
  239.                 err(1, "fclose(%s)", argv[3]);
  240.  
  241.         /* Write the new file */
  242.         //org:
  243.         //if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
  244.         //new:
  245.         if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))<0) ||
  246.                 (write(fd,_new,newsize)!=newsize) || (close(fd)==-1))
  247.                 err(1,"%s",argv[2]);
  248.  
  249.         free(_new);
  250.         free(old);
  251.  
  252.         return 0;
  253. }
  254.