scclib
Stable Cloud Computing C++ Library
hex.cc
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
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 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <encode/hex.h>
32 #include <string>
33 #include <sstream>
34 
35 using namespace scc::encode;
36 
37 void Hex::bin_to_hex(const void* xloc, size_t len, std::string& hex, bool lower_case)
38 {
39  char* loc = (char*)xloc;
40 
41  hex.resize(2*len);
42 
43  char* out = (char*)hex.data();
44 
45  char hc = lower_case ? 'a' : 'A';
46 
47  while (len)
48  {
49  char hi = (*loc >> 4) & 0xf;
50  char lo = *loc & 0xf;
51 
52  if (hi < 10)
53  {
54  *out = hi+'0';
55  }
56  else
57  {
58  *out = hi-10+hc;
59  }
60  if (lo < 10)
61  {
62  *(out+1) = lo+'0';
63  }
64  else
65  {
66  *(out+1) = lo-10+hc;
67  }
68  loc++;
69  out += 2;
70  len--;
71  }
72 }
73 
74 std::string Hex::bin_to_hexstr(const void* loc, size_t len, const std::string& delimit, int limit, const std::string& limit_msg, bool lower_case)
75 {
76  std::string hex;
77  bin_to_hex(loc, len, hex, lower_case);
78 
79  std::stringstream s;
80 
81  for (size_t i = 0; i < hex.size(); i += 2)
82  {
83  if (limit == 0)
84  {
85  s << limit_msg;
86  break;
87  }
88  if (i != 0)
89  {
90  s << delimit;
91  }
92  s << hex[i] << hex[i+1];
93  if (limit > 0)
94  {
95  limit--;
96  }
97  }
98 
99  return s.str();
100 }
101 
102 std::string Hex::bin_to_hexstr(const std::vector<char>& bin, const std::string& delimit, int limit, const std::string& limit_msg, bool lower_case)
103 {
104  return bin_to_hexstr(bin.data(), bin.size(), delimit, limit, limit_msg, lower_case);
105 }
106 
107 void Hex::bin_to_hex(const std::vector<char>& bin, std::string& hex, bool lower_case)
108 {
109  bin_to_hex(bin.data(), bin.size(), hex, lower_case);
110 }
111 
112 std::string Hex::bin_to_hex(const void* loc, size_t len, bool lower_case)
113 {
114  std::string s;
115  bin_to_hex(loc, len, s, lower_case);
116  return s;
117 }
118 
119 std::string Hex::bin_to_hex(const std::vector<char>& bin, bool lower_case)
120 {
121  std::string s;
122  bin_to_hex(bin, s, lower_case);
123  return s;
124 }
125 
126 void Hex::hex_to_bin(const std::string& hex, std::vector<char>& bin)
127 {
128  bin.resize(hex.size()/2);
129 
130  size_t stor = 0;
131  for (size_t i = 0; i < hex.size()-1; i += 2)
132  {
133  char hi = hex[i];
134  char lo = hex[i+1];
135 
136  if (hi >= '0' && hi <= '9')
137  {
138  hi = (hi-'0');
139  }
140  else if (hi >= 'a' && hi <= 'f')
141  {
142  hi = (hi-'a') + 10;
143  }
144  else if (hi >= 'A' && hi <= 'F')
145  {
146  hi = (hi-'A') + 10;
147  }
148  else
149  {
150  bin.resize(stor);
151  return;
152  }
153  if (lo >= '0' && lo <= '9')
154  {
155  lo = (lo-'0');
156  }
157  else if (lo >= 'a' && lo <= 'f')
158  {
159  lo = (lo-'a') + 10;
160  }
161  else if (lo >= 'A' && lo <= 'F')
162  {
163  lo = (lo-'F') + 10;
164  }
165  else
166  {
167  bin.resize(stor);
168  return;
169  }
170  bin[i/2] = ((hi << 4) | lo);
171  stor++;
172  }
173  bin.resize(stor);
174 }
static void bin_to_hex(const std::vector< char > &, std::string &, bool=true)
Encode to hex from binary.
Definition: hex.cc:107
static std::string bin_to_hexstr(const std::vector< char > &, const std::string &="", int limit=-1, const std::string &=" +more", bool=true)
Binary to human readable string.
Definition: hex.cc:102
static void hex_to_bin(const std::string &, std::vector< char > &)
Decode from hex to binary.
Definition: hex.cc:126
Binary to hex string converter.