Advertisement
Guest User

strip16bytes.c

a guest
Apr 30th, 2017
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     if(argc < 2)
  7.     {
  8.         printf("Error: you need to give me a file name to work on.\n");
  9.         return (1);
  10.     }
  11.     else
  12.     {
  13.         FILE *infile;
  14.         if ((infile = fopen(argv[1], "rb")))
  15.         /* 'b' is necessary because windows is dumb and noncompliant as usual */
  16.         {
  17.             /* do this if the file exists: */
  18.             fseek(infile, 0L, SEEK_END);
  19.             long size = ftell(infile);
  20.             fseek(infile, 0L, SEEK_SET);
  21.             printf("size: %ld\n",size);
  22.             if(size < 16)
  23.             {
  24.                 printf("The file size is less than 16 bytes; this shouldn't ever happen.\nExiting.\n");
  25.                 fclose(infile);
  26.                 return (1);
  27.             }
  28.             /* set an output file name, in the format of origfilename_stripped.swf */
  29.             int output;
  30.             char outputFileName[strlen(argv[1]) + 10]; /* '_stripped' is 9 bytes, null terminator makes ten */
  31.             size_t j=0; /* size_t is annoying. */
  32.             int k=0; /* because size_t is unsigned I have to keep track with both signed and unsigned numbers. */
  33.             while ( j < ( strlen(argv[1]) - 4 )) /* '.swf' is 4 chars */
  34.             {
  35.                 outputFileName[k]=argv[1][k];
  36.                 ++j;
  37.                 ++k;
  38.             }
  39.             /* then append '_stripped.swf' */
  40.             /* yes, there are better ways. but I am too lazy to try to remember them. */
  41.             outputFileName[k]='_';
  42.             outputFileName[k+1]='s';
  43.             outputFileName[k+2]='t';
  44.             outputFileName[k+3]='r';
  45.             outputFileName[k+4]='i';
  46.             outputFileName[k+5]='p';
  47.             outputFileName[k+6]='p';
  48.             outputFileName[k+7]='e';
  49.             outputFileName[k+8]='d';
  50.             outputFileName[k+9]='.';
  51.             outputFileName[k+10]='s';
  52.             outputFileName[k+11]='w';
  53.             outputFileName[k+12]='f';
  54.             outputFileName[k+13]='\0';
  55.             printf("output file name: %s\n",outputFileName);
  56.  
  57.             FILE *outfile;
  58.             outfile = fopen(outputFileName,"w+b");
  59.             /* 'b' is necessary because windows is dumb and noncompliant as usual */
  60.             long i=0;
  61.             while(i < (size - (long)16))
  62.             {
  63.                 /* start copying bytes from infile to outfile. */
  64.                 /* stops 16 bytes from the end of the input file. */
  65.                 /* this has the effect of truncating 16 bytes. */
  66.                 fputc(fgetc(infile),outfile);
  67.                 ++i;
  68.             }
  69.             printf("%ld\n",i);
  70.  
  71.             fclose(outfile);
  72.             fclose(infile);
  73.             return (0);
  74.         }
  75.     }
  76.     return (0);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement