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
|
#include <drizzled/global.h>
#include "binlog_encoding.h"
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <string.h>
#include <getopt.h>
using std::ios;
using std::cout;
using std::cerr;
using std::flush;
void print_usage_and_exit(char *prog) {
const char *name= strrchr(prog, '/');
if (name)
++name;
else
name= "length";
cerr << "Usage:\n"
<< " " << name << " [ -vvvx ] -e <number> ...\n";
cerr << " " << name << " [ -vvvx ] -d <byte> ...\n";
cerr << flush;
exit(1);
}
void encode(int argc, char *argv[], int verbose_level, bool hex_output) {
for (int i = 0 ; i < argc ; ++i) {
size_t length = strtoul(argv[i], NULL, 0);
if (length < 2)
throw std::invalid_argument("Length has to be > 1");
unsigned char buf[128];
unsigned char *end= length_encode(length, buf);
ios::fmtflags saved_flags= cout.flags();
if (verbose_level > 0)
cout << "Length " << length << ": ";
if (hex_output)
cout << std::hex << std::setw(2) << std::setfill('0');
unsigned char *ptr= buf;
while (true) {
if (hex_output)
cout << "0x";
cout << (unsigned int) *ptr;
if (++ptr == end)
break;
cout << " ";
}
cout << std::endl;
cout.setf(saved_flags);
}
}
void decode(int argc, char *argv[], int verbose_level, bool hex_output) {
unsigned char buf[128];
for (int i = 0 ; i < argc ; ++i)
buf[i]= strtoul(argv[i], NULL, 0);
size_t length;
(void) length_decode(buf, &length);
ios::fmtflags saved_flags= cout.flags();
if (verbose_level > 0)
cout << "Length ";
if (hex_output)
cout.setf(ios::hex, ios::basefield);
cout << length << std::endl;
cout.setf(saved_flags);
}
int main(int argc, char *argv[]) {
enum { NO_ACTION, ENCODE_ACTION, DECODE_ACTION } action= NO_ACTION;
static struct option long_options[] = {
{ "decode", 0 /* has_arg */, NULL, 'd' },
{ "encode", 0 /* has_arg */, NULL, 'e' },
{ "verbose", 0 /* has_arg */, NULL, 'v' },
{ "hex", 0 /* has_arg */, NULL, 'x' },
{ 0, 0, 0, 0 }
};
int verbose_level= 0;
bool hex_output= false;
int ch;
while ((ch= getopt_long(argc, argv, "devx", long_options, NULL)) != -1) {
switch (ch) {
case 0:
case '?':
print_usage_and_exit(argv[0]);
break;
case 'd':
action= DECODE_ACTION;
break;
case 'e':
action= ENCODE_ACTION;
break;
case 'v':
++verbose_level;
break;
case 'x':
hex_output= true;
break;
}
}
try {
switch (action) {
case ENCODE_ACTION:
encode(argc - optind, argv + optind, verbose_level, hex_output);
break;
case DECODE_ACTION:
decode(argc - optind, argv + optind, verbose_level, hex_output);
break;
default:
print_usage_and_exit(argv[0]);
break;
}
}
catch (std::invalid_argument& ex) {
cerr << ex.what() << "\n";
print_usage_and_exit(argv[0]);
}
return EXIT_SUCCESS;
}
|