2
#include "binlog_encoding.h"
15
void print_usage_and_exit(char *prog) {
16
const char *name= strrchr(prog, '/');
22
<< " " << name << " [ -vvvx ] -e <number> ...\n";
23
cerr << " " << name << " [ -vvvx ] -d <byte> ...\n";
28
void encode(int argc, char *argv[], int verbose_level, bool hex_output) {
29
for (int i = 0 ; i < argc ; ++i) {
30
size_t length = strtoul(argv[i], NULL, 0);
33
throw std::invalid_argument("Length has to be > 1");
35
unsigned char buf[128];
36
unsigned char *end= length_encode(length, buf);
37
ios::fmtflags saved_flags= cout.flags();
38
if (verbose_level > 0)
39
cout << "Length " << length << ": ";
41
cout << std::hex << std::setw(2) << std::setfill('0');
42
unsigned char *ptr= buf;
46
cout << (unsigned int) *ptr;
52
cout.setf(saved_flags);
57
void decode(int argc, char *argv[], int verbose_level, bool hex_output) {
58
unsigned char buf[128];
59
for (int i = 0 ; i < argc ; ++i)
60
buf[i]= strtoul(argv[i], NULL, 0);
63
(void) length_decode(buf, &length);
65
ios::fmtflags saved_flags= cout.flags();
66
if (verbose_level > 0)
69
cout.setf(ios::hex, ios::basefield);
70
cout << length << std::endl;
71
cout.setf(saved_flags);
75
int main(int argc, char *argv[]) {
76
enum { NO_ACTION, ENCODE_ACTION, DECODE_ACTION } action= NO_ACTION;
78
static struct option long_options[] = {
79
{ "decode", 0 /* has_arg */, NULL, 'd' },
80
{ "encode", 0 /* has_arg */, NULL, 'e' },
81
{ "verbose", 0 /* has_arg */, NULL, 'v' },
82
{ "hex", 0 /* has_arg */, NULL, 'x' },
87
bool hex_output= false;
90
while ((ch= getopt_long(argc, argv, "devx", long_options, NULL)) != -1) {
94
print_usage_and_exit(argv[0]);
98
action= DECODE_ACTION;
102
action= ENCODE_ACTION;
118
encode(argc - optind, argv + optind, verbose_level, hex_output);
121
decode(argc - optind, argv + optind, verbose_level, hex_output);
124
print_usage_and_exit(argv[0]);
128
catch (std::invalid_argument& ex) {
129
cerr << ex.what() << "\n";
130
print_usage_and_exit(argv[0]);