1// Copyright 2012 Google Inc. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef POLO_ENCODING_ENCODINGOPTION_H_
16#define POLO_ENCODING_ENCODINGOPTION_H_
17
18#include <stdint.h>
19#include <functional>
20#include <set>
21#include <string>
22
23namespace polo {
24namespace encoding {
25
26// An encoding option for a challenge message consisting of an encoding scheme
27// and symbol length.
28class EncodingOption {
29 public:
30  // Representation of a specific encoding type. The numeric values should
31  // be sorted by encoding complexity from least to greatest.
32  enum EncodingType {
33    // Unknown encoding type.
34    kUnknown = 0,
35
36    // Text message composed of characters [0-9].
37    kNumeric = 1,
38
39    // Text message composed of characters [0-9A-Za-z]+.
40    kAlphaNumeric = 2,
41
42    // Text message composed of characters [0-9A-Fa-f]+.
43    kHexadecimal = 3,
44
45    // 2-dimensional barcode, containing binary bitstream.
46    kQRCode = 4,
47  };
48
49  // Creates a new encoding option.
50  // @param encoding_type the encoding type
51  // @param symbol_length the encoding symbole length
52  EncodingOption(EncodingType encoding_type, uint32_t symbol_length);
53
54  // Gets the encoding scheme for the challenge message.
55  EncodingType encoding_type() const;
56
57  // Gets the number of symbols used in the challenge message for the encoding
58  // type specified by this encoding option. For example, a single symbol for
59  // hexadecimal encoding consists of 4-bits from the set [0-9A-Fa-f].
60  uint32_t symbol_length() const;
61
62  // Determines whether the given encoding option is the same as this one.
63  bool Equals(const EncodingOption& other) const;
64
65  // Returns a string representation of this encoding option.
66  std::string ToString() const;
67
68  // EncodingOption comparator for set ordering.
69  struct EncodingOptionComparator : public std::binary_function<
70      EncodingOption, EncodingOption, bool> {
71    bool operator()(const EncodingOption& option1,
72                    const EncodingOption& option2) {
73      // Sort encoding options by complexity.
74      return (option1.encoding_type() == option2.encoding_type()
75          && option1.symbol_length() < option2.symbol_length())
76          || (option1.encoding_type() < option2.encoding_type());
77    }
78  };
79
80  // Predicate for finding an encoding option.
81  struct EncodingOptionPredicate
82      : public std::unary_function<EncodingOption, bool> {
83    const EncodingOption& option_;
84
85    explicit EncodingOptionPredicate(const EncodingOption& option)
86        : option_(option) {}
87
88    bool operator()(const EncodingOption& other) const {
89      return option_.Equals(other);
90    }
91  };
92
93  // Definition for a set of EncodingOptions that are ordered by complexity.
94  typedef std::set<EncodingOption, EncodingOptionComparator>
95      EncodingSet;
96
97 private:
98  EncodingType encoding_type_;
99  uint32_t symbol_length_;
100};
101
102}  // namespace encoding
103}  // namespace polo
104
105#endif  // POLO_ENCODING_ENCODINGOPTION_H_
106