diff --git a/.clang-format b/.clang-format new file mode 100644 index 000000000..31bd69e6b --- /dev/null +++ b/.clang-format @@ -0,0 +1,39 @@ +--- +Language: Cpp +BasedOnStyle: Chromium +IndentWidth: 4 +ColumnLimit: 78 +UseTab: Never +IndentCaseLabels: false +InsertBraces: false +PointerAlignment: Right + +BreakBeforeBraces: Linux +BreakBeforeBinaryOperators: true + +BinPackArguments: true +BinPackParameters: true + +AlignAfterOpenBracket: Align + +AlignConsecutiveMacros: + Enabled: true + AcrossComments: true + AcrossEmptyLines: false + +AlignConsecutiveDeclarations: + Enabled: true + AcrossComments: false + AcrossEmptyLines: false + +AlignConsecutiveAssignments: + Enabled: true + AcrossComments: false + AcrossEmptyLines: false + +AlignArrayOfStructures: Left +SortIncludes: Never +SpaceAfterCStyleCast: false +AllowShortEnumsOnASingleLine: false +AllowShortCaseLabelsOnASingleLine: true +... diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml new file mode 100644 index 000000000..a935b6d57 --- /dev/null +++ b/.github/workflows/pre-commit.yml @@ -0,0 +1,13 @@ +name: pre-commit + +on: [pull_request] + +jobs: + pre-commit: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - uses: actions/setup-python@v4 + with: + python-version: '3.10' + - uses: pre-commit/action@v3.0.0 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 000000000..ace7b2d4f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,18 @@ +# To install hooks, run: +# pre-commit install --hook-type pre-commit +# pre-commit install --hook-type commit-msg + +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.3.0 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-yaml + - id: check-added-large-files + +# - repo: https://github.com/pre-commit/mirrors-clang-format +# rev: v16.0.6 +# hooks: +# - id: clang-format +# types_or: [c] diff --git a/app/src/apdu.c b/app/src/apdu.c index 77bc9c74f..008e7c758 100644 --- a/app/src/apdu.c +++ b/app/src/apdu.c @@ -27,14 +27,11 @@ #include "apdu.h" #include "globals.h" -const uint8_t version[4] = { - 0 /* wallet */, - MAJOR_VERSION, - MINOR_VERSION, - PATCH_VERSION -}; - -void handle_unimplemented(__attribute__((unused))command_t *cmd) { +const uint8_t version[4] + = {0 /* wallet */, MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION}; + +void handle_unimplemented(__attribute__((unused)) command_t *cmd) +{ TZ_PREAMBLE(("cmd=0x%p", cmd)); PRINTF("[ERROR] Unimplemented instruction 0x%02x\n", cmd->ins); @@ -42,7 +39,8 @@ void handle_unimplemented(__attribute__((unused))command_t *cmd) { TZ_POSTAMBLE; } -void handle_apdu_version(__attribute__((unused))command_t *cmd) { +void handle_apdu_version(__attribute__((unused)) command_t *cmd) +{ TZ_PREAMBLE(("cmd=0x%p", cmd)); TZ_ASSERT(EXC_UNEXPECTED_STATE, global.step == ST_IDLE); @@ -50,7 +48,8 @@ void handle_apdu_version(__attribute__((unused))command_t *cmd) { TZ_POSTAMBLE; } -void handle_apdu_git(__attribute__((unused))command_t *cmd) { +void handle_apdu_git(__attribute__((unused)) command_t *cmd) +{ static const char commit[] = COMMIT; TZ_PREAMBLE(("cmd=0x%p", cmd)); diff --git a/app/src/apdu.h b/app/src/apdu.h index 06edcd01b..15e5e7521 100644 --- a/app/src/apdu.h +++ b/app/src/apdu.h @@ -47,7 +47,7 @@ #define INS_SIGN_WITH_HASH 0x0F // Last valid instruction code -#define INS_MAX 0x0F +#define INS_MAX 0x0F /* * All of the handlers must be defined together below. We @@ -82,7 +82,7 @@ * */ -typedef void (tz_handler)(command_t *); +typedef void(tz_handler)(command_t *); typedef tz_handler *tz_handler_t; tz_handler handle_unimplemented; diff --git a/app/src/apdu_pubkey.c b/app/src/apdu_pubkey.c index 5f03cde25..bad568294 100644 --- a/app/src/apdu_pubkey.c +++ b/app/src/apdu_pubkey.c @@ -37,16 +37,18 @@ static void prompt_address(void); static void format_pkh(char *); static void stream_cb(tz_ui_cb_type_t); -static void provide_pubkey(void) { - buffer_t bufs[2] = {0}; - uint8_t byte; +static void provide_pubkey(void) +{ + buffer_t bufs[2] = {0}; + uint8_t byte; cx_ecfp_public_key_t pubkey; TZ_PREAMBLE(("void")); // Application could be PIN-locked, and pubkey->W_len would then be 0, // so throwing an error rather than returning an empty key TZ_ASSERT(EXC_SECURITY, os_global_pin_is_validated() == BOLOS_UX_OK); - TZ_CHECK(generate_public_key(&pubkey, global.path_with_curve.derivation_type, + TZ_CHECK(generate_public_key(&pubkey, + global.path_with_curve.derivation_type, &global.path_with_curve.bip32_path)); byte = pubkey.W_len; @@ -60,12 +62,14 @@ static void provide_pubkey(void) { TZ_POSTAMBLE; } -static void format_pkh(char *buffer) { +static void format_pkh(char *buffer) +{ cx_ecfp_public_key_t pubkey = {0}; - uint8_t hash[21]; + uint8_t hash[21]; TZ_PREAMBLE(("buffer=%p", buffer)); - TZ_CHECK(generate_public_key(&pubkey, global.path_with_curve.derivation_type, + TZ_CHECK(generate_public_key(&pubkey, + global.path_with_curve.derivation_type, &global.path_with_curve.bip32_path)); // clang-format off TZ_CHECK(public_key_hash(hash+1, 20, NULL, @@ -83,7 +87,8 @@ static void format_pkh(char *buffer) { TZ_POSTAMBLE; } -static void stream_cb(tz_ui_cb_type_t type) { +static void stream_cb(tz_ui_cb_type_t type) +{ TZ_PREAMBLE(("type=%u", type)); // clang-format off @@ -98,7 +103,8 @@ static void stream_cb(tz_ui_cb_type_t type) { } #ifdef HAVE_BAGL -static void prompt_address(void) { +static void prompt_address(void) +{ char buf[TZ_UI_STREAM_CONTENTS_SIZE + 1]; TZ_PREAMBLE(("void")); @@ -117,25 +123,29 @@ static void prompt_address(void) { #include "nbgl_use_case.h" -static void cancel_callback(void) { +static void cancel_callback(void) +{ stream_cb(TZ_UI_STREAM_CB_REJECT); nbgl_useCaseStatus("Address rejected", false, ui_home_init); } -static void approve_callback(void) { +static void approve_callback(void) +{ stream_cb(TZ_UI_STREAM_CB_ACCEPT); nbgl_useCaseStatus("ADDRESS\nVERIFIED", true, ui_home_init); } -static void confirmation_callback(bool confirm) { +static void confirmation_callback(bool confirm) +{ if (confirm) { - approve_callback(); + approve_callback(); } else { - cancel_callback(); + cancel_callback(); } } -static void verify_address(void) { +static void verify_address(void) +{ char buf[TZ_UI_STREAM_CONTENTS_SIZE + 1]; TZ_PREAMBLE(("void")); @@ -144,16 +154,19 @@ static void verify_address(void) { TZ_POSTAMBLE; } -static void prompt_address(void) { +static void prompt_address(void) +{ TZ_PREAMBLE(("void")); global.step = ST_PROMPT; - nbgl_useCaseReviewStart(&C_tezos, "Verify Tezos\naddress", NULL, "Cancel", verify_address, cancel_callback); + nbgl_useCaseReviewStart(&C_tezos, "Verify Tezos\naddress", NULL, "Cancel", + verify_address, cancel_callback); TZ_POSTAMBLE; } #endif -void handle_apdu_get_public_key(command_t *cmd) { +void handle_apdu_get_public_key(command_t *cmd) +{ bool prompt = cmd->ins == INS_PROMPT_PUBLIC_KEY; TZ_PREAMBLE(("cmd=%p", cmd)); @@ -162,17 +175,18 @@ void handle_apdu_get_public_key(command_t *cmd) { // do not expose pks without prompt through U2F (permissionless legacy // comm in browser) - TZ_ASSERT(EXC_HID_REQUIRED, prompt || G_io_apdu_media != IO_APDU_MEDIA_U2F); + TZ_ASSERT(EXC_HID_REQUIRED, + prompt || G_io_apdu_media != IO_APDU_MEDIA_U2F); global.path_with_curve.derivation_type = cmd->p2; TZ_ASSERT(EXC_WRONG_PARAM, check_derivation_type(global.path_with_curve.derivation_type)); - TZ_CHECK(read_bip32_path(&global.path_with_curve.bip32_path, - cmd->data, cmd->lc)); + TZ_CHECK(read_bip32_path(&global.path_with_curve.bip32_path, cmd->data, + cmd->lc)); if (prompt) - prompt_address(); + prompt_address(); else - provide_pubkey(); + provide_pubkey(); TZ_POSTAMBLE; } diff --git a/app/src/apdu_sign.c b/app/src/apdu_sign.c index 7004609ec..9ce10f3f8 100644 --- a/app/src/apdu_sign.c +++ b/app/src/apdu_sign.c @@ -46,8 +46,8 @@ typedef struct { command_t cmd; - bool is_last; - bool is_first; + bool is_last; + bool is_first; } packet_t; /* Prototypes */ @@ -66,7 +66,6 @@ static void handle_data_apdu(packet_t *); static void handle_data_apdu_clear(packet_t *); static void handle_data_apdu_blind(packet_t *); - /* Macros */ #define P1_FIRST 0x00 @@ -76,11 +75,12 @@ static void handle_data_apdu_blind(packet_t *); #define TZ_UI_STREAM_CB_CANCEL 0xf0 -#define APDU_SIGN_ASSERT(_cond) TZ_ASSERT(EXC_UNEXPECTED_SIGN_STATE, (_cond)) -#define APDU_SIGN_ASSERT_STEP(x) APDU_SIGN_ASSERT(global.apdu.sign.step == (x)) - +#define APDU_SIGN_ASSERT(_cond) TZ_ASSERT(EXC_UNEXPECTED_SIGN_STATE, (_cond)) +#define APDU_SIGN_ASSERT_STEP(x) \ + APDU_SIGN_ASSERT(global.apdu.sign.step == (x)) -static void init_packet(packet_t *pkt, command_t *cmd) { +static void init_packet(packet_t *pkt, command_t *cmd) +{ pkt->cmd.cla = cmd->cla; pkt->cmd.ins = cmd->ins; pkt->cmd.p1 = cmd->p1; @@ -91,10 +91,10 @@ static void init_packet(packet_t *pkt, command_t *cmd) { pkt->is_first = (cmd->p1 & ~P1_LAST_MARKER) == 0; } - -static void sign_packet(void) { +static void sign_packet(void) +{ buffer_t bufs[2] = {0}; - uint8_t sig[MAX_SIGNATURE_SIZE]; + uint8_t sig[MAX_SIGNATURE_SIZE]; TZ_PREAMBLE(("void")); APDU_SIGN_ASSERT_STEP(SIGN_ST_WAIT_USER_INPUT); @@ -105,8 +105,8 @@ static void sign_packet(void) { bufs[1].ptr = sig; bufs[1].size = sizeof(sig); TZ_CHECK(sign(global.path_with_curve.derivation_type, - &global.path_with_curve.bip32_path, - bufs[0].ptr, bufs[0].size, sig, &bufs[1].size)); + &global.path_with_curve.bip32_path, bufs[0].ptr, + bufs[0].size, sig, &bufs[1].size)); /* If we aren't returning the hash, zero its buffer... */ if (!global.apdu.sign.return_hash) @@ -118,7 +118,8 @@ static void sign_packet(void) { TZ_POSTAMBLE; } -static void send_reject(void) { +static void send_reject(void) +{ TZ_PREAMBLE(("void")); APDU_SIGN_ASSERT_STEP(SIGN_ST_WAIT_USER_INPUT); @@ -127,11 +128,12 @@ static void send_reject(void) { TZ_POSTAMBLE; } -static void send_continue(void) { +static void send_continue(void) +{ TZ_PREAMBLE(("void")); - APDU_SIGN_ASSERT(global.apdu.sign.step == SIGN_ST_WAIT_USER_INPUT || - global.apdu.sign.step == SIGN_ST_WAIT_DATA); + APDU_SIGN_ASSERT(global.apdu.sign.step == SIGN_ST_WAIT_USER_INPUT + || global.apdu.sign.step == SIGN_ST_WAIT_DATA); APDU_SIGN_ASSERT(!global.apdu.sign.received_last_msg); io_send_sw(SW_OK); @@ -140,9 +142,10 @@ static void send_continue(void) { TZ_POSTAMBLE; } -static void refill() { - size_t wrote = 0; - tz_parser_state *st = &global.apdu.sign.u.clear.parser_state; +static void refill() +{ + size_t wrote = 0; + tz_parser_state *st = &global.apdu.sign.u.clear.parser_state; TZ_PREAMBLE(("void")); while (!TZ_IS_BLOCKED(tz_operation_parser_step(st))) @@ -155,25 +158,21 @@ static void refill() { wrote = tz_ui_stream_push(TZ_UI_STREAM_CB_NOCB, st->field_name, global.line_buf, TZ_UI_ICON_NONE); - tz_parser_flush_up_to(st, global.line_buf, - TZ_UI_STREAM_CONTENTS_SIZE, wrote); - break; - case TZ_BLO_FEED_ME: - TZ_CHECK(send_continue()); + tz_parser_flush_up_to(st, global.line_buf, TZ_UI_STREAM_CONTENTS_SIZE, + wrote); break; + case TZ_BLO_FEED_ME: TZ_CHECK(send_continue()); break; case TZ_BLO_DONE: TZ_ASSERT(EXC_UNEXPECTED_STATE, global.apdu.sign.received_last_msg && st->regs.ilen == 0); if (st->regs.oofs != 0) - goto last_screen; + goto last_screen; global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT; tz_ui_stream_push_accept_reject(); tz_ui_stream_close(); break; case TZ_ERR_INVALID_STATE: - tz_ui_stream_push(TZ_UI_STREAM_CB_CANCEL, - "Unknown error", - "", + tz_ui_stream_push(TZ_UI_STREAM_CB_CANCEL, "Unknown error", "", TZ_UI_ICON_CROSS); tz_ui_stream_close(); break; @@ -182,45 +181,39 @@ static void refill() { case TZ_ERR_UNSUPPORTED: case TZ_ERR_TOO_LARGE: case TZ_ERR_TOO_DEEP: - tz_ui_stream_push(TZ_UI_STREAM_CB_CANCEL, - "Parsing error", - tz_parser_result_name(st->errno), - TZ_UI_ICON_CROSS); + tz_ui_stream_push(TZ_UI_STREAM_CB_CANCEL, "Parsing error", + tz_parser_result_name(st->errno), TZ_UI_ICON_CROSS); tz_ui_stream_close(); break; - default: - TZ_FAIL(EXC_UNEXPECTED_STATE); + default: TZ_FAIL(EXC_UNEXPECTED_STATE); } TZ_POSTAMBLE; } -static void send_cancel(void) { +static void send_cancel(void) +{ tz_parser_state *st = &global.apdu.sign.u.clear.parser_state; TZ_PREAMBLE(("void")); - global.step = ST_IDLE; + global.step = ST_IDLE; global.apdu.sign.step = SIGN_ST_IDLE; switch (st->errno) { - case TZ_ERR_INVALID_STATE: - TZ_FAIL(EXC_UNEXPECTED_STATE); - break; + case TZ_ERR_INVALID_STATE: TZ_FAIL(EXC_UNEXPECTED_STATE); break; case TZ_ERR_INVALID_TAG: case TZ_ERR_INVALID_OP: case TZ_ERR_UNSUPPORTED: case TZ_ERR_TOO_LARGE: - case TZ_ERR_TOO_DEEP: - TZ_FAIL(EXC_PARSE_ERROR); - break; - default: - TZ_FAIL(EXC_UNEXPECTED_STATE); + case TZ_ERR_TOO_DEEP: TZ_FAIL(EXC_PARSE_ERROR); break; + default: TZ_FAIL(EXC_UNEXPECTED_STATE); } TZ_POSTAMBLE; } -static void stream_cb(tz_ui_cb_type_t type) { +static void stream_cb(tz_ui_cb_type_t type) +{ TZ_PREAMBLE(("type=%u", type)); // clang-format off @@ -236,14 +229,15 @@ static void stream_cb(tz_ui_cb_type_t type) { TZ_POSTAMBLE; } -static void handle_first_apdu(packet_t *pkt) { +static void handle_first_apdu(packet_t *pkt) +{ TZ_PREAMBLE(("pkt=%p", pkt)); TZ_ASSERT_NOTNULL(pkt); APDU_SIGN_ASSERT_STEP(SIGN_ST_IDLE); - TZ_CHECK(read_bip32_path(&global.path_with_curve.bip32_path, pkt->cmd.data, - pkt->cmd.lc)); + TZ_CHECK(read_bip32_path(&global.path_with_curve.bip32_path, + pkt->cmd.data, pkt->cmd.lc)); global.path_with_curve.derivation_type = pkt->cmd.p2; TZ_CHECK(check_derivation_type(global.path_with_curve.derivation_type)); TZ_CHECK(cx_blake2b_init_no_throw(&global.apdu.hash.state, @@ -264,7 +258,8 @@ static void handle_first_apdu(packet_t *pkt) { TZ_POSTAMBLE; } -static void handle_first_apdu_clear(__attribute__((unused)) packet_t *pkt) { +static void handle_first_apdu_clear(__attribute__((unused)) packet_t *pkt) +{ tz_parser_state *st = &global.apdu.sign.u.clear.parser_state; tz_operation_parser_init(st, TZ_UNKNOWN_SIZE, false); @@ -272,8 +267,8 @@ static void handle_first_apdu_clear(__attribute__((unused)) packet_t *pkt) { tz_parser_flush(st, global.line_buf, TZ_UI_STREAM_CONTENTS_SIZE); } -static void handle_first_apdu_blind(__attribute__((unused)) packet_t *pkt) { - +static void handle_first_apdu_blind(__attribute__((unused)) packet_t *pkt) +{ /* * We set the tag to zero here which indicates that it is unset. * The first data packet will set it to the first byte. @@ -285,18 +280,18 @@ static void handle_first_apdu_blind(__attribute__((unused)) packet_t *pkt) { global.apdu.sign.u.blind.tag = 0; } -static void handle_data_apdu(packet_t *pkt) { +static void handle_data_apdu(packet_t *pkt) +{ TZ_PREAMBLE(("pkt=%p", pkt)); APDU_SIGN_ASSERT_STEP(SIGN_ST_WAIT_DATA); TZ_ASSERT_NOTNULL(pkt); - global.apdu.sign.packet_index++; // XXX drop or check + global.apdu.sign.packet_index++; // XXX drop or check CX_CHECK(cx_hash_no_throw((cx_hash_t *)&global.apdu.hash.state, - pkt->is_last ? CX_LAST : 0, - pkt->cmd.data, pkt->cmd.lc, - global.apdu.hash.final_hash, + pkt->is_last ? CX_LAST : 0, pkt->cmd.data, + pkt->cmd.lc, global.apdu.hash.final_hash, sizeof(global.apdu.hash.final_hash))); if (pkt->is_last) @@ -313,7 +308,8 @@ static void handle_data_apdu(packet_t *pkt) { TZ_POSTAMBLE; } -static void handle_data_apdu_clear(packet_t *pkt) { +static void handle_data_apdu_clear(packet_t *pkt) +{ tz_parser_state *st = &global.apdu.sign.u.clear.parser_state; TZ_PREAMBLE(("pkt=0x%p", pkt)); @@ -326,7 +322,8 @@ static void handle_data_apdu_clear(packet_t *pkt) { tz_parser_refill(st, pkt->cmd.data, pkt->cmd.lc); if (pkt->is_last) - tz_operation_parser_set_size(st, global.apdu.sign.u.clear.total_length); + tz_operation_parser_set_size(st, + global.apdu.sign.u.clear.total_length); TZ_CHECK(refill()); tz_ui_stream(); @@ -335,9 +332,10 @@ static void handle_data_apdu_clear(packet_t *pkt) { #ifdef HAVE_NBGL static nbgl_layoutTagValueList_t useCaseTagValueList; -static nbgl_pageInfoLongPress_t infoLongPress; +static nbgl_pageInfoLongPress_t infoLongPress; -void reject_blindsign_cb(void) { +void reject_blindsign_cb(void) +{ FUNC_ENTER(("void")); stream_cb(TZ_UI_STREAM_CB_REJECT); @@ -346,7 +344,8 @@ void reject_blindsign_cb(void) { FUNC_LEAVE(); } -void accept_blindsign_cb(void) { +void accept_blindsign_cb(void) +{ FUNC_ENTER(("void")); stream_cb(TZ_UI_STREAM_CB_ACCEPT); @@ -355,7 +354,8 @@ void accept_blindsign_cb(void) { FUNC_LEAVE(); } -static void reviewChoice(bool confirm) { +static void reviewChoice(bool confirm) +{ FUNC_ENTER(("confirm=%d", confirm)); if (confirm) { @@ -367,38 +367,40 @@ static void reviewChoice(bool confirm) { FUNC_LEAVE(); } -static const char* transaction_type; +static const char *transaction_type; static char hash[TZ_BASE58_BUFFER_SIZE(sizeof(global.apdu.hash.final_hash))]; -static nbgl_layoutTagValue_t pair; -static nbgl_layoutTagValue_t* getTagValuePair(uint8_t pairIndex) { +static nbgl_layoutTagValue_t pair; +static nbgl_layoutTagValue_t *getTagValuePair(uint8_t pairIndex) +{ switch (pairIndex) { case 0: - pair.item = "Type"; + pair.item = "Type"; pair.value = transaction_type; break; case 1: - pair.item = "Hash"; + pair.item = "Hash"; pair.value = hash; break; - default: - return NULL; + default: return NULL; } return &pair; } -void continue_blindsign_cb(void) { +void continue_blindsign_cb(void) +{ FUNC_ENTER(("void")); - useCaseTagValueList.pairs = NULL; - useCaseTagValueList.callback = getTagValuePair; - useCaseTagValueList.startIndex = 0; - useCaseTagValueList.nbPairs = 2; + useCaseTagValueList.pairs = NULL; + useCaseTagValueList.callback = getTagValuePair; + useCaseTagValueList.startIndex = 0; + useCaseTagValueList.nbPairs = 2; useCaseTagValueList.smallCaseForValue = false; - useCaseTagValueList.wrapping = false; - infoLongPress.icon = &C_tezos; - infoLongPress.text = "Approve"; - infoLongPress.longPressText = "Sign"; - nbgl_useCaseStaticReview(&useCaseTagValueList, &infoLongPress, "Reject", reviewChoice); + useCaseTagValueList.wrapping = false; + infoLongPress.icon = &C_tezos; + infoLongPress.text = "Approve"; + infoLongPress.longPressText = "Sign"; + nbgl_useCaseStaticReview(&useCaseTagValueList, &infoLongPress, "Reject", + reviewChoice); FUNC_LEAVE(); } @@ -406,7 +408,8 @@ void continue_blindsign_cb(void) { #endif #define FINAL_HASH global.apdu.hash.final_hash -static void handle_data_apdu_blind(packet_t *pkt) { +static void handle_data_apdu_blind(packet_t *pkt) +{ TZ_PREAMBLE(("pkt=0x%p", pkt)); TZ_ASSERT_NOTNULL(pkt); @@ -417,11 +420,11 @@ static void handle_data_apdu_blind(packet_t *pkt) { goto end; } - char obuf[TZ_BASE58_BUFFER_SIZE(sizeof(FINAL_HASH))]; + char obuf[TZ_BASE58_BUFFER_SIZE(sizeof(FINAL_HASH))]; const char *type = "unknown type"; global.apdu.sign.received_last_msg = true; - global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT; + global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT; tz_format_base58(FINAL_HASH, sizeof(FINAL_HASH), obuf); @@ -448,19 +451,15 @@ static void handle_data_apdu_blind(packet_t *pkt) { #ifdef HAVE_NBGL char request[80]; - snprintf(request, sizeof(request), "Review request to blind\nsign %s", type); + snprintf(request, sizeof(request), "Review request to blind\nsign %s", + type); global.apdu.sign.step = SIGN_ST_WAIT_USER_INPUT; transaction_type = type; strcpy(hash, obuf); - nbgl_useCaseReviewStart(&C_tezos, - request, - NULL, - "Reject request", - continue_blindsign_cb, - reject_blindsign_cb - ); + nbgl_useCaseReviewStart(&C_tezos, request, NULL, "Reject request", + continue_blindsign_cb, reject_blindsign_cb); #endif tz_ui_stream_close(); @@ -471,8 +470,9 @@ static void handle_data_apdu_blind(packet_t *pkt) { } #undef FINAL_HASH -void handle_apdu_sign(command_t *cmd) { - bool return_hash = cmd->ins == INS_SIGN_WITH_HASH; +void handle_apdu_sign(command_t *cmd) +{ + bool return_hash = cmd->ins == INS_SIGN_WITH_HASH; packet_t pkt; TZ_PREAMBLE(("cmd=0x%p", cmd)); diff --git a/app/src/apdu_sign.h b/app/src/apdu_sign.h index 7dfa83e2a..c509c6bea 100644 --- a/app/src/apdu_sign.h +++ b/app/src/apdu_sign.h @@ -30,7 +30,7 @@ typedef struct { cx_blake2b_t state; - uint8_t final_hash[SIGN_HASH_SIZE]; + uint8_t final_hash[SIGN_HASH_SIZE]; } apdu_hash_state_t; typedef enum { @@ -43,12 +43,12 @@ typedef struct { uint8_t packet_index; sign_step_t step; - bool return_hash; - bool received_last_msg; + bool return_hash; + bool received_last_msg; union { struct { - size_t total_length; + size_t total_length; tz_parser_state parser_state; } clear; struct { diff --git a/app/src/app_main.c b/app/src/app_main.c index 065b72f3d..7bc2dd2f0 100644 --- a/app/src/app_main.c +++ b/app/src/app_main.c @@ -30,11 +30,13 @@ #define CLA 0x80 -void app_exit(void) { +void app_exit(void) +{ os_sched_exit(-1); } -static void dispatch(command_t *cmd) { +static void dispatch(command_t *cmd) +{ tz_handler_t f; TZ_PREAMBLE(("cmd=0x%p {cla=0x02x ins=%u ...}", cmd, cmd->cla, cmd->ins)); @@ -46,8 +48,7 @@ static void dispatch(command_t *cmd) { PRINTF("[ERROR] received instruction whilst on Quit screen\n"); TZ_FAIL(EXC_UNEXPECTED_STATE); break; - default: - break; + default: break; } // clang-format off @@ -78,10 +79,10 @@ static void dispatch(command_t *cmd) { TZ_POSTAMBLE; } - -void app_main() { +void app_main() +{ command_t cmd; - int rx; + int rx; FUNC_ENTER(("void")); app_stack_canary = 0xDEADBEEF; @@ -99,9 +100,9 @@ void app_main() { PRINTF("[PTR] G_io_apdu_buffer: 0x%p\n", G_io_apdu_buffer); PRINTF("[SIZEOF] G_io_apdu_buffer: %u\n", sizeof(G_io_apdu_buffer)); PRINTF("[PTR] G_io_seproxyhal_spi_buffer: 0x%p\n", - G_io_seproxyhal_spi_buffer); + G_io_seproxyhal_spi_buffer); PRINTF("[SIZEOF] G_io_seproxyhal_spi_buffer: %u\n", - sizeof(G_io_seproxyhal_spi_buffer)); + sizeof(G_io_seproxyhal_spi_buffer)); PRINTF("[PTR] G_ux: 0x%p\n", &G_ux); PRINTF("[SIZEOF] G_ux: %u\n", sizeof(G_ux)); PRINTF("[PTR] G_ux_os: 0x%p\n", &G_ux_os); diff --git a/app/src/compat.h b/app/src/compat.h index 9a753e19d..041244866 100644 --- a/app/src/compat.h +++ b/app/src/compat.h @@ -17,7 +17,7 @@ static inline int io_send_response_buffers(const buffer_t *rdatalist, uint8_t buf[128]; size_t tx = 0; - for (size_t i=0; i < count; i++) { + for (size_t i = 0; i < count; i++) { memcpy(&buf[tx], rdatalist[i].ptr, rdatalist[i].size); tx += rdatalist[i].size; } diff --git a/app/src/exception.h b/app/src/exception.h index 9d2da3531..e97cd4e51 100644 --- a/app/src/exception.h +++ b/app/src/exception.h @@ -22,7 +22,7 @@ #include -#define SW_OK 0x9000 +#define SW_OK 0x9000 // Standard APDU error codes: // https://www.eftlab.co.uk/index.php/site-map/knowledge-base/118-apdu-response-list @@ -40,10 +40,10 @@ #define EXC_CLASS 0x6E00 #define EXC_MEMORY_ERROR 0x9200 -#define EXC_UNEXPECTED_STATE 0x9001 -#define EXC_UNEXPECTED_SIGN_STATE 0x9002 -#define EXC_UNKNOWN_CX_ERR 0x9003 -#define EXC_UNKNOWN 0x90FF +#define EXC_UNEXPECTED_STATE 0x9001 +#define EXC_UNEXPECTED_SIGN_STATE 0x9002 +#define EXC_UNKNOWN_CX_ERR 0x9003 +#define EXC_UNKNOWN 0x90FF /* * In the handlers and the routines that they call, we define a set of @@ -77,48 +77,51 @@ * with this framework, reply with io_send_sw() and return TZ_DONE. */ -#define TZ_PREAMBLE(_args) \ - uint16_t _sw_ret_code = 0x0000; \ - cx_err_t error = CX_OK; \ - if (0) \ - goto bail; \ - if (0) \ - goto end; \ - FUNC_ENTER(_args) - -#define TZ_POSTAMBLE \ - end: \ - if (error != CX_OK) { \ - _sw_ret_code = EXC_UNKNOWN_CX_ERR; \ - PRINTF("CX_CHECK failed with 0x%08x", error); \ - } \ - bail: \ - if (_sw_ret_code) { \ - global.step = ST_ERROR; \ - io_send_sw(_sw_ret_code); \ - } \ - FUNC_LEAVE(); - -#define TZ_FAIL(_sw_code) do { \ - _sw_ret_code = _sw_code; \ - goto bail; \ - } while (0) - -#define TZ_ASSERT(_err, _cond) do { \ - if (!(_cond)) { \ - PRINTF("Assertion (\"%s\") on %s:%u failed with %s\n", \ - #_cond, __FILE__, __LINE__, #_err); \ - TZ_FAIL(_err); \ - } \ - } while (0) - -#define TZ_CHECK(_call) do { \ - (_call); \ - if (global.step == ST_ERROR) { \ - PRINTF("TZ_CHECK(\"%s\") on %s:%u\n", #_call, \ - __FILE__, __LINE__); \ - goto bail; \ - } \ - } while (0) +#define TZ_PREAMBLE(_args) \ + uint16_t _sw_ret_code = 0x0000; \ + cx_err_t error = CX_OK; \ + if (0) \ + goto bail; \ + if (0) \ + goto end; \ + FUNC_ENTER(_args) + +#define TZ_POSTAMBLE \ + end: \ + if (error != CX_OK) { \ + _sw_ret_code = EXC_UNKNOWN_CX_ERR; \ + PRINTF("CX_CHECK failed with 0x%08x", error); \ + } \ + bail: \ + if (_sw_ret_code) { \ + global.step = ST_ERROR; \ + io_send_sw(_sw_ret_code); \ + } \ + FUNC_LEAVE(); + +#define TZ_FAIL(_sw_code) \ + do { \ + _sw_ret_code = _sw_code; \ + goto bail; \ + } while (0) + +#define TZ_ASSERT(_err, _cond) \ + do { \ + if (!(_cond)) { \ + PRINTF("Assertion (\"%s\") on %s:%u failed with %s\n", #_cond, \ + __FILE__, __LINE__, #_err); \ + TZ_FAIL(_err); \ + } \ + } while (0) + +#define TZ_CHECK(_call) \ + do { \ + (_call); \ + if (global.step == ST_ERROR) { \ + PRINTF("TZ_CHECK(\"%s\") on %s:%u\n", #_call, __FILE__, \ + __LINE__); \ + goto bail; \ + } \ + } while (0) #define TZ_ASSERT_NOTNULL(_x) TZ_ASSERT(EXC_MEMORY_ERROR, (_x) != NULL) diff --git a/app/src/globals.c b/app/src/globals.c index e5d5ba2d9..b5ac722b0 100644 --- a/app/src/globals.c +++ b/app/src/globals.c @@ -28,20 +28,18 @@ globals_t global; const settings_t N_settings_real; -void init_globals(void) { +void init_globals(void) +{ memset(&global, 0, sizeof(global)); memset(G_io_seproxyhal_spi_buffer, 0, sizeof(G_io_seproxyhal_spi_buffer)); memset(&G_ux, 0, sizeof(G_ux)); memset(&G_ux_params, 0, sizeof(G_ux_params)); } -void toggle_blindsigning(void) { +void toggle_blindsigning(void) +{ settings_t tmp; - memcpy(&tmp, - (void*) &N_settings, - sizeof(tmp)); + memcpy(&tmp, (void *)&N_settings, sizeof(tmp)); tmp.blindsigning = !N_settings.blindsigning; - nvm_write((void*) &N_settings, - (void*) &tmp, - sizeof(N_settings)); + nvm_write((void *)&N_settings, (void *)&tmp, sizeof(N_settings)); } diff --git a/app/src/globals.h b/app/src/globals.h index 49bf72fa8..633d121fe 100644 --- a/app/src/globals.h +++ b/app/src/globals.h @@ -26,9 +26,9 @@ #define TZ_SCREEN_WITDH_FULL_REGULAR_11PX 19 #define TZ_SCREEN_WITDH_BETWEEN_ICONS_BOLD_11PX 16 #ifdef TARGET_NANOS -# define TZ_SCREEN_LINES_11PX 2 +#define TZ_SCREEN_LINES_11PX 2 #else -# define TZ_SCREEN_LINES_11PX 5 +#define TZ_SCREEN_LINES_11PX 5 #endif #include "apdu.h" @@ -70,34 +70,35 @@ typedef enum { typedef struct { /* State */ - main_step_t step; - tz_ui_stream_t stream; + main_step_t step; + tz_ui_stream_t stream; bip32_path_with_curve_t path_with_curve; struct { apdu_hash_state_t hash; apdu_sign_state_t sign; } apdu; char line_buf[TZ_UI_STREAM_CONTENTS_SIZE + 1]; -# ifdef HAVE_BAGL +#ifdef HAVE_BAGL struct { bagl_element_t bagls[5 + TZ_SCREEN_LINES_11PX]; - char lines[TZ_SCREEN_LINES_11PX][TZ_SCREEN_WITDH_FULL_REGULAR_11PX + 1]; + char lines[TZ_SCREEN_LINES_11PX] + [TZ_SCREEN_WITDH_FULL_REGULAR_11PX + 1]; } ux; -# endif -# ifdef HAVE_NBGL +#endif +#ifdef HAVE_NBGL screen_t home_screen; -# endif +#endif } globals_t; /* Settings */ typedef struct { - bool blindsigning; + bool blindsigning; } settings_t; extern globals_t global; extern const settings_t N_settings_real; -#define N_settings (*(volatile settings_t *) PIC(&N_settings_real)) +#define N_settings (*(volatile settings_t *)PIC(&N_settings_real)) extern unsigned int app_stack_canary; // From SDK diff --git a/app/src/keys.c b/app/src/keys.c index 4b68f20ee..e5ced9c71 100644 --- a/app/src/keys.c +++ b/app/src/keys.c @@ -33,36 +33,34 @@ #include "keys.h" #include "globals.h" -static cx_curve_t derivation_type_to_cx_curve(derivation_type_t - derivation_type) { +static cx_curve_t derivation_type_to_cx_curve( + derivation_type_t derivation_type) +{ switch (derivation_type) { case DERIVATION_TYPE_ED25519: - case DERIVATION_TYPE_BIP32_ED25519: - return CX_CURVE_Ed25519; - case DERIVATION_TYPE_SECP256K1: - return CX_CURVE_SECP256K1; - case DERIVATION_TYPE_SECP256R1: - return CX_CURVE_SECP256R1; - default: - return CX_CURVE_NONE; + case DERIVATION_TYPE_BIP32_ED25519: return CX_CURVE_Ed25519; + case DERIVATION_TYPE_SECP256K1: return CX_CURVE_SECP256K1; + case DERIVATION_TYPE_SECP256R1: return CX_CURVE_SECP256R1; + default: return CX_CURVE_NONE; } } -void read_bip32_path(bip32_path_t *out, const uint8_t *in, - size_t in_size) { +void read_bip32_path(bip32_path_t *out, const uint8_t *in, size_t in_size) +{ buffer_t cdata = {in, in_size, 0}; TZ_PREAMBLE(("out=%p, in=%p, in_size=%u", out, in, in_size)); TZ_ASSERT(EXC_WRONG_LENGTH_FOR_INS, - buffer_read_u8(&cdata, &out->length) && - buffer_read_bip32_path(&cdata, (uint32_t *) &out->components, - out->length)); + buffer_read_u8(&cdata, &out->length) + && buffer_read_bip32_path( + &cdata, (uint32_t *)&out->components, out->length)); TZ_POSTAMBLE; } void generate_public_key(cx_ecfp_public_key_t *public_key, - derivation_type_t derivation_type, - const bip32_path_t *bip32_path) { + derivation_type_t derivation_type, + const bip32_path_t *bip32_path) +{ TZ_PREAMBLE(("public_key=%p, derivation_type=%d, bip32_path=%p", public_key, derivation_type, bip32_path)); @@ -71,21 +69,15 @@ void generate_public_key(cx_ecfp_public_key_t *public_key, int derivation_mode = HDW_NORMAL; if (derivation_type == DERIVATION_TYPE_ED25519) - derivation_mode = HDW_ED25519_SLIP10; + derivation_mode = HDW_ED25519_SLIP10; - CX_CHECK(bip32_derive_with_seed_get_pubkey_256(derivation_mode, - public_key->curve, - bip32_path->components, - bip32_path->length, - public_key->W, - NULL, - CX_SHA512, - NULL, 0)); + CX_CHECK(bip32_derive_with_seed_get_pubkey_256( + derivation_mode, public_key->curve, bip32_path->components, + bip32_path->length, public_key->W, NULL, CX_SHA512, NULL, 0)); if (public_key->curve == CX_CURVE_Ed25519) { - CX_CHECK(cx_edwards_compress_point_no_throw(CX_CURVE_Ed25519, - public_key->W, - public_key->W_len)); + CX_CHECK(cx_edwards_compress_point_no_throw( + CX_CURVE_Ed25519, public_key->W, public_key->W_len)); public_key->W_len = 33; } @@ -95,13 +87,15 @@ void generate_public_key(cx_ecfp_public_key_t *public_key, #define HASH_SIZE 20 void public_key_hash(uint8_t *hash_out, size_t hash_out_size, - cx_ecfp_public_key_t *compressed_out, - derivation_type_t derivation_type, - const cx_ecfp_public_key_t *public_key) { - TZ_PREAMBLE(("hash_out=%p, hash_out_size=%u, compressed_out=%p, " - "derivation_type=%d, public_key=%p", - hash_out, hash_out_size, compressed_out, - derivation_type, public_key)); + cx_ecfp_public_key_t *compressed_out, + derivation_type_t derivation_type, + const cx_ecfp_public_key_t *public_key) +{ + TZ_PREAMBLE( + ("hash_out=%p, hash_out_size=%u, compressed_out=%p, " + "derivation_type=%d, public_key=%p", + hash_out, hash_out_size, compressed_out, derivation_type, + public_key)); TZ_ASSERT_NOTNULL(hash_out); TZ_ASSERT_NOTNULL(public_key); @@ -117,21 +111,16 @@ void public_key_hash(uint8_t *hash_out, size_t hash_out_size, case DERIVATION_TYPE_SECP256K1: case DERIVATION_TYPE_SECP256R1: memcpy(compressed.W, public_key->W, public_key->W_len); - compressed.W[0] = 0x02 + (public_key->W[64] & 0x01); + compressed.W[0] = 0x02 + (public_key->W[64] & 0x01); compressed.W_len = 33; break; - default: - TZ_FAIL(EXC_WRONG_PARAM); + default: TZ_FAIL(EXC_WRONG_PARAM); } cx_blake2b_t hash_state; CX_CHECK(cx_blake2b_init_no_throw(&hash_state, HASH_SIZE * 8)); - CX_CHECK(cx_hash_no_throw((cx_hash_t *) &hash_state, - CX_LAST, - compressed.W, - compressed.W_len, - hash_out, - HASH_SIZE)); + CX_CHECK(cx_hash_no_throw((cx_hash_t *)&hash_state, CX_LAST, compressed.W, + compressed.W_len, hash_out, HASH_SIZE)); if (compressed_out != NULL) { memmove(compressed_out, &compressed, sizeof(*compressed_out)); } @@ -139,16 +128,16 @@ void public_key_hash(uint8_t *hash_out, size_t hash_out_size, TZ_POSTAMBLE; } -void sign(derivation_type_t derivation_type, - const bip32_path_t *path, - const uint8_t *hash, size_t hashlen, - uint8_t *sig, size_t *siglen) { - unsigned derivation_mode; - uint32_t info; +void sign(derivation_type_t derivation_type, const bip32_path_t *path, + const uint8_t *hash, size_t hashlen, uint8_t *sig, size_t *siglen) +{ + unsigned derivation_mode; + uint32_t info; cx_curve_t curve = derivation_type_to_cx_curve(derivation_type); - TZ_PREAMBLE(("sig=%p, siglen=%u, derivation_type=%d, " - "path=%p, hash=%p, hashlen=%u", - sig, *siglen, derivation_type, path, hash, hashlen)); + TZ_PREAMBLE( + ("sig=%p, siglen=%u, derivation_type=%d, " + "path=%p, hash=%p, hashlen=%u", + sig, *siglen, derivation_type, path, hash, hashlen)); TZ_ASSERT_NOTNULL(path); TZ_ASSERT_NOTNULL(hash); TZ_ASSERT_NOTNULL(sig); @@ -160,30 +149,19 @@ void sign(derivation_type_t derivation_type, derivation_mode = HDW_NORMAL; if (derivation_type == DERIVATION_TYPE_ED25519) derivation_mode = HDW_ED25519_SLIP10; - CX_CHECK(bip32_derive_with_seed_eddsa_sign_hash_256(derivation_mode, - curve, - path->components, - path->length, - CX_SHA512, - hash, hashlen, - sig, siglen, - NULL, 0)); + CX_CHECK(bip32_derive_with_seed_eddsa_sign_hash_256( + derivation_mode, curve, path->components, path->length, CX_SHA512, + hash, hashlen, sig, siglen, NULL, 0)); break; case DERIVATION_TYPE_SECP256K1: case DERIVATION_TYPE_SECP256R1: - CX_CHECK(bip32_derive_ecdsa_sign_hash_256(curve, - path->components, - path->length, - CX_RND_RFC6979 | CX_LAST, - CX_SHA256, - hash, hashlen, - sig, siglen, &info)); + CX_CHECK(bip32_derive_ecdsa_sign_hash_256( + curve, path->components, path->length, CX_RND_RFC6979 | CX_LAST, + CX_SHA256, hash, hashlen, sig, siglen, &info)); if (info & CX_ECCINFO_PARITY_ODD) sig[0] |= 0x01; break; - default: - TZ_FAIL(EXC_WRONG_VALUES); - break; + default: TZ_FAIL(EXC_WRONG_VALUES); break; } TZ_POSTAMBLE; } diff --git a/app/src/keys.h b/app/src/keys.h index b77dc6aee..7205a4a2f 100644 --- a/app/src/keys.h +++ b/app/src/keys.h @@ -31,26 +31,26 @@ #include "exception.h" -#define MAX_BIP32_LEN 10 +#define MAX_BIP32_LEN 10 #define SIGN_HASH_SIZE 32 /* The values in the following enum are from the on-the-wire protocol */ typedef enum { - DERIVATION_TYPE_ED25519 = 0, - DERIVATION_TYPE_SECP256K1 = 1, - DERIVATION_TYPE_SECP256R1 = 2, + DERIVATION_TYPE_ED25519 = 0, + DERIVATION_TYPE_SECP256K1 = 1, + DERIVATION_TYPE_SECP256R1 = 2, DERIVATION_TYPE_BIP32_ED25519 = 3, - DERIVATION_TYPE_MAX = 4 + DERIVATION_TYPE_MAX = 4 } derivation_type_t; typedef struct { - uint8_t length; + uint8_t length; uint32_t components[MAX_BIP32_LEN]; } bip32_path_t; typedef struct { - bip32_path_t bip32_path; + bip32_path_t bip32_path; derivation_type_t derivation_type; } bip32_path_with_curve_t; @@ -62,6 +62,7 @@ void public_key_hash(uint8_t *, size_t, cx_ecfp_public_key_t *, void sign(derivation_type_t, const bip32_path_t *, const uint8_t *, size_t, uint8_t *, size_t *); -static inline bool check_derivation_type(derivation_type_t code) { +static inline bool check_derivation_type(derivation_type_t code) +{ return (code >= DERIVATION_TYPE_ED25519 && code < DERIVATION_TYPE_MAX); } diff --git a/app/src/parser/compat.h b/app/src/parser/compat.h index 0d96a9692..9fe85695c 100644 --- a/app/src/parser/compat.h +++ b/app/src/parser/compat.h @@ -20,28 +20,30 @@ // for BOLOS or running in the Tezos client/testing. #if __has_include("os.h") #include "os.h" -# if __has_include("cx.h") -# define ACTUALLY_ON_LEDGER -# endif +#if __has_include("cx.h") +#define ACTUALLY_ON_LEDGER +#endif #endif // On BOLOS, use the SDK. // Elsewhere, simulate the BOLOS API calls we need. #ifdef ACTUALLY_ON_LEDGER -# include "os.h" -# include "os_io_seproxyhal.h" -# include "cx.h" -# if CX_APILEVEL < 8 -# error "Only compiles with API level 8 or higher; requires newer firmware" -# endif +#include "os.h" +#include "os_io_seproxyhal.h" +#include "cx.h" +#if CX_APILEVEL < 8 +#error "Only compiles with API level 8 or higher; requires newer firmware" +#endif #else -# include -# define PIC(x) ((void*) x) -# ifdef TEZOS_DEBUG -# define PRINTF printf -# else -# define PRINTF(...) do {} while (0) -# endif +#include +#define PIC(x) ((void *)x) +#ifdef TEZOS_DEBUG +#define PRINTF printf +#else +#define PRINTF(...) \ + do { \ + } while (0) +#endif #endif #include @@ -50,11 +52,11 @@ // Type-safe versions of true/false #undef true -#define true ((bool) 1) +#define true ((bool)1) #undef false -#define false ((bool) 0) +#define false ((bool)0) #ifdef MIN #undef MIN #endif -#define MIN(a,b) (((a)<(b))?(a):(b)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) diff --git a/app/src/parser/formatting.c b/app/src/parser/formatting.c index 5e5e04f80..166245a39 100644 --- a/app/src/parser/formatting.c +++ b/app/src/parser/formatting.c @@ -22,167 +22,170 @@ // // NEVER REORDER elements of this array as the index is used as a // selector by `michelson_op_name`. -const char* const tz_michelson_op_names_ordered[TZ_LAST_MICHELSON_OPCODE+1] = { - "parameter", // 0 - "storage", // 1 - "code", // 2 - "False", // 3 - "Elt", // 4 - "Left", // 5 - "None", // 6 - "Pair", // 7 - "Right", // 8 - "Some", // 9 - "True", // 10 - "Unit", // 11 - "PACK", // 12 - "UNPACK", // 13 - "BLAKE2B", // 14 - "SHA256", // 15 - "SHA512", // 16 - "ABS", // 17 - "ADD", // 18 - "AMOUNT", // 19 - "AND", // 20 - "BALANCE", // 21 - "CAR", // 22 - "CDR", // 23 - "CHECK_SIGNATURE", // 24 - "COMPARE", // 25 - "CONCAT", // 26 - "CONS", // 27 - "CREATE_ACCOUNT", // 28 - "CREATE_CONTRACT", // 29 - "IMPLICIT_ACCOUNT", // 30 - "DIP", // 31 - "DROP", // 32 - "DUP", // 33 - "EDIV", // 34 - "EMPTY_MAP", // 35 - "EMPTY_SET", // 36 - "EQ", // 37 - "EXEC", // 38 - "FAILWITH", // 39 - "GE", // 40 - "GET", // 41 - "GT", // 42 - "HASH_KEY", // 43 - "IF", // 44 - "IF_CONS", // 45 - "IF_LEFT", // 46 - "IF_NONE", // 47 - "INT", // 48 - "LAMBDA", // 49 - "LE", // 50 - "LEFT", // 51 - "LOOP", // 52 - "LSL", // 53 - "LSR", // 54 - "LT", // 55 - "MAP", // 56 - "MEM", // 57 - "MUL", // 58 - "NEG", // 59 - "NEQ", // 60 - "NIL", // 61 - "NONE", // 62 - "NOT", // 63 - "NOW", // 64 - "OR", // 65 - "PAIR", // 66 - "PUSH", // 67 - "RIGHT", // 68 - "SIZE", // 69 - "SOME", // 70 - "SOURCE", // 71 - "SENDER", // 72 - "SELF", // 73 - "STEPS_TO_QUOTA", // 74 [DEPRECATED] - "SUB", // 75 - "SWAP", // 76 - "TRANSFER_TOKENS", // 77 - "SET_DELEGATE", // 78 - "UNIT", // 79 - "UPDATE", // 80 - "XOR", // 81 - "ITER", // 82 - "LOOP_LEFT", // 83 - "ADDRESS", // 84 - "CONTRACT", // 85 - "ISNAT", // 86 - "CAST", // 87 - "RENAME", // 88 - "bool", // 89 - "contract", // 90 - "int", // 91 - "key", // 92 - "key_hash", // 93 - "lambda", // 94 - "list", // 95 - "map", // 96 - "big_map", // 97 - "nat", // 98 - "option", // 99 - "or", // 100 - "pair", // 101 - "set", // 102 - "signature", // 103 - "string", // 104 - "bytes", // 105 - "mutez", // 106 - "timestamp", // 107 - "unit", // 108 - "operation", // 109 - "address", // 110 - "SLICE", // 111 - "DIG", // 112 - "DUG", // 113 - "EMPTY_BIG_MAP", // 114 - "APPLY", // 115 - "chain_id", // 116 - "CHAIN_ID", // 117 - "LEVEL", // 118 - "SELF_ADDRESS", // 119 - "never", // 120 - "NEVER", // 121 - "UNPAIR", // 122 - "VOTING_POWER", // 123 - "TOTAL_VOTING_POWER", // 124 - "KECCAK", // 125 - "SHA3", // 126 - "PAIRING_CHECK", // 127 - "bls12_381_g1", // 128 - "bls12_381_g2", // 129 - "bls12_381_fr", // 130 - "sapling_state", // 131 - "sapling_transaction_deprecated", // 132 [DEPRECATED] - "SAPLING_EMPTY_STATE", // 133 - "SAPLING_VERIFY_UPDATE", // 134 - "ticket", // 135 - "TICKET_DEPRECATED", // 136 - "READ_TICKET", // 137 - "SPLIT_TICKET", // 138 - "JOIN_TICKETS", // 139 - "GET_AND_UPDATE", // 140 - "chest", // 141 - "chest_key", // 142 - "OPEN_CHEST", // 143 - "VIEW", // 144 - "view", // 145 - "constant", // 146 - "SUB_MUTEZ", // 147 - "tx_rollup_l2_address", // 148 - "MIN_BLOCK_TIME", // 149 - "sapling_transaction", // 150 - "EMIT", // 151 - "Lambda_rec", // 152 - "LAMBDA_REC", // 153 - "TICKET" // 154 +const char *const tz_michelson_op_names_ordered[TZ_LAST_MICHELSON_OPCODE + 1] + = { + "parameter", // 0 + "storage", // 1 + "code", // 2 + "False", // 3 + "Elt", // 4 + "Left", // 5 + "None", // 6 + "Pair", // 7 + "Right", // 8 + "Some", // 9 + "True", // 10 + "Unit", // 11 + "PACK", // 12 + "UNPACK", // 13 + "BLAKE2B", // 14 + "SHA256", // 15 + "SHA512", // 16 + "ABS", // 17 + "ADD", // 18 + "AMOUNT", // 19 + "AND", // 20 + "BALANCE", // 21 + "CAR", // 22 + "CDR", // 23 + "CHECK_SIGNATURE", // 24 + "COMPARE", // 25 + "CONCAT", // 26 + "CONS", // 27 + "CREATE_ACCOUNT", // 28 + "CREATE_CONTRACT", // 29 + "IMPLICIT_ACCOUNT", // 30 + "DIP", // 31 + "DROP", // 32 + "DUP", // 33 + "EDIV", // 34 + "EMPTY_MAP", // 35 + "EMPTY_SET", // 36 + "EQ", // 37 + "EXEC", // 38 + "FAILWITH", // 39 + "GE", // 40 + "GET", // 41 + "GT", // 42 + "HASH_KEY", // 43 + "IF", // 44 + "IF_CONS", // 45 + "IF_LEFT", // 46 + "IF_NONE", // 47 + "INT", // 48 + "LAMBDA", // 49 + "LE", // 50 + "LEFT", // 51 + "LOOP", // 52 + "LSL", // 53 + "LSR", // 54 + "LT", // 55 + "MAP", // 56 + "MEM", // 57 + "MUL", // 58 + "NEG", // 59 + "NEQ", // 60 + "NIL", // 61 + "NONE", // 62 + "NOT", // 63 + "NOW", // 64 + "OR", // 65 + "PAIR", // 66 + "PUSH", // 67 + "RIGHT", // 68 + "SIZE", // 69 + "SOME", // 70 + "SOURCE", // 71 + "SENDER", // 72 + "SELF", // 73 + "STEPS_TO_QUOTA", // 74 [DEPRECATED] + "SUB", // 75 + "SWAP", // 76 + "TRANSFER_TOKENS", // 77 + "SET_DELEGATE", // 78 + "UNIT", // 79 + "UPDATE", // 80 + "XOR", // 81 + "ITER", // 82 + "LOOP_LEFT", // 83 + "ADDRESS", // 84 + "CONTRACT", // 85 + "ISNAT", // 86 + "CAST", // 87 + "RENAME", // 88 + "bool", // 89 + "contract", // 90 + "int", // 91 + "key", // 92 + "key_hash", // 93 + "lambda", // 94 + "list", // 95 + "map", // 96 + "big_map", // 97 + "nat", // 98 + "option", // 99 + "or", // 100 + "pair", // 101 + "set", // 102 + "signature", // 103 + "string", // 104 + "bytes", // 105 + "mutez", // 106 + "timestamp", // 107 + "unit", // 108 + "operation", // 109 + "address", // 110 + "SLICE", // 111 + "DIG", // 112 + "DUG", // 113 + "EMPTY_BIG_MAP", // 114 + "APPLY", // 115 + "chain_id", // 116 + "CHAIN_ID", // 117 + "LEVEL", // 118 + "SELF_ADDRESS", // 119 + "never", // 120 + "NEVER", // 121 + "UNPAIR", // 122 + "VOTING_POWER", // 123 + "TOTAL_VOTING_POWER", // 124 + "KECCAK", // 125 + "SHA3", // 126 + "PAIRING_CHECK", // 127 + "bls12_381_g1", // 128 + "bls12_381_g2", // 129 + "bls12_381_fr", // 130 + "sapling_state", // 131 + "sapling_transaction_deprecated", // 132 [DEPRECATED] + "SAPLING_EMPTY_STATE", // 133 + "SAPLING_VERIFY_UPDATE", // 134 + "ticket", // 135 + "TICKET_DEPRECATED", // 136 + "READ_TICKET", // 137 + "SPLIT_TICKET", // 138 + "JOIN_TICKETS", // 139 + "GET_AND_UPDATE", // 140 + "chest", // 141 + "chest_key", // 142 + "OPEN_CHEST", // 143 + "VIEW", // 144 + "view", // 145 + "constant", // 146 + "SUB_MUTEZ", // 147 + "tx_rollup_l2_address", // 148 + "MIN_BLOCK_TIME", // 149 + "sapling_transaction", // 150 + "EMIT", // 151 + "Lambda_rec", // 152 + "LAMBDA_REC", // 153 + "TICKET" // 154 }; -const char* tz_michelson_op_name(uint8_t op_code) { - if (op_code > TZ_LAST_MICHELSON_OPCODE) return NULL; - return PIC(tz_michelson_op_names_ordered[op_code]); +const char *tz_michelson_op_name(uint8_t op_code) +{ + if (op_code > TZ_LAST_MICHELSON_OPCODE) + return NULL; + return PIC(tz_michelson_op_names_ordered[op_code]); } /* @@ -195,41 +198,47 @@ const char* tz_michelson_op_name(uint8_t op_code) { * under the terms of the standard MIT license. */ -static const char tz_b58digits_ordered[] = - "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; +static const char tz_b58digits_ordered[] + = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; -void tz_format_base58(const uint8_t *n, size_t l, char *obuf) { - int carry; +void tz_format_base58(const uint8_t *n, size_t l, char *obuf) +{ + int carry; size_t i, j, high, zcount = 0, obuf_len = TZ_BASE58_BUFFER_SIZE(l); memset(obuf, 0, obuf_len); - while (zcount < l && !n[zcount]) ++zcount; + while (zcount < l && !n[zcount]) + ++zcount; for (i = zcount, high = obuf_len - 1; i < l; ++i, high = j) { carry = n[i]; - for (j = obuf_len - 1; ((int) j >= 0) && ((j > high) || carry); --j) { + for (j = obuf_len - 1; ((int)j >= 0) && ((j > high) || carry); --j) { carry += 256 * obuf[j]; obuf[j] = carry % 58; carry /= 58; } } - if (zcount) memset(obuf, '1', zcount); + if (zcount) + memset(obuf, '1', zcount); - for (j = 0; !obuf[j]; ++j); - for (i = 0; j < obuf_len; ++i, ++j) obuf[i] = - tz_b58digits_ordered[(unsigned)obuf[j]]; + for (j = 0; !obuf[j]; ++j) + ; + for (i = 0; j < obuf_len; ++i, ++j) + obuf[i] = tz_b58digits_ordered[(unsigned)obuf[j]]; obuf[i] = '\0'; } -void tz_format_decimal(const uint8_t *n, size_t l, char *obuf) { - int carry; +void tz_format_decimal(const uint8_t *n, size_t l, char *obuf) +{ + int carry; size_t i, j, high, zcount = 0, obuf_len = TZ_DECIMAL_BUFFER_SIZE(l); memset(obuf, 0, obuf_len); - while (zcount < l && !n[l-zcount-1]) ++zcount; + while (zcount < l && !n[l - zcount - 1]) + ++zcount; if (zcount == l) { obuf[0] = '0'; @@ -238,15 +247,17 @@ void tz_format_decimal(const uint8_t *n, size_t l, char *obuf) { for (i = zcount, high = obuf_len - 1; i < l; ++i, high = j) { carry = n[l - i - 1]; - for (j = obuf_len - 1; ((int) j >= 0) && ((j > high) || carry); --j) { + for (j = obuf_len - 1; ((int)j >= 0) && ((j > high) || carry); --j) { carry += 256 * obuf[j]; obuf[j] = carry % 10; carry /= 10; } } - for (j = 0; !obuf[j]; ++j); - for (i = 0; j < obuf_len; ++i, ++j) obuf[i] = '0' + obuf[j]; + for (j = 0; !obuf[j]; ++j) + ; + for (i = 0; j < obuf_len; ++i, ++j) + obuf[i] = '0' + obuf[j]; obuf[i] = '\0'; } @@ -255,14 +266,19 @@ void tz_format_decimal(const uint8_t *n, size_t l, char *obuf) { // package does not publish their C header file for others to use, so // we are forced to piggy import the external definitions in order to // access them. -struct sha256_ctx { uint64_t sz; uint8_t buf[128]; uint32_t h[8];}; +struct sha256_ctx { + uint64_t sz; + uint8_t buf[128]; + uint32_t h[8]; +}; extern void digestif_sha256_init(struct sha256_ctx *); extern void digestif_sha256_update(struct sha256_ctx *, uint8_t *, uint32_t); extern void digestif_sha256_finalize(struct sha256_ctx *, uint8_t *); static void cx_hash_sha256(uint8_t *data, size_t size, uint8_t *out, - size_t size_out) { + size_t size_out) +{ struct sha256_ctx ctx; - uint8_t res[32]; + uint8_t res[32]; digestif_sha256_init(&ctx); digestif_sha256_update(&ctx, data, size); digestif_sha256_finalize(&ctx, res); @@ -345,9 +361,10 @@ find_prefix(const char *s, const uint8_t **p, size_t *pl, size_t dl) // clang-format on int tz_format_base58check(const char *sprefix, const uint8_t *data, - size_t size, char *obuf) { + size_t size, char *obuf) +{ const uint8_t *prefix = NULL; - size_t prefix_len; + size_t prefix_len; if (find_prefix(sprefix, &prefix, &prefix_len, size)) return 1; @@ -358,16 +375,17 @@ int tz_format_base58check(const char *sprefix, const uint8_t *data, return 1; memcpy(prepared, prefix, prefix_len); - memcpy(prepared+prefix_len, data, size); + memcpy(prepared + prefix_len, data, size); uint8_t tmp[32]; - cx_hash_sha256(prepared, size+prefix_len, tmp, 32); + cx_hash_sha256(prepared, size + prefix_len, tmp, 32); cx_hash_sha256(tmp, 32, tmp, 32); - memcpy(prepared+size+prefix_len, tmp, 4); - tz_format_base58(prepared, prefix_len+size+4, obuf); + memcpy(prepared + size + prefix_len, tmp, 4); + tz_format_base58(prepared, prefix_len + size + 4, obuf); return 0; } -int tz_format_pkh(const uint8_t *data, size_t size, char *obuf) { +int tz_format_pkh(const uint8_t *data, size_t size, char *obuf) +{ const char *prefix; if (size < 1) @@ -382,10 +400,11 @@ int tz_format_pkh(const uint8_t *data, size_t size, char *obuf) { } // clang-format on - return tz_format_base58check(prefix, data+1, size-1, obuf); + return tz_format_base58check(prefix, data + 1, size - 1, obuf); } -int tz_format_pk(const uint8_t *data, size_t size, char *obuf) { +int tz_format_pk(const uint8_t *data, size_t size, char *obuf) +{ const char *prefix; if (size < 1) @@ -400,20 +419,23 @@ int tz_format_pk(const uint8_t *data, size_t size, char *obuf) { } // clang-format on - return tz_format_base58check(prefix, data+1, size-1, obuf); + return tz_format_base58check(prefix, data + 1, size - 1, obuf); } /* Deprecated, use tz_format_base58check("o", ...) instead */ -int tz_format_oph(const uint8_t *data, size_t size, char *obuf) { +int tz_format_oph(const uint8_t *data, size_t size, char *obuf) +{ return tz_format_base58check("o", data, size, obuf); } /* Deprecated, use tz_format_base58check("B", ...) instead */ -int tz_format_bh(const uint8_t *data, size_t size, char *obuf) { - return tz_format_base58check("B", data, size, obuf); +int tz_format_bh(const uint8_t *data, size_t size, char *obuf) +{ + return tz_format_base58check("B", data, size, obuf); } -int tz_format_address(const uint8_t *data, size_t size, char *obuf) { +int tz_format_address(const uint8_t *data, size_t size, char *obuf) +{ const char *prefix; if (size < 1) @@ -430,5 +452,5 @@ int tz_format_address(const uint8_t *data, size_t size, char *obuf) { } // clang-format on - return tz_format_base58check(prefix, data+1, size-2, obuf); + return tz_format_base58check(prefix, data + 1, size - 2, obuf); } diff --git a/app/src/parser/formatting.h b/app/src/parser/formatting.h index 4c6f46cd3..b258b6bcc 100644 --- a/app/src/parser/formatting.h +++ b/app/src/parser/formatting.h @@ -24,168 +24,168 @@ // Tezos protocol upgrade to support new opcodes, the existing opcodes // are guaranteed to stay unchanged, so not updating does not break // security. -const char* tz_michelson_op_name(uint8_t); +const char *tz_michelson_op_name(uint8_t); #define TZ_LAST_MICHELSON_OPCODE 154 // Should be kept in sync with the last protocol update, including // order, currently defined in the `michelson_v1_primitives.ml` file // in the Tezos protocol code. typedef enum { - TZ_MICHELSON_OP_parameter = 0, - TZ_MICHELSON_OP_storage = 1, - TZ_MICHELSON_OP_code = 2, - TZ_MICHELSON_OP_False = 3, - TZ_MICHELSON_OP_Elt = 4, - TZ_MICHELSON_OP_Left = 5, - TZ_MICHELSON_OP_None = 6, - TZ_MICHELSON_OP_Pair = 7, - TZ_MICHELSON_OP_Right = 8, - TZ_MICHELSON_OP_Some = 9, - TZ_MICHELSON_OP_True = 10, - TZ_MICHELSON_OP_Unit = 11, - TZ_MICHELSON_OP_PACK = 12, - TZ_MICHELSON_OP_UNPACK = 13, - TZ_MICHELSON_OP_BLAKE2B = 14, - TZ_MICHELSON_OP_SHA256 = 15, - TZ_MICHELSON_OP_SHA512 = 16, - TZ_MICHELSON_OP_ABS = 17, - TZ_MICHELSON_OP_ADD = 18, - TZ_MICHELSON_OP_AMOUNT = 19, - TZ_MICHELSON_OP_AND = 20, - TZ_MICHELSON_OP_BALANCE = 21, - TZ_MICHELSON_OP_CAR = 22, - TZ_MICHELSON_OP_CDR = 23, - TZ_MICHELSON_OP_CHECK_SIGNATURE = 24, - TZ_MICHELSON_OP_COMPARE = 25, - TZ_MICHELSON_OP_CONCAT = 26, - TZ_MICHELSON_OP_CONS = 27, - TZ_MICHELSON_OP_CREATE_ACCOUNT = 28, - TZ_MICHELSON_OP_CREATE_CONTRACT = 29, - TZ_MICHELSON_OP_IMPLICIT_ACCOUNT = 30, - TZ_MICHELSON_OP_DIP = 31, - TZ_MICHELSON_OP_DROP = 32, - TZ_MICHELSON_OP_DUP = 33, - TZ_MICHELSON_OP_EDIV = 34, - TZ_MICHELSON_OP_EMPTY_MAP = 35, - TZ_MICHELSON_OP_EMPTY_SET = 36, - TZ_MICHELSON_OP_EQ = 37, - TZ_MICHELSON_OP_EXEC = 38, - TZ_MICHELSON_OP_FAILWITH = 39, - TZ_MICHELSON_OP_GE = 40, - TZ_MICHELSON_OP_GET = 41, - TZ_MICHELSON_OP_GT = 42, - TZ_MICHELSON_OP_HASH_KEY = 43, - TZ_MICHELSON_OP_IF = 44, - TZ_MICHELSON_OP_IF_CONS = 45, - TZ_MICHELSON_OP_IF_LEFT = 46, - TZ_MICHELSON_OP_IF_NONE = 47, - TZ_MICHELSON_OP_INT = 48, - TZ_MICHELSON_OP_LAMBDA = 49, - TZ_MICHELSON_OP_LE = 50, - TZ_MICHELSON_OP_LEFT = 51, - TZ_MICHELSON_OP_LOOP = 52, - TZ_MICHELSON_OP_LSL = 53, - TZ_MICHELSON_OP_LSR = 54, - TZ_MICHELSON_OP_LT = 55, - TZ_MICHELSON_OP_MAP = 56, - TZ_MICHELSON_OP_MEM = 57, - TZ_MICHELSON_OP_MUL = 58, - TZ_MICHELSON_OP_NEG = 59, - TZ_MICHELSON_OP_NEQ = 60, - TZ_MICHELSON_OP_NIL = 61, - TZ_MICHELSON_OP_NONE = 62, - TZ_MICHELSON_OP_NOT = 63, - TZ_MICHELSON_OP_NOW = 64, - TZ_MICHELSON_OP_OR = 65, - TZ_MICHELSON_OP_PAIR = 66, - TZ_MICHELSON_OP_PUSH = 67, - TZ_MICHELSON_OP_RIGHT = 68, - TZ_MICHELSON_OP_SIZE = 69, - TZ_MICHELSON_OP_SOME = 70, - TZ_MICHELSON_OP_SOURCE = 71, - TZ_MICHELSON_OP_SENDER = 72, - TZ_MICHELSON_OP_SELF = 73, - TZ_MICHELSON_OP_STEPS_TO_QUOTA = 74, - TZ_MICHELSON_OP_SUB = 75, - TZ_MICHELSON_OP_SWAP = 76, - TZ_MICHELSON_OP_TRANSFER_TOKENS = 77, - TZ_MICHELSON_OP_SET_DELEGATE = 78, - TZ_MICHELSON_OP_UNIT = 79, - TZ_MICHELSON_OP_UPDATE = 80, - TZ_MICHELSON_OP_XOR = 81, - TZ_MICHELSON_OP_ITER = 82, - TZ_MICHELSON_OP_LOOP_LEFT = 83, - TZ_MICHELSON_OP_ADDRESS = 84, - TZ_MICHELSON_OP_CONTRACT = 85, - TZ_MICHELSON_OP_ISNAT = 86, - TZ_MICHELSON_OP_CAST = 87, - TZ_MICHELSON_OP_RENAME = 88, - TZ_MICHELSON_OP_bool = 89, - TZ_MICHELSON_OP_contract = 90, - TZ_MICHELSON_OP_int = 91, - TZ_MICHELSON_OP_key = 92, - TZ_MICHELSON_OP_key_hash = 93, - TZ_MICHELSON_OP_lambda = 94, - TZ_MICHELSON_OP_list = 95, - TZ_MICHELSON_OP_map = 96, - TZ_MICHELSON_OP_big_map = 97, - TZ_MICHELSON_OP_nat = 98, - TZ_MICHELSON_OP_option = 99, - TZ_MICHELSON_OP_or = 100, - TZ_MICHELSON_OP_pair = 101, - TZ_MICHELSON_OP_set = 102, - TZ_MICHELSON_OP_signature = 103, - TZ_MICHELSON_OP_string = 104, - TZ_MICHELSON_OP_bytes = 105, - TZ_MICHELSON_OP_mutez = 106, - TZ_MICHELSON_OP_timestamp = 107, - TZ_MICHELSON_OP_unit = 108, - TZ_MICHELSON_OP_operation = 109, - TZ_MICHELSON_OP_address = 110, - TZ_MICHELSON_OP_SLICE = 111, - TZ_MICHELSON_OP_DIG = 112, - TZ_MICHELSON_OP_DUG = 113, - TZ_MICHELSON_OP_EMPTY_BIG_MAP = 114, - TZ_MICHELSON_OP_APPLY = 115, - TZ_MICHELSON_OP_chain_id = 116, - TZ_MICHELSON_OP_CHAIN_ID = 117, - TZ_MICHELSON_OP_LEVEL = 118, - TZ_MICHELSON_OP_SELF_ADDRESS = 119, - TZ_MICHELSON_OP_never = 120, - TZ_MICHELSON_OP_NEVER = 121, - TZ_MICHELSON_OP_UNPAIR = 122, - TZ_MICHELSON_OP_VOTING_POWER = 123, - TZ_MICHELSON_OP_TOTAL_VOTING_POWER = 124, - TZ_MICHELSON_OP_KECCAK = 125, - TZ_MICHELSON_OP_SHA3 = 126, - TZ_MICHELSON_OP_PAIRING_CHECK = 127, - TZ_MICHELSON_OP_bls12_381_g1 = 128, - TZ_MICHELSON_OP_bls12_381_g2 = 129, - TZ_MICHELSON_OP_bls12_381_fr = 130, - TZ_MICHELSON_OP_sapling_state = 131, + TZ_MICHELSON_OP_parameter = 0, + TZ_MICHELSON_OP_storage = 1, + TZ_MICHELSON_OP_code = 2, + TZ_MICHELSON_OP_False = 3, + TZ_MICHELSON_OP_Elt = 4, + TZ_MICHELSON_OP_Left = 5, + TZ_MICHELSON_OP_None = 6, + TZ_MICHELSON_OP_Pair = 7, + TZ_MICHELSON_OP_Right = 8, + TZ_MICHELSON_OP_Some = 9, + TZ_MICHELSON_OP_True = 10, + TZ_MICHELSON_OP_Unit = 11, + TZ_MICHELSON_OP_PACK = 12, + TZ_MICHELSON_OP_UNPACK = 13, + TZ_MICHELSON_OP_BLAKE2B = 14, + TZ_MICHELSON_OP_SHA256 = 15, + TZ_MICHELSON_OP_SHA512 = 16, + TZ_MICHELSON_OP_ABS = 17, + TZ_MICHELSON_OP_ADD = 18, + TZ_MICHELSON_OP_AMOUNT = 19, + TZ_MICHELSON_OP_AND = 20, + TZ_MICHELSON_OP_BALANCE = 21, + TZ_MICHELSON_OP_CAR = 22, + TZ_MICHELSON_OP_CDR = 23, + TZ_MICHELSON_OP_CHECK_SIGNATURE = 24, + TZ_MICHELSON_OP_COMPARE = 25, + TZ_MICHELSON_OP_CONCAT = 26, + TZ_MICHELSON_OP_CONS = 27, + TZ_MICHELSON_OP_CREATE_ACCOUNT = 28, + TZ_MICHELSON_OP_CREATE_CONTRACT = 29, + TZ_MICHELSON_OP_IMPLICIT_ACCOUNT = 30, + TZ_MICHELSON_OP_DIP = 31, + TZ_MICHELSON_OP_DROP = 32, + TZ_MICHELSON_OP_DUP = 33, + TZ_MICHELSON_OP_EDIV = 34, + TZ_MICHELSON_OP_EMPTY_MAP = 35, + TZ_MICHELSON_OP_EMPTY_SET = 36, + TZ_MICHELSON_OP_EQ = 37, + TZ_MICHELSON_OP_EXEC = 38, + TZ_MICHELSON_OP_FAILWITH = 39, + TZ_MICHELSON_OP_GE = 40, + TZ_MICHELSON_OP_GET = 41, + TZ_MICHELSON_OP_GT = 42, + TZ_MICHELSON_OP_HASH_KEY = 43, + TZ_MICHELSON_OP_IF = 44, + TZ_MICHELSON_OP_IF_CONS = 45, + TZ_MICHELSON_OP_IF_LEFT = 46, + TZ_MICHELSON_OP_IF_NONE = 47, + TZ_MICHELSON_OP_INT = 48, + TZ_MICHELSON_OP_LAMBDA = 49, + TZ_MICHELSON_OP_LE = 50, + TZ_MICHELSON_OP_LEFT = 51, + TZ_MICHELSON_OP_LOOP = 52, + TZ_MICHELSON_OP_LSL = 53, + TZ_MICHELSON_OP_LSR = 54, + TZ_MICHELSON_OP_LT = 55, + TZ_MICHELSON_OP_MAP = 56, + TZ_MICHELSON_OP_MEM = 57, + TZ_MICHELSON_OP_MUL = 58, + TZ_MICHELSON_OP_NEG = 59, + TZ_MICHELSON_OP_NEQ = 60, + TZ_MICHELSON_OP_NIL = 61, + TZ_MICHELSON_OP_NONE = 62, + TZ_MICHELSON_OP_NOT = 63, + TZ_MICHELSON_OP_NOW = 64, + TZ_MICHELSON_OP_OR = 65, + TZ_MICHELSON_OP_PAIR = 66, + TZ_MICHELSON_OP_PUSH = 67, + TZ_MICHELSON_OP_RIGHT = 68, + TZ_MICHELSON_OP_SIZE = 69, + TZ_MICHELSON_OP_SOME = 70, + TZ_MICHELSON_OP_SOURCE = 71, + TZ_MICHELSON_OP_SENDER = 72, + TZ_MICHELSON_OP_SELF = 73, + TZ_MICHELSON_OP_STEPS_TO_QUOTA = 74, + TZ_MICHELSON_OP_SUB = 75, + TZ_MICHELSON_OP_SWAP = 76, + TZ_MICHELSON_OP_TRANSFER_TOKENS = 77, + TZ_MICHELSON_OP_SET_DELEGATE = 78, + TZ_MICHELSON_OP_UNIT = 79, + TZ_MICHELSON_OP_UPDATE = 80, + TZ_MICHELSON_OP_XOR = 81, + TZ_MICHELSON_OP_ITER = 82, + TZ_MICHELSON_OP_LOOP_LEFT = 83, + TZ_MICHELSON_OP_ADDRESS = 84, + TZ_MICHELSON_OP_CONTRACT = 85, + TZ_MICHELSON_OP_ISNAT = 86, + TZ_MICHELSON_OP_CAST = 87, + TZ_MICHELSON_OP_RENAME = 88, + TZ_MICHELSON_OP_bool = 89, + TZ_MICHELSON_OP_contract = 90, + TZ_MICHELSON_OP_int = 91, + TZ_MICHELSON_OP_key = 92, + TZ_MICHELSON_OP_key_hash = 93, + TZ_MICHELSON_OP_lambda = 94, + TZ_MICHELSON_OP_list = 95, + TZ_MICHELSON_OP_map = 96, + TZ_MICHELSON_OP_big_map = 97, + TZ_MICHELSON_OP_nat = 98, + TZ_MICHELSON_OP_option = 99, + TZ_MICHELSON_OP_or = 100, + TZ_MICHELSON_OP_pair = 101, + TZ_MICHELSON_OP_set = 102, + TZ_MICHELSON_OP_signature = 103, + TZ_MICHELSON_OP_string = 104, + TZ_MICHELSON_OP_bytes = 105, + TZ_MICHELSON_OP_mutez = 106, + TZ_MICHELSON_OP_timestamp = 107, + TZ_MICHELSON_OP_unit = 108, + TZ_MICHELSON_OP_operation = 109, + TZ_MICHELSON_OP_address = 110, + TZ_MICHELSON_OP_SLICE = 111, + TZ_MICHELSON_OP_DIG = 112, + TZ_MICHELSON_OP_DUG = 113, + TZ_MICHELSON_OP_EMPTY_BIG_MAP = 114, + TZ_MICHELSON_OP_APPLY = 115, + TZ_MICHELSON_OP_chain_id = 116, + TZ_MICHELSON_OP_CHAIN_ID = 117, + TZ_MICHELSON_OP_LEVEL = 118, + TZ_MICHELSON_OP_SELF_ADDRESS = 119, + TZ_MICHELSON_OP_never = 120, + TZ_MICHELSON_OP_NEVER = 121, + TZ_MICHELSON_OP_UNPAIR = 122, + TZ_MICHELSON_OP_VOTING_POWER = 123, + TZ_MICHELSON_OP_TOTAL_VOTING_POWER = 124, + TZ_MICHELSON_OP_KECCAK = 125, + TZ_MICHELSON_OP_SHA3 = 126, + TZ_MICHELSON_OP_PAIRING_CHECK = 127, + TZ_MICHELSON_OP_bls12_381_g1 = 128, + TZ_MICHELSON_OP_bls12_381_g2 = 129, + TZ_MICHELSON_OP_bls12_381_fr = 130, + TZ_MICHELSON_OP_sapling_state = 131, TZ_MICHELSON_OP_sapling_transaction_deprecated = 132, - TZ_MICHELSON_OP_SAPLING_EMPTY_STATE = 133, - TZ_MICHELSON_OP_SAPLING_VERIFY_UPDATE = 134, - TZ_MICHELSON_OP_ticket = 135, - TZ_MICHELSON_OP_TICKET_DEPRECATED = 136, - TZ_MICHELSON_OP_READ_TICKET = 137, - TZ_MICHELSON_OP_SPLIT_TICKET = 138, - TZ_MICHELSON_OP_JOIN_TICKETS = 139, - TZ_MICHELSON_OP_GET_AND_UPDATE = 140, - TZ_MICHELSON_OP_chest = 141, - TZ_MICHELSON_OP_chest_key = 142, - TZ_MICHELSON_OP_OPEN_CHEST = 143, - TZ_MICHELSON_OP_VIEW = 144, - TZ_MICHELSON_OP_view = 145, - TZ_MICHELSON_OP_constant = 146, - TZ_MICHELSON_OP_SUB_MUTEZ = 147, - TZ_MICHELSON_OP_tx_rollup_l2_address = 148, - TZ_MICHELSON_OP_MIN_BLOCK_TIME = 149, - TZ_MICHELSON_OP_sapling_transaction = 150, - TZ_MICHELSON_OP_EMIT = 151, - TZ_MICHELSON_OP_Lambda_rec = 152, - TZ_MICHELSON_OP_LAMBDA_REC = 153, - TZ_MICHELSON_OP_TICKET = 154 + TZ_MICHELSON_OP_SAPLING_EMPTY_STATE = 133, + TZ_MICHELSON_OP_SAPLING_VERIFY_UPDATE = 134, + TZ_MICHELSON_OP_ticket = 135, + TZ_MICHELSON_OP_TICKET_DEPRECATED = 136, + TZ_MICHELSON_OP_READ_TICKET = 137, + TZ_MICHELSON_OP_SPLIT_TICKET = 138, + TZ_MICHELSON_OP_JOIN_TICKETS = 139, + TZ_MICHELSON_OP_GET_AND_UPDATE = 140, + TZ_MICHELSON_OP_chest = 141, + TZ_MICHELSON_OP_chest_key = 142, + TZ_MICHELSON_OP_OPEN_CHEST = 143, + TZ_MICHELSON_OP_VIEW = 144, + TZ_MICHELSON_OP_view = 145, + TZ_MICHELSON_OP_constant = 146, + TZ_MICHELSON_OP_SUB_MUTEZ = 147, + TZ_MICHELSON_OP_tx_rollup_l2_address = 148, + TZ_MICHELSON_OP_MIN_BLOCK_TIME = 149, + TZ_MICHELSON_OP_sapling_transaction = 150, + TZ_MICHELSON_OP_EMIT = 151, + TZ_MICHELSON_OP_Lambda_rec = 152, + TZ_MICHELSON_OP_LAMBDA_REC = 153, + TZ_MICHELSON_OP_TICKET = 154 } tz_michelson_opcode; // Formats a positive number of arbitrary to decimal. The number is diff --git a/app/src/parser/micheline_parser.c b/app/src/parser/micheline_parser.c index aaf03b02d..9e9f01f15 100644 --- a/app/src/parser/micheline_parser.c +++ b/app/src/parser/micheline_parser.c @@ -25,54 +25,43 @@ /* Prototypes */ static tz_parser_result push_frame(tz_parser_state *, - tz_micheline_parser_step_kind); + tz_micheline_parser_step_kind); static tz_parser_result pop_frame(tz_parser_state *); static tz_parser_result begin_sized(tz_parser_state *); static tz_parser_result print_escaped(tz_parser_state *, uint8_t); static tz_parser_result parser_put(tz_parser_state *, char); static tz_parser_result tag_selection(tz_parser_state *, uint8_t); - #ifdef TEZOS_DEBUG -const char *const tz_micheline_parser_step_name[] = { - "TAG", - "PRIM_OP", - "PRIM_NAME", - "PRIM", - "SIZE", - "SEQ", - "BYTES", - "STRING", - "ANNOT", - "INT", - "PRINT_INT", - "CONTINUE" -}; +const char *const tz_micheline_parser_step_name[] + = {"TAG", "PRIM_OP", "PRIM_NAME", "PRIM", "SIZE", "SEQ", + "BYTES", "STRING", "ANNOT", "INT", "PRINT_INT", "CONTINUE"}; #endif - const char hex_c[] = "0123456789ABCDEF"; -void tz_micheline_parser_init(tz_parser_state *state) { +void tz_micheline_parser_init(tz_parser_state *state) +{ tz_micheline_state *m = &state->micheline; - m->frame = m->stack; + m->frame = m->stack; m->stack[0].step = TZ_MICHELINE_STEP_TAG; } -static tz_parser_result push_frame(tz_parser_state *state, - tz_micheline_parser_step_kind step) { +static tz_parser_result push_frame(tz_parser_state *state, + tz_micheline_parser_step_kind step) +{ tz_micheline_state *m = &state->micheline; - if (m->frame >= - &m->stack[TZ_MICHELINE_STACK_DEPTH - 1]) + if (m->frame >= &m->stack[TZ_MICHELINE_STACK_DEPTH - 1]) tz_raise(TOO_DEEP); - m->frame++ ; + m->frame++; m->frame->step = step; tz_continue; } -static tz_parser_result pop_frame(tz_parser_state *state) { +static tz_parser_result pop_frame(tz_parser_state *state) +{ tz_micheline_state *m = &state->micheline; if (m->frame == m->stack) { @@ -83,17 +72,20 @@ static tz_parser_result pop_frame(tz_parser_state *state) { tz_continue; } -static tz_parser_result begin_sized(tz_parser_state *state) { +static tz_parser_result begin_sized(tz_parser_state *state) +{ tz_micheline_state *m = &state->micheline; - if (push_frame(state, TZ_MICHELINE_STEP_SIZE)) tz_reraise; + if (push_frame(state, TZ_MICHELINE_STEP_SIZE)) + tz_reraise; m->frame->step_size.size = 0; - m->frame->stop = state->ofs+4; + m->frame->stop = state->ofs + 4; tz_continue; } -static tz_parser_result print_escaped(tz_parser_state *state, uint8_t b) { - char* buf = (char*) state->buffers.capture; +static tz_parser_result print_escaped(tz_parser_state *state, uint8_t b) +{ + char *buf = (char *)state->buffers.capture; tz_must(push_frame(state, TZ_MICHELINE_STEP_PRINT_CAPTURE)); state->micheline.frame->step_capture.ofs = 0; // clang-format off @@ -114,38 +106,39 @@ static tz_parser_result print_escaped(tz_parser_state *state, uint8_t b) { tz_continue; } -static tz_parser_result parser_put(tz_parser_state *state, char c) { - PRINTF("[DEBUG] put(char: '%c',int: %d)\n", c, (int) c); +static tz_parser_result parser_put(tz_parser_state *state, char c) +{ + PRINTF("[DEBUG] put(char: '%c',int: %d)\n", c, (int)c); return tz_parser_put(state, c); } -static tz_parser_result tag_selection(tz_parser_state *state, uint8_t t) { +static tz_parser_result tag_selection(tz_parser_state *state, uint8_t t) +{ tz_micheline_state *m = &state->micheline; - uint8_t nargs; - uint8_t annot; - uint8_t wrap; + uint8_t nargs; + uint8_t annot; + uint8_t wrap; switch (t) { case TZ_MICHELINE_TAG_INT: m->frame->step = TZ_MICHELINE_STEP_INT; - tz_parse_num_state_init(&state->buffers.num, - &m->frame->step_int); - for (int i = 0; i < TZ_NUM_BUFFER_SIZE/8;i++) + tz_parse_num_state_init(&state->buffers.num, &m->frame->step_int); + for (int i = 0; i < TZ_NUM_BUFFER_SIZE / 8; i++) state->buffers.num.bytes[i] = 0; break; case TZ_MICHELINE_TAG_SEQ: - m->frame->step = TZ_MICHELINE_STEP_SEQ; + m->frame->step = TZ_MICHELINE_STEP_SEQ; m->frame->step_seq.first = true; tz_must(begin_sized(state)); break; case TZ_MICHELINE_TAG_BYTES: - m->frame->step = TZ_MICHELINE_STEP_BYTES; - m->frame->step_bytes.first = true; + m->frame->step = TZ_MICHELINE_STEP_BYTES; + m->frame->step_bytes.first = true; m->frame->step_bytes.has_rem_half = false; tz_must(begin_sized(state)); break; case TZ_MICHELINE_TAG_STRING: - m->frame->step = TZ_MICHELINE_STEP_STRING; + m->frame->step = TZ_MICHELINE_STEP_STRING; m->frame->step_string.first = true; tz_must(begin_sized(state)); break; @@ -155,23 +148,23 @@ static tz_parser_result tag_selection(tz_parser_state *state, uint8_t t) { case TZ_MICHELINE_TAG_PRIM_1_NOANNOTS: case TZ_MICHELINE_TAG_PRIM_2_ANNOTS: case TZ_MICHELINE_TAG_PRIM_2_NOANNOTS: - nargs = (t-3) >> 1; + nargs = (t - 3) >> 1; annot = (~t & 1); - wrap = (m->frame > m->stack) - && m->frame[-1].step == TZ_MICHELINE_STEP_PRIM - && (nargs>0 || annot); + wrap = (m->frame > m->stack) + && m->frame[-1].step == TZ_MICHELINE_STEP_PRIM + && (nargs > 0 || annot); goto common_prim; case TZ_MICHELINE_TAG_PRIM_N: - wrap = (m->frame > m->stack) && - m->frame[-1].step == TZ_MICHELINE_STEP_PRIM; + wrap = (m->frame > m->stack) + && m->frame[-1].step == TZ_MICHELINE_STEP_PRIM; nargs = 3; annot = true; common_prim: - m->frame->step = TZ_MICHELINE_STEP_PRIM_OP; - m->frame->step_prim.ofs = 0; + m->frame->step = TZ_MICHELINE_STEP_PRIM_OP; + m->frame->step_prim.ofs = 0; m->frame->step_prim.nargs = nargs; - m->frame->step_prim.wrap = wrap; - m->frame->step_prim.spc = false; + m->frame->step_prim.wrap = wrap; + m->frame->step_prim.spc = false; m->frame->step_prim.first = true; m->frame->step_prim.annot = annot; break; @@ -180,40 +173,43 @@ static tz_parser_result tag_selection(tz_parser_state *state, uint8_t t) { tz_continue; } -tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { +tz_parser_result tz_micheline_parser_step(tz_parser_state *state) +{ tz_micheline_state *m = &state->micheline; - uint8_t b; - uint8_t op; - uint8_t t; + uint8_t b; + uint8_t op; + uint8_t t; // cannot restart after error - if (TZ_IS_ERR(state->errno)) tz_reraise; + if (TZ_IS_ERR(state->errno)) + tz_reraise; // nothing else to do - if (state->micheline.frame == NULL) tz_stop(DONE); + if (state->micheline.frame == NULL) + tz_stop(DONE); - PRINTF("[DEBUG] micheline(frame: %d, offset:%d/%d, step: %s, errno: %s)\n", - (int) (m->frame - m->stack), - (int) state->ofs, - (int) m->frame->stop, - (const char*) PIC(tz_micheline_parser_step_name[m->frame->step]), - tz_parser_result_name(state->errno)); + PRINTF( + "[DEBUG] micheline(frame: %d, offset:%d/%d, step: %s, errno: %s)\n", + (int)(m->frame - m->stack), (int)state->ofs, (int)m->frame->stop, + (const char *)PIC(tz_micheline_parser_step_name[m->frame->step]), + tz_parser_result_name(state->errno)); switch (state->micheline.frame->step) { case TZ_MICHELINE_STEP_INT: tz_must(tz_parser_read(state, &b)); - tz_must(tz_parse_int_step(&state->buffers.num, &m->frame->step_int, b)); + tz_must( + tz_parse_int_step(&state->buffers.num, &m->frame->step_int, b)); if (m->frame->step_int.stop) { - m->frame->step = TZ_MICHELINE_STEP_PRINT_INT; + m->frame->step = TZ_MICHELINE_STEP_PRINT_INT; m->frame->step_int.size = 0; } - break; + break; case TZ_MICHELINE_STEP_PRINT_INT: if (m->frame->step_int.sign) { tz_must(parser_put(state, '-')); m->frame->step_int.sign = 0; } else if (state->buffers.num.decimal[m->frame->step_int.size]) { - tz_must(parser_put(state, - state->buffers.num.decimal[m->frame->step_int.size])); + tz_must(parser_put( + state, state->buffers.num.decimal[m->frame->step_int.size])); m->frame->step_int.size++; } else { tz_must(pop_frame(state)); @@ -222,7 +218,7 @@ tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { case TZ_MICHELINE_STEP_SIZE: tz_must(tz_parser_read(state, &b)); if (m->frame->step_size.size > 255) - tz_raise(TOO_LARGE); // enforce 16-bit restriction + tz_raise(TOO_LARGE); // enforce 16-bit restriction m->frame->step_size.size = m->frame->step_size.size << 8 | b; if (m->frame->stop == state->ofs) { m->frame[-1].stop = state->ofs + m->frame->step_size.size; @@ -249,9 +245,10 @@ tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { } break; case TZ_MICHELINE_STEP_PRINT_CAPTURE: - if (state->buffers.capture[state->micheline.frame->step_capture.ofs]) { - tz_must(parser_put(state, - state->buffers.capture[m->frame->step_capture.ofs])); + if (state->buffers + .capture[state->micheline.frame->step_capture.ofs]) { + tz_must(parser_put( + state, state->buffers.capture[m->frame->step_capture.ofs])); m->frame->step_capture.ofs++; } else { tz_must(pop_frame(state)); @@ -264,8 +261,8 @@ tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { } else if (state->micheline.frame->step_bytes.first) { tz_must(parser_put(state, '0')); m->frame->step_bytes.has_rem_half = true; - m->frame->step_bytes.rem_half = 'x'; - m->frame->step_bytes.first = false; + m->frame->step_bytes.rem_half = 'x'; + m->frame->step_bytes.first = false; } else if (m->frame->stop == state->ofs) { tz_must(pop_frame(state)); } else { @@ -274,7 +271,7 @@ tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { half = hex_c[(b & 0xF0) >> 4]; tz_must(parser_put(state, half)); m->frame->step_bytes.has_rem_half = true; - m->frame->step_bytes.rem_half = hex_c[b & 0x0F]; + m->frame->step_bytes.rem_half = hex_c[b & 0x0F]; tz_parser_skip(state); } break; @@ -316,8 +313,9 @@ tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { break; case TZ_MICHELINE_STEP_PRIM_OP: tz_must(tz_parser_read(state, &op)); - if (tz_michelson_op_name(op) == NULL) tz_raise(INVALID_OP); - m->frame->step = TZ_MICHELINE_STEP_PRIM_NAME; + if (tz_michelson_op_name(op) == NULL) + tz_raise(INVALID_OP); + m->frame->step = TZ_MICHELINE_STEP_PRIM_NAME; m->frame->step_prim.op = op; break; case TZ_MICHELINE_STEP_PRIM_NAME: @@ -325,9 +323,11 @@ tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { tz_must(parser_put(state, '(')); m->frame->step_prim.first = false; } - if (tz_michelson_op_name(m->frame->step_prim.op)[m->frame->step_prim.ofs]) { - tz_must(parser_put(state, - tz_michelson_op_name(m->frame->step_prim.op)[m->frame->step_prim.ofs])); + if (tz_michelson_op_name( + m->frame->step_prim.op)[m->frame->step_prim.ofs]) { + tz_must(parser_put( + state, tz_michelson_op_name( + m->frame->step_prim.op)[m->frame->step_prim.ofs])); m->frame->step_prim.ofs++; } else { m->frame->step = TZ_MICHELINE_STEP_PRIM; @@ -336,9 +336,9 @@ tz_parser_result tz_micheline_parser_step(tz_parser_state *state) { } break; case TZ_MICHELINE_STEP_PRIM: - if (m->frame->step_prim.nargs == 0 || - (m->frame->step_prim.nargs == 3 && - m->frame->stop == state->ofs)) { + if (m->frame->step_prim.nargs == 0 + || (m->frame->step_prim.nargs == 3 + && m->frame->stop == state->ofs)) { if (m->frame->step_prim.annot) { m->frame->step_prim.annot = false; tz_must(push_frame(state, TZ_MICHELINE_STEP_ANNOT)); diff --git a/app/src/parser/micheline_parser.h b/app/src/parser/micheline_parser.h index b96944eff..9d5b28f6c 100644 --- a/app/src/parser/micheline_parser.h +++ b/app/src/parser/micheline_parser.h @@ -18,5 +18,5 @@ #include "parser_state.h" -void tz_micheline_parser_init(tz_parser_state *); +void tz_micheline_parser_init(tz_parser_state *); tz_parser_result tz_micheline_parser_step(tz_parser_state *); diff --git a/app/src/parser/micheline_state.h b/app/src/parser/micheline_state.h index ff3284726..2a2e60c4a 100644 --- a/app/src/parser/micheline_state.h +++ b/app/src/parser/micheline_state.h @@ -61,42 +61,43 @@ typedef enum { typedef struct { tz_micheline_parser_step_kind step : 4; - uint16_t stop; + uint16_t stop; union { struct { uint16_t size; - } step_size; // TZ_MICHELINE_STEP_SIZE + } step_size; // TZ_MICHELINE_STEP_SIZE struct { uint8_t first : 1; - } step_seq; // TZ_MICHELINE_STEP_SEQ + } step_seq; // TZ_MICHELINE_STEP_SEQ struct { - uint8_t first : 1, has_rem_half: 1; + uint8_t first : 1, has_rem_half : 1; uint8_t rem_half; - } step_bytes; // TZ_MICHELINE_STEP_BYTES + } step_bytes; // TZ_MICHELINE_STEP_BYTES struct { uint8_t first : 1; - } step_string; // TZ_MICHELINE_STEP_STRING + } step_string; // TZ_MICHELINE_STEP_STRING struct { uint8_t first : 1; - } step_annot; // TZ_MICHELINE_STEP_ANNOT - tz_num_parser_regs step_int; // TZ_MICHELINE_STEP_INT, - // TZ_MICHELINE_STEP_PRINT_INT + } step_annot; // TZ_MICHELINE_STEP_ANNOT + tz_num_parser_regs step_int; // TZ_MICHELINE_STEP_INT, + // TZ_MICHELINE_STEP_PRINT_INT struct { uint8_t op; uint8_t ofs; uint8_t nargs : 2, wrap : 1, spc : 1, annot : 1, first : 1; - } step_prim; // TZ_MICHELINE_STEP_PRIM_OP, - // TZ_MICHELINE_STEP_PRIM_NAME, - // TZ_MICHELINE_STEP_PRIM + } step_prim; // TZ_MICHELINE_STEP_PRIM_OP, + // TZ_MICHELINE_STEP_PRIM_NAME, + // TZ_MICHELINE_STEP_PRIM struct { int ofs; - } step_capture; // TZ_MICHELINE_STEP_CAPTURE_BYTES, - // TZ_MICHELINE_STEP_PRINT_CAPTURE - tz_micheline_capture_kind step_capturing; // TZ_MICHELINE_STEP_CAPTURING + } step_capture; // TZ_MICHELINE_STEP_CAPTURE_BYTES, + // TZ_MICHELINE_STEP_PRINT_CAPTURE + tz_micheline_capture_kind + step_capturing; // TZ_MICHELINE_STEP_CAPTURING }; } tz_micheline_parser_frame; typedef struct { - tz_micheline_parser_frame stack[TZ_MICHELINE_STACK_DEPTH]; - tz_micheline_parser_frame *frame; // init == stack, NULL when done + tz_micheline_parser_frame stack[TZ_MICHELINE_STACK_DEPTH]; + tz_micheline_parser_frame *frame; // init == stack, NULL when done } tz_micheline_state; diff --git a/app/src/parser/num_parser.c b/app/src/parser/num_parser.c index 985d2a574..17b06becd 100644 --- a/app/src/parser/num_parser.c +++ b/app/src/parser/num_parser.c @@ -17,34 +17,37 @@ #include "num_parser.h" void tz_parse_num_state_init(tz_num_parser_buffer *buffers, - tz_num_parser_regs *regs) { + tz_num_parser_regs *regs) +{ buffers->bytes[0] = 0; - regs->size = 0; - regs->sign = 0; - regs->stop = 0; + regs->size = 0; + regs->sign = 0; + regs->stop = 0; } tz_parser_result tz_parse_num_step(tz_num_parser_buffer *buffers, - tz_num_parser_regs *regs, - uint8_t b, bool natural) { + tz_num_parser_regs *regs, uint8_t b, + bool natural) +{ uint8_t v, cont, s; cont = b >> 7; if (regs->size == 0 && !natural) { - v = b & 0x3F; - regs->sign = (b >> 6)&1; - s = 6; + v = b & 0x3F; + regs->sign = (b >> 6) & 1; + s = 6; } else { v = b & 0x7F; s = 7; } - uint8_t lo = v << (regs->size & 7); - uint8_t hi = v >> (8 - (regs->size & 7)); - int lo_idx = regs->size/8; - int hi_idx = lo_idx+1; + uint8_t lo = v << (regs->size & 7); + uint8_t hi = v >> (8 - (regs->size & 7)); + int lo_idx = regs->size / 8; + int hi_idx = lo_idx + 1; buffers->bytes[lo_idx] |= lo; - if (hi_idx >= TZ_NUM_BUFFER_SIZE/8) { + if (hi_idx >= TZ_NUM_BUFFER_SIZE / 8) { // accept and dismiss a few trailing zeroes - if (hi || cont) return TZ_ERR_TOO_LARGE; + if (hi || cont) + return TZ_ERR_TOO_LARGE; regs->size = TZ_NUM_BUFFER_SIZE; } else { buffers->bytes[hi_idx] = hi; @@ -52,17 +55,20 @@ tz_parser_result tz_parse_num_step(tz_num_parser_buffer *buffers, } if (!cont) { regs->stop = true; - tz_format_decimal(buffers->bytes, (regs->size+7)/8, buffers->decimal); + tz_format_decimal(buffers->bytes, (regs->size + 7) / 8, + buffers->decimal); } return TZ_CONTINUE; } tz_parser_result tz_parse_int_step(tz_num_parser_buffer *buffers, - tz_num_parser_regs *regs, uint8_t b) { + tz_num_parser_regs *regs, uint8_t b) +{ return tz_parse_num_step(buffers, regs, b, 0); } tz_parser_result tz_parse_nat_step(tz_num_parser_buffer *buffers, - tz_num_parser_regs *regs, uint8_t b) { + tz_num_parser_regs *regs, uint8_t b) +{ return tz_parse_num_step(buffers, regs, b, 1); } diff --git a/app/src/parser/num_parser.h b/app/src/parser/num_parser.h index 6f263f243..9565506bc 100644 --- a/app/src/parser/num_parser.h +++ b/app/src/parser/num_parser.h @@ -20,11 +20,8 @@ void tz_parse_num_state_init(tz_num_parser_buffer *, tz_num_parser_regs *); tz_parser_result tz_parse_num_step(tz_num_parser_buffer *, - tz_num_parser_regs *, - uint8_t, bool); + tz_num_parser_regs *, uint8_t, bool); tz_parser_result tz_parse_int_step(tz_num_parser_buffer *, - tz_num_parser_regs *, - uint8_t); + tz_num_parser_regs *, uint8_t); tz_parser_result tz_parse_nat_step(tz_num_parser_buffer *, - tz_num_parser_regs *, - uint8_t); + tz_num_parser_regs *, uint8_t); diff --git a/app/src/parser/num_state.h b/app/src/parser/num_state.h index 4d4c271e6..363e2ac14 100644 --- a/app/src/parser/num_state.h +++ b/app/src/parser/num_state.h @@ -20,12 +20,12 @@ typedef struct { uint16_t size; - uint8_t sign : 1, stop : 1; + uint8_t sign : 1, stop : 1; } tz_num_parser_regs; #define TZ_NUM_BUFFER_SIZE 256 typedef struct { - uint8_t bytes[TZ_NUM_BUFFER_SIZE/8]; - char decimal[TZ_DECIMAL_BUFFER_SIZE(TZ_NUM_BUFFER_SIZE/8)]; + uint8_t bytes[TZ_NUM_BUFFER_SIZE / 8]; + char decimal[TZ_DECIMAL_BUFFER_SIZE(TZ_NUM_BUFFER_SIZE / 8)]; } tz_num_parser_buffer; diff --git a/app/src/parser/operation_parser.c b/app/src/parser/operation_parser.c index 32d8a1cdb..7b2240537 100644 --- a/app/src/parser/operation_parser.c +++ b/app/src/parser/operation_parser.c @@ -26,362 +26,366 @@ static tz_parser_result push_frame(tz_parser_state *, tz_operation_parser_step_kind); static tz_parser_result pop_frame(tz_parser_state *); - #ifdef TEZOS_DEBUG -const char *const tz_operation_parser_step_name[] = { - "MAGIC", - "READ_BINARY", - "BRANCH", - "BATCH", - "TAG", - "SIZE", - "OPERATION", - "PRINT", - "PARTIAL_PRINT", - "READ_NUM", - "READ_INT32", - "READ_PK", - "READ_BYTES", - "READ_STRING", - "READ_SMART_ENTRYPOINT", - "READ_MICHELINE", - "READ_SORU_MESSAGES", - "READ_SORU_KIND", - "READ_BALLOT", - "READ_PROTOS" -}; +const char *const tz_operation_parser_step_name[] = {"MAGIC", + "READ_BINARY", + "BRANCH", + "BATCH", + "TAG", + "SIZE", + "OPERATION", + "PRINT", + "PARTIAL_PRINT", + "READ_NUM", + "READ_INT32", + "READ_PK", + "READ_BYTES", + "READ_STRING", + "READ_SMART_ENTRYPOINT", + "READ_MICHELINE", + "READ_SORU_MESSAGES", + "READ_SORU_KIND", + "READ_BALLOT", + "READ_PROTOS"}; #endif const tz_operation_field_descriptor proposals_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_PKH, true, false, false }, - { "Period", TZ_OPERATION_FIELD_INT32, true, false, false }, - { "Proposal", TZ_OPERATION_FIELD_PROTOS, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_PKH, true, false, false}, + {"Period", TZ_OPERATION_FIELD_INT32, true, false, false}, + {"Proposal", TZ_OPERATION_FIELD_PROTOS, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor ballot_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_PKH, true, false, false }, - { "Period", TZ_OPERATION_FIELD_INT32, true, false, false }, - { "Proposal", TZ_OPERATION_FIELD_PROTO, true, false, false }, - { "Ballot", TZ_OPERATION_FIELD_BALLOT, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_PKH, true, false, false}, + {"Period", TZ_OPERATION_FIELD_INT32, true, false, false}, + {"Proposal", TZ_OPERATION_FIELD_PROTO, true, false, false}, + {"Ballot", TZ_OPERATION_FIELD_BALLOT, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor failing_noop_fields[] = { - // Name, Kind, Req, Skip, None - { "Message", TZ_OPERATION_FIELD_BINARY, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Message", TZ_OPERATION_FIELD_BINARY, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor transaction_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Amount", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Destination", TZ_OPERATION_FIELD_DESTINATION, true, false, false }, - { "Parameter", TZ_OPERATION_FIELD_PARAMETER, false,false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Amount", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Destination", TZ_OPERATION_FIELD_DESTINATION, true, false, false}, + {"Parameter", TZ_OPERATION_FIELD_PARAMETER, false, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor reveal_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Public key", TZ_OPERATION_FIELD_PK, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Public key", TZ_OPERATION_FIELD_PK, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor delegation_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Delegate", TZ_OPERATION_FIELD_PKH, false,false, true }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Delegate", TZ_OPERATION_FIELD_PKH, false, false, true }, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor reg_glb_cst_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Value", TZ_OPERATION_FIELD_EXPR, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Value", TZ_OPERATION_FIELD_EXPR, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor set_deposit_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Staking limit", TZ_OPERATION_FIELD_AMOUNT, false,false, true }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Staking limit", TZ_OPERATION_FIELD_AMOUNT, false, false, true }, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor inc_paid_stg_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Amount", TZ_OPERATION_FIELD_INT, true, false, false }, - { "Destination", TZ_OPERATION_FIELD_DESTINATION, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Amount", TZ_OPERATION_FIELD_INT, true, false, false}, + {"Destination", TZ_OPERATION_FIELD_DESTINATION, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor update_ck_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Public key", TZ_OPERATION_FIELD_PK, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Public key", TZ_OPERATION_FIELD_PK, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor origination_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Balance", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Delegate", TZ_OPERATION_FIELD_PKH, false,false, true }, - { "Code", TZ_OPERATION_FIELD_EXPR, true, false, false }, - { "Storage", TZ_OPERATION_FIELD_EXPR, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Balance", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Delegate", TZ_OPERATION_FIELD_PKH, false, false, true }, + {"Code", TZ_OPERATION_FIELD_EXPR, true, false, false}, + {"Storage", TZ_OPERATION_FIELD_EXPR, true, false, false}, + {NULL, 0, 0, 0, 0 } }; - const tz_operation_field_descriptor transfer_tck_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Contents", TZ_OPERATION_FIELD_EXPR, true, false, false }, - { "Type", TZ_OPERATION_FIELD_EXPR, true, false, false }, - { "Ticketer", TZ_OPERATION_FIELD_DESTINATION, true, false, false }, - { "Amount", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Destination", TZ_OPERATION_FIELD_DESTINATION, true, false, false }, - { "Entrypoint", TZ_OPERATION_FIELD_STRING, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Contents", TZ_OPERATION_FIELD_EXPR, true, false, false}, + {"Type", TZ_OPERATION_FIELD_EXPR, true, false, false}, + {"Ticketer", TZ_OPERATION_FIELD_DESTINATION, true, false, false}, + {"Amount", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Destination", TZ_OPERATION_FIELD_DESTINATION, true, false, false}, + {"Entrypoint", TZ_OPERATION_FIELD_STRING, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor soru_add_msg_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Message", TZ_OPERATION_FIELD_SORU_MESSAGES, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Message", TZ_OPERATION_FIELD_SORU_MESSAGES, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor soru_exe_msg_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Rollup", TZ_OPERATION_FIELD_SR, true, false, false }, - { "Commitment", TZ_OPERATION_FIELD_SRC, true, false, false }, - { "Output proof", TZ_OPERATION_FIELD_BINARY, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Rollup", TZ_OPERATION_FIELD_SR, true, false, false}, + {"Commitment", TZ_OPERATION_FIELD_SRC, true, false, false}, + {"Output proof", TZ_OPERATION_FIELD_BINARY, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_field_descriptor soru_origin_fields[] = { - // Name, Kind, Req, Skip, None - { "Source", TZ_OPERATION_FIELD_SOURCE, true, true, false }, - { "Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false }, - { "Counter", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Gas", TZ_OPERATION_FIELD_NAT, true, true, false }, - { "Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false }, - { "Kind", TZ_OPERATION_FIELD_SORU_KIND, true, false, false }, - { "Kernel", TZ_OPERATION_FIELD_BINARY, true, false, false }, - { "Proof", TZ_OPERATION_FIELD_BINARY, true, false, false }, - { "Parameters", TZ_OPERATION_FIELD_EXPR, true, false, false }, - { NULL, 0, 0, 0, 0 } + // Name, Kind, Req, Skip, None + {"Source", TZ_OPERATION_FIELD_SOURCE, true, true, false}, + {"Fee", TZ_OPERATION_FIELD_AMOUNT, true, false, false}, + {"Counter", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Gas", TZ_OPERATION_FIELD_NAT, true, true, false}, + {"Storage limit", TZ_OPERATION_FIELD_NAT, true, false, false}, + {"Kind", TZ_OPERATION_FIELD_SORU_KIND, true, false, false}, + {"Kernel", TZ_OPERATION_FIELD_BINARY, true, false, false}, + {"Proof", TZ_OPERATION_FIELD_BINARY, true, false, false}, + {"Parameters", TZ_OPERATION_FIELD_EXPR, true, false, false}, + {NULL, 0, 0, 0, 0 } }; const tz_operation_descriptor tz_operation_descriptors[] = { - { TZ_OPERATION_TAG_PROPOSALS, "Proposals", proposals_fields }, - { TZ_OPERATION_TAG_BALLOT, "Ballot", ballot_fields }, - { TZ_OPERATION_TAG_FAILING_NOOP, "Failing noop", failing_noop_fields }, - { TZ_OPERATION_TAG_REVEAL, "Reveal", reveal_fields }, - { TZ_OPERATION_TAG_TRANSACTION, "Transaction", transaction_fields }, - { TZ_OPERATION_TAG_ORIGINATION, "Origination", origination_fields }, - { TZ_OPERATION_TAG_DELEGATION, "Delegation", delegation_fields }, - { TZ_OPERATION_TAG_REG_GLB_CST, "Register global constant", reg_glb_cst_fields }, - { TZ_OPERATION_TAG_SET_DEPOSIT, "Set deposit limit", set_deposit_fields }, - { TZ_OPERATION_TAG_INC_PAID_STG, "Increase paid storage", inc_paid_stg_fields }, - { TZ_OPERATION_TAG_UPDATE_CK, "Set consensus key", update_ck_fields }, - { TZ_OPERATION_TAG_TRANSFER_TCK, "Transfer ticket", transfer_tck_fields }, - { TZ_OPERATION_TAG_SORU_ADD_MSG, "SR: send messages", soru_add_msg_fields }, - { TZ_OPERATION_TAG_SORU_EXE_MSG, "SR: execute outbox message", soru_exe_msg_fields }, - { TZ_OPERATION_TAG_SORU_ORIGIN, "SR: originate", soru_origin_fields }, - { 0, NULL, 0 } + {TZ_OPERATION_TAG_PROPOSALS, "Proposals", proposals_fields }, + {TZ_OPERATION_TAG_BALLOT, "Ballot", ballot_fields }, + {TZ_OPERATION_TAG_FAILING_NOOP, "Failing noop", failing_noop_fields}, + {TZ_OPERATION_TAG_REVEAL, "Reveal", reveal_fields }, + {TZ_OPERATION_TAG_TRANSACTION, "Transaction", transaction_fields }, + {TZ_OPERATION_TAG_ORIGINATION, "Origination", origination_fields }, + {TZ_OPERATION_TAG_DELEGATION, "Delegation", delegation_fields }, + {TZ_OPERATION_TAG_REG_GLB_CST, "Register global constant", + reg_glb_cst_fields }, + {TZ_OPERATION_TAG_SET_DEPOSIT, "Set deposit limit", set_deposit_fields }, + {TZ_OPERATION_TAG_INC_PAID_STG, "Increase paid storage", + inc_paid_stg_fields }, + {TZ_OPERATION_TAG_UPDATE_CK, "Set consensus key", update_ck_fields }, + {TZ_OPERATION_TAG_TRANSFER_TCK, "Transfer ticket", transfer_tck_fields}, + {TZ_OPERATION_TAG_SORU_ADD_MSG, "SR: send messages", soru_add_msg_fields}, + {TZ_OPERATION_TAG_SORU_EXE_MSG, "SR: execute outbox message", + soru_exe_msg_fields }, + {TZ_OPERATION_TAG_SORU_ORIGIN, "SR: originate", soru_origin_fields }, + {0, NULL, 0 } }; -static const char* expression_name = "Expression"; -static const char* unset_message = "Field unset"; +static const char *expression_name = "Expression"; +static const char *unset_message = "Field unset"; -static tz_parser_result push_frame(tz_parser_state *state, - tz_operation_parser_step_kind step) { +static tz_parser_result push_frame(tz_parser_state *state, + tz_operation_parser_step_kind step) +{ tz_operation_state *op = &state->operation; - if (op->frame >= - &op->stack[TZ_OPERATION_STACK_DEPTH - 1]) + if (op->frame >= &op->stack[TZ_OPERATION_STACK_DEPTH - 1]) tz_raise(TOO_DEEP); - op->frame++ ; + op->frame++; op->frame->step = step; tz_continue; } -static tz_parser_result pop_frame(tz_parser_state *state) { +static tz_parser_result pop_frame(tz_parser_state *state) +{ tz_operation_state *op = &state->operation; if (op->frame == op->stack) { op->frame = NULL; - tz_stop(DONE); + tz_stop(DONE); } op->frame--; tz_continue; } -void tz_operation_parser_set_size(tz_parser_state *state, uint16_t size) { +void tz_operation_parser_set_size(tz_parser_state *state, uint16_t size) +{ state->operation.stack[0].stop = size; } void tz_operation_parser_init(tz_parser_state *state, uint16_t size, - bool skip_magic) { + bool skip_magic) +{ tz_operation_state *op = &state->operation; tz_parser_init(state); state->operation.seen_reveal = 0; memset(&state->operation.source, 0, 22); memset(&state->operation.destination, 0, 22); - op->batch_index = 0; - op->frame = op->stack; + op->batch_index = 0; + op->frame = op->stack; op->stack[0].stop = size; if (!skip_magic) { op->stack[0].step = TZ_OPERATION_STEP_MAGIC; } else { strcpy(state->field_name, "Branch"); op->stack[0].step = TZ_OPERATION_STEP_BRANCH; - push_frame(state, TZ_OPERATION_STEP_READ_BYTES); // ignore result, - // assume success + push_frame(state, TZ_OPERATION_STEP_READ_BYTES); // ignore result, + // assume success op->frame->step_read_bytes.kind = TZ_OPERATION_FIELD_BH; op->frame->step_read_bytes.skip = true; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 32; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 32; } } -static tz_parser_result tz_print_string(tz_parser_state *state) { +static tz_parser_result tz_print_string(tz_parser_state *state) +{ tz_operation_state *op = &state->operation; if (op->frame->step_read_string.skip) { tz_must(pop_frame(state)); tz_continue; } - op->frame->step = TZ_OPERATION_STEP_PRINT; - op->frame->step_print.str = (char*) state->buffers.capture; + op->frame->step = TZ_OPERATION_STEP_PRINT; + op->frame->step_print.str = (char *)state->buffers.capture; tz_continue; } -tz_parser_result tz_operation_parser_step(tz_parser_state *state) { - tz_operation_state *op = &state->operation; - tz_parser_regs *regs = &state->regs; - uint8_t *capture = state->buffers.capture; +tz_parser_result tz_operation_parser_step(tz_parser_state *state) +{ + tz_operation_state *op = &state->operation; + tz_parser_regs *regs = &state->regs; + uint8_t *capture = state->buffers.capture; // cannot restart after error - if (TZ_IS_ERR(state->errno)) tz_reraise; + if (TZ_IS_ERR(state->errno)) + tz_reraise; // nothing else to do - if (op->frame == NULL) tz_stop(DONE); - - PRINTF("[DEBUG] operation(frame: %d, offset:%d/%d, ilen: %d, olen: %d, " - "step: %s, errno: %s)\n", - (int) (op->frame - op->stack), - (int) state->ofs, - (int) op->stack[0].stop, - (int) regs->ilen, - (int) regs->oofs, - (const char*) - PIC(tz_operation_parser_step_name[op->frame->step]), - tz_parser_result_name(state->errno)); + if (op->frame == NULL) + tz_stop(DONE); + + PRINTF( + "[DEBUG] operation(frame: %d, offset:%d/%d, ilen: %d, olen: %d, " + "step: %s, errno: %s)\n", + (int)(op->frame - op->stack), (int)state->ofs, (int)op->stack[0].stop, + (int)regs->ilen, (int)regs->oofs, + (const char *)PIC(tz_operation_parser_step_name[op->frame->step]), + tz_parser_result_name(state->errno)); switch (op->frame->step) { case TZ_OPERATION_STEP_MAGIC: { uint8_t b; tz_must(tz_parser_read(state, &b)); switch (b) { - case 3: // manager/anonymous operation + case 3: // manager/anonymous operation strcpy(state->field_name, "Branch"); op->stack[0].step = TZ_OPERATION_STEP_BRANCH; - push_frame(state, TZ_OPERATION_STEP_READ_BYTES); // ignore result, - // assume success + push_frame(state, + TZ_OPERATION_STEP_READ_BYTES); // ignore result, + // assume success op->frame->step_read_bytes.kind = TZ_OPERATION_FIELD_BH; op->frame->step_read_bytes.skip = true; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 32; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 32; break; - case 5: // micheline expression + case 5: // micheline expression op->frame->step = TZ_OPERATION_STEP_READ_MICHELINE; op->frame->step_read_micheline.inited = 0; - op->frame->step_read_micheline.skip = false; - op->frame->step_read_micheline.name = - (char*) PIC(expression_name); + op->frame->step_read_micheline.skip = false; + op->frame->step_read_micheline.name + = (char *)PIC(expression_name); op->frame->stop = 0; break; default: tz_raise(INVALID_TAG); } - break; - } + break; + } case TZ_OPERATION_STEP_SIZE: { uint8_t b; tz_must(tz_parser_read(state, &b)); if (op->frame->step_size.size > 255) - tz_raise(TOO_LARGE); // enforce 16-bit restriction + tz_raise(TOO_LARGE); // enforce 16-bit restriction op->frame->step_size.size = op->frame->step_size.size << 8 | b; op->frame->step_size.size_len--; if (op->frame->step_size.size_len <= 0) { - op->frame[-1].stop = - state->ofs + op->frame->step_size.size; + op->frame[-1].stop = state->ofs + op->frame->step_size.size; tz_must(pop_frame(state)); } break; } case TZ_OPERATION_STEP_TAG: { - const tz_operation_descriptor* d; - uint8_t t; + const tz_operation_descriptor *d; + uint8_t t; tz_must(tz_parser_read(state, &t)); - for (d = tz_operation_descriptors; d->tag != TZ_OPERATION_TAG_END; d++) { + for (d = tz_operation_descriptors; d->tag != TZ_OPERATION_TAG_END; + d++) { if (d->tag == t) { op->frame->step = TZ_OPERATION_STEP_OPERATION; op->frame->step_operation.descriptor = d; - op->frame->step_operation.field = 0; + op->frame->step_operation.field = 0; tz_must(push_frame(state, TZ_OPERATION_STEP_PRINT)); snprintf(state->field_name, 30, "Operation (%d)", op->batch_index); @@ -389,24 +393,23 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_continue; } } - tz_raise(INVALID_TAG); + tz_raise(INVALID_TAG); } case TZ_OPERATION_STEP_READ_MICHELINE: { if (!op->frame->step_read_micheline.inited) { op->frame->step_read_micheline.inited = 1; - strcpy(state->field_name, - op->frame->step_read_micheline.name); + strcpy(state->field_name, op->frame->step_read_micheline.name); tz_micheline_parser_init(state); } tz_micheline_parser_step(state); if (state->errno == TZ_BLO_DONE) { - if (op->frame->stop != 0 && - state->ofs != op->frame->stop) + if (op->frame->stop != 0 && state->ofs != op->frame->stop) tz_raise(TOO_LARGE); tz_must(pop_frame(state)); if (regs->oofs > 0) tz_stop(IM_FULL); - else tz_continue; + else + tz_continue; } tz_reraise; } @@ -421,14 +424,15 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_must(pop_frame(state)); tz_continue; } - char *str = state->buffers.num.decimal; + char *str = state->buffers.num.decimal; op->frame->step = TZ_OPERATION_STEP_PRINT; switch (op->frame->step_read_num.kind) { case TZ_OPERATION_FIELD_INT: case TZ_OPERATION_FIELD_NAT: break; case TZ_OPERATION_FIELD_AMOUNT: { int len = 0; - while (str[len])len++; + while (str[len]) + len++; if (len == 1 && str[0] == 0) // just 0 goto add_currency; @@ -436,29 +440,36 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { // less than one tez, pad left up to the '0.' int j; int pad = 7 - len; - for (j=len;j>=0;j--) str[j+pad] = str[j]; - for (j=0;j= 0; j--) + str[j + pad] = str[j]; + for (j = 0; j < pad; j++) + str[j] = '0'; len = 7; } int no_decimals = 1; - for (int i=0;i<6;i++) no_decimals &= (str[len-1-i] == '0'); + for (int i = 0; i < 6; i++) + no_decimals &= (str[len - 1 - i] == '0'); if (no_decimals) { // integral value, don't include the decimal part (no '.'_ str[len - 6] = 0; len -= 6; } else { // more than one tez, add the '.' - for (int i=0;i<6;i++) str[len-i] = str[len-i-1]; + for (int i = 0; i < 6; i++) + str[len - i] = str[len - i - 1]; str[len - 6] = '.'; len++; str[len] = 0; // drop trailing non significant zeroes - while (str[len-1] == '0') { len--; str[len] = 0; } + while (str[len - 1] == '0') { + len--; + str[len] = 0; + } } - add_currency: - str[len] = ' '; - str[len+1] = 't'; - str[len+2] = 'z'; + add_currency: + str[len] = ' '; + str[len + 1] = 't'; + str[len + 2] = 'z'; len += 3; str[len] = 0; break; @@ -471,18 +482,17 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { } case TZ_OPERATION_STEP_READ_INT32: { uint32_t value = 0; - uint8_t b; + uint8_t b; for (int ofs = 0; ofs < 4; ofs++) { tz_must(tz_parser_read(state, &b)); value = value << 8 | b; } - snprintf((char*) capture, 30, "%d", value); + snprintf((char *)capture, 30, "%d", value); tz_must(tz_print_string(state)); break; } case TZ_OPERATION_STEP_READ_BYTES: { - if (op->frame->step_read_bytes.ofs < - op->frame->step_read_bytes.len) { + if (op->frame->step_read_bytes.ofs < op->frame->step_read_bytes.len) { uint8_t *c; c = &capture[op->frame->step_read_bytes.ofs]; tz_must(tz_parser_read(state, c)); @@ -497,43 +507,46 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { memcpy(op->source, capture, 22); __attribute__((fallthrough)); case TZ_OPERATION_FIELD_PKH: - if (tz_format_pkh(capture, 21, (char*) capture)) + if (tz_format_pkh(capture, 21, (char *)capture)) tz_raise(INVALID_TAG); break; case TZ_OPERATION_FIELD_PK: if (tz_format_pk(capture, op->frame->step_read_bytes.len, - (char*) capture)) + (char *)capture)) tz_raise(INVALID_TAG); break; case TZ_OPERATION_FIELD_SR: - if (tz_format_base58check("sr1", capture, 20, (char*) capture)) + if (tz_format_base58check("sr1", capture, 20, + (char *)capture)) tz_raise(INVALID_TAG); break; case TZ_OPERATION_FIELD_SRC: - if (tz_format_base58check("src1", capture, 32, (char*) capture)) + if (tz_format_base58check("src1", capture, 32, + (char *)capture)) tz_raise(INVALID_TAG); break; case TZ_OPERATION_FIELD_PROTO: - if (tz_format_base58check("proto", capture, 32, (char*) capture)) + if (tz_format_base58check("proto", capture, 32, + (char *)capture)) tz_raise(INVALID_TAG); break; case TZ_OPERATION_FIELD_DESTINATION: memcpy(op->destination, capture, 22); - if (tz_format_address(capture, 22, (char*) capture)) + if (tz_format_address(capture, 22, (char *)capture)) tz_raise(INVALID_TAG); break; case TZ_OPERATION_FIELD_OPH: - if (tz_format_oph(capture, 32, (char*) capture)) + if (tz_format_oph(capture, 32, (char *)capture)) tz_raise(INVALID_TAG); break; case TZ_OPERATION_FIELD_BH: - if (tz_format_bh(capture, 32, (char*) capture)) + if (tz_format_bh(capture, 32, (char *)capture)) tz_raise(INVALID_TAG); break; default: tz_raise(INVALID_STATE); } - op->frame->step = TZ_OPERATION_STEP_PRINT; - op->frame->step_print.str = (char*) capture; + op->frame->step = TZ_OPERATION_STEP_PRINT; + op->frame->step_print.str = (char *)capture; } break; } @@ -569,17 +582,18 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { if (state->ofs == op->frame->stop) { capture[op->frame->step_read_string.ofs] = 0; tz_must(tz_print_string(state)); - } else if (op->frame->step_read_string.ofs + 2 >= TZ_CAPTURE_BUFFER_SIZE) { + } else if (op->frame->step_read_string.ofs + 2 + >= TZ_CAPTURE_BUFFER_SIZE) { capture[op->frame->step_read_string.ofs] = 0; - op->frame->step_read_string.ofs = 0; + op->frame->step_read_string.ofs = 0; if (!op->frame->step_read_string.skip) { tz_must(push_frame(state, TZ_OPERATION_STEP_PARTIAL_PRINT)); - op->frame->step_print.str = (char*) capture; + op->frame->step_print.str = (char *)capture; } } else { uint8_t b; tz_must(tz_parser_read(state, &b)); - char* buf = (char*) capture + op->frame->step_read_string.ofs; + char *buf = (char *)capture + op->frame->step_read_string.ofs; snprintf(buf, 4, "%02x", b); op->frame->step_read_string.ofs += 2; } @@ -590,34 +604,34 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_must(tz_parser_read(state, &b)); switch (b) { case 0: - strcpy((char*) capture, "default"); + strcpy((char *)capture, "default"); tz_must(tz_print_string(state)); break; case 1: - strcpy((char*) capture, "root"); + strcpy((char *)capture, "root"); tz_must(tz_print_string(state)); break; case 2: - strcpy((char*) capture, "do"); + strcpy((char *)capture, "do"); tz_must(tz_print_string(state)); break; case 3: - strcpy((char*) capture, "set_delegate"); + strcpy((char *)capture, "set_delegate"); tz_must(tz_print_string(state)); break; case 4: - strcpy((char*) capture, "remove_delegate"); + strcpy((char *)capture, "remove_delegate"); tz_must(tz_print_string(state)); break; case 5: - strcpy((char*) capture, "deposit"); + strcpy((char *)capture, "deposit"); tz_must(tz_print_string(state)); break; case 0xFF: - op->frame->step = TZ_OPERATION_STEP_READ_STRING; + op->frame->step = TZ_OPERATION_STEP_READ_STRING; op->frame->step_read_string.ofs = 0; tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_size.size = 0; op->frame->step_size.size_len = 1; break; default: tz_raise(INVALID_TAG); @@ -625,10 +639,10 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { break; } case TZ_OPERATION_STEP_OPERATION: { - const tz_operation_descriptor *d = - op->frame->step_operation.descriptor; - const tz_operation_field_descriptor *field = - PIC(&d->fields[op->frame->step_operation.field]); + const tz_operation_descriptor *d + = op->frame->step_operation.descriptor; + const tz_operation_field_descriptor *field + = PIC(&d->fields[op->frame->step_operation.field]); const char *name = PIC(field->name); // Remaining content from previous section - display this first. @@ -646,19 +660,20 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { op->frame->step_operation.field++; if (!present) { if (field->display_none) { - if (field->skip) tz_raise(INVALID_STATE); + if (field->skip) + tz_raise(INVALID_STATE); tz_must(push_frame(state, TZ_OPERATION_STEP_PRINT)); - op->frame->step_print.str = (char*) unset_message; + op->frame->step_print.str = (char *)unset_message; } tz_continue; } switch (field->kind) { case TZ_OPERATION_FIELD_BINARY: { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_BINARY)); - op->frame->step_read_string.ofs = 0; + op->frame->step_read_string.ofs = 0; op->frame->step_read_string.skip = field->skip; tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_size.size = 0; op->frame->step_size.size_len = 4; break; } @@ -667,8 +682,8 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_BYTES)); op->frame->step_read_bytes.kind = field->kind; op->frame->step_read_bytes.skip = field->skip; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 21; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 21; break; } case TZ_OPERATION_FIELD_PK: { @@ -680,33 +695,33 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_BYTES)); op->frame->step_read_bytes.kind = field->kind; op->frame->step_read_bytes.skip = field->skip; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 20; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 20; break; } case TZ_OPERATION_FIELD_SRC: { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_BYTES)); op->frame->step_read_bytes.kind = field->kind; op->frame->step_read_bytes.skip = field->skip; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 32; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 32; break; } case TZ_OPERATION_FIELD_PROTO: { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_BYTES)); op->frame->step_read_bytes.kind = field->kind; op->frame->step_read_bytes.skip = field->skip; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 32; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 32; break; } case TZ_OPERATION_FIELD_PROTOS: { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_PROTOS)); - op->frame->step_read_list.name = name; + op->frame->step_read_list.name = name; op->frame->step_read_list.index = 0; - op->frame->step_read_list.skip = field->skip; - tz_must (push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_read_list.skip = field->skip; + tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); + op->frame->step_size.size = 0; op->frame->step_size.size_len = 4; break; } @@ -714,8 +729,8 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_BYTES)); op->frame->step_read_bytes.kind = field->kind; op->frame->step_read_bytes.skip = field->skip; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 22; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 22; break; } case TZ_OPERATION_FIELD_NAT: @@ -723,8 +738,8 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_NUM)); tz_parse_num_state_init(&state->buffers.num, &op->frame->step_read_num.state); - op->frame->step_read_num.kind = field->kind; - op->frame->step_read_num.skip = field->skip; + op->frame->step_read_num.kind = field->kind; + op->frame->step_read_num.skip = field->skip; op->frame->step_read_num.natural = 1; break; } @@ -732,8 +747,8 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_NUM)); tz_parse_num_state_init(&state->buffers.num, &op->frame->step_read_num.state); - op->frame->step_read_num.kind = field->kind; - op->frame->step_read_num.skip = field->skip; + op->frame->step_read_num.kind = field->kind; + op->frame->step_read_num.skip = field->skip; op->frame->step_read_num.natural = 0; break; } @@ -745,43 +760,45 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { case TZ_OPERATION_FIELD_PARAMETER: { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_MICHELINE)); op->frame->step_read_micheline.inited = 0; - op->frame->step_read_micheline.skip = field->skip; - op->frame->step_read_micheline.name = name; + op->frame->step_read_micheline.skip = field->skip; + op->frame->step_read_micheline.name = name; tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_size.size = 0; op->frame->step_size.size_len = 4; - tz_must(push_frame(state, TZ_OPERATION_STEP_READ_SMART_ENTRYPOINT)); + tz_must(push_frame(state, + TZ_OPERATION_STEP_READ_SMART_ENTRYPOINT)); strcpy(state->field_name, "Entrypoint"); - op->frame->step_read_string.ofs = 0; + op->frame->step_read_string.ofs = 0; op->frame->step_read_string.skip = field->skip; break; } case TZ_OPERATION_FIELD_EXPR: { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_MICHELINE)); op->frame->step_read_micheline.inited = 0; - op->frame->step_read_micheline.skip = field->skip; - op->frame->step_read_micheline.name = name; + op->frame->step_read_micheline.skip = field->skip; + op->frame->step_read_micheline.name = name; tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_size.size = 0; op->frame->step_size.size_len = 4; break; } case TZ_OPERATION_FIELD_STRING: { tz_must(push_frame(state, TZ_OPERATION_STEP_READ_STRING)); - op->frame->step_read_string.ofs = 0; + op->frame->step_read_string.ofs = 0; op->frame->step_read_string.skip = field->skip; tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_size.size = 0; op->frame->step_size.size_len = 4; break; } case TZ_OPERATION_FIELD_SORU_MESSAGES: { - tz_must(push_frame(state, TZ_OPERATION_STEP_READ_SORU_MESSAGES)); - op->frame->step_read_list.name = name; + tz_must( + push_frame(state, TZ_OPERATION_STEP_READ_SORU_MESSAGES)); + op->frame->step_read_list.name = name; op->frame->step_read_list.index = 0; - op->frame->step_read_list.skip = field->skip; + op->frame->step_read_list.skip = field->skip; tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_size.size = 0; op->frame->step_size.size_len = 4; break; } @@ -804,30 +821,29 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { uint8_t b; tz_must(tz_parser_peek(state, &b)); op->frame->step_read_bytes.kind = TZ_OPERATION_FIELD_PK; - op->frame->step_read_bytes.ofs = 0; - switch (b){ - case 0: // edpk + op->frame->step_read_bytes.ofs = 0; + switch (b) { + case 0: // edpk op->frame->step_read_bytes.len = 33; break; - case 1: // sppk + case 1: // sppk op->frame->step_read_bytes.len = 34; break; - case 2: // p2pk + case 2: // p2pk op->frame->step_read_bytes.len = 34; break; - case 3: // BLpk + case 3: // BLpk op->frame->step_read_bytes.len = 49; break; - default: - tz_raise(INVALID_TAG); + default: tz_raise(INVALID_TAG); } op->frame->step = TZ_OPERATION_STEP_READ_BYTES; break; } case TZ_OPERATION_STEP_READ_SORU_MESSAGES: { - uint8_t skip = op->frame->step_read_list.skip; - const char* name = op->frame->step_read_list.name; - uint16_t index = op->frame->step_read_list.index; + uint8_t skip = op->frame->step_read_list.skip; + const char *name = op->frame->step_read_list.name; + uint16_t index = op->frame->step_read_list.index; // Remaining content from previous message - display this first. if (regs->oofs > 0) @@ -838,11 +854,12 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { } else { op->frame->step_read_list.index++; tz_must(push_frame(state, TZ_OPERATION_STEP_READ_BINARY)); - snprintf(state->field_name, TZ_FIELD_NAME_SIZE, "%s (%d)", name, index); - op->frame->step_read_string.ofs = 0; + snprintf(state->field_name, TZ_FIELD_NAME_SIZE, "%s (%d)", name, + index); + op->frame->step_read_string.ofs = 0; op->frame->step_read_string.skip = skip; tz_must(push_frame(state, TZ_OPERATION_STEP_SIZE)); - op->frame->step_size.size = 0; + op->frame->step_size.size = 0; op->frame->step_size.size_len = 4; } break; @@ -850,13 +867,9 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { case TZ_OPERATION_STEP_READ_SORU_KIND: { uint8_t b; tz_must(tz_parser_read(state, &b)); - switch(b) { - case 0: - strcpy((char*) capture, "arith"); - break; - case 1: - strcpy((char*) capture, "wasm_2_0_0"); - break; + switch (b) { + case 0: strcpy((char *)capture, "arith"); break; + case 1: strcpy((char *)capture, "wasm_2_0_0"); break; default: tz_raise(INVALID_TAG); } tz_must(tz_print_string(state)); @@ -865,25 +878,19 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { case TZ_OPERATION_STEP_READ_BALLOT: { uint8_t b; tz_must(tz_parser_read(state, &b)); - switch(b) { - case 0: - strcpy((char*) capture, "yay"); - break; - case 1: - strcpy((char*) capture, "nay"); - break; - case 2: - strcpy((char*) capture, "pass"); - break; + switch (b) { + case 0: strcpy((char *)capture, "yay"); break; + case 1: strcpy((char *)capture, "nay"); break; + case 2: strcpy((char *)capture, "pass"); break; default: tz_raise(INVALID_TAG); } tz_must(tz_print_string(state)); break; } case TZ_OPERATION_STEP_READ_PROTOS: { - uint8_t skip = op->frame->step_read_list.skip; - const char* name = op->frame->step_read_list.name; - uint16_t index = op->frame->step_read_list.index; + uint8_t skip = op->frame->step_read_list.skip; + const char *name = op->frame->step_read_list.name; + uint16_t index = op->frame->step_read_list.index; // Remaining content from previous proto - display this first. if (regs->oofs > 0) @@ -897,13 +904,13 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { snprintf(state->field_name, 30, "%s (%d)", name, index); op->frame->step_read_bytes.kind = TZ_OPERATION_FIELD_PROTO; op->frame->step_read_bytes.skip = skip; - op->frame->step_read_bytes.ofs = 0; - op->frame->step_read_bytes.len = 32; + op->frame->step_read_bytes.ofs = 0; + op->frame->step_read_bytes.len = 32; } break; } case TZ_OPERATION_STEP_PRINT: { - const char* str = PIC(op->frame->step_print.str); + const char *str = PIC(op->frame->step_print.str); if (*str) { tz_must(tz_parser_put(state, *str)); op->frame->step_print.str++; @@ -914,7 +921,7 @@ tz_parser_result tz_operation_parser_step(tz_parser_state *state) { break; } case TZ_OPERATION_STEP_PARTIAL_PRINT: { - const char* str = PIC(op->frame->step_print.str); + const char *str = PIC(op->frame->step_print.str); if (*str) { tz_must(tz_parser_put(state, *str)); op->frame->step_print.str++; diff --git a/app/src/parser/operation_parser.h b/app/src/parser/operation_parser.h index eba79def5..fb5004eb1 100644 --- a/app/src/parser/operation_parser.h +++ b/app/src/parser/operation_parser.h @@ -19,6 +19,6 @@ #include "parser_state.h" #define TZ_UNKNOWN_SIZE 0xFFFF -void tz_operation_parser_init(tz_parser_state *, uint16_t, bool); -void tz_operation_parser_set_size(tz_parser_state *, uint16_t); +void tz_operation_parser_init(tz_parser_state *, uint16_t, bool); +void tz_operation_parser_set_size(tz_parser_state *, uint16_t); tz_parser_result tz_operation_parser_step(tz_parser_state *); diff --git a/app/src/parser/operation_state.h b/app/src/parser/operation_state.h index 5a606322a..96a4e3661 100644 --- a/app/src/parser/operation_state.h +++ b/app/src/parser/operation_state.h @@ -20,10 +20,10 @@ #include "num_state.h" typedef enum { - TZ_OPERATION_TAG_END = 0, - TZ_OPERATION_TAG_PROPOSALS = 5, - TZ_OPERATION_TAG_BALLOT = 6, - TZ_OPERATION_TAG_FAILING_NOOP = 17, + TZ_OPERATION_TAG_END = 0, + TZ_OPERATION_TAG_PROPOSALS = 5, + TZ_OPERATION_TAG_BALLOT = 6, + TZ_OPERATION_TAG_FAILING_NOOP = 17, TZ_OPERATION_TAG_REVEAL = 107, TZ_OPERATION_TAG_TRANSACTION = 108, TZ_OPERATION_TAG_ORIGINATION = 109, @@ -62,7 +62,7 @@ typedef enum { } tz_operation_parser_step_kind; typedef enum { - TZ_OPERATION_FIELD_SKIP, // not for use in field descriptors + TZ_OPERATION_FIELD_SKIP, // not for use in field descriptors TZ_OPERATION_FIELD_BINARY, TZ_OPERATION_FIELD_INT, TZ_OPERATION_FIELD_NAT, @@ -86,58 +86,57 @@ typedef enum { TZ_OPERATION_FIELD_BALLOT } tz_operation_field_kind; - typedef struct { - const char *name; + const char *name; tz_operation_field_kind kind : 5; - uint8_t required : 1, skip : 1, display_none : 1; + uint8_t required : 1, skip : 1, display_none : 1; } tz_operation_field_descriptor; typedef struct { - tz_operation_tag tag; - const char *name; + tz_operation_tag tag; + const char *name; const tz_operation_field_descriptor *fields; } tz_operation_descriptor; typedef struct { tz_operation_parser_step_kind step : 5; - uint16_t stop; + uint16_t stop; union { struct { - uint8_t size_len; + uint8_t size_len; uint16_t size; } step_size; struct { const tz_operation_descriptor *descriptor; - uint8_t field; + uint8_t field; } step_operation; struct { const char *str; } step_print; struct { - uint16_t ofs; - uint16_t len; + uint16_t ofs; + uint16_t len; tz_operation_field_kind kind : 5; - uint8_t skip : 1; + uint8_t skip : 1; } step_read_bytes; struct { - tz_num_parser_regs state; + tz_num_parser_regs state; tz_operation_field_kind kind : 5; - uint8_t skip : 1, natural : 1; + uint8_t skip : 1, natural : 1; } step_read_num; struct { uint16_t ofs; - uint8_t skip : 1; + uint8_t skip : 1; } step_read_string; struct { const char *name; - uint8_t inited : 1; - uint8_t skip : 1; + uint8_t inited : 1; + uint8_t skip : 1; } step_read_micheline; struct { const char *name; - uint16_t index; - uint8_t skip : 1; + uint16_t index; + uint8_t skip : 1; } step_read_list; }; } tz_operation_parser_frame; @@ -145,10 +144,10 @@ typedef struct { #define TZ_OPERATION_STACK_DEPTH 6 typedef struct { - tz_operation_parser_frame stack[TZ_OPERATION_STACK_DEPTH]; - tz_operation_parser_frame *frame; // init == stack, NULL when done - uint8_t seen_reveal : 1; // check at most one reveal - uint8_t source[22]; // check consistent source in batch - uint8_t destination[22]; // saved for entrypoint dispatch - uint16_t batch_index; // to print a sequence number + tz_operation_parser_frame stack[TZ_OPERATION_STACK_DEPTH]; + tz_operation_parser_frame *frame; // init == stack, NULL when done + uint8_t seen_reveal : 1; // check at most one reveal + uint8_t source[22]; // check consistent source in batch + uint8_t destination[22]; // saved for entrypoint dispatch + uint16_t batch_index; // to print a sequence number } tz_operation_state; diff --git a/app/src/parser/parser_state.c b/app/src/parser/parser_state.c index f98f68fdc..6ded4deec 100644 --- a/app/src/parser/parser_state.c +++ b/app/src/parser/parser_state.c @@ -1,4 +1,5 @@ -/* Tezos Embedded C parser for Ledger - Full parser state definition and helpers +/* Tezos Embedded C parser for Ledger - Full parser state definition and + helpers Copyright 2023 Nomadic Labs Copyright 2023 TriliTech @@ -17,10 +18,13 @@ #include "parser_state.h" -#define TZ_LABEL(_x) case TZ_ ## _x: return #_x -#define BLO_LABEL(_x) case TZ_BLO_ ## _x: return #_x +#define TZ_LABEL(_x) \ + case TZ_##_x: return #_x +#define BLO_LABEL(_x) \ + case TZ_BLO_##_x: return #_x -const char* tz_parser_result_name(tz_parser_result code) { +const char *tz_parser_result_name(tz_parser_result code) +{ // clang-format off switch (code) { TZ_LABEL(CONTINUE); @@ -40,18 +44,21 @@ const char* tz_parser_result_name(tz_parser_result code) { // clang-format on } -void tz_parser_init(tz_parser_state *state) { - state->errno = TZ_CONTINUE; - state->ofs = 0; +void tz_parser_init(tz_parser_state *state) +{ + state->errno = TZ_CONTINUE; + state->ofs = 0; state->field_name[0] = 0; } -void tz_parser_flush(tz_parser_state *st, char *obuf, size_t olen) { +void tz_parser_flush(tz_parser_state *st, char *obuf, size_t olen) +{ tz_parser_flush_up_to(st, obuf, olen, olen); } void tz_parser_flush_up_to(tz_parser_state *st, char *obuf, size_t olen, - size_t up_to) { + size_t up_to) +{ tz_parser_regs *regs = &st->regs; regs->obuf = obuf; @@ -66,7 +73,8 @@ void tz_parser_flush_up_to(tz_parser_state *st, char *obuf, size_t olen, memset(regs->obuf + regs->oofs, 0x0, regs->olen); } -void tz_parser_refill(tz_parser_state *st, uint8_t *ibuf, size_t ilen) { +void tz_parser_refill(tz_parser_state *st, uint8_t *ibuf, size_t ilen) +{ tz_parser_regs *regs = &st->regs; regs->ibuf = ibuf; @@ -75,40 +83,48 @@ void tz_parser_refill(tz_parser_state *st, uint8_t *ibuf, size_t ilen) { } tz_parser_result tz_parser_set_errno(tz_parser_state *state, - tz_parser_result code) { + tz_parser_result code) +{ state->errno = ((code == TZ_BREAK) ? TZ_CONTINUE : code); return code; } -tz_parser_result tz_parser_put(tz_parser_state *state, char c) { - tz_parser_regs *regs = &state->regs; +tz_parser_result tz_parser_put(tz_parser_state *state, char c) +{ + tz_parser_regs *regs = &state->regs; - if (regs->olen<1) tz_stop(IM_FULL); + if (regs->olen < 1) + tz_stop(IM_FULL); regs->obuf[regs->oofs] = c; regs->oofs++; regs->olen--; tz_continue; } -tz_parser_result tz_parser_read(tz_parser_state *state, uint8_t *r) { +tz_parser_result tz_parser_read(tz_parser_state *state, uint8_t *r) +{ tz_parser_regs *regs = &state->regs; - if (regs->ilen<1) tz_stop(FEED_ME); + if (regs->ilen < 1) + tz_stop(FEED_ME); state->ofs++; regs->ilen--; *r = regs->ibuf[regs->iofs++]; tz_continue; } -tz_parser_result tz_parser_peek(tz_parser_state *state, uint8_t *r) { +tz_parser_result tz_parser_peek(tz_parser_state *state, uint8_t *r) +{ tz_parser_regs *regs = &state->regs; - if (regs->ilen<1) tz_stop(FEED_ME); + if (regs->ilen < 1) + tz_stop(FEED_ME); *r = regs->ibuf[regs->iofs]; tz_continue; } -void tz_parser_skip(tz_parser_state *state) { +void tz_parser_skip(tz_parser_state *state) +{ tz_parser_regs *regs = &state->regs; regs->iofs++; diff --git a/app/src/parser/parser_state.h b/app/src/parser/parser_state.h index 0f57df7fb..2ff16d4cc 100644 --- a/app/src/parser/parser_state.h +++ b/app/src/parser/parser_state.h @@ -1,4 +1,5 @@ -/* Tezos Embedded C parser for Ledger - Full parser state definition and helpers +/* Tezos Embedded C parser for Ledger - Full parser state definition and + helpers Copyright 2023 Nomadic Labs Copyright 2023 TriliTech @@ -27,30 +28,30 @@ typedef struct { // update `ibuf`, `iofs` and `ilen` at once with `parser_regs_refill` // invariant between two refills: // `iofs + ilen` = (constant) number of readable bytes in `ibuf` - uint8_t* ibuf; // input buffer - size_t iofs; // current offset - size_t ilen; // remaining bytes readable in input + uint8_t *ibuf; // input buffer + size_t iofs; // current offset + size_t ilen; // remaining bytes readable in input // update `obuf`, `oofs` and `olen` at once with `parser_regs_flush` // invariant between two refills: // `oofs + olen` = (constant) number of readable bytes in `obuf` - char* obuf; // output buffer - size_t oofs; // current offset - size_t olen; // remaining bytes writable in output + char *obuf; // output buffer + size_t oofs; // current offset + size_t olen; // remaining bytes writable in output } tz_parser_regs; // Parser state -#define TZ_FIELD_NAME_SIZE 80 +#define TZ_FIELD_NAME_SIZE 80 #define TZ_CAPTURE_BUFFER_SIZE 256 typedef enum { // success and non blocking, should loop again - TZ_CONTINUE = 0, // fall through rest of current step - TZ_BREAK, // signals caller to return, errno should be CONTINUE + TZ_CONTINUE = 0, // fall through rest of current step + TZ_BREAK, // signals caller to return, errno should be CONTINUE // success but parsing blocked - TZ_BLO_DONE = 100, // parsing complete - TZ_BLO_FEED_ME, // blocked on read from input - TZ_BLO_IM_FULL, // blocked on output space + TZ_BLO_DONE = 100, // parsing complete + TZ_BLO_FEED_ME, // blocked on read from input + TZ_BLO_IM_FULL, // blocked on output space // everything below is an error TZ_ERR_INVALID_TAG = 200, TZ_ERR_INVALID_OP, @@ -60,21 +61,21 @@ typedef enum { TZ_ERR_INVALID_STATE, } tz_parser_result; #define TZ_IS_BLOCKED(code) (code >= 100) -#define TZ_IS_ERR(code) (code >= 200) +#define TZ_IS_ERR(code) (code >= 200) -const char* tz_parser_result_name(tz_parser_result); +const char *tz_parser_result_name(tz_parser_result); typedef struct { tz_parser_regs regs; // common fields to communicate with caller tz_parser_result errno; - char field_name[TZ_FIELD_NAME_SIZE]; + char field_name[TZ_FIELD_NAME_SIZE]; // common singleton buffers int ofs; struct { tz_num_parser_buffer num; - uint8_t capture[TZ_CAPTURE_BUFFER_SIZE]; + uint8_t capture[TZ_CAPTURE_BUFFER_SIZE]; } buffers; // input type specific state tz_micheline_state micheline; @@ -83,7 +84,7 @@ typedef struct { void tz_parser_init(tz_parser_state *); void tz_parser_flush(tz_parser_state *, char *, size_t); -void tz_parser_flush_up_to(tz_parser_state *, char*, size_t, size_t); +void tz_parser_flush_up_to(tz_parser_state *, char *, size_t, size_t); void tz_parser_refill(tz_parser_state *, uint8_t *, size_t); void tz_parser_skip(tz_parser_state *); @@ -95,24 +96,26 @@ tz_parser_result tz_parser_peek(tz_parser_state *, uint8_t *); tz_parser_result tz_parser_set_errno(tz_parser_state *, tz_parser_result); #ifdef TEZOS_DEBUG -# define tz_return(e) do { \ - tz_parser_result _c = (e); \ - if (_c) { \ - PRINTF ("[DEBUG] tz_return(code: %s, loc: %s:%d)\n", \ - tz_parser_result_name(_c), __FILE__, __LINE__); \ - } \ - return tz_parser_set_errno(state, _c); \ - } while (0) +#define tz_return(e) \ + do { \ + tz_parser_result _c = (e); \ + if (_c) { \ + PRINTF("[DEBUG] tz_return(code: %s, loc: %s:%d)\n", \ + tz_parser_result_name(_c), __FILE__, __LINE__); \ + } \ + return tz_parser_set_errno(state, _c); \ + } while (0) #else -# define tz_return(e) return tz_parser_set_errno(state, e) +#define tz_return(e) return tz_parser_set_errno(state, e) #endif #define tz_raise(e) tz_return(TZ_ERR_##e) -#define tz_stop(e) tz_return(TZ_BLO_##e) -#define tz_reraise return state->errno -#define tz_must(cond) do { \ - tz_parser_result _err = cond; \ - if (_err) \ - tz_return(_err); \ - } while (0) +#define tz_stop(e) tz_return(TZ_BLO_##e) +#define tz_reraise return state->errno +#define tz_must(cond) \ + do { \ + tz_parser_result _err = cond; \ + if (_err) \ + tz_return(_err); \ + } while (0) #define tz_continue tz_return(TZ_CONTINUE) -#define tz_break tz_return(TZ_BREAK) +#define tz_break tz_return(TZ_BREAK) diff --git a/app/src/ui_commons.h b/app/src/ui_commons.h index 903548d2e..4e4e5f285 100644 --- a/app/src/ui_commons.h +++ b/app/src/ui_commons.h @@ -26,16 +26,16 @@ #include #include -#define DISPLAY(elts, cb)\ - memcpy(global.ux.bagls, elts, sizeof(elts)); \ +#define DISPLAY(elts, cb) \ + memcpy(global.ux.bagls, elts, sizeof(elts)); \ G_ux.stack[0].element_arrays[0].element_array = global.ux.bagls; \ - G_ux.stack[0].element_arrays[0].element_array_count = \ - sizeof(elts)/sizeof(bagl_element_t); \ - G_ux.stack[0].button_push_callback = cb; \ - G_ux.stack[0].screen_before_element_display_callback = NULL; \ - UX_WAKE_UP(); \ + G_ux.stack[0].element_arrays[0].element_array_count \ + = sizeof(elts) / sizeof(bagl_element_t); \ + G_ux.stack[0].button_push_callback = cb; \ + G_ux.stack[0].screen_before_element_display_callback = NULL; \ + UX_WAKE_UP(); \ UX_REDISPLAY(); #define REGULAR BAGL_FONT_OPEN_SANS_REGULAR_11px | BAGL_FONT_ALIGNMENT_CENTER -#define BOLD BAGL_FONT_OPEN_SANS_EXTRABOLD_11px | BAGL_FONT_ALIGNMENT_CENTER -#endif // HAVE_BAGL +#define BOLD BAGL_FONT_OPEN_SANS_EXTRABOLD_11px | BAGL_FONT_ALIGNMENT_CENTER +#endif // HAVE_BAGL diff --git a/app/src/ui_home.c b/app/src/ui_home.c index f1b646de5..318aa865b 100644 --- a/app/src/ui_home.c +++ b/app/src/ui_home.c @@ -28,48 +28,49 @@ static void cb(tz_ui_cb_type_t); - -static void cb(tz_ui_cb_type_t type) { +static void cb(tz_ui_cb_type_t type) +{ FUNC_ENTER(("type=%u\n", type)); switch (type) { case SCREEN_BLIND_SIGN: - case SCREEN_CLEAR_SIGN: - break; - case SCREEN_SETTINGS: - ui_settings_init(); - break; - case SCREEN_QUIT: - app_exit(); + case SCREEN_CLEAR_SIGN: break; + case SCREEN_SETTINGS: ui_settings_init(); break; + case SCREEN_QUIT: app_exit(); } FUNC_LEAVE(); } #ifdef TARGET_NANOS -static void clear_sign_screen(void) { - tz_ui_stream_push(SCREEN_CLEAR_SIGN, "ready for", - "safe signing", TZ_UI_ICON_NONE); +static void clear_sign_screen(void) +{ + tz_ui_stream_push(SCREEN_CLEAR_SIGN, "ready for", "safe signing", + TZ_UI_ICON_NONE); } -static void blind_sign_screen(void) { - tz_ui_stream_push(SCREEN_BLIND_SIGN, "ready for", - "BLIND signing", TZ_UI_ICON_NONE); +static void blind_sign_screen(void) +{ + tz_ui_stream_push(SCREEN_BLIND_SIGN, "ready for", "BLIND signing", + TZ_UI_ICON_NONE); } #else -static void clear_sign_screen(void) { +static void clear_sign_screen(void) +{ tz_ui_stream_push(SCREEN_CLEAR_SIGN, "Tezos Wallet", "ready for\nsafe signing", TZ_UI_ICON_NONE); } -static void blind_sign_screen(void) { +static void blind_sign_screen(void) +{ tz_ui_stream_push(SCREEN_BLIND_SIGN, "Tezos Wallet", "ready for\nBLIND signing", TZ_UI_ICON_NONE); } #endif -#endif // HAVE_BAGL +#endif // HAVE_BAGL -void ui_home_init(void) { +void ui_home_init(void) +{ FUNC_ENTER(("void")); #ifdef HAVE_BAGL tz_ui_stream_init(cb); diff --git a/app/src/ui_home_nbgl.c b/app/src/ui_home_nbgl.c index b2fcb6673..a72df40b9 100644 --- a/app/src/ui_home_nbgl.c +++ b/app/src/ui_home_nbgl.c @@ -30,9 +30,9 @@ void tz_ui_home_redisplay(void); -static const char* const infoTypes[] = {"Version", "Developer", "Copyright"}; -static const char* const infoContents[] = - {APPVERSION, "Tezos", "(c) 2023 "}; +static const char *const infoTypes[] = {"Version", "Developer", "Copyright"}; +static const char *const infoContents[] + = {APPVERSION, "Tezos", "(c) 2023 "}; enum { BLIND_SIGNING_TOKEN = FIRST_USER_TOKEN, @@ -40,37 +40,37 @@ enum { static nbgl_layoutSwitch_t switches[1]; -static bool navigation_cb_wallet(__attribute__((unused))uint8_t page, - nbgl_pageContent_t* content) { +static bool navigation_cb_wallet(__attribute__((unused)) uint8_t page, + nbgl_pageContent_t *content) +{ switch (page) { case 0: - content->type = INFOS_LIST; - content->infosList.nbInfos = 3; - content->infosList.infoTypes = infoTypes; + content->type = INFOS_LIST; + content->infosList.nbInfos = 3; + content->infosList.infoTypes = infoTypes; content->infosList.infoContents = infoContents; break; case 1: - switches[0] = - (nbgl_layoutSwitch_t) - {.initState = N_settings.blindsigning ? ON_STATE : OFF_STATE, - .text = "Blind signing", - .subText = "Enable blindsigning", - .token = BLIND_SIGNING_TOKEN, - .tuneId = TUNE_TAP_CASUAL}; - - content->type = SWITCHES_LIST; + switches[0] = (nbgl_layoutSwitch_t){ + .initState = N_settings.blindsigning ? ON_STATE : OFF_STATE, + .text = "Blind signing", + .subText = "Enable blindsigning", + .token = BLIND_SIGNING_TOKEN, + .tuneId = TUNE_TAP_CASUAL}; + + content->type = SWITCHES_LIST; content->switchesList.nbSwitches = 1; - content->switchesList.switches = (nbgl_layoutSwitch_t*) switches; + content->switchesList.switches = (nbgl_layoutSwitch_t *)switches; break; - default: - return false; + default: return false; } return true; } -static void controls_callback(int token, - __attribute__((unused))uint8_t index) { +static void controls_callback(int token, + __attribute__((unused)) uint8_t index) +{ switch (token) { case BLIND_SIGNING_TOKEN: toggle_blindsigning(); @@ -81,14 +81,16 @@ static void controls_callback(int token, } } -static void ui_menu_about_wallet(void) { +static void ui_menu_about_wallet(void) +{ uint8_t nb_screens = 2; - nbgl_useCaseSettings("Tezos wallet", - 0, nb_screens, false, tz_ui_home_redisplay, - navigation_cb_wallet, controls_callback); + nbgl_useCaseSettings("Tezos wallet", 0, nb_screens, false, + tz_ui_home_redisplay, navigation_cb_wallet, + controls_callback); } -static void ui_toggle_clear_blind(void) { +static void ui_toggle_clear_blind(void) +{ // clang-format off switch (global.home_screen) { case SCREEN_CLEAR_SIGN: global.home_screen = SCREEN_BLIND_SIGN; break; @@ -101,44 +103,35 @@ static void ui_toggle_clear_blind(void) { tz_ui_home_redisplay(); } -void tz_ui_home_redisplay(void) { +void tz_ui_home_redisplay(void) +{ FUNC_ENTER(("void")); if (!N_settings.blindsigning) { - nbgl_useCaseHome("Tezos Wallet", - &C_tezos, - "Ready for signing", - true, - ui_menu_about_wallet, - app_exit); + nbgl_useCaseHome("Tezos Wallet", &C_tezos, "Ready for signing", true, + ui_menu_about_wallet, app_exit); } else { - const char* button_text; - const char* tagline; + const char *button_text; + const char *tagline; switch (global.home_screen) { case SCREEN_CLEAR_SIGN: - tagline = "Ready for clear signing"; + tagline = "Ready for clear signing"; button_text = "blind sign"; break; case SCREEN_BLIND_SIGN: - tagline = "Ready for blind signing"; + tagline = "Ready for blind signing"; button_text = "clear sign"; break; - default: - THROW (EXC_UNEXPECTED_STATE); + default: THROW(EXC_UNEXPECTED_STATE); } - nbgl_useCaseHomeExt("Tezos Wallet", - &C_tezos, - tagline, - true, - button_text, - ui_toggle_clear_blind, - ui_menu_about_wallet, - app_exit); + nbgl_useCaseHomeExt("Tezos Wallet", &C_tezos, tagline, true, + button_text, ui_toggle_clear_blind, + ui_menu_about_wallet, app_exit); } FUNC_LEAVE(); } -#endif // HAVE_NBGL +#endif // HAVE_NBGL diff --git a/app/src/ui_settings.c b/app/src/ui_settings.c index 21386755b..d65ac62e4 100644 --- a/app/src/ui_settings.c +++ b/app/src/ui_settings.c @@ -23,12 +23,10 @@ static void cb(tz_ui_cb_type_t); -#define BLIND_SIGNING 0x01 -#define BACK 0x02 +#define BLIND_SIGNING 0x01 +#define BACK 0x02 - -static void -cb(tz_ui_cb_type_t type) +static void cb(tz_ui_cb_type_t type) { FUNC_ENTER(("type=%u\n", type)); switch (type) { @@ -36,14 +34,11 @@ cb(tz_ui_cb_type_t type) toggle_blindsigning(); ui_settings_init(); break; - case BACK: - ui_home_init(); - break; + case BACK: ui_home_init(); break; } } -void -ui_settings_init(void) +void ui_settings_init(void) { const char *bsigning = "DISABLED"; @@ -53,7 +48,8 @@ ui_settings_init(void) bsigning = "ENABLED"; tz_ui_stream_init(cb); - tz_ui_stream_push(BLIND_SIGNING, "Blind Signing", bsigning, TZ_UI_ICON_NONE); + tz_ui_stream_push(BLIND_SIGNING, "Blind Signing", bsigning, + TZ_UI_ICON_NONE); tz_ui_stream_push(BACK, "Back", "", TZ_UI_ICON_DASHBOARD); tz_ui_stream_close(); tz_ui_stream_start(); diff --git a/app/src/ui_settings.h b/app/src/ui_settings.h index e60109852..31e391fc7 100644 --- a/app/src/ui_settings.h +++ b/app/src/ui_settings.h @@ -4,4 +4,4 @@ #pragma once -void ui_settings_init(void); +void ui_settings_init(void); diff --git a/app/src/ui_stream.c b/app/src/ui_stream.c index 2a9d7b23b..8af4a4ed6 100644 --- a/app/src/ui_stream.c +++ b/app/src/ui_stream.c @@ -21,31 +21,33 @@ #ifdef HAVE_BAGL static unsigned int cb(unsigned int, unsigned int); -static const char *find_icon(tz_ui_icon_t); -static void pred(void); -static void succ(void); -static void change_screen_left(void); -static void change_screen_right(void); -static void redisplay(void); +static const char *find_icon(tz_ui_icon_t); +static void pred(void); +static void succ(void); +static void change_screen_left(void); +static void change_screen_right(void); +static void redisplay(void); -const bagl_icon_details_t C_icon_rien = { 0, 0, 1, NULL, NULL }; -#endif // HAVE_BAGL +const bagl_icon_details_t C_icon_rien = {0, 0, 1, NULL, NULL}; +#endif // HAVE_BAGL // Model -void tz_ui_stream_init(void (*cb)(uint8_t)) { +void tz_ui_stream_init(void (*cb)(uint8_t)) +{ tz_ui_stream_t *s = &global.stream; FUNC_ENTER(("cb=%p", cb)); memset(s, 0x0, sizeof(*s)); - s->cb = cb; - s->full = false; + s->cb = cb; + s->full = false; s->current = 0; - s->total = -1; + s->total = -1; FUNC_LEAVE(); } -void tz_ui_stream_push_accept_reject(void) { +void tz_ui_stream_push_accept_reject(void) +{ FUNC_ENTER(("void")); tz_ui_stream_push(TZ_UI_STREAM_CB_ACCEPT, "Accept?", "Press both buttons to accept.", TZ_UI_ICON_TICK); @@ -54,7 +56,8 @@ void tz_ui_stream_push_accept_reject(void) { FUNC_LEAVE(); } -void tz_ui_stream_close() { +void tz_ui_stream_close() +{ tz_ui_stream_t *s = &global.stream; FUNC_ENTER(("void")); @@ -67,7 +70,8 @@ void tz_ui_stream_close() { } #ifdef HAVE_BAGL -uint8_t tz_ui_max_line_chars(const char* value, int length) { +uint8_t tz_ui_max_line_chars(const char *value, int length) +{ uint8_t will_fit = MIN(TZ_UI_STREAM_CONTENTS_WIDTH, length); FUNC_ENTER(("value=\"%s\", length=%d", value, length)); @@ -77,27 +81,29 @@ uint8_t tz_ui_max_line_chars(const char* value, int length) { if (tmp && (tmp - value) <= will_fit) will_fit = (tmp - value); -# ifdef TARGET_NANOS +#ifdef TARGET_NANOS will_fit = se_get_cropped_length(value, will_fit, BAGL_WIDTH, BAGL_ENCODING_LATIN1); -# else +#else uint8_t width; will_fit++; do { width = bagl_compute_line_width(BAGL_FONT_OPEN_SANS_REGULAR_11px, 0, - value, --will_fit, BAGL_ENCODING_LATIN1); + value, --will_fit, + BAGL_ENCODING_LATIN1); } while (width >= BAGL_WIDTH); PRINTF("[DEBUG] max_line_width(value: \"%s\", width: %d, will_fit: %d)\n", value, width, will_fit); -# endif +#endif FUNC_LEAVE(); return will_fit; } size_t tz_ui_stream_push_all(tz_ui_cb_type_t type, const char *title, - const char *value, tz_ui_icon_t icon) { + const char *value, tz_ui_icon_t icon) +{ size_t obuflen; size_t i = 0; @@ -110,14 +116,16 @@ size_t tz_ui_stream_push_all(tz_ui_cb_type_t type, const char *title, } size_t tz_ui_stream_push(tz_ui_cb_type_t type, const char *title, - const char *value, tz_ui_icon_t icon) { + const char *value, tz_ui_icon_t icon) +{ return tz_ui_stream_pushl(type, title, value, -1, icon); } size_t tz_ui_stream_pushl(tz_ui_cb_type_t type, const char *title, - const char *value, ssize_t max, tz_ui_icon_t icon) { + const char *value, ssize_t max, tz_ui_icon_t icon) +{ tz_ui_stream_t *s = &global.stream; - size_t i; + size_t i; FUNC_ENTER(("title=%s, value=%s", title, value)); if (s->full) { @@ -125,7 +133,7 @@ size_t tz_ui_stream_pushl(tz_ui_cb_type_t type, const char *title, THROW(EXC_UNKNOWN); } #ifdef TEZOS_DEBUG - int prev_total = s->total; + int prev_total = s->total; int prev_current = s->current; #endif @@ -155,8 +163,10 @@ size_t tz_ui_stream_pushl(tz_ui_cb_type_t type, const char *title, will_fit = tz_ui_max_line_chars(&value[offset], length - offset); - PRINTF("[DEBUG] split(value: \"%s\", will_fit: %d, line: %d, " - "offset: %d)\n", &value[offset], will_fit, line, offset); + PRINTF( + "[DEBUG] split(value: \"%s\", will_fit: %d, line: %d, " + "offset: %d)\n", + &value[offset], will_fit, line, offset); strlcpy(s->screens[bucket].body[line], &value[offset], will_fit + 1); @@ -168,7 +178,7 @@ size_t tz_ui_stream_pushl(tz_ui_cb_type_t type, const char *title, PRINTF("[DEBUG] tz_ui_stream_pushl(%s, %s, %u)\n", title, value, max); PRINTF("[DEBUG] bucket %d\n", bucket); PRINTF("[DEBUG] title: \"%s\"\n", s->screens[bucket].title); - for (line=0; line < TZ_UI_STREAM_CONTENTS_LINES; line++) + for (line = 0; line < TZ_UI_STREAM_CONTENTS_LINES; line++) PRINTF("[DEBUG] value[%d]: \"%s\"\n", line, s->screens[bucket].body[line]); PRINTF("[DEBUG] total: %d -> %d\n", prev_total, s->total); @@ -178,28 +188,31 @@ size_t tz_ui_stream_pushl(tz_ui_cb_type_t type, const char *title, return offset; } -#endif // HAVE_BAGL +#endif // HAVE_BAGL -tz_ui_cb_type_t tz_ui_stream_get_type(void) { - tz_ui_stream_t *s = &global.stream; - size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS; +tz_ui_cb_type_t tz_ui_stream_get_type(void) +{ + tz_ui_stream_t *s = &global.stream; + size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS; return s->screens[bucket].type; } #ifdef HAVE_BAGL -static void pred() { +static void pred() +{ tz_ui_stream_t *s = &global.stream; FUNC_ENTER(("void")); - if (s->current >= 1 && - s->current >= s->total - TZ_UI_STREAM_HISTORY_SCREENS + 2) { + if (s->current >= 1 + && s->current >= s->total - TZ_UI_STREAM_HISTORY_SCREENS + 2) { s->current--; } FUNC_LEAVE(); } -static void succ() { +static void succ() +{ tz_ui_stream_t *s = &global.stream; FUNC_ENTER(("void")); @@ -209,43 +222,39 @@ static void succ() { } FUNC_LEAVE(); } -#endif // HAVE_BAGL +#endif // HAVE_BAGL // View #ifdef HAVE_BAGL -static unsigned int cb(unsigned int button_mask, - __attribute__((unused)) - unsigned int button_mask_counter) { - tz_ui_stream_t *s = &global.stream; - size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS; - uint8_t type = s->screens[bucket].type; +static unsigned int cb( + unsigned int button_mask, + __attribute__((unused)) unsigned int button_mask_counter) +{ + tz_ui_stream_t *s = &global.stream; + size_t bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS; + uint8_t type = s->screens[bucket].type; FUNC_ENTER(("button_mask=%d, button_mask_counter=%d", button_mask, button_mask_counter)); switch (button_mask) { - case BUTTON_EVT_RELEASED | BUTTON_LEFT: - change_screen_left(); - break; - case BUTTON_EVT_RELEASED | BUTTON_RIGHT: - change_screen_right(); - break; + case BUTTON_EVT_RELEASED | BUTTON_LEFT: change_screen_left(); break; + case BUTTON_EVT_RELEASED | BUTTON_RIGHT: change_screen_right(); break; case BUTTON_EVT_RELEASED | BUTTON_LEFT | BUTTON_RIGHT: if (type) s->cb(type); if (type & TZ_UI_STREAM_CB_MAINMASK) ui_home_init(); break; - default: - break; + default: break; } FUNC_LEAVE(); return 0; } -static const char *find_icon(tz_ui_icon_t icon) { - +static const char *find_icon(tz_ui_icon_t icon) +{ // clang-format off switch (icon) { case TZ_UI_ICON_TICK: return (const char *)&C_icon_validate_14; @@ -257,50 +266,62 @@ static const char *find_icon(tz_ui_icon_t icon) { // clang-format on } -static void redisplay() { +static void redisplay() +{ bagl_element_t init[] = { - // {type, userid, x, y, width, height, stroke, radius, - // fill, fgcolor, bgcolor, font_id, icon_id}, text/icon - {{ BAGL_RECTANGLE, 0x00, 0, 0, 128, BAGL_HEIGHT, 0, 0, - BAGL_FILL, 0x000000, 0xFFFFFF, 0, 0 }, NULL }, - {{ BAGL_ICON, 0x00, 1, 1, 7, 7, 0, 0, 0, 0xFFFFFF, 0x000000, 0, - BAGL_GLYPH_NOGLYPH }, (const char*) &C_icon_rien }, - {{ BAGL_ICON, 0x00, 120, 1, 7, 7, 0, 0, 0, 0xFFFFFF, 0x000000, 0, - BAGL_GLYPH_NOGLYPH }, (const char*) &C_icon_rien }, - {{ BAGL_LABELINE, 0x02, 8, 8, 112, 11, 0, 0, 0, 0xFFFFFF, 0x000000, - BOLD, 0 }, global.ux.lines[0] }, + // {type, userid, x, y, width, height, stroke, radius, + // fill, fgcolor, bgcolor, font_id, icon_id}, text/icon + {{BAGL_RECTANGLE, 0x00, 0, 0, 128, BAGL_HEIGHT, 0, 0, BAGL_FILL, + 0x000000, 0xFFFFFF, 0, 0}, + NULL }, + {{BAGL_ICON, 0x00, 1, 1, 7, 7, 0, 0, 0, 0xFFFFFF, 0x000000, 0, + BAGL_GLYPH_NOGLYPH}, + (const char *)&C_icon_rien}, + {{BAGL_ICON, 0x00, 120, 1, 7, 7, 0, 0, 0, 0xFFFFFF, 0x000000, 0, + BAGL_GLYPH_NOGLYPH}, + (const char *)&C_icon_rien}, + {{BAGL_LABELINE, 0x02, 8, 8, 112, 11, 0, 0, 0, 0xFFFFFF, 0x000000, + BOLD, 0}, + global.ux.lines[0] }, #ifdef TARGET_NANOS - {{ BAGL_LABELINE, 0x02, 0, 19, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, - REGULAR, 0 }, global.ux.lines[1] }, - {{ BAGL_ICON, 0x00, 56, 14, 16, 16, 0, 0, 0, 0xFFFFFF, 0x000000, 0, - BAGL_GLYPH_NOGLYPH }, (const char*) &C_icon_rien }, + {{BAGL_LABELINE, 0x02, 0, 19, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, + REGULAR, 0}, + global.ux.lines[1] }, + {{BAGL_ICON, 0x00, 56, 14, 16, 16, 0, 0, 0, 0xFFFFFF, 0x000000, 0, + BAGL_GLYPH_NOGLYPH}, + (const char *)&C_icon_rien}, #else - {{ BAGL_LABELINE, 0x02, 0, 21, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, - REGULAR, 0 }, global.ux.lines[1] }, - {{ BAGL_LABELINE, 0x02, 0, 34, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, - REGULAR, 0 }, global.ux.lines[2] }, - {{ BAGL_LABELINE, 0x02, 0, 47, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, - REGULAR, 0 }, global.ux.lines[3] }, - {{ BAGL_LABELINE, 0x02, 0, 60, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, - REGULAR, 0 }, global.ux.lines[4] }, - {{ BAGL_ICON, 0x00, 56, 47, 16, 16, 0, 0, 0, 0xFFFFFF, 0x000000, 0, - BAGL_GLYPH_NOGLYPH }, (const char*) &C_icon_rien }, + {{BAGL_LABELINE, 0x02, 0, 21, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, + REGULAR, 0}, + global.ux.lines[1]}, + {{BAGL_LABELINE, 0x02, 0, 34, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, + REGULAR, 0}, + global.ux.lines[2]}, + {{BAGL_LABELINE, 0x02, 0, 47, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, + REGULAR, 0}, + global.ux.lines[3]}, + {{BAGL_LABELINE, 0x02, 0, 60, 128, 11, 0, 0, 0, 0xFFFFFF, 0x000000, + REGULAR, 0}, + global.ux.lines[4]}, + {{BAGL_ICON, 0x00, 56, 47, 16, 16, 0, 0, 0, 0xFFFFFF, 0x000000, 0, + BAGL_GLYPH_NOGLYPH}, + (const char *)&C_icon_rien}, #endif }; tz_ui_stream_t *s = &global.stream; - size_t bucket, i; + size_t bucket, i; FUNC_ENTER(("void")); for (i = 0; i < TZ_SCREEN_LINES_11PX; i++) - global.ux.lines[i][0]=0; + global.ux.lines[i][0] = 0; bucket = s->current % TZ_UI_STREAM_HISTORY_SCREENS; STRLCPY(global.ux.lines[0], s->screens[bucket].title); for (i = 0; i < TZ_UI_STREAM_CONTENTS_LINES; i++) { - STRLCPY(global.ux.lines[i+1], s->screens[bucket].body[i]); + STRLCPY(global.ux.lines[i + 1], s->screens[bucket].body[i]); } tz_ui_icon_t icon = s->screens[bucket].icon; @@ -308,33 +329,36 @@ static void redisplay() { #ifdef TARGET_NANOS global.ux.lines[1][0] = 0; #endif - init[sizeof(init)/sizeof(bagl_element_t)-1].text = find_icon(icon); + init[sizeof(init) / sizeof(bagl_element_t) - 1].text + = find_icon(icon); } /* If we aren't on the first screen, we can go back */ if (s->current > 0) - init[1].text = (const char*) &C_icon_go_left; + init[1].text = (const char *)&C_icon_go_left; /* Unless we can't... */ if (s->current == s->total - TZ_UI_STREAM_HISTORY_SCREENS + 1) - init[1].text = (const char*) &C_icon_go_forbid; + init[1].text = (const char *)&C_icon_go_forbid; /* If we aren't full or aren't on the last page, we can go right */ if (!s->full || s->current < s->total) - init[2].text = (const char*) &C_icon_go_right; + init[2].text = (const char *)&C_icon_go_right; DISPLAY(init, cb); FUNC_LEAVE(); } -static void change_screen_left() { +static void change_screen_left() +{ FUNC_ENTER(("void")); pred(); redisplay(); FUNC_LEAVE(); } -static void change_screen_right() { +static void change_screen_right() +{ tz_ui_stream_t *s = &global.stream; FUNC_ENTER(("void")); @@ -348,9 +372,10 @@ static void change_screen_right() { redisplay(); FUNC_LEAVE(); } -#endif // HAVE_BAGL +#endif // HAVE_BAGL -void tz_ui_stream_start(void) { +void tz_ui_stream_start(void) +{ FUNC_ENTER(("void")); #ifdef HAVE_BAGL redisplay(); @@ -358,7 +383,8 @@ void tz_ui_stream_start(void) { FUNC_LEAVE(); } -void tz_ui_stream() { +void tz_ui_stream() +{ FUNC_ENTER(("void")); #ifdef HAVE_BAGL @@ -367,6 +393,6 @@ void tz_ui_stream() { succ(); redisplay(); -#endif // HAVE_BAGL +#endif // HAVE_BAGL FUNC_LEAVE(); } diff --git a/app/src/ui_stream.h b/app/src/ui_stream.h index 014b67277..9b0398b76 100644 --- a/app/src/ui_stream.h +++ b/app/src/ui_stream.h @@ -38,13 +38,13 @@ #include -#define TZ_UI_STREAM_HISTORY_SCREENS 8 -#define TZ_UI_STREAM_TITLE_WIDTH TZ_SCREEN_WITDH_BETWEEN_ICONS_BOLD_11PX -#define TZ_UI_STREAM_CONTENTS_WIDTH TZ_SCREEN_WITDH_FULL_REGULAR_11PX -#define TZ_UI_STREAM_CONTENTS_LINES (TZ_SCREEN_LINES_11PX - 1) +#define TZ_UI_STREAM_HISTORY_SCREENS 8 +#define TZ_UI_STREAM_TITLE_WIDTH TZ_SCREEN_WITDH_BETWEEN_ICONS_BOLD_11PX +#define TZ_UI_STREAM_CONTENTS_WIDTH TZ_SCREEN_WITDH_FULL_REGULAR_11PX +#define TZ_UI_STREAM_CONTENTS_LINES (TZ_SCREEN_LINES_11PX - 1) -#define TZ_UI_STREAM_CONTENTS_SIZE (TZ_UI_STREAM_CONTENTS_WIDTH * \ - TZ_UI_STREAM_CONTENTS_LINES) +#define TZ_UI_STREAM_CONTENTS_SIZE \ + (TZ_UI_STREAM_CONTENTS_WIDTH * TZ_UI_STREAM_CONTENTS_LINES) /* * In the following structure, "type" is passed to our callback and @@ -60,38 +60,38 @@ */ typedef uint8_t tz_ui_cb_type_t; -#define TZ_UI_STREAM_CB_NOCB 0x00 -#define TZ_UI_STREAM_CB_REFILL 0xef -#define TZ_UI_STREAM_CB_MAINMASK 0xf0 -#define TZ_UI_STREAM_CB_REJECT 0xfe -#define TZ_UI_STREAM_CB_ACCEPT 0xff +#define TZ_UI_STREAM_CB_NOCB 0x00 +#define TZ_UI_STREAM_CB_REFILL 0xef +#define TZ_UI_STREAM_CB_MAINMASK 0xf0 +#define TZ_UI_STREAM_CB_REJECT 0xfe +#define TZ_UI_STREAM_CB_ACCEPT 0xff /* * The icons we used are generalised to allow for seamless Stax support */ typedef uint8_t tz_ui_icon_t; -#define TZ_UI_ICON_NONE 0x00 -#define TZ_UI_ICON_TICK 0x01 -#define TZ_UI_ICON_CROSS 0x02 -#define TZ_UI_ICON_DASHBOARD 0x03 -#define TZ_UI_ICON_SETTINGS 0x04 +#define TZ_UI_ICON_NONE 0x00 +#define TZ_UI_ICON_TICK 0x01 +#define TZ_UI_ICON_CROSS 0x02 +#define TZ_UI_ICON_DASHBOARD 0x03 +#define TZ_UI_ICON_SETTINGS 0x04 typedef struct { - tz_ui_icon_t icon; + tz_ui_icon_t icon; tz_ui_cb_type_t type; - char title[TZ_UI_STREAM_TITLE_WIDTH + 1]; + char title[TZ_UI_STREAM_TITLE_WIDTH + 1]; char body[TZ_UI_STREAM_CONTENTS_LINES][TZ_UI_STREAM_CONTENTS_WIDTH + 1]; } tz_ui_stream_screen_t; typedef struct { void (*cb)(tz_ui_cb_type_t); tz_ui_stream_screen_t screens[TZ_UI_STREAM_HISTORY_SCREENS]; - int16_t current; - int16_t total; - bool full; - // FIXME: workaround for issue with non-local control flow. Remove once fixed - // see !66 + int16_t current; + int16_t total; + bool full; + // FIXME: workaround for issue with non-local control flow. Remove once + // fixed see !66 bool pressed_right; } tz_ui_stream_t; @@ -107,8 +107,8 @@ size_t tz_ui_stream_pushl(tz_ui_cb_type_t, const char *, const char *, ssize_t, tz_ui_icon_t); size_t tz_ui_stream_push_all(tz_ui_cb_type_t, const char *, const char *, tz_ui_icon_t); -void tz_ui_stream_push_accept_reject(void); -void tz_ui_stream_close(void); -void tz_ui_stream(void); -void tz_ui_stream_start(void); +void tz_ui_stream_push_accept_reject(void); +void tz_ui_stream_close(void); +void tz_ui_stream(void); +void tz_ui_stream_start(void); tz_ui_cb_type_t tz_ui_stream_get_type(void); diff --git a/app/src/ui_stream_nbgl.c b/app/src/ui_stream_nbgl.c index c3f53614c..2aea4dcf5 100644 --- a/app/src/ui_stream_nbgl.c +++ b/app/src/ui_stream_nbgl.c @@ -20,21 +20,24 @@ #include "globals.h" size_t tz_ui_stream_push(tz_ui_cb_type_t type, const char *title, - const char *value, tz_ui_icon_t icon) { + const char *value, tz_ui_icon_t icon) +{ return tz_ui_stream_pushl(type, title, value, -1, icon); } -size_t tz_ui_stream_push_all(__attribute__((unused))tz_ui_cb_type_t type, - __attribute__((unused))const char *title, - const char *value, - __attribute__((unused))tz_ui_icon_t icon) { +size_t tz_ui_stream_push_all(__attribute__((unused)) tz_ui_cb_type_t type, + __attribute__((unused)) const char *title, + const char *value, + __attribute__((unused)) tz_ui_icon_t icon) +{ return strlen(value); } -size_t tz_ui_stream_pushl(__attribute__((unused))tz_ui_cb_type_t type, - __attribute__((unused))const char *title, +size_t tz_ui_stream_pushl(__attribute__((unused)) tz_ui_cb_type_t type, + __attribute__((unused)) const char *title, const char *value, ssize_t max, - __attribute__((unused))tz_ui_icon_t icon) { + __attribute__((unused)) tz_ui_icon_t icon) +{ size_t length = strlen(value); if (max != -1) diff --git a/app/src/utils.h b/app/src/utils.h index 5850e9da1..8c8ce4b67 100644 --- a/app/src/utils.h +++ b/app/src/utils.h @@ -3,7 +3,7 @@ #include "globals.h" -#define STRLCPY(x, y) strlcpy((x), (y), sizeof(x)) +#define STRLCPY(x, y) strlcpy((x), (y), sizeof(x)) /* * Debugging macros. @@ -16,22 +16,26 @@ */ #ifdef TEZOS_DEBUG -#define FUNC_ENTER(x) do { \ - uint8_t _tmp; \ - PRINTF("[DEBUG] call %s(", __func__); \ - PRINTF x; \ - PRINTF(") at %s:%u\n", __FILE__, __LINE__); \ - PRINTF("[DEBUG] stack = 0x%p (%s)\n", &_tmp, __func__); \ - if (app_stack_canary != 0xDEADBEEF) \ - PRINTF("[DEBUG] Stack (0x%p) has been smashed\n", \ - &app_stack_canary); \ - } while (0) -#define FUNC_LEAVE() do { \ - if (app_stack_canary != 0xDEADBEEF) \ - PRINTF("[DEBUG] Stack (0x%p) has been smashed " \ - "(leaving function)\n", &app_stack_canary); \ - PRINTF("[DEBUG] leave %s\n", __func__); \ - } while (0) +#define FUNC_ENTER(x) \ + do { \ + uint8_t _tmp; \ + PRINTF("[DEBUG] call %s(", __func__); \ + PRINTF x; \ + PRINTF(") at %s:%u\n", __FILE__, __LINE__); \ + PRINTF("[DEBUG] stack = 0x%p (%s)\n", &_tmp, __func__); \ + if (app_stack_canary != 0xDEADBEEF) \ + PRINTF("[DEBUG] Stack (0x%p) has been smashed\n", \ + &app_stack_canary); \ + } while (0) +#define FUNC_LEAVE() \ + do { \ + if (app_stack_canary != 0xDEADBEEF) \ + PRINTF( \ + "[DEBUG] Stack (0x%p) has been smashed " \ + "(leaving function)\n", \ + &app_stack_canary); \ + PRINTF("[DEBUG] leave %s\n", __func__); \ + } while (0) #else #define FUNC_ENTER(x) #define FUNC_LEAVE() diff --git a/tests/unit/micheline_cparse_stubs.c b/tests/unit/micheline_cparse_stubs.c index 72fcd2874..4ddad3ecf 100644 --- a/tests/unit/micheline_cparse_stubs.c +++ b/tests/unit/micheline_cparse_stubs.c @@ -21,147 +21,144 @@ #include "micheline_parser.h" #include "operation_parser.h" -CAMLprim value micheline_cparse_capture_name(value mlstate) { - CAMLparam1(mlstate); - CAMLlocal1(r); - tz_parser_state **state = Data_abstract_val(mlstate); - r = caml_copy_string((*state)->field_name); - CAMLreturn(r); +CAMLprim value micheline_cparse_capture_name(value mlstate) +{ + CAMLparam1(mlstate); + CAMLlocal1(r); + tz_parser_state **state = Data_abstract_val(mlstate); + r = caml_copy_string((*state)->field_name); + CAMLreturn(r); } -CAMLprim value micheline_cparse_init(value size) { - CAMLparam1(size); - CAMLlocal1(r); - r = caml_alloc(sizeof(value), Abstract_tag); - tz_parser_state *state = malloc(sizeof(tz_parser_state)); - *((tz_parser_state**) Data_abstract_val(r)) = state; - tz_micheline_parser_init(state); - size_t s = Long_val(size); - if (s >= 0xFFFF) caml_failwith("micheline_cparse_init: size too large"); - tz_operation_parser_init(state, (uint16_t) s, 1); - CAMLreturn(r); +CAMLprim value micheline_cparse_init(value size) +{ + CAMLparam1(size); + CAMLlocal1(r); + r = caml_alloc(sizeof(value), Abstract_tag); + tz_parser_state *state = malloc(sizeof(tz_parser_state)); + *((tz_parser_state **)Data_abstract_val(r)) = state; + tz_micheline_parser_init(state); + size_t s = Long_val(size); + if (s >= 0xFFFF) + caml_failwith("micheline_cparse_init: size too large"); + tz_operation_parser_init(state, (uint16_t)s, 1); + CAMLreturn(r); } -CAMLprim value micheline_cparse_step(value mlstate, value input, - value output) { - CAMLparam3(mlstate, input, output); - CAMLlocal3(ibuf, obuf, r); - ibuf = Field(input, 0); - int iofs = Int_val(Field(input, 1)); - int ilen = Int_val(Field(input, 2)); - obuf = Field(output, 0); - int oofs = Int_val(Field(output, 1)); - int olen = Int_val(Field(output, 2)); - // Data_abstract_val() returns a value that could change. - // It must be dereferenced before being used. - // https://gitlab.com/nomadic-labs/tezos-ledger-app-revamp/-/merge_requests/58#note_1434368804 - tz_parser_state *state = *((tz_parser_state**) Data_abstract_val(mlstate)); - - state->regs.ibuf = Bytes_val(ibuf); - state->regs.iofs = iofs; - state->regs.ilen = ilen; - state->regs.obuf = (char*) Bytes_val(obuf); - state->regs.oofs = oofs; - state->regs.olen = olen; - - while (!TZ_IS_BLOCKED(tz_micheline_parser_step(state))); - - int read = ilen - state->regs.ilen; - int written = olen - state->regs.olen; - - r = caml_alloc_tuple(3); - Store_field(r, 0, Val_int(read)); - Store_field(r, 1, Val_int(written)); - - switch (state->errno) { - case TZ_BLO_DONE: - Store_field(r, 2, Val_int(0)); - break; - case TZ_BLO_FEED_ME: - Store_field(r, 2, Val_int(1)); - break; - case TZ_BLO_IM_FULL: - Store_field(r, 2, Val_int(2)); - break; - case TZ_ERR_INVALID_TAG: - caml_failwith("micheline_cparse_step: invalid tag"); - case TZ_ERR_INVALID_OP: - caml_failwith("micheline_cparse_step: invalid operation"); - case TZ_ERR_UNSUPPORTED: - caml_failwith("micheline_cparse_step: unsupported data"); - case TZ_ERR_TOO_LARGE: - caml_failwith("micheline_cparse_step: data size limitation exceeded"); - case TZ_ERR_TOO_DEEP: - caml_failwith("micheline_cparse_step: expression too deep"); - case TZ_ERR_INVALID_STATE: - caml_failwith("micheline_cparse_step: invalid state"); - default: - char err[100]; - snprintf(err, sizeof(err), "micheline_cparse_step: unknown error code %d", - state->errno); - caml_failwith(err); - } - CAMLreturn(r); +CAMLprim value micheline_cparse_step(value mlstate, value input, value output) +{ + CAMLparam3(mlstate, input, output); + CAMLlocal3(ibuf, obuf, r); + ibuf = Field(input, 0); + int iofs = Int_val(Field(input, 1)); + int ilen = Int_val(Field(input, 2)); + obuf = Field(output, 0); + int oofs = Int_val(Field(output, 1)); + int olen = Int_val(Field(output, 2)); + // Data_abstract_val() returns a value that could change. + // It must be dereferenced before being used. + // https://gitlab.com/nomadic-labs/tezos-ledger-app-revamp/-/merge_requests/58#note_1434368804 + tz_parser_state *state + = *((tz_parser_state **)Data_abstract_val(mlstate)); + + state->regs.ibuf = Bytes_val(ibuf); + state->regs.iofs = iofs; + state->regs.ilen = ilen; + state->regs.obuf = (char *)Bytes_val(obuf); + state->regs.oofs = oofs; + state->regs.olen = olen; + + while (!TZ_IS_BLOCKED(tz_micheline_parser_step(state))) + ; + + int read = ilen - state->regs.ilen; + int written = olen - state->regs.olen; + + r = caml_alloc_tuple(3); + Store_field(r, 0, Val_int(read)); + Store_field(r, 1, Val_int(written)); + + switch (state->errno) { + case TZ_BLO_DONE: Store_field(r, 2, Val_int(0)); break; + case TZ_BLO_FEED_ME: Store_field(r, 2, Val_int(1)); break; + case TZ_BLO_IM_FULL: Store_field(r, 2, Val_int(2)); break; + case TZ_ERR_INVALID_TAG: + caml_failwith("micheline_cparse_step: invalid tag"); + case TZ_ERR_INVALID_OP: + caml_failwith("micheline_cparse_step: invalid operation"); + case TZ_ERR_UNSUPPORTED: + caml_failwith("micheline_cparse_step: unsupported data"); + case TZ_ERR_TOO_LARGE: + caml_failwith("micheline_cparse_step: data size limitation exceeded"); + case TZ_ERR_TOO_DEEP: + caml_failwith("micheline_cparse_step: expression too deep"); + case TZ_ERR_INVALID_STATE: + caml_failwith("micheline_cparse_step: invalid state"); + default: + char err[100]; + snprintf(err, sizeof(err), + "micheline_cparse_step: unknown error code %d", + state->errno); + caml_failwith(err); + } + CAMLreturn(r); } -CAMLprim value operation_cparse_step(value mlstate, value input, - value output) { - CAMLparam3(mlstate, input, output); - CAMLlocal3(ibuf, obuf, r); - ibuf = Field(input, 0); - int iofs = Int_val(Field(input, 1)); - int ilen = Int_val(Field(input, 2)); - obuf = Field(output, 0); - int oofs = Int_val(Field(output, 1)); - int olen = Int_val(Field(output, 2)); - // Data_abstract_val() returns a value that could change. - // It must be dereferenced before being used. - // https://gitlab.com/nomadic-labs/tezos-ledger-app-revamp/-/merge_requests/58#note_1434368804 - tz_parser_state *state = *((tz_parser_state**) Data_abstract_val(mlstate)); - - state->regs.ibuf = Bytes_val(ibuf); - state->regs.iofs = iofs; - state->regs.ilen = ilen; - state->regs.obuf = (char*) Bytes_val(obuf); - state->regs.oofs = oofs; - state->regs.olen = olen; - - while (!TZ_IS_BLOCKED(tz_operation_parser_step(state))); - - int read = ilen - state->regs.ilen; - int written = olen - state->regs.olen; - - r = caml_alloc_tuple(3); - Store_field(r, 0, Val_int(read)); - Store_field(r, 1, Val_int(written)); - - switch (state->errno) { - case TZ_BLO_DONE: - Store_field(r, 2, Val_int(0)); - break; - case TZ_BLO_FEED_ME: - Store_field(r, 2, Val_int(1)); - break; - case TZ_BLO_IM_FULL: - Store_field(r, 2, Val_int(2)); - break; - case TZ_ERR_INVALID_TAG: - caml_failwith("operation_cparse_step: invalid tag"); - case TZ_ERR_INVALID_OP: - caml_failwith("operation_cparse_step: invalid operation"); - case TZ_ERR_UNSUPPORTED: - caml_failwith("operation_cparse_step: unsupported data"); - case TZ_ERR_TOO_LARGE: - caml_failwith("operation_cparse_step: data size limitation exceeded"); - case TZ_ERR_TOO_DEEP: - caml_failwith("operation_cparse_step: expression too deep"); - case TZ_ERR_INVALID_STATE: - caml_failwith("operation_cparse_step: invalid state"); - default: - char err[100]; - snprintf(err, sizeof(err), "operation_cparse_step: unknown error code %d", - state->errno); - caml_failwith(err); - } - CAMLreturn(r); +CAMLprim value operation_cparse_step(value mlstate, value input, value output) +{ + CAMLparam3(mlstate, input, output); + CAMLlocal3(ibuf, obuf, r); + ibuf = Field(input, 0); + int iofs = Int_val(Field(input, 1)); + int ilen = Int_val(Field(input, 2)); + obuf = Field(output, 0); + int oofs = Int_val(Field(output, 1)); + int olen = Int_val(Field(output, 2)); + // Data_abstract_val() returns a value that could change. + // It must be dereferenced before being used. + // https://gitlab.com/nomadic-labs/tezos-ledger-app-revamp/-/merge_requests/58#note_1434368804 + tz_parser_state *state + = *((tz_parser_state **)Data_abstract_val(mlstate)); + + state->regs.ibuf = Bytes_val(ibuf); + state->regs.iofs = iofs; + state->regs.ilen = ilen; + state->regs.obuf = (char *)Bytes_val(obuf); + state->regs.oofs = oofs; + state->regs.olen = olen; + + while (!TZ_IS_BLOCKED(tz_operation_parser_step(state))) + ; + + int read = ilen - state->regs.ilen; + int written = olen - state->regs.olen; + + r = caml_alloc_tuple(3); + Store_field(r, 0, Val_int(read)); + Store_field(r, 1, Val_int(written)); + + switch (state->errno) { + case TZ_BLO_DONE: Store_field(r, 2, Val_int(0)); break; + case TZ_BLO_FEED_ME: Store_field(r, 2, Val_int(1)); break; + case TZ_BLO_IM_FULL: Store_field(r, 2, Val_int(2)); break; + case TZ_ERR_INVALID_TAG: + caml_failwith("operation_cparse_step: invalid tag"); + case TZ_ERR_INVALID_OP: + caml_failwith("operation_cparse_step: invalid operation"); + case TZ_ERR_UNSUPPORTED: + caml_failwith("operation_cparse_step: unsupported data"); + case TZ_ERR_TOO_LARGE: + caml_failwith("operation_cparse_step: data size limitation exceeded"); + case TZ_ERR_TOO_DEEP: + caml_failwith("operation_cparse_step: expression too deep"); + case TZ_ERR_INVALID_STATE: + caml_failwith("operation_cparse_step: invalid state"); + default: + char err[100]; + snprintf(err, sizeof(err), + "operation_cparse_step: unknown error code %d", + state->errno); + caml_failwith(err); + } + CAMLreturn(r); }