Monero
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2018-2019, tevador <tevador@gmail.com>
3
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the copyright holder nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#pragma once
30
31#include <cstring>
32#include <cstdlib>
33#include <iostream>
34#include <fstream>
35
36constexpr char hexmap[] = "0123456789abcdef";
37inline void outputHex(std::ostream& os, const char* data, int length) {
38 for (int i = 0; i < length; ++i) {
39 os << hexmap[(data[i] & 0xF0) >> 4];
40 os << hexmap[data[i] & 0x0F];
41 }
42}
43
44char parseNibble(char hex) {
45 hex &= ~0x20;
46 if (hex & 0x40) {
47 hex -= 'A' - 10;
48 }
49 else {
50 hex &= 0xf;
51 }
52 return hex;
53}
54
55void hex2bin(const char *in, int length, char *out) {
56 for (int i = 0; i < length; i += 2) {
57 char nibble1 = parseNibble(*in++);
58 char nibble2 = parseNibble(*in++);
59 *out++ = nibble1 << 4 | nibble2;
60 }
61}
62
63constexpr bool stringsEqual(char const * a, char const * b) {
64 return *a == *b && (*a == '\0' || stringsEqual(a + 1, b + 1));
65}
66
67template<size_t N>
68bool equalsHex(const void* hash, const char (&hex)[N]) {
69 char reference[N / 2];
70 hex2bin(hex, N - 1, reference);
71 return memcmp(hash, reference, sizeof(reference)) == 0;
72}
73
74inline void dump(const char* buffer, uint64_t count, const char* name) {
75 std::ofstream fout(name, std::ios::out | std::ios::binary);
76 fout.write(buffer, count);
77 fout.close();
78}
79
80inline void readOption(const char* option, int argc, char** argv, bool& out) {
81 for (int i = 0; i < argc; ++i) {
82 if (strcmp(argv[i], option) == 0) {
83 out = true;
84 return;
85 }
86 }
87 out = false;
88}
89
90inline void readIntOption(const char* option, int argc, char** argv, int& out, int defaultValue) {
91 for (int i = 0; i < argc - 1; ++i) {
92 if (strcmp(argv[i], option) == 0 && (out = atoi(argv[i + 1])) > 0) {
93 return;
94 }
95 }
96 out = defaultValue;
97}
98
99inline void readUInt64Option(const char* option, int argc, char** argv, uint64_t& out, uint64_t defaultValue) {
100 for (int i = 0; i < argc - 1; ++i) {
101 if (strcmp(argv[i], option) == 0 && (out = std::strtoull(argv[i + 1], NULL, 0)) > 0) {
102 return;
103 }
104 }
105 out = defaultValue;
106}
107
108inline void readFloatOption(const char* option, int argc, char** argv, double& out, double defaultValue) {
109 for (int i = 0; i < argc - 1; ++i) {
110 if (strcmp(argv[i], option) == 0 && (out = atof(argv[i + 1])) > 0) {
111 return;
112 }
113 }
114 out = defaultValue;
115}
116
117inline void readInt(int argc, char** argv, int& out, int defaultValue) {
118 for (int i = 0; i < argc; ++i) {
119 if (*argv[i] != '-' && (out = atoi(argv[i])) > 0) {
120 return;
121 }
122 }
123 out = defaultValue;
124}
cryptonote::block b
Definition: block.cpp:40
const T buffer
Definition: byte_slice.cpp:83
static constexpr const char hex[]
Definition: wipeable_string.cpp:36
int * count
Definition: gmock_stress_test.cc:176
const char * name
Definition: options.c:30
const GenericPointer< typename T::ValueType > T2 defaultValue
Definition: pointer.h:1124
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition: pointer.h:1124
unsigned __int64 uint64_t
Definition: stdint.h:136
Definition: options.h:95
Definition: blockchain_usage.cpp:72
std::string data
Definition: base58.cpp:37
void outputHex(std::ostream &os, const char *data, int length)
Definition: utility.hpp:37
constexpr char hexmap[]
Definition: utility.hpp:36
void readIntOption(const char *option, int argc, char **argv, int &out, int defaultValue)
Definition: utility.hpp:90
void readUInt64Option(const char *option, int argc, char **argv, uint64_t &out, uint64_t defaultValue)
Definition: utility.hpp:99
void hex2bin(const char *in, int length, char *out)
Definition: utility.hpp:55
void readOption(const char *option, int argc, char **argv, bool &out)
Definition: utility.hpp:80
void dump(const char *buffer, uint64_t count, const char *name)
Definition: utility.hpp:74
char parseNibble(char hex)
Definition: utility.hpp:44
bool equalsHex(const void *hash, const char(&hex)[N])
Definition: utility.hpp:68
constexpr bool stringsEqual(char const *a, char const *b)
Definition: utility.hpp:63
void readInt(int argc, char **argv, int &out, int defaultValue)
Definition: utility.hpp:117
void readFloatOption(const char *option, int argc, char **argv, double &out, double defaultValue)
Definition: utility.hpp:108