SHARE
TWEET

Untitled

a guest Jun 25th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <limits.h>
  4. #include "Code.h"
  5.  
  6. Code* new_code(unsigned char* s, int size) {    
  7.     Code* tmp = (Code*)malloc(sizeof(Code));
  8.     tmp->s = s;
  9.     tmp->size = size;
  10.     return tmp;
  11. }
  12.  
  13. void countFreq(int* freqTable, FILE* in) {        
  14.     int c;
  15.     while ((c = fgetc(in)) != EOF) {
  16.         freqTable[c]++;
  17.     }
  18. }
  19.  
  20. void base(Stack**stack, int* freqTable) {      
  21.     for (int i = 0; i < 256; i++) {
  22.         if (freqTable[i]) {
  23.             push(stack, new_tree(i, freqTable[i]));
  24.         }
  25.     }
  26. }
  27.  
  28. void Resize(Code* buffer) {              
  29.     buffer->size++;
  30.     int isOverflow = buffer->size % CHAR_BIT;
  31.     if (isOverflow == 1) {
  32.         int bytes = buffer->size / CHAR_BIT + 1;
  33.         buffer->s = (unsigned char*)realloc(buffer->s, bytes);
  34.         buffer->s[bytes - 1] = 0;
  35.     }
  36.  
  37. }
  38.  
  39. unsigned char* copyCode(Code* buffer) {            
  40.     int bytes = (buffer->size - 1) / CHAR_BIT + 1;
  41.     unsigned char* new_s = (unsigned char*)malloc(bytes);
  42.     for (int i = 0; i < bytes; i++) {
  43.         new_s[i] = buffer->s[i];
  44.     }
  45.     return new_s;
  46. }
  47.  
  48. void t_code(FILE* in, FILE* out, Code** codeTable, BitStream* bitStream) {  
  49.     int c, i;
  50.     while ((c = fgetc(in)) != EOF) {
  51.         for (i = 0; i < codeTable[c]->size / CHAR_BIT; i++) {
  52.             writeBits(codeTable[c]->s[i], CHAR_BIT, bitStream, out);
  53.         }
  54.         writeBits(codeTable[c]->s[i], codeTable[c]->size % CHAR_BIT, bitStream, out);
  55.     }
  56.     if (bitStream->size) {
  57.         fputc(bitStream->byte << (CHAR_BIT - bitStream->size), out);
  58.     }
  59. }
  60.  
  61. void writeSize(unsigned int size, FILE* out) {    
  62.     fwrite(&size, sizeof(unsigned int), 1, out);
  63. }
  64.  
  65. void freeCodeTable(Code** codeTable) {
  66.     for (int i = 0; i < 256; i++) {
  67.         if (codeTable[i]) {
  68.             free(codeTable[i]->s);
  69.             free(codeTable[i]);
  70.         }
  71.     }
  72. }
  73.  
  74. void code(FILE* in, FILE* out) {      
  75.     int k;
  76.     int freqTable[256];
  77.     Stack* stack = NULL;
  78.     Code* codeTable[256];
  79.     Tree* codeTree = NULL;
  80.     BitStream* bitStream = new_bit();
  81.     memset(freqTable, 0, sizeof(int) * 256);
  82.     memset(codeTable, NULL, sizeof(Code*) * 256);
  83.  
  84.     while ((k = fgetc(in)) != EOF) {
  85.         freqTable[k]++;
  86.     }
  87.     base(&stack, freqTable);
  88.     codeTree = CodeTree(&stack);
  89.     if (codeTree) {
  90.         writeSize(codeTree->f, out);
  91.     }
  92.     writeTree(codeTree, bitStream, out);
  93.     CodeTable(codeTable, codeTree, new_code(NULL, 0));
  94.     fseek(in, 3, SEEK_SET);
  95.     t_code(in, out, codeTable, bitStream);
  96.     free(bitStream);
  97.     freeCodeTable(codeTable);
  98.     freeTree(codeTree);
  99. }
  100.  
  101. int readSize(FILE* in, unsigned int* size) {    
  102.     return fread(size, sizeof(unsigned int), 1, in);
  103. }
  104.  
  105. void decodeText(Tree* codeTree, FILE* in, FILE* out, unsigned int size, BitStream* bitStream) {    
  106.     Tree* tmpCodeTree;
  107.     for (unsigned int i = 0; i < size; i++) {
  108.         tmpCodeTree = codeTree;
  109.         while (tmpCodeTree->left) {
  110.             if (getBit(bitStream, in)) {
  111.                 tmpCodeTree = tmpCodeTree->right;
  112.             }
  113.             else {
  114.                 tmpCodeTree = tmpCodeTree->left;
  115.             }
  116.         }
  117.         fputc(tmpCodeTree->s, out);
  118.     }
  119. }
  120.  
  121. void decode(FILE* in, FILE* out) {  
  122.     unsigned int size;
  123.     if (!readSize(in, &size)) {
  124.         return;
  125.     }
  126.     BitStream* bitStream = new_bit();
  127.     Tree* codeTree = getCodeTree(in, bitStream);
  128.     decodeText(codeTree, in, out, size, bitStream);
  129.     free(bitStream);
  130.     freeTree(codeTree);
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top