aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/tests/validation_test_input_parser.cc
blob: f3097372a412aea629f7a6b9779fb3242908d435 (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h"

#include <assert.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include <limits>
#include <map>
#include <set>
#include <utility>

#include "mojo/public/c/system/macros.h"

namespace mojo {
namespace test {
namespace {

class ValidationTestInputParser {
 public:
  ValidationTestInputParser(const std::string& input,
                            std::vector<uint8_t>* data,
                            size_t* num_handles,
                            std::string* error_message);
  ~ValidationTestInputParser();

  bool Run();

 private:
  struct DataType;

  typedef std::pair<const char*, const char*> Range;

  typedef bool (ValidationTestInputParser::*ParseDataFunc)(
      const DataType& type,
      const std::string& value_string);

  struct DataType {
    const char* name;
    size_t name_size;
    size_t data_size;
    ParseDataFunc parse_data_func;
  };

  // A dist4/8 item that hasn't been matched with an anchr item.
  struct PendingDistanceItem {
    // Where this data item is located in |data_|.
    size_t pos;
    // Either 4 or 8 (bytes).
    size_t data_size;
  };

  bool GetNextItem(Range* range);

  bool ParseItem(const Range& range);

  bool ParseUnsignedInteger(const DataType& type,
                            const std::string& value_string);
  bool ParseSignedInteger(const DataType& type,
                          const std::string& value_string);
  bool ParseFloat(const DataType& type, const std::string& value_string);
  bool ParseDouble(const DataType& type, const std::string& value_string);
  bool ParseBinarySequence(const DataType& type,
                           const std::string& value_string);
  bool ParseDistance(const DataType& type, const std::string& value_string);
  bool ParseAnchor(const DataType& type, const std::string& value_string);
  bool ParseHandles(const DataType& type, const std::string& value_string);

  bool StartsWith(const Range& range, const char* prefix, size_t prefix_length);

  bool ConvertToUnsignedInteger(const std::string& value_string,
                                unsigned long long int* value);

  template <typename T>
  void AppendData(T data) {
    size_t pos = data_->size();
    data_->resize(pos + sizeof(T));
    memcpy(&(*data_)[pos], &data, sizeof(T));
  }

  template <typename TargetType, typename InputType>
  bool ConvertAndAppendData(InputType value) {
    if (value > std::numeric_limits<TargetType>::max() ||
        value < std::numeric_limits<TargetType>::min()) {
      return false;
    }
    AppendData(static_cast<TargetType>(value));
    return true;
  }

  template <typename TargetType, typename InputType>
  bool ConvertAndFillData(size_t pos, InputType value) {
    if (value > std::numeric_limits<TargetType>::max() ||
        value < std::numeric_limits<TargetType>::min()) {
      return false;
    }
    TargetType target_value = static_cast<TargetType>(value);
    assert(pos + sizeof(TargetType) <= data_->size());
    memcpy(&(*data_)[pos], &target_value, sizeof(TargetType));
    return true;
  }

  static const DataType kDataTypes[];
  static const size_t kDataTypeCount;

  const std::string& input_;
  size_t input_cursor_;

  std::vector<uint8_t>* data_;
  size_t* num_handles_;
  std::string* error_message_;

  std::map<std::string, PendingDistanceItem> pending_distance_items_;
  std::set<std::string> anchors_;
};

#define DATA_TYPE(name, data_size, parse_data_func) \
  { name, sizeof(name) - 1, data_size, parse_data_func }

const ValidationTestInputParser::DataType
    ValidationTestInputParser::kDataTypes[] = {
        DATA_TYPE("[u1]", 1, &ValidationTestInputParser::ParseUnsignedInteger),
        DATA_TYPE("[u2]", 2, &ValidationTestInputParser::ParseUnsignedInteger),
        DATA_TYPE("[u4]", 4, &ValidationTestInputParser::ParseUnsignedInteger),
        DATA_TYPE("[u8]", 8, &ValidationTestInputParser::ParseUnsignedInteger),
        DATA_TYPE("[s1]", 1, &ValidationTestInputParser::ParseSignedInteger),
        DATA_TYPE("[s2]", 2, &ValidationTestInputParser::ParseSignedInteger),
        DATA_TYPE("[s4]", 4, &ValidationTestInputParser::ParseSignedInteger),
        DATA_TYPE("[s8]", 8, &ValidationTestInputParser::ParseSignedInteger),
        DATA_TYPE("[b]", 1, &ValidationTestInputParser::ParseBinarySequence),
        DATA_TYPE("[f]", 4, &ValidationTestInputParser::ParseFloat),
        DATA_TYPE("[d]", 8, &ValidationTestInputParser::ParseDouble),
        DATA_TYPE("[dist4]", 4, &ValidationTestInputParser::ParseDistance),
        DATA_TYPE("[dist8]", 8, &ValidationTestInputParser::ParseDistance),
        DATA_TYPE("[anchr]", 0, &ValidationTestInputParser::ParseAnchor),
        DATA_TYPE("[handles]", 0, &ValidationTestInputParser::ParseHandles)};

const size_t ValidationTestInputParser::kDataTypeCount =
    sizeof(ValidationTestInputParser::kDataTypes) /
    sizeof(ValidationTestInputParser::kDataTypes[0]);

ValidationTestInputParser::ValidationTestInputParser(const std::string& input,
                                                     std::vector<uint8_t>* data,
                                                     size_t* num_handles,
                                                     std::string* error_message)
    : input_(input),
      input_cursor_(0),
      data_(data),
      num_handles_(num_handles),
      error_message_(error_message) {
  assert(data_);
  assert(num_handles_);
  assert(error_message_);
  data_->clear();
  *num_handles_ = 0;
  error_message_->clear();
}

ValidationTestInputParser::~ValidationTestInputParser() {
}

bool ValidationTestInputParser::Run() {
  Range range;
  bool result = true;
  while (result && GetNextItem(&range))
    result = ParseItem(range);

  if (!result) {
    *error_message_ =
        "Error occurred when parsing " + std::string(range.first, range.second);
  } else if (!pending_distance_items_.empty()) {
    // We have parsed all the contents in |input_| successfully, but there are
    // unmatched dist4/8 items.
    *error_message_ = "Error occurred when matching [dist4/8] and [anchr].";
    result = false;
  }
  if (!result) {
    data_->clear();
    *num_handles_ = 0;
  } else {
    assert(error_message_->empty());
  }

  return result;
}

bool ValidationTestInputParser::GetNextItem(Range* range) {
  const char kWhitespaceChars[] = " \t\n\r";
  const char kItemDelimiters[] = " \t\n\r/";
  const char kEndOfLineChars[] = "\n\r";
  while (true) {
    // Skip leading whitespaces.
    // If there are no non-whitespace characters left, |input_cursor_| will be
    // set to std::npos.
    input_cursor_ = input_.find_first_not_of(kWhitespaceChars, input_cursor_);

    if (input_cursor_ >= input_.size())
      return false;

    if (StartsWith(
            Range(&input_[0] + input_cursor_, &input_[0] + input_.size()),
            "//",
            2)) {
      // Skip contents until the end of the line.
      input_cursor_ = input_.find_first_of(kEndOfLineChars, input_cursor_);
    } else {
      range->first = &input_[0] + input_cursor_;
      input_cursor_ = input_.find_first_of(kItemDelimiters, input_cursor_);
      range->second = input_cursor_ >= input_.size()
                          ? &input_[0] + input_.size()
                          : &input_[0] + input_cursor_;
      return true;
    }
  }
  return false;
}

bool ValidationTestInputParser::ParseItem(const Range& range) {
  for (size_t i = 0; i < kDataTypeCount; ++i) {
    if (StartsWith(range, kDataTypes[i].name, kDataTypes[i].name_size)) {
      return (this->*kDataTypes[i].parse_data_func)(
          kDataTypes[i],
          std::string(range.first + kDataTypes[i].name_size, range.second));
    }
  }

  // "[u1]" is optional.
  return ParseUnsignedInteger(kDataTypes[0],
                              std::string(range.first, range.second));
}

bool ValidationTestInputParser::ParseUnsignedInteger(
    const DataType& type,
    const std::string& value_string) {
  unsigned long long int value;
  if (!ConvertToUnsignedInteger(value_string, &value))
    return false;

  switch (type.data_size) {
    case 1:
      return ConvertAndAppendData<uint8_t>(value);
    case 2:
      return ConvertAndAppendData<uint16_t>(value);
    case 4:
      return ConvertAndAppendData<uint32_t>(value);
    case 8:
      return ConvertAndAppendData<uint64_t>(value);
    default:
      assert(false);
      return false;
  }
}

bool ValidationTestInputParser::ParseSignedInteger(
    const DataType& type,
    const std::string& value_string) {
  long long int value;
  if (sscanf(value_string.c_str(), "%lli", &value) != 1)
    return false;

  switch (type.data_size) {
    case 1:
      return ConvertAndAppendData<int8_t>(value);
    case 2:
      return ConvertAndAppendData<int16_t>(value);
    case 4:
      return ConvertAndAppendData<int32_t>(value);
    case 8:
      return ConvertAndAppendData<int64_t>(value);
    default:
      assert(false);
      return false;
  }
}

bool ValidationTestInputParser::ParseFloat(const DataType& type,
                                           const std::string& value_string) {
  static_assert(sizeof(float) == 4, "sizeof(float) is not 4");

  float value;
  if (sscanf(value_string.c_str(), "%f", &value) != 1)
    return false;

  AppendData(value);
  return true;
}

bool ValidationTestInputParser::ParseDouble(const DataType& type,
                                            const std::string& value_string) {
  static_assert(sizeof(double) == 8, "sizeof(double) is not 8");

  double value;
  if (sscanf(value_string.c_str(), "%lf", &value) != 1)
    return false;

  AppendData(value);
  return true;
}

bool ValidationTestInputParser::ParseBinarySequence(
    const DataType& type,
    const std::string& value_string) {
  if (value_string.size() != 8)
    return false;

  uint8_t value = 0;
  for (std::string::const_iterator iter = value_string.begin();
       iter != value_string.end();
       ++iter) {
    value <<= 1;
    if (*iter == '1')
      value++;
    else if (*iter != '0')
      return false;
  }
  AppendData(value);
  return true;
}

bool ValidationTestInputParser::ParseDistance(const DataType& type,
                                              const std::string& value_string) {
  if (pending_distance_items_.find(value_string) !=
      pending_distance_items_.end())
    return false;

  PendingDistanceItem item = {data_->size(), type.data_size};
  data_->resize(data_->size() + type.data_size);
  pending_distance_items_[value_string] = item;

  return true;
}

bool ValidationTestInputParser::ParseAnchor(const DataType& type,
                                            const std::string& value_string) {
  if (anchors_.find(value_string) != anchors_.end())
    return false;
  anchors_.insert(value_string);

  std::map<std::string, PendingDistanceItem>::iterator iter =
      pending_distance_items_.find(value_string);
  if (iter == pending_distance_items_.end())
    return false;

  PendingDistanceItem dist_item = iter->second;
  pending_distance_items_.erase(iter);

  size_t distance = data_->size() - dist_item.pos;
  switch (dist_item.data_size) {
    case 4:
      return ConvertAndFillData<uint32_t>(dist_item.pos, distance);
    case 8:
      return ConvertAndFillData<uint64_t>(dist_item.pos, distance);
    default:
      assert(false);
      return false;
  }
}

bool ValidationTestInputParser::ParseHandles(const DataType& type,
                                             const std::string& value_string) {
  // It should be the first item.
  if (!data_->empty())
    return false;

  unsigned long long int value;
  if (!ConvertToUnsignedInteger(value_string, &value))
    return false;

  if (value > std::numeric_limits<size_t>::max())
    return false;

  *num_handles_ = static_cast<size_t>(value);
  return true;
}

bool ValidationTestInputParser::StartsWith(const Range& range,
                                           const char* prefix,
                                           size_t prefix_length) {
  if (static_cast<size_t>(range.second - range.first) < prefix_length)
    return false;

  return memcmp(range.first, prefix, prefix_length) == 0;
}

bool ValidationTestInputParser::ConvertToUnsignedInteger(
    const std::string& value_string,
    unsigned long long int* value) {
  const char* format = nullptr;
  if (value_string.find_first_of("xX") != std::string::npos)
    format = "%llx";
  else
    format = "%llu";
  return sscanf(value_string.c_str(), format, value) == 1;
}

}  // namespace

bool ParseValidationTestInput(const std::string& input,
                              std::vector<uint8_t>* data,
                              size_t* num_handles,
                              std::string* error_message) {
  ValidationTestInputParser parser(input, data, num_handles, error_message);
  return parser.Run();
}

}  // namespace test
}  // namespace mojo