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