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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140 | #include "gate/gatemain.h"
#include "gate/console.hpp"
#include "gate/applications.hpp"
#include "gate/files.hpp"
#include "gate/encode/sha1hash.hpp"
#include "gate/encode/sha256hash.hpp"
#include "gate/encode/md5hash.hpp"
#include "gate/encode/crchash.hpp"
#include "gate/encode/base64.hpp"
#include <stdio.h>
#if defined(GATE_COMPILER_MSVC) && defined(GATE_LEAK_DETECTION) && defined(GATE_SYS_WIN) && !defined(GATE_SYS_WINCE) && (GATE_ARCH != GATE_ARCH_ARM32) && (GATE_ARCH != GATE_ARCH_ARM64)
# include <vld.h>
#endif
namespace gate
{
namespace apps
{
using namespace gate::enc;
class GATE_API_LOCAL HashApp : public App
{
private:
Blob buildHash(IHashBuilder& builder, Stream& content)
{
builder.update(content);
return builder.computeHash();
}
void printHash(Blob const& data)
{
/*
char buffer[4096] = GATE_INIT_EMPTY;
size_t used = Base64::encode(static_cast<char const*>(data.data()), data.length(), buffer, sizeof(buffer));
Console.writeBlock(buffer, used);
*/
Console << String::toHex(static_cast<char const*>(data.data()), data.length());
Console << strings::NewLine;
}
public:
virtual void run() override
{
static AppOptionDef help = AppOptionDef::createSwitch("help", "?", "show_help", "Shows help");
static AppOptionDef md5 = AppOptionDef::createSwitch("md5", "5", "md5_hash", "Creates an MD5 hash");
static AppOptionDef crc16 = AppOptionDef::createSwitch("crc16", "16", "crc16_hash", "Creates an MD5 hash");
static AppOptionDef crc32 = AppOptionDef::createSwitch("crc32", "32", "crc32_hash", "Creates an MD5 hash");
static AppOptionDef sha1 = AppOptionDef::createSwitch("sha1", "1", "sha1_hash", "Creates an MD5 hash");
static AppOptionDef sha256 = AppOptionDef::createSwitch("sha256", "256", "sha256_hash", "Creates an MD5 hash");
static AppOptionDef from_stdin = AppOptionDef::createSwitch("stdin", "S", "stdin", "Read input data from stdin");
static AppOptionDef input_data = AppOptionDef::createString("data", "D", "", "input_data", "Input data");
static AppOptionDef input_file = AppOptionDef::createString("", "", "", "input_file_path", "Path to file with input data");
static AppOptionDef* options[] =
{
&help,
&md5,
&crc16, &crc32,
&sha1, &sha256,
&from_stdin, &input_data, &input_file
};
static size_t const optionsCount = sizeof(options) / sizeof(options[0]);
App::parseAppOptions(this->getArgs(), options, optionsCount);
if (help)
{
App::printAppOptions(options, optionsCount, Console);
return;
}
Stream strm = NullStream();
if (from_stdin)<--- Variable 'strm' is reassigned a value before the old one has been used.
{
strm = Console;
}<--- Variable 'strm' is reassigned a value before the old one has been used.
else if (input_data)
{
strm = StringStream(input_data.getString());
}
else
{
String filePath = input_file.getString();
if (filePath.empty())
{
GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Missing file path", 0);
}
strm = File::open(filePath, File::Open_Read);
}
Blob outdata;
if (md5)
{
Md5HashBuilder builder;
outdata = buildHash(builder, strm);
}
else if (crc16)
{
Crc16HashBuilder builder;
outdata = buildHash(builder, strm);
}
else if (crc32)
{
Crc32HashBuilder builder;
outdata = buildHash(builder, strm);
}
else if (sha1)
{
Sha1HashBuilder builder;
outdata = buildHash(builder, strm);
}
else if (sha256)
{
Sha256HashBuilder builder;
outdata = buildHash(builder, strm);
}
else
{
GATEXX_RAISE_EXCEPTION(results::InvalidArg, "Missing hash algorithm argument", 0);
}
printHash(outdata);
}
};
} // end of namespace apps
} // end of namespace gate
#include <stdio.h>
int gate_main(char const* program, char const* const* arguments, gate_size_t argcount, gate_uintptr_t apphandle)
{
gate::apps::HashApp app;
return gate::App::runApp(app, program, arguments, argcount, apphandle);
}
|