summaryrefslogtreecommitdiffhomepage
path: root/src/validate.cpp
blob: 78e61755500b3e0427932ac8861199fbbec4e6ce (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
#include "file.h"

#include "unicode.h"

#include <boost/endian.hpp>

#include <filesystem>
#include <functional>
#include <iostream>
#include <string>
#include <unordered_map>

namespace fs = std::filesystem;

namespace {

void usage()
{
 std::cout << "Usage: validate <format> <file>" << std::endl;
 std::cout << "Format:" << std::endl;
 std::cout << "    UTF-8     UTF-8" << std::endl;
 std::cout << "    UTF-16    UTF-16, big or little endian" << std::endl;
 std::cout << "    UTF-16LE  UTF-16, little endian" << std::endl;
 std::cout << "    UTF-16BE  UTF-16, big endian" << std::endl;
 std::cout << "    UTF-32    UTF-32, big or little endian" << std::endl;
 std::cout << "    UTF-32LE  UTF-32, little endian" << std::endl;
 std::cout << "    UTF-32BE  UTF-32, big endian" << std::endl;
 std::cout << "Exit code: 0 if valid, 1 otherwise." << std::endl;
}

std::unordered_map<std::string, std::function<bool(const std::string&)>> validate_map
{
 { "UTF-8", [](const std::string& s) -> bool { return unicode::is_valid_utf(s); }},
 { "UTF-16", [](const std::string& s) -> bool
  {
   if (s.size() & 1) // need even number of bytes
    return false;

   std::u16string data(s.size() / 2, u'\0');
   std::memcpy(data.data(), s.data(), s.size());

   if (unicode::is_valid_utf(data))
    return true;

   // maybe reverse endianess
   std::for_each(data.begin(), data.end(), [](char16_t& c){boost::endian::endian_reverse_inplace(c);});

   return unicode::is_valid_utf(data);
  }
 },
 { "UTF-16LE", [](const std::string& s) -> bool
  {
   if (s.size() & 1) // need even number of bytes
    return false;

   std::u16string data(s.size() / 2, u'\0');
   std::memcpy(data.data(), s.data(), s.size());

   if (boost::endian::order::native != boost::endian::order::little)
    std::for_each(data.begin(), data.end(), [](char16_t& c){boost::endian::native_to_little_inplace(c);});

   return unicode::is_valid_utf(data);
  }
 },
 { "UTF-16BE", [](const std::string& s) -> bool
  {
   if (s.size() & 1) // need even number of bytes
    return false;

   std::u16string data(s.size() / 2, u'\0');
   std::memcpy(data.data(), s.data(), s.size());

   if (boost::endian::order::native != boost::endian::order::big)
    std::for_each(data.begin(), data.end(), [](char16_t& c){boost::endian::native_to_big_inplace(c);});

   return unicode::is_valid_utf(data);
  }
 },
 { "UTF-32", [](const std::string& s) -> bool
  {
   if (s.size() & 3) // need even number of bytes
    return false;

   std::u32string data(s.size() / 4, U'\0');
   std::memcpy(data.data(), s.data(), s.size());

   if (unicode::is_valid_utf(data))
    return true;

   // maybe reverse endianess
   std::for_each(data.begin(), data.end(), [](char32_t& c){boost::endian::endian_reverse_inplace(c);});

   return unicode::is_valid_utf(data);
  }
 },
 { "UTF-32LE", [](const std::string& s) -> bool
  {
   if (s.size() & 3) // need multiple of 4 bytes
    return false;

   std::u32string data(s.size() / 4, U'\0');
   std::memcpy(data.data(), s.data(), s.size());

   if (boost::endian::order::native != boost::endian::order::little)
    std::for_each(data.begin(), data.end(), [](char32_t& c){boost::endian::native_to_little_inplace(c);});

   return unicode::is_valid_utf(data);
  }
 },
 { "UTF-32BE", [](const std::string& s) -> bool
  {
   if (s.size() & 3) // need multiple of 4 bytes
    return false;

   std::u32string data(s.size() / 4, U'\0');
   std::memcpy(data.data(), s.data(), s.size());

   if (boost::endian::order::native != boost::endian::order::big)
    std::for_each(data.begin(), data.end(), [](char32_t& c){boost::endian::native_to_big_inplace(c);});

   return unicode::is_valid_utf(data);
  }
 },
};

}

int main(int argc, char* argv[])
{
 if (argc != 3) {
  usage();
  return 1;
 }

 try {
  std::string format {argv[1]};
  fs::path path {argv[2]};

  std::string data{unicode::File::getFile(path)};

  auto it { validate_map.find(format) };
  if (it == validate_map.end()) {
   std::cerr << "Error: Encoding " << format << " not supported." << std::endl;
   return 1;
  }

  return it->second(data) ? 0 : 1;

 } catch (const std::exception& ex) {
  std::cerr << "Error: " << ex.what() << std::endl;
  return 1;
 }
 return 0;
}