-
Notifications
You must be signed in to change notification settings - Fork 505
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
crates/samples/components/json_validator_client/Cargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[package] | ||
name = "sample_component_json_validator_client" | ||
version = "0.0.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[build-dependencies] | ||
cc = "1.0" | ||
|
||
[dependencies.windows-targets] | ||
path = "../../../../crates/libs/targets" | ||
|
||
# TODO: this causes a warning about lack of linkage target. The point is to ensure that this binary dependency is built first but | ||
# Cargo doesn't respect cdylib targets. https://github.com/rust-lang/cargo/issues/7825 | ||
[dependencies.sample_component_json_validator] | ||
path = "../json_validator" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
fn main() { | ||
if !cfg!(target_env = "msvc") { | ||
return; | ||
} | ||
|
||
println!("cargo:rerun-if-changed=src/client.cpp"); | ||
println!("cargo:rustc-link-lib=windows.0.52.0"); | ||
|
||
cc::Build::new() | ||
.cpp(true) | ||
.std("c++20") | ||
.file("src/client.cpp") | ||
.compile("client"); | ||
} |
44 changes: 44 additions & 0 deletions
44
crates/samples/components/json_validator_client/src/client.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <stdint.h> | ||
#include <assert.h> | ||
#include <windows.h> | ||
#include <string_view> | ||
|
||
typedef HRESULT (__stdcall *CreateJsonValidator)(char const* schema, size_t schema_len, uintptr_t* handle); | ||
|
||
typedef HRESULT (__stdcall *ValidateJson)(uintptr_t handle, char const* value, size_t value_len, char** sanitized_value, size_t* sanitized_value_len); | ||
|
||
typedef void (__stdcall *CloseJsonValidator)(uintptr_t handle); | ||
|
||
extern "C" { | ||
void __stdcall client() { | ||
auto library = LoadLibraryExW(L"sample_component_json_validator.dll", 0, LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); | ||
assert(library != 0); | ||
|
||
auto create = reinterpret_cast<CreateJsonValidator>(GetProcAddress(library, "CreateJsonValidator")); | ||
assert(create); | ||
|
||
auto validate = reinterpret_cast<ValidateJson>(GetProcAddress(library, "ValidateJson")); | ||
assert(validate); | ||
|
||
auto close = reinterpret_cast<CloseJsonValidator>(GetProcAddress(library, "CloseJsonValidator")); | ||
assert(close); | ||
|
||
std::string_view schema = "{\"maxLength\": 5}"; | ||
std::string_view json = "\"Hello\" "; // trailing space will be removed from sanitized result | ||
std::string_view json_invalid = "\"Hello world\""; // this json is too long | ||
|
||
uintptr_t validator = 0; | ||
assert(S_OK == create(schema.data(), schema.size(), &validator)); | ||
|
||
char* sanitized_value = nullptr; | ||
size_t sanitized_value_len = 0; | ||
assert(S_OK == validate(validator, json.data(), json.size(), &sanitized_value, &sanitized_value_len)); | ||
std::string_view sanitized(sanitized_value, sanitized_value_len); | ||
assert(sanitized == "\"Hello\""); | ||
CoTaskMemFree(sanitized_value); | ||
|
||
assert(E_INVALIDARG == validate(validator, json_invalid.data(), json_invalid.size(), &sanitized_value, &sanitized_value_len)); | ||
|
||
close(validator); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
crates/samples/components/json_validator_client/src/lib.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#![cfg(target_env = "msvc")] | ||
|
||
#[test] | ||
fn test() { | ||
extern "system" { | ||
fn client(); | ||
} | ||
unsafe { | ||
client(); | ||
} | ||
} |