Skip to content

Commit

Permalink
update clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
may01 committed Jan 29, 2025
1 parent 88cf860 commit a231c89
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 36 deletions.
25 changes: 12 additions & 13 deletions src/common/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <string.h>
#include "parse.h"

#define MAX_AMOUNT_STR_LEN 21 // 19 for u64 + 1 for '\0' +1 for '.'
#define MAX_AMOUNT_STR_LEN 21 // 19 for u64 + 1 for '\0' +1 for '.'

bool adjustDecimals(const char *src,
uint32_t srcLength,
Expand All @@ -36,11 +36,11 @@ bool adjustDecimals(const char *src,
}
target[offset++] = '0';
target[offset++] = '.';
for (uint32_t i = 0; i < delta; i++) {
for (uint32_t i = 0; i < delta; i++) {
target[offset++] = '0';
}
startOffset = offset;
for (uint32_t i = 0; i < srcLength; i++) {
for (uint32_t i = 0; i < srcLength; i++) {
target[offset++] = src[i];
}
target[offset] = '\0';
Expand All @@ -50,14 +50,14 @@ bool adjustDecimals(const char *src,
if (targetLength < srcLength + 1 + 1) {
return false;
}
while (offset < delta) {
while (offset < delta) {
target[offset++] = src[sourceOffset++];
}
if (decimals != 0) {
target[offset++] = '.';
}
startOffset = offset;
while (sourceOffset < srcLength) {
while (sourceOffset < srcLength) {
target[offset++] = src[sourceOffset++];
}
target[offset] = '\0';
Expand All @@ -71,7 +71,7 @@ bool adjustDecimals(const char *src,
lastZeroOffset = 0;
}
}
if (lastZeroOffset != 0) {
if (lastZeroOffset != 0) {
target[lastZeroOffset] = '\0';
if (target[lastZeroOffset - 1] == '.') {
target[lastZeroOffset - 1] = '\0';
Expand All @@ -80,24 +80,23 @@ bool adjustDecimals(const char *src,
return true;
}
unsigned short print_amount(uint64_t amount, char *out, uint32_t outlen, uint8_t decimals) {

if (amount == 0) {
if (outlen < 2) {
return 0;
}
out[0] = '0';
out[1] = '\0';
out[1] = '\0';

return strlen(out);
}

char tmp[MAX_AMOUNT_STR_LEN];
char tmp[MAX_AMOUNT_STR_LEN];
char tmp2[MAX_AMOUNT_STR_LEN];
uint32_t numDigits = 0, i;
uint64_t base = 1;

while (base <= amount) {
base *= 10;
while (base <= amount) {
base *= 10;
numDigits++;
}
if (numDigits > sizeof(tmp) - 2) {
Expand All @@ -110,10 +109,10 @@ unsigned short print_amount(uint64_t amount, char *out, uint32_t outlen, uint8_t
}
tmp[i] = '\0';
out[0] = '\0';
if ( adjustDecimals(tmp, i, tmp2, MAX_AMOUNT_STR_LEN, decimals) ) {
if (adjustDecimals(tmp, i, tmp2, MAX_AMOUNT_STR_LEN, decimals)) {
if (strlen(tmp2) < outlen - 1) {
strlcpy(out, tmp2, outlen);
}
}
}
return strlen(out);
}
1 change: 0 additions & 1 deletion src/common/parse.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "os.h"


#ifndef PARSE_H
#define PARSE_H

Expand Down
16 changes: 6 additions & 10 deletions src/handler/get_public_key.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@
#include "../helper/send_response.h"

int get_public_key(buffer_t *cdata,
uint8_t *output_bip32_path_len,
uint32_t *output_bip32_path,
pubkey_ctx_t *output_pubkey_ctx) {
uint8_t *output_bip32_path_len,
uint32_t *output_bip32_path,
pubkey_ctx_t *output_pubkey_ctx) {
if (!buffer_read_u8(cdata, output_bip32_path_len)) {
return io_send_sw(SW_WRONG_DATA_LENGTH);
}
Expand Down Expand Up @@ -101,7 +101,7 @@ int get_public_key(buffer_t *cdata,
// Generate corresponding public key
cx_ecfp_public_key_t public_key = {0};
error = crypto_init_public_key(&private_key, &public_key, output_pubkey_ctx->raw_public_key);
// Wipe the private key from memory to protect against memory attacks
// Wipe the private key from memory to protect against memory attacks
explicit_bzero(&private_key, sizeof(private_key));

if (error != CX_OK) {
Expand All @@ -116,10 +116,8 @@ int handler_get_public_key(buffer_t *cdata, bool display) {
explicit_bzero(&G_context, sizeof(G_context));
G_context.req_type = CONFIRM_ADDRESS;

int err = get_public_key(cdata,
&G_context.bip32_path_len,
G_context.bip32_path,
&G_context.pk_info);
int err =
get_public_key(cdata, &G_context.bip32_path_len, G_context.bip32_path, &G_context.pk_info);

if (err) {
G_context.req_type = REQUEST_UNDEFINED;
Expand All @@ -135,5 +133,3 @@ int handler_get_public_key(buffer_t *cdata, bool display) {
G_context.req_type = REQUEST_UNDEFINED; // all the work is done, reset the context
return helper_send_response_pubkey();
}


10 changes: 5 additions & 5 deletions src/handler/get_public_key.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ int handler_get_public_key(buffer_t *cdata, bool display);
/**
* Helper function for GET_PUBLIC_KEY and CHECK_ADDRESS command. If successfully parse
* BIP32 path, derive public key/chain. The public key is stored in output_public_key,
* and the BIP32 path and its length are stored in output_bip32_path and
* and the BIP32 path and its length are stored in output_bip32_path and
* output_bip32_path_len respectively.
*
* @param[in] cdata
Expand All @@ -39,11 +39,11 @@ int handler_get_public_key(buffer_t *cdata, bool display);
* Buffer to store the BIP32 path.
* @param[out] output_public_key
* Buffer to store the public key.
*
*
* @return zero if success, error code otherwise.
*
*/
int get_public_key(buffer_t *cdata,
uint8_t *output_bip32_path_len,
uint32_t output_bip32_path[MAX_BIP32_PATH],
pubkey_ctx_t *output_public_key);
uint8_t *output_bip32_path_len,
uint32_t output_bip32_path[MAX_BIP32_PATH],
pubkey_ctx_t *output_public_key);
10 changes: 7 additions & 3 deletions src/swap/handle_check_address.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ void swap_handle_check_address(check_address_parameters_t *params) {
return;
}
PRINTF("address_parameters_length: %d\n", params->address_parameters_length);
PRINTF("address_parameters: %.*H\n", params->address_parameters_length, params->address_parameters);
PRINTF("address_parameters: %.*H\n",
params->address_parameters_length,
params->address_parameters);

if (params->address_to_check == NULL) {
PRINTF("address_to_check is empty\n");
Expand All @@ -54,7 +56,8 @@ void swap_handle_check_address(check_address_parameters_t *params) {

if (strlen(params->address_to_check) != ADDRESS_STRING_LENGTH) {
PRINTF("address_to_check length should be %d, not %d\n",
ADDRESS_STRING_LENGTH, strlen(params->address_to_check));
ADDRESS_STRING_LENGTH,
strlen(params->address_to_check));
return;
}

Expand All @@ -77,7 +80,8 @@ void swap_handle_check_address(check_address_parameters_t *params) {
return;
}
char prefixed_address[ADDRESS_STRING_LENGTH + 1];
if (0 > format_prefixed_hex(address, sizeof(address), prefixed_address, sizeof(prefixed_address))) {
if (0 >
format_prefixed_hex(address, sizeof(address), prefixed_address, sizeof(prefixed_address))) {
return;
}

Expand Down
7 changes: 3 additions & 4 deletions src/swap/handle_get_printable_amount.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@

/* Set empty printable_amount on error, printable amount otherwise */
void swap_handle_get_printable_amount(get_printable_amount_parameters_t* params) {

uint64_t amount;
uint64_t amount;
uint8_t decimals;
char ticker[MAX_TICKER_LEN] = {0};

Expand All @@ -18,8 +17,8 @@ void swap_handle_get_printable_amount(get_printable_amount_parameters_t* params)
// If the amount is a fee, its value is nominated in APT
// If there is no coin_configuration, consider that we are doing a APT swap
if (params->is_fee || params->coin_configuration == NULL) {
memcpy(ticker, "APT", sizeof("APT"));
decimals = APT_DECIMAL_PRECISION;
memcpy(ticker, "APT", sizeof("APT"));
decimals = APT_DECIMAL_PRECISION;
} else {
if (!swap_parse_config(params->coin_configuration,
params->coin_configuration_length,
Expand Down

0 comments on commit a231c89

Please sign in to comment.