Embedded SDK
Embedded SDK
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 
23 volatile bool G_called_from_swap;
24 volatile bool G_swap_response_ready;
25 
26 bool swap_str_to_u64(const uint8_t *src, size_t length, uint64_t *result)
27 {
28  if (length > sizeof(uint64_t)) {
29  return false;
30  }
31  uint64_t value = 0;
32  for (size_t i = 0; i < length; i++) {
33  value <<= 8;
34  value |= src[i];
35  }
36  *result = value;
37  return true;
38 }
39 
40 bool swap_parse_config(const uint8_t *config,
41  uint8_t config_len,
42  char *ticker,
43  uint8_t ticker_buf_len,
44  uint8_t *decimals)
45 {
46  uint8_t ticker_len, offset = 0;
47  if (config_len == 0 || config == NULL) {
48  return false;
49  }
50  ticker_len = config[offset++];
51  if (ticker_len == 0 || ticker_len >= ticker_buf_len - 1 || config_len - offset < ticker_len) {
52  return false;
53  }
54  memcpy(ticker, config + offset, ticker_len);
55  offset += ticker_len;
56  ticker[ticker_len] = '\0';
57 
58  if (config_len - offset < 1) {
59  return false;
60  }
61  *decimals = config[offset];
62  return true;
63 }
64 
65 #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
bool swap_str_to_u64(const uint8_t *src, size_t length, uint64_t *result)
unsigned char uint8_t
Definition: usbd_conf.h:53