Browse code
Reformat, add TODO
Ed Langley authored on 13/06/2017 21:39:50
Showing 3 changed files
Showing 3 changed files
... | ... |
@@ -12,6 +12,7 @@ |
12 | 12 |
#include <bitset> |
13 | 13 |
#include <unordered_map> |
14 | 14 |
#include <memory> |
15 |
+#include <iostream> |
|
15 | 16 |
|
16 | 17 |
#include "base32.h" |
17 | 18 |
|
... | ... |
@@ -27,7 +28,7 @@ namespace |
27 | 28 |
class base32_impl : public base32_ifc |
28 | 29 |
{ |
29 | 30 |
private: |
30 |
- uint64_t calculate_padding_bytes (uint64_t extra_bytes) |
|
31 |
+ uint64_t calculate_padding_bytes (uint64_t extra_bytes) const |
|
31 | 32 |
{ |
32 | 33 |
auto padding_chars = 0; |
33 | 34 |
|
... | ... |
@@ -52,7 +53,7 @@ private: |
52 | 53 |
return padding_chars; |
53 | 54 |
} |
54 | 55 |
|
55 |
- std::string &pad_string (uint64_t extra_bytes, std::string &input) |
|
56 |
+ std::string &pad_string (uint64_t extra_bytes, std::string &input) const |
|
56 | 57 |
{ |
57 | 58 |
auto padding_chars = calculate_padding_bytes (extra_bytes); |
58 | 59 |
|
... | ... |
@@ -68,7 +69,7 @@ private: |
68 | 69 |
} |
69 | 70 |
|
70 | 71 |
public: |
71 |
- std::string encode (const std::vector<uint8_t> data) override |
|
72 |
+ std::string encode (const std::vector<uint8_t> data) const override |
|
72 | 73 |
{ |
73 | 74 |
std::string result; |
74 | 75 |
|
... | ... |
@@ -117,7 +118,7 @@ public: |
117 | 118 |
|
118 | 119 |
private: |
119 | 120 |
|
120 |
- std::unordered_map <char, unsigned char> construct_lookup_table() |
|
121 |
+ std::unordered_map <char, unsigned char> construct_lookup_table() const |
|
121 | 122 |
{ |
122 | 123 |
std::unordered_map<char, unsigned char> lookup_table; |
123 | 124 |
|
... | ... |
@@ -129,7 +130,7 @@ private: |
129 | 130 |
} |
130 | 131 |
|
131 | 132 |
std::pair<uint8_t, uint8_t> split_value (uint8_t item, uint8_t pos, |
132 |
- uint8_t width) |
|
133 |
+ uint8_t width) const |
|
133 | 134 |
{ |
134 | 135 |
auto shift = width-pos; |
135 | 136 |
uint8_t part2_mask = (1 << (shift)) - 1; |
... | ... |
@@ -140,7 +141,7 @@ private: |
140 | 141 |
|
141 | 142 |
void set_vector_at_bit (std::vector<uint8_t> &data, uint8_t item, |
142 | 143 |
std::vector<uint8_t>::size_type byte_offset, uint8_t bit_offset_from_start, |
143 |
- uint8_t width) |
|
144 |
+ uint8_t width) const |
|
144 | 145 |
{ |
145 | 146 |
bool need_to_split = bit_offset_from_start + width > 8; |
146 | 147 |
uint8_t bit_offset_from_end = 7 - bit_offset_from_start; |
... | ... |
@@ -159,7 +160,7 @@ private: |
159 | 160 |
} |
160 | 161 |
} |
161 | 162 |
|
162 |
- std::string::size_type calculate_decoded_size (std::string input) |
|
163 |
+ std::string::size_type calculate_decoded_size (std::string input) const |
|
163 | 164 |
{ |
164 | 165 |
std::string::size_type input_size = input.size(); |
165 | 166 |
std::string::size_type first_equals = input.find_first_of ('='); |
... | ... |
@@ -173,8 +174,9 @@ private: |
173 | 174 |
|
174 | 175 |
public: |
175 | 176 |
|
176 |
- std::vector<uint8_t> decode (std::string input) override |
|
177 |
+ std::vector<uint8_t> decode (std::string input) const override |
|
177 | 178 |
{ |
179 |
+ //TODO: where to pad input to required size? |
|
178 | 180 |
auto lookup_table = construct_lookup_table(); |
179 | 181 |
auto input_size = calculate_decoded_size (input); |
180 | 182 |
|
... | ... |
@@ -193,6 +195,8 @@ public: |
193 | 195 |
bits_written += 5; |
194 | 196 |
} |
195 | 197 |
|
198 |
+ |
|
199 |
+ |
|
196 | 200 |
return result; |
197 | 201 |
} |
198 | 202 |
}; |
... | ... |
@@ -19,8 +19,8 @@ |
19 | 19 |
class base32_ifc |
20 | 20 |
{ |
21 | 21 |
public: |
22 |
- virtual std::string encode (std::vector<uint8_t> input) = 0; |
|
23 |
- virtual std::vector<uint8_t> decode (std::string input) = 0; |
|
22 |
+ virtual std::string encode (std::vector<uint8_t> input) const = 0; |
|
23 |
+ virtual std::vector<uint8_t> decode (std::string input) const = 0; |
|
24 | 24 |
}; |
25 | 25 |
|
26 | 26 |
class base32 |
... | ... |
@@ -32,16 +32,15 @@ private: |
32 | 32 |
public: |
33 | 33 |
base32 (); |
34 | 34 |
|
35 |
- std::string encode (std::vector<uint8_t> input) |
|
35 |
+ std::string encode (std::vector<uint8_t> input) const |
|
36 | 36 |
{ |
37 | 37 |
return delegate_->encode (input); |
38 | 38 |
}; |
39 | 39 |
|
40 |
- std::vector<uint8_t> decode (std::string input) |
|
40 |
+ std::vector<uint8_t> decode (std::string input) const |
|
41 | 41 |
{ |
42 | 42 |
return delegate_->decode (input); |
43 | 43 |
}; |
44 | 44 |
}; |
45 | 45 |
|
46 | 46 |
#endif |
47 |
- |
... | ... |
@@ -58,6 +58,18 @@ int precomputed_values() |
58 | 58 |
succeed(); |
59 | 59 |
} |
60 | 60 |
|
61 |
+int decode_validates_input() |
|
62 |
+{ |
|
63 |
+ // given |
|
64 |
+ // The token for key 76I6WTYEUTNCJUREMGKVM45PMA and time '2017/01/01 00:00:00' is 258675 |
|
65 |
+ base32 codec = base32(); |
|
66 |
+ |
|
67 |
+ std::vector<uint8_t> result = codec.decode("\x00"); |
|
68 |
+ check(result.empty(), "invalid input doesn't produce an empty result"); |
|
69 |
+ |
|
70 |
+ succeed(); |
|
71 |
+} |
|
72 |
+ |
|
61 | 73 |
int roundtrip() |
62 | 74 |
{ |
63 | 75 |
// given |
... | ... |
@@ -66,7 +78,6 @@ int roundtrip() |
66 | 78 |
{ 5, 5, 5, 5, 5, 5, }, { 6, 6, 6, 6, 6, 6, 6, } |
67 | 79 |
}; |
68 | 80 |
|
69 |
- // The token for key 76I6WTYEUTNCJUREMGKVM45PMA and time '2017/01/01 00:00:00' is 258675 |
|
70 | 81 |
base32 codec = base32(); |
71 | 82 |
|
72 | 83 |
for (auto expected: values) { |
... | ... |
@@ -82,6 +93,7 @@ int roundtrip() |
82 | 93 |
int run_tests() |
83 | 94 |
{ |
84 | 95 |
test (precomputed_values); |
96 |
+ test (decode_validates_input); |
|
85 | 97 |
test (roundtrip); |
86 | 98 |
succeed(); |
87 | 99 |
} |
... | ... |
@@ -90,4 +102,3 @@ int main (int argc, char *argv[]) |
90 | 102 |
{ |
91 | 103 |
return !run_tests(); |
92 | 104 |
} |
93 |
- |