/src/pldm_shims/common/utils.hpp
Line | Count | Source |
1 | | // Copyright 2026 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | // |
15 | | //////////////////////////////////////////////////////////////////////////////// |
16 | | |
17 | | #pragma once |
18 | | |
19 | | #include <string> |
20 | | #include <algorithm> |
21 | | #include <cctype> |
22 | | |
23 | | // Forward declaration of variable_field from libpldm |
24 | | struct variable_field; |
25 | | |
26 | | namespace pldm |
27 | | { |
28 | | namespace utils |
29 | | { |
30 | | |
31 | | inline std::string toString(const struct variable_field& var) |
32 | 0 | { |
33 | | // We will cast the struct pointers. In libpldm, variable_field is: |
34 | | // struct variable_field { const uint8_t* ptr; size_t length; } |
35 | | // We can access them via reinterpret_cast if needed, or since we include |
36 | | // libpldm headers in the fuzzer, the full definition will be available. |
37 | | |
38 | | // To be safe and compile without knowing the exact layout of variable_field here, |
39 | | // we can implement this in a .cpp file or just assume the definition is available |
40 | | // because this header is always included after libpldm headers. |
41 | | // In package_parser.cpp: |
42 | | // #include <libpldm/firmware_update.h> (defines variable_field) |
43 | | // #include <phosphor-logging/lg2.hpp> |
44 | | // So the definition is indeed available! |
45 | | |
46 | 0 | const uint8_t* ptr = reinterpret_cast<const uint8_t*>(var.ptr); |
47 | 0 | size_t length = var.length; |
48 | |
|
49 | 0 | if (ptr == nullptr || !length) |
50 | 0 | { |
51 | 0 | return ""; |
52 | 0 | } |
53 | | |
54 | 0 | std::string str(reinterpret_cast<const char*>(ptr), length); |
55 | 0 | std::replace_if( |
56 | 0 | str.begin(), str.end(), [](const char& c) { return !std::isprint(c); }, ' '); |
57 | 0 | return str; |
58 | 0 | } |
59 | | |
60 | | } // namespace utils |
61 | | } // namespace pldm |