/
Utils.impl.hpp
190 lines (161 loc) · 5.21 KB
/
Utils.impl.hpp
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*****************************************************************************/
/* Milliways - B+ trees and key-value store C++ library */
/* */
/* Copyright 2016 Marco Pantaleoni and J CUBE Inc. Tokyo, Japan. */
/* */
/* Author: Marco Pantaleoni <marco.pantaleoni@gmail.com> */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*****************************************************************************/
#ifndef MILLIWAYS_UTILS_H
#include "Utils.h"
#endif
#ifndef MILLIWAYS_UTILS_IMPL_H
//#define MILLIWAYS_UTILS_IMPL_H
#include <iostream>
#include <sstream>
#include <iomanip>
#include <ctype.h>
#include <stdio.h>
#ifdef _MSC_VER
#include <WinSock.h>
#else
#include <arpa/inet.h>
#endif
namespace milliways {
inline void hexdump(void* ptr, int buflen)
{
unsigned char *buf = (unsigned char*) ptr;
int i, j;
for (i = 0; i < buflen; i += 16)
{
fprintf(stderr, "%06x: ", i);
for (j = 0; j < 16; j++)
if (i+j < buflen)
fprintf(stderr, "%02x ", buf[i+j]);
else
fprintf(stderr, " ");
printf(" ");
for (j = 0; j < 16; j++)
if (i+j < buflen)
fprintf(stderr, "%c", isprint(buf[i+j]) ? buf[i+j] : '.');
fprintf(stderr, "\n");
}
}
inline std::ostream& hexdump(std::ostream& out, const void* ptr, int buflen)
{
const unsigned char *buf = (const unsigned char*) ptr;
int i, j;
long oldw;
char oldfill;
for (i = 0; i < buflen; i += 16)
{
oldw = out.width();
oldfill = out.fill();
out << std::setfill('0') << std::setw(6) << std::hex << i <<
std::setfill(oldfill) << std::setw(oldw) << std::dec << ": ";
// fprintf(stderr, "%06x: ", i);
for (j = 0; j < 16; j++)
{
if (i + j < buflen)
{
out << std::setfill('0') << std::setw(2) << std::hex << (unsigned int) (buf[i + j]) <<
std::setfill(oldfill) << std::setw(oldw) << std::dec << " ";
// fprintf(stderr, "%02x ", buf[i + j]);
} else {
out << " ";
// fprintf(stderr, " ");
}
}
out << " ";
// printf(" ");
for (j = 0; j < 16; j++)
{
if (i + j < buflen)
{
out << (char)(isprint(buf[i + j]) ? buf[i + j] : '.');
// fprintf(stderr, "%c", isprint(buf[i + j]) ? buf[i + j] : '.');
}
}
out << std::endl;
// fprintf(stderr, "\n");
}
return out;
}
inline std::string s_hexdump(const void* ptr, int buflen)
{
std::ostringstream ss;
hexdump(ss, ptr, buflen);
return ss.str();
}
inline std::string hexify(const std::string& input)
{
static const char* const lut = "0123456789ABCDEF";
size_t len = input.length();
std::string output(2 * len, '\0');
for (size_t i = 0; i < len; ++i)
{
const unsigned char c = input[i];
output[i * 2] = lut[(c & 0xF0) >> 4];
output[i * 2 + 1] = lut[(c & 0x0F)];
}
return output;
}
inline static unsigned int hexdigit2value(char hexdigit)
{
if ((hexdigit >= '0') && (hexdigit <= '9'))
return (hexdigit - '0');
else if ((hexdigit >= 'a') && (hexdigit <= 'f'))
return 10 + (hexdigit - 'a');
else if ((hexdigit >= 'A') && (hexdigit <= 'F'))
return 10 + (hexdigit - 'A');
return 10000;
}
inline std::string dehexify(const std::string& input)
{
size_t hexlen = input.length();
bool oddlen = (hexlen % 2) ? true : false;
size_t binlen = oddlen ? (hexlen / 2 + 1) : (hexlen / 2);
std::string output(binlen, '\0');
size_t b_i = 0, h_i = 0;
if (oddlen && (h_i < hexlen))
{
char ch = input[h_i];
output[b_i] = static_cast<char>(hexdigit2value(ch));
++h_i;
++b_i;
}
while (h_i < hexlen)
{
char ch = input[h_i];
output[b_i] = static_cast<char>(hexdigit2value(ch) << 4);
if (++h_i >= hexlen)
break;
ch = input[h_i];
output[b_i] = (output[b_i] & 0xF0) | static_cast<char>(hexdigit2value(ch));
++b_i;
if (++h_i >= hexlen)
break;
}
assert(b_i == binlen);
assert(h_i == hexlen);
return output;
}
/* ----------------------------------------------------------------- *
* shptr<T> *
* ----------------------------------------------------------------- */
#if defined(USE_MILLIWAYS_SHPTR)
shptr_manager shptr_manager::s_instance;
#endif /* defined(USE_MILLIWAYS_SHPTR) */
} /* end of namespace milliways */
#endif /* MILLIWAYS_UTILS_IMPL_H */