summaryrefslogtreecommitdiffstats
path: root/botan/src/codec/hex/hex.cpp
blob: fbacc278b7e19ee0123f1c1b77c1f57fcebe3407 (plain)
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
191
/*
* Hex Encoder/Decoder
* (C) 1999-2007 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#include <botan/hex.h>
#include <botan/parsing.h>
#include <botan/charset.h>
#include <botan/exceptn.h>
#include <algorithm>

namespace Botan {

/*
* Hex_Encoder Constructor
*/
Hex_Encoder::Hex_Encoder(bool breaks, u32bit length, Case c) :
   casing(c), line_length(breaks ? length : 0)
   {
   in.create(64);
   out.create(2*in.size());
   counter = position = 0;
   }

/*
* Hex_Encoder Constructor
*/
Hex_Encoder::Hex_Encoder(Case c) : casing(c), line_length(0)
   {
   in.create(64);
   out.create(2*in.size());
   counter = position = 0;
   }

/*
* Hex Encoding Operation
*/
void Hex_Encoder::encode(byte in, byte out[2], Hex_Encoder::Case casing)
   {
   const byte* BIN_TO_HEX = ((casing == Uppercase) ? BIN_TO_HEX_UPPER :
                                                     BIN_TO_HEX_LOWER);

   out[0] = BIN_TO_HEX[((in >> 4) & 0x0F)];
   out[1] = BIN_TO_HEX[((in     ) & 0x0F)];
   }

/*
* Encode and send a block
*/
void Hex_Encoder::encode_and_send(const byte block[], u32bit length)
   {
   for(u32bit j = 0; j != length; ++j)
      encode(block[j], out + 2*j, casing);

   if(line_length == 0)
      send(out, 2*length);
   else
      {
      u32bit remaining = 2*length, offset = 0;
      while(remaining)
         {
         u32bit sent = std::min(line_length - counter, remaining);
         send(out + offset, sent);
         counter += sent;
         remaining -= sent;
         offset += sent;
         if(counter == line_length)
            {
            send('\n');
            counter = 0;
            }
         }
      }
   }

/*
* Convert some data into hex format
*/
void Hex_Encoder::write(const byte input[], u32bit length)
   {
   in.copy(position, input, length);
   if(position + length >= in.size())
      {
      encode_and_send(in, in.size());
      input += (in.size() - position);
      length -= (in.size() - position);
      while(length >= in.size())
         {
         encode_and_send(input, in.size());
         input += in.size();
         length -= in.size();
         }
      in.copy(input, length);
      position = 0;
      }
   position += length;
   }

/*
* Flush buffers
*/
void Hex_Encoder::end_msg()
   {
   encode_and_send(in, position);
   if(counter && line_length)
      send('\n');
   counter = position = 0;
   }

/*
* Hex_Decoder Constructor
*/
Hex_Decoder::Hex_Decoder(Decoder_Checking c) : checking(c)
   {
   in.create(64);
   out.create(in.size() / 2);
   position = 0;
   }

/*
* Check if a character is a valid hex char
*/
bool Hex_Decoder::is_valid(byte in)
   {
   return (HEX_TO_BIN[in] != 0x80);
   }

/*
* Handle processing an invalid character
*/
void Hex_Decoder::handle_bad_char(byte c)
   {
   if(checking == NONE)
      return;

   if((checking == IGNORE_WS) && Charset::is_space(c))
      return;

   throw Decoding_Error("Hex_Decoder: Invalid hex character: " +
                        to_string(c));
   }

/*
* Hex Decoding Operation
*/
byte Hex_Decoder::decode(const byte hex[2])
   {
   return ((HEX_TO_BIN[hex[0]] << 4) | HEX_TO_BIN[hex[1]]);
   }

/*
* Decode and send a block
*/
void Hex_Decoder::decode_and_send(const byte block[], u32bit length)
   {
   for(u32bit j = 0; j != length / 2; ++j)
      out[j] = decode(block + 2*j);
   send(out, length / 2);
   }

/*
* Convert some data from hex format
*/
void Hex_Decoder::write(const byte input[], u32bit length)
   {
   for(u32bit j = 0; j != length; ++j)
      {
      if(is_valid(input[j]))
         in[position++] = input[j];
      else
         handle_bad_char(input[j]);
      if(position == in.size())
         {
         decode_and_send(in, in.size());
         position = 0;
         }
      }
   }

/*
* Flush buffers
*/
void Hex_Decoder::end_msg()
   {
   decode_and_send(in, position);
   position = 0;
   }

}