Embedded SDK
Embedded SDK
Loading...
Searching...
No Matches
swap_utils.c
Go to the documentation of this file.
1/*******************************************************************************
2 * (c) 2023 Ledger
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 ********************************************************************************/
16#ifdef HAVE_SWAP
17
18#include <stddef.h>
19#include <string.h>
20#include <stdbool.h>
21#include <stdint.h>
22
23volatile bool G_called_from_swap;
24volatile bool G_swap_response_ready;
25// Save the BSS address where we will write the return value when finished
26// Don't use in application, SDK only
28
29bool swap_str_to_u64(const uint8_t *src, size_t length, uint64_t *result)
30{
31 if (length > sizeof(uint64_t)) {
32 return false;
33 }
34 uint64_t value = 0;
35 for (size_t i = 0; i < length; i++) {
36 value <<= 8;
37 value |= src[i];
38 }
39 *result = value;
40 return true;
41}
42
43bool swap_parse_config(const uint8_t *config,
44 uint8_t config_len,
45 char *ticker,
46 uint8_t ticker_buf_len,
47 uint8_t *decimals)
48{
49 uint8_t ticker_len, offset = 0;
50 if (config_len == 0 || config == NULL) {
51 return false;
52 }
53 ticker_len = config[offset++];
54 if (ticker_len == 0 || ticker_len >= ticker_buf_len - 1 || config_len - offset < ticker_len) {
55 return false;
56 }
57 memcpy(ticker, config + offset, ticker_len);
58 offset += ticker_len;
59 ticker[ticker_len] = '\0';
60
61 if (config_len - offset < 1) {
62 return false;
63 }
64 *decimals = config[offset];
65 return true;
66}
67
68#endif // HAVE_SWAP
volatile bool G_called_from_swap
bool swap_parse_config(const uint8_t *config, uint8_t config_len, char *ticker, uint8_t ticker_buf_len, uint8_t *decimals)
volatile bool G_swap_response_ready
volatile uint8_t * G_swap_signing_return_value_address
bool swap_str_to_u64(const uint8_t *src, size_t length, uint64_t *result)
unsigned char uint8_t
Definition usbd_conf.h:53