-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
115 lines (97 loc) · 3.25 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <stdio.h>
#include <stdlib.h>
#include "z_NHEX.h"
int main(int argc, char* argv[]) {
if (argc <= 1)
{
printf("Usage: nhex -help\n");
return 1;
}
if (strcmp(argv[1], "-help") == 0)
{
printf("\nWELCOME TO NHEX MINI HEX EDITOR\n");
printf("\n\t -s -h \"string\" => converts string ascii to hex\n");
printf("\n\t -h -s \"hex\" => converts string hex to ascii\n");
printf("\n\t -r -s -p \"filename\" => reads file and converts to hex\n");
printf("\n\t -r -h -p \"filename\" => reads file and converts to string\n");
printf("\n\t -w -s \"filename\" \"string\" => writes file and converts to hex\n");
printf("\n\t -w -h \"filename\" \"hex\" => writes file and converts to string\n");
printf("\n\t -r -s -w \"filename\" \"filename2\"\n\t => reads file and converts to hex in another file\n");
printf("\n\t -r -h -w \"filename\" \"filename2\"\n\t => reads file and converts to string in another file\n");
printf("\n\nEND OF THE NHEX HELP MENU\n");
return 1;
}
if (strcmp(argv[1], "-r") == 0 && strcmp(argv[2], "-s") == 0 && strcmp(argv[3], "-p") == 0 && argv[4] != NULL)
{
printf("\n");
ReadBinPrintHex(argv[4]);
printf("\n");
}
else if (strcmp(argv[1], "-r") == 0 && strcmp(argv[2], "-h") == 0 && strcmp(argv[3], "-p") == 0 && argv[4] != NULL)
{
size_t len = strlen(argv[4]);
unsigned char* str1;
str1 = ReadBin(argv[4]);
if (str1 == NULL)
return 1;
unsigned char* str2 = (unsigned char*)malloc(len);
HexToAscii(str1, str2);
printf("\n%s\n", str2);
free(str1);
}
else if (strcmp(argv[1], "-r") == 0 && strcmp(argv[2], "-s") == 0 && strcmp(argv[3], "-w") == 0 && argv[4] != NULL)
{
unsigned char* str1;
str1 = ReadBin(argv[4]);
if (str1 == NULL)
return 1;
size_t len = strlen(str1);
unsigned char* str2 = (unsigned char*)malloc(1000);
unsigned char* hex = (unsigned char*)malloc(1000);
AsciiToHex(str1, str2, hex);
if(WriteBin(argv[5], hex) == 1)
printf("\nSuccessfully written to the file.\n");
free(str2);
free(hex);
}
else if (strcmp(argv[1], "-r") == 0 && strcmp(argv[2], "-h") == 0 && strcmp(argv[3], "-w") == 0 && argv[4] != NULL)
{
unsigned char* str1;
str1 = ReadBin(argv[4]);
if (str1 == NULL)
return 1;
size_t len = strlen(str1);
unsigned char* str2 = (unsigned char*)malloc(len*2 + len);
HexToAscii(str1, str2);
if (WriteBin(argv[5], str2) == 1)
printf("\nSuccessfully written to the file.\n");
free(str2);
}
else if (strcmp(argv[1], "-w") == 0 && strcmp(argv[2], "-s") == 0 && argv[3] != NULL)
{
if (ConvertString(argc, argv, 3, "w") == 1)
printf("\nSuccessfully written to the file.\n");
}
else if (strcmp(argv[1], "-w") == 0 && strcmp(argv[2], "-h") == 0 && argv[3] != NULL)
{
if (ConvertHex(argc, argv, 3, "w") == 1)
printf("\nSuccessfully written to the file.\n");
}
else if (strcmp(argv[1], "-h") == 0 && strcmp(argv[2], "-s") == 0 && argv[3] != NULL)
{
printf("\n");
ConvertHex(argc, argv, 3, "p");
printf("\n");
}
else if (strcmp(argv[1], "-s") == 0 && strcmp(argv[2], "-h") == 0 && argv[3] != NULL)
{
printf("\n");
ConvertString(argc, argv, 3, "p");
printf("\n");
}
else
{
printf("Invalid argument.\n");
}
return 0;
}