Advertisement
Guest User

Untitled

a guest
May 26th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "en-decrypt.h"
  4. #include "usefulfunc.h"
  5. #include <string.h>
  6. #include <math.h>
  7. #include <unistd.h>
  8. #include <stdarg.h>
  9. #include <time.h>
  10.  
  11. char make_numeric(char ch)
  12. {
  13.     if(ch<91 && ch>63) return ch-16;
  14.     if(ch<123 && ch>95) return ch-48;
  15.     if(ch<58 && ch>47) return ch;
  16.     return ch+16;
  17. }
  18.  
  19. char* strip(char* str,int length)
  20. {
  21.     char *start=str,*end=&str[length-1];
  22.    
  23.     while(*start>0 && *start<33 || *start==127) ++start;
  24.     if(!*start) return start;
  25.    
  26.     while(*end>=0 && *end<33 || *end==127) --end;
  27.     ++end;
  28.     *end=0;
  29.    
  30.     return start;
  31.  
  32. }
  33.  
  34. int main()
  35. {
  36.  
  37.     int shift,len_chshift,init,len_filename,i=0;
  38.     char *chshift,*line=NULL,*decrypted_line,*stripped_line,chinit,chlen_chshift;
  39.     char *filename=NULL,*newfilename=NULL;
  40.     FILE *oldfile=NULL;
  41.     FILE *newfile=NULL;
  42.     size_t lenline,len=0;
  43.    
  44.    
  45.     printf("-9");
  46.     /*FILENAME*/
  47.     printf("Enter the file name: ");
  48.     /*printf("-8");*/
  49.     filename=getIn();
  50.     /*printf("-7");*/
  51.     if(!access(filename,F_OK))
  52.     {
  53.         len_filename=strlen(filename);
  54.         /*printf("-6");*/
  55.         i=len_filename;
  56.         while(filename[i]!='.') --i;       
  57.         chlen_chshift=filename[i-1];
  58.         chlen_chshift=make_numeric(chlen_chshift);
  59.         len_chshift=chlen_chshift-48;
  60.         /*printf("-5");*/
  61.         chshift=calloc(len_chshift+1,1);
  62.         /*NEWFILENAME*/
  63.         newfilename=calloc(i+1,1);
  64.         /*printf("-4");*/
  65.         strncpy(newfilename,filename,i);
  66.         /*printf("-3");*/
  67.         strcat(newfilename,"(decrypted).txt");
  68.         /*printf("-2");*/
  69.         chinit=make_numeric(filename[0]);
  70.         init=chinit-48;
  71.         /*SHIFT*/
  72.         i=0;
  73.         /*printf("-1");*/
  74.         while(i!=len_chshift)
  75.         {
  76.             chshift[i]=make_numeric(filename[(i+1)*init]);
  77.             ++i;
  78.         }
  79.         /*printf("0");*/
  80.         shift=atoi(chshift);
  81.        
  82.         /*printf("1");*/
  83.         if(!(oldfile=fopen(filename,"r")))
  84.         {
  85.             perror("Error");
  86.             if(newfilename) free(newfilename);
  87.             if(filename) free(filename);
  88.             if(chshift) free(chshift);
  89.             exit(1);
  90.         }
  91.         /*printf("2");*/
  92.        
  93.         if(!(newfile=fopen(newfilename,"+ab")))
  94.         {
  95.             perror("Error");
  96.             if(newfilename) free(newfilename);
  97.             if(filename) free(filename);
  98.             if(chshift) free(chshift);
  99.             fclose(oldfile);
  100.             exit(1);
  101.         }
  102.         while ((lenline = getline(&line, &len, oldfile)) != -1)
  103.         {
  104.             stripped_line=strip(line,lenline);
  105.             decrypted_line=caesar_decrypt(stripped_line, &shift);
  106.             if(!decrypted_line)
  107.             {
  108.                 printf("Could not allocate memory\n");
  109.                 if(newfilename) free(newfilename);
  110.                 if(filename) free(filename);
  111.                 if(chshift) free(chshift);
  112.                 exit(1);
  113.             }
  114.            
  115.             fprintf(newfile,"%s\n",decrypted_line);
  116.            
  117.             if(decrypted_line) free(decrypted_line);
  118.             decrypted_line=NULL;
  119.            
  120.             free(line);
  121.             line=NULL;
  122.             stripped_line=NULL;
  123.             decrypted_line=NULL;
  124.         }
  125.         free(line);
  126.         line=NULL;
  127.     }
  128.     else
  129.     {
  130.         printf("Cannot access the file.");
  131.         if(filename) free(filename);
  132.         exit(0);
  133.     }
  134.    
  135.     if(newfilename) free(newfilename);
  136.     if(filename) free(filename);
  137.     if(chshift) free(chshift);
  138.     fclose(oldfile);
  139.     fclose(newfile);
  140.     return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement