Karl Zhang | 3de5ab1 | 2021-05-31 11:45:48 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (c) 2019-2020, Arm Limited. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef STRING_OPS_HPP |
| 9 | #define STRING_OPS_HPP |
| 10 | |
| 11 | #include <cstddef> |
| 12 | #include <string> |
| 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | // Replace first occurrence of find_str within orig of replace_str: |
| 17 | size_t find_replace_1st (const string &find_str, const string &replace_str, |
| 18 | string &orig); |
| 19 | |
| 20 | // Replace all occurrences of find_str in "this" string, with replace_str: |
| 21 | size_t find_replace_all (const string &find_str, const string &replace_str, |
| 22 | string &orig); |
| 23 | |
| 24 | /* In both of the above string-replacement functions, the return value is start |
| 25 | offset to the (last) occurrence of "find_str" within "orig." */ |
| 26 | |
| 27 | /* formalize() turns an abbreviated, "human" name for a PSA setting into its |
| 28 | presumed official name. Examples: |
| 29 | "export" with prefix "PSA_KEY_USAGE_" becomes "PSA_KEY_USAGE_EXPORT". |
| 30 | "generic_error" with prefix "PSA_ERROR_" becomes "PSA_ERROR_GENERIC_ERROR". |
| 31 | "PSA_ERROR_INVALID_ARGUMENT" stays unchanged as "PSA_ERROR_INVALID_ARGUMENT". |
| 32 | */ |
| 33 | string formalize (string input, string prefix); |
| 34 | |
| 35 | /* string_or_hex() takes a string (e.g., describing a key's "material" -- data), |
| 36 | and: |
| 37 | * If it "appears to be" alphanumeric, then returns it unaltered. |
| 38 | * Otherwise, if it "appears to be binary data, it returns a new string of |
| 39 | it as hex digits. |
| 40 | * clump_size is how many bytes to clump together between spaces */ |
| 41 | string string_or_hex (string input, int clump_size); |
| 42 | |
| 43 | /* binary_from_hex() takes a string of hex characters, and returns a string |
| 44 | containing those raw byte values (i.e., not human-readable, and not really |
| 45 | a string in the usual sense of the word). In the future, it would be good |
| 46 | to address binary data better. */ |
| 47 | string binary_from_hex (string input); |
| 48 | |
| 49 | #endif // #ifndef STRING_OPS_HPP |