Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <errno.h>
- #include <string.h>
- #include <dirent.h>
- #include <unistd.h>
- /* Functii pentru validarea argumentelor */
- int checkIntegerArgument(char* s);
- int checkFilePath(char* path);
- /* Functii pentru task1(Histogram) */
- int createMask(int noBits);
- char* charToString(char c);
- int stringToDecimal(char* c);
- void shiftRight(char c[], int bits);
- int* getFreqForChar(char b, int bits);
- void histogram(int noBits, char* path);
- /* Functii pentru task2(Cea mai lunga secventa de 1) */
- void findSequence(char c, int* longestSequence, int* bytesRead, int* maxSequence, int* zeroBefore, int* newSeq, int* begOfLongestSeq);
- void longestSetSequence(char* path);
- /* Functii pentru task3(Cautarea template-urilor) */
- char* shiftLeft(char *c, char b, int dim);
- void findTemplate(char* sablon, int dim, int fd);
- void umplere(char c, char* sir, char* sablon, int dim, int* i, int* nr);
- int checkTemplateFileVersion(char* path);
- int getNumberOfTemplates(char* path);
- void getFileMetaData(char* path, int bits, int* dimensions, int* offsets, int* noTemplates);
- void templateFunctionV1(int bits, int templateNumber, char* templatePath, char* filePath);
- void templateFunctionV2(int bits, char* templatePath, char* filePath);
- int main(int argc, char* argv[])
- {
- char* option = argv[1];
- /* HISTOGRAM */
- if(strcmp(option,"histogram") == 0)
- {
- if(argc != 4)
- {
- perror("Argumente insuficiente!");
- exit(1);
- }
- char* bits = argv[2];
- char* pathToFile = argv[3];
- char* ptr = strtok(argv[2],"=");
- char* array[2];
- int count = 0;
- while(ptr != NULL)
- {
- array[count++] = ptr;
- ptr = strtok(NULL,"=");
- }
- if(strcmp(array[0],"bits") != 0)
- {
- printf("Argumentul 2 este invalid\n");
- exit(2);
- }
- if(checkIntegerArgument(array[1]) == 0)
- {
- printf("Argumentul 2 este invalid. Dupa egal nu se afla intreg\n");
- exit(2);
- }
- int intValueOfArg = atoi(array[1]);
- if(intValueOfArg != 1 && intValueOfArg != 2 && intValueOfArg != 4 && intValueOfArg != 8)
- {
- printf("ERROR\nInvalid number of bits\nSupported values are: 1, 2, 4, 8\n");
- exit(3);
- }
- char* ptr2 = strtok(argv[3],"=");
- while(ptr2 != NULL)
- {
- //printf("%s\n",ptr2);
- if(strchr(ptr2,'/') != NULL)
- {
- if(checkFilePath(ptr2) == 0)
- {
- printf("ERROR\nInvalid file path\n");
- exit(3);
- }
- else
- {
- int fd = open(ptr2,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut deschide fisierul!");
- exit(1);
- }
- else
- {
- histogram(intValueOfArg,ptr2);
- }
- }
- }
- ptr2 = strtok(NULL,"=");
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- /* LONGEST SET SEQUENCE */
- if(strcmp(argv[1],"runs") == 0)
- {
- if(argc != 3)
- {
- perror("Argumente insuficiente!");
- exit(1);
- }
- char* pathToFile = argv[2];
- char* ptr2 = strtok(argv[2],"=");
- int count2 = 0;
- while(ptr2 != NULL)
- {
- if(strchr(ptr2,'/') != NULL)
- {
- if(checkFilePath(ptr2) == 0)
- {
- printf("ERROR\nInvalid file path\n");
- exit(3);
- }
- else
- {
- int fd = open(ptr2,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut deschide fisierul!");
- exit(1);
- }
- else
- {
- longestSetSequence(ptr2);
- }
- }
- }
- ptr2 = strtok(NULL,"=");
- }
- }
- /////////////////////////////////////////////////////////////////////////////////////////////////////
- if(strcmp(argv[1],"template") == 0)
- {
- if(argc == 5)
- {
- char* pathToTemplate = argv[3];
- char* pathToFile = argv[4];
- char* bits = argv[2];
- char* ptr = strtok(argv[2],"=");
- char* array[2];
- int count = 0;
- while(ptr != NULL)
- {
- array[count++] = ptr;
- ptr = strtok(NULL,"=");
- }
- if(strcmp(array[0],"bits") != 0)
- {
- printf("Argumentul 2 este invalid\n");
- exit(2);
- }
- if(checkIntegerArgument(array[1]) == 0)
- {
- printf("Argumentul 2 este invalid. Dupa egal nu se afla intreg\n");
- exit(2);
- }
- int intValueOfArg = atoi(array[1]);
- if(intValueOfArg != 8 && intValueOfArg != 16 && intValueOfArg != 24 && intValueOfArg != 32)
- {
- printf("ERROR\nInvalid number of bits\nSupported values are: 8, 16, 24, 32\n");
- exit(3);
- }
- char* ptr2 = strtok(argv[3],"=");
- while(ptr2 != NULL)
- {
- if(strchr(ptr2,'/') != NULL)
- {
- if(checkFilePath(ptr2) == 0)
- {
- printf("ERROR\nInvalid file path\n");
- exit(3);
- }
- else
- {
- int fd = open(ptr2,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut deschide fisierul!");
- exit(1);
- }
- else
- {
- break;
- //printf("%s\n",pathToTemplate);
- }
- }
- }
- ptr2 = strtok(NULL,"=");
- }
- char* ptr3 = strtok(argv[4],"=");
- while(ptr3 != NULL)
- {
- if(strchr(ptr3,'/') != NULL)
- {
- if(checkFilePath(ptr3) == 0)
- {
- printf("ERROR\nInvalid file path\n");
- exit(3);
- }
- else
- {
- int fd = open(ptr3,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut deschide fisierul!");
- exit(1);
- }
- else
- {
- templateFunctionV2(intValueOfArg,ptr2,ptr3);
- }
- }
- }
- ptr3 = strtok(NULL,"=");
- }
- }
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////
- if(strcmp(argv[1],"template") == 0)
- {
- if(argc == 6)
- {
- char* pathToTemplate = argv[4];
- char* pathToFile = argv[5];
- char* bits = argv[2];
- char* templateNumber = argv[3];
- char* ptr = strtok(argv[2],"=");
- char* array[2];
- int count = 0;
- while(ptr != NULL)
- {
- array[count++] = ptr;
- ptr = strtok(NULL,"=");
- }
- if(strcmp(array[0],"bits") != 0)
- {
- printf("Argumentul 2 este invalid\n");
- exit(2);
- }
- if(checkIntegerArgument(array[1]) == 0)
- {
- printf("Argumentul 2 este invalid. Dupa egal nu se afla intreg\n");
- exit(2);
- }
- int intValueOfArg = atoi(array[1]);
- if(intValueOfArg != 8 && intValueOfArg != 16 && intValueOfArg != 24 && intValueOfArg != 32)
- {
- printf("ERROR\nInvalid number of bits\nSupported values are: 8, 16, 24, 32\n");
- exit(3);
- }
- //int intValueOfTemplate = 5;
- char* ptrT = strtok(argv[3],"=");
- char* arrayT[2];
- int countT = 0;
- while(ptrT != NULL)
- {
- arrayT[countT++] = ptrT;
- ptrT = strtok(NULL,"=");
- }
- if(strcmp(arrayT[0],"template") != 0)
- {
- printf("Argumentul 3 este invalid\nCorect este template=dimension");
- exit(2);
- }
- if(checkIntegerArgument(arrayT[1]) == 0)
- {
- printf("Argumentul 3 este invalid. Dupa egal nu se afla un intreg\n");
- exit(2);
- }
- int intValueOfTemplate = atoi(arrayT[1]);
- char* ptr2 = strtok(argv[4],"=");
- while(ptr2 != NULL)
- {
- if(strchr(ptr2,'/') != NULL)
- {
- if(checkFilePath(ptr2) == 0)
- {
- printf("ERROR\nInvalid file path\n");
- exit(3);
- }
- else
- {
- int fd = open(ptr2,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut deschide fisierul!");
- exit(1);
- }
- else
- {
- break;
- //printf("%s\n",pathToTemplate);
- }
- }
- }
- ptr2 = strtok(NULL,"=");
- }
- char* ptr3 = strtok(argv[5],"=");
- while(ptr3 != NULL)
- {
- if(strchr(ptr3,'/') != NULL)
- {
- if(checkFilePath(ptr3) == 0)
- {
- printf("ERROR\nInvalid file path\n");
- exit(3);
- }
- else
- {
- int fd = open(ptr3,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut deschide fisierul!");
- exit(1);
- }
- else
- {
- templateFunctionV1(intValueOfArg,intValueOfTemplate,ptr2,ptr3);
- }
- }
- }
- ptr3 = strtok(NULL,"=");
- }
- }
- }
- }
- /* FUNCTII PENTRU VALIDAREA ARGUMENTELOR */
- int checkFilePath(char* path)
- {
- int fd = open(path,O_RDONLY);
- if(fd == -1)
- {
- printf("ERROR\nInvalid file path\n");
- exit(1);
- }
- struct stat statbuf;
- lstat(path,&statbuf);
- if(S_ISREG(statbuf.st_mode))
- {
- return 1;
- }
- else
- {
- printf("ERROR\nInvalid file path\nThe specified path does not lead to a valid file\n");
- exit(1);
- }
- close(fd);
- }
- int checkIntegerArgument(char* s)
- {
- for(int i = 0; i < strlen(s); i++)
- {
- if(s[i] < '0' || s[i] > '9')
- {
- return 0;
- }
- }
- return 1;
- }
- int checkTemplateFileVersion(char* path)
- {
- int fd = open(path,O_RDONLY);
- if(fd == -1)
- {
- perror("Fisierul nu s-a putut deschide!");
- exit(1);
- }
- char c;
- char c2;
- long cursor = lseek(fd,0,SEEK_SET);
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- char* p = charToString(c);
- char* p1 = charToString(c2);
- strcat(p1,p);
- int v = stringToDecimal(p1);
- if(v >= 12345 && v <= 54321)
- {
- return v;
- }
- else
- {
- printf("ERROR\nWrong file version\nIt must be 12345 and 54321\n");
- exit(1);
- }
- }
- int getNumberOfTemplates(char* path)
- {
- int fd = open(path,O_RDONLY);
- if(fd == -1)
- {
- perror("Fisierul nu s-a putut deschide!");
- exit(1);
- }
- char c;
- char c2;
- long cursor = lseek(fd,2,SEEK_SET);
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- char* p = charToString(c);
- char* p1 = charToString(c2);
- strcat(p1,p);
- int nrSabloane = stringToDecimal(p1);
- return nrSabloane;
- }
- void getFileMetaData(char* path, int bits, int* dimensions, int* offsets, int* noTemplates)
- {
- int fd = open(path,O_RDONLY);
- if(fd == -1)
- {
- perror("Fisierul nu s-a putut deschide!");
- exit(1);
- }
- int nrSabloane = getNumberOfTemplates(path);
- long cursor = lseek(fd,4,SEEK_SET);
- char c,c2;
- char* p;
- char* p1;
- char* pp;
- char* pp1;
- int count = 0;
- for(int i = 1; i <= nrSabloane; i++)
- {
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- p = charToString(c);
- p1 = charToString(c2);
- strcat(p1,p);
- int dim = stringToDecimal(p1);
- if(dim == bits)
- {
- (*dimensions) = dim;
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- p = charToString(c);
- p1 = charToString(c2);
- strcat(p1,p);
- int nrSab = stringToDecimal(p1);
- (*noTemplates) = nrSab;
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- p = charToString(c);
- p1 = charToString(c2);
- strcat(p1,p);
- pp = p1;
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- p = charToString(c);
- p1 = charToString(c2);
- strcat(p1,p);
- pp1 = p1;
- strcat(pp1,pp);
- int offsetInceput = stringToDecimal(pp1);
- (*offsets) = offsetInceput;
- printf("Dimensiunea sablon %d este %d | Numarul de sabloane este %d | Offsetul: %d \n",i,dim,nrSab,offsetInceput);
- }
- else
- {
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- p = charToString(c);
- p1 = charToString(c2);
- strcat(p1,p);
- int nrSab = stringToDecimal(p1);
- //(*noTemplates) = nrSab;
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- p = charToString(c);
- p1 = charToString(c2);
- strcat(p1,p);
- pp = p1;
- read(fd,&c,sizeof(char));
- read(fd,&c2,sizeof(char));
- p = charToString(c);
- p1 = charToString(c2);
- strcat(p1,p);
- pp1 = p1;
- strcat(pp1,pp);
- int offsetInceput = stringToDecimal(pp1);
- //(*offsets) = offsetInceput;
- }
- }
- }
- /* FUNCTIILE PRINCIPALE */
- void histogram(int noBits, char* path)
- {
- int fd = open(path,O_RDONLY);
- if(fd == -1)
- {
- perror("Fisierul nu s-a putut deschide\n");
- exit(1);
- }
- char buffer[16384];
- if(read(fd,&buffer,sizeof(buffer)) < 0)
- {
- printf("Nu am putut citi din fisier!\n");
- exit(4);
- }
- long cursor = lseek(fd,0,SEEK_SET);
- int* ap = calloc((1 << noBits), sizeof(int));
- int* aparitii = calloc((1 << noBits), sizeof(int));
- if(ap == NULL || aparitii == NULL)
- {
- printf("Eroare la alocarea memoriei\n");
- exit(4);
- }
- while(read(fd,&buffer,sizeof(buffer)) != 0)
- {
- for(int i = 0; i < sizeof(buffer); i++)
- {
- ap = getFreqForChar(buffer[i],noBits);
- for(int i = 0; i < (1 << noBits); i++)
- {
- aparitii[i] += ap[i];
- }
- }
- }
- for(int j = 0; j < (1 << noBits); j++)
- {
- char c = j;
- printf("%s: %d\n",charToString(c),aparitii[j]);
- }
- free(ap);
- free(aparitii);
- close(fd);
- }
- void longestSetSequence(char* path)
- {
- int fd = open(path,O_RDONLY);
- if(fd == -1)
- {
- printf("Fisierul nu s-a putut deschide!");
- exit(1);
- }
- char buffer[16348];
- if(read(fd,&buffer,sizeof(buffer)) < 0)
- {
- printf("Nu am putut citi din fisier!\n");
- exit(4);
- }
- long cursor = lseek(fd,0,SEEK_SET);
- int longestSequence = 0;
- int bytesRead = 0;
- int maxSequence = 0;
- int newSequence = 0;
- int begOfLongestSequence = 0;
- int zeroBefore = 0;
- while(read(fd,&buffer,sizeof(buffer)) != 0)
- {
- for(int i = 0; i < sizeof(buffer); i++)
- {
- findSequence(buffer[i],&longestSequence,&bytesRead,&maxSequence,&zeroBefore,&newSequence,&begOfLongestSequence);
- }
- }
- printf("SUCCES\nLength of the longest run: %d\nOffset: %d bytes + %d bits\n",longestSequence,begOfLongestSequence / 8, begOfLongestSequence % 8);
- }
- void templateFunctionV2(int bits, char* templatePath, char* filePath)
- {
- int fd = open(templatePath,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut citi din fisier!");
- exit(1);
- }
- int v = checkTemplateFileVersion(templatePath);
- printf("Versiune fisier: %d\n",v);
- int nrSabloane = getNumberOfTemplates(templatePath);
- printf("Tipuri de sabloane: %d\n",nrSabloane);
- int offsets;
- int dimensions;
- int noTemplates;
- getFileMetaData(templatePath,bits,&dimensions,&offsets,&noTemplates);
- int start = offsets;
- int dimension = dimensions;
- int noTemp = noTemplates;
- long cursor = lseek(fd,start,SEEK_SET);
- printf("START: %d\n",start);
- while(noTemp--)
- {
- char toRead[dimension];
- read(fd,toRead,sizeof(toRead) / 8);
- char toReadCopy[dimension/8][8];
- int nr1 = 0;
- int l = 0;
- int q = 0;
- while(q < dimension)
- {
- char* ss = charToString(toRead[q]);
- nr1++;
- if(nr1 <= dimension / 8)
- {
- strcpy(toReadCopy[nr1-1],ss);
- }
- else
- {
- break;
- }
- q++;
- }
- int fd2 = open(filePath,O_RDONLY);
- char template[dimension];
- for(int i = 0; i < dimension / 8; i++)
- {
- for(int j = 0; j < dimension; j++)
- {
- template[8 * i + j] = toReadCopy[i][j];
- template[dimension] = '\0';
- }
- }
- printf("\nSablonul: %s ",template);
- findTemplate(template,dimension,fd2);
- }
- printf("\n");
- }
- void templateFunctionV1(int bits, int templateNumber, char* templatePath, char* filePath)
- {
- int fd = open(templatePath,O_RDONLY);
- if(fd == -1)
- {
- printf("Nu s-a putut citi din fisier!");
- exit(1);
- }
- int v = checkTemplateFileVersion(templatePath);
- printf("Versiune fisier: %d\n",v);
- int nrSabloane = getNumberOfTemplates(templatePath);
- printf("Tipuri de sabloane: %d\n",nrSabloane);
- int* offsets = (int*)malloc(4 * sizeof(int));
- int* dimensions = (int*)malloc(4 * sizeof(int));
- int* noTemplates = (int*)malloc(4 * sizeof(int));
- //getFileMetaData(templatePath,&dimensions,&offsets,&noTemplates);
- int start;
- int dimension;
- int noTemp;
- switch(bits)
- {
- case 8: start = offsets[0]; dimension = dimensions[0]; noTemp = noTemplates[0]; break;
- case 16: start = offsets[1]; dimension = dimensions[1]; noTemp = noTemplates[1]; break;
- case 24: start = offsets[2]; dimension = dimensions[2]; noTemp = noTemplates[2]; break;
- case 32: start = offsets[3]; dimension = dimensions[3]; noTemp = noTemplates[3]; break;
- }
- long cursor = lseek(fd,start,SEEK_SET);
- char toRead[dimension];
- char toReadCopy[dimension/8][8];
- int i = 0;
- while(i < noTemp)
- {
- read(fd,toRead,sizeof(toRead) / 8);
- if(i == templateNumber)
- {
- int nr1 = 0;
- int l = 0;
- int q = 0;
- while(q < dimension)
- {
- char* ss = charToString(toRead[q]);
- nr1++;
- if(nr1 <= dimension / 8)
- {
- strcpy(toReadCopy[nr1-1],ss);
- }
- else
- {
- break;
- }
- q++;
- }
- int fd2 = open(filePath,O_RDONLY);
- char template[dimension];
- for(int i = 0; i < dimension / 8; i++)
- {
- for(int j = 0; j < dimension; j++)
- {
- template[8 * i + j] = toReadCopy[i][j];
- template[dimension] = '\0';
- }
- }
- printf("\nSablonul: %s ",template);
- findTemplate(template,dimension,fd2);
- }
- i++;
- }
- printf("\n");
- }
- /* FUNCTII AUXILIARE */
- int createMask(int noBits)
- {
- int rez = 0;
- for(int i = 0; i < noBits; i++)
- {
- rez |= 1 << i;
- }
- return rez;
- }
- char* charToString(char c)
- {
- int valC = (int)c;
- char* octet = (char*)malloc(9 * sizeof(char));
- if(octet == NULL)
- {
- printf("Eroare la alocarea memoriei!");
- exit(4);
- }
- octet[0] = '\0';
- for(int z = (1 << 7); z > 0; z >>= 1)
- {
- strcat(octet,(((valC & z) == z) ? "1" : "0"));
- }
- return octet;
- }
- int stringToDecimal(char* c)
- {
- int decValue = 0;
- int putere = 0;
- for(int i = strlen(c) - 1; i >= 0; i--)
- {
- int k = c[i] - '0';
- k = k <<= putere;
- putere++;
- decValue += k;
- }
- return decValue;
- }
- void shiftRight(char c[], int bits)
- {
- int i = 0;
- while(i < bits)
- {
- memmove(c+1,c,strlen(c) - 1);
- c[0] = '0';
- i++;
- }
- }
- char* shiftLeft(char *c, char b, int dim)
- {
- memmove(c,c+1,dim);
- c[dim - 1] = b;
- return c;
- }
- int* getFreqForChar(char b, int bits)
- {
- int* aparitii = (int*)calloc((1<<bits) , sizeof(int));
- if(aparitii == NULL)
- {
- printf("Eroare la alocarea memoriei!");
- exit(4);
- }
- char* c = charToString(b);
- while (strcmp(c,"00000000") != 0)
- {
- int i = 0;
- while(i < 8 / bits)
- {
- int extract = stringToDecimal(c);
- int val = createMask(bits);
- int ap = extract & val;
- aparitii[ap]++;
- shiftRight(c,bits);
- i++;
- }
- }
- return aparitii;
- }
- void findSequence(char c, int* longestSequence, int* bytesRead, int* maxSequence, int* zeroBefore, int* newSeq, int* begOfLongestSeq)
- {
- int valC = (int)c;
- int var = *maxSequence;
- for(int j = 7; j >= 0; j--)
- {
- int copy = (valC >> j);
- if((copy & 1) != 0)
- {
- if((*zeroBefore) == 0)
- {
- (*zeroBefore) = 1;
- (*newSeq) = (*bytesRead);
- }
- (*maxSequence)++;
- if((*maxSequence) > (*longestSequence))
- {
- (*begOfLongestSeq) = (*newSeq);
- (*longestSequence) = (*maxSequence);
- }
- }
- else
- {
- (*zeroBefore) = 0;
- (*maxSequence) = 0;
- }
- (*bytesRead)++;
- }
- }
- void umplere(char c, char* sir, char* sablon, int dim, int* i, int* nr)
- {
- int valC = (int)c;
- for(unsigned char z = (1 << 7); z > 0; z >>= 1)
- {
- if(*i < dim)
- {
- strcat(sir,(((valC & z) == z) ? "1" : "0"));
- (*i)++;
- if(((*i) == dim) && (strcmp(sir,sablon) == 0))
- {
- (*nr)++;
- }
- }
- else
- {
- if((valC & z) == z)
- {
- sir = shiftLeft(sir,'1',dim);
- }
- else
- {
- sir = shiftLeft(sir,'0',dim);
- }
- if(strcmp(sablon,sir) == 0)
- {
- (*nr)++;
- }
- }
- }
- }
- void findTemplate(char* sablon, int dim, int fd)
- {
- char* sir = (char*)malloc(dim * sizeof(char));
- char c;
- int i = 0;
- int nr = 0;
- while(read(fd,&c,sizeof(char)) != 0)
- {
- umplere(c,sir,sablon,dim,&i,&nr);
- }
- printf(" | Numar de aparitii: %d",nr);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement