Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Katona David - 30224
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h> //O_RDONLY
- #include <string.h> //strcmp
- #include <unistd.h> //open & read
- #include <dirent.h>
- #include <limits.h>
- #include <sys/stat.h>
- int pow2(int a, int b)
- {
- int rez = 1;
- for( int i = 1; i <= b; i++)
- rez *= a;
- return rez;
- }
- void intToBin(int x,int bitNumber)
- {
- for(int i = bitNumber -1 ; i >= 0; i--)
- {
- if (i%7==0) printf(" ");
- int k = x >> i;
- if((k & 1) == 1 )
- printf("1");
- else
- printf("0");
- }
- printf("\n");
- }
- void histogram(char* path, int bitNr)
- {
- int fd;
- char byteRead;
- int arraySize = pow2(2,bitNr);
- int *rez = calloc(arraySize, 4);
- if ((fd = open(path, O_RDONLY)) < 0)
- {
- perror("Open failed.");
- return;
- }
- while(read(fd, &byteRead, 1) > 0)
- {
- for( int i = 0; i < 8/bitNr ; i++)
- {
- int x = (1 << bitNr) - 1;
- int y = byteRead & x;
- rez[y]++;
- byteRead >>= bitNr;
- }
- }
- printf("\nSUCCESS\n");
- for(int i = 0; i < arraySize; i++)
- {
- intToBin(i,bitNr);
- printf(": %d\n", rez[i]);
- }
- }
- void runs(char* path)
- {
- int fd, maxx = 0, curr = 0, start=0, byte, bits;
- char byteRead;
- if ((fd = open(path, O_RDONLY)) < 0)
- {
- perror("Open failed.");
- return;
- }
- int fileS = lseek( fd, 0, SEEK_END)-1;
- while(fileS>0)
- {
- lseek( fd, fileS, SEEK_SET);
- read( fd, &byteRead, 1);
- for( int i = 0; i < 8; i++)
- {
- int x = byteRead & 1;
- if(x == 1)
- {
- curr++;
- start = 1;
- if( curr > maxx)
- {
- maxx = curr;
- byte = fileS;
- }
- }
- else
- {
- curr = 0;
- if(maxx == curr && start==1)
- {
- bits = 8-i;
- start = 0;
- }
- }
- byteRead >>= 1;
- }
- fileS--;
- }
- printf("SUCCESS\nLength of the longest run: %d\nOffset: %d bytes + %d bits\n",maxx,byte,bits);
- }
- unsigned long swapBits(unsigned long t, int bitNr)
- {
- unsigned long swapped=0;
- if (bitNr==8)
- {
- swapped=((t<<8)&0xff00)|((t>>8)&0x00ff);
- }
- else if (bitNr==12)
- {
- swapped=((t<<16)&0xff0000) | (t&0x00ff00) | ((t>>16)&0x0000ff);
- }
- else if (bitNr==16)
- {
- swapped=((t<<24)&0xff000000) | ((t<<8)&0x00ff0000) | ((t>>8)&0x0000ff00) | ((t>>24)&0x000000ff);
- }
- else if (bitNr==24)
- {
- swapped=(((t<<40)&0xff0000000000) | ((t<<24)&0x00ff00000000) | ((t<<8)&0x0000ff000000) | ((t>>8)&0x000000ff0000) | ((t>>24)&0x000000ff00) | ((t>>40)&0x0000000000ff));
- }
- else if (bitNr==32)
- {
- swapped=((t<<56)&0xff00000000000000) | ((t<<40)&0x00ff000000000000) | ((t<<24)&0x0000ff0000000000) | ((t<<8)&0x000000ff00000000) | ((t>>8)&0x00000000ff000000) | ((t>>24)&0x00000000ff0000) | ((t>>40)&0x000000000000ff00) | ((t>>56)&0x00000000000000ff);
- }
- return swapped;
- }
- int find_match(unsigned int sablon, unsigned long t, int bitNr)
- {
- unsigned long b=t;
- unsigned int sablonB=sablon;
- int correct, occ=0;
- for (int i=1; i<=bitNr; i++)
- {
- correct=1;
- sablon=sablonB;
- t=b>>(bitNr+1-i);
- for (int j=1; j<=bitNr; j++)
- {
- if ((t&1) == (sablon&1))
- {
- t>>=1;
- sablon>>=1;
- }
- else
- {
- correct=0;
- break;
- }
- }
- if (correct==1)
- occ++;
- }
- return occ;
- }
- void template_fn(char* tmPath, char* filePath, int bitNr, int tempNr)
- {
- int fdT, fdF, offset, occ=0, fileS, fileSb;
- short ver, no_of_cat, size_of_cat, no_of_temp;
- unsigned int sablon;
- unsigned long t;
- if ((fdT = open(tmPath, O_RDONLY)) < 0)
- {
- perror("ERROR\nInvalid file path\n");
- return;
- }
- if ((fdF = open(filePath, O_RDONLY)) < 0)
- {
- perror("ERROR\nInvalid file path\n");
- return;
- }
- read(fdT, &ver, 2);
- if (ver>54321 || ver<12345)
- {
- printf("ERROR\nInvalid template_fn file\nWrong version");
- return;
- }
- read(fdT, &no_of_cat, 2);
- while(read(fdT, &size_of_cat, 2)>0)
- {
- if (size_of_cat==bitNr)
- {
- read(fdT, &no_of_temp, 2); //no of template_fns in category
- read(fdT, &offset, 4);
- break;
- }
- else
- {
- lseek(fdT, 6, SEEK_CUR);
- no_of_cat--;
- }
- }
- if (tempNr>no_of_temp)
- {
- printf("ERROR\nInvalid template_fn number\n");
- return;
- }
- if (no_of_cat==0)
- {
- printf("ERROR\nInvalid template_fn category\n");
- return;
- }
- if (tempNr==0) //do all tests
- {
- lseek(fdT, offset, SEEK_SET); //cursor in front of first test
- printf("\nSUCCESS\n");
- for (int i=1; i<=no_of_temp; i++)
- {
- occ=0;
- sablon^=sablon;
- read(fdT, &sablon, bitNr/8);
- if (bitNr!=8)
- sablon=swapBits(sablon, bitNr/2);
- fileS = lseek( fdF, 0, SEEK_END);
- fileSb=fileS;
- lseek(fdF, 0, SEEK_SET);
- while(fileS>=bitNr/4)
- {
- read(fdF, &t, bitNr/4);
- t=swapBits(t, bitNr);
- occ+=find_match(sablon, t, bitNr);
- fileS-=bitNr/8;
- lseek(fdF, -(bitNr/8), SEEK_CUR);
- }
- if (fileSb%(bitNr/8)!=0) //leftover bytes in file
- {
- lseek(fdF, -(bitNr/4), SEEK_END);
- read(fdF, &t, bitNr/4);
- t=swapBits(t, bitNr);
- occ+=find_match(sablon, t, bitNr);
- }
- printf("Occurences of template_fn %d (%x) of length %d: %d\n", i, sablon, bitNr, occ);
- }
- }
- else //do one test
- {
- lseek(fdT, (offset + (bitNr/8)*(tempNr-1)), SEEK_SET);
- printf("\nSUCCESS\n");
- occ=0;
- fileS = lseek( fdF, 0, SEEK_END);
- fileSb=fileS;
- sablon^=sablon;
- read(fdT, &sablon, bitNr/8);
- if (bitNr!=8)
- sablon=swapBits(sablon, bitNr/2);
- lseek(fdF, 0, SEEK_SET);
- while(fileS>=bitNr/4)
- {
- read(fdF, &t, bitNr/4);
- t=swapBits(t, bitNr);
- occ+=find_match(sablon, t, bitNr);
- fileS-=bitNr/8;
- lseek(fdF, -(bitNr/8), SEEK_CUR);
- }
- if (fileSb%(bitNr/8)!=0) //leftover bytes in file
- {
- lseek(fdF, -(bitNr/4), SEEK_END);
- read(fdF, &t, bitNr/4);
- t=swapBits(t, bitNr);
- occ+=find_match(sablon, t, bitNr);
- }
- printf("\nOccurences of template_fn %d (%x) of length %d: %d\n", tempNr, sablon, bitNr, occ);
- }
- }
- void list(int rec, char* filter, char* dirPath)
- {
- char op[256], path[256], crit[10];
- struct dirent *de;
- struct stat st;
- DIR* dir=opendir(dirPath);
- if(dir==0)
- {
- perror("ERROR\nCould not open directory\n");
- return;
- }
- printf("\nSUCCESS\n")
- strcpy(op, filter);
- for(int i=0; i<4; ++i) crit[i]=op[i];
- crit[4]=0;
- if(strcmp("size", crit)==0)
- {
- int k;
- char s[25];
- strcpy(s,op+13);
- k=atoi(s);
- while((de=readdir(dir)))
- {
- char nume[256], num[256];
- strcpy(nume, path);
- strcpy(num, "/");
- strcat(num, de->d_name);
- strcat(nume, num);
- stat(nume, &st);
- int sz=st.st_size;
- if(sz>=k)
- {
- printf("%s\n", de->d_name);
- }
- }
- }
- else if(strcmp("name", crit)==0)
- {
- char eq[256];
- strcpy(eq, op+14);
- while((de=readdir(dir)))
- {
- char nume[256];
- strcpy(nume, de->d_name);
- if(strstr(nume, eq)!=NULL)
- {
- printf("%s\n", de->d_name);
- }
- }
- }
- else if(strcmp("temp", crit)==0)
- {
- while((de=readdir(dir)))
- {
- char nume[256], num[256];
- strcpy(nume, path);
- strcpy(num, "/");
- strcat(num, de->d_name);
- strcat(nume, num);
- lstat(nume, &st);
- if(S_ISREG(st.st_mode))
- {
- printf("%s\n", nume);
- }
- }
- }
- else if(strcmp("hist", crit)==0)
- {
- char s[10];
- strcpy(s, op+12);
- int k=atoi(s);
- if(k!=1 && k!=2 && k!=4 && k!=8)
- {
- printf("Invalid value for N");
- return ;
- }
- while((de=readdir(dir)))
- {
- char nume[256], num[256], p;
- strcpy(nume, path);
- strcpy(num, "/");
- strcat(num, de->d_name);
- strcat(nume, num);
- stat(nume, &st);
- int sz=st.st_size;
- sz*=8;
- sz/=100;
- int nrb, val, v[100], minim=INT_MAX, maxim=0;
- nrb--;
- for(int i=0; i<100; ++i) v[i]=0;
- int f=open(nume, O_RDONLY);
- if(f!=-1)
- {
- while(read(f, &p, 1)>0)
- {
- int i=0;
- while(i<8/k)
- {
- nrb=1<<k;
- val=k & nrb;
- v[val]++;
- i++;
- p>>=k;
- }
- }
- for(int i=0; i<100; ++i)
- if(v[i])
- {
- if(maxim<v[i]) maxim=v[i];
- if(minim>v[i]) minim=v[i];
- }
- if(maxim-minim<sz && minim!=INT_MAX)
- {
- printf("%s\n", nume);
- }
- }
- }
- }
- }
- int main(int argc, char** argv)
- {
- int bits, tempNr;
- if(strcmp(argv[1], "histogram")==0)
- {
- if (argc!=4)
- {
- printf("USAGE: %s histogram number_of_bits /path/to/file\n", argv[0]);
- exit(0);
- }
- bits=atoi(argv[2]);
- if (bits!=1 && bits!=2 && bits!=4 && bits!=8)
- perror("ERROR\nInvalid number of bits\nSupported values are: 1, 2, 4, 8\n");
- else
- histogram(argv[3], bits);
- }
- else if(strcmp(argv[1], "runs")==0)
- {
- if (argc!=3)
- {
- printf("USAGE: %s runs /path/to/file\n", argv[0]);
- exit(0);
- }
- else
- runs(argv[2]);
- }
- else if(strcmp(argv[1], "template_fn")==0)
- {
- if (argc!=5 && argc!=6)
- {
- printf("USAGE: ./t1 template_fn <number_of_bits> <number_of_template_fn> <template_fn_file_path> <file_path> OR\n./t1 <number_of_bits> <template_fn_file_path> <file_path>\n");
- exit(0);
- }
- bits=atoi(argv[2]);
- if (bits!=8 && bits!=16 && bits!=24 && bits!=32)
- perror("ERROR\nInvalid number of bits\nSupported values are: 8, 16, 24, 32\n");
- else if (argc==5)
- {
- template_fn(argv[3], argv[4], bits, 0);
- }
- else if (argc==6)
- {
- tempNr=atoi(argv[3]);
- if (tempNr<=0)
- {
- perror("ERROR\n<number_of_template_fn> has to be grater than 0\n");
- return 0;
- }
- else
- template_fn(argv[4], argv[5], bits, tempNr);
- }
- }
- else if(strcmp(argv[1], "list")==0)
- {
- if (argc!=4 && argc!=5)
- {
- printf("USAGE: ./t1 list [recursive] <filtering_options> <dir_path>");
- exit(0);
- }
- else if (argc==4) //non-recursive
- {
- list(0, argv[2], argv[3]);
- }
- else if (argc==5) //recursive
- {
- list(1, argv[3], argv[4]);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement