OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/public/interfaces/bindings/tests/validation_parser/validation_par
ser.h" | |
6 | |
7 #include <stdlib.h> | |
8 | |
9 #include "mojo/public/cpp/bindings/tests/validation_test_input_parser.h" | |
10 | |
11 // C interface for the validation test parser. | |
12 // | |
13 // This routine malloc()s return error and space for data which | |
14 // must be freed by the caller. Returns a null pointer in *data | |
15 // and a message in *(return value) on failure. Returns a valid | |
16 // pointer and size in *data and *data_len on success, and a | |
17 // null pointer in *(return value). | |
18 extern "C" char* ParseValidationTest(const char* input, // Input | |
19 size_t* num_handles, // Output | |
20 uint8_t** data, // Output | |
21 size_t* data_len) // Output | |
22 { | |
23 // C++ interface | |
24 std::string cpp_input(input); | |
25 std::vector<uint8_t> cpp_data; | |
26 std::string error_message; | |
27 // Call the parser | |
28 if (!mojo::test::ParseValidationTestInput(cpp_input, &cpp_data, num_handles, | |
29 &error_message)) { | |
30 // Allocate buffer to return error string | |
31 // Add 1 to allocation for null terminator | |
32 int len = error_message.size() + 1; | |
33 char* ret_err = (char*)malloc(len); | |
34 strncpy(ret_err, error_message.c_str(), len); | |
35 // Set data to null and size to 0 since we failed. | |
36 *data = nullptr; | |
37 *data_len = 0; | |
38 return ret_err; | |
39 } | |
40 int cpp_data_size = cpp_data.size(); | |
41 if (cpp_data_size != 0) { | |
42 // Allocate a buffer for the returned data. | |
43 *data = (uint8_t*)malloc(cpp_data_size); | |
44 *data_len = cpp_data_size; | |
45 // Copy the parsed output into the buffer. | |
46 memcpy(*data, cpp_data.data(), cpp_data_size); | |
47 } else { | |
48 // If we have no data, set the pointer to null and size to 0. | |
49 *data = nullptr; | |
50 *data_len = 0; | |
51 } | |
52 return nullptr; | |
53 } | |
OLD | NEW |