#include #include #include typedef struct table table; struct table { unsigned int taille; unsigned int * taillecode; char * carac; char ** code; }; table * gettable(FILE*); int getcarac(char *,int, table *,char *); void getdata(FILE *, table *); void printtabcomp ( table * ); void supprimerTable(table * ); int main ( int argc, char ** argv){ if (argc!=2) { printf("Nombre d'arguments incorrects, syntaxe : \"./dehuf \"fichiercomp\"\"\n"); return 1; } FILE * fichier = fopen(argv[1], "rb"); if (fichier == NULL) { printf("Erreur, l'ouverture du fichier source à rencontré une erreur, verifiez que vous avez les droits en lecture sur le fichier.\n"); } table * donneecompression = gettable(fichier); //printtabcomp(donneecompression); getdata(fichier, donneecompression); supprimerTable(donneecompression); return 0; } void supprimerTable(table * tablecod){ for (unsigned int i = 0; i < tablecod->taille; i ++){ free(tablecod->code[i]); } free(tablecod->taillecode); free(tablecod->carac); free(tablecod); } table * gettable(FILE* fichier){ int temp=0; table * tablecodage = malloc(sizeof(table)); tablecodage->taille = fgetc(fichier); if (tablecodage->taille == 0) tablecodage->taille=256; tablecodage->taillecode=malloc(sizeof(int)*tablecodage->taille); tablecodage->carac=malloc(sizeof(char)*tablecodage->taille); tablecodage->code=malloc(sizeof(char*)*tablecodage->taille); int caracrestant = tablecodage->taille; int caracint =0; while (caracrestant>0){ tablecodage->taillecode[tablecodage->taille-caracrestant]=fgetc(fichier); tablecodage->code[tablecodage->taille-caracrestant]=malloc(sizeof(char)*tablecodage->taillecode[tablecodage->taille-caracrestant]); for (int i =0; i < tablecodage->taillecode[tablecodage->taille-caracrestant] ; i ++){ if ( i%8 == 0){ caracint =fgetc(fichier); } if (caracint >= (float)pow(2,7-(i%8))){ tablecodage->code[tablecodage->taille-caracrestant][i]='1'; caracint -= pow(2,7-(i%8)); } else { tablecodage->code[tablecodage->taille-caracrestant][i]='0'; } } temp=fgetc(fichier); tablecodage->carac[tablecodage->taille-caracrestant]=(char)temp; caracrestant --; } return tablecodage; } int getcarac(char * code,int taille, table * tablecodage,char * carac){ if (taille == 0){ return 0; } int i =0; int trouve = 0; while ( i < tablecodage->taille && !trouve){ if (tablecodage->taillecode[i]==taille){ int j =0; int cestpaslui = 0; while ( j < taille && !cestpaslui){ if ( code[j] != tablecodage->code[i][j]){ cestpaslui=1; } else j ++; } trouve= !cestpaslui ? 1 : 0; } if (!trouve) i ++; } if (trouve){ *carac=tablecodage->carac[i]; return 1; } else return 0; } void printtabcomp ( table * tabcomp){ for (int i =0; i < tabcomp->taille; i ++){ if (tabcomp->carac[i]==(char)10) { printf("Carac = LF"); } else if (tabcomp->carac[i]==(char)13) { printf("Carac = CR"); } else printf("Carac = %c", tabcomp->carac[i]); printf(", taille chemin = %d, chemin = ", tabcomp->taillecode[i]); for (int j =0; j < tabcomp->taillecode[i]; j ++){ printf("%c",tabcomp->code[i][j]); } printf("\n"); } } void getdata(FILE * fichier, table * donneecompression){ int currentcarac=fgetc(fichier); int nextcarac=fgetc(fichier); int lastcarac=fgetc(fichier); int cpt=0; int taille=0; char * code = malloc(0); char carac; int trouve=0; while (lastcarac!=EOF){ while ( !(trouve=getcarac(code,taille,donneecompression,&carac))/*&&lastcarac!=EOF*/){ if ( cpt == 8){ currentcarac=nextcarac; nextcarac=lastcarac; lastcarac=fgetc(fichier); cpt =0; } taille ++; code=realloc(code,sizeof(char)*taille); if (currentcarac>=pow(2,7-cpt)){ code[taille-1]='1'; currentcarac-=pow(2,7-cpt); } else code[taille-1]='0'; cpt ++; } printf("%c", carac); taille = 0; code=realloc(code,0); } printf("\n"); }