Advertisement
image28

Binary Patch Extractor

Jun 25th, 2016
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. struct header
  4. {
  5.     int action;
  6.     long size;
  7.     char filename[512];
  8. };
  9.  
  10. int main( int argc, char *argv[] )
  11. {
  12.     FILE *input;
  13.     FILE *output;
  14.     int size=13049;
  15.     long fs=0;
  16.     long count=0;
  17.     char inbyte='\0';
  18.     struct header index[256];
  19.     long pos=0;
  20.     char csize[512];
  21.     int d=0;
  22.     int end=0;
  23.     char cmd[512];
  24.     int fpos=0;
  25.  
  26.     if ( ( input = fopen(argv[0],"rb") ) == NULL ) exit(-1);
  27.     //output=fopen("out.txt","wb");
  28.  
  29.     fseek(input,0L,SEEK_END);
  30.     fs=ftell(input);
  31.     fseek(input,size,SEEK_SET);
  32.  
  33.     while ( ! end )
  34.     {
  35.         inbyte='\0';
  36.         fread(&inbyte,1,1,input);
  37.  
  38.         if ( inbyte == 'A' )
  39.         {
  40.             // ADD
  41.             index[pos].action=0;
  42.  
  43.             for(d=0;d<3;d++)
  44.                 fread(&inbyte,1,1,input);
  45.  
  46.             inbyte='\0';
  47.             fread(&inbyte,1,1,input);
  48.            
  49.             for(d=0;d<512;d++)
  50.                 csize[d]='\0';
  51.  
  52.             while ( inbyte != 32 )
  53.             {
  54.                 csize[count]=inbyte;
  55.                 inbyte='\0';
  56.                 fread(&inbyte,1,1,input);
  57.                 count++;
  58.             }count=0;
  59.  
  60.             // convert string to number
  61.             index[pos].size=strtol(csize,NULL,10);
  62.             printf("%s %d\n",csize, index[pos].size);
  63.  
  64.             inbyte='\0';
  65.             fread(&inbyte,1,1,input);
  66.  
  67.             while ( inbyte != '\n' )
  68.             {
  69.                 index[pos].filename[count]=inbyte;
  70.                 inbyte='\0';
  71.                 fread(&inbyte,1,1,input);
  72.                 count++;
  73.             }count=0;
  74.  
  75.             printf("%d %s %d\n",index[pos].action, index[pos].filename, index[pos].size);
  76.  
  77.         }
  78.         else if ( inbyte == 'U' )
  79.         {
  80.             // UPDATE
  81.             for(d=0;d<6;d++)
  82.                 fread(&inbyte,1,1,input);
  83.  
  84.             index[pos].action=1;
  85.  
  86.             inbyte='\0';
  87.             fread(&inbyte,1,1,input);
  88.            
  89.             for(d=0;d<512;d++)
  90.                 csize[d]='\0';
  91.  
  92.             while ( inbyte != 32 )
  93.             {
  94.                 csize[count]=inbyte;
  95.                 inbyte='\0';
  96.                 fread(&inbyte,1,1,input);
  97.                 count++;
  98.             }count=0;
  99.  
  100.             // convert string to number
  101.             index[pos].size=strtol(csize,NULL,10);
  102.  
  103.             inbyte='\0';
  104.             fread(&inbyte,1,1,input);
  105.  
  106.             while ( inbyte != '\n' )
  107.             {
  108.                 index[pos].filename[count]=inbyte;
  109.                 inbyte='\0';
  110.                 fread(&inbyte,1,1,input);
  111.                 count++;
  112.             }count=0;
  113.  
  114.             printf("%d %s %d\n",index[pos].action, index[pos].filename, index[pos].size);
  115.  
  116.         }
  117.         else if ( inbyte == 'D' )
  118.         {
  119.             // DELETE
  120.             for(d=0;d<6;d++)
  121.                 fread(&inbyte,1,1,input);
  122.  
  123.             index[pos].action=2;
  124.  
  125.             inbyte='\0';
  126.             fread(&inbyte,1,1,input);
  127.  
  128.             while ( inbyte != '\n' )
  129.             {
  130.                 index[pos].filename[count]=inbyte;
  131.                 inbyte='\0';
  132.                 fread(&inbyte,1,1,input);
  133.                 count++;
  134.             }count=0;
  135.  
  136.             printf("%d %s\n",index[pos].action, index[pos].filename);
  137.  
  138.         }else{
  139.             while ( inbyte != '\n' )
  140.                 fread(&inbyte,1,1,input);
  141.             end=1;
  142.         }
  143.        
  144.        
  145.         pos++;
  146.     }
  147.  
  148.     for(d=0;d<pos-1;d++)
  149.     {
  150.         if ( index[d].action == 2 )
  151.         {
  152.             strcpy(cmd,"rm ");
  153.             strcat(cmd,index[d].filename);
  154.             printf("Removing %s\n",index[d].filename);
  155.             system(cmd);
  156.         }
  157.         else if ( index[d].action == 0 )
  158.         {
  159.             fpos=0;
  160.             printf("Adding %s with size of %d bytes\n",index[d].filename, index[d].size);
  161.             output=fopen(index[d].filename,"wb");
  162.             while ( fpos < index[d].size )
  163.             {
  164.                 inbyte='\0';
  165.                 fread(&inbyte,1,1,input);
  166.                 fwrite(&inbyte,1,1,output);
  167.                 fpos++;
  168.             }  
  169.             fclose(output);
  170.         }
  171.         else if ( index[d].action == 1 )
  172.         {
  173.             strcpy(cmd,"rm ");
  174.             strcat(cmd,index[d].filename);
  175.             printf("Updating %s with size of %d bytes\n",index[d].filename, index[d].size);
  176.             system(cmd);
  177.  
  178.             fpos=0;
  179.             output=fopen(index[d].filename,"wb");
  180.             while ( fpos < index[d].size )
  181.             {
  182.                 inbyte='\0';
  183.                 fread(&inbyte,1,1,input);
  184.                 fwrite(&inbyte,1,1,output);
  185.                 fpos++;
  186.             }  
  187.             fclose(output);
  188.  
  189.            
  190.         }
  191.  
  192.     }
  193.  
  194.     fclose(input);
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement