-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtlvutil.cpp
More file actions
175 lines (155 loc) · 5.07 KB
/
Copy pathtlvutil.cpp
File metadata and controls
175 lines (155 loc) · 5.07 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <libtlv/tlv.hpp>
#include <CLI/CLI.hpp>
#include <fstream>
#include <iterator>
#include <iostream>
class TlvUtil
{
public:
enum class TlvFormat : int {
Hex,
Binary,
Formatted
};
private:
CLI::App cliApp;
std::string inPath;
std::string outPath;
TlvFormat inFormat;
TlvFormat outFormat;
Tlv tree;
void validate_format( TlvFormat& target, const std::string& val )
{
if( CLI::detail::to_lower(val) == "hex" )
target = TlvFormat::Hex;
else if( CLI::detail::to_lower(val) == "bin")
target = TlvFormat::Binary;
else if( CLI::detail::to_lower(val) == "formatted")
target = TlvFormat::Formatted;
else
throw CLI::ValidationError( "Invalid TLV format: \"" + val + "\"" );
}
public:
TlvUtil() :
cliApp( "Command line utility for operations on tlv encoded data", "tlvutil" ),
outPath("-"),
inFormat(TlvFormat::Hex),
outFormat(TlvFormat::Formatted)
{
cliApp.add_option("--in", inPath, "Path to tlv input data or - for stdin")->required(true);
cliApp.add_option("--out", outPath, "Path to tlv output data or - for stdout (default)");
cliApp.add_option("--inform", "Input format [hex, bin, formatted]")->each([&]( const std::string& val ){ validate_format( inFormat, val ); });
cliApp.add_option("--outform", "Output format [hex, bin, formatted]")->each([&]( const std::string& val ){ validate_format( outFormat, val ); });
}
void read_tlv()
{
try
{
if( inPath == "-" )
{
read_tlv_istream( std::cin );
}
else
{
std::ifstream inStream( inPath, std::ios::binary );
inStream.exceptions( std::ios::failbit | std::ios::badbit );
read_tlv_istream( inStream );
}
}
catch( std::ios::failure& )
{
std::cerr << "Error reading input data:" << std::endl << strerror(errno) << std::endl;
std::exit( -1 );
}
catch ( std::exception& e )
{
std::cerr << "Error parsing input data:" << std::endl << e.what() << std::endl;
std::exit( -1 );
}
}
void read_tlv_istream( std::istream& in )
{
std::vector<char> inBuffer( (std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>() );
if( inFormat == TlvFormat::Binary )
{
auto status = tree.parse_all( reinterpret_cast<uint8_t*>(inBuffer.data()), inBuffer.size() );
if( !status.ok() )
{
throw std::runtime_error( status.message() );
}
}
else if ( inFormat == TlvFormat::Hex )
{
std::string_view hexStr( inBuffer.data(), inBuffer.size() );
auto binData = LibtlvUtil::unhexify( hexStr, true );
auto status = tree.parse_all( binData.data(), binData.size() );
if( !status.ok() )
{
throw std::runtime_error( status.message() );
}
}
else if ( inFormat == TlvFormat::Formatted )
{
auto status = tree.parse_formatted( reinterpret_cast<uint8_t*>( inBuffer.data() ), inBuffer.size() );
if( !status.ok() )
{
throw std::runtime_error( status.message() );
}
}
}
void write_tlv()
{
try
{
if( outPath == "-" )
{
write_tlv_ostream( std::cout );
}
else
{
std::ofstream outStream( outPath, std::ios::binary );
outStream.exceptions( std::ios::failbit | std::ios::badbit );
write_tlv_ostream( outStream );
}
}
catch ( std::ios::failure& )
{
std::cerr << "Error writing output data:" << std::endl << strerror(errno) << std::endl;
std::exit( -1 );
}
}
void write_tlv_ostream( std::ostream& out )
{
std::vector<uint8_t> outBuffer;
if( outFormat == TlvFormat::Binary )
{
outBuffer = tree.dump();
}
else if( outFormat == TlvFormat::Hex )
{
auto hexStr = LibtlvUtil::hexify( tree.dump() );
outBuffer.assign( hexStr.data(), hexStr.data() + hexStr.size() );
}
else if( outFormat == TlvFormat::Formatted )
{
auto formattedStr = tree.dump_formatted();
outBuffer.assign( formattedStr.data(), formattedStr.data() + formattedStr.size() );
}
out.write( reinterpret_cast<const char*>(outBuffer.data()), outBuffer.size() );
}
int run( int argc, char** argv )
{
CLI11_PARSE(cliApp, argc, argv);
// read TLV input
read_tlv();
// write TLV output
write_tlv();
// Maybe in the future implement optional transformations like tag search
return 0;
};
};
int main( int argc, char** argv )
{
TlvUtil util;
util.run( argc, argv );
}