int IP::checkMask(string mask) { size_t pos = 0, posBefore = 0; int tal; string binary; bool correct = true; pos = mask.find("."); posBefore = pos + 1; tal = convertStringToInt(mask.substr(0, pos)); //talet i första delen if (pos == string::npos || pos > 3 || pos == 0 || tal > 255) // kollar att det finns en punkt och att första delen har en storlek mellan 1-3 (192.168.1.1) OK! (.168.1.1) INTE OK! return 0; binary = decimalToBinary(tal); pos = mask.find(".", pos + 1); tal = convertStringToInt(mask.substr(posBefore, pos)); // talet i andra delen posBefore = pos + 1; if (pos == string::npos || pos > 7 || pos < 3 || tal > 255) // kollar att den finns en andra punkt och att den delen har en storlek mellan 1-3 return 0; tal = convertStringToInt(mask.substr(posBefore, pos)); // talet i tredje delen pos = mask.find(".", pos + 1); if (pos == string::npos || pos > 11 || pos < 5 || tal > 255) // kollar att den finns en tredje punkt och att den delen har en storlek mellan 1-3 return 0; tal = convertStringToInt(mask.substr(pos + 1)); // talet i fjärde delen if (tal > 255) return 0; return 1; }
/** * @brief Converter::fromDoubleString2BinaryString * @param pDoubleString * @return * Funcion para convertir un string double en string binario * Por ej: 7.3 -> 73 (01001001) y contador - 1 (00000001) que es cantidad * de espacios después del punto 10 ** (contador - 1) */ std::string Converter::fromDoubleString2BinaryString( std::string pDoubleString ) { int contador = 0; double doubleNumber = fromString2Double( pDoubleString ); for(int i = pDoubleString.find("."); i < pDoubleString.length() ; i++){ contador++; } int intDoubleNumber = doubleNumber * ( pow( 10, (contador - 1) ) ); //cout << contador - 1 << endl; //cout << pDoubleString.find(".") << endl; cout << doubleNumber << endl; cout << intDoubleNumber << endl; std::string entero = decimalToBinary( fromInt2String(intDoubleNumber) ); cout << entero << endl; std::string decimal = decimalToBinary( fromInt2String(contador - 1) ); cout << decimal << endl; cout << entero + decimal << endl; return completeBinary( entero + decimal ); //entero + decimal; }
int IP::checkCorrectInput(string ipadress, string mask) { // returnerar 0 ifall någon input är felaktig //kontrollerar IP int ipCheck = 0, maskCheck = 0; ipCheck = checkIP(ipadress); //kontrollerar masken string masken = decimalToBinary(255); return 1; }
void main(void) { char input[30], opcode[30], in[20], ch, labelFile[20]; int indicator1, indicator2, indicator3, indicator4, i; int lineNumber; //to indicate line number in code int countError; //to count the number of errors in the code int countLabel; // to count number of labels in the input.txt FILE *inputFile, *opcodeFile, *registerFile, *decimalFile, *outputFile, *labelOutput, *labelInput, *fh; inputFile = fopen("input.txt", "r"); //reference to input.txt file containing input assembly code labelOutput = fopen("label.txt", "w"); //reference to label.txt file stores information about label present in input.txt if((!inputFile) || (!labelOutput)) //checks whether files openup or have some error while opening them { printf("Error in opening files\n"); return; } printf("Program for a simple Two Pass Assembler\n\n"); printf("---------------------Processing Input-------------------------\n"); lineNumber = 0; countError = 0; countLabel = 0; int *bin; fscanf(inputFile,"%s",in); //scans input file // identifies labels in the assembly input file through presence of ":" by running till end of input file while(!feof(inputFile)) { if(strchr(in,':') != NULL) //if colon occurs means label in assembly instruction input file { countLabel++; bin = decimalToBinary(countLabel); int i1; char inp[20]; int length=strlen(in); for(i1=0;i1<length-1;i1++) { inp[i1]=in[i1]; } inp[i1]='\0'; int c; c=0; fprintf(labelOutput, "%s ",inp); while(c < 16) { fprintf(labelOutput, "%d", *(bin+c)); c++; } fprintf(labelOutput,"\n"); } fscanf(inputFile, "%s", in); } fprintf(labelOutput,"----"); // add ---- at the end of label.txt to indicate end of file fclose(labelOutput); fclose(inputFile); inputFile = fopen("input.txt", "r"); labelInput = fopen("label.txt", "r"); opcodeFile = fopen("opcode.txt", "r"); //reference to opcode.txt file containing opcode for our ISA decimalFile = fopen("decimalToBinary.txt", "r");//reference to decimalToBinary.txt file containing binary equivalent of decimal outputFile = fopen("output.txt", "w"); //reference to output.txt file which stores binary conversion of assembly instructions registerFile = fopen("register.txt", "r"); //reference to register.txt file containing binary equivalent of all registers if((!inputFile) || (!outputFile) || (!opcodeFile) || (!registerFile) || (!decimalFile) || (!labelInput)) //checks whether files openup or have some error while opening them { printf("Error in opening files\n"); return; } while(!feof(inputFile)) //This while loop runs till the end of input file { rewind(opcodeFile); //sets cursor to the begining of file so that we can use it for next scan of input file rewind(registerFile); rewind(decimalFile); rewind(labelInput); fscanf(inputFile, "%s", input); if(strcasecmp(input, "HLT")==0) { fprintf(outputFile,"\n10100000111001000010010010000000"); //if HLT occurs in input file append this to output file break; } //This while loop scans labelFile and use it to write binary equivalent of labels present in input file while(1) { fscanf(labelInput,"%s",labelFile); //read label from label.txt if(strcasecmp(labelFile,"----")==0) // if end of label.txt file reached break from parent while loop { break; } if(strchr(input,':') != NULL) { int i1; char inp[20]; int length=strlen(input); for(i1=0;i1<length-1;i1++) { inp[i1]=input[i1]; } inp[i1]='\0'; if(strcasecmp(inp, labelFile) == 0) { //if label found fscanf(labelInput,"%s",labelFile); //read binary equivalent of label from label.txt fprintf(outputFile,"\n0000000000000000%s",labelFile); //write in output file indicator1 = 1; break; } } else if(strcasecmp(input, labelFile) == 0) { //if label found fscanf(labelInput,"%s",labelFile); //read binary equivalent of label from label.txt fprintf(outputFile,"%s",labelFile); //write in output file indicator1 = 1; break; } else { fscanf(labelInput,"%s",labelFile); //read binary value equivalent to label scanned above indicator1 = 0; } } //This while loop scans opcodeFile and use it to write binary equivalent of opcodes present in input file while(1) { fscanf(opcodeFile,"%s",opcode); //read first opcode from opcode.txt file if(strcasecmp(opcode,"----")==0) //if at the end of opcode.txt break from parent while loop { break; } if(strcasecmp(input, opcode) == 0) { //if opcode found fscanf(opcodeFile, "%s", opcode); //read binary equivalent of opcode from opcode.txt fprintf(outputFile, "\n%s", opcode); //write in output file indicator2 = 1; break; } else { fscanf(opcodeFile, "%s", opcode); //read binary value equivalent to opcode scanned above indicator2 = 0; } } if(indicator2 == 1) //if value of indicator2 becomes one means an opcode found which must be written in a new line of code lineNumber++; //This while loop scans registerFile and use it to write binary equivalent of register present in input file while(1) { fscanf(registerFile, "%s", opcode); //read first register from register.txt file if(strcasecmp(opcode, "----") == 0) //if at the end of registerFile.txt break from parent while loop { break; } if(strcasecmp(input, opcode) == 0) { //if register found fscanf(registerFile, "%s", opcode); //read binary equivalent of register from registerFile.txt fprintf(outputFile, "%s", opcode); //write in output file indicator3 = 1; break; } else { fscanf(registerFile,"%s",opcode); //read binary value equivalent to register scanned above indicator3 = 0; } } //This while loop scans decimalFile and use it to write binary equivalent of decimals present in input file while(1) { fscanf(decimalFile,"%s",opcode); //read first decimal from decimalFile.txt file if(strcasecmp(opcode,"----")==0) //if at the end of decimalFile.txt break from parent while loop { break; } if(strcasecmp(input,opcode)==0) { //if decimal found fscanf(decimalFile,"%s",opcode); //read binary equivalent of decimal from decimalFile.txt fprintf(outputFile,"%s",opcode); indicator4 = 1; break; } else { //read binary value equivalent to decimal scanned above fscanf(decimalFile,"%s",opcode); indicator4 = 0; } } if(!indicator1 && !indicator2 && !indicator3 && !indicator4) { printf("\nError occured in Line Number: %d", lineNumber); printf("\n\"%s\" is not a valid Mnemonic or Operand\n",input); fprintf(outputFile," \"%s\"",input); //write incorrect mnemonic or operand to output.txt countError++; printf("\n1. Continue execution\t2. Exit Program\n"); scanf("%d", &i); switch(i) { case 1: continue; break; case 2: break; default: printf("Wrong Entry\n"); } } } fclose(inputFile); fclose(outputFile); fclose(opcodeFile); fclose(labelInput); fclose(registerFile); fclose(decimalFile); if (countError == 0) printf("\nProgram Assembled Successfully.\n"); //count variable to print total number of errors else printf("\nTotal %d Errors found in code.\n", countError); return; }
void decompressHuffman(FILE** file, FILE** ptFileOutput, char* fileInputName) { char c; char* bufferCode; char currentOctet[7]; int i = 0, tailleBuf; int valueChar; int tailleDico = 0, tailleTab; int* intTab; char* charTab; char* tabChar; char* decodedFileName; elementListe* elemL = NULL; elementListe** ptListe = &elemL; /* temps de traitement */ clock_t start_time, end_time; start_time = clock(); bufferCode = calloc(1, sizeof(char)); if (bufferCode == NULL) { printf("Erreur d'allocation bufferCode.\n"); exit(-1); } /* Lire la taille des structures au début du fichier */ fread(&tailleDico, sizeof(int), 1, *file); /*printf("%d\n", tailleDico);*/ charTab = calloc(1, sizeof(char)); intTab = calloc(1, sizeof(int)); if ((intTab == NULL) || (charTab == NULL)) { printf("Erreur d'allocation intTab ou charTab.\n"); exit(-1); } i = 0; /* Extraction du dictionnaire */ while (i < tailleDico) { charTab = realloc(charTab, sizeof(char) * i + 1); intTab = realloc(intTab, sizeof(int) * i + 1); if ((intTab == NULL) || (charTab == NULL)) { printf("Erreur reallocation intTab ou charTab.\n"); exit(-1); } fread(&charTab[i], sizeof(char), 1, *file); fread(&intTab[i], sizeof(int), 1, *file); i++; } /* Affichage des caractères */ /*for (i = 0; i < tailleDico; i++) { printf("1- %c %d\n", charTab[i], intTab[i]); }*/ tailleTab = tailleDico; /* Creation de liste chainee */ for (i = 0; i < tailleTab; i++) { createChainedList(ptListe, charTab[i], intTab[i]); } printf("\n"); while ((*ptListe)->suivant != NULL) { insertNewNodeInChainedList(ptListe); } /* La liste ne contient plus qu'un seul element qui contiend l'arbre entier */ tabChar = calloc(1, sizeof(char*)); if (tabChar == NULL) { printf("Erreur d'allocation tabChar.\n"); exit(-1); } /* Parcours d'arbre*/ prefixeHuffmanTree(elemL->noeudIntermediaire, tabChar, 0); /* affichage caractères codés */ for (i = 0; i < 256; i++) { if (code[i]) printf("'%c': %s\n", i, code[i]); } printf("Decompression en cours, veuillez patienter . . .\n"); /* on parcours le fichier compresse */ bufferCode = calloc(1, sizeof(char)); if(bufferCode == NULL){ printf("Erreur d'allocation bufferCode.\n"); exit(-1); } while (!feof(*file)) { c = fgetc(*file); bufferCode = realloc(bufferCode, 7 * (i + 2) * sizeof(char)); if(bufferCode == NULL){ printf("Erreur d'allocation bufferCode.\n"); exit(-1); } valueChar = c; decimalToBinary(valueChar, currentOctet); strcat(bufferCode, currentOctet); i++; /*printf("%c", c);*/ } /* on supprime les 8 derniers caracteres = \0 */ tailleBuf = strlen(bufferCode); for (i = (tailleBuf - 8); i < tailleBuf; i++) { bufferCode[i] = '\0'; } /* Affichage du buffer */ /*printf("%s\n", bufferCode);*/ /*Ecriture de la taille de la table des fréquences */ decodedFileName = calloc((8 + strlen(fileInputName)), sizeof(char)); if (decodedFileName == NULL) { printf("Erreur d'allocation fileOutputName.\n"); exit(-1); } strcat(fileInputName, ".decoded"); *ptFileOutput = createFile(fileInputName); closeFile(ptFileOutput); openFile(fileInputName, ptFileOutput, "wb+"); decodeHuffmanTree(bufferCode, elemL->noeudIntermediaire, ptFileOutput); closeFile(ptFileOutput); free(decodedFileName); decodedFileName = NULL; free(bufferCode); free(elemL); elemL = NULL; free(intTab); intTab = NULL; free(charTab); charTab = NULL; free(tabChar); tabChar = NULL; end_time = clock(); printf("Decompression effectuee en %lu s.\n", (long) ((end_time - start_time) / CLOCKS_PER_SEC)); }