Question

In: Computer Science

How can I refactor my current code to Reverse engineer an unknown file format containing the...

How can I refactor my current code to Reverse engineer an unknown file format containing the same data fields but stored using a different representation in C.

My Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


typedef struct{
   double slope;
   char spade;
   int water;
   short wood;
   char afternoon;
   float daughter;
   unsigned long beginner;
   char scissors;
   char structure;
   char competition;
   char force;
   char mark[11];
   short copy;
   double grain;
   unsigned char cent;
   int banana;
} setup;


setup *pointer;


int compareTo(const void *a, const void *b){
   const setup *x = *( setup **)a;
   const setup *y = *( setup **)b;
  
   if(x -> daughter > y -> daughter){
       return -1;
   } else if
       (x -> daughter < y -> daughter){
           return 1;
   }
  
   if(x -> slope > y -> slope){
       return -1;
   } else if
       (x -> slope < y -> slope){
           return 1;
   }
  
   if(x -> afternoon > y -> afternoon){
       return 1;
   } else if
       (x -> afternoon < y -> afternoon){
           return -1;
   }
  
   if(x -> beginner > y -> beginner){
       return 1;
   } else if
       (x -> beginner < y -> beginner){
           return -1;
   }
  
   if(x -> cent > y -> cent){
       return 1;
   } else if
       (x -> cent < y -> cent){
           return -1;
   }
  
   if(x -> spade > y -> spade){
       return 1;
   } else if
       (x -> spade < y -> spade){
           return -1;
   }
  
   if(x -> scissors > y -> scissors){
       return -1;
   } else if
       (x -> scissors < y -> scissors){
           return 1;
   }
  
   if(x -> competition > y -> competition){
       return 1;
   } else if
       (x -> competition < y -> competition){
           return -1;
   }
  
   if(x -> water > y -> water){
       return -1;
   } else if
       (x -> water < y -> water){
           return 1;
   }
  
   if(x -> force > y -> force){
       return -1;
   } else if
       (x -> force < y -> force){
           return 1;
   }
  
   if(x -> copy > y -> copy){
       return -1;
   } else if
       (x -> copy < y -> copy){
           return 1;
   }
  
   if(x -> wood > y -> wood){
       return -1;
   } else if
       (x -> wood < y -> wood){
           return 1;
   }
  
   if(x -> banana > y -> banana){
       return 1;
   } else if
       (x -> banana < y -> banana){
           return -1;
   }
  
   if(x -> structure > y -> structure){
       return -1;
   } else if
       (x -> structure < y -> structure){
           return 1;
   }
  
   if (strcmp(x -> mark , y -> mark) > 0 ){
       return -1;
   } else if
       (strcmp(x -> mark , y -> mark) < 0 ){
           return 1;
   }
  
   if(x -> grain > y -> grain){
       return -1;
   } else if
       (x -> grain < y -> grain){
           return 1;
   }
   return 0;
}
int main(int argc, char **argv){
   FILE * file;
   FILE * file2;
   int size = 13;
   int count = 0;
   if (argc > 1) { // Open failed
   file = fopen(argv[1], "rb");
   file2 = fopen(argv[2], "wb");
   } else {
   fprintf (stderr, "Could not open file: %s\n");
   exit (1);
   }
   if (file == NULL) { // Open failed
   fprintf (stderr, "No such file %s\n", argv[1]);
   exit (1);
   }
   if (file2 == NULL){
   fprintf( stderr, "No such file %s\n", argv[2]);
   exit(1);
   }
   pointer = (setup*) malloc(size*sizeof(setup));
   while(1){
       if(fread(&pointer[count].slope, sizeof(double),1,file) != 1)
   break;
       fread(&pointer[count].spade, sizeof(char),1,file);
       fread(&pointer[count].water, sizeof(int),1,file);
       fread(&pointer[count].wood, sizeof(short),1,file);
       fread(&pointer[count].afternoon, sizeof(char),1,file);
       fread(&pointer[count].daughter, sizeof(float),1,file);
       fread(&pointer[count].beginner, sizeof(unsigned long),1,file);
       fread(&pointer[count].scissors, sizeof(char),1,file);
       fread(&pointer[count].structure, sizeof(char),1,file);
       fread(&pointer[count].competition, sizeof(char),1,file);
       fread(&pointer[count].force, sizeof(char),1,file);
       fread(&pointer[count].mark, sizeof(char[11]),1,file);
       fread(&pointer[count].copy, sizeof(short),1,file);
       fread(&pointer[count].grain, sizeof(double),1,file);
       fread(&pointer[count].cent, sizeof(unsigned char),1,file);
       fread(&pointer[count].banana, sizeof(int),1,file);
       count++;
      
       if(count >= size){
           size = size *2;
           pointer = (setup*) realloc (pointer, size* sizeof(setup));
   }
  
   }
   setup *pointer2[count];
   for(int j = 0; j < count; j++){
   pointer2[j] = &pointer[j];
   }

   qsort(pointer2, count, sizeof(*pointer2), compareTo);
  
  
   for(int k = 0; k < count; k++){
   fwrite(&pointer2[k]->slope, sizeof(double), 1, file2);
   fwrite(&pointer2[k]->spade, sizeof(char), 1, file2);
   fwrite(&pointer2[k]->water, sizeof(int), 1, file2);
   fwrite(&pointer2[k]->wood, sizeof(short), 1, file2);
   fwrite(&pointer2[k]->afternoon, sizeof(char), 1, file2);
   fwrite(&pointer2[k]->daughter, sizeof(float), 1, file2);
   fwrite(&pointer2[k]->beginner, sizeof(unsigned long), 1, file2);
   fwrite(&pointer2[k]->scissors, sizeof(char), 1, file2);
   fwrite(&pointer2[k]->structure, sizeof(char), 1, file2);
   fwrite(&pointer2[k]->competition, sizeof(char), 1, file2);
   fwrite(&pointer2[k]->force, sizeof(char), 1, file2);
   fwrite(&pointer2[k]->mark, sizeof(char[11]), 1, file2);
   fwrite(&pointer2[k]->copy, sizeof(short), 1, file2);
   fwrite(&pointer2[k]->grain, sizeof(double), 1, file2);
   fwrite(&pointer2[k]->cent, sizeof(unsigned char), 1, file2);
   fwrite(&pointer2[k]->banana, sizeof(int), 1, file2);
   }
   free(pointer);
   fclose(file);
   fclose(file2);
   return 0;
}

Solutions

Expert Solution

ANS :a) SOURCE CODE REFACTORING : can improve the quality and maintainability of your project by restructuring your code while not modifying the runtime behavior. Visual Studio Code supports refactoring operations (refactoring) such as Extract Method and Extract Variable to improve your code base from within your editor.

For example, a common refactoring used to avoid duplicating code (a maintenance headache) is the Extract Method refactoring, where you select source code that you'd like to reuse elsewhere and pull it out into its own shared method.

Refactoring are provided by a language service and VS Code has built-in support for Typescript and JavaScript refactoring through the Typescript language service. Refactoring support for other programming languages is provided through VS Code extensions which contribute language services. The UI and commands for refactoring are the same across languages, and in this topic we'll demonstrate refactoring support with the Typescript language service.

Code Actions = Quick Fixes and refactoring

In VS Code, Code Actions can provide both refactorings and Quick Fixes for detected issues (highlighted with green squiggles). An available Code Action is announced by a lightbulb near the source code when the cursor is on a squiggle or selected text region. Clicking on the Code Action lightbulb or using the Quick Fix command Ctrl+. will display Quick Fixes and refactorings.

If you'd just like to see refactorings without Quick Fixes, you can use the Refactor command (Ctrl+Shift+R).

Note: If you prefer to not see the Code Action lightbulb in your editor, you can disable lightbulbs with the editor.lightbulb.enable setting. You can still open Quick Fixes through Quick Fix command and Ctrl+. keyboard shortcut.

Refactoring actions

Extract Method

Select the source code you'd like to extract and then click on the lightbulb in the gutter or press (Ctrl+.) to see available refactorings. Source code fragments can be extracted into a new method, or into a new function at various different scopes. During the extract refactoring, you will be prompted to provide a meaningful name.

Extract Variable

TypeScript language service provides Extract to const refactoring to create a new local variable for the currently selected expression:

When working with classes, you can also extract a value to a new property.

Rename symbol

Renaming is a common operation related to refactoring source code and VS Code has a separate Rename Symbol command (F2). Some languages support rename symbol across files. Press F2 and then type the new desired name and press Enter. All usages of the symbol will be renamed, across files.

Keybindings for Code Actions

The editor.action.codeAction command lets you configure keybindings for specific Code Actions. This keybinding, for example, triggers the Extract function refactoring Code Actions:

{
  "key": "ctrl+shift+r ctrl+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.function"
  }
}

Code Action kinds are specified by extensions using the enhanced CodeActionProvided API. Kinds are hierarchical, so "kind": "refactor" will show all refactoring Code Actions, whereas "kind": "refactor.extract.function" will only show Extract function refactorings.

Using the above keybinding, if only a single "refactor.extract.function" Code Action is available, it will be automatically applied. If multiple Extract function Code Actions are available, we bring up a context menu to select them:

You can also control how/when Code Actions are automatically applied using the apply argument:

{
  "key": "ctrl+shift+r ctrl+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.function",
    "apply": "first"
  }
}

Valid values for "apply":

  • "first" - Always automatically apply the first available Code Action.
  • "ifSingle" - Default. Automatically apply the Code Action if only one is available. Otherwise, show the context menu.
  • "never" - Always show the Code Action context menu, even if only a single Code Action is available.

When a Code Action keybinding is configured with "preferred": true, only preferred Quick Fixes and refactorings are shown. A preferred Quick Fix addresses the underlying error, while a preferred refactoring is the most common refactoring choice. For example, while multiple refactor.extract.constant refactorings may exist, each extracting to a different scope in the file, the preferred refactor.extract.constant refactoring is the one that extracts to a local variable.

This keybinding uses "preferred": true to create a refactoring that always tries to extract the selected source code to a constant in the local scope:

{
  "key": "shift+ctrl+e",
  "command": "editor.action.codeAction",
  "args": {
    "kind": "refactor.extract.constant",
    "preferred": true,
    "apply": "ifsingle"
  }
}

Related Solutions

Below is my source code for file merging. when i run the code my merged file...
Below is my source code for file merging. when i run the code my merged file is blank and it never shows merging complete prompt. i dont see any errors or why my code would be causing this. i saved both files with male names and female names in the same location my source code is in as a rtf #include #include #include using namespace std; int main() { ifstream inFile1; ifstream inFile2; ofstream outFile1; int mClientNumber, fClientNumber; string mClientName;...
How do I add the information below to my current code that I have also posted...
How do I add the information below to my current code that I have also posted below. <!DOCTYPE html> <html> <!-- The author of this code is: Ikeem Mays --> <body> <header> <h1> My Grocery Site </h1> </header> <article> This is web content about a grocery store that might be in any town. The store stocks fresh produce, as well as essential grocery items. Below are category lists of products you can find in the grocery store. </article> <div class...
Python I want to name my hero and my alien in my code how do I...
Python I want to name my hero and my alien in my code how do I do that: Keep in mind I don't want to change my code except to give the hero and alien a name import random class Hero:     def __init__(self,ammo,health):         self.ammo=ammo         self.health=health     def blast(self):         print("The Hero blasts an Alien!")         if self.ammo>0:             self.ammo-=1             return True         else:             print("Oh no! Hero is out of ammo.")             return False     def...
How can I improve my code below to meet the requirements of this assignment with syntax...
How can I improve my code below to meet the requirements of this assignment with syntax add to my code to complete the assignment below: #include <iostream> #include <vector> using namespace std; class Inventory { private: int itemNumber; int quantity; double cost; double totalCost; public: Inventory() { itemNumber = 0; quantity = 0; cost = 0; totalCost = 0; } Inventory(int n, int q, double c) { itemNumber = n; quantity = q; cost = c; setTotalCost(); } void setItemNumber(int...
I need to formulate a clinical question using PICO format from my current clinical area. My...
I need to formulate a clinical question using PICO format from my current clinical area. My keywords: falls, dementia, psychotropic drugs
I need to formulate a clinical question using PICO format from my current clinical area. My...
I need to formulate a clinical question using PICO format from my current clinical area. My keywords: falls, dementia, psychotropic drugs
I would like to compare my current code with that of someone else to be sure...
I would like to compare my current code with that of someone else to be sure I did this assignment correctly. My main concern is with Problems 2 and 4. I sometimes struggle with testing the methods I have created. If you can, please answer all of the questions so that I may reference them if I am still lost on Problems 2 and 4. Each problem requires the one before it and so seeing the entire assignment as opposed...
How can a company reverse a trend of higher current liabilities over current assets?
How can a company reverse a trend of higher current liabilities over current assets?
I didn't know , how to write this code // This file is part of www.nand2tetris.org...
I didn't know , how to write this code // This file is part of www.nand2tetris.org // and the book "The Elements of Computing Systems" // by Nisan and Schocken, MIT Press. // File name: projects/04/Fill.asm // Runs an infinite loop that listens to the keyboard input. // When a key is pressed (any key), the program blackens the screen, // i.e. writes "black" in every pixel; // the screen should remain fully black as long as the key is...
I can see one unknown android device on my network, but it hassame host-name as...
I can see one unknown android device on my network, but it has same host-name as my android device.QUESTION1.Is this possible that on same network two different devices can have same host-name ?2.Is someone spoofing my device and accessing my network ?SITUATION1.I have WPA2-psk security key encryption.2.Both have different but similar MAC addresses. (first three octets are same).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT