1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2009 Sun Microsystems
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
#include "binlog_encoding.h"
35
static void print_usage_and_exit(char *prog) {
36
const char *name= strrchr(prog, '/');
42
<< " " << name << " [ -vvvx ] -e <number> ...\n";
43
cerr << " " << name << " [ -vvvx ] -d <byte> ...\n";
48
static void encode(int argc, char *argv[], int verbose_level, bool hex_output)
50
for (int i = 0 ; i < argc ; ++i) {
51
size_t length = strtoul(argv[i], NULL, 0);
54
throw std::invalid_argument("Length has to be > 1");
56
unsigned char buf[128];
57
unsigned char *end= length_encode(length, buf);
58
ios::fmtflags saved_flags= cout.flags();
59
if (verbose_level > 0)
60
cout << "Length " << length << ": ";
62
cout << std::hex << std::setw(2) << std::setfill('0');
63
unsigned char *ptr= buf;
67
cout << (unsigned int) *ptr;
73
cout.setf(saved_flags);
78
static void decode(int argc, char *argv[], int verbose_level, bool hex_output)
80
unsigned char buf[128];
81
for (int i = 0 ; i < argc ; ++i)
82
buf[i]= strtoul(argv[i], NULL, 0);
85
(void) length_decode(buf, &length);
87
ios::fmtflags saved_flags= cout.flags();
88
if (verbose_level > 0)
91
cout.setf(ios::hex, ios::basefield);
92
cout << length << std::endl;
93
cout.setf(saved_flags);
97
int main(int argc, char *argv[]) {
98
enum { NO_ACTION, ENCODE_ACTION, DECODE_ACTION } action= NO_ACTION;
100
static struct option long_options[] = {
101
{ "decode", 0 /* has_arg */, NULL, 'd' },
102
{ "encode", 0 /* has_arg */, NULL, 'e' },
103
{ "verbose", 0 /* has_arg */, NULL, 'v' },
104
{ "hex", 0 /* has_arg */, NULL, 'x' },
108
int verbose_level= 0;
109
bool hex_output= false;
112
while ((ch= getopt_long(argc, argv, "devx", long_options, NULL)) != -1) {
116
print_usage_and_exit(argv[0]);
120
action= DECODE_ACTION;
124
action= ENCODE_ACTION;
140
encode(argc - optind, argv + optind, verbose_level, hex_output);
143
decode(argc - optind, argv + optind, verbose_level, hex_output);
146
print_usage_and_exit(argv[0]);
150
catch (std::invalid_argument& ex) {
151
cerr << ex.what() << "\n";
152
print_usage_and_exit(argv[0]);