Advertisement
Guest User

Untitled

a guest
Mar 30th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.30 KB | None | 0 0
  1. /*Katona David - 30224
  2. */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fcntl.h> //O_RDONLY
  6. #include <string.h> //strcmp
  7. #include <unistd.h> //open & read
  8. #include <dirent.h>
  9. #include <limits.h>
  10. #include <sys/stat.h>
  11.  
  12.  
  13. int pow2(int a, int b)
  14. {
  15. int rez = 1;
  16. for( int i = 1; i <= b; i++)
  17. rez *= a;
  18. return rez;
  19. }
  20. void intToBin(int x,int bitNumber)
  21. {
  22. for(int i = bitNumber -1 ; i >= 0; i--)
  23. {
  24. if (i%7==0) printf(" ");
  25. int k = x >> i;
  26. if((k & 1) == 1 )
  27. printf("1");
  28. else
  29. printf("0");
  30. }
  31. printf("\n");
  32. }
  33. void histogram(char* path, int bitNr)
  34. {
  35. int fd;
  36. char byteRead;
  37. int arraySize = pow2(2,bitNr);
  38. int *rez = calloc(arraySize, 4);
  39.  
  40. if ((fd = open(path, O_RDONLY)) < 0)
  41. {
  42. perror("Open failed.");
  43. return;
  44. }
  45. while(read(fd, &byteRead, 1) > 0)
  46. {
  47. for( int i = 0; i < 8/bitNr ; i++)
  48. {
  49. int x = (1 << bitNr) - 1;
  50. int y = byteRead & x;
  51. rez[y]++;
  52. byteRead >>= bitNr;
  53. }
  54. }
  55. printf("\nSUCCESS\n");
  56. for(int i = 0; i < arraySize; i++)
  57. {
  58. intToBin(i,bitNr);
  59. printf(": %d\n", rez[i]);
  60. }
  61. }
  62.  
  63. void runs(char* path)
  64. {
  65. int fd, maxx = 0, curr = 0, start=0, byte, bits;
  66. char byteRead;
  67. if ((fd = open(path, O_RDONLY)) < 0)
  68. {
  69. perror("Open failed.");
  70. return;
  71. }
  72. int fileS = lseek( fd, 0, SEEK_END)-1;
  73. while(fileS>0)
  74. {
  75. lseek( fd, fileS, SEEK_SET);
  76. read( fd, &byteRead, 1);
  77. for( int i = 0; i < 8; i++)
  78. {
  79. int x = byteRead & 1;
  80. if(x == 1)
  81. {
  82. curr++;
  83. start = 1;
  84. if( curr > maxx)
  85. {
  86. maxx = curr;
  87. byte = fileS;
  88. }
  89. }
  90. else
  91. {
  92. curr = 0;
  93. if(maxx == curr && start==1)
  94. {
  95. bits = 8-i;
  96. start = 0;
  97. }
  98. }
  99. byteRead >>= 1;
  100. }
  101. fileS--;
  102. }
  103. printf("SUCCESS\nLength of the longest run: %d\nOffset: %d bytes + %d bits\n",maxx,byte,bits);
  104. }
  105. unsigned long swapBits(unsigned long t, int bitNr)
  106. {
  107. unsigned long swapped=0;
  108.  
  109. if (bitNr==8)
  110. {
  111. swapped=((t<<8)&0xff00)|((t>>8)&0x00ff);
  112. }
  113. else if (bitNr==12)
  114. {
  115. swapped=((t<<16)&0xff0000) | (t&0x00ff00) | ((t>>16)&0x0000ff);
  116. }
  117. else if (bitNr==16)
  118. {
  119. swapped=((t<<24)&0xff000000) | ((t<<8)&0x00ff0000) | ((t>>8)&0x0000ff00) | ((t>>24)&0x000000ff);
  120. }
  121. else if (bitNr==24)
  122. {
  123. swapped=(((t<<40)&0xff0000000000) | ((t<<24)&0x00ff00000000) | ((t<<8)&0x0000ff000000) | ((t>>8)&0x000000ff0000) | ((t>>24)&0x000000ff00) | ((t>>40)&0x0000000000ff));
  124. }
  125. else if (bitNr==32)
  126. {
  127. 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);
  128. }
  129. return swapped;
  130.  
  131. }
  132. int find_match(unsigned int sablon, unsigned long t, int bitNr)
  133. {
  134. unsigned long b=t;
  135. unsigned int sablonB=sablon;
  136. int correct, occ=0;
  137. for (int i=1; i<=bitNr; i++)
  138. {
  139. correct=1;
  140. sablon=sablonB;
  141. t=b>>(bitNr+1-i);
  142. for (int j=1; j<=bitNr; j++)
  143. {
  144. if ((t&1) == (sablon&1))
  145. {
  146. t>>=1;
  147. sablon>>=1;
  148. }
  149. else
  150. {
  151. correct=0;
  152. break;
  153. }
  154. }
  155. if (correct==1)
  156. occ++;
  157. }
  158. return occ;
  159. }
  160. void template_fn(char* tmPath, char* filePath, int bitNr, int tempNr)
  161. {
  162. int fdT, fdF, offset, occ=0, fileS, fileSb;
  163. short ver, no_of_cat, size_of_cat, no_of_temp;
  164. unsigned int sablon;
  165. unsigned long t;
  166. if ((fdT = open(tmPath, O_RDONLY)) < 0)
  167. {
  168. perror("ERROR\nInvalid file path\n");
  169. return;
  170. }
  171. if ((fdF = open(filePath, O_RDONLY)) < 0)
  172. {
  173. perror("ERROR\nInvalid file path\n");
  174. return;
  175. }
  176. read(fdT, &ver, 2);
  177. if (ver>54321 || ver<12345)
  178. {
  179. printf("ERROR\nInvalid template_fn file\nWrong version");
  180. return;
  181. }
  182. read(fdT, &no_of_cat, 2);
  183. while(read(fdT, &size_of_cat, 2)>0)
  184. {
  185. if (size_of_cat==bitNr)
  186. {
  187. read(fdT, &no_of_temp, 2); //no of template_fns in category
  188. read(fdT, &offset, 4);
  189. break;
  190. }
  191. else
  192. {
  193. lseek(fdT, 6, SEEK_CUR);
  194. no_of_cat--;
  195. }
  196. }
  197. if (tempNr>no_of_temp)
  198. {
  199. printf("ERROR\nInvalid template_fn number\n");
  200. return;
  201. }
  202. if (no_of_cat==0)
  203. {
  204. printf("ERROR\nInvalid template_fn category\n");
  205. return;
  206. }
  207.  
  208. if (tempNr==0) //do all tests
  209. {
  210. lseek(fdT, offset, SEEK_SET); //cursor in front of first test
  211. printf("\nSUCCESS\n");
  212. for (int i=1; i<=no_of_temp; i++)
  213. {
  214. occ=0;
  215. sablon^=sablon;
  216. read(fdT, &sablon, bitNr/8);
  217. if (bitNr!=8)
  218. sablon=swapBits(sablon, bitNr/2);
  219. fileS = lseek( fdF, 0, SEEK_END);
  220. fileSb=fileS;
  221. lseek(fdF, 0, SEEK_SET);
  222. while(fileS>=bitNr/4)
  223. {
  224. read(fdF, &t, bitNr/4);
  225. t=swapBits(t, bitNr);
  226. occ+=find_match(sablon, t, bitNr);
  227. fileS-=bitNr/8;
  228. lseek(fdF, -(bitNr/8), SEEK_CUR);
  229. }
  230. if (fileSb%(bitNr/8)!=0) //leftover bytes in file
  231. {
  232. lseek(fdF, -(bitNr/4), SEEK_END);
  233. read(fdF, &t, bitNr/4);
  234. t=swapBits(t, bitNr);
  235. occ+=find_match(sablon, t, bitNr);
  236. }
  237. printf("Occurences of template_fn %d (%x) of length %d: %d\n", i, sablon, bitNr, occ);
  238. }
  239. }
  240. else //do one test
  241. {
  242. lseek(fdT, (offset + (bitNr/8)*(tempNr-1)), SEEK_SET);
  243. printf("\nSUCCESS\n");
  244. occ=0;
  245. fileS = lseek( fdF, 0, SEEK_END);
  246. fileSb=fileS;
  247. sablon^=sablon;
  248. read(fdT, &sablon, bitNr/8);
  249. if (bitNr!=8)
  250. sablon=swapBits(sablon, bitNr/2);
  251. lseek(fdF, 0, SEEK_SET);
  252. while(fileS>=bitNr/4)
  253. {
  254. read(fdF, &t, bitNr/4);
  255. t=swapBits(t, bitNr);
  256. occ+=find_match(sablon, t, bitNr);
  257. fileS-=bitNr/8;
  258. lseek(fdF, -(bitNr/8), SEEK_CUR);
  259. }
  260. if (fileSb%(bitNr/8)!=0) //leftover bytes in file
  261. {
  262. lseek(fdF, -(bitNr/4), SEEK_END);
  263. read(fdF, &t, bitNr/4);
  264. t=swapBits(t, bitNr);
  265. occ+=find_match(sablon, t, bitNr);
  266. }
  267. printf("\nOccurences of template_fn %d (%x) of length %d: %d\n", tempNr, sablon, bitNr, occ);
  268. }
  269. }
  270. void list(int rec, char* filter, char* dirPath)
  271. {
  272. char op[256], path[256], crit[10];
  273. struct dirent *de;
  274. struct stat st;
  275. DIR* dir=opendir(dirPath);
  276. if(dir==0)
  277. {
  278. perror("ERROR\nCould not open directory\n");
  279. return;
  280. }
  281. printf("\nSUCCESS\n")
  282.  
  283. strcpy(op, filter);
  284. for(int i=0; i<4; ++i) crit[i]=op[i];
  285. crit[4]=0;
  286. if(strcmp("size", crit)==0)
  287. {
  288. int k;
  289. char s[25];
  290. strcpy(s,op+13);
  291. k=atoi(s);
  292. while((de=readdir(dir)))
  293. {
  294. char nume[256], num[256];
  295. strcpy(nume, path);
  296. strcpy(num, "/");
  297. strcat(num, de->d_name);
  298. strcat(nume, num);
  299. stat(nume, &st);
  300. int sz=st.st_size;
  301. if(sz>=k)
  302. {
  303. printf("%s\n", de->d_name);
  304. }
  305. }
  306. }
  307. else if(strcmp("name", crit)==0)
  308. {
  309. char eq[256];
  310. strcpy(eq, op+14);
  311. while((de=readdir(dir)))
  312. {
  313. char nume[256];
  314. strcpy(nume, de->d_name);
  315. if(strstr(nume, eq)!=NULL)
  316. {
  317. printf("%s\n", de->d_name);
  318. }
  319. }
  320. }
  321. else if(strcmp("temp", crit)==0)
  322. {
  323. while((de=readdir(dir)))
  324. {
  325. char nume[256], num[256];
  326. strcpy(nume, path);
  327. strcpy(num, "/");
  328. strcat(num, de->d_name);
  329. strcat(nume, num);
  330. lstat(nume, &st);
  331. if(S_ISREG(st.st_mode))
  332. {
  333. printf("%s\n", nume);
  334. }
  335. }
  336. }
  337. else if(strcmp("hist", crit)==0)
  338. {
  339. char s[10];
  340. strcpy(s, op+12);
  341. int k=atoi(s);
  342. if(k!=1 && k!=2 && k!=4 && k!=8)
  343. {
  344. printf("Invalid value for N");
  345. return ;
  346. }
  347. while((de=readdir(dir)))
  348. {
  349. char nume[256], num[256], p;
  350. strcpy(nume, path);
  351. strcpy(num, "/");
  352. strcat(num, de->d_name);
  353. strcat(nume, num);
  354. stat(nume, &st);
  355. int sz=st.st_size;
  356. sz*=8;
  357. sz/=100;
  358. int nrb, val, v[100], minim=INT_MAX, maxim=0;
  359. nrb--;
  360. for(int i=0; i<100; ++i) v[i]=0;
  361. int f=open(nume, O_RDONLY);
  362. if(f!=-1)
  363. {
  364. while(read(f, &p, 1)>0)
  365. {
  366. int i=0;
  367. while(i<8/k)
  368. {
  369. nrb=1<<k;
  370. val=k & nrb;
  371. v[val]++;
  372. i++;
  373. p>>=k;
  374. }
  375. }
  376. for(int i=0; i<100; ++i)
  377. if(v[i])
  378. {
  379. if(maxim<v[i]) maxim=v[i];
  380. if(minim>v[i]) minim=v[i];
  381. }
  382. if(maxim-minim<sz && minim!=INT_MAX)
  383. {
  384. printf("%s\n", nume);
  385. }
  386. }
  387. }
  388. }
  389. }
  390. int main(int argc, char** argv)
  391. {
  392. int bits, tempNr;
  393. if(strcmp(argv[1], "histogram")==0)
  394. {
  395. if (argc!=4)
  396. {
  397. printf("USAGE: %s histogram number_of_bits /path/to/file\n", argv[0]);
  398. exit(0);
  399. }
  400. bits=atoi(argv[2]);
  401. if (bits!=1 && bits!=2 && bits!=4 && bits!=8)
  402. perror("ERROR\nInvalid number of bits\nSupported values are: 1, 2, 4, 8\n");
  403. else
  404. histogram(argv[3], bits);
  405. }
  406. else if(strcmp(argv[1], "runs")==0)
  407. {
  408. if (argc!=3)
  409. {
  410. printf("USAGE: %s runs /path/to/file\n", argv[0]);
  411. exit(0);
  412. }
  413. else
  414. runs(argv[2]);
  415. }
  416. else if(strcmp(argv[1], "template_fn")==0)
  417. {
  418. if (argc!=5 && argc!=6)
  419. {
  420. 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");
  421. exit(0);
  422. }
  423. bits=atoi(argv[2]);
  424. if (bits!=8 && bits!=16 && bits!=24 && bits!=32)
  425. perror("ERROR\nInvalid number of bits\nSupported values are: 8, 16, 24, 32\n");
  426. else if (argc==5)
  427. {
  428. template_fn(argv[3], argv[4], bits, 0);
  429. }
  430. else if (argc==6)
  431. {
  432. tempNr=atoi(argv[3]);
  433. if (tempNr<=0)
  434. {
  435. perror("ERROR\n<number_of_template_fn> has to be grater than 0\n");
  436. return 0;
  437. }
  438. else
  439. template_fn(argv[4], argv[5], bits, tempNr);
  440. }
  441. }
  442. else if(strcmp(argv[1], "list")==0)
  443. {
  444. if (argc!=4 && argc!=5)
  445. {
  446. printf("USAGE: ./t1 list [recursive] <filtering_options> <dir_path>");
  447. exit(0);
  448. }
  449. else if (argc==4) //non-recursive
  450. {
  451. list(0, argv[2], argv[3]);
  452. }
  453. else if (argc==5) //recursive
  454. {
  455. list(1, argv[3], argv[4]);
  456. }
  457. }
  458. return 0;
  459. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement