In: Computer Science
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;
}
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":
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" } }