From 96d374eeb736d6bcd1be6611f25b9d62efc77818 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Fri, 25 Nov 2022 00:14:31 +0100 Subject: [PATCH] huffman progress, scripting --- compile-all.sh | 4 +- compile-and-run.sh | 4 +- compile.sh | 4 - huffman/bin/huffman | Bin 21568 -> 21464 bytes huffman/huffman.c | 354 ++++++++++++++++++++++---------------------- 5 files changed, 182 insertions(+), 184 deletions(-) diff --git a/compile-all.sh b/compile-all.sh index 7a61f4a..9392fa4 100755 --- a/compile-all.sh +++ b/compile-all.sh @@ -3,7 +3,9 @@ returnCode=0 echo -e "compiling all files in working directory $(pwd)\n" for file in $(/bin/ls *.c); do - ./compile.sh $file; + echo "compiling $file ..." + noext=$(echo "$file" | cut -f 1 -d '.') + gcc $file -o bin/$noext -lm if [ "$?" -ne 0 ] then echo -e "\nERROR: could not compile $file !\n"; diff --git a/compile-and-run.sh b/compile-and-run.sh index 2b5dd18..0a989da 100755 --- a/compile-and-run.sh +++ b/compile-and-run.sh @@ -1,4 +1,6 @@ #!/bin/bash noext=$(echo "$1" | cut -f 1 -d '.') -./compile.sh $1 +echo "compiling $1 ..." +noext=$(echo "$1" | cut -f 1 -d '.') +gcc $1 -o bin/$noext -lm ./bin/$noext $2 $3 $4 $5 $6 $7 $8 $9 diff --git a/compile.sh b/compile.sh index 688a322..cfad674 100755 --- a/compile.sh +++ b/compile.sh @@ -1,8 +1,4 @@ #!/bin/bash echo "compiling $1 ..." noext=$(echo "$1" | cut -f 1 -d '.') - -# to treat warnings as errors, use the following line -#gcc $1 -o bin/$noext -lm -Werror=format -# gcc $1 -o bin/$noext -lm diff --git a/huffman/bin/huffman b/huffman/bin/huffman index 03ecade5fe3a9d81ef23acc1cc6bcfeafba49f1b..04e95087fdc7cb77de37bf155c99d83cf997b65e 100755 GIT binary patch delta 4627 zcmb7IeRPx68Gmn@&?bOs3N;O1O@YpUkftgq^{}?k!V8Qq(H*N(DQ#+VjA=;|1&>?m zLOhM3T6V!9V`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f8^2tDA<%V$+ElIJ!hQP4Py&@akx(3xt= z)HxF8^g6A{;fo-z+Wn~fFg~nZEJack_{gb5v8sT)X*Hv8i#OMZnYO^G&3}=y=fQqD1J;ll>z3GE#jqg z*i&-UbR7$siYL$ow@Qkb7rIMi`%B1+LtgmSh9G?WhD!%f8ZTOeaFAi??@0=ir8agA z(n@9aH|WRtE8;RUSj%LV#OEBSDVN#zXl<{+Z-5o=qu~o!7;8Tim&wisl*Zxhal#em zYxK_)0cQ1``l9G5!rbQ8t(HdbLq1u3p<%W5YIAViSol zgxp1cQT|=I!90_$fz6}`*6-TF*jZ?+$~ASlG;SC4R~4~dnB}u`cM%K15uE#=ALlZd u#d!*xdy3d)ID&H_^yB;t%) is the index of the first // non-option argument. If it is >= argc, there were no non-option arguments. if (verbose) - printf("selected file: %s\n", filestring); + printf("selected file: %s\n", filestring); if (filestring) { - if(debug) - printf("[DEBUG]processing given file argument.\n"); - // open the given file in binary mode, I want this to work with any files, - // not just textfiles. - fptrR = fopen(filestring, "rb"); - if (fptrR == NULL) { - fprintf(stderr, "The given file does not exist or is unavailable.\n"); - exit(EXIT_FAILURE); - } - // causes bugs. - fseek(fptrR, 0L, SEEK_END); - filelen = ftell(fptrR); - fseek(fptrR, 0L, SEEK_SET); + if(debug) + printf("[DEBUG]processing given file argument.\n"); + // open the given file in binary mode, I want this to work with any files, + // not just textfiles. + fptrR = fopen(filestring, "rb"); + if (fptrR == NULL) { + fprintf(stderr, "The given file does not exist or is unavailable.\n"); + exit(EXIT_FAILURE); + } + // causes bugs. + fseek(fptrR, 0L, SEEK_END); + filelen = ftell(fptrR); + fseek(fptrR, 0L, SEEK_SET); } else { - // empty filestring or filestring is NULL - fprintf(stderr, "Usage: %s [-dvhx -f] [file]\n", argv[0]); - exit(EXIT_FAILURE); + // empty filestring or filestring is NULL + fprintf(stderr, "Usage: %s [-dvhx -f] [file]\n", argv[0]); + exit(EXIT_FAILURE); } // TODO check file size and spit a "what the heck im not a 10x dev, do a if(verbose) - printf("filesize: %ldB\n", filelen); + printf("filesize: %ldB\n", filelen); if (extract_mode) { - printf("extracting is not yet implemented.\n"); - // decompress the file + printf("extracting is not yet implemented.\n"); + // decompress the file } else { - // compress the file - if (verbose) - printf("compressing file...\n"); + // compress the file + if (verbose) + printf("compressing file...\n"); - // frequency analysis + // frequency analysis - uint8_t buf[512]; + uint8_t buf[512]; - // dump start of file if debugging - // FIXME add conditions if the part to print is smaller than 512B - if(debug){ - printf("[DEBUG]First 512 bytes are:\n"); - fread(buf, 1, 512, fptrR); - for(int i=0;i<512;i++){ - if(i%16==0) - printf("%08x\t", i); - printf("%02x ", buf[i]); - if(i%16==7) - printf(" "); - if(i%16==15){ - printf("\n"); - } - } - } + // dump start of file if debugging + // FIXME add conditions if the part to print is smaller than 512B + if(debug){ + printf("[DEBUG]First 512 bytes are:\n"); + fread(buf, 1, 512, fptrR); + for(int i=0;i<512;i++){ + if(i%16==0) + printf("%08x\t", i); + printf("%02x ", buf[i]); + if(i%16==7) + printf(" "); + if(i%16==15){ + printf("\n"); + } + } + } - uint64_t occurences[256]; - for(int i=0;i<256;i++){ - occurences[i]=0; - } - // FIXME doesnt loop through full file! only 50% for larger files - // Backup occurence counting algorithm - /* - while(!feof(fptrR)){ - fseek(fptrR, 512, SEEK_CUR); - if(fread(buf, 1, 512, fptrR)){ - for(int i=0;i<512;i++){ - occurences[buf[i]]++; - } - } - else{ - fprintf(stderr, "Error when processing file.\n"); - exit(EXIT_FAILURE); - } + uint64_t occurences[256]; + for(int i=0;i<256;i++){ + occurences[i]=0; + } + // FIXME doesnt loop through full file! only 50% for larger files + // Backup occurence counting algorithm + /* + while(!feof(fptrR)){ + fseek(fptrR, 512, SEEK_CUR); + if(fread(buf, 1, 512, fptrR)){ + for(int i=0;i<512;i++){ + occurences[buf[i]]++; + } + } + else{ + fprintf(stderr, "Error when processing file.\n"); + exit(EXIT_FAILURE); + } - // advance filepointer 512 bytes foreward. If not possible, set endOfFile flag. - // FIXME - offset += 512; - } - */ - // backup - /* - while(1){ - fseek(fptrR, 512, SEEK_CUR); // this line seems the be making the most problems + // advance filepointer 512 bytes foreward. If not possible, set endOfFile flag. + // FIXME + offset += 512; + } + */ + // backup + while(1){ + fseek(fptrR, 512, SEEK_CUR); // this line seems the be making the most problems - // On success, fread() and fwri)te() return the number of items read or written. - // This number equals the number of bytes transferred only when size is - // 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero). - // fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. + // On success, fread() and fwri)te() return the number of items read or written. + // This number equals the number of bytes transferred only when size is + // 1. If an error occurs, or the end of the file is reached, the return value is a short item count (or zero). + // fread() does not distinguish between end-of-file and error, and callers must use feof(3) and ferror(3) to determine which occurred. - // FIXME )This is a buggy mess - if(512 == fread(buf, 1, 512, fptrR)){ - for(int i=0;i<512;i++){ - occurences[buf[i]]++;) - } - }) - else if(0 == fread(buf, 1, 512, fptrR)){ - if(debug) - printf("[DEBUG]fread returned 0! ftell for current position is %lu\n", ftell(fptrR)); - break; - } - else{ - fprintf(stderr, "Error when processing file: %lu, At offset %lu\n",fread(buf, 1, 512, fptrR), ftell(fptrR)); - exit(EXIT_FAILURE); - } - if(ftell(fptrR) > filelen) { - // ??? unknown error - fprintf(stderr, "tried reading further than the file is long somehow?\n"); - exit(EXIT_FAILURE); - } - } - */ - // ALMOST WORKS! ~200 bytes lost for a 10M file!!! - uint8_t bufMini[1]; - while(1){ - if(fread(bufMini, 1, 1, fptrR)){ - occurences[buf[0]]++; - } - else{ - if(ferror(fptrR)){ - fprintf(stderr, "encountered error when reading file.\n"); - exit(EXIT_FAILURE); - } - } - fseek(fptrR, 1, SEEK_SET); - if(ferror(fptrR)){ - fprintf(stderr, "encountered error when reading file.\n"); - exit(EXIT_FAILURE); - } - else if(feof(fptrR)){ - continue; - } - } - if(debug){ - printf("Occurences (Hex):\n"); - for(int i=0;i<256;i++){ - if(i%4==0) - printf("\n"); - printf("0x%02x: %016lx\t", i, occurences[i]); - } - printf("\n\nfile length(by pointer):\t\t%luB\n", filelen); - long long int addedUpOccurences = 0; // FIXME might not be enough storage for larger files! - for(int i=0;i<256;i++){ - addedUpOccurences += occurences[i]; - } - printf("file length(added up occurences):\t%lldB\n", addedUpOccurences); - } + // FIXME )This is a buggy mess + if(512 == fread(buf, 1, 512, fptrR)){ + for(int i=0;i<512;i++){ + occurences[buf[i]]++; + } + } + else if(0 == fread(buf, 1, 512, fptrR)){ + if(debug) + printf("[DEBUG]fread returned 0! ftell for current position is %lu\n", ftell(fptrR)); + break; + } + else{ + fprintf(stderr, "Error when processing file: %lu, At offset %lu\n",fread(buf, 1, 512, fptrR), ftell(fptrR)); + exit(EXIT_FAILURE); + } + if(ftell(fptrR) > filelen) { + // ??? unknown error + fprintf(stderr, "tried reading further than the file is long somehow?\n"); + exit(EXIT_FAILURE); + } + } + // ALMOST WORKS! ~200 bytes lost for a 10M file!!! + uint8_t bufMini[1]; + // while(1){ + // if(fread(bufMini, 1, 1, fptrR)){ + // occurences[buf[0]]++; + // } + // else{ + // if(ferror(fptrR)){ + // fprintf(stderr, "encountered error when reading file.\n"); + // exit(EXIT_FAILURE); + // } + // } + // fseek(fptrR, 1, SEEK_SET); + // if(ferror(fptrR)){ + // fprintf(stderr, "encountered error when reading file.\n"); + // exit(EXIT_FAILURE); + // } + // else if(feof(fptrR)){ + // break; + // } + // } + if(debug){ + printf("Occurences (Hex):\n"); + for(int i=0;i<256;i++){ + if(i%4==0) + printf("\n"); + printf("0x%02x: %016lx\t", i, occurences[i]); + } + printf("\n\nfile length(by pointer):\t\t%luB\n", filelen); + long long int addedUpOccurences = 0; // FIXME might not be enough storage for larger files! + for(int i=0;i<256;i++){ + addedUpOccurences += occurences[i]; + } + printf("file length(added up occurences):\t%lldB\n", addedUpOccurences); + } - if(verbose) - printf("\n\nDone calculating occurences of bytes.\n"); + if(verbose) + printf("\n\nDone calculating occurences of bytes.\n"); - // TODO - // calculate the frequencies of the bytes. - double frequencies[256]; - for(int i=0;i<256;i++){ - frequencies[i]=((double)occurences[i]/(double)filelen)*100; // calculate frequencies of bytes in percent (example: 05.23 (%)) - } - if(debug){ - printf("Frequencies:\n"); - for(int i=0;i<256;i++){ - if(i%8==0) - printf("\n"); - printf("0x%02x: %05.02f%%\t", i, frequencies[i]); - } - double addedUpFrequencies = 0; - for(int i=0;i<256;i++){ - addedUpFrequencies += frequencies[i]; - } - printf("\n\nadded up frequencies: %05.02f%%\n",addedUpFrequencies); - } + // TODO + // calculate the frequencies of the bytes. + double frequencies[256]; + for(int i=0;i<256;i++){ + frequencies[i]=((double)occurences[i]/(double)filelen)*100; // calculate frequencies of bytes in percent (example: 05.23 (%)) + } + if(debug){ + printf("Frequencies:\n"); + for(int i=0;i<256;i++){ + if(i%8==0) + printf("\n"); + printf("0x%02x: %05.02f%%\t", i, frequencies[i]); + } + double addedUpFrequencies = 0; + for(int i=0;i<256;i++){ + addedUpFrequencies += frequencies[i]; + } + printf("\n\nadded up frequencies: %05.02f%%\n",addedUpFrequencies); + } } fclose(fptrR); printf("\n");