DCCL v3
binary.h
1 // Copyright 2009-2017 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (for 2013-)
3 // Massachusetts Institute of Technology (for 2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Dynamic Compact Control Language Library
8 // ("DCCL").
9 //
10 // DCCL is free software: you can redistribute it and/or modify
11 // it under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // DCCL is distributed in the hope that it will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with DCCL. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef DCCLBINARY20100713H
23 #define DCCLBINARY20100713H
24 
25 #include <iomanip>
26 #include <cmath>
27 #include <sstream>
28 
29 #include "dccl/common.h"
30 
31 #define DCCL_HAS_B64 1
32 
33 #if DCCL_HAS_B64
34 #include "b64/encode.h"
35 #include "b64/decode.h"
36 #endif
37 
38 namespace dccl
39 {
41 
42 
47  inline void hex_decode(const std::string& in, std::string* out)
48  {
49  static const short char0_9_to_number = 48;
50  static const short charA_F_to_number = 55;
51  static const short chara_f_to_number = 87;
52 
53  int in_size = in.size();
54  int out_size = in_size >> 1;
55  if(in_size & 1)
56  ++out_size;
57 
58  out->assign(out_size, '\0');
59  for(int i = (in_size & 1) ? -1 : 0, n = in_size;
60  i < n;
61  i += 2)
62  {
63  int out_i = (in_size & 1) ? (i+1) / 2 : i/2;
64 
65  if(i >= 0)
66  {
67  if(in[i] >= '0' && in[i] <= '9')
68  (*out)[out_i] |= ((in[i]-char0_9_to_number) & 0x0f) << 4;
69  else if(in[i] >= 'A' && in[i] <= 'F')
70  (*out)[out_i] |= ((in[i]-charA_F_to_number) & 0x0f) << 4;
71  else if(in[i] >= 'a' && in[i] <= 'f')
72  (*out)[out_i] |= ((in[i]-chara_f_to_number) & 0x0f) << 4;
73  }
74 
75  if(in[i+1] >= '0' && in[i+1] <= '9')
76  (*out)[out_i] |= (in[i+1]-char0_9_to_number) & 0x0f;
77  else if(in[i+1] >= 'A' && in[i+1] <= 'F')
78  (*out)[out_i] |= (in[i+1]-charA_F_to_number) & 0x0f;
79  else if(in[i+1] >= 'a' && in[i+1] <= 'f')
80  (*out)[out_i] |= (in[i+1]-chara_f_to_number) & 0x0f;
81  }
82  }
83 
84  inline std::string hex_decode(const std::string& in)
85  {
86  std::string out;
87  hex_decode(in, &out);
88  return out;
89  }
90 
97  template <typename CharIterator>
98  inline void hex_encode(CharIterator begin, CharIterator end, std::string* out, bool upper_case = false)
99  {
100  static const short char0_9_to_number = 48;
101  static const short charA_F_to_number = 55;
102  static const short chara_f_to_number = 87;
103 
104  size_t in_size = std::distance(begin, end);
105  size_t out_size = in_size << 1;
106 
107  out->clear();
108  out->resize(out_size);
109 
110  size_t i = 0;
111  for(CharIterator it = begin; it != end; ++it)
112  {
113  short msn = (*it >> 4) & 0x0f;
114  short lsn = *it & 0x0f;
115 
116  if(msn >= 0 && msn <= 9)
117  (*out)[2*i] = msn + char0_9_to_number;
118  else if(msn >= 10 && msn <= 15)
119  (*out)[2*i] = msn + (upper_case ? charA_F_to_number : chara_f_to_number);
120 
121  if(lsn >= 0 && lsn <= 9)
122  (*out)[2*i+1] = lsn + char0_9_to_number;
123  else if(lsn >= 10 && lsn <= 15)
124  (*out)[2*i+1] = lsn + (upper_case ? charA_F_to_number : chara_f_to_number);
125 
126  i++;
127  }
128  }
129 
130  template <typename CharIterator>
131  inline std::string hex_encode(CharIterator begin, CharIterator end)
132  {
133  std::string out;
134  hex_encode(begin, end, &out);
135  return out;
136 
137  }
138 
144  inline void hex_encode(const std::string& in, std::string* out, bool upper_case = false)
145  {
146  hex_encode(in.begin(), in.end(), out, upper_case);
147  }
148 
149  inline std::string hex_encode(const std::string& in)
150  {
151  std::string out;
152  hex_encode(in, &out);
153  return out;
154  }
155 
156 
157 #if DCCL_HAS_B64
158  inline std::string b64_encode(const std::string& in)
159  {
160  std::stringstream instream(in);
161  std::stringstream outstream;
162  base64::encoder D;
163  D.encode(instream, outstream);
164  return outstream.str();
165  }
166 
167  inline std::string b64_decode(const std::string& in)
168  {
169  std::stringstream instream(in);
170  std::stringstream outstream;
171  base64::decoder D;
172  D.decode(instream, outstream);
173  return outstream.str();
174  }
175 #endif
176 
178  inline unsigned ceil_log2(dccl::uint64 v)
179  {
180  // r will be one greater (ceil) if v is not a power of 2
181  unsigned r = ((v & (v - 1)) == 0) ? 0 : 1;
182  while (v >>= 1)
183  r++;
184  return r;
185  }
186 
187  inline unsigned long ceil_log2(double d)
188  { return ceil_log2(static_cast<dccl::uint64>(std::ceil(d))); }
189 
190  inline unsigned long ceil_log2(int i)
191  { return ceil_log2(static_cast<dccl::uint64>(i)); }
192 
193  inline unsigned long ceil_log2(long i)
194  { return ceil_log2(static_cast<dccl::uint64>(i)); }
195 
196  inline unsigned long ceil_log2(unsigned i)
197  { return ceil_log2(static_cast<dccl::uint64>(i)); }
198 
199  inline double log2(double d)
200  {
201  static double log_2 = log(2);
202  return log(d)/log_2;
203  }
204 
205 
206 
208 }
209 
210 #endif
void hex_decode(const std::string &in, std::string *out)
Decodes a (little-endian) hexadecimal string to a byte string. Index 0 and 1 (first byte) of in are w...
Definition: binary.h:47
unsigned ceil_log2(dccl::uint64 v)
Definition: binary.h:178
google::protobuf::uint64 uint64
an unsigned 64 bit integer
Definition: common.h:59
void hex_encode(CharIterator begin, CharIterator end, std::string *out, bool upper_case=false)
Encodes a (little-endian) hexadecimal string from a byte string. Index 0 of begin is written to index...
Definition: binary.h:98
Dynamic Compact Control Language namespace.