Monero
language_base.h
Go to the documentation of this file.
1 // Copyright (c) 2014-2018, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 
35 #ifndef LANGUAGE_BASE_H
36 #define LANGUAGE_BASE_H
37 
38 #include <vector>
39 #include <unordered_map>
40 #include <string>
41 #include "misc_log_ex.h"
42 
47 namespace Language
48 {
56  template<typename T>
57  inline T utf8prefix(const T &s, size_t count)
58  {
59  T prefix = "";
60  size_t avail = s.size();
61  const char *ptr = s.data();
62  while (count-- && avail--)
63  {
64  prefix += *ptr++;
65  while (avail && ((*ptr) & 0xc0) == 0x80)
66  {
67  prefix += *ptr++;
68  --avail;
69  }
70  }
71  return prefix;
72  }
73 
79  class Base
80  {
81  protected:
82  enum {
85  };
86  enum {
87  NWORDS = 1626
88  };
89  std::vector<std::string> word_list;
90  std::unordered_map<epee::wipeable_string, uint32_t> word_map;
91  std::unordered_map<epee::wipeable_string, uint32_t> trimmed_word_map;
92  std::string language_name;
93  std::string english_language_name;
98  void populate_maps(uint32_t flags = 0)
99  {
100  int ii;
101  std::vector<std::string>::const_iterator it;
102  if (word_list.size () != NWORDS)
103  throw std::runtime_error("Wrong word list length for " + language_name);
104  for (it = word_list.begin(), ii = 0; it != word_list.end(); it++, ii++)
105  {
106  word_map[*it] = ii;
107  if ((*it).size() < unique_prefix_length)
108  {
109  if (flags & ALLOW_SHORT_WORDS)
110  MWARNING(language_name << " word '" << *it << "' is shorter than its prefix length, " << unique_prefix_length);
111  else
112  throw std::runtime_error("Too short word in " + language_name + " word list: " + *it);
113  }
114  epee::wipeable_string trimmed;
115  if (it->length() > unique_prefix_length)
116  {
117  trimmed = utf8prefix(*it, unique_prefix_length);
118  }
119  else
120  {
121  trimmed = *it;
122  }
123  if (trimmed_word_map.find(trimmed) != trimmed_word_map.end())
124  {
125  if (flags & ALLOW_DUPLICATE_PREFIXES)
126  MWARNING("Duplicate prefix in " << language_name << " word list: " << std::string(trimmed.data(), trimmed.size()));
127  else
128  throw std::runtime_error("Duplicate prefix in " + language_name + " word list: " + std::string(trimmed.data(), trimmed.size()));
129  }
130  trimmed_word_map[trimmed] = ii;
131  }
132  }
133  public:
134  Base(const char *language_name, const char *english_language_name, const std::vector<std::string> &words, uint32_t prefix_length):
135  word_list(words),
136  unique_prefix_length(prefix_length),
139  {
140  }
141  virtual ~Base()
142  {
143  }
144  void set_words(const char * const words[])
145  {
146  word_list.resize(NWORDS);
147  for (size_t i = 0; i < NWORDS; ++i)
148  word_list[i] = words[i];
149  }
154  const std::vector<std::string>& get_word_list() const
155  {
156  return word_list;
157  }
162  const std::unordered_map<epee::wipeable_string, uint32_t>& get_word_map() const
163  {
164  return word_map;
165  }
170  const std::unordered_map<epee::wipeable_string, uint32_t>& get_trimmed_word_map() const
171  {
172  return trimmed_word_map;
173  }
178  const std::string &get_language_name() const
179  {
180  return language_name;
181  }
186  const std::string &get_english_language_name() const
187  {
188  return english_language_name;
189  }
194  uint32_t get_unique_prefix_length() const
195  {
196  return unique_prefix_length;
197  }
198  };
199 }
200 
201 #endif
const uint32_t T[512]
Definition: groestl_tables.h:33
Base(const char *language_name, const char *english_language_name, const std::vector< std::string > &words, uint32_t prefix_length)
Definition: language_base.h:134
std::string language_name
Definition: language_base.h:92
std::vector< std::string > word_list
Definition: language_base.h:89
A base language class which all languages have to inherit from for Polymorphism.
Definition: language_base.h:79
Definition: language_base.h:83
void set_words(const char *const words[])
Definition: language_base.h:144
uint32_t get_unique_prefix_length() const
Returns the number of unique starting characters to be used for matching.
Definition: language_base.h:194
Mnemonic language related namespace.
T utf8prefix(const T &s, size_t count)
Returns a string made of (at most) the first count characters in s. Assumes well formedness. No check is made for this.
Definition: language_base.h:57
std::string english_language_name
Definition: language_base.h:93
const std::vector< std::string > & get_word_list() const
Returns a pointer to the word list.
Definition: language_base.h:154
virtual ~Base()
Definition: language_base.h:141
const std::unordered_map< epee::wipeable_string, uint32_t > & get_trimmed_word_map() const
Returns a pointer to the trimmed word map.
Definition: language_base.h:170
const std::string & get_english_language_name() const
Returns the name of the language in English.
Definition: language_base.h:186
std::unordered_map< epee::wipeable_string, uint32_t > trimmed_word_map
Definition: language_base.h:91
Definition: language_base.h:84
void populate_maps(uint32_t flags=0)
Populates the word maps after the list is ready.
Definition: language_base.h:98
Definition: language_base.h:87
const std::string & get_language_name() const
Returns the name of the language.
Definition: language_base.h:178
std::unordered_map< epee::wipeable_string, uint32_t > word_map
Definition: language_base.h:90
const std::unordered_map< epee::wipeable_string, uint32_t > & get_word_map() const
Returns a pointer to the word map.
Definition: language_base.h:162
#define s(x, c)
Definition: aesb.c:46
uint32_t unique_prefix_length
Definition: language_base.h:94