diff --git a/.github/workflows/ledger.yml b/.github/workflows/ledger.yml index bedf8b8..f73ca36 100644 --- a/.github/workflows/ledger.yml +++ b/.github/workflows/ledger.yml @@ -3,9 +3,13 @@ name: CI on: # Triggers the workflow on push or pull request events but only for the main branch push: - branches: [ main ] + branches: + - main + - develop pull_request: - branches: [ main ] + branches: + - main + - develop # Allows you to run this workflow manually from the Actions tab workflow_dispatch: @@ -20,6 +24,8 @@ jobs: steps: - uses: actions/checkout@v2 + with: + submodules: true - name: Build with Clang Static Analyzer run: | diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 401ad16..a046122 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -3,7 +3,7 @@ on: workflow_dispatch: push: pull_request: - branches: [ main ] + branches: [ main, develop ] jobs: configure: diff --git a/.gitmodules b/.gitmodules index 27e9369..721a322 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,3 +7,6 @@ [submodule "deps/nanosplus-secure-sdk"] path = deps/nanosplus-secure-sdk url = https://github.com/LedgerHQ/nanosplus-secure-sdk +[submodule "deps/ledger-zxlib"] + path = deps/ledger-zxlib + url = https://github.com/Zondax/ledger-zxlib.git diff --git a/README.md b/README.md index 23a9108..ac5c625 100644 --- a/README.md +++ b/README.md @@ -4,9 +4,10 @@ --- -![zondax](docs/zondax.jpg) +![zondax_light](docs/zondax_light.png#gh-light-mode-only) +![zondax_dark](docs/zondax_dark.png#gh-dark-mode-only) -_Please visit our website at [zondax.ch](zondax.ch)_ +_Please visit our website at [zondax.ch](https://www.zondax.ch)_ --- diff --git a/app/Makefile b/app/Makefile index b0fd8ee..27455da 100755 --- a/app/Makefile +++ b/app/Makefile @@ -27,8 +27,8 @@ all: bin/app.elf @echo "APPNAME=\"${APPNAME}\"" >> $(OUTPUT_INSTALLER) @echo "APPVERSION=\"${APPVERSION}\"" >> $(OUTPUT_INSTALLER) @echo "APPPATH=\""${APPPATH}"\"" >> $(OUTPUT_INSTALLER) - @echo "LOAD_PARAMS=\"${COMMON_LOAD_PARAMS}\"" >> $(OUTPUT_INSTALLER) - @echo "DELETE_PARAMS=\"${COMMON_DELETE_PARAMS}\"" >> $(OUTPUT_INSTALLER) + @echo "LOAD_PARAMS=($$(echo "${APP_LOAD_PARAMS}" | sed -e "s|"${APPNAME}"|\""${APPNAME}"\"|"))" >> $(OUTPUT_INSTALLER) + @echo "DELETE_PARAMS=($$(echo "${COMMON_DELETE_PARAMS}" | sed -e "s|"${APPNAME}"|\""${APPNAME}"\"|"))" >> $(OUTPUT_INSTALLER) @echo "APPHEX=\"" >> $(OUTPUT_INSTALLER) @cat $(CURDIR)/bin/app.hex >> $(OUTPUT_INSTALLER) @echo "\"" >> $(OUTPUT_INSTALLER) diff --git a/app/Makefile.version b/app/Makefile.version index 1869e56..468d502 100644 --- a/app/Makefile.version +++ b/app/Makefile.version @@ -3,4 +3,4 @@ APPVERSION_M=2 # This is the `spec_version` field of `Runtime` APPVERSION_N=0 # This is the patch version of this release -APPVERSION_P=1 +APPVERSION_P=2 diff --git a/app/src/apdu_handler.c b/app/src/apdu_handler.c index efd6e45..f78b7dd 100644 --- a/app/src/apdu_handler.c +++ b/app/src/apdu_handler.c @@ -47,7 +47,7 @@ __Z_INLINE void handleGetAddrSecp256K1(volatile uint32_t *flags, volatile uint32 if (requireConfirmation) { view_review_init(addr_getItem, addr_getNumItems, app_reply_address); - view_review_show(); + view_review_show(0x03); *flags |= IO_ASYNCH_REPLY; return; } @@ -80,7 +80,7 @@ __Z_INLINE void handleSignSecp256K1(volatile uint32_t *flags, volatile uint32_t CHECK_APP_CANARY() view_review_init(tx_getItem, tx_getNumItems, app_sign); - view_review_show(); + view_review_show(0x03); *flags |= IO_ASYNCH_REPLY; } diff --git a/deps/ledger-zxlib b/deps/ledger-zxlib new file mode 160000 index 0000000..ccf9cf8 --- /dev/null +++ b/deps/ledger-zxlib @@ -0,0 +1 @@ +Subproject commit ccf9cf81b8d4d83a58ccd5948d8f80ef582e2efe diff --git a/deps/ledger-zxlib/.editorconfig b/deps/ledger-zxlib/.editorconfig deleted file mode 100644 index 2d7db14..0000000 --- a/deps/ledger-zxlib/.editorconfig +++ /dev/null @@ -1,16 +0,0 @@ -# top-most EditorConfig file -root = true - -[*] -charset = utf-8 -trim_trailing_whitespace = true -end_of_line = lf -insert_final_newline = true - -[*.{c,h,cpp,hpp}] -indent_style = space -indent_size = 4 - -[*.{yml,sh}] -indent_style = space -indent_size = 2 diff --git a/deps/ledger-zxlib/.github/workflows/main.yml b/deps/ledger-zxlib/.github/workflows/main.yml deleted file mode 100644 index 485c5c5..0000000 --- a/deps/ledger-zxlib/.github/workflows/main.yml +++ /dev/null @@ -1,29 +0,0 @@ -name: "Test/Build" -on: - workflow_dispatch: - push: - pull_request: - branches: [ main ] - -jobs: - configure: - runs-on: ubuntu-latest - outputs: - uid_gid: ${{ steps.get-user.outputs.uid_gid }} - steps: - - id: get-user - run: echo "::set-output name=uid_gid::$(id -u):$(id -g)" - - build: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v2 - with: - submodules: true - - name: Install deps - run: | - sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10 - brew install conan - conan config install https://github.com/conan-io/conanclientcert.git - - run: cmake -DCMAKE_BUILD_TYPE=Debug . && make diff --git a/deps/ledger-zxlib/.gitignore b/deps/ledger-zxlib/.gitignore deleted file mode 100644 index d3d05a3..0000000 --- a/deps/ledger-zxlib/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ - -\.idea/ - -cmake-build-debug/ diff --git a/deps/ledger-zxlib/CMakeLists.txt b/deps/ledger-zxlib/CMakeLists.txt deleted file mode 100644 index a8937e7..0000000 --- a/deps/ledger-zxlib/CMakeLists.txt +++ /dev/null @@ -1,57 +0,0 @@ -#******************************************************************************* -#* (c) 2018 Zondax GmbH -#* -#* Licensed under the Apache License, Version 2.0 (the "License"); -#* you may not use this file except in compliance with the License. -#* You may obtain a copy of the License at -#* -#* http://www.apache.org/licenses/LICENSE-2.0 -#* -#* Unless required by applicable law or agreed to in writing, software -#* distributed under the License is distributed on an "AS IS" BASIS, -#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -#* See the License for the specific language governing permissions and -#* limitations under the License. -#******************************************************************************** -cmake_minimum_required(VERSION 3.0) -project(ledger-zxlib) - -set(CMAKE_CXX_STANDARD 11) - -add_subdirectory(cmake/gtest) - -include_directories( - ${CMAKE_CURRENT_SOURCE_DIR}/include -) - -############### - -file(GLOB_RECURSE ZXLIB_SRC - ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c - ) - -file(GLOB_RECURSE TESTS_SRC - ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.cpp - ) - -############### -set(BUILD_TESTS OFF CACHE BOOL "Enables tests") - -add_library(zxlib STATIC ${ZXLIB_SRC}) -target_include_directories(zxlib PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include) - -enable_testing() - -add_executable(zxlib_tests - ${TESTS_SRC} - ) - -target_include_directories(zxlib_tests PRIVATE - ${CMAKE_CURRENT_SOURCE_DIR}/include - ${gtest_SOURCE_DIR}/include - ${gmock_SOURCE_DIR}/include - ) - -target_link_libraries(zxlib_tests gtest_main zxlib) - -add_test(ZXLIB_TESTS zxlib_tests) diff --git a/deps/ledger-zxlib/LICENSE b/deps/ledger-zxlib/LICENSE deleted file mode 100644 index 0fa613e..0000000 --- a/deps/ledger-zxlib/LICENSE +++ /dev/null @@ -1,201 +0,0 @@ - Apache License - Version 2.0, January 2004 - http://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - APPENDIX: How to apply the Apache License to your work. - - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. - - Copyright 2018-2020 Zondax GmbH - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/deps/ledger-zxlib/README.md b/deps/ledger-zxlib/README.md deleted file mode 100644 index d75ac57..0000000 --- a/deps/ledger-zxlib/README.md +++ /dev/null @@ -1,5 +0,0 @@ -# ledger-zxlib - -[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) -[![GithubActions](https://github.com/zondax/ledger-zxlib/actions/workflows/main.yml/badge.svg)](https://github.com/Zondax/ledger-zxlib/blob/main/.github/workflows/main.yaml) -[![CodeFactor](https://www.codefactor.io/repository/github/zondax/ledger-zxlib/badge)](https://www.codefactor.io/repository/github/zondax/ledger-zxlib) diff --git a/deps/ledger-zxlib/app/common/view.c b/deps/ledger-zxlib/app/common/view.c deleted file mode 100644 index 0a5fc19..0000000 --- a/deps/ledger-zxlib/app/common/view.c +++ /dev/null @@ -1,323 +0,0 @@ -/******************************************************************************* -* (c) 2018, 2019 Zondax GmbH -* (c) 2016 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "view.h" -#include "coin.h" -#include "view_internal.h" -#include "crypto.h" - -#include "actions.h" -#include "apdu_codes.h" -#include "ux.h" -#include "bagl.h" -#include "zxmacros.h" -#include "view_templates.h" -#include "tx.h" -#include "addr.h" -#include "app_mode.h" -#include "zxerror.h" - -#include -#include -#include - -view_t viewdata; - -void h_approve(__Z_UNUSED unsigned int _) { - zemu_log_stack("h_approve"); - - view_idle_show(0, NULL); - UX_WAIT(); - if (viewdata.viewfuncAccept != NULL) { - viewdata.viewfuncAccept(); - } -} - -void h_reject(__Z_UNUSED unsigned int _) { - zemu_log_stack("h_reject"); - - view_idle_show(0, NULL); - UX_WAIT(); - app_reject(); -} - -void h_error_accept(__Z_UNUSED unsigned int _) { - view_idle_show(0, NULL); - UX_WAIT(); - app_reply_error(); -} - -void h_initialize(__Z_UNUSED unsigned int _) { - view_idle_show(0, NULL); - UX_WAIT(); -} - -/////////////////////////////////// -// Paging related - -void h_paging_init() { - zemu_log_stack("h_paging_init"); - - viewdata.itemIdx = 0; - viewdata.pageIdx = 0; - viewdata.pageCount = 1; - viewdata.itemCount = 0xFF; -} - -bool h_paging_can_increase() { - if (viewdata.pageIdx + 1 < viewdata.pageCount) { - zemu_log_stack("h_paging_can_increase"); - return true; - } - - // passed page count, go to next index - if (viewdata.itemCount > 0 && viewdata.itemIdx < (viewdata.itemCount - 1 + INCLUDE_ACTIONS_COUNT)) { - zemu_log_stack("h_paging_can_increase"); - return true; - } - - zemu_log_stack("h_paging_can_increase NO"); - return false; -} - -void h_paging_increase() { - zemu_log_stack("h_paging_increase"); - - if (viewdata.pageIdx + 1 < viewdata.pageCount) { - // increase page - viewdata.pageIdx++; - return; - } - - // passed page count, go to next index - if (viewdata.itemCount > 0 && viewdata.itemIdx < (viewdata.itemCount - 1 + INCLUDE_ACTIONS_COUNT)) { - viewdata.itemIdx++; - viewdata.pageIdx = 0; - } -} - -bool h_paging_can_decrease() { - if (viewdata.pageIdx != 0) { - zemu_log_stack("h_paging_can_decrease"); - return true; - } - - if (viewdata.itemIdx > 0) { - zemu_log_stack("h_paging_can_decrease"); - return true; - } - - zemu_log_stack("h_paging_can_decrease NO"); - return false; -} - -void h_paging_decrease() { - char buffer[50]; - snprintf(buffer, sizeof(buffer), "h_paging_decrease Idx %d", viewdata.itemIdx); - zemu_log_stack(buffer); - - if (viewdata.pageIdx != 0) { - viewdata.pageIdx--; - zemu_log_stack("page--"); - return; - } - - if (viewdata.itemIdx > 0) { - viewdata.itemIdx--; - zemu_log_stack("item--"); - // jump to last page. update will cap this value - viewdata.pageIdx = 255; - } -} - -/////////////////////////////////// -// Paging related - -#ifdef INCLUDE_ACTIONS_AS_ITEMS -bool is_accept_item(){ - return viewdata.itemIdx == viewdata.itemCount - 1; -} - -void set_accept_item(){ - viewdata.itemIdx = viewdata.itemCount - 1; - viewdata.pageIdx = 0; -} - -bool is_reject_item(){ - return viewdata.itemIdx == viewdata.itemCount; -} -#endif - -void h_review_action() { -#ifdef INCLUDE_ACTIONS_AS_ITEMS - if( is_accept_item() ){ - zemu_log_stack("action_accept"); - h_approve(1); - return; - } - - if( is_reject_item() ){ - zemu_log_stack("action_reject"); - h_reject(1); - return; - } - - zemu_log_stack("quick accept"); - if (app_mode_expert()) { - set_accept_item(); - h_review_update(); - return; - } -#endif -} - -zxerr_t h_review_update_data() { - if (viewdata.viewfuncGetNumItems == NULL) { - zemu_log_stack("h_review_update_data - GetNumItems==NULL"); - return zxerr_no_data; - } - if (viewdata.viewfuncGetItem == NULL) { - zemu_log_stack("h_review_update_data - GetItem==NULL"); - return zxerr_no_data; - } - - if (viewdata.viewfuncGetItem == NULL) { - zemu_log_stack("h_review_update_data - GetItem==NULL"); - return zxerr_no_data; - } - - if (viewdata.viewfuncGetItem == NULL) { - zemu_log_stack("h_review_update_data - GetItem==NULL"); - return zxerr_no_data; - } - - char buffer[20]; - snprintf(buffer, sizeof(buffer), "update Idx %d/%d", viewdata.itemIdx, viewdata.pageIdx); - zemu_log_stack(buffer); - -#ifdef INCLUDE_ACTIONS_AS_ITEMS - viewdata.pageCount = 1; - - if( is_accept_item() ){ - snprintf(viewdata.key, MAX_CHARS_PER_KEY_LINE, "%s",""); - snprintf(viewdata.value, MAX_CHARS_PER_VALUE1_LINE, "%s", APPROVE_LABEL); - splitValueField(); - zemu_log_stack("show_accept_action - accept item"); - viewdata.pageIdx = 0; - return zxerr_ok; - } - - if( is_reject_item() ){ - snprintf(viewdata.key, MAX_CHARS_PER_KEY_LINE, "%s", ""); - snprintf(viewdata.value, MAX_CHARS_PER_VALUE1_LINE, "%s", REJECT_LABEL); - splitValueField(); - zemu_log_stack("show_reject_action - reject item"); - viewdata.pageIdx = 0; - return zxerr_ok; - } -#endif - - do { - CHECK_ZXERR(viewdata.viewfuncGetNumItems(&viewdata.itemCount)) - - //Verify how many chars fit in display (nanos) - CHECK_ZXERR(viewdata.viewfuncGetItem( - viewdata.itemIdx, - viewdata.key, MAX_CHARS_PER_KEY_LINE, - viewdata.value, MAX_CHARS_PER_VALUE1_LINE, - 0, &viewdata.pageCount)) - viewdata.pageCount = 1; - const max_char_display dyn_max_char_per_line1 = get_max_char_per_line(); - - // be sure we are not out of bounds - CHECK_ZXERR(viewdata.viewfuncGetItem( - viewdata.itemIdx, - viewdata.key, MAX_CHARS_PER_KEY_LINE, - viewdata.value, dyn_max_char_per_line1, - 0, &viewdata.pageCount)) - if (viewdata.pageCount != 0 && viewdata.pageIdx > viewdata.pageCount) { - // try again and get last page - viewdata.pageIdx = viewdata.pageCount - 1; - } - CHECK_ZXERR(viewdata.viewfuncGetItem( - viewdata.itemIdx, - viewdata.key, MAX_CHARS_PER_KEY_LINE, - viewdata.value, dyn_max_char_per_line1, - viewdata.pageIdx, &viewdata.pageCount)) - - viewdata.itemCount++; - - if (viewdata.pageCount > 1) { - uint8_t keyLen = strlen(viewdata.key); - if (keyLen < MAX_CHARS_PER_KEY_LINE) { - snprintf(viewdata.key + keyLen, - MAX_CHARS_PER_KEY_LINE - keyLen, - " [%d/%d]", - viewdata.pageIdx + 1, - viewdata.pageCount); - } - } - - if (viewdata.pageCount == 0) { - h_paging_increase(); - } - } while (viewdata.pageCount == 0); - - splitValueAddress(); - return zxerr_ok; -} - -/////////////////////////////////// -// General - -void io_seproxyhal_display(const bagl_element_t *element) { - io_seproxyhal_display_default((bagl_element_t *) element); -} - -void view_init(void) { - UX_INIT(); -#ifdef APP_SECRET_MODE_ENABLED - viewdata.secret_click_count = 0; -#endif -} - -void view_idle_show(uint8_t item_idx, char *statusString) { - view_idle_show_impl(item_idx, statusString); -} - -void view_message_show(char *title, char *message) { - view_message_impl(title, message); -} - -void view_review_init(viewfunc_getItem_t viewfuncGetItem, - viewfunc_getNumItems_t viewfuncGetNumItems, - viewfunc_accept_t viewfuncAccept) { - viewdata.viewfuncGetItem = viewfuncGetItem; - viewdata.viewfuncGetNumItems = viewfuncGetNumItems; - viewdata.viewfuncAccept = viewfuncAccept; -} - -void view_review_show() { - view_review_show_impl(); -} - -void view_error_show() { - snprintf(viewdata.key, MAX_CHARS_PER_KEY_LINE, "ERROR"); - snprintf(viewdata.value, MAX_CHARS_PER_VALUE1_LINE, "SHOWING DATA"); - splitValueField(); - view_error_show_impl(); -} diff --git a/deps/ledger-zxlib/app/common/view.h b/deps/ledger-zxlib/app/common/view.h deleted file mode 100644 index 6c9e777..0000000 --- a/deps/ledger-zxlib/app/common/view.h +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* -* (c) 2018-2020 Zondax GmbH -* (c) 2016 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#include -#include "coin.h" -#include "zxerror.h" - -#if defined(LEDGER_SPECIFIC) -#include "bolos_target.h" -#if defined(BOLOS_SDK) -#include "os.h" -#include "cx.h" -#endif -#endif - -typedef zxerr_t (*viewfunc_getNumItems_t)(uint8_t *num_items); - -typedef zxerr_t (*viewfunc_getItem_t)(int8_t displayIdx, - char *outKey, uint16_t outKeyLen, - char *outVal, uint16_t outValLen, - uint8_t pageIdx, uint8_t *pageCount); - -typedef void (*viewfunc_accept_t)(); - -#ifdef APP_SECRET_MODE_ENABLED -zxerr_t secret_enabled(); -#endif - -/// view_init (initializes UI) -void view_init(); - -/// view_initialize_show (idle view - main menu + status) -void view_initialize_show(uint8_t item_idx, char *statusString); - -/// view_idle_show (idle view - main menu + status) -void view_idle_show(uint8_t item_idx, char *statusString); - -void view_message_show(char *title, char *message); - -/// view_error (error view) -void view_error_show(); - -void view_review_init(viewfunc_getItem_t viewfuncGetItem, - viewfunc_getNumItems_t viewfuncGetNumItems, - viewfunc_accept_t viewfuncAccept); - -void view_review_show(); diff --git a/deps/ledger-zxlib/app/common/view_internal.h b/deps/ledger-zxlib/app/common/view_internal.h deleted file mode 100644 index 4f08a73..0000000 --- a/deps/ledger-zxlib/app/common/view_internal.h +++ /dev/null @@ -1,135 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* (c) 2016 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#include -#include -#include "coin.h" -#include "zxerror.h" -#include "view.h" - -#define CUR_FLOW G_ux.flow_stack[G_ux.stack_count-1] - -#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) -#define MAX_CHARS_PER_KEY_LINE 64 -#define MAX_CHARS_PER_VALUE1_LINE 4096 -#define MAX_CHARS_HEXMESSAGE 160 -#else -#ifndef MAX_CHARS_PER_VALUE_LINE -#define MAX_CHARS_PER_VALUE_LINE (17) -#endif -#define MAX_CHARS_PER_KEY_LINE (MAX_CHARS_PER_VALUE_LINE+1) -#define MAX_CHARS_PER_VALUE1_LINE (2*MAX_CHARS_PER_VALUE_LINE+1) -#define MAX_CHARS_PER_VALUE2_LINE (MAX_CHARS_PER_VALUE_LINE+1) -#define MAX_CHARS_HEXMESSAGE 40 -#endif - -// This takes data from G_io_apdu_buffer that is prefilled with the address - -#define APPROVE_LABEL "APPROVE" -#define REJECT_LABEL "REJECT" - -#if defined(TARGET_NANOS) -#define INCLUDE_ACTIONS_AS_ITEMS 2 -#define INCLUDE_ACTIONS_COUNT (INCLUDE_ACTIONS_AS_ITEMS-1) -typedef uint8_t max_char_display; -#else -#define INCLUDE_ACTIONS_COUNT 0 -typedef int max_char_display; -#endif - -typedef struct { - struct { - char key[MAX_CHARS_PER_KEY_LINE]; - char value[MAX_CHARS_PER_VALUE1_LINE]; -#if defined(TARGET_NANOS) - char value2[MAX_CHARS_PER_VALUE2_LINE]; -#endif - }; - viewfunc_getItem_t viewfuncGetItem; - viewfunc_getNumItems_t viewfuncGetNumItems; - viewfunc_accept_t viewfuncAccept; - -#ifdef APP_SECRET_MODE_ENABLED - uint8_t secret_click_count; -#endif - uint8_t itemIdx; - uint8_t itemCount; - uint8_t pageIdx; - uint8_t pageCount; -} view_t; - -typedef enum { - view_action_unknown, - view_action_accept, - view_action_reject, -} view_action_t; - -extern view_t viewdata; - -#define print_title(...) snprintf(viewdata.title, sizeof(viewdata.title), __VA_ARGS__) -#define print_key(...) snprintf(viewdata.key, sizeof(viewdata.key), __VA_ARGS__); -#define print_value(...) snprintf(viewdata.value, sizeof(viewdata.value), __VA_ARGS__); - -#if defined(TARGET_NANOS) -#define print_value2(...) snprintf(viewdata.value2, sizeof(viewdata.value2), __VA_ARGS__); -#endif - -void splitValueField(); -void splitValueAddress(); -max_char_display get_max_char_per_line(); - -/////////////////////////////////////////////// -/////////////////////////////////////////////// -/////////////////////////////////////////////// -/////////////////////////////////////////////// -/////////////////////////////////////////////// -/////////////////////////////////////////////// -/////////////////////////////////////////////// -/////////////////////////////////////////////// - -void view_initialize_show_impl(uint8_t item_idx, char *statusString); - -void view_idle_show_impl(uint8_t item_idx, char *statusString); - -void view_message_impl(char *title, char *message); - -void view_error_show_impl(); - -void h_paging_init(); - -bool h_paging_can_increase(); - -void h_paging_increase(); - -bool h_paging_can_decrease(); - -void h_paging_decrease(); - -void view_review_show_impl(); - -void h_approve(unsigned int _); - -void h_reject(unsigned int _); - -void h_review_action(); - -void h_review_update(); - -void h_error_accept(unsigned int _); - -zxerr_t h_review_update_data(); diff --git a/deps/ledger-zxlib/app/common/view_s.c b/deps/ledger-zxlib/app/common/view_s.c deleted file mode 100644 index d323b5c..0000000 --- a/deps/ledger-zxlib/app/common/view_s.c +++ /dev/null @@ -1,336 +0,0 @@ -/******************************************************************************* -* (c) 2018, 2019 Zondax GmbH -* (c) 2016 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "app_mode.h" -#include "view.h" -#include "view_internal.h" -#include "apdu_codes.h" -#include "ux.h" -#include "bagl.h" -#include "zxmacros.h" -#include "view_templates.h" -#include "zxutils_ledger.h" - -#include -#include - -#if defined(TARGET_NANOS) - -void h_initialize(); - -#define BAGL_WIDTH 128 -#define BAGL_HEIGHT 32 -#define BAGL_WIDTH_MARGIN 10 - -void h_expert_toggle(); -void h_expert_update(); -void h_review_button_left(); -void h_review_button_right(); -void h_review_button_both(); - -bool exceed_pixel_in_display(const uint8_t length); - -#ifdef APP_SECRET_MODE_ENABLED -void h_secret_click(); -#endif - -ux_state_t ux; - -void os_exit(uint32_t id) { - (void)id; - os_sched_exit(0); -} - -const ux_menu_entry_t menu_main[] = { - {NULL, NULL, 0, &C_icon_app, MENU_MAIN_APP_LINE1, viewdata.key, 33, 12}, - {NULL, h_expert_toggle, 0, &C_icon_app, "Expert mode:", viewdata.value, 33, 12}, - {NULL, NULL, 0, &C_icon_app, APPVERSION_LINE1, APPVERSION_LINE2, 33, 12}, - - {NULL, -#ifdef APP_SECRET_MODE_ENABLED - h_secret_click, -#else - NULL, -#endif - 0, &C_icon_app, "Developed by:", "Zondax.ch", 33, 12}, - - {NULL, NULL, 0, &C_icon_app, "License: ", "Apache 2.0", 33, 12}, - {NULL, os_exit, 0, &C_icon_dashboard, "Quit", NULL, 50, 29}, - UX_MENU_END -}; - -const ux_menu_entry_t menu_initialize[] = { - {NULL, NULL, 0, &C_icon_app, MENU_MAIN_APP_LINE1, viewdata.key, 33, 12}, - {NULL, h_initialize, 0, &C_icon_app, "Click to", "Initialize", 33, 12}, - {NULL, h_expert_toggle, 0, &C_icon_app, "Expert mode:", viewdata.value, 33, 12}, - {NULL, NULL, 0, &C_icon_app, APPVERSION_LINE1, APPVERSION_LINE2, 33, 12}, - {NULL, NULL, 0, &C_icon_app, "Developed by:", "Zondax.ch", 33, 12}, - {NULL, NULL, 0, &C_icon_app, "License: ", "Apache 2.0", 33, 12}, - {NULL, os_exit, 0, &C_icon_dashboard, "Quit", NULL, 50, 29}, - UX_MENU_END -}; - -static const bagl_element_t view_message[] = { - UI_BACKGROUND, - UI_LabelLine(UIID_LABEL + 0, 0, 8, UI_SCREEN_WIDTH, UI_11PX, UI_WHITE, UI_BLACK, viewdata.key), - UI_LabelLine(UIID_LABEL + 1, 0, 19, UI_SCREEN_WIDTH, UI_11PX, UI_WHITE, UI_BLACK, viewdata.value), -}; - -static const bagl_element_t view_review[] = { - UI_BACKGROUND_LEFT_RIGHT_ICONS, - UI_LabelLine(UIID_LABEL + 0, 0, 8, UI_SCREEN_WIDTH, UI_11PX, UI_WHITE, UI_BLACK, viewdata.key), - UI_LabelLine(UIID_LABEL + 1, 0, 19, UI_SCREEN_WIDTH, UI_11PX, UI_WHITE, UI_BLACK, viewdata.value), - UI_LabelLine(UIID_LABEL + 2, 0, 30, UI_SCREEN_WIDTH, UI_11PX, UI_WHITE, UI_BLACK, viewdata.value2), -}; - -static const bagl_element_t view_error[] = { - UI_FillRectangle(0, 0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT, 0x000000, 0xFFFFFF), - UI_Icon(0, 128 - 7, 0, 7, 7, BAGL_GLYPH_ICON_CHECK), - UI_LabelLine(UIID_LABEL + 0, 0, 8, UI_SCREEN_WIDTH, UI_11PX, UI_WHITE, UI_BLACK, viewdata.key), - UI_LabelLine(UIID_LABEL + 0, 0, 19, UI_SCREEN_WIDTH, UI_11PX, UI_WHITE, UI_BLACK, viewdata.value), - UI_LabelLineScrolling(UIID_LABELSCROLL, 0, 30, 128, UI_11PX, UI_WHITE, UI_BLACK, viewdata.value2), -}; - -static unsigned int view_error_button(unsigned int button_mask, __Z_UNUSED unsigned int button_mask_counter) { - switch (button_mask) { - case BUTTON_EVT_RELEASED | BUTTON_LEFT | BUTTON_RIGHT: - case BUTTON_EVT_RELEASED | BUTTON_LEFT: - break; - case BUTTON_EVT_RELEASED | BUTTON_RIGHT: - h_error_accept(0); - break; - } - return 0; -} - -static unsigned int view_message_button(unsigned int button_mask, __Z_UNUSED unsigned int button_mask_counter) { - switch (button_mask) { - case BUTTON_EVT_RELEASED | BUTTON_LEFT | BUTTON_RIGHT: - case BUTTON_EVT_RELEASED | BUTTON_LEFT: - case BUTTON_EVT_RELEASED | BUTTON_RIGHT: - break; - } - return 0; -} - -static unsigned int view_review_button(unsigned int button_mask, __Z_UNUSED unsigned int button_mask_counter) { - switch (button_mask) { - case BUTTON_EVT_RELEASED | BUTTON_LEFT | BUTTON_RIGHT: - h_review_button_both(); - break; - case BUTTON_EVT_RELEASED | BUTTON_LEFT: - // Press left to progress to the previous element - h_review_button_left(); - break; - - case BUTTON_EVT_RELEASED | BUTTON_RIGHT: - // Press right to progress to the next element - h_review_button_right(); - break; - } - return 0; -} - -const bagl_element_t *view_prepro(const bagl_element_t *element) { - switch (element->component.userid) { - case UIID_ICONLEFT: - if (!h_paging_can_decrease()){ - return NULL; - } - UX_CALLBACK_SET_INTERVAL(2000); - break; - case UIID_ICONRIGHT: - if (!h_paging_can_increase()){ - return NULL; - } - UX_CALLBACK_SET_INTERVAL(2000); - break; - case UIID_LABELSCROLL: - UX_CALLBACK_SET_INTERVAL( - MAX(3000, 1000 + bagl_label_roundtrip_duration_ms(element, 7)) - ); - break; - } - return element; -} - -const bagl_element_t *view_prepro_idle(const bagl_element_t *element) { - switch (element->component.userid) { - case UIID_ICONLEFT: - case UIID_ICONRIGHT: - return NULL; - } - return element; -} - -void h_review_update() { - zxerr_t err = h_review_update_data(); - switch(err) { - case zxerr_ok: - UX_DISPLAY(view_review, view_prepro); - break; - default: - view_error_show(); - UX_WAIT(); - break; - } -} - -void h_review_button_left() { - zemu_log_stack("h_review_button_left"); - h_paging_decrease(); - h_review_update(); -} - -void h_review_button_right() { - zemu_log_stack("h_review_button_right"); - h_paging_increase(); - h_review_update(); -} - -void h_review_button_both() { - zemu_log_stack("h_review_button_left"); - h_review_action(); -} - -void splitValueField() { - print_value2(""); - const uint16_t vlen = strlen(viewdata.value); - if (vlen > MAX_CHARS_PER_VALUE2_LINE - 1) { - snprintf(viewdata.value2, MAX_CHARS_PER_VALUE2_LINE, "%s", viewdata.value + MAX_CHARS_PER_VALUE_LINE); - viewdata.value[MAX_CHARS_PER_VALUE_LINE] = 0; - } -} -void splitValueAddress() { - uint8_t len = MAX_CHARS_PER_VALUE_LINE; - bool exceeding_max = exceed_pixel_in_display(len); - while(exceeding_max && len--) { - exceeding_max = exceed_pixel_in_display(len); - } - print_value2(""); - const uint16_t vlen = strlen(viewdata.value); - //if viewdata.value == NULL --> len = 0 - if (vlen > len && len > 0) { - snprintf(viewdata.value2, MAX_CHARS_PER_VALUE2_LINE, "%s", viewdata.value + len); - viewdata.value[len] = 0; - } -} - -max_char_display get_max_char_per_line() { - uint8_t len = MAX_CHARS_PER_VALUE_LINE; - bool exceeding_max = exceed_pixel_in_display(len); - while(exceeding_max && len--) { - exceeding_max = exceed_pixel_in_display(len); - } - //MAX_CHARS_PER_VALUE1_LINE is defined this way - return (len > 0) ? (2 * len + 1) : len; -} - -bool exceed_pixel_in_display(const uint8_t length) { - const unsigned short strWidth = zx_compute_line_width_light(viewdata.value, length); - return (strWidth >= (BAGL_WIDTH - BAGL_WIDTH_MARGIN)); -} - -////////////////////////// -////////////////////////// -////////////////////////// -////////////////////////// -////////////////////////// - -void view_initialize_show_impl(uint8_t item_idx, char *statusString) { - if (statusString == NULL ) { - snprintf(viewdata.key, MAX_CHARS_PER_VALUE_LINE, "%s", MENU_MAIN_APP_LINE2); - } else { - snprintf(viewdata.key, MAX_CHARS_PER_VALUE_LINE, "%s", statusString); - } - h_expert_update(); - UX_MENU_DISPLAY(item_idx, menu_initialize, NULL); -} - -void view_idle_show_impl(uint8_t item_idx, char *statusString) { - if (statusString == NULL ) { - snprintf(viewdata.key, MAX_CHARS_PER_VALUE_LINE, "%s", MENU_MAIN_APP_LINE2); -#ifdef APP_SECRET_MODE_ENABLED - if (app_mode_secret()) { - snprintf(viewdata.key, MAX_CHARS_PER_VALUE_LINE, "%s", MENU_MAIN_APP_LINE2_SECRET); - } -#endif - } else { - snprintf(viewdata.key, MAX_CHARS_PER_VALUE_LINE, "%s", statusString); - } - h_expert_update(); - UX_MENU_DISPLAY(item_idx, menu_main, NULL); -} - -void view_message_impl(char *title, char *message) { - snprintf(viewdata.key, MAX_CHARS_PER_VALUE_LINE, "%s", title); - snprintf(viewdata.value, MAX_CHARS_PER_VALUE_LINE, "%s", message); - UX_DISPLAY(view_message, view_prepro_idle); -} - -void view_error_show_impl() { - UX_DISPLAY(view_error, view_prepro); -} - -void h_expert_toggle() { - app_mode_set_expert(!app_mode_expert()); - view_idle_show(1, NULL); -} - -#ifdef APP_SECRET_MODE_ENABLED -void h_secret_click() { - if (COIN_SECRET_REQUIRED_CLICKS == 0) { - // There is no secret mode - return; - } - - viewdata.secret_click_count++; - - char buffer[50]; - snprintf(buffer, sizeof(buffer), "secret click %d\n", viewdata.secret_click_count); - zemu_log(buffer); - - if (viewdata.secret_click_count >= COIN_SECRET_REQUIRED_CLICKS) { - secret_enabled(); - viewdata.secret_click_count = 0; - } -} -#endif - -void h_expert_update() { - snprintf(viewdata.value, MAX_CHARS_PER_VALUE_LINE, "disabled"); - if (app_mode_expert()) { - snprintf(viewdata.value, MAX_CHARS_PER_VALUE_LINE, "enabled"); - } -} - -void view_review_show_impl() { - zemu_log_stack("view_review_show_impl"); - - h_paging_init(); - - zxerr_t err = h_review_update_data(); - switch(err) { - case zxerr_ok: - UX_DISPLAY(view_review, view_prepro); - break; - default: - view_error_show(); - break; - } -} -#endif diff --git a/deps/ledger-zxlib/app/common/view_x.c b/deps/ledger-zxlib/app/common/view_x.c deleted file mode 100644 index f8dd8bc..0000000 --- a/deps/ledger-zxlib/app/common/view_x.c +++ /dev/null @@ -1,292 +0,0 @@ -/******************************************************************************* -* (c) 2018, 2019 Zondax GmbH -* (c) 2016 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "app_mode.h" -#include "view.h" -#include "view_internal.h" -#include "actions.h" -#include "apdu_codes.h" -#include "glyphs.h" -#include "bagl.h" -#include "zxmacros.h" -#include "view_templates.h" -#include "tx.h" - -#ifdef APP_SECRET_MODE_ENABLED -#include "secret.h" -#endif - -#include -#include - -#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) - -void h_expert_toggle(); -void h_expert_update(); -void h_review_loop_start(); -void h_review_loop_inside(); -void h_review_loop_end(); - -#ifdef APP_SECRET_MODE_ENABLED -void h_secret_click(); -#endif - -#include "ux.h" -ux_state_t G_ux; -bolos_ux_params_t G_ux_params; -uint8_t flow_inside_loop; - - -UX_STEP_NOCB(ux_idle_flow_1_step, pbb, { &C_icon_app, MENU_MAIN_APP_LINE1, viewdata.key,}); -UX_STEP_CB_INIT(ux_idle_flow_2_step, bn, h_expert_update(), h_expert_toggle(), { "Expert mode:", viewdata.value, }); -UX_STEP_NOCB(ux_idle_flow_3_step, bn, { APPVERSION_LINE1, APPVERSION_LINE2, }); - -#ifdef APP_SECRET_MODE_ENABLED -UX_STEP_CB(ux_idle_flow_4_step, bn, h_secret_click(), { "Developed by:", "Zondax.ch", }); -#else -UX_STEP_NOCB(ux_idle_flow_4_step, bn, { "Developed by:", "Zondax.ch", }); -#endif - -UX_STEP_NOCB(ux_idle_flow_5_step, bn, { "License:", "Apache 2.0", }); -UX_STEP_CB(ux_idle_flow_6_step, pb, os_sched_exit(-1), { &C_icon_dashboard, "Quit",}); - -const ux_flow_step_t *const ux_idle_flow [] = { - &ux_idle_flow_1_step, - &ux_idle_flow_2_step, - &ux_idle_flow_3_step, - &ux_idle_flow_4_step, - &ux_idle_flow_5_step, - &ux_idle_flow_6_step, - FLOW_END_STEP, -}; - -/////////// - -UX_STEP_NOCB(ux_message_flow_1_step, pbb, { &C_icon_app, viewdata.key, viewdata.value,}); - -UX_FLOW( - ux_message_flow, - &ux_message_flow_1_step -); - -/////////// - -UX_STEP_NOCB(ux_error_flow_1_step, bnnn_paging, { .title = viewdata.key, .text = viewdata.value, }); -UX_STEP_VALID(ux_error_flow_2_step, pb, h_error_accept(0), { &C_icon_validate_14, "Ok"}); - -UX_FLOW( - ux_error_flow, - &ux_error_flow_1_step, - &ux_error_flow_2_step -); - -/////////// - -UX_FLOW_DEF_NOCB(ux_review_flow_1_review_title, pbb, { &C_icon_app, "Please", "review",}); -UX_STEP_INIT(ux_review_flow_2_start_step, NULL, NULL, { h_review_loop_start(); }); -UX_STEP_NOCB_INIT(ux_review_flow_2_step, bnnn_paging, { h_review_loop_inside(); }, { .title = viewdata.key, .text = viewdata.value, }); -UX_STEP_INIT(ux_review_flow_2_end_step, NULL, NULL, { h_review_loop_end(); }); -UX_STEP_VALID(ux_review_flow_3_step, pb, h_approve(0), { &C_icon_validate_14, APPROVE_LABEL }); -UX_STEP_VALID(ux_review_flow_4_step, pb, h_reject(0), { &C_icon_crossmark, REJECT_LABEL }); - -const ux_flow_step_t *const ux_review_flow[] = { - &ux_review_flow_1_review_title, - &ux_review_flow_2_start_step, - &ux_review_flow_2_step, - &ux_review_flow_2_end_step, - &ux_review_flow_3_step, - &ux_review_flow_4_step, - FLOW_END_STEP, -}; - -////////////////////////// -////////////////////////// -////////////////////////// -////////////////////////// -////////////////////////// - -void h_review_update() { - zxerr_t err = h_review_update_data(); - switch(err) { - case zxerr_ok: - case zxerr_no_data: - break; - default: - view_error_show(); - break; - } -} - -void h_review_loop_start() { - if (flow_inside_loop) { - // coming from right - - if (!h_paging_can_decrease()) { - // exit to the left - flow_inside_loop = 0; - ux_flow_prev(); - return; - } - - h_paging_decrease(); - } else { - // coming from left - h_paging_init(); - } - - h_review_update(); - - ux_flow_next(); -} - -void h_review_loop_inside() { - flow_inside_loop = 1; -} - -void h_review_loop_end() { - if (flow_inside_loop) { - // coming from left - h_paging_increase(); - zxerr_t err = h_review_update_data(); - - switch(err) { - case zxerr_ok: - ux_layout_bnnn_paging_reset(); - break; - case zxerr_no_data: { - flow_inside_loop = 0; - ux_flow_next(); - return; - } - default: - view_error_show(); - break; - } - } else { - // coming from right - h_paging_decrease(); - h_review_update(); - } - - // move to prev flow but trick paging to show first page - CUR_FLOW.prev_index = CUR_FLOW.index-2; - CUR_FLOW.index--; - ux_flow_relayout(); -} - -void splitValueField() { - uint16_t vlen = strlen(viewdata.value); - if (vlen == 0 ) { - snprintf(viewdata.value, MAX_CHARS_PER_VALUE1_LINE, " "); - } -} - -void splitValueAddress() { - splitValueField(); -} - -max_char_display get_max_char_per_line() { - return MAX_CHARS_PER_VALUE1_LINE; -} - -void h_expert_toggle() { - app_mode_set_expert(!app_mode_expert()); - ux_flow_init(0, ux_idle_flow, &ux_idle_flow_2_step); -} - -void h_expert_update() { - snprintf(viewdata.value, MAX_CHARS_PER_VALUE1_LINE, "disabled"); - if (app_mode_expert()) { - snprintf(viewdata.value, MAX_CHARS_PER_VALUE1_LINE, "enabled"); - } -} - -#ifdef APP_SECRET_MODE_ENABLED -void h_secret_click() { - if (COIN_SECRET_REQUIRED_CLICKS == 0) { - // There is no secret mode - return; - } - - viewdata.secret_click_count++; - - char buffer[50]; - snprintf(buffer, sizeof(buffer), "secret click %d\n", viewdata.secret_click_count); - zemu_log(buffer); - - if (viewdata.secret_click_count >= COIN_SECRET_REQUIRED_CLICKS) { - secret_enabled(); - viewdata.secret_click_count = 0; - return; - } - - ux_flow_init(0, ux_idle_flow, &ux_idle_flow_4_step); -} -#endif - -////////////////////////// -////////////////////////// -////////////////////////// -////////////////////////// -////////////////////////// - -void view_idle_show_impl(__Z_UNUSED uint8_t item_idx, char *statusString) { - if (statusString == NULL ) { - if (app_mode_secret()) { - snprintf(viewdata.key, MAX_CHARS_PER_KEY_LINE, "%s", MENU_MAIN_APP_LINE2_SECRET); - } else { - snprintf(viewdata.key, MAX_CHARS_PER_KEY_LINE, "%s", MENU_MAIN_APP_LINE2); - } - } else { - snprintf(viewdata.key, MAX_CHARS_PER_KEY_LINE, "%s", statusString); - } - - if(G_ux.stack_count == 0) { - ux_stack_push(); - } - ux_flow_init(0, ux_idle_flow, NULL); -} - -void view_review_show_impl(){ - h_paging_init(); - h_paging_decrease(); - //// - flow_inside_loop = 0; - if(G_ux.stack_count == 0) { - ux_stack_push(); - } - ux_flow_init(0, ux_review_flow, NULL); -} - -void view_message_impl(char *title, char *message) { - snprintf(viewdata.key, MAX_CHARS_PER_KEY_LINE, "%s", title); - snprintf(viewdata.value, MAX_CHARS_PER_VALUE1_LINE, "%s", message); - ux_layout_bnnn_paging_reset(); - if(G_ux.stack_count == 0) { - ux_stack_push(); - } - ux_flow_init(0, ux_message_flow, NULL); -} - -void view_error_show_impl() { - ux_layout_bnnn_paging_reset(); - if(G_ux.stack_count == 0) { - ux_stack_push(); - } - ux_flow_init(0, ux_error_flow, NULL); -} -#endif diff --git a/deps/ledger-zxlib/cmake/gtest/CMakeLists.txt b/deps/ledger-zxlib/cmake/gtest/CMakeLists.txt deleted file mode 100644 index c64d5d8..0000000 --- a/deps/ledger-zxlib/cmake/gtest/CMakeLists.txt +++ /dev/null @@ -1,31 +0,0 @@ -############################## -# Google Test -# Based on instructions in https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project -# Download and unpack googletest at configure time -configure_file(CMakeLists.txt.gtest.in ${CMAKE_BINARY_DIR}/googletest-download/CMakeLists.txt) - -execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download) -if (result) - message(FATAL_ERROR "CMake step for googletest failed: ${result}") -endif () - -execute_process(COMMAND ${CMAKE_COMMAND} --build . - RESULT_VARIABLE result - WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download) -if (result) - message(FATAL_ERROR "Build step for googletest failed: ${result}") -endif () - -# Prevent overriding the parent project's compiler/linker settings on Windows -set(gtest_force_shared_crt ON CACHE BOOL "" FORCE) - -add_subdirectory( - ${CMAKE_BINARY_DIR}/googletest-src - ${CMAKE_BINARY_DIR}/googletest-build -) - -if (CMAKE_VERSION VERSION_LESS 3.0.0) - include_directories("${gtest_SOURCE_DIR}/include") -endif () diff --git a/deps/ledger-zxlib/cmake/gtest/CMakeLists.txt.gtest.in b/deps/ledger-zxlib/cmake/gtest/CMakeLists.txt.gtest.in deleted file mode 100644 index b30f8be..0000000 --- a/deps/ledger-zxlib/cmake/gtest/CMakeLists.txt.gtest.in +++ /dev/null @@ -1,16 +0,0 @@ -# Based on https://github.com/google/googletest/tree/master/googletest#incorporating-into-an-existing-cmake-project -cmake_minimum_required(VERSION 3.0.0) - -project(googletest-download NONE) - -include(ExternalProject) -ExternalProject_Add(googletest - GIT_REPOSITORY https://github.com/google/googletest.git - GIT_TAG release-1.11.0 - SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" - BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - ) diff --git a/deps/ledger-zxlib/dockerized_build.mk b/deps/ledger-zxlib/dockerized_build.mk deleted file mode 100644 index 50d9c37..0000000 --- a/deps/ledger-zxlib/dockerized_build.mk +++ /dev/null @@ -1,326 +0,0 @@ -#******************************************************************************* -#* (c) 2019-2021 Zondax GmbH -#* -#* Licensed under the Apache License, Version 2.0 (the "License"); -#* you may not use this file except in compliance with the License. -#* You may obtain a copy of the License at -#* -#* http://www.apache.org/licenses/LICENSE-2.0 -#* -#* Unless required by applicable law or agreed to in writing, software -#* distributed under the License is distributed on an "AS IS" BASIS, -#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -#* See the License for the specific language governing permissions and -#* limitations under the License. -#******************************************************************************** - -.PHONY: all deps build clean load delete check_python show_info_recovery_mode - -TESTS_ZEMU_DIR?=$(CURDIR)/tests_zemu -EXAMPLE_VUE_DIR?=$(CURDIR)/example_vue -TESTS_JS_PACKAGE?= -TESTS_JS_DIR?= - -LEDGER_SRC=$(CURDIR)/app -DOCKER_APP_SRC=/project -DOCKER_APP_BIN=$(DOCKER_APP_SRC)/app/bin/app.elf - -DOCKER_BOLOS_SDKS=/project/deps/nanos-secure-sdk -DOCKER_BOLOS_SDKX=/project/deps/nanox-secure-sdk -DOCKER_BOLOS_SDKS2=/project/deps/nanosplus-secure-sdk - -# Note: This is not an SSH key, and being public represents no risk -SCP_PUBKEY=049bc79d139c70c83a4b19e8922e5ee3e0080bb14a2e8b0752aa42cda90a1463f689b0fa68c1c0246845c2074787b649d0d8a6c0b97d4607065eee3057bdf16b83 -SCP_PRIVKEY=ff701d781f43ce106f72dc26a46b6a83e053b5d07bb3d4ceab79c91ca822a66b - -INTERACTIVE:=$(shell [ -t 0 ] && echo 1) -USERID:=$(shell id -u) -$(info USERID : $(USERID)) -$(info TESTS_ZEMU_DIR : $(TESTS_ZEMU_DIR)) -$(info EXAMPLE_VUE_DIR : $(EXAMPLE_VUE_DIR)) -$(info TESTS_JS_DIR : $(TESTS_JS_DIR)) -$(info TESTS_JS_PACKAGE : $(TESTS_JS_PACKAGE)) - -DOCKER_IMAGE_ZONDAX=zondax/builder-bolos:latest -DOCKER_IMAGE_LEDGER=ghcr.io/ledgerhq/ledger-app-builder/ledger-app-builder:latest - -ifdef INTERACTIVE -INTERACTIVE_SETTING:="-i" -TTY_SETTING:="-t" -else -INTERACTIVE_SETTING:= -TTY_SETTING:= -endif - -UNAME_S := $(shell uname -s) -ifeq ($(UNAME_S),Linux) - NPROC=$(shell nproc) -endif -ifeq ($(UNAME_S),Darwin) - NPROC=$(shell sysctl -n hw.physicalcpu) -endif - -define run_docker - docker run $(TTY_SETTING) $(INTERACTIVE_SETTING) --rm \ - -e SCP_PRIVKEY=$(SCP_PRIVKEY) \ - -e BOLOS_SDK=$(1) \ - -e BOLOS_ENV=/opt/bolos \ - -u $(USERID) \ - -v $(shell pwd):/project \ - -e SUPPORT_SR25519=$(SUPPORT_SR25519) \ - -e SUBSTRATE_PARSER_FULL=$(SUBSTRATE_PARSER_FULL) \ - -e COIN=$(COIN) \ - -e APP_TESTING=$(APP_TESTING) \ - $(DOCKER_IMAGE_ZONDAX) "$(2)" -endef - -define run_docker_ledger - docker run $(TTY_SETTING) $(INTERACTIVE_SETTING) --rm \ - -v $(shell pwd):/app \ - $(DOCKER_IMAGE_LEDGER) "$(1)" -endef - -all: - @$(MAKE) clean_output - @$(MAKE) clean_build - @$(MAKE) buildS - @$(MAKE) clean_build - @$(MAKE) buildX - @$(MAKE) clean_build - @$(MAKE) buildS2 - -.PHONY: check_python -check_python: - @python -c 'import sys; sys.exit(3-sys.version_info.major)' || (echo "The python command does not point to Python 3"; exit 1) - -.PHONY: deps -deps: check_python - @echo "Install dependencies" - $(CURDIR)/deps/ledger-zxlib/scripts/install_deps.sh - -.PHONY: pull -pull: - docker pull $(DOCKER_IMAGE_ZONDAX) - docker pull $(DOCKER_IMAGE_LEDGER) - -.PHONY: ledger_lint -ledger_lint: - $(call run_docker_ledger,"scan-build --use-cc=clang -analyze-headers -enable-checker security -enable-checker unix -enable-checker valist -o scan-build --status-bugs make default") - -.PHONY: build_rustS -build_rustS: - $(call run_docker,$(DOCKER_BOLOS_SDKS),make -C $(DOCKER_APP_SRC) rust) - -.PHONY: build_rustX -build_rustX: - $(call run_docker,$(DOCKER_BOLOS_SDKX),make -C $(DOCKER_APP_SRC) rust) - -.PHONY: build_rustS2 -build_rustS2: - $(call run_docker,$(DOCKER_BOLOS_SDKS2),make -C $(DOCKER_APP_SRC) rust) - -.PHONY: convert_icon -convert_icon: - @convert $(LEDGER_SRC)/tmp.gif -monochrome -size 16x16 -depth 1 $(LEDGER_SRC)/nanos_icon.gif - @convert $(LEDGER_SRC)/nanos_icon.gif -crop 14x14+1+1 +repage -negate $(LEDGER_SRC)/nanox_icon.gif - -.PHONY: buildS -buildS: build_rustS - $(call run_docker,$(DOCKER_BOLOS_SDKS),make -j $(NPROC) -C $(DOCKER_APP_SRC)) - -.PHONY: buildX -buildX: build_rustX - $(call run_docker,$(DOCKER_BOLOS_SDKX),make -j $(NPROC) -C $(DOCKER_APP_SRC)) - -.PHONY: buildS2 -buildS2: build_rustS2 - $(call run_docker,$(DOCKER_BOLOS_SDKS2),make -j $(NPROC) -C $(DOCKER_APP_SRC)) - -.PHONY: clean_output -clean_output: - @echo "Removing output files" - @rm -f app/output/app* || true - -.PHONY: clean -clean_build: - $(call run_docker,$(DOCKER_BOLOS_SDKS),make -C $(DOCKER_APP_SRC) clean) - -.PHONY: clean -clean: clean_output clean_build - -.PHONY: listvariants -listvariants: - $(call run_docker,$(DOCKER_BOLOS_SDKS),make -C $(DOCKER_APP_SRC) listvariants) - -.PHONY: shellS -shellS: - $(call run_docker,$(DOCKER_BOLOS_SDKS) -t,bash) - -.PHONY: shellX -shellX: - $(call run_docker,$(DOCKER_BOLOS_SDKX) -t,bash) - -.PHONY: shellS2 -shellS2: - $(call run_docker,$(DOCKER_BOLOS_SDKS2) -t,bash) - -.PHONY: load -load: - ${LEDGER_SRC}/pkg/installer_s.sh load - -.PHONY: delete -delete: - ${LEDGER_SRC}/pkg/installer_s.sh delete - -.PHONY: loadX -loadX: - ${LEDGER_SRC}/pkg/installer_x.sh load - -.PHONY: deleteX -deleteX: - ${LEDGER_SRC}/pkg/installer_x.sh delete - -.PHONY: loadS2 -loadS2: - ${LEDGER_SRC}/pkg/installer_s2.sh load - -.PHONY: deleteS2 -deleteS2: - ${LEDGER_SRC}/pkg/installer_s2.sh delete - -.PHONY: show_info_recovery_mode -show_info_recovery_mode: - @echo "This command requires a Ledger Nano S in recovery mode. To go into recovery mode, follow:" - @echo " 1. Settings -> Device -> Reset all and confirm" - @echo " 2. Unplug device, press and hold the right button, plug-in again" - @echo " 3. Navigate to the main menu" - @echo "If everything was correct, no PIN needs to be entered." - -# This target will initialize the device with the integration testing mnemonic -.PHONY: dev_init -dev_init: show_info_recovery_mode - @echo "Initializing device with test mnemonic! WARNING TAKES 2 MINUTES AND REQUIRES RECOVERY MODE" - @python -m ledgerblue.hostOnboard --apdu --id 0 --prefix "" --passphrase "" --pin 5555 --words "equip will roof matter pink blind book anxiety banner elbow sun young" - -# This target will initialize the device with the secondary integration testing mnemonic (Bob) -.PHONY: dev_init_secondary -dev_init_secondary: check_python show_info_recovery_mode - @echo "Initializing device with secondary test mnemonic! WARNING TAKES 2 MINUTES AND REQUIRES RECOVERY MODE" - @python -m ledgerblue.hostOnboard --apdu --id 0 --prefix "" --passphrase "" --pin 5555 --words "elite vote proof agree february step sibling sand grocery axis false cup" - -# This target will setup a custom developer certificate -.PHONY: dev_ca -dev_ca: check_python - @python -m ledgerblue.setupCustomCA --targetId 0x31100004 --public $(SCP_PUBKEY) --name zondax - -# This target will setup a custom developer certificate -.PHONY: dev_caX -dev_caX: check_python - @python -m ledgerblue.setupCustomCA --targetId 0x33000004 --public $(SCP_PUBKEY) --name zondax - -.PHONY: dev_ca_delete -dev_ca_delete: check_python - @python -m ledgerblue.resetCustomCA --targetId 0x31100004 - -# This target will setup a custom developer certificate -.PHONY: dev_ca2 -dev_ca2: check_python - @python -m ledgerblue.setupCustomCA --targetId 0x33000004 --public $(SCP_PUBKEY) --name zondax - -.PHONY: dev_ca_delete2 -dev_ca_delete2: check_python - @python -m ledgerblue.resetCustomCA --targetId 0x33000004 - -########################## VUE Section ############################### - -.PHONY: vue_install_js_link -ifeq ($(TESTS_JS_DIR),) -vue_install_js_link: - @echo "No local package defined" -else -vue_install_js_link: - # First unlink everything - cd $(TESTS_JS_DIR) && yarn unlink || true - cd $(EXAMPLE_VUE_DIR) && yarn unlink $(TESTS_JS_PACKAGE) || true -# # Now build and link - cd $(TESTS_JS_DIR) && yarn install && yarn build && yarn link || true - cd $(EXAMPLE_VUE_DIR) && yarn link $(TESTS_JS_PACKAGE) && yarn install || true - @echo - # List linked packages - @echo - @cd $(EXAMPLE_VUE_DIR) && ( ls -l node_modules ; ls -l node_modules/@* ) | grep ^l || true - @echo -endif - -.PHONY: vue -vue: vue_install_js_link - cd $(EXAMPLE_VUE_DIR) && yarn install && yarn serve - -########################## VUE Section ############################### - -.PHONY: zemu_install_js_link -ifeq ($(TESTS_JS_DIR),) -zemu_install_js_link: - @echo "No local package defined" -else -zemu_install_js_link: - # First unlink everything - cd $(TESTS_JS_DIR) && yarn unlink || true - cd $(TESTS_ZEMU_DIR) && yarn unlink $(TESTS_JS_PACKAGE) || true - # Now build and link - cd $(TESTS_JS_DIR) && yarn install && yarn build && yarn link || true - cd $(TESTS_ZEMU_DIR) && yarn link $(TESTS_JS_PACKAGE) && yarn install || true - @echo - # List linked packages - @echo - @cd $(TESTS_ZEMU_DIR) && ( ls -l node_modules ; ls -l node_modules/@* ) | grep ^l || true - @echo -endif - -.PHONY: zemu_install -zemu_install: zemu_install_js_link - # and now install everything - cd $(TESTS_ZEMU_DIR) && yarn install - -.PHONY: zemu -zemu: - cd $(TESTS_ZEMU_DIR)/tools && node debug.mjs $(COIN) - -.PHONY: zemu_val -zemu_val: - cd $(TESTS_ZEMU_DIR)/tools && node debug_val.mjs - -.PHONY: zemu_debug -zemu_debug: - cd $(TESTS_ZEMU_DIR)/tools && node debug.mjs $(COIN) debug - -########################## TEST Section ############################### - -.PHONY: zemu_test -zemu_test: - cd $(TESTS_ZEMU_DIR) && yarn test$(COIN) - -.PHONY: rust_test -rust_test: - cd app/rust && cargo test - -.PHONY: cpp_test -cpp_test: - mkdir -p build && cd build && cmake -DCMAKE_BUILD_TYPE=Debug .. && make - cd build && GTEST_COLOR=1 ASAN_OPTIONS=detect_leaks=0 ctest -VV - -########################## FUZZING Section ############################### - -.PHONY: fuzz_build -fuzz_build: - cmake -B build -DCMAKE_C_COMPILER=clang-11 -DCMAKE_CXX_COMPILER=clang++-11 -DCMAKE_BUILD_TYPE=Debug -DENABLE_FUZZING=1 -DENABLE_SANITIZERS=1 . - make -C build - -.PHONY: fuzz -fuzz: fuzz_build - ./fuzz/run-fuzzers.py - -.PHONY: fuzz_crash -fuzz_crash: FUZZ_LOGGING=1 -fuzz_crash: fuzz_build - ./fuzz/run-fuzz-crashes.py diff --git a/deps/ledger-zxlib/include/apdu_codes.h b/deps/ledger-zxlib/include/apdu_codes.h deleted file mode 100644 index 5f55265..0000000 --- a/deps/ledger-zxlib/include/apdu_codes.h +++ /dev/null @@ -1,50 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#pragma once - -#include "inttypes.h" -#include "zxmacros.h" - -// Based on ISO7816 - -#define APDU_CODE_OK 0x9000 -#define APDU_CODE_BUSY 0x9001 - -#define APDU_CODE_EXECUTION_ERROR 0x6400 - -#define APDU_CODE_WRONG_LENGTH 0x6700 - -#define APDU_CODE_EMPTY_BUFFER 0x6982 -#define APDU_CODE_OUTPUT_BUFFER_TOO_SMALL 0x6983 -#define APDU_CODE_DATA_INVALID 0x6984 -#define APDU_CODE_CONDITIONS_NOT_SATISFIED 0x6985 -#define APDU_CODE_COMMAND_NOT_ALLOWED 0x6986 -#define APDU_CODE_TX_NOT_INITIALIZED 0x6987 - -#define APDU_CODE_BAD_KEY_HANDLE 0x6A80 -#define APDU_CODE_INVALIDP1P2 0x6B00 -#define APDU_CODE_INS_NOT_SUPPORTED 0x6D00 -#define APDU_CODE_CLA_NOT_SUPPORTED 0x6E00 - -#define APDU_CODE_UNKNOWN 0x6F00 -#define APDU_CODE_SIGN_VERIFY_ERROR 0x6F01 - - -__Z_INLINE void set_code(uint8_t *buffer, uint8_t offset, uint16_t value) { - *(buffer + offset) = (uint8_t) (value >> 8); - *(buffer + offset + 1) = (uint8_t) (value & 0xFF); -} diff --git a/deps/ledger-zxlib/include/app_mode.h b/deps/ledger-zxlib/include/app_mode.h deleted file mode 100644 index 11c72e8..0000000 --- a/deps/ledger-zxlib/include/app_mode.h +++ /dev/null @@ -1,37 +0,0 @@ -/******************************************************************************* -* (c) 2016 Ledger -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once -#include "zxmacros.h" -#include "stdbool.h" - -#ifdef __cplusplus -extern "C" { -#endif - -void app_mode_reset(); - -bool app_mode_expert(); - -void app_mode_set_expert(uint8_t val); - -bool app_mode_secret(); - -void app_mode_set_secret(uint8_t val); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/base58.h b/deps/ledger-zxlib/include/base58.h deleted file mode 100644 index b76412d..0000000 --- a/deps/ledger-zxlib/include/base58.h +++ /dev/null @@ -1,38 +0,0 @@ -/******************************************************************************* -* Ledger App - Bitcoin Wallet -* (c) 2019 Zondax GmbH -* (c) 2016-2019 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#pragma once - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -int decode_base58(const char *in, size_t length, - unsigned char *out, size_t *outlen); - -int encode_base58(const unsigned char *in, size_t length, - unsigned char *out, size_t *outlen); - -char encode_base58_clip(unsigned char v); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/base64.h b/deps/ledger-zxlib/include/base64.h deleted file mode 100644 index 99bc7c0..0000000 --- a/deps/ledger-zxlib/include/base64.h +++ /dev/null @@ -1,29 +0,0 @@ -/******************************************************************************* -* (c) 2020 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -uint16_t base64_encode(char *out, uint16_t outlen, const uint8_t *in, uint16_t inlen); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/bech32.h b/deps/ledger-zxlib/include/bech32.h deleted file mode 100644 index bd9fd14..0000000 --- a/deps/ledger-zxlib/include/bech32.h +++ /dev/null @@ -1,39 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include "zxerror.h" - -#define MAX_INPUT_SIZE 64 - -// the following function encodes directly from bytes -// it will internally convert from 8 to 5 bits and return a -// zero-terminated string in output - -zxerr_t bech32EncodeFromBytes(char *out, - size_t out_len, - const char *hrp, - const uint8_t *in, - size_t in_len, - uint8_t pad); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/bignum.h b/deps/ledger-zxlib/include/bignum.h deleted file mode 100644 index 9bf31f8..0000000 --- a/deps/ledger-zxlib/include/bignum.h +++ /dev/null @@ -1,35 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -bool_t bignumLittleEndian_bcdprint(char *outBuffer, uint16_t outBufferLen, const uint8_t *inBCD, uint16_t inBCDLen); -void bignumLittleEndian_to_bcd(uint8_t *bcdOut, uint16_t bcdOutLen, const uint8_t *binValue, uint16_t binValueLen); - -bool_t bignumBigEndian_bcdprint(char *outBuffer, uint16_t outBufferLen, const uint8_t *bcdIn, uint16_t bcdInLen); -void bignumBigEndian_to_bcd(uint8_t *bcdOut, uint16_t bcdOutLen, const uint8_t *binValue, uint16_t binValueLen); - - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/bittools.h b/deps/ledger-zxlib/include/bittools.h deleted file mode 100644 index 1903878..0000000 --- a/deps/ledger-zxlib/include/bittools.h +++ /dev/null @@ -1,37 +0,0 @@ -// This code has been adapted from: -/* Copyright (c) 2017 Pieter Wuille - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -int convert_bits(uint8_t *out, - size_t *outlen, - int outBits, - const uint8_t *in, - size_t inLen, - int inBits, int pad); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/buffering.h b/deps/ledger-zxlib/include/buffering.h deleted file mode 100644 index ec6e80c..0000000 --- a/deps/ledger-zxlib/include/buffering.h +++ /dev/null @@ -1,67 +0,0 @@ -/******************************************************************************* -* (c) 2016 Ledger -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -typedef struct { - uint8_t *data; - uint16_t size; - uint16_t pos; - uint8_t in_use: 1; -} buffer_state_t; - -/// Initialize buffer -/// \param ram_buffer -/// \param ram_buffer_size -/// \param flash_buffer -/// \param flash_buffer_size -void buffering_init(uint8_t *ram_buffer, - uint16_t ram_buffer_size, - uint8_t *flash_buffer, - uint16_t flash_buffer_size); - -/// Reset buffer -void buffering_reset(); - -/// Append data to the buffer -/// \param data -/// \param length -/// \return the number of appended bytes -int buffering_append(uint8_t *data, int length); - -/// buffering_get_ram_buffer -/// \return -buffer_state_t *buffering_get_ram_buffer(); - -/// buffering_get_flash_buffer -/// \return -buffer_state_t *buffering_get_flash_buffer(); - -/// buffering_get_buffer -/// \return -buffer_state_t *buffering_get_buffer(); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/hexutils.h b/deps/ledger-zxlib/include/hexutils.h deleted file mode 100644 index 3cbaad8..0000000 --- a/deps/ledger-zxlib/include/hexutils.h +++ /dev/null @@ -1,30 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#pragma once - -#include -#include - -#ifdef __cplusplus -extern "C" { -#endif - -size_t parseHexString(uint8_t *out, uint16_t outLen, const char *input); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/segwit_addr.h b/deps/ledger-zxlib/include/segwit_addr.h deleted file mode 100644 index f97bcfa..0000000 --- a/deps/ledger-zxlib/include/segwit_addr.h +++ /dev/null @@ -1,101 +0,0 @@ -/* Copyright (c) 2017 Pieter Wuille - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef _SEGWIT_ADDR_H_ -#define _SEGWIT_ADDR_H_ 1 - -#include - -/** Encode a SegWit address - * - * Out: output: Pointer to a buffer of size 73 + strlen(hrp) that will be - * updated to contain the null-terminated address. - * In: hrp: Pointer to the null-terminated human readable part to use - * (chain/network specific). - * ver: Version of the witness program (between 0 and 16 inclusive). - * prog: Data bytes for the witness program (between 2 and 40 bytes). - * prog_len: Number of data bytes in prog. - * Returns 1 if successful. - */ -int segwit_addr_encode( - char *output, - const char *hrp, - int ver, - const uint8_t *prog, - size_t prog_len -); - -/** Decode a SegWit address - * - * Out: ver: Pointer to an int that will be updated to contain the witness - * program version (between 0 and 16 inclusive). - * prog: Pointer to a buffer of size 40 that will be updated to - * contain the witness program bytes. - * prog_len: Pointer to a size_t that will be updated to contain the length - * of bytes in prog. - * hrp: Pointer to the null-terminated human readable part that is - * expected (chain/network specific). - * addr: Pointer to the null-terminated address. - * Returns 1 if successful. - */ -int segwit_addr_decode( - int *ver, - uint8_t *prog, - size_t *prog_len, - const char *hrp, - const char *addr -); - -/** Encode a Bech32 string - * - * Out: output: Pointer to a buffer of size strlen(hrp) + data_len + 8 that - * will be updated to contain the null-terminated Bech32 string. - * In: hrp : Pointer to the null-terminated human readable part. - * data : Pointer to an array of 5-bit values. - * data_len: Length of the data array. - * Returns 1 if successful. - */ -int bech32_encode( - char *output, - const char *hrp, - const uint8_t *data, - size_t data_len -); - -/** Decode a Bech32 string - * - * Out: hrp: Pointer to a buffer of size strlen(input) - 6. Will be - * updated to contain the null-terminated human readable part. - * data: Pointer to a buffer of size strlen(input) - 8 that will - * hold the encoded 5-bit data values. - * data_len: Pointer to a size_t that will be updated to be the number - * of entries in data. - * In: input: Pointer to a null-terminated Bech32 string. - * Returns 1 if successful. - */ -int bech32_decode( - char *hrp, - uint8_t *data, - size_t *data_len, - const char *input -); - -#endif diff --git a/deps/ledger-zxlib/include/sigutils.h b/deps/ledger-zxlib/include/sigutils.h deleted file mode 100644 index 862f809..0000000 --- a/deps/ledger-zxlib/include/sigutils.h +++ /dev/null @@ -1,41 +0,0 @@ -/******************************************************************************* -* (c) 2020 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { - no_error = 0, - invalid_derPrefix, - invalid_payloadLen, - invalid_rmaker, - invalid_rLen, - invalid_smarker, - invalid_sLen, -} err_convert_e; - -err_convert_e convertDERtoRSV(const uint8_t *inSignatureDER, - unsigned int inInfo, - uint8_t *outR, - uint8_t *outS, - uint8_t *outV); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/timeutils.h b/deps/ledger-zxlib/include/timeutils.h deleted file mode 100644 index d291477..0000000 --- a/deps/ledger-zxlib/include/timeutils.h +++ /dev/null @@ -1,62 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include "zxmacros.h" -#include "zxerror.h" - -__Z_INLINE const char *getMonth(uint8_t tm_mon) { - switch (tm_mon) { - case 1: - return "Jan"; - case 2: - return "Feb"; - case 3: - return "Mar"; - case 4: - return "Apr"; - case 5: - return "May"; - case 6: - return "Jun"; - case 7: - return "Jul"; - case 8: - return "Aug"; - case 9: - return "Sep"; - case 10: - return "Oct"; - case 11: - return "Nov"; - case 12: - return "Dec"; - default: - return "ERR"; - } -} - -zxerr_t printTime(char *out, uint16_t outLen, uint64_t t); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/utf8.h b/deps/ledger-zxlib/include/utf8.h deleted file mode 100644 index fe2e8d9..0000000 --- a/deps/ledger-zxlib/include/utf8.h +++ /dev/null @@ -1,1682 +0,0 @@ -/* The latest version of this library is available on GitHub; - * https://github.com/sheredom/utf8.h */ - -/* This is free and unencumbered software released into the public domain. - * - * Anyone is free to copy, modify, publish, use, compile, sell, or - * distribute this software, either in source code form or as a compiled - * binary, for any purpose, commercial or non-commercial, and by any - * means. - * - * In jurisdictions that recognize copyright laws, the author or authors - * of this software dedicate any and all copyright interest in the - * software to the public domain. We make this dedication for the benefit - * of the public at large and to the detriment of our heirs and - * successors. We intend this dedication to be an overt act of - * relinquishment in perpetuity of all present and future rights to this - * software under copyright law. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, - * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. - * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR - * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, - * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR - * OTHER DEALINGS IN THE SOFTWARE. - * - * For more information, please refer to */ - -#ifndef SHEREDOM_UTF8_H_INCLUDED -#define SHEREDOM_UTF8_H_INCLUDED - -#if defined(_MSC_VER) -#pragma warning(push) - -/* disable warning: no function prototype given: converting '()' to '(void)' */ -#pragma warning(disable : 4255) - -/* disable warning: '__cplusplus' is not defined as a preprocessor macro, - * replacing with '0' for '#if/#elif' */ -#pragma warning(disable : 4668) - -/* disable warning: bytes padding added after construct */ -#pragma warning(disable : 4820) -#endif - -#include -#include - -#if defined(_MSC_VER) -#pragma warning(pop) -#endif - -#if defined(_MSC_VER) && (_MSC_VER < 1920) -typedef __int32 utf8_int32_t; -#else -#include -typedef int32_t utf8_int32_t; -#endif - -#if defined(__clang__) -#pragma clang diagnostic push -#pragma clang diagnostic ignored "-Wold-style-cast" -#pragma clang diagnostic ignored "-Wcast-qual" -#endif - -#ifdef __cplusplus -extern "C" { -#endif - -#if defined(_MSC_VER) -#define utf8_nonnull -#define utf8_pure -#define utf8_restrict __restrict -#define utf8_weak __inline -#elif defined(__clang__) || defined(__GNUC__) -#define utf8_nonnull __attribute__((nonnull)) -#define utf8_pure __attribute__((pure)) -#define utf8_restrict __restrict__ -#define utf8_weak __attribute__((weak)) -#else -#error Non clang, non gcc, non MSVC compiler found! -#endif - -#ifdef __cplusplus -#define utf8_null NULL -#else -#define utf8_null 0 -#endif - -#if (defined(__cplusplus) && __cplusplus >= 201402L) -#define utf8_constexpr14 constexpr -#define utf8_constexpr14_impl constexpr -#else -/* constexpr and weak are incompatible. so only enable one of them */ -#define utf8_constexpr14 utf8_weak -#define utf8_constexpr14_impl -#endif - -#if defined(__cplusplus) && __cplusplus >= 202002L -using utf8_int8_t = char8_t; /* Introduced in C++20 */ -#else -typedef char utf8_int8_t; -#endif - -/* Return less than 0, 0, greater than 0 if src1 < src2, src1 == src2, src1 > - * src2 respectively, case insensitive. */ -utf8_constexpr14 utf8_nonnull utf8_pure int -utf8casecmp(const utf8_int8_t *src1, const utf8_int8_t *src2); - -/* Append the utf8 string src onto the utf8 string dst. */ -utf8_nonnull utf8_weak utf8_int8_t * -utf8cat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src); - -/* Find the first match of the utf8 codepoint chr in the utf8 string src. */ -utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * -utf8chr(const utf8_int8_t *src, utf8_int32_t chr); - -/* Return less than 0, 0, greater than 0 if src1 < src2, - * src1 == src2, src1 > src2 respectively. */ -utf8_constexpr14 utf8_nonnull utf8_pure int utf8cmp(const utf8_int8_t *src1, - const utf8_int8_t *src2); - -/* Copy the utf8 string src onto the memory allocated in dst. */ -utf8_nonnull utf8_weak utf8_int8_t * - utf8cpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src); - -/* Number of utf8 codepoints in the utf8 string src that consists entirely - * of utf8 codepoints not from the utf8 string reject. */ -utf8_constexpr14 utf8_nonnull utf8_pure size_t -utf8cspn(const utf8_int8_t *src, const utf8_int8_t *reject); - -/* Duplicate the utf8 string src by getting its size, malloc'ing a new buffer - * copying over the data, and returning that. Or 0 if malloc failed. */ -utf8_weak utf8_int8_t *utf8dup(const utf8_int8_t *src); - -/* Number of utf8 codepoints in the utf8 string str, - * excluding the null terminating byte. */ -utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8len(const utf8_int8_t *str); - -/* Similar to utf8len, except that only at most n bytes of src are looked. */ -utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8nlen(const utf8_int8_t *str, - size_t n); - -/* Return less than 0, 0, greater than 0 if src1 < src2, src1 == src2, src1 > - * src2 respectively, case insensitive. Checking at most n bytes of each utf8 - * string. */ -utf8_constexpr14 utf8_nonnull utf8_pure int -utf8ncasecmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n); - -/* Append the utf8 string src onto the utf8 string dst, - * writing at most n+1 bytes. Can produce an invalid utf8 - * string if n falls partway through a utf8 codepoint. */ -utf8_nonnull utf8_weak utf8_int8_t * - utf8ncat(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src, -size_t n); - -/* Return less than 0, 0, greater than 0 if src1 < src2, - * src1 == src2, src1 > src2 respectively. Checking at most n - * bytes of each utf8 string. */ -utf8_constexpr14 utf8_nonnull utf8_pure int -utf8ncmp(const utf8_int8_t *src1, const utf8_int8_t *src2, size_t n); - -/* Copy the utf8 string src onto the memory allocated in dst. - * Copies at most n bytes. If n falls partway through a utf8 - * codepoint, or if dst doesn't have enough room for a null - * terminator, the final string will be cut short to preserve - * utf8 validity. */ - -utf8_nonnull utf8_weak utf8_int8_t * - utf8ncpy(utf8_int8_t *utf8_restrict dst, const utf8_int8_t *utf8_restrict src, -size_t n); - -/* Similar to utf8dup, except that at most n bytes of src are copied. If src is - * longer than n, only n bytes are copied and a null byte is added. - * - * Returns a new string if successful, 0 otherwise */ -utf8_weak utf8_int8_t *utf8ndup(const utf8_int8_t *src, size_t n); - -/* Locates the first occurrence in the utf8 string str of any byte in the - * utf8 string accept, or 0 if no match was found. */ -utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * -utf8pbrk(const utf8_int8_t *str, const utf8_int8_t *accept); - -/* Find the last match of the utf8 codepoint chr in the utf8 string src. */ -utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * -utf8rchr(const utf8_int8_t *src, int chr); - -/* Number of bytes in the utf8 string str, - * including the null terminating byte. */ -utf8_constexpr14 utf8_nonnull utf8_pure size_t utf8size(const utf8_int8_t *str); - -/* Similar to utf8size, except that the null terminating byte is excluded. */ -utf8_constexpr14 utf8_nonnull utf8_pure size_t -utf8size_lazy(const utf8_int8_t *str); - -/* Similar to utf8size, except that only at most n bytes of src are looked and - * the null terminating byte is excluded. */ -utf8_constexpr14 utf8_nonnull utf8_pure size_t -utf8nsize_lazy(const utf8_int8_t *str, size_t n); - -/* Number of utf8 codepoints in the utf8 string src that consists entirely - * of utf8 codepoints from the utf8 string accept. */ -utf8_constexpr14 utf8_nonnull utf8_pure size_t -utf8spn(const utf8_int8_t *src, const utf8_int8_t *accept); - -/* The position of the utf8 string needle in the utf8 string haystack. */ -utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * -utf8str(const utf8_int8_t *haystack, const utf8_int8_t *needle); - -/* The position of the utf8 string needle in the utf8 string haystack, case - * insensitive. */ -utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * -utf8casestr(const utf8_int8_t *haystack, const utf8_int8_t *needle); - -/* Return 0 on success, or the position of the invalid - * utf8 codepoint on failure. */ -utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * -utf8valid(const utf8_int8_t *str); - -/* Similar to utf8valid, except that only at most n bytes of src are looked. */ -utf8_constexpr14 utf8_nonnull utf8_pure utf8_int8_t * -utf8nvalid(const utf8_int8_t *str, size_t n); - -/* Given a null-terminated string, makes the string valid by replacing invalid - * codepoints with a 1-byte replacement. Returns 0 on success. */ -utf8_nonnull utf8_weak int utf8makevalid(utf8_int8_t *str, -const utf8_int32_t replacement); - -/* Sets out_codepoint to the current utf8 codepoint in str, and returns the - * address of the next utf8 codepoint after the current one in str. */ -utf8_constexpr14 utf8_nonnull utf8_int8_t * -utf8codepoint(const utf8_int8_t *utf8_restrict str, - utf8_int32_t *utf8_restrict out_codepoint); - -/* Calculates the size of the next utf8 codepoint in str. */ -utf8_constexpr14 utf8_nonnull size_t -utf8codepointcalcsize(const utf8_int8_t *str); - -/* Returns the size of the given codepoint in bytes. */ -utf8_constexpr14 size_t utf8codepointsize(utf8_int32_t chr); - -/* Write a codepoint to the given string, and return the address to the next - * place after the written codepoint. Pass how many bytes left in the buffer to - * n. If there is not enough space for the codepoint, this function returns - * null. */ -utf8_nonnull utf8_weak utf8_int8_t * - utf8catcodepoint(utf8_int8_t *str, utf8_int32_t chr, size_t n); - -/* Returns 1 if the given character is lowercase, or 0 if it is not. */ -utf8_constexpr14 int utf8islower(utf8_int32_t chr); - -/* Returns 1 if the given character is uppercase, or 0 if it is not. */ -utf8_constexpr14 int utf8isupper(utf8_int32_t chr); - -/* Transform the given string into all lowercase codepoints. */ -utf8_nonnull utf8_weak void utf8lwr(utf8_int8_t *utf8_restrict str); - -/* Transform the given string into all uppercase codepoints. */ -utf8_nonnull utf8_weak void utf8upr(utf8_int8_t *utf8_restrict str); - -/* Make a codepoint lower case if possible. */ -utf8_constexpr14 utf8_int32_t utf8lwrcodepoint(utf8_int32_t cp); - -/* Make a codepoint upper case if possible. */ -utf8_constexpr14 utf8_int32_t utf8uprcodepoint(utf8_int32_t cp); - -/* Sets out_codepoint to the current utf8 codepoint in str, and returns the - * address of the previous utf8 codepoint before the current one in str. */ -utf8_constexpr14 utf8_nonnull utf8_int8_t * -utf8rcodepoint(const utf8_int8_t *utf8_restrict str, - utf8_int32_t *utf8_restrict out_codepoint); - -/* Duplicate the utf8 string src by getting its size, calling alloc_func_ptr to - * copy over data to a new buffer, and returning that. Or 0 if alloc_func_ptr - * returned null. */ -utf8_weak utf8_int8_t *utf8dup_ex(const utf8_int8_t *src, - utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, - size_t), - utf8_int8_t *user_data); - -/* Similar to utf8dup, except that at most n bytes of src are copied. If src is - * longer than n, only n bytes are copied and a null byte is added. - * - * Returns a new string if successful, 0 otherwise. */ -utf8_weak utf8_int8_t *utf8ndup_ex(const utf8_int8_t *src, size_t n, - utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, - size_t), - utf8_int8_t *user_data); - -#undef utf8_weak -#undef utf8_pure -#undef utf8_nonnull - -utf8_constexpr14_impl int utf8casecmp(const utf8_int8_t *src1, - const utf8_int8_t *src2) { - utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0, - src2_upr_cp = 0, src1_orig_cp = 0, src2_orig_cp = 0; - - for (;;) { - src1 = utf8codepoint(src1, &src1_orig_cp); - src2 = utf8codepoint(src2, &src2_orig_cp); - - /* lower the srcs if required */ - src1_lwr_cp = utf8lwrcodepoint(src1_orig_cp); - src2_lwr_cp = utf8lwrcodepoint(src2_orig_cp); - - /* lower the srcs if required */ - src1_upr_cp = utf8uprcodepoint(src1_orig_cp); - src2_upr_cp = utf8uprcodepoint(src2_orig_cp); - - /* check if the lowered codepoints match */ - if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) { - return 0; - } else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) { - continue; - } - - /* if they don't match, then we return the difference between the characters - */ - return src1_lwr_cp - src2_lwr_cp; - } -} - -utf8_int8_t *utf8cat(utf8_int8_t *utf8_restrict dst, -const utf8_int8_t *utf8_restrict src) { -utf8_int8_t *d = dst; -/* find the null terminating byte in dst */ -while ('\0' != *d) { -d++; -} - -/* overwriting the null terminating byte in dst, append src byte-by-byte */ -while ('\0' != *src) { -*d++ = *src++; -} - -/* write out a new null terminating byte into dst */ -*d = '\0'; - -return dst; -} - -utf8_constexpr14_impl utf8_int8_t *utf8chr(const utf8_int8_t *src, - utf8_int32_t chr) { - utf8_int8_t c[5] = {'\0', '\0', '\0', '\0', '\0'}; - - if (0 == chr) { - /* being asked to return position of null terminating byte, so - * just run s to the end, and return! */ - while ('\0' != *src) { - src++; - } - return (utf8_int8_t *)src; - } else if (0 == ((utf8_int32_t)0xffffff80 & chr)) { - /* 1-byte/7-bit ascii - * (0b0xxxxxxx) */ - c[0] = (utf8_int8_t)chr; - } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) { - /* 2-byte/11-bit utf8 code point - * (0b110xxxxx 0b10xxxxxx) */ - c[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)(chr >> 6)); - c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); - } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) { - /* 3-byte/16-bit utf8 code point - * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */ - c[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)(chr >> 12)); - c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); - c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); - } else { /* if (0 == ((int)0xffe00000 & chr)) { */ - /* 4-byte/21-bit utf8 code point - * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */ - c[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)(chr >> 18)); - c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f)); - c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); - c[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); - } - - /* we've made c into a 2 utf8 codepoint string, one for the chr we are - * seeking, another for the null terminating byte. Now use utf8str to - * search */ - return utf8str(src, c); -} - -utf8_constexpr14_impl int utf8cmp(const utf8_int8_t *src1, - const utf8_int8_t *src2) { - while (('\0' != *src1) || ('\0' != *src2)) { - if (*src1 < *src2) { - return -1; - } else if (*src1 > *src2) { - return 1; - } - - src1++; - src2++; - } - - /* both utf8 strings matched */ - return 0; -} - -utf8_constexpr14_impl int utf8coll(const utf8_int8_t *src1, - const utf8_int8_t *src2); - -utf8_int8_t *utf8cpy(utf8_int8_t *utf8_restrict dst, -const utf8_int8_t *utf8_restrict src) { -utf8_int8_t *d = dst; - -/* overwriting anything previously in dst, write byte-by-byte - * from src */ -while ('\0' != *src) { -*d++ = *src++; -} - -/* append null terminating byte */ -*d = '\0'; - -return dst; -} - -utf8_constexpr14_impl size_t utf8cspn(const utf8_int8_t *src, - const utf8_int8_t *reject) { - size_t chars = 0; - - while ('\0' != *src) { - const utf8_int8_t *r = reject; - size_t offset = 0; - - while ('\0' != *r) { - /* checking that if *r is the start of a utf8 codepoint - * (it is not 0b10xxxxxx) and we have successfully matched - * a previous character (0 < offset) - we found a match */ - if ((0x80 != (0xc0 & *r)) && (0 < offset)) { - return chars; - } else { - if (*r == src[offset]) { - /* part of a utf8 codepoint matched, so move our checking - * onwards to the next byte */ - offset++; - r++; - } else { - /* r could be in the middle of an unmatching utf8 code point, - * so we need to march it on to the next character beginning, */ - - do { - r++; - } while (0x80 == (0xc0 & *r)); - - /* reset offset too as we found a mismatch */ - offset = 0; - } - } - } - - /* found a match at the end of *r, so didn't get a chance to test it */ - if (0 < offset) { - return chars; - } - - /* the current utf8 codepoint in src did not match reject, but src - * could have been partway through a utf8 codepoint, so we need to - * march it onto the next utf8 codepoint starting byte */ - do { - src++; - } while ((0x80 == (0xc0 & *src))); - chars++; - } - - return chars; -} - -utf8_int8_t *utf8dup(const utf8_int8_t *src) { - return utf8dup_ex(src, utf8_null, utf8_null); -} - -utf8_int8_t *utf8dup_ex(const utf8_int8_t *src, - utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t), - utf8_int8_t *user_data) { -utf8_int8_t *n = utf8_null; - -/* figure out how many bytes (including the terminator) we need to copy first - */ -size_t bytes = utf8size(src); - -if (alloc_func_ptr) { -n = alloc_func_ptr(user_data, bytes); -} else { -n = (utf8_int8_t *)malloc(bytes); -} - -if (utf8_null == n) { -/* out of memory so we bail */ -return utf8_null; -} else { -bytes = 0; - -/* copy src byte-by-byte into our new utf8 string */ -while ('\0' != src[bytes]) { -n[bytes] = src[bytes]; -bytes++; -} - -/* append null terminating byte */ -n[bytes] = '\0'; -return n; -} -} - -utf8_constexpr14_impl utf8_int8_t *utf8fry(const utf8_int8_t *str); - -utf8_constexpr14_impl size_t utf8len(const utf8_int8_t *str) { - return utf8nlen(str, SIZE_MAX); -} - -utf8_constexpr14_impl size_t utf8nlen(const utf8_int8_t *str, size_t n) { - const utf8_int8_t *t = str; - size_t length = 0; - - while ((size_t)(str - t) < n && '\0' != *str) { - if (0xf0 == (0xf8 & *str)) { - /* 4-byte utf8 code point (began with 0b11110xxx) */ - str += 4; - } else if (0xe0 == (0xf0 & *str)) { - /* 3-byte utf8 code point (began with 0b1110xxxx) */ - str += 3; - } else if (0xc0 == (0xe0 & *str)) { - /* 2-byte utf8 code point (began with 0b110xxxxx) */ - str += 2; - } else { /* if (0x00 == (0x80 & *s)) { */ - /* 1-byte ascii (began with 0b0xxxxxxx) */ - str += 1; - } - - /* no matter the bytes we marched s forward by, it was - * only 1 utf8 codepoint */ - length++; - } - - if ((size_t)(str - t) > n) { - length--; - } - return length; -} - -utf8_constexpr14_impl int utf8ncasecmp(const utf8_int8_t *src1, - const utf8_int8_t *src2, size_t n) { - utf8_int32_t src1_lwr_cp = 0, src2_lwr_cp = 0, src1_upr_cp = 0, - src2_upr_cp = 0, src1_orig_cp = 0, src2_orig_cp = 0; - - do { - const utf8_int8_t *const s1 = src1; - const utf8_int8_t *const s2 = src2; - - /* first check that we have enough bytes left in n to contain an entire - * codepoint */ - if (0 == n) { - return 0; - } - - if ((1 == n) && ((0xc0 == (0xe0 & *s1)) || (0xc0 == (0xe0 & *s2)))) { - const utf8_int32_t c1 = (0xe0 & *s1); - const utf8_int32_t c2 = (0xe0 & *s2); - - if (c1 < c2) { - return c1 - c2; - } else { - return 0; - } - } - - if ((2 >= n) && ((0xe0 == (0xf0 & *s1)) || (0xe0 == (0xf0 & *s2)))) { - const utf8_int32_t c1 = (0xf0 & *s1); - const utf8_int32_t c2 = (0xf0 & *s2); - - if (c1 < c2) { - return c1 - c2; - } else { - return 0; - } - } - - if ((3 >= n) && ((0xf0 == (0xf8 & *s1)) || (0xf0 == (0xf8 & *s2)))) { - const utf8_int32_t c1 = (0xf8 & *s1); - const utf8_int32_t c2 = (0xf8 & *s2); - - if (c1 < c2) { - return c1 - c2; - } else { - return 0; - } - } - - src1 = utf8codepoint(src1, &src1_orig_cp); - src2 = utf8codepoint(src2, &src2_orig_cp); - n -= utf8codepointsize(src1_orig_cp); - - src1_lwr_cp = utf8lwrcodepoint(src1_orig_cp); - src2_lwr_cp = utf8lwrcodepoint(src2_orig_cp); - - src1_upr_cp = utf8uprcodepoint(src1_orig_cp); - src2_upr_cp = utf8uprcodepoint(src2_orig_cp); - - /* check if the lowered codepoints match */ - if ((0 == src1_orig_cp) && (0 == src2_orig_cp)) { - return 0; - } else if ((src1_lwr_cp == src2_lwr_cp) || (src1_upr_cp == src2_upr_cp)) { - continue; - } - - /* if they don't match, then we return the difference between the characters - */ - return src1_lwr_cp - src2_lwr_cp; - } while (0 < n); - - /* both utf8 strings matched */ - return 0; -} - -utf8_int8_t *utf8ncat(utf8_int8_t *utf8_restrict dst, -const utf8_int8_t *utf8_restrict src, size_t n) { -utf8_int8_t *d = dst; - -/* find the null terminating byte in dst */ -while ('\0' != *d) { -d++; -} - -/* overwriting the null terminating byte in dst, append src byte-by-byte - * stopping if we run out of space */ -do { -*d++ = *src++; -} while (('\0' != *src) && (0 != --n)); - -/* write out a new null terminating byte into dst */ -*d = '\0'; - -return dst; -} - -utf8_constexpr14_impl int utf8ncmp(const utf8_int8_t *src1, - const utf8_int8_t *src2, size_t n) { - while ((0 != n--) && (('\0' != *src1) || ('\0' != *src2))) { - if (*src1 < *src2) { - return -1; - } else if (*src1 > *src2) { - return 1; - } - - src1++; - src2++; - } - - /* both utf8 strings matched */ - return 0; -} - -utf8_int8_t *utf8ncpy(utf8_int8_t *utf8_restrict dst, -const utf8_int8_t *utf8_restrict src, size_t n) { -utf8_int8_t *d = dst; -size_t index = 0, check_index = 0; - -if (n == 0) { -return dst; -} - -/* overwriting anything previously in dst, write byte-by-byte - * from src */ -for (index = 0; index < n; index++) { -d[index] = src[index]; -if ('\0' == src[index]) { -break; -} -} - -for (check_index = index - 1; -check_index > 0 && 0x80 == (0xc0 & d[check_index]); check_index--) { -/* just moving the index */ -} - -if (check_index < index && -(index - check_index) < utf8codepointsize(d[check_index])) { -index = check_index; -} - -/* append null terminating byte */ -for (; index < n; index++) { -d[index] = 0; -} - -return dst; -} - -utf8_int8_t *utf8ndup(const utf8_int8_t *src, size_t n) { - return utf8ndup_ex(src, n, utf8_null, utf8_null); -} - -utf8_int8_t *utf8ndup_ex(const utf8_int8_t *src, size_t n, - utf8_int8_t *(*alloc_func_ptr)(utf8_int8_t *, size_t), - utf8_int8_t *user_data) { -utf8_int8_t *c = utf8_null; -size_t bytes = 0; - -/* Find the end of the string or stop when n is reached */ -while ('\0' != src[bytes] && bytes < n) { -bytes++; -} - -/* In case bytes is actually less than n, we need to set it - * to be used later in the copy byte by byte. */ -n = bytes; - -if (alloc_func_ptr) { -c = alloc_func_ptr(user_data, bytes + 1); -} else { -c = (utf8_int8_t *)malloc(bytes + 1); -} - -if (utf8_null == c) { -/* out of memory so we bail */ -return utf8_null; -} - -bytes = 0; - -/* copy src byte-by-byte into our new utf8 string */ -while ('\0' != src[bytes] && bytes < n) { -c[bytes] = src[bytes]; -bytes++; -} - -/* append null terminating byte */ -c[bytes] = '\0'; -return c; -} - -utf8_constexpr14_impl utf8_int8_t *utf8rchr(const utf8_int8_t *src, int chr) { - - utf8_int8_t *match = utf8_null; - utf8_int8_t c[5] = {'\0', '\0', '\0', '\0', '\0'}; - - if (0 == chr) { - /* being asked to return position of null terminating byte, so - * just run s to the end, and return! */ - while ('\0' != *src) { - src++; - } - return (utf8_int8_t *)src; - } else if (0 == ((int)0xffffff80 & chr)) { - /* 1-byte/7-bit ascii - * (0b0xxxxxxx) */ - c[0] = (utf8_int8_t)chr; - } else if (0 == ((int)0xfffff800 & chr)) { - /* 2-byte/11-bit utf8 code point - * (0b110xxxxx 0b10xxxxxx) */ - c[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)(chr >> 6)); - c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); - } else if (0 == ((int)0xffff0000 & chr)) { - /* 3-byte/16-bit utf8 code point - * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */ - c[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)(chr >> 12)); - c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); - c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); - } else { /* if (0 == ((int)0xffe00000 & chr)) { */ - /* 4-byte/21-bit utf8 code point - * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */ - c[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)(chr >> 18)); - c[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f)); - c[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); - c[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); - } - - /* we've created a 2 utf8 codepoint string in c that is - * the utf8 character asked for by chr, and a null - * terminating byte */ - - while ('\0' != *src) { - size_t offset = 0; - - while (src[offset] == c[offset]) { - offset++; - } - - if ('\0' == c[offset]) { - /* we found a matching utf8 code point */ - match = (utf8_int8_t *)src; - src += offset; - } else { - src += offset; - - /* need to march s along to next utf8 codepoint start - * (the next byte that doesn't match 0b10xxxxxx) */ - if ('\0' != *src) { - do { - src++; - } while (0x80 == (0xc0 & *src)); - } - } - } - - /* return the last match we found (or 0 if no match was found) */ - return match; -} - -utf8_constexpr14_impl utf8_int8_t *utf8pbrk(const utf8_int8_t *str, - const utf8_int8_t *accept) { - while ('\0' != *str) { - const utf8_int8_t *a = accept; - size_t offset = 0; - - while ('\0' != *a) { - /* checking that if *a is the start of a utf8 codepoint - * (it is not 0b10xxxxxx) and we have successfully matched - * a previous character (0 < offset) - we found a match */ - if ((0x80 != (0xc0 & *a)) && (0 < offset)) { - return (utf8_int8_t *)str; - } else { - if (*a == str[offset]) { - /* part of a utf8 codepoint matched, so move our checking - * onwards to the next byte */ - offset++; - a++; - } else { - /* r could be in the middle of an unmatching utf8 code point, - * so we need to march it on to the next character beginning, */ - - do { - a++; - } while (0x80 == (0xc0 & *a)); - - /* reset offset too as we found a mismatch */ - offset = 0; - } - } - } - - /* we found a match on the last utf8 codepoint */ - if (0 < offset) { - return (utf8_int8_t *)str; - } - - /* the current utf8 codepoint in src did not match accept, but src - * could have been partway through a utf8 codepoint, so we need to - * march it onto the next utf8 codepoint starting byte */ - do { - str++; - } while ((0x80 == (0xc0 & *str))); - } - - return utf8_null; -} - -utf8_constexpr14_impl size_t utf8size(const utf8_int8_t *str) { - return utf8size_lazy(str) + 1; -} - -utf8_constexpr14_impl size_t utf8size_lazy(const utf8_int8_t *str) { - return utf8nsize_lazy(str, SIZE_MAX); -} - -utf8_constexpr14_impl size_t utf8nsize_lazy(const utf8_int8_t *str, size_t n) { - size_t size = 0; - while (size < n && '\0' != str[size]) { - size++; - } - return size; -} - -utf8_constexpr14_impl size_t utf8spn(const utf8_int8_t *src, - const utf8_int8_t *accept) { - size_t chars = 0; - - while ('\0' != *src) { - const utf8_int8_t *a = accept; - size_t offset = 0; - - while ('\0' != *a) { - /* checking that if *r is the start of a utf8 codepoint - * (it is not 0b10xxxxxx) and we have successfully matched - * a previous character (0 < offset) - we found a match */ - if ((0x80 != (0xc0 & *a)) && (0 < offset)) { - /* found a match, so increment the number of utf8 codepoints - * that have matched and stop checking whether any other utf8 - * codepoints in a match */ - chars++; - src += offset; - offset = 0; - break; - } else { - if (*a == src[offset]) { - offset++; - a++; - } else { - /* a could be in the middle of an unmatching utf8 codepoint, - * so we need to march it on to the next character beginning, */ - do { - a++; - } while (0x80 == (0xc0 & *a)); - - /* reset offset too as we found a mismatch */ - offset = 0; - } - } - } - - /* found a match at the end of *a, so didn't get a chance to test it */ - if (0 < offset) { - chars++; - src += offset; - continue; - } - - /* if a got to its terminating null byte, then we didn't find a match. - * Return the current number of matched utf8 codepoints */ - if ('\0' == *a) { - return chars; - } - } - - return chars; -} - -utf8_constexpr14_impl utf8_int8_t *utf8str(const utf8_int8_t *haystack, - const utf8_int8_t *needle) { - utf8_int32_t throwaway_codepoint = 0; - - /* if needle has no utf8 codepoints before the null terminating - * byte then return haystack */ - if ('\0' == *needle) { - return (utf8_int8_t *)haystack; - } - - while ('\0' != *haystack) { - const utf8_int8_t *maybeMatch = haystack; - const utf8_int8_t *n = needle; - - while (*haystack == *n && (*haystack != '\0' && *n != '\0')) { - n++; - haystack++; - } - - if ('\0' == *n) { - /* we found the whole utf8 string for needle in haystack at - * maybeMatch, so return it */ - return (utf8_int8_t *)maybeMatch; - } else { - /* h could be in the middle of an unmatching utf8 codepoint, - * so we need to march it on to the next character beginning - * starting from the current character */ - haystack = utf8codepoint(maybeMatch, &throwaway_codepoint); - } - } - - /* no match */ - return utf8_null; -} - -utf8_constexpr14_impl utf8_int8_t *utf8casestr(const utf8_int8_t *haystack, - const utf8_int8_t *needle) { - /* if needle has no utf8 codepoints before the null terminating - * byte then return haystack */ - if ('\0' == *needle) { - return (utf8_int8_t *)haystack; - } - - for (;;) { - const utf8_int8_t *maybeMatch = haystack; - const utf8_int8_t *n = needle; - utf8_int32_t h_cp = 0, n_cp = 0; - - /* Get the next code point and track it */ - const utf8_int8_t *nextH = haystack = utf8codepoint(haystack, &h_cp); - n = utf8codepoint(n, &n_cp); - - while ((0 != h_cp) && (0 != n_cp)) { - h_cp = utf8lwrcodepoint(h_cp); - n_cp = utf8lwrcodepoint(n_cp); - - /* if we find a mismatch, bail out! */ - if (h_cp != n_cp) { - break; - } - - haystack = utf8codepoint(haystack, &h_cp); - n = utf8codepoint(n, &n_cp); - } - - if (0 == n_cp) { - /* we found the whole utf8 string for needle in haystack at - * maybeMatch, so return it */ - return (utf8_int8_t *)maybeMatch; - } - - if (0 == h_cp) { - /* no match */ - return utf8_null; - } - - /* Roll back to the next code point in the haystack to test */ - haystack = nextH; - } -} - -utf8_constexpr14_impl utf8_int8_t *utf8valid(const utf8_int8_t *str) { - return utf8nvalid(str, SIZE_MAX); -} - -utf8_constexpr14_impl utf8_int8_t *utf8nvalid(const utf8_int8_t *str, - size_t n) { - const utf8_int8_t *t = str; - size_t consumed = 0, remained = 0; - - while ((void)(consumed = (size_t)(str - t)), consumed < n && '\0' != *str) { - remained = n - consumed; - - if (0xf0 == (0xf8 & *str)) { - /* ensure that there's 4 bytes or more remained */ - if (remained < 4) { - return (utf8_int8_t *)str; - } - - /* ensure each of the 3 following bytes in this 4-byte - * utf8 codepoint began with 0b10xxxxxx */ - if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2])) || - (0x80 != (0xc0 & str[3]))) { - return (utf8_int8_t *)str; - } - - /* ensure that our utf8 codepoint ended after 4 bytes */ - if (0x80 == (0xc0 & str[4])) { - return (utf8_int8_t *)str; - } - - /* ensure that the top 5 bits of this 4-byte utf8 - * codepoint were not 0, as then we could have used - * one of the smaller encodings */ - if ((0 == (0x07 & str[0])) && (0 == (0x30 & str[1]))) { - return (utf8_int8_t *)str; - } - - /* 4-byte utf8 code point (began with 0b11110xxx) */ - str += 4; - } else if (0xe0 == (0xf0 & *str)) { - /* ensure that there's 3 bytes or more remained */ - if (remained < 3) { - return (utf8_int8_t *)str; - } - - /* ensure each of the 2 following bytes in this 3-byte - * utf8 codepoint began with 0b10xxxxxx */ - if ((0x80 != (0xc0 & str[1])) || (0x80 != (0xc0 & str[2]))) { - return (utf8_int8_t *)str; - } - - /* ensure that our utf8 codepoint ended after 3 bytes */ - if (0x80 == (0xc0 & str[3])) { - return (utf8_int8_t *)str; - } - - /* ensure that the top 5 bits of this 3-byte utf8 - * codepoint were not 0, as then we could have used - * one of the smaller encodings */ - if ((0 == (0x0f & str[0])) && (0 == (0x20 & str[1]))) { - return (utf8_int8_t *)str; - } - - /* 3-byte utf8 code point (began with 0b1110xxxx) */ - str += 3; - } else if (0xc0 == (0xe0 & *str)) { - /* ensure that there's 2 bytes or more remained */ - if (remained < 2) { - return (utf8_int8_t *)str; - } - - /* ensure the 1 following byte in this 2-byte - * utf8 codepoint began with 0b10xxxxxx */ - if (0x80 != (0xc0 & str[1])) { - return (utf8_int8_t *)str; - } - - /* ensure that our utf8 codepoint ended after 2 bytes */ - if (0x80 == (0xc0 & str[2])) { - return (utf8_int8_t *)str; - } - - /* ensure that the top 4 bits of this 2-byte utf8 - * codepoint were not 0, as then we could have used - * one of the smaller encodings */ - if (0 == (0x1e & str[0])) { - return (utf8_int8_t *)str; - } - - /* 2-byte utf8 code point (began with 0b110xxxxx) */ - str += 2; - } else if (0x00 == (0x80 & *str)) { - /* 1-byte ascii (began with 0b0xxxxxxx) */ - str += 1; - } else { - /* we have an invalid 0b1xxxxxxx utf8 code point entry */ - return (utf8_int8_t *)str; - } - } - - return utf8_null; -} - -int utf8makevalid(utf8_int8_t *str, const utf8_int32_t replacement) { -utf8_int8_t *read = str; -utf8_int8_t *write = read; -const utf8_int8_t r = (utf8_int8_t)replacement; -utf8_int32_t codepoint = 0; - -if (replacement > 0x7f) { -return -1; -} - -while ('\0' != *read) { -if (0xf0 == (0xf8 & *read)) { -/* ensure each of the 3 following bytes in this 4-byte - * utf8 codepoint began with 0b10xxxxxx */ -if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2])) || -(0x80 != (0xc0 & read[3]))) { -*write++ = r; -read++; -continue; -} - -/* 4-byte utf8 code point (began with 0b11110xxx) */ -read = utf8codepoint(read, &codepoint); -write = utf8catcodepoint(write, codepoint, 4); -} else if (0xe0 == (0xf0 & *read)) { -/* ensure each of the 2 following bytes in this 3-byte - * utf8 codepoint began with 0b10xxxxxx */ -if ((0x80 != (0xc0 & read[1])) || (0x80 != (0xc0 & read[2]))) { -*write++ = r; -read++; -continue; -} - -/* 3-byte utf8 code point (began with 0b1110xxxx) */ -read = utf8codepoint(read, &codepoint); -write = utf8catcodepoint(write, codepoint, 3); -} else if (0xc0 == (0xe0 & *read)) { -/* ensure the 1 following byte in this 2-byte - * utf8 codepoint began with 0b10xxxxxx */ -if (0x80 != (0xc0 & read[1])) { -*write++ = r; -read++; -continue; -} - -/* 2-byte utf8 code point (began with 0b110xxxxx) */ -read = utf8codepoint(read, &codepoint); -write = utf8catcodepoint(write, codepoint, 2); -} else if (0x00 == (0x80 & *read)) { -/* 1-byte ascii (began with 0b0xxxxxxx) */ -read = utf8codepoint(read, &codepoint); -write = utf8catcodepoint(write, codepoint, 1); -} else { -/* if we got here then we've got a dangling continuation (0b10xxxxxx) */ -*write++ = r; -read++; -continue; -} -} - -*write = '\0'; - -return 0; -} - -utf8_constexpr14_impl utf8_int8_t * -utf8codepoint(const utf8_int8_t *utf8_restrict str, - utf8_int32_t *utf8_restrict out_codepoint) { - if (0xf0 == (0xf8 & str[0])) { - /* 4 byte utf8 codepoint */ - *out_codepoint = ((0x07 & str[0]) << 18) | ((0x3f & str[1]) << 12) | - ((0x3f & str[2]) << 6) | (0x3f & str[3]); - str += 4; - } else if (0xe0 == (0xf0 & str[0])) { - /* 3 byte utf8 codepoint */ - *out_codepoint = - ((0x0f & str[0]) << 12) | ((0x3f & str[1]) << 6) | (0x3f & str[2]); - str += 3; - } else if (0xc0 == (0xe0 & str[0])) { - /* 2 byte utf8 codepoint */ - *out_codepoint = ((0x1f & str[0]) << 6) | (0x3f & str[1]); - str += 2; - } else { - /* 1 byte utf8 codepoint otherwise */ - *out_codepoint = str[0]; - str += 1; - } - - return (utf8_int8_t *)str; -} - -utf8_constexpr14_impl size_t utf8codepointcalcsize(const utf8_int8_t *str) { - if (0xf0 == (0xf8 & str[0])) { - /* 4 byte utf8 codepoint */ - return 4; - } else if (0xe0 == (0xf0 & str[0])) { - /* 3 byte utf8 codepoint */ - return 3; - } else if (0xc0 == (0xe0 & str[0])) { - /* 2 byte utf8 codepoint */ - return 2; - } - - /* 1 byte utf8 codepoint otherwise */ - return 1; -} - -utf8_constexpr14_impl size_t utf8codepointsize(utf8_int32_t chr) { - if (0 == ((utf8_int32_t)0xffffff80 & chr)) { - return 1; - } else if (0 == ((utf8_int32_t)0xfffff800 & chr)) { - return 2; - } else if (0 == ((utf8_int32_t)0xffff0000 & chr)) { - return 3; - } else { /* if (0 == ((int)0xffe00000 & chr)) { */ - return 4; - } -} - -utf8_int8_t *utf8catcodepoint(utf8_int8_t *str, utf8_int32_t chr, size_t n) { -if (0 == ((utf8_int32_t)0xffffff80 & chr)) { -/* 1-byte/7-bit ascii - * (0b0xxxxxxx) */ -if (n < 1) { -return utf8_null; -} -str[0] = (utf8_int8_t)chr; -str += 1; -} else if (0 == ((utf8_int32_t)0xfffff800 & chr)) { -/* 2-byte/11-bit utf8 code point - * (0b110xxxxx 0b10xxxxxx) */ -if (n < 2) { -return utf8_null; -} -str[0] = (utf8_int8_t)(0xc0 | (utf8_int8_t)((chr >> 6) & 0x1f)); -str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); -str += 2; -} else if (0 == ((utf8_int32_t)0xffff0000 & chr)) { -/* 3-byte/16-bit utf8 code point - * (0b1110xxxx 0b10xxxxxx 0b10xxxxxx) */ -if (n < 3) { -return utf8_null; -} -str[0] = (utf8_int8_t)(0xe0 | (utf8_int8_t)((chr >> 12) & 0x0f)); -str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); -str[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); -str += 3; -} else { /* if (0 == ((int)0xffe00000 & chr)) { */ -/* 4-byte/21-bit utf8 code point - * (0b11110xxx 0b10xxxxxx 0b10xxxxxx 0b10xxxxxx) */ -if (n < 4) { -return utf8_null; -} -str[0] = (utf8_int8_t)(0xf0 | (utf8_int8_t)((chr >> 18) & 0x07)); -str[1] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 12) & 0x3f)); -str[2] = (utf8_int8_t)(0x80 | (utf8_int8_t)((chr >> 6) & 0x3f)); -str[3] = (utf8_int8_t)(0x80 | (utf8_int8_t)(chr & 0x3f)); -str += 4; -} - -return str; -} - -utf8_constexpr14_impl int utf8islower(utf8_int32_t chr) { - return chr != utf8uprcodepoint(chr); -} - -utf8_constexpr14_impl int utf8isupper(utf8_int32_t chr) { - return chr != utf8lwrcodepoint(chr); -} - -void utf8lwr(utf8_int8_t *utf8_restrict str) { -utf8_int32_t cp = 0; -utf8_int8_t *pn = utf8codepoint(str, &cp); - -while (cp != 0) { -const utf8_int32_t lwr_cp = utf8lwrcodepoint(cp); -const size_t size = utf8codepointsize(lwr_cp); - -if (lwr_cp != cp) { -utf8catcodepoint(str, lwr_cp, size); -} - -str = pn; -pn = utf8codepoint(str, &cp); -} -} - -void utf8upr(utf8_int8_t *utf8_restrict str) { -utf8_int32_t cp = 0; -utf8_int8_t *pn = utf8codepoint(str, &cp); - -while (cp != 0) { -const utf8_int32_t lwr_cp = utf8uprcodepoint(cp); -const size_t size = utf8codepointsize(lwr_cp); - -if (lwr_cp != cp) { -utf8catcodepoint(str, lwr_cp, size); -} - -str = pn; -pn = utf8codepoint(str, &cp); -} -} - -utf8_constexpr14_impl utf8_int32_t utf8lwrcodepoint(utf8_int32_t cp) { - if (((0x0041 <= cp) && (0x005a >= cp)) || - ((0x00c0 <= cp) && (0x00d6 >= cp)) || - ((0x00d8 <= cp) && (0x00de >= cp)) || - ((0x0391 <= cp) && (0x03a1 >= cp)) || - ((0x03a3 <= cp) && (0x03ab >= cp)) || - ((0x0410 <= cp) && (0x042f >= cp))) { - cp += 32; - } else if ((0x0400 <= cp) && (0x040f >= cp)) { - cp += 80; - } else if (((0x0100 <= cp) && (0x012f >= cp)) || - ((0x0132 <= cp) && (0x0137 >= cp)) || - ((0x014a <= cp) && (0x0177 >= cp)) || - ((0x0182 <= cp) && (0x0185 >= cp)) || - ((0x01a0 <= cp) && (0x01a5 >= cp)) || - ((0x01de <= cp) && (0x01ef >= cp)) || - ((0x01f8 <= cp) && (0x021f >= cp)) || - ((0x0222 <= cp) && (0x0233 >= cp)) || - ((0x0246 <= cp) && (0x024f >= cp)) || - ((0x03d8 <= cp) && (0x03ef >= cp)) || - ((0x0460 <= cp) && (0x0481 >= cp)) || - ((0x048a <= cp) && (0x04ff >= cp))) { - cp |= 0x1; - } else if (((0x0139 <= cp) && (0x0148 >= cp)) || - ((0x0179 <= cp) && (0x017e >= cp)) || - ((0x01af <= cp) && (0x01b0 >= cp)) || - ((0x01b3 <= cp) && (0x01b6 >= cp)) || - ((0x01cd <= cp) && (0x01dc >= cp))) { - cp += 1; - cp &= ~0x1; - } else { - switch (cp) { - default: - break; - case 0x0178: - cp = 0x00ff; - break; - case 0x0243: - cp = 0x0180; - break; - case 0x018e: - cp = 0x01dd; - break; - case 0x023d: - cp = 0x019a; - break; - case 0x0220: - cp = 0x019e; - break; - case 0x01b7: - cp = 0x0292; - break; - case 0x01c4: - cp = 0x01c6; - break; - case 0x01c7: - cp = 0x01c9; - break; - case 0x01ca: - cp = 0x01cc; - break; - case 0x01f1: - cp = 0x01f3; - break; - case 0x01f7: - cp = 0x01bf; - break; - case 0x0187: - cp = 0x0188; - break; - case 0x018b: - cp = 0x018c; - break; - case 0x0191: - cp = 0x0192; - break; - case 0x0198: - cp = 0x0199; - break; - case 0x01a7: - cp = 0x01a8; - break; - case 0x01ac: - cp = 0x01ad; - break; - case 0x01af: - cp = 0x01b0; - break; - case 0x01b8: - cp = 0x01b9; - break; - case 0x01bc: - cp = 0x01bd; - break; - case 0x01f4: - cp = 0x01f5; - break; - case 0x023b: - cp = 0x023c; - break; - case 0x0241: - cp = 0x0242; - break; - case 0x03fd: - cp = 0x037b; - break; - case 0x03fe: - cp = 0x037c; - break; - case 0x03ff: - cp = 0x037d; - break; - case 0x037f: - cp = 0x03f3; - break; - case 0x0386: - cp = 0x03ac; - break; - case 0x0388: - cp = 0x03ad; - break; - case 0x0389: - cp = 0x03ae; - break; - case 0x038a: - cp = 0x03af; - break; - case 0x038c: - cp = 0x03cc; - break; - case 0x038e: - cp = 0x03cd; - break; - case 0x038f: - cp = 0x03ce; - break; - case 0x0370: - cp = 0x0371; - break; - case 0x0372: - cp = 0x0373; - break; - case 0x0376: - cp = 0x0377; - break; - case 0x03f4: - cp = 0x03b8; - break; - case 0x03cf: - cp = 0x03d7; - break; - case 0x03f9: - cp = 0x03f2; - break; - case 0x03f7: - cp = 0x03f8; - break; - case 0x03fa: - cp = 0x03fb; - break; - } - } - - return cp; -} - -utf8_constexpr14_impl utf8_int32_t utf8uprcodepoint(utf8_int32_t cp) { - if (((0x0061 <= cp) && (0x007a >= cp)) || - ((0x00e0 <= cp) && (0x00f6 >= cp)) || - ((0x00f8 <= cp) && (0x00fe >= cp)) || - ((0x03b1 <= cp) && (0x03c1 >= cp)) || - ((0x03c3 <= cp) && (0x03cb >= cp)) || - ((0x0430 <= cp) && (0x044f >= cp))) { - cp -= 32; - } else if ((0x0450 <= cp) && (0x045f >= cp)) { - cp -= 80; - } else if (((0x0100 <= cp) && (0x012f >= cp)) || - ((0x0132 <= cp) && (0x0137 >= cp)) || - ((0x014a <= cp) && (0x0177 >= cp)) || - ((0x0182 <= cp) && (0x0185 >= cp)) || - ((0x01a0 <= cp) && (0x01a5 >= cp)) || - ((0x01de <= cp) && (0x01ef >= cp)) || - ((0x01f8 <= cp) && (0x021f >= cp)) || - ((0x0222 <= cp) && (0x0233 >= cp)) || - ((0x0246 <= cp) && (0x024f >= cp)) || - ((0x03d8 <= cp) && (0x03ef >= cp)) || - ((0x0460 <= cp) && (0x0481 >= cp)) || - ((0x048a <= cp) && (0x04ff >= cp))) { - cp &= ~0x1; - } else if (((0x0139 <= cp) && (0x0148 >= cp)) || - ((0x0179 <= cp) && (0x017e >= cp)) || - ((0x01af <= cp) && (0x01b0 >= cp)) || - ((0x01b3 <= cp) && (0x01b6 >= cp)) || - ((0x01cd <= cp) && (0x01dc >= cp))) { - cp -= 1; - cp |= 0x1; - } else { - switch (cp) { - default: - break; - case 0x00ff: - cp = 0x0178; - break; - case 0x0180: - cp = 0x0243; - break; - case 0x01dd: - cp = 0x018e; - break; - case 0x019a: - cp = 0x023d; - break; - case 0x019e: - cp = 0x0220; - break; - case 0x0292: - cp = 0x01b7; - break; - case 0x01c6: - cp = 0x01c4; - break; - case 0x01c9: - cp = 0x01c7; - break; - case 0x01cc: - cp = 0x01ca; - break; - case 0x01f3: - cp = 0x01f1; - break; - case 0x01bf: - cp = 0x01f7; - break; - case 0x0188: - cp = 0x0187; - break; - case 0x018c: - cp = 0x018b; - break; - case 0x0192: - cp = 0x0191; - break; - case 0x0199: - cp = 0x0198; - break; - case 0x01a8: - cp = 0x01a7; - break; - case 0x01ad: - cp = 0x01ac; - break; - case 0x01b0: - cp = 0x01af; - break; - case 0x01b9: - cp = 0x01b8; - break; - case 0x01bd: - cp = 0x01bc; - break; - case 0x01f5: - cp = 0x01f4; - break; - case 0x023c: - cp = 0x023b; - break; - case 0x0242: - cp = 0x0241; - break; - case 0x037b: - cp = 0x03fd; - break; - case 0x037c: - cp = 0x03fe; - break; - case 0x037d: - cp = 0x03ff; - break; - case 0x03f3: - cp = 0x037f; - break; - case 0x03ac: - cp = 0x0386; - break; - case 0x03ad: - cp = 0x0388; - break; - case 0x03ae: - cp = 0x0389; - break; - case 0x03af: - cp = 0x038a; - break; - case 0x03cc: - cp = 0x038c; - break; - case 0x03cd: - cp = 0x038e; - break; - case 0x03ce: - cp = 0x038f; - break; - case 0x0371: - cp = 0x0370; - break; - case 0x0373: - cp = 0x0372; - break; - case 0x0377: - cp = 0x0376; - break; - case 0x03d1: - cp = 0x0398; - break; - case 0x03d7: - cp = 0x03cf; - break; - case 0x03f2: - cp = 0x03f9; - break; - case 0x03f8: - cp = 0x03f7; - break; - case 0x03fb: - cp = 0x03fa; - break; - } - } - - return cp; -} - -utf8_constexpr14_impl utf8_int8_t * -utf8rcodepoint(const utf8_int8_t *utf8_restrict str, - utf8_int32_t *utf8_restrict out_codepoint) { - const utf8_int8_t *s = (const utf8_int8_t *)str; - - if (0xf0 == (0xf8 & s[0])) { - /* 4 byte utf8 codepoint */ - *out_codepoint = ((0x07 & s[0]) << 18) | ((0x3f & s[1]) << 12) | - ((0x3f & s[2]) << 6) | (0x3f & s[3]); - } else if (0xe0 == (0xf0 & s[0])) { - /* 3 byte utf8 codepoint */ - *out_codepoint = - ((0x0f & s[0]) << 12) | ((0x3f & s[1]) << 6) | (0x3f & s[2]); - } else if (0xc0 == (0xe0 & s[0])) { - /* 2 byte utf8 codepoint */ - *out_codepoint = ((0x1f & s[0]) << 6) | (0x3f & s[1]); - } else { - /* 1 byte utf8 codepoint otherwise */ - *out_codepoint = s[0]; - } - - do { - s--; - } while ((0 != (0x80 & s[0])) && (0x80 == (0xc0 & s[0]))); - - return (utf8_int8_t *)s; -} - -#undef utf8_restrict -#undef utf8_constexpr14 -#undef utf8_null - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#if defined(__clang__) -#pragma clang diagnostic pop -#endif - -#endif /* SHEREDOM_UTF8_H_INCLUDED */ diff --git a/deps/ledger-zxlib/include/view_templates.h b/deps/ledger-zxlib/include/view_templates.h deleted file mode 100644 index a301b60..0000000 --- a/deps/ledger-zxlib/include/view_templates.h +++ /dev/null @@ -1,168 +0,0 @@ -/******************************************************************************* -* (c) 2016 Ledger -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#include - -#define UI_CENTER11PX BAGL_FONT_OPEN_SANS_REGULAR_11px | BAGL_FONT_ALIGNMENT_CENTER -#define UI_CENTER11PX_BOLD BAGL_FONT_OPEN_SANS_EXTRABOLD_11px | BAGL_FONT_ALIGNMENT_CENTER -#define UI_11PX 11 - -#define DEFAULT_FONT BAGL_FONT_OPEN_SANS_LIGHT_16px | BAGL_FONT_ALIGNMENT_LEFT -#define UI_WHITE 0xFFFFFF -#define UI_BLACK 0x000000 - -#define UIID_ICONLEFT 0x10 -#define UIID_ICONRIGHT 0x11 -#define UIID_LABEL 0x20 -#define UIID_LABELSCROLL 0x71 - -#define UI_FillRectangle(id, x, y, w, h, fgcolor, bgcolor) \ -{ \ - { \ - BAGL_RECTANGLE, /* type */ \ - id, /* usedid */ \ - x, /* x */ \ - y, /* y */ \ - w, /* width */ \ - h, /* height */ \ - 0, /* stroke */ \ - 0, /* radius */ \ - BAGL_FILL, /* fill */ \ - fgcolor, /* fgcolor */ \ - bgcolor, /* bgcolor */ \ - 0, /* font_id */ \ - 0 /* icon_id */ \ - }, \ - NULL, /* text */ \ -} - -#define UI_LabelLine(id, x, y, w, h, fgcolor, bgcolor, text) \ -{ \ - { \ - BAGL_LABELINE, /* type */ \ - id, /* usedid */ \ - x, /* x */ \ - y, /* y */ \ - w, /* width */ \ - h, /* height */ \ - 0, /* stroke */ \ - 0, /* radius */ \ - 0, /* fill */ \ - fgcolor, /* fgcolor */ \ - bgcolor, /* bgcolor */ \ - UI_CENTER11PX, /* font_id */ \ - 0 /* icon_id */ \ - }, \ - text, /* text */ \ -} - -#define UI_LabelLineScrolling(id, x, y, w, h, fgcolor, bgcolor, text) \ -{ \ - { \ - BAGL_LABELINE, /* type */ \ - id, /* usedid */ \ - x, /* x */ \ - y, /* y */ \ - w, /* width */ \ - h, /* height */ \ - 5 | BAGL_STROKE_FLAG_ONESHOT, /* stroke | scr pause */ \ - 0, /* radius */ \ - 0, /* fill */ \ - fgcolor, /* fgcolor */ \ - bgcolor, /* bgcolor */ \ - UI_CENTER11PX, /* font_id */ \ - 50 /* icon_id / scroll speed */ \ - }, \ - text, /* text */ \ -} - -#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) -#define UI_SCREEN_WIDTH 128 -#define UI_SCREEN_HEIGHT 64 - -#define BAGL_GLYPH_ICON_LEFT ((const char*)&C_icon_left) -#define BAGL_GLYPH_ICON_RIGHT ((const char*)&C_icon_right) -#define BAGL_GLYPH_ICON_CROSS ((const char*)&C_icon_crossmark) -#define BAGL_GLYPH_ICON_CHECK ((const char*)&C_icon_validate) - -#define UI_Icon(id, x, y, w, h, icon) \ -{ \ - { \ - BAGL_ICON, /* type */ \ - id, /* usedid */ \ - x, /* x */ \ - y, /* y */ \ - w, /* width */ \ - h, /* height */ \ - 0, /* stroke */ \ - 0, /* radius */ \ - 0, /* fill */ \ - UI_WHITE, /* fgcolor */ \ - UI_BLACK, /* bgcolor */ \ - 0, /* font_id */ \ - 0 /* icon_id */ \ - }, \ - icon, /* text */ \ - 0, /* touch_area_brim */ \ - 0, /* overfgcolor */ \ - 0, /* overbgcolor */ \ - NULL, /* tap */ \ - NULL, /* out */ \ - NULL, /* over */ \ -} - -#define UI_BACKGROUND \ - UI_FillRectangle(0, 0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT, 0x000000, 0xFFFFFF) - -#define UI_BACKGROUND_LEFT_RIGHT_ICONS \ - UI_BACKGROUND, \ - UI_Icon(UIID_ICONLEFT, 2, 28, 4, 7, BAGL_GLYPH_ICON_LEFT), \ - UI_Icon(UIID_ICONRIGHT, 122, 28, 4, 7, BAGL_GLYPH_ICON_RIGHT) - -#else -#define UI_SCREEN_WIDTH 128 -#define UI_SCREEN_HEIGHT 32 - -#define UI_Icon(id, x, y, w, h, icon) \ -{ \ - { \ - BAGL_ICON, /* type */ \ - id, /* usedid */ \ - x, /* x */ \ - y, /* y */ \ - w, /* width */ \ - h, /* height */ \ - 0, /* stroke */ \ - 0, /* radius */ \ - 0, /* fill */ \ - UI_WHITE, /* fgcolor */ \ - UI_BLACK, /* bgcolor */ \ - 0, /* font_id */ \ - icon /* icon_id */ \ - }, \ - NULL, /* text */ \ -} - -#define UI_BACKGROUND \ - UI_FillRectangle(0, 0, 0, UI_SCREEN_WIDTH, UI_SCREEN_HEIGHT, 0x000000, 0xFFFFFF) - -#define UI_BACKGROUND_LEFT_RIGHT_ICONS \ - UI_BACKGROUND, \ - UI_Icon(UIID_ICONLEFT, 0, 0, 7, 7, BAGL_GLYPH_ICON_LEFT), \ - UI_Icon(UIID_ICONRIGHT, 128 - 7, 0, 7, 7, BAGL_GLYPH_ICON_RIGHT) -#endif diff --git a/deps/ledger-zxlib/include/zxerror.h b/deps/ledger-zxlib/include/zxerror.h deleted file mode 100644 index 3a245c5..0000000 --- a/deps/ledger-zxlib/include/zxerror.h +++ /dev/null @@ -1,107 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#pragma once - -#include "zxmacros.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#define CHECK_ZXERR(CALL) { \ - zxerr_t err = CALL; \ - if (err!=zxerr_ok) return err;} - -typedef enum { - zxerr_unknown = 0b00000000, - zxerr_ok = 0b00000011, - zxerr_no_data = 0b00000101, - zxerr_buffer_too_small = 0b00000110, - zxerr_out_of_bounds = 0b00001001, - zxerr_encoding_failed = 0b00001010, - zxerr_invalid_crypto_settings = 0b00001100, - zxerr_ledger_api_error = 0b00001111, -} zxerr_t; - -__Z_INLINE uint8_t getErrorMessage(char *buffer, uint16_t bufferLen, zxerr_t err) { - MEMZERO(buffer, bufferLen); - - switch (err) { - case zxerr_unknown: - snprintf(buffer, bufferLen, "zxerr_unknown"); - break; - case zxerr_ok: - snprintf(buffer, bufferLen, "zxerr_ok"); - break; - case zxerr_no_data: - snprintf(buffer, bufferLen, "zxerr_no_data"); - break; - case zxerr_out_of_bounds: - snprintf(buffer, bufferLen, "zxerr_out_of_bounds"); - break; - case zxerr_encoding_failed: - snprintf(buffer, bufferLen, "zxerr_encoding_failed"); - break; - case zxerr_invalid_crypto_settings: - snprintf(buffer, bufferLen, "zxerr_invalid_crypto_settings"); - break; - case zxerr_ledger_api_error: - snprintf(buffer, bufferLen, "zxerr_ledger_api_error"); - break; - default: - snprintf(buffer, bufferLen, "err N/A"); - } - - return strlen(buffer); -} - -//0b00000000 -//0b00000011 -//0b00000101 -//0b00000110 -//0b00001001 -//0b00001010 -//0b00001100 -//0b00001111 -//0b00010001 -//0b00010010 -//0b00010100 -//0b00010111 -//0b00011000 -//0b00011011 -//0b00011101 -//0b00011110 -//0b00100001 -//0b00100010 -//0b00100100 -//0b00100111 -//0b00101000 -//0b00101011 -//0b00101101 -//0b00101110 -//0b00110000 -//0b00110011 -//0b00110101 -//0b00110110 -//0b00111001 -//0b00111010 -//0b00111100 -//0b00111111 - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/zxformat.h b/deps/ledger-zxlib/include/zxformat.h deleted file mode 100644 index d7e54db..0000000 --- a/deps/ledger-zxlib/include/zxformat.h +++ /dev/null @@ -1,367 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -#include "zxmacros.h" -#include "zxerror.h" - -#define NUM_TO_STR(TYPE) __Z_INLINE const char * TYPE##_to_str(char *data, int dataLen, TYPE##_t number) { \ - if (dataLen < 2) return "Buffer too small"; \ - MEMZERO(data, dataLen); \ - char *p = data; \ - if (number < 0) { *(p++) = '-'; data++; } \ - else if (number == 0) { *(p++) = '0'; } \ - TYPE##_t tmp; \ - while (number != 0) { \ - if (p - data >= (dataLen - 1)) { return "Buffer too small"; } \ - tmp = number % 10; \ - tmp = tmp < 0 ? -tmp : tmp; \ - *(p++) = (char) ('0' + tmp); \ - number /= 10u; \ - } \ - while (p > data) { \ - p--; \ - char z = *data; *data = *p; *p = z; \ - data++; \ - } \ - return NULL; \ -} - -NUM_TO_STR(int32) - -NUM_TO_STR(int64) - -NUM_TO_STR(uint64) - -size_t z_strlen(const char *buffer, size_t maxSize); - -zxerr_t z_str3join(char *buffer, size_t bufferSize, const char *prefix, const char *suffix); - -__Z_INLINE void bip32_to_str(char *s, uint32_t max, const uint32_t *path, uint8_t pathLen) { - MEMZERO(s, max); - - if (pathLen == 0) { - snprintf(s, max, "EMPTY PATH"); - return; - } - - if (pathLen > 5) { - snprintf(s, max, "ERROR"); - return; - } - - uint32_t offset = 0; - for (uint16_t i = 0; i < pathLen; i++) { - size_t written; - - // Warning: overcomplicated because Ledger's snprintf does not return number of written bytes - - snprintf(s + offset, max - offset, "%d", path[i] & 0x7FFFFFFFu); - written = strlen(s + offset); - if (written == 0 || written >= max - offset) { - snprintf(s, max, "ERROR"); - return; - } - offset += written; - - if ((path[i] & 0x80000000u) != 0) { - snprintf(s + offset, max - offset, "'"); - written = strlen(s + offset); - if (written == 0 || written >= max - offset) { - snprintf(s, max, "ERROR"); - return; - } - offset += written; - } - - if (i != pathLen - 1) { - snprintf(s + offset, max - offset, "/"); - written = strlen(s + offset); - if (written == 0 || written >= max - offset) { - snprintf(s, max, "ERROR"); - return; - } - offset += written; - } - } -} - -__Z_INLINE void bip44_to_str(char *s, uint32_t max, const uint32_t path[5]) { - bip32_to_str(s, max, path, 5); -} - -__Z_INLINE int8_t str_to_int8(const char *start, const char *end, char *error) { - int sign = 1; - if (*start == '-') { - sign = -1; - start++; - } - - int64_t value = 0; - int multiplier = 1; - for (const char *s = end - 1; s >= start; s--) { - int delta = (*s - '0'); - if (delta >= 0 && delta <= 9) { - value += (delta * multiplier); - multiplier *= 10; - } else { - if (error != NULL) { - *error = 1; - return 0; - } - } - } - - value *= sign; - if (value >= INT8_MIN && value <= INT8_MAX) { - return (int8_t) value; - } - if (error != NULL) { - *error = 1; - } - return 0; -} - -__Z_INLINE int64_t str_to_int64(const char *start, const char *end, char *error) { - int sign = 1; - if (*start == '-') { - sign = -1; - start++; - } - - int64_t value = 0; - int64_t multiplier = 1; - for (const char *s = end - 1; s >= start; s--) { - int64_t delta = (*s - '0'); - if (delta >= 0 && delta <= 9) { - value += delta * multiplier; - multiplier *= 10; - } else { - if (error != NULL) { - *error = 1; - return 0; - } - } - } - - return value * sign; -} - -uint8_t intstr_to_fpstr_inplace(char *number, size_t number_max_size, uint8_t decimalPlaces); - -__Z_INLINE uint8_t fpstr_to_str(char *out, uint16_t outLen, const char *number, uint8_t decimals) { - MEMZERO(out, outLen); - size_t digits = strlen(number); - - if (decimals == 0) { - if (digits == 0) { - snprintf(out, outLen, "0"); - return 0; - } - - if (outLen < digits) { - snprintf(out, outLen, "ERR"); - return 1; - } - - // No need for formatting - snprintf(out, outLen, "%s", number); - return 0; - } - - if ((outLen < decimals + 2)) { - snprintf(out, outLen, "ERR"); - return 1; - } - - if (outLen < digits + 2) { - snprintf(out, outLen, "ERR"); - return 1; - } - - if (digits <= decimals) { - if (outLen <= decimals + 2) { - snprintf(out, outLen, "ERR"); - return 1; - } - - // First part - snprintf(out, outLen, "0."); - out += 2; - outLen -= 2; - - MEMSET(out, '0', decimals - digits); - out += decimals - digits; - outLen -= decimals - digits; - - snprintf(out, outLen, "%s", number); - return 0; - } - - const size_t shift = digits - decimals; - snprintf(out, outLen, "%s", number); - number += shift; - - out += shift; - outLen -= shift; - - *out++ = '.'; - outLen--; - snprintf(out, outLen, "%s", number); - return 0; -} - -__Z_INLINE uint16_t fpuint64_to_str(char *out, uint16_t outLen, const uint64_t value, uint8_t decimals) { - char buffer[30]; - MEMZERO(buffer, sizeof(buffer)); - uint64_to_str(buffer, sizeof(buffer), value); - fpstr_to_str(out, outLen, buffer, decimals); - return (uint16_t) strlen(out); -} - -__Z_INLINE void number_inplace_trimming(char *s, uint8_t non_trimmed) { - const size_t len = strlen(s); - if (len == 0 || len == 1 || len > 1024) { - return; - } - - int16_t dec_point = -1; - for (int16_t i = 0; i < (int16_t) len && dec_point < 0; i++) { - if (s[i] == '.') { - dec_point = i; - } - } - if (dec_point < 0) { - return; - } - - const size_t limit = (size_t) dec_point + non_trimmed; - for (size_t i = (len - 1); i > limit && s[i] == '0'; i--) { - s[i] = 0; - } -} - -__Z_INLINE uint64_t uint64_from_BEarray(const uint8_t data[8]) { - uint64_t result = 0; - for (uint8_t i = 0; i < 8u; i++) { - result <<= 8u; - result += data[i]; - } - return result; -} - -__Z_INLINE uint32_t array_to_hexstr(char *dst, uint16_t dstLen, const uint8_t *src, uint8_t count) { - MEMZERO(dst, dstLen); - if (dstLen < (count * 2 + 1)) { - return 0; - } - - const char hexchars[] = "0123456789abcdef"; - for (uint8_t i = 0; i < count; i++, src++) { - *dst++ = hexchars[*src >> 4u]; - *dst++ = hexchars[*src & 0x0Fu]; - } - *dst = 0; // terminate string - - return (uint32_t) (count * 2); -} - -__Z_INLINE void pageStringExt(char *outValue, uint16_t outValueLen, - const char *inValue, uint16_t inValueLen, - uint8_t pageIdx, uint8_t *pageCount) { - MEMZERO(outValue, outValueLen); - *pageCount = 0; - - outValueLen--; // leave space for NULL termination - if (outValueLen == 0) { - return; - } - - if (inValueLen == 0) { - return; - } - - *pageCount = (uint8_t) (inValueLen / outValueLen); - const uint16_t lastChunkLen = (inValueLen % outValueLen); - - if (lastChunkLen > 0) { - (*pageCount)++; - } - - if (pageIdx < *pageCount) { - if (lastChunkLen > 0 && pageIdx == *pageCount - 1) { - MEMCPY(outValue, inValue + (pageIdx * outValueLen), lastChunkLen); - } else { - MEMCPY(outValue, inValue + (pageIdx * outValueLen), outValueLen); - } - } -} - -__Z_INLINE void pageString(char *outValue, uint16_t outValueLen, - const char *inValue, - uint8_t pageIdx, uint8_t *pageCount) { - pageStringExt(outValue, outValueLen, inValue, (uint16_t) strlen(inValue), pageIdx, pageCount); -} - -__Z_INLINE zxerr_t formatBufferData( - const uint8_t *ptr, - uint64_t len, - char *outValue, - uint16_t outValueLen, - uint8_t pageIdx, - uint8_t *pageCount) { - char bufferUI[500 + 1]; - MEMZERO(bufferUI, sizeof(bufferUI)); - MEMZERO(outValue, 0); - CHECK_APP_CANARY() - - if (len >= sizeof(bufferUI)) { - return zxerr_buffer_too_small; - } - memcpy(bufferUI, ptr, len); - - // Check we have all ascii - uint8_t allAscii = 1; - for (size_t i = 0; i < len && allAscii; i++) { - if (bufferUI[i] < 32 || bufferUI[i] > 127) { - allAscii = 0; - } - } - - if (!allAscii) { - bufferUI[0] = '0'; - bufferUI[1] = 'x'; - if (array_to_hexstr(bufferUI + 2, sizeof(bufferUI) - 2, ptr, len) == 0) { - return zxerr_buffer_too_small; - } - } - - pageString(outValue, outValueLen, bufferUI, pageIdx, pageCount); - - return zxerr_ok; -} - -size_t asciify(char *utf8_in); - -size_t asciify_ext(const char *utf8_in, char *ascii_only_out); - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/zxmacros.h b/deps/ledger-zxlib/include/zxmacros.h deleted file mode 100644 index b5bf7e8..0000000 --- a/deps/ledger-zxlib/include/zxmacros.h +++ /dev/null @@ -1,117 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#pragma clang diagnostic push -#pragma ide diagnostic ignored "modernize-use-nullptr" -#pragma ide diagnostic ignored "OCUnusedGlobalDeclarationInspection" -#pragma ide diagnostic ignored "OCUnusedMacroInspection" -#pragma ide diagnostic ignored "modernize-deprecated-headers" - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include -#include "string.h" - -#ifndef __APPLE__ - -extern void explicit_bzero(void *s, size_t n) __THROW __nonnull ((1)); - -#endif - -#define __Z_INLINE inline __attribute__((always_inline)) static -#define __Z_UNUSED __attribute__((unused)) -#define NV_ALIGN __attribute__ ((aligned(64))) - -#if defined(LEDGER_SPECIFIC) -#include "bolos_target.h" -#endif - -#if defined (TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2) -#include "zxmacros_ledger.h" -#else - -#include "zxmacros_x64.h" - -#endif - -#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ -#define ZX_SWAP(v) (((v) & 0x000000FFu) << 24u | ((v) & 0x0000FF00u) << 8u | ((v) & 0x00FF0000u) >> 8u | ((v) & 0xFF000000u) >> 24u) -#define HtoNL(v) ZX_SWAP( v ) -#define NtoHL(v) ZX_SWAP( v ) -#else -#define HtoNL(x) (x) -#define NtoHL(x) (x) -#endif - -#define SET_NV(DST, TYPE, VAL) { \ - TYPE nvset_tmp=(VAL); \ - MEMCPY_NV((void*) PIC(DST), (void *) PIC(&nvset_tmp), sizeof(TYPE)); \ -} - -__Z_INLINE void strncpy_s(char *dst, const char *src, size_t dstSize) { - MEMZERO(dst, dstSize); - strncpy(dst, src, dstSize - 1); -} - -#define sizeof_field(type, member) sizeof(((type *)0)->member) -#define array_length(array) (sizeof(array) / sizeof((array)[0])) - -void zemu_trace(const char *file, uint32_t line); - -#define ZEMU_TRACE() zemu_trace( __func__, __LINE__ ); - -__attribute__((unused)) void check_app_canary(); - -void handle_stack_overflow(); - -void zemu_log_stack(const char *ctx); - -#if (defined (TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2)) -#if defined(ZEMU_LOGGING) -__Z_INLINE void zemu_log(const char *buf) -{ - asm volatile ( - "movs r0, #0x04\n" - "movs r1, %0\n" - "svc 0xab\n" - :: "r"(buf) : "r0", "r1" - ); -} -#else -__Z_INLINE void zemu_log(__Z_UNUSED const char *_) {} -#endif -#else -__Z_INLINE void zemu_log(__Z_UNUSED const char *msg) { - printf("%s\n", msg); -} -#endif - -#if APP_TESTING -#define ZEMU_LOGF(SIZE, ...) { char tmp[(SIZE)]; snprintf(tmp, (SIZE), __VA_ARGS__); zemu_log(tmp); } -#else -#define ZEMU_LOGF(SIZE, ...) {} -#endif - -#ifdef __cplusplus -} -#endif - -#pragma clang diagnostic pop diff --git a/deps/ledger-zxlib/include/zxmacros_ledger.h b/deps/ledger-zxlib/include/zxmacros_ledger.h deleted file mode 100644 index 7604b82..0000000 --- a/deps/ledger-zxlib/include/zxmacros_ledger.h +++ /dev/null @@ -1,57 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#if defined (TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2) - -#include "os.h" -#include "cx.h" - -#define MEMCPY_NV nvm_write - -// This macros are kept for backwards compatibility -// the most recent SDK has unified implementations and deprecated the original os_*** -#define MEMCPY memmove -#define MEMMOVE memmove -#define MEMSET memset -#define MEMCMP memcmp -#define MEMZERO explicit_bzero - -#if defined(TARGET_NANOX) || defined(TARGET_NANOS2) -#include "ux.h" -#define NV_CONST const -#define NV_VOLATILE volatile -#define IS_UX_ALLOWED (G_ux_params.len != BOLOS_UX_IGNORE && G_ux_params.len != BOLOS_UX_CONTINUE) -#else -#include "ux.h" -#include "os_io_seproxyhal.h" -#define NV_CONST -#define NV_VOLATILE -#define IS_UX_ALLOWED (G_ux_params.len != BOLOS_UX_IGNORE && G_ux_params.len != BOLOS_UX_CONTINUE) -#endif - -#define CHECK_APP_CANARY() check_app_canary(); -#define APP_STACK_CANARY_MAGIC 0xDEAD0031 -extern unsigned int app_stack_canary; - -#define WAIT_EVENT() io_seproxyhal_spi_recv(G_io_seproxyhal_spi_buffer, sizeof(G_io_seproxyhal_spi_buffer), 0) - -#define UX_WAIT() \ - while (!UX_DISPLAYED()) { WAIT_EVENT(); UX_DISPLAY_NEXT_ELEMENT(); } \ - WAIT_EVENT(); \ - io_seproxyhal_general_status(); \ - WAIT_EVENT() -#endif diff --git a/deps/ledger-zxlib/include/zxmacros_x64.h b/deps/ledger-zxlib/include/zxmacros_x64.h deleted file mode 100644 index 22d27dc..0000000 --- a/deps/ledger-zxlib/include/zxmacros_x64.h +++ /dev/null @@ -1,40 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#if !defined (TARGET_NANOS) && !defined(TARGET_NANOX) && !defined(TARGET_NANOS2) - -// This macros are kept for backwards compatibility -// the most recent SDK has unified implementations and deprecated the original os_*** -#define MEMMOVE memmove -#define MEMSET memset -#define MEMCPY memcpy -#define MEMCMP memcmp -#define MEMCPY_NV memcpy - -#define PIC(x) (x) -#define CHECK_APP_CANARY() {} -#define CX_ECCINFO_PARITY_ODD 1u -#define CX_ECCINFO_xGTn 2u - -#ifndef __APPLE__ -#define MEMZERO explicit_bzero -#else -__Z_INLINE void __memzero(void *buffer, size_t s) { memset(buffer, 0, s); } -#define MEMZERO __memzero -#endif - -#endif diff --git a/deps/ledger-zxlib/include/zxtypes.h b/deps/ledger-zxlib/include/zxtypes.h deleted file mode 100644 index 9aae677..0000000 --- a/deps/ledger-zxlib/include/zxtypes.h +++ /dev/null @@ -1,30 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -typedef enum { - bool_false = 0, - bool_true = 1, -} bool_t; - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/include/zxutils_ledger.h b/deps/ledger-zxlib/include/zxutils_ledger.h deleted file mode 100644 index 7d3fc05..0000000 --- a/deps/ledger-zxlib/include/zxutils_ledger.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -unsigned short zx_compute_line_width_light(const char* text, unsigned char text_length); - - -#ifdef __cplusplus -} -#endif \ No newline at end of file diff --git a/deps/ledger-zxlib/include/zxversion.h b/deps/ledger-zxlib/include/zxversion.h deleted file mode 100644 index d3db356..0000000 --- a/deps/ledger-zxlib/include/zxversion.h +++ /dev/null @@ -1,20 +0,0 @@ -/******************************************************************************* -* (c) 2018 - 2022 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#pragma once - -#define ZXLIB_MAJOR 12 -#define ZXLIB_MINOR 0 -#define ZXLIB_PATCH 1 diff --git a/deps/ledger-zxlib/scripts/install_deps.sh b/deps/ledger-zxlib/scripts/install_deps.sh deleted file mode 100755 index 46b3765..0000000 --- a/deps/ledger-zxlib/scripts/install_deps.sh +++ /dev/null @@ -1,34 +0,0 @@ -#!/usr/bin/env bash -#******************************************************************************* -#* (c) 2018 Zondax GmbH -#* -#* Licensed under the Apache License, Version 2.0 (the "License"); -#* you may not use this file except in compliance with the License. -#* You may obtain a copy of the License at -#* -#* http://www.apache.org/licenses/LICENSE-2.0 -#* -#* Unless required by applicable law or agreed to in writing, software -#* distributed under the License is distributed on an "AS IS" BASIS, -#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -#* See the License for the specific language governing permissions and -#* limitations under the License. -#******************************************************************************** - -os_string="$(uname -s)" -case "${os_string}" in - Linux*) - sudo apt-get install libusb-1.0.0 libudev-dev - pip install -U setuptools - pip install -U --no-cache ledgerblue ecpy - pip install -U conan - ;; - Darwin*) - brew install libusb - pip install -U ledgerblue ecpy - pip install -U conan - ;; - *) - echo "OS not recognized" - ;; -esac diff --git a/deps/ledger-zxlib/scripts/template.sh b/deps/ledger-zxlib/scripts/template.sh deleted file mode 100755 index 8f0e720..0000000 --- a/deps/ledger-zxlib/scripts/template.sh +++ /dev/null @@ -1,60 +0,0 @@ -#!/usr/bin/env bash -#******************************************************************************* -# (c) 2018 Zondax GmbH -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -#******************************************************************************* - -if [ -z "$APPNAME" ] -then - echo "This script has not been configured correctly" - exit 1 -fi - -# check python 3 has been installed -if ! command -v python3 &>/dev/null; then - echo Python 3 is not installed - exit -fi - -python3 -m ledgerblue.loadApp -h &>/dev/null; -if [ $? -ne 0 ]; then - echo - echo "ERR: ledgerblue pip package not found." - echo "please install using 'pip install ledgerblue'" - echo - exit -fi - -TMP_HEX_DIR=$(mktemp -d -t ci-XXXXXXXXXX) -mkdir -p ${TMP_HEX_DIR}/bin -BIN_HEX_FILE=${TMP_HEX_DIR}/bin/app.hex -echo -e "${APPHEX}" > ${BIN_HEX_FILE} - -case "$1" in - 'load') - cd "$TMP_HEX_DIR" || exit - python3 -m ledgerblue.loadApp --appFlags 0x200 --delete ${LOAD_PARAMS} --path ${APPPATH} --path "44'/1'" - ;; - 'delete') - python3 -m ledgerblue.deleteApp ${DELETE_PARAMS} - ;; - 'version') - echo "v${APPVERSION}" - ;; - *) - echo "Zondax Installer [$APPNAME-$APPVERSION] [Warning: use only for test/demo apps]" - echo " load - Load $APPNAME app" - echo " delete - Delete $APPNAME app" - echo " version - Show $APPNAME app version" -esac diff --git a/deps/ledger-zxlib/src/app_mode.c b/deps/ledger-zxlib/src/app_mode.c deleted file mode 100644 index 9d80d02..0000000 --- a/deps/ledger-zxlib/src/app_mode.c +++ /dev/null @@ -1,85 +0,0 @@ -/******************************************************************************* -* (c) 2020 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "app_mode.h" - -typedef struct { - uint8_t expert; -} app_mode_persistent_t; - -typedef struct { - uint8_t secret; -} app_mode_temporary_t; - -app_mode_temporary_t app_mode_temporary; - -#if defined(TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2) -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// -app_mode_persistent_t NV_CONST N_appmode_impl __attribute__ ((aligned(64))); -#define N_appmode (*(NV_VOLATILE app_mode_persistent_t *)PIC(&N_appmode_impl)) - -void app_mode_reset(){ - app_mode_temporary.secret = 0; -} - -bool app_mode_expert() { - return N_appmode.expert; -} - -void app_mode_set_expert(uint8_t val) { - app_mode_persistent_t mode; - mode.expert = val; - MEMCPY_NV( (void*) PIC(&N_appmode_impl), (void*) &mode, sizeof(app_mode_persistent_t)); -} - -#else -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// - -app_mode_persistent_t app_mode; - -void app_mode_reset() { - app_mode.expert = 0; - app_mode_temporary.secret = 0; -} - -bool app_mode_expert() { - return app_mode.expert; -} - -void app_mode_set_expert(uint8_t val) { - app_mode.expert = val; -} - -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// -////////////////////////////////////////////////////////////// - -#endif - -bool app_mode_secret() { - return app_mode_temporary.secret; -} - -void app_mode_set_secret(uint8_t val) { - app_mode_temporary.secret = val; -} diff --git a/deps/ledger-zxlib/src/base58.c b/deps/ledger-zxlib/src/base58.c deleted file mode 100644 index e6fce41..0000000 --- a/deps/ledger-zxlib/src/base58.c +++ /dev/null @@ -1,153 +0,0 @@ -/******************************************************************************* -* Adapted from Ledger App - Bitcoin Wallet -* (c) 2019 Zondax GmbH -* -* Ledger App - Bitcoin Wallet -* (c) 2016-2019 Ledger -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "base58.h" -#include "zxmacros.h" - -#define MAX_DEC_INPUT_SIZE 164 -#define MAX_ENC_INPUT_SIZE 120 - -unsigned char const BASE58TABLE[] = { - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, - 0x10, 0xff, 0x11, 0x12, 0x13, 0x14, 0x15, 0xff, 0x16, 0x17, 0x18, 0x19, - 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, - 0xff, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, - 0x37, 0x38, 0x39, 0xff, 0xff, 0xff, 0xff, 0xff}; - -unsigned char const BASE58ALPHABET[] = { - '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', - 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', - 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', - 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; - -int decode_base58(const char *in, size_t length, - unsigned char *out, size_t *outlen) { - unsigned char tmp[MAX_DEC_INPUT_SIZE]; - unsigned char buffer[MAX_DEC_INPUT_SIZE] = {0}; - unsigned char i; - unsigned char j; - unsigned char startAt; - unsigned char zeroCount = 0; - if (length > MAX_DEC_INPUT_SIZE) { - return -1; - } - MEMMOVE(tmp, in, length); - for (i = 0; i < length; i++) { - if (in[i] >= sizeof(BASE58TABLE)) { - return -1; - } - tmp[i] = BASE58TABLE[(int) in[i]]; - if (tmp[i] == 0xff) { - return -1; - } - } - while ((zeroCount < length) && (tmp[zeroCount] == 0)) { - ++zeroCount; - } - j = length; - startAt = zeroCount; - while (startAt < length) { - unsigned short remainder = 0; - unsigned char divLoop; - for (divLoop = startAt; divLoop < length; divLoop++) { - unsigned short digit256 = (unsigned short) (tmp[divLoop] & 0xff); - unsigned short tmpDiv = remainder * 58 + digit256; - tmp[divLoop] = (unsigned char) (tmpDiv / 256); - remainder = (tmpDiv % 256); - } - if (tmp[startAt] == 0) { - ++startAt; - } - buffer[--j] = (unsigned char) remainder; - } - while ((j < length) && (buffer[j] == 0)) { - ++j; - } - length = length - (j - zeroCount); - if (*outlen < length) { - return -1; - } - - MEMMOVE(out, buffer + j - zeroCount, length); - *outlen = length; - return 0; -} - -int encode_base58(const unsigned char *in, size_t length, - unsigned char *out, size_t *outlen) { - unsigned char buffer[MAX_ENC_INPUT_SIZE * 138 / 100 + 1] = {0}; - size_t i, j; - size_t startAt, stopAt; - size_t zeroCount = 0; - size_t outputSize; - - if (length > MAX_ENC_INPUT_SIZE) { - return -1; - } - - while ((zeroCount < length) && (in[zeroCount] == 0)) { - ++zeroCount; - } - - outputSize = (length - zeroCount) * 138 / 100 + 1; - stopAt = outputSize - 1; - for (startAt = zeroCount; startAt < length; startAt++) { - int carry = in[startAt]; - for (j = outputSize - 1; (int) j >= 0; j--) { - carry += 256 * buffer[j]; - buffer[j] = carry % 58; - carry /= 58; - - if (j <= stopAt - 1 && carry == 0) { - break; - } - } - stopAt = j; - } - - j = 0; - while (j < outputSize && buffer[j] == 0) { - j += 1; - } - - if (*outlen < zeroCount + outputSize - j) { - *outlen = zeroCount + outputSize - j; - return -1; - } - - MEMSET(out, BASE58ALPHABET[0], zeroCount); - - i = zeroCount; - while (j < outputSize) { - out[i++] = BASE58ALPHABET[buffer[j++]]; - } - *outlen = i; - return 0; -} - -char encode_base58_clip(const unsigned char v) { - return BASE58ALPHABET[v % 58]; -} diff --git a/deps/ledger-zxlib/src/base64.c b/deps/ledger-zxlib/src/base64.c deleted file mode 100644 index ad601f8..0000000 --- a/deps/ledger-zxlib/src/base64.c +++ /dev/null @@ -1,71 +0,0 @@ -/******************************************************************************* -* (c) 2020 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include -#include "base64.h" - -#define BASE64_PADDING_CHAR '=' - -const char base64_charset[] = { - 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', - 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', - 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', - 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/', -}; - -uint16_t base64_encode(char *out, uint16_t outlen, const uint8_t *in, uint16_t inlen) { - MEMZERO(out, outlen); - - // Check uppeer bound or bailout - uint16_t minspace = inlen / 6; - if (inlen % 6 != 0) minspace++; - minspace++; // zero termination - if (outlen < minspace) { - return 0; - } - - int8_t carry_count = 0; - uint8_t carry_value = 0; - uint16_t out_idx = 0; - - for (uint16_t i = 0; i < inlen; i++) { - const uint8_t c = in[i]; - - const uint8_t shift = (6 - carry_count); - const uint8_t idx = (carry_value << shift) | (c >> (carry_count + 2)); - carry_value = c & (0xFFu >> shift); - carry_count += 2; - - out[out_idx++] = base64_charset[idx]; - - // Check if we have another complete byte ready - if (carry_count == 6) { - out[out_idx++] = base64_charset[carry_value]; - carry_value = 0; - carry_count = 0; - } - } - - // If there is any left over add and pad - if (carry_count > 0) { - out[out_idx++] = base64_charset[carry_value << (6 - carry_count)]; - while (carry_count < 6) { - out[out_idx++] = BASE64_PADDING_CHAR; - carry_count += 2; - } - } - - return out_idx; -} diff --git a/deps/ledger-zxlib/src/bech32.c b/deps/ledger-zxlib/src/bech32.c deleted file mode 100644 index 21fc639..0000000 --- a/deps/ledger-zxlib/src/bech32.c +++ /dev/null @@ -1,58 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include -#include -#include -#include "bech32.h" -#include "segwit_addr.h" -#include "bittools.h" - -zxerr_t bech32EncodeFromBytes(char *out, - size_t out_len, - const char *hrp, - const uint8_t *in, - size_t in_len, - uint8_t pad) { - MEMZERO(out, out_len); - - if (in_len > MAX_INPUT_SIZE) { - return zxerr_out_of_bounds; - } - - size_t hrplen = strlen(hrp); - // We set a lower bound to ensure this is safe - if (out_len < hrplen + (in_len * 2) + 7) { - return zxerr_buffer_too_small; - } - - // Overestimate required size *2==(8/4) instead of *(8/5) - uint8_t tmp_data[MAX_INPUT_SIZE * 2]; - size_t tmp_size = 0; - MEMZERO(tmp_data, sizeof(tmp_data)); - - convert_bits(tmp_data, &tmp_size, 5, in, in_len, 8, pad); - if (tmp_size >= out_len) { - return zxerr_out_of_bounds; - } - - int err = bech32_encode(out, hrp, tmp_data, tmp_size); - if (err == 0) { - return zxerr_encoding_failed; - } - - return zxerr_ok; -} diff --git a/deps/ledger-zxlib/src/bignum.c b/deps/ledger-zxlib/src/bignum.c deleted file mode 100644 index 442ef70..0000000 --- a/deps/ledger-zxlib/src/bignum.c +++ /dev/null @@ -1,148 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include "zxtypes.h" -#include "bignum.h" - -bool_t bignumLittleEndian_bcdprint(char *outBuffer, uint16_t outBufferLen, - const uint8_t *inBCD, uint16_t inBCDLen) { - static const char hexchars[] = "0123456789ABCDEF"; - uint8_t started = 0; - MEMZERO(outBuffer, outBufferLen); - - if (outBufferLen < 4) { - return bool_false; - } - - if (inBCDLen * 2 > outBufferLen) { - snprintf(outBuffer, outBufferLen, "ERR"); - return bool_false; - } - - for (uint8_t i = 0; i < inBCDLen; i++, inBCD++) { - if (started || *inBCD != 0) { - if (started || (*inBCD >> 4u) != 0) { - *outBuffer = hexchars[*inBCD >> 4u]; - outBuffer++; - } - *outBuffer = hexchars[*inBCD & 0x0Fu]; - outBuffer++; - started = 1; - } - } - - if (!started) { - snprintf(outBuffer, outBufferLen, "0"); - } - - return bool_true; -} - -void bignumLittleEndian_to_bcd(uint8_t *bcdOut, uint16_t bcdOutLen, - const uint8_t *binValue, uint16_t binValueLen) { - MEMZERO(bcdOut, bcdOutLen); - - uint8_t carry = 0; - for (uint16_t bitIdx = 0; bitIdx < binValueLen * 8; bitIdx++) { - // Fix bcd - for (uint16_t j = 0; j < bcdOutLen; j++) { - if ((bcdOut[j] & 0x0Fu) > 0x04u) { - bcdOut[j] += 0x03u; - } - if ((bcdOut[j] & 0xF0u) > 0x40u) { - bcdOut[j] += 0x30u; - } - } - - // get bit - const uint16_t byteIdx = bitIdx >> 3u; - const uint8_t mask = 0x80u >> (bitIdx & 0x7u); - carry = (uint8_t) ((binValue[binValueLen - byteIdx - 1] & mask) > 0); - - // Shift bcd - for (uint16_t j = 0; j < bcdOutLen; j++) { - uint8_t carry2 = (uint8_t) (bcdOut[bcdOutLen - j - 1] > 127u); - bcdOut[bcdOutLen - j - 1] <<= 1u; - bcdOut[bcdOutLen - j - 1] += carry; - carry = carry2; - } - } -} - -bool_t bignumBigEndian_bcdprint(char *outBuffer, uint16_t outBufferLen, - const uint8_t *bcdIn, uint16_t bcdInLen) { - static const char hexchars[] = "0123456789ABCDEF"; - uint8_t started = 0; - MEMZERO(outBuffer, outBufferLen); - - if (outBufferLen < 4) { - return bool_false; - } - - if (bcdInLen * 2 > outBufferLen) { - snprintf(outBuffer, outBufferLen, "ERR"); - return bool_false; - } - - for (uint16_t i = 0; i < bcdInLen; i++) { - uint8_t v = bcdIn[bcdInLen - i - 1]; - if (started || v != 0) { - if (started || (v >> 4u) != 0) { - *outBuffer = hexchars[v >> 4u]; - outBuffer++; - } - *outBuffer = hexchars[v & 0x0Fu]; - outBuffer++; - started = 1; - } - } - - if (!started) { - snprintf(outBuffer, outBufferLen, "0"); - } - - return bool_true; -} - -void bignumBigEndian_to_bcd(uint8_t *bcdOut, uint16_t bcdOutLen, - const uint8_t *binValue, uint16_t binValueLen) { - MEMZERO(bcdOut, bcdOutLen); - - uint8_t carry = 0; - for (uint16_t bitIdx = 0; bitIdx < binValueLen * 8; bitIdx++) { - // Fix bcd - for (uint16_t j = 0; j < bcdOutLen; j++) { - if ((bcdOut[j] & 0x0Fu) > 0x04u) { - bcdOut[j] += 0x03u; - } - if ((bcdOut[j] & 0xF0u) > 0x40u) { - bcdOut[j] += 0x30u; - } - } - - // get bit - const uint16_t byteIdx = bitIdx >> 3u; - const uint8_t mask = 0x80u >> (bitIdx & 0x7u); - carry = (uint8_t) ((binValue[byteIdx] & mask) > 0); - - // Shift bcd - for (uint16_t j = 0; j < bcdOutLen; j++) { - uint8_t carry2 = (uint8_t) (bcdOut[j] > 127u); - bcdOut[j] <<= 1u; - bcdOut[j] += carry; - carry = carry2; - } - } -} diff --git a/deps/ledger-zxlib/src/buffering.c b/deps/ledger-zxlib/src/buffering.c deleted file mode 100644 index e956782..0000000 --- a/deps/ledger-zxlib/src/buffering.c +++ /dev/null @@ -1,95 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "buffering.h" -#include - -#ifdef __cplusplus -extern "C" { -#endif - -buffer_state_t ram; // Ram -buffer_state_t flash; // Flash - -void buffering_init(uint8_t *ram_buffer, - uint16_t ram_buffer_size, - uint8_t *flash_buffer, - uint16_t flash_buffer_size) { - ram.data = ram_buffer; - ram.size = ram_buffer_size; - ram.pos = 0; - ram.in_use = 1; - - flash.data = flash_buffer; - flash.size = flash_buffer_size; - flash.pos = 0; - flash.in_use = 0; -} - -void buffering_reset() { - ram.pos = 0; - ram.in_use = 1; - flash.pos = 0; - flash.in_use = 0; -} - -int buffering_append(uint8_t *data, int length) { - if (ram.in_use) { - if (ram.size - ram.pos >= length) { - // RAM in use, append to ram if there is enough space - MEMCPY(ram.data + ram.pos, data, (size_t) length); - ram.pos += length; - } else { - // If RAM is not big enough copy memory to flash - ram.in_use = 0; - flash.in_use = 1; - if (ram.pos > 0) { - buffering_append(ram.data, ram.pos); - } - int num_bytes = buffering_append(data, length); - ram.pos = 0; - return num_bytes; - } - } else { - // Flash in use, append to flash - if (flash.size - flash.pos >= length) { - MEMCPY_NV(flash.data + flash.pos, data, (size_t) length); - flash.pos += length; - } else { - return 0; - } - } - return length; -} - -buffer_state_t *buffering_get_ram_buffer() { - return &ram; -} - -buffer_state_t *buffering_get_flash_buffer() { - return &flash; -} - -buffer_state_t *buffering_get_buffer() { - if (ram.in_use) { - return &ram; - } - return &flash; -} - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/src/hexutils.c b/deps/ledger-zxlib/src/hexutils.c deleted file mode 100644 index 66b8089..0000000 --- a/deps/ledger-zxlib/src/hexutils.c +++ /dev/null @@ -1,57 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include -#include -#include "hexutils.h" - -uint8_t hex2dec(char c, char *out) { - c = (char) tolower((int) c); - - if (!isxdigit((int) c)) { - return 1; - } - - if (isdigit((int) c)) { - *out = (char) (c - '0'); - return 0; - } - - *out = (char) (c - 'a' + 10); - return 0; -} - -size_t parseHexString(uint8_t *out, uint16_t outLen, const char *input) { - size_t len = strnlen(input, outLen * 2u + 1u); - if ( (len / 2) > outLen) { - return 0; - } - if (len % 2 == 1) { - return 0; - } - - for (size_t i = 0; i < len; i += 2) { - char tmp1, tmp2; - if (hex2dec(input[i], &tmp1)) - return 0; - if (hex2dec(input[i + 1], &tmp2)) - return 0; - - out[i >> 1u] = (tmp1 << 4u) + tmp2; - } - - return (len / 2); -} diff --git a/deps/ledger-zxlib/src/segwit_addr.c b/deps/ledger-zxlib/src/segwit_addr.c deleted file mode 100644 index 23ee214..0000000 --- a/deps/ledger-zxlib/src/segwit_addr.c +++ /dev/null @@ -1,191 +0,0 @@ -/* Copyright (c) 2017 Pieter Wuille - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ -#include -#include -#include - -#include "segwit_addr.h" - -uint32_t bech32_polymod_step(uint32_t pre) { - uint8_t b = pre >> 25u; - return ((pre & 0x1FFFFFFu) << 5u) ^ - (-((b >> 0u) & 1u) & 0x3b6a57b2UL) ^ - (-((b >> 1u) & 1u) & 0x26508e6dUL) ^ - (-((b >> 2u) & 1u) & 0x1ea119faUL) ^ - (-((b >> 3u) & 1u) & 0x3d4233ddUL) ^ - (-((b >> 4u) & 1u) & 0x2a1462b3UL); -} - -static const char* charset = "qpzry9x8gf2tvdw0s3jn54khce6mua7l"; - -static const int8_t charset_rev[128] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - 15, -1, 10, 17, 21, 20, 26, 30, 7, 5, -1, -1, -1, -1, -1, -1, - -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, - 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1, - -1, 29, -1, 24, 13, 25, 9, 8, 23, -1, 18, 22, 31, 27, 19, -1, - 1, 0, 3, 16, 11, 28, 12, 14, 6, 4, 2, -1, -1, -1, -1, -1 -}; - -int bech32_encode(char *output, const char *hrp, const uint8_t *data, size_t data_len) { - uint32_t chk = 1; - size_t i = 0; - while (hrp[i] != 0) { - char ch = hrp[i]; - if (ch < 33 || ch > 126) { - return 0; - } - - if (ch >= 'A' && ch <= 'Z') return 0; - chk = bech32_polymod_step(chk) ^ (ch >> 5u); - ++i; - } - if (i + 7 + data_len > 90) return 0; - chk = bech32_polymod_step(chk); - while (*hrp != 0) { - chk = bech32_polymod_step(chk) ^ (*hrp & 0x1fu); - *(output++) = *(hrp++); - } - *(output++) = '1'; - for (i = 0; i < data_len; ++i) { - if (*data >> 5u) return 0; - chk = bech32_polymod_step(chk) ^ (*data); - *(output++) = charset[*(data++)]; - } - for (i = 0; i < 6; ++i) { - chk = bech32_polymod_step(chk); - } - chk ^= 1; - for (i = 0; i < 6; ++i) { - *(output++) = charset[(chk >> ((5u - i) * 5u)) & 0x1fu]; - } - *output = 0; - return 1; -} - -int bech32_decode(char* hrp, uint8_t *data, size_t *data_len, const char *input) { - uint32_t chk = 1; - size_t i; - size_t input_len = strlen(input); - size_t hrp_len; - int have_lower = 0, have_upper = 0; - if (input_len < 8 || input_len > 90) { - return 0; - } - *data_len = 0; - while (*data_len < input_len && input[(input_len - 1) - *data_len] != '1') { - ++(*data_len); - } - hrp_len = input_len - (1 + *data_len); - if (1 + *data_len >= input_len || *data_len < 6) { - return 0; - } - *(data_len) -= 6; - for (i = 0; i < hrp_len; ++i) { - char ch = input[i]; - if (ch < 33 || ch > 126) { - return 0; - } - if (ch >= 'a' && ch <= 'z') { - have_lower = 1; - } else if (ch >= 'A' && ch <= 'Z') { - have_upper = 1; - ch = (ch - 'A') + 'a'; - } - hrp[i] = ch; - chk = bech32_polymod_step(chk) ^ (ch >> 5u); - } - hrp[i] = 0; - chk = bech32_polymod_step(chk); - for (i = 0; i < hrp_len; ++i) { - chk = bech32_polymod_step(chk) ^ (input[i] & 0x1fu); - } - ++i; - while (i < input_len) { - int v = (input[i] & 0x80u) ? -1 : charset_rev[(int)input[i]]; - if (input[i] >= 'a' && input[i] <= 'z') have_lower = 1; - if (input[i] >= 'A' && input[i] <= 'Z') have_upper = 1; - if (v == -1) { - return 0; - } - chk = bech32_polymod_step(chk) ^ v; - if (i + 6 < input_len) { - data[i - (1 + hrp_len)] = v; - } - ++i; - } - if (have_lower && have_upper) { - return 0; - } - return chk == 1; -} - -int convert_bits(uint8_t* out, size_t* outlen, int outBits, const uint8_t* in, size_t inLen, int inBits, int pad) { - uint32_t val = 0; - int bits = 0; - uint32_t maxv = (((uint32_t)1u) << outBits) - 1u; - while (inLen--) { - val = (val << inBits) | *(in++); - bits += inBits; - while (bits >= outBits) { - bits -= outBits; - out[(*outlen)++] = (val >> bits) & maxv; - } - } - if (pad) { - if (bits) { - out[(*outlen)++] = (val << (outBits - bits)) & maxv; - } - } else if (((val << (outBits - bits)) & maxv) || bits >= inBits) { - return 0; - } - return 1; -} - -int segwit_addr_encode(char *output, const char *hrp, int witver, const uint8_t *witprog, size_t witprog_len) { - uint8_t data[65]; - size_t datalen = 0; - if (witver > 16) return 0; - if (witver == 0 && witprog_len != 20 && witprog_len != 32) return 0; - if (witprog_len < 2 || witprog_len > 40) return 0; - data[0] = witver; - convert_bits(data + 1, &datalen, 5, witprog, witprog_len, 8, 1); - ++datalen; - return bech32_encode(output, hrp, data, datalen); -} - -int segwit_addr_decode(int* witver, uint8_t* witdata, size_t* witdata_len, const char* hrp, const char* addr) { - uint8_t data[84]; - char hrp_actual[84]; - size_t data_len; - if (!bech32_decode(hrp_actual, data, &data_len, addr)) return 0; - if (data_len == 0 || data_len > 65) return 0; - if (strncmp(hrp, hrp_actual, 84) != 0) return 0; - if (data[0] > 16) return 0; - *witdata_len = 0; - if (!convert_bits(witdata, witdata_len, 8, data + 1, data_len - 1, 5, 0)) return 0; - if (*witdata_len < 2 || *witdata_len > 40) return 0; - if (data[0] == 0 && *witdata_len != 20 && *witdata_len != 32) return 0; - *witver = data[0]; - return 1; -} diff --git a/deps/ledger-zxlib/src/sigutils.c b/deps/ledger-zxlib/src/sigutils.c deleted file mode 100644 index 107088f..0000000 --- a/deps/ledger-zxlib/src/sigutils.c +++ /dev/null @@ -1,115 +0,0 @@ -/******************************************************************************* -* (c) 2020 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include -#include - -#define MINPAYLOADLEN 1 -#define PAYLOADLEN 32 -#define MAXPAYLOADLEN 33 - -err_convert_e convertDERtoRSV(const uint8_t *inSignatureDER, - unsigned int inInfo, - uint8_t *outR, - uint8_t *outS, - uint8_t *outV) { - - // https://github.com/libbitcoin/libbitcoin-system/wiki/ECDSA-and-DER-Signatures#serialised-der-signature-sequence - // 0 [1 byte] - DER Prefix - // 1 [1 byte] - Payload len - // 2 [1 byte] - R Marker. Always 02 - // 3 [1 byte] - R Len RLEN - // ROFFSET ... [.?. byte] - R ROFFSET - // ROFFSET+RLEN [1 byte] - S Marker. Always 02 - // ROFFSET+RLEN+1 [1 byte] - S Length SLEN - // ROFFSET+RLEN+2 [.?. byte] - S SOFFSET - // Prepare response - // R [32] - // S [32] - // V [1] - - MEMZERO(outR, 32); - MEMZERO(outS, 32); - MEMZERO(outV, 1); - - const uint8_t derPrefix = *(inSignatureDER); - if (derPrefix != 0x30) { - return invalid_derPrefix; - } - - const uint8_t payloadLen = *(inSignatureDER + 1); - const uint8_t minPayloadLen = 2 + MINPAYLOADLEN + 2 + MINPAYLOADLEN; - const uint8_t maxPayloadLen = 2 + MAXPAYLOADLEN + 2 + MAXPAYLOADLEN; - if (payloadLen < minPayloadLen || payloadLen > maxPayloadLen) { - return invalid_payloadLen; - } - - const uint8_t rMarker = *(inSignatureDER + 2); - if (rMarker != 0x02) { - return invalid_rmaker; - } - - uint8_t rLen = *(inSignatureDER + 3); - if (rLen > MAXPAYLOADLEN || rLen < MINPAYLOADLEN) { - return invalid_rLen; - } - - const uint8_t sMarker = *(inSignatureDER + 4 + rLen); - if (sMarker != 0x02) { - return invalid_smarker; - } - - uint8_t sLen = *(inSignatureDER + 4 + rLen + 1); - if (sLen > MAXPAYLOADLEN || sLen < MINPAYLOADLEN) { - return invalid_sLen; - } - - // Get data fields - const uint8_t *rPtr = inSignatureDER + 4; - const uint8_t *sPtr = inSignatureDER + 4 + rLen + 2; - - // Correct field pointers - if (rLen < PAYLOADLEN) { - outR += PAYLOADLEN - rLen; - } - if (rLen > PAYLOADLEN) { - rPtr += rLen - PAYLOADLEN; // move forward get only 32 bytes - rLen = PAYLOADLEN; - } - - if (sLen < PAYLOADLEN) { - outS += PAYLOADLEN - sLen; - } - if (sLen > PAYLOADLEN) { - sPtr += sLen - PAYLOADLEN; // move forward get only 32 bytes - sLen = PAYLOADLEN; - } - - // Prepare V - *outV = 0; - if (inInfo & CX_ECCINFO_PARITY_ODD) { - *outV += 1; - } - if (inInfo & CX_ECCINFO_xGTn) { - *outV += 2; - } - - // Copy things - MEMCPY(outR, rPtr, rLen); - MEMCPY(outS, sPtr, sLen); - - return no_error; -} diff --git a/deps/ledger-zxlib/src/timeutils.c b/deps/ledger-zxlib/src/timeutils.c deleted file mode 100644 index 0efa919..0000000 --- a/deps/ledger-zxlib/src/timeutils.c +++ /dev/null @@ -1,508 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include "zxmacros.h" -#include "timeutils.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include - -const uint8_t monthDays[] = { - 31, - 28, - 31, - 30, - 31, - 30, - 31, - 31, - 30, - 31, - 30, - 31 -}; - -const uint32_t yearLookup[] = { - 0, - 365, - 730, - 1096, - 1461, - 1826, - 2191, - 2557, - 2922, - 3287, - 3652, - 4018, - 4383, - 4748, - 5113, - 5479, - 5844, - 6209, - 6574, - 6940, - 7305, - 7670, - 8035, - 8401, - 8766, - 9131, - 9496, - 9862, - 10227, - 10592, - 10957, - 11323, - 11688, - 12053, - 12418, - 12784, - 13149, - 13514, - 13879, - 14245, - 14610, - 14975, - 15340, - 15706, - 16071, - 16436, - 16801, - 17167, - 17532, - 17897, - 18262, - 18628, - 18993, - 19358, - 19723, - 20089, - 20454, - 20819, - 21184, - 21550, - 21915, - 22280, - 22645, - 23011, - 23376, - 23741, - 24106, - 24472, - 24837, - 25202, - 25567, - 25933, - 26298, - 26663, - 27028, - 27394, - 27759, - 28124, - 28489, - 28855, - 29220, - 29585, - 29950, - 30316, - 30681, - 31046, - 31411, - 31777, - 32142, - 32507, - 32872, - 33238, - 33603, - 33968, - 34333, - 34699, - 35064, - 35429, - 35794, - 36160, - 36525, - 36890, - 37255, - 37621, - 37986, - 38351, - 38716, - 39082, - 39447, - 39812, - 40177, - 40543, - 40908, - 41273, - 41638, - 42004, - 42369, - 42734, - 43099, - 43465, - 43830, - 44195, - 44560, - 44926, - 45291, - 45656, - 46021, - 46387, - 46752, - 47117, - 47482, - 47847, - 48212, - 48577, - 48942, - 49308, - 49673, - 50038, - 50403, - 50769, - 51134, - 51499, - 51864, - 52230, - 52595, - 52960, - 53325, - 53691, - 54056, - 54421, - 54786, - 55152, - 55517, - 55882, - 56247, - 56613, - 56978, - 57343, - 57708, - 58074, - 58439, - 58804, - 59169, - 59535, - 59900, - 60265, - 60630, - 60996, - 61361, - 61726, - 62091, - 62457, - 62822, - 63187, - 63552, - 63918, - 64283, - 64648, - 65013, - 65379, - 65744, - 66109, - 66474, - 66840, - 67205, - 67570, - 67935, - 68301, - 68666, - 69031, - 69396, - 69762, - 70127, - 70492, - 70857, - 71223, - 71588, - 71953, - 72318, - 72684, - 73049, - 73414, - 73779, - 74145, - 74510, - 74875, - 75240, - 75606, - 75971, - 76336, - 76701, - 77067, - 77432, - 77797, - 78162, - 78528, - 78893, - 79258, - 79623, - 79989, - 80354, - 80719, - 81084, - 81450, - 81815, - 82180, - 82545, - 82911, - 83276, - 83641, - 84006, - 84371, - 84736, - 85101, - 85466, - 85832, - 86197, - 86562, - 86927, - 87293, - 87658, - 88023, - 88388, - 88754, - 89119, - 89484, - 89849, - 90215, - 90580, - 90945, - 91310, - 91676, - 92041, - 92406, - 92771, - 93137, - 93502, - 93867, - 94232, - 94598, - 94963, - 95328, - 95693, - 96059, - 96424, - 96789, - 97154, - 97520, - 97885, - 98250, - 98615, - 98981, - 99346, - 99711, - 100076, - 100442, - 100807, - 101172, - 101537, - 101903, - 102268, - 102633, - 102998, - 103364, - 103729, - 104094, - 104459, - 104825, - 105190, - 105555, - 105920, - 106286, - 106651, - 107016, - 107381, - 107747, - 108112, - 108477, - 108842, - 109208, - 109573, - 109938, - 110303, - 110669, - 111034, - 111399, - 111764, - 112130, - 112495, - 112860, - 113225, - 113591, - 113956, - 114321, - 114686, - 115052, - 115417, - 115782, - 116147, - 116513, - 116878, - 117243, - 117608, - 117974, - 118339, - 118704, - 119069, - 119435, - 119800, - 120165, - 120530, - 120895, - 121260, - 121625, - 121990, - 122356, - 122721, - 123086, - 123451, - 123817, - 124182, - 124547, - 124912, - 125278, - 125643, - 126008, - 126373, - 126739, - 127104, - 127469, - 127834, - 128200, - 128565, - 128930, - 129295, - 129661, - 130026, - 130391, - 130756, - 131122, - 131487, - 131852, - 132217, - 132583, - 132948, - 133313, - 133678, - 134044, - 134409, - 134774, - 135139, - 135505, - 135870, - 136235, - 136600, - 136966, - 137331, - 137696, - 138061, - 138427, - 138792, - 139157, - 139522, - 139888, - 140253, - 140618, - 140983, - 141349, - 141714, - 142079, - 142444, - 142810, - 143175, - 143540, - 143905, - 144271, - 144636, - 145001, - 145366, - 145732, -}; - -// ARM does not implement gmtime. This is a simple alternative implementation -// based on section 4.16 -// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html -zxerr_t printTime(char *out, uint16_t outLen, uint64_t t) { - uint8_t tm_sec; - uint8_t tm_min; - uint8_t tm_hour; - uint16_t tm_day; - uint8_t tm_mon; - uint16_t tm_year; - - tm_sec = (uint8_t) (t % 60); - t -= tm_sec; - t /= 60; - - tm_min = (uint8_t) (t % 60); - t -= tm_min; - t /= 60; - - tm_hour = (uint8_t) (t % 24); - t -= tm_hour; - t /= 24; - - // Look up tm_year - tm_year = 0; - const uint16_t yearLookupSize = sizeof(yearLookup)/sizeof(yearLookup[0]); - while (tm_year < yearLookupSize && yearLookup[tm_year] <= t) tm_year++; - - if (tm_year == 0 || tm_year == yearLookupSize) { - return zxerr_out_of_bounds; - } - tm_year--; - - tm_day = (uint16_t) (t - yearLookup[tm_year] + 1); - tm_year = (uint16_t) (1970 + tm_year); - - // Get day/month - uint8_t leap = (uint8_t) (tm_year % 4 == 0 && (tm_year % 100 != 0 || tm_year % 400 == 0) ? 1 : 0); - - for (tm_mon = 0; tm_mon < 12; tm_mon++) { - uint8_t tmp = monthDays[tm_mon]; - tmp += (tm_mon == 1 ? leap : 0); - if (tm_day <= tmp) { - break; - } - tm_day -= tmp; - } - tm_mon++; - - const char *monthName = getMonth(tm_mon); - - // YYYYmmdd HH:MM:SS - snprintf(out, outLen, "%02d%s%04d %02d:%02d:%02dUTC", - tm_day, - monthName, - tm_year, - tm_hour, tm_min, tm_sec - ); - - return zxerr_ok; -} - -#ifdef __cplusplus -} -#endif diff --git a/deps/ledger-zxlib/src/zxformat.c b/deps/ledger-zxlib/src/zxformat.c deleted file mode 100644 index ee6d5ce..0000000 --- a/deps/ledger-zxlib/src/zxformat.c +++ /dev/null @@ -1,144 +0,0 @@ -/******************************************************************************* -* (c) 2020 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include "zxformat.h" -#include -#include -#include "utf8.h" - -size_t asciify(char *utf8_in_ascii_out) { - return asciify_ext(utf8_in_ascii_out, utf8_in_ascii_out); -} - -size_t asciify_ext(const char *utf8_in, char *ascii_only_out) { - void *p = (void *) utf8_in; - char *q = ascii_only_out; - - // utf8valid returns zero on success - while (*((char *) p) && utf8valid(p) == 0) { - utf8_int32_t tmp_codepoint = 0; - p = utf8codepoint(p, &tmp_codepoint); - *q = (char) ((tmp_codepoint >= 32 && tmp_codepoint <= (int32_t) 0x7F) ? tmp_codepoint : '.'); - q++; - } - - // Terminate string - *q = 0; - return q - ascii_only_out; -} - -uint8_t intstr_to_fpstr_inplace(char *number, size_t number_max_size, uint8_t decimalPlaces) { - uint16_t numChars = strnlen(number, number_max_size); - MEMZERO(number + numChars, number_max_size - numChars); - - if (number_max_size < 1) { - // No space to do anything - return 0; - } - - if (number_max_size <= numChars) { - // No space to do anything - return 0; - } - - if (numChars == 0) { - // Empty number, make a zero - snprintf(number, number_max_size, "0"); - numChars = 1; - } - - // Check all are numbers - uint16_t firstDigit = numChars; - for (int i = 0; i < numChars; i++) { - if (number[i] < '0' || number[i] > '9') { - snprintf(number, number_max_size, "ERR"); - return 0; - } - if (number[i] != '0' && firstDigit > i) { - firstDigit = i; - } - } - - // Trim any incorrect leading zeros - if (firstDigit == numChars) { - snprintf(number, number_max_size, "0"); - numChars = 1; - } else { - // Trim leading zeros - MEMMOVE(number, number + firstDigit, numChars - firstDigit); - MEMZERO(number + numChars - firstDigit, firstDigit); - } - - // If there are no decimal places return - if (decimalPlaces == 0) { - return numChars; - } - - // Now insert decimal point - -// 0123456789012 <-decimal places -// abcd < numChars = 4 -// abcd < shift -// 000000000abcd < fill -// 0.00000000abcd < add decimal point - - if (numChars < decimalPlaces + 1) { - // Move to end - const uint16_t padSize = decimalPlaces - numChars + 1; - MEMMOVE(number + padSize, number, numChars); - MEMSET(number, '0', padSize); - numChars = strlen(number); - } - - // add decimal point - const uint16_t pointPosition = numChars - decimalPlaces; - MEMMOVE(number + pointPosition + 1, number + pointPosition, decimalPlaces); // shift content - number[pointPosition] = '.'; - - numChars = strlen(number); - return numChars; -} - -size_t z_strlen(const char *buffer, size_t maxSize) { - if (buffer == NULL) return 0; - const size_t tmp = strlen(buffer); - return tmp < maxSize ? tmp : maxSize; -} - -zxerr_t z_str3join(char *buffer, size_t bufferSize, const char *prefix, const char *suffix) { - size_t messageSize = z_strlen(buffer, bufferSize); - const size_t prefixSize = z_strlen(prefix, bufferSize); - const size_t suffixSize = z_strlen(suffix, bufferSize); - - size_t requiredSize = 1 /* termination */ + messageSize + prefixSize + suffixSize; - - if (bufferSize < requiredSize) { - snprintf(buffer, bufferSize, "ERR???"); - return zxerr_buffer_too_small; - } - - if (suffixSize > 0) { - memmove(buffer + messageSize, suffix, suffixSize); - buffer[messageSize + suffixSize] = 0; - } - - // shift and add prefix - if (prefixSize > 0) { - memmove(buffer + prefixSize, buffer, messageSize + suffixSize + 1); - memmove(buffer, prefix, prefixSize); - } - - return zxerr_ok; -} diff --git a/deps/ledger-zxlib/src/zxmacros.c b/deps/ledger-zxlib/src/zxmacros.c deleted file mode 100644 index 5dd172b..0000000 --- a/deps/ledger-zxlib/src/zxmacros.c +++ /dev/null @@ -1,68 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include "zxmacros.h" - -#pragma clang diagnostic push -#pragma ide diagnostic ignored "EndlessLoop" - -void handle_stack_overflow() { - zemu_log("!!!!!!!!!!!!!!!!!!!!!! CANARY TRIGGERED!!! STACK OVERFLOW DETECTED\n"); -#if defined (TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2) - io_seproxyhal_se_reset(); -#else - while (1); -#endif -} - -#pragma clang diagnostic pop - -__Z_UNUSED void check_app_canary() { -#if defined (TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2) - if (app_stack_canary != APP_STACK_CANARY_MAGIC) handle_stack_overflow(); -#endif -} - -#if defined(ZEMU_LOGGING) && (defined (TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2)) -void zemu_log_stack(const char *ctx) { - #define STACK_SHIFT 20 - void* p = NULL; - char buf[70]; - snprintf(buf, sizeof(buf), "|SP| %p %p (%d) : %s\n", - &app_stack_canary, - ((void*)&p)+STACK_SHIFT, - (uint32_t)((void*)&p)+STACK_SHIFT - (uint32_t)&app_stack_canary, - ctx); - zemu_log(buf); - (void) ctx; -} -#else - -void zemu_log_stack(__Z_UNUSED const char *ctx) {} - -#endif - - -#if defined(ZEMU_LOGGING) && (defined (TARGET_NANOS) || defined(TARGET_NANOX) || defined(TARGET_NANOS2)) -void zemu_trace(const char *file, uint32_t line) { - char buf[200]; - snprintf(buf, sizeof(buf), "|TRACE| %s:%d\n", file, line); - zemu_log(buf); -} -#else - -void zemu_trace(__Z_UNUSED const char *file, __Z_UNUSED uint32_t line) {} - -#endif diff --git a/deps/ledger-zxlib/src/zxutils_ledger.c b/deps/ledger-zxlib/src/zxutils_ledger.c deleted file mode 100644 index 4800665..0000000 --- a/deps/ledger-zxlib/src/zxutils_ledger.c +++ /dev/null @@ -1,154 +0,0 @@ -//#******************************************************************************* -//#* (c) 2021 Zondax GmbH -//#* (c) 2020 Ledger SAS -//#* -//#* Licensed under the Apache License, Version 2.0 (the "License"); -//#* you may not use this file except in compliance with the License. -//#* You may obtain a copy of the License at -//#* -//#* http://www.apache.org/licenses/LICENSE-2.0 -//#* -//#* Unless required by applicable law or agreed to in writing, software -//#* distributed under the License is distributed on an "AS IS" BASIS, -//#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -//#* See the License for the specific language governing permissions and -//#* limitations under the License. -//#******************************************************************************** -#include "zxutils_ledger.h" - -#ifndef NULL -#define NULL ((void *)0) -#endif - -// We implement a light mechanism in order to be able to retrieve the width of -// nano S characters, in the two possible fonts: -// - BAGL_FONT_OPEN_SANS_EXTRABOLD_11px, -// - BAGL_FONT_OPEN_SANS_REGULAR_11px. -#define NANOS_FIRST_CHAR 0x20 -#define NANOS_LAST_CHAR 0x7F - -// OPEN_SANS_REGULAR_11PX << 4 | OPEN_SANS_EXTRABOLD_11PX -const char nanos_characters_width[96] = { - 3 << 4 | 3, /* code 0020 */ - 3 << 4 | 3, /* code 0021 */ - 4 << 4 | 6, /* code 0022 */ - 7 << 4 | 7, /* code 0023 */ - 6 << 4 | 6, /* code 0024 */ - 9 << 4 | 10, /* code 0025 */ - 8 << 4 | 9, /* code 0026 */ - 2 << 4 | 3, /* code 0027 */ - 3 << 4 | 4, /* code 0028 */ - 3 << 4 | 4, /* code 0029 */ - 6 << 4 | 6, /* code 002A */ - 6 << 4 | 6, /* code 002B */ - 3 << 4 | 3, /* code 002C */ - 4 << 4 | 4, /* code 002D */ - 3 << 4 | 3, /* code 002E */ - 4 << 4 | 5, /* code 002F */ - 6 << 4 | 8, /* code 0030 */ - 6 << 4 | 6, /* code 0031 */ - 6 << 4 | 7, /* code 0032 */ - 6 << 4 | 7, /* code 0033 */ - 8 << 4 | 8, /* code 0034 */ - 6 << 4 | 6, /* code 0035 */ - 6 << 4 | 8, /* code 0036 */ - 6 << 4 | 7, /* code 0037 */ - 6 << 4 | 8, /* code 0038 */ - 6 << 4 | 8, /* code 0039 */ - 3 << 4 | 3, /* code 003A */ - 3 << 4 | 3, /* code 003B */ - 6 << 4 | 5, /* code 003C */ - 6 << 4 | 6, /* code 003D */ - 6 << 4 | 5, /* code 003E */ - 5 << 4 | 6, /* code 003F */ - 10 << 4 | 10, /* code 0040 */ - 7 << 4 | 8, /* code 0041 */ - 7 << 4 | 7, /* code 0042 */ - 7 << 4 | 7, /* code 0043 */ - 8 << 4 | 8, /* code 0044 */ - 6 << 4 | 6, /* code 0045 */ - 6 << 4 | 6, /* code 0046 */ - 8 << 4 | 8, /* code 0047 */ - 8 << 4 | 8, /* code 0048 */ - 3 << 4 | 4, /* code 0049 */ - 4 << 4 | 5, /* code 004A */ - 7 << 4 | 8, /* code 004B */ - 6 << 4 | 6, /* code 004C */ - 10 << 4 | 11, /* code 004D */ - 8 << 4 | 9, /* code 004E */ - 9 << 4 | 9, /* code 004F */ - 7 << 4 | 7, /* code 0050 */ - 9 << 4 | 9, /* code 0051 */ - 7 << 4 | 8, /* code 0052 */ - 6 << 4 | 6, /* code 0053 */ - 7 << 4 | 6, /* code 0054 */ - 8 << 4 | 8, /* code 0055 */ - 7 << 4 | 6, /* code 0056 */ - 10 << 4 | 11, /* code 0057 */ - 6 << 4 | 8, /* code 0058 */ - 6 << 4 | 7, /* code 0059 */ - 6 << 4 | 7, /* code 005A */ - 4 << 4 | 5, /* code 005B */ - 4 << 4 | 5, /* code 005C */ - 4 << 4 | 5, /* code 005D */ - 6 << 4 | 7, /* code 005E */ - 5 << 4 | 6, /* code 005F */ - 6 << 4 | 7, /* code 0060 */ - 6 << 4 | 7, /* code 0061 */ - 7 << 4 | 7, /* code 0062 */ - 5 << 4 | 6, /* code 0063 */ - 7 << 4 | 7, /* code 0064 */ - 6 << 4 | 7, /* code 0065 */ - 5 << 4 | 6, /* code 0066 */ - 6 << 4 | 7, /* code 0067 */ - 7 << 4 | 7, /* code 0068 */ - 3 << 4 | 4, /* code 0069 */ - 4 << 4 | 5, /* code 006A */ - 6 << 4 | 7, /* code 006B */ - 3 << 4 | 4, /* code 006C */ - 10 << 4 | 10, /* code 006D */ - 7 << 4 | 7, /* code 006E */ - 7 << 4 | 7, /* code 006F */ - 7 << 4 | 7, /* code 0070 */ - 7 << 4 | 7, /* code 0071 */ - 4 << 4 | 5, /* code 0072 */ - 5 << 4 | 6, /* code 0073 */ - 4 << 4 | 5, /* code 0074 */ - 7 << 4 | 7, /* code 0075 */ - 6 << 4 | 7, /* code 0076 */ - 9 << 4 | 10, /* code 0077 */ - 6 << 4 | 7, /* code 0078 */ - 6 << 4 | 7, /* code 0079 */ - 5 << 4 | 6, /* code 007A */ - 4 << 4 | 5, /* code 007B */ - 6 << 4 | 6, /* code 007C */ - 4 << 4 | 5, /* code 007D */ - 6 << 4 | 6, /* code 007E */ - 7 << 4 | 6, /* code 007F */ -}; - -unsigned short zx_compute_line_width_light(const char* text, unsigned char text_length) { - char current_char; - unsigned short line_width = 0; - - if(text == NULL) { - return 0xFFFF; - } - - // We parse the characters of the input text on all the input length. - while (text_length--) { - current_char = *text; - - if (current_char < NANOS_FIRST_CHAR || current_char > NANOS_LAST_CHAR) { - if (current_char == '\n' || current_char == '\r') { - break; - } - } - else { - // Regular. - line_width += (nanos_characters_width[current_char - NANOS_FIRST_CHAR] >> 0x04) & 0x0F; - } - text++; - } - return line_width; -} diff --git a/deps/ledger-zxlib/templates/Makefile.root b/deps/ledger-zxlib/templates/Makefile.root deleted file mode 100644 index 566f607..0000000 --- a/deps/ledger-zxlib/templates/Makefile.root +++ /dev/null @@ -1,29 +0,0 @@ -#******************************************************************************* -#* (c) 2019 Zondax GmbH -#* -#* Licensed under the Apache License, Version 2.0 (the "License"); -#* you may not use this file except in compliance with the License. -#* You may obtain a copy of the License at -#* -#* http://www.apache.org/licenses/LICENSE-2.0 -#* -#* Unless required by applicable law or agreed to in writing, software -#* distributed under the License is distributed on an "AS IS" BASIS, -#* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -#* See the License for the specific language governing permissions and -#* limitations under the License. -#******************************************************************************** - -# We use BOLOS_SDK to determine the development environment that is being used -# BOLOS_SDK IS DEFINED We use the plain Makefile for Ledger -# BOLOS_SDK NOT DEFINED We use a containerized build approach - -ifeq ($(BOLOS_SDK),) -include $(CURDIR)/deps/ledger-zxlib/cmake/dockerized_build.mk -else -default: - $(MAKE) -C app -%: - $(info "Calling app Makefile for target $@") - COIN=$(COIN) $(MAKE) -C app $@ -endif diff --git a/deps/ledger-zxlib/tests/asciify.cpp b/deps/ledger-zxlib/tests/asciify.cpp deleted file mode 100644 index f54e0a0..0000000 --- a/deps/ledger-zxlib/tests/asciify.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include -#include -#include - -namespace { - TEST(ASCIIFY, pure) { - char input[] = "This is only ascii"; - char have[50]; - - asciify_ext(input, have); - - EXPECT_STREQ(input, have); - } - - TEST(ASCIIFY, ascii_below_32) { - char input[] = "\05test"; - char want[] = ".test"; - char have[50]; - - EXPECT_EQ(5, strlen(input)); - - size_t ascii_len = asciify_ext(input, have); - std::cout << have << std::endl; - - EXPECT_EQ(strlen(want), ascii_len); - EXPECT_STREQ(want, have); - } - - TEST(ASCIIFY, extended) { - char input[] = "cumpleaños"; - char want[] = "cumplea.os"; - char have[50]; - - EXPECT_EQ(11, strlen(input)); - - size_t ascii_len = asciify_ext(input, have); - std::cout << have << std::endl; - - EXPECT_EQ(strlen(want), ascii_len); - EXPECT_STREQ(want, have); - } - - TEST(ASCIIFY, utf8) { - char input[] = "哈Something哈"; - char want[] = ".Something."; - char have[50]; - - EXPECT_EQ(15, strlen(input)); - - size_t ascii_len = asciify_ext(input, have); - std::cout << have << std::endl; - - EXPECT_EQ(strlen(want), ascii_len); - EXPECT_STREQ(want, have); - } - - TEST(ASCIIFY, inplace_pure) { - char data[] = "This is only ascii"; - char want[] = "This is only ascii"; - - asciify(data); - EXPECT_STREQ(want, data); - } - - TEST(ASCIIFY, inplace_ascii_below_32) { - char data[] = "\05test"; - char want[] = ".test"; - - EXPECT_EQ(5, strlen(data)); - - size_t ascii_len = asciify(data); - std::cout << data << std::endl; - - EXPECT_EQ(strlen(want), ascii_len); - EXPECT_STREQ(want, data); - } - - TEST(ASCIIFY, inplace_extended) { - char data[] = "cumpleaños"; - char want[] = "cumplea.os"; - - EXPECT_EQ(11, strlen(data)); - - size_t ascii_len = asciify(data); - std::cout << data << std::endl; - - EXPECT_EQ(strlen(want), ascii_len); - EXPECT_STREQ(want, data); - } - - TEST(ASCIIFY, inplace_utf8) { - char data[] = "哈Something哈"; - char want[] = ".Something."; - - EXPECT_EQ(15, strlen(data)); - - size_t ascii_len = asciify(data); - std::cout << data << std::endl; - - EXPECT_EQ(strlen(want), ascii_len); - EXPECT_STREQ(want, data); - } - -} diff --git a/deps/ledger-zxlib/tests/base64.cpp b/deps/ledger-zxlib/tests/base64.cpp deleted file mode 100644 index af9a8e5..0000000 --- a/deps/ledger-zxlib/tests/base64.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include -#include "base64.h" - -namespace { - TEST(BASE64, basic_case) { - char out[100]; - uint8_t data[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19}; - - base64_encode(out, sizeof(out), data, 0); - EXPECT_STREQ(out, ""); - - base64_encode(out, sizeof(out), data, 1); - EXPECT_STREQ(out, "AQ=="); - - base64_encode(out, sizeof(out), data, 2); - EXPECT_STREQ(out, "AQM="); - - base64_encode(out, sizeof(out), data, 3); - EXPECT_STREQ(out, "AQMF"); - - base64_encode(out, sizeof(out), data, 4); - EXPECT_STREQ(out, "AQMFBw=="); - - base64_encode(out, sizeof(out), data, 5); - EXPECT_STREQ(out, "AQMFBwk="); - - base64_encode(out, sizeof(out), data, 6); - EXPECT_STREQ(out, "AQMFBwkL"); - - base64_encode(out, sizeof(out), data, 7); - EXPECT_STREQ(out, "AQMFBwkLDQ=="); - - base64_encode(out, sizeof(out), data, 8); - EXPECT_STREQ(out, "AQMFBwkLDQ8="); - - base64_encode(out, sizeof(out), data, 9); - EXPECT_STREQ(out, "AQMFBwkLDQ8R"); - - base64_encode(out, sizeof(out), data, 10); - EXPECT_STREQ(out, "AQMFBwkLDQ8REw=="); - } -} diff --git a/deps/ledger-zxlib/tests/bech32.cpp b/deps/ledger-zxlib/tests/bech32.cpp deleted file mode 100644 index 4e79141..0000000 --- a/deps/ledger-zxlib/tests/bech32.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include -#include -#include -#include - -namespace { - TEST(BECH32, hex_to_address) { - char addr_out[100]; - const char *hrp = "zx"; - - uint8_t data1[] = {1, 3, 5}; - uint8_t data2[] = {1, 3, 5, 7, 9, 11, 13}; - - auto err = bech32EncodeFromBytes(addr_out, sizeof(addr_out), hrp, data1, sizeof(data1), 0); - ASSERT_EQ(err, zxerr_ok); - std::cout << addr_out << std::endl; - ASSERT_STREQ("zx1qypse825ac", addr_out); - - err = bech32EncodeFromBytes(addr_out, sizeof(addr_out), hrp, data2, sizeof(data2), 0); - ASSERT_EQ(err, zxerr_ok); - std::cout << addr_out << std::endl; - ASSERT_STREQ("zx1qyps2pcfpvx20dk22", addr_out); - - /// - err = bech32EncodeFromBytes(addr_out, sizeof(addr_out), hrp, data1, sizeof(data1), 1); - ASSERT_EQ(err, zxerr_ok); - std::cout << addr_out << std::endl; - ASSERT_STREQ("zx1qyps2ucfnzd", addr_out); - - err = bech32EncodeFromBytes(addr_out, sizeof(addr_out), hrp, data2, sizeof(data2), 1); - ASSERT_EQ(err, zxerr_ok); - std::cout << addr_out << std::endl; - ASSERT_STREQ("zx1qyps2pcfpvxshamanz", addr_out); - } - - TEST(BECH32, huge_input) { - char addr_out[200]; - const char *hrp = "zx"; - - auto data = std::vector(1000, 0x55); - - auto err = bech32EncodeFromBytes(addr_out, sizeof(addr_out), hrp, data.data(), data.size(),0); - ASSERT_EQ(err, zxerr_out_of_bounds); - - std::cout << addr_out << std::endl; - } - - TEST(BECH32, small_output) { - char addr_out[1000]; - const char *hrp = "zx"; - - auto data = std::vector(32, 0x55); - - MEMZERO(addr_out, sizeof(addr_out)); - - // declare size to be smaller - const size_t declared_size = 52; - - auto err = bech32EncodeFromBytes(addr_out, declared_size, hrp, data.data(), data.size(), 0); - ASSERT_EQ(err, zxerr_buffer_too_small); - - for (int i = declared_size; i < sizeof(addr_out); i++) { - ASSERT_EQ(addr_out[i], 0); - } - - std::cout << addr_out << std::endl; - } -} diff --git a/deps/ledger-zxlib/tests/bip44path.cpp b/deps/ledger-zxlib/tests/bip44path.cpp deleted file mode 100644 index 807d5a6..0000000 --- a/deps/ledger-zxlib/tests/bip44path.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include -#include -#include - -namespace { - TEST(MACROS, bip32empty) { - char buffer[100]; - uint32_t path[] = {44, 60, 0, 0, 1}; - bip32_to_str(buffer, sizeof(buffer), path, 0); - EXPECT_EQ("EMPTY PATH", std::string(buffer)); - } - - TEST(MACROS, bip32tooManyChildren) { - char buffer[100]; - uint32_t path[] = {44, 60, 0, 0, 1}; - bip32_to_str(buffer, sizeof(buffer), path, 200); - EXPECT_EQ("ERROR", std::string(buffer)); - } - - TEST(MACROS, bip32notEnoughSpaceInBuffer1) { - char buffer[6]; - uint32_t path[] = {1234, 60, 0, 0, 1}; - bip32_to_str(buffer, sizeof(buffer), path, 5); - EXPECT_EQ("ERROR", std::string(buffer)); - } - - TEST(MACROS, bip32notEnoughSpaceInBuffer2) { - char buffer[9]; - uint32_t path[] = {1, 1, 1, 1, 1}; - bip32_to_str(buffer, sizeof(buffer), path, 5); - EXPECT_EQ("ERROR", std::string(buffer)); - } - - TEST(MACROS, bip32notEnoughSpaceInBuffer3) { - char buffer[10]; - uint32_t path[] = {1, 1, 1, 1, 0x80000001 }; - bip32_to_str(buffer, sizeof(buffer), path, 5); - EXPECT_EQ("ERROR", std::string(buffer)); - } - - TEST(MACROS, bip32justEnoughSpaceInBuffer3) { - char buffer[10]; - uint32_t path[] = {1, 1, 1, 1, 1}; - bip32_to_str(buffer, sizeof(buffer), path, 5); - EXPECT_EQ("1/1/1/1/1", std::string(buffer)); - } - - TEST(MACROS, bip44path1) { - uint32_t path[] = {44, 60, 0, 0, 1}; - - char buffer[100]; - bip44_to_str(buffer, sizeof(buffer), path); - - EXPECT_EQ("44/60/0/0/1", std::string(buffer)); - } - - TEST(MACROS, bip44path2) { - uint32_t path[] = {0x8000002c, 60, 0, 0, 1}; - - char buffer[100]; - bip44_to_str(buffer, sizeof(buffer), path); - - EXPECT_EQ("44'/60/0/0/1", std::string(buffer)); - } - - TEST(MACROS, bip44path3) { - uint32_t path[] = {0x8000002c, 60, 0, 0, 0x80000001}; - - char buffer[100]; - bip44_to_str(buffer, sizeof(buffer), path); - - EXPECT_EQ("44'/60/0/0/1'", std::string(buffer)); - } -} diff --git a/deps/ledger-zxlib/tests/buffering_tests.cpp b/deps/ledger-zxlib/tests/buffering_tests.cpp deleted file mode 100644 index c7292d1..0000000 --- a/deps/ledger-zxlib/tests/buffering_tests.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "gtest/gtest.h" -#include "buffering.h" - -namespace { - - TEST(Buffering, SmallBuffer) { - uint8_t ram_buffer[100]; - uint8_t flash_buffer[1000]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - flash_buffer, - sizeof(flash_buffer)); - - // Data is small enough to fit into ram buffer - uint8_t small[50]; - auto num_bytes = buffering_append(small, sizeof(small)); - EXPECT_EQ(sizeof(small), num_bytes) << "Append should not return error"; - - EXPECT_TRUE(buffering_get_ram_buffer()->in_use) << "Writing small buffer should only write to RAM"; - EXPECT_FALSE(buffering_get_flash_buffer()->in_use) << "Writing big buffer should write data to FLASH"; - EXPECT_EQ(50, buffering_get_ram_buffer()->pos) << "Wrong position of the written data in the ram buffer"; - EXPECT_EQ(100, buffering_get_ram_buffer()->size) << "Wrong size of the ram buffer"; - EXPECT_EQ(0, buffering_get_flash_buffer()->pos) << "Wrong position of the written data in the flash buffer"; - EXPECT_EQ(1000, buffering_get_flash_buffer()->size) << "Wrong size of the flash buffer"; - } - - TEST(Buffering, BigBuffer) { - uint8_t ram_buffer[100]; - uint8_t flash_buffer[1000]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - flash_buffer, - sizeof(flash_buffer)); - - // Data is too big to fit into ram buffer, it will be written directly to flash - uint8_t big[500]; - auto num_bytes = buffering_append(big, sizeof(big)); - EXPECT_EQ(sizeof(big), num_bytes) << "Append should not return error"; - - EXPECT_FALSE(buffering_get_ram_buffer()->in_use) << "Writing big buffer should write data to FLASH"; - EXPECT_TRUE(buffering_get_flash_buffer()->in_use) << "Writing big buffer should write data to FLASH"; - EXPECT_EQ(0, buffering_get_ram_buffer()->pos) << "Wrong position of the written data in the ram buffer"; - EXPECT_EQ(100, buffering_get_ram_buffer()->size) << "Wrong size of the ram buffer"; - EXPECT_EQ(500, buffering_get_flash_buffer()->pos) << "Wrong position of the written data in the flash buffer"; - EXPECT_EQ(1000, buffering_get_flash_buffer()->size) << "Wrong size of the flash buffer"; - } - - TEST(Buffering, SmallBufferMultipleTimesWithinRam) { - uint8_t ram_buffer[100]; - uint8_t flash_buffer[1000]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - flash_buffer, - sizeof(flash_buffer)); - - uint8_t small[40]; - auto num_bytes = buffering_append(small, sizeof(small)); - EXPECT_EQ(sizeof(small), num_bytes) << "Append should not return error"; - EXPECT_TRUE(buffering_get_ram_buffer()->in_use) << "Writing small buffer should only write to RAM"; - EXPECT_FALSE(buffering_get_flash_buffer()->in_use) << "Writing big buffer should write data to FLASH"; - - // Here we write another chunk which should not top over the ram buffer - buffering_append(small, sizeof(small)); - EXPECT_TRUE(buffering_get_ram_buffer()->in_use) << "Writing small buffer should only write to RAM"; - EXPECT_FALSE(buffering_get_flash_buffer()->in_use) << "Writing big buffer should write data to FLASH"; - - EXPECT_EQ(sizeof(small) * 2, buffering_get_ram_buffer()->pos) << "Data should be written to RAM"; - EXPECT_EQ(100, buffering_get_ram_buffer()->size) << "Wrong size of the ram buffer"; - EXPECT_EQ(0, buffering_get_flash_buffer()->pos) << "Data should be written to RAM"; - EXPECT_EQ(1000, buffering_get_flash_buffer()->size) << "Wrong size of the flash buffer"; - } - - TEST(Buffering, SmallBufferMultipleTimesToFlash) { - uint8_t ram_buffer[100]; - uint8_t flash_buffer[1000]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - flash_buffer, - sizeof(flash_buffer)); - - uint8_t small[100]; - buffering_append(small, sizeof(small)); - EXPECT_TRUE(buffering_get_ram_buffer()->in_use) << "Writing small buffer should only write to RAM"; - EXPECT_FALSE(buffering_get_flash_buffer()->in_use) << "Writing big buffer should write data to FLASH"; - - // Here we append another small buffer, this time we're going to exceed ram's size - // data will be copied to nvram - buffering_append(small, sizeof(small)); - EXPECT_FALSE(buffering_get_ram_buffer()->in_use) << "Data should be now in FLASH"; - EXPECT_TRUE(buffering_get_flash_buffer()->in_use) << "Data should be now in FLASH"; - - EXPECT_EQ(0, buffering_get_ram_buffer()->pos) << "RAM buffer should be reset"; - EXPECT_EQ(100, buffering_get_ram_buffer()->size) << "Wrong size of the ram buffer"; - EXPECT_EQ(200, buffering_get_flash_buffer()->pos) << "Wrong position of the written data in the flash buffer"; - EXPECT_EQ(1000, buffering_get_flash_buffer()->size) << "Wrong size of the flash buffer"; - } - - TEST(Buffering, SmallBufferMultipleTimes_CheckData) { - uint8_t ram_buffer[100]; - uint8_t flash_buffer[1000]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - flash_buffer, - sizeof(flash_buffer)); - - uint8_t small1[100]; - for (int i = 0; i < sizeof(small1); i++) { - small1[i] = i; - } - buffering_append(small1, sizeof(small1)); - - uint8_t small2[200]; - for (int i = 0; i < sizeof(small2); i++) { - small2[i] = 100 - i; - } - auto num_bytes = buffering_append(small2, sizeof(small2)); - EXPECT_EQ(sizeof(small2), num_bytes) << "Append should not return error"; - - // In this test we want to make sure that data is not compromised. - uint8_t *dst = buffering_get_flash_buffer()->data; - for (int i = 0; i < sizeof(small1) + sizeof(small2); i++) { - if (i < sizeof(small1)) { - EXPECT_EQ(dst[i], small1[i]) << "Wrong data written to FLASH"; - } else { - EXPECT_EQ(dst[i], small2[i - sizeof(small1)]) << "Wrong data written to FLASH"; - } - } - } - - TEST(Buffering, Reset) { - uint8_t ram_buffer[100]; - uint8_t flash_buffer[1000]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - flash_buffer, - sizeof(flash_buffer)); - - uint8_t big[1000]; - auto num_bytes = buffering_append(big, sizeof(big)); - EXPECT_EQ(sizeof(big), num_bytes) << "Append should not return error"; - - EXPECT_FALSE(buffering_get_ram_buffer()->in_use) << "Writing big buffer should only write to FLASH"; - EXPECT_TRUE(buffering_get_flash_buffer()->in_use) << "Writing big buffer should only write to FLASH"; - - buffering_reset(); - - EXPECT_TRUE(buffering_get_ram_buffer()->in_use) << "After reset RAM should be enabled by default"; - EXPECT_FALSE(buffering_get_flash_buffer()->in_use) << "After reset RAM should be enabled by default"; - } - - TEST(Buffering, NotEnoughRoomInFlash) { - uint8_t ram_buffer[100]; - uint8_t flash_buffer[1000]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - flash_buffer, - sizeof(flash_buffer)); - - uint8_t big[1101]; - auto num_bytes = buffering_append(big, sizeof(big)); - EXPECT_EQ(0, num_bytes) << "Appending outside the bounds of the buffer should return error"; - } - - TEST(Buffering, NoFlashOnlyRAM) { - uint8_t ram_buffer[100]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - nullptr, 0); - - uint8_t small[10]; - auto num_bytes = buffering_append(small, sizeof(small)); - EXPECT_EQ(10, num_bytes) << "Could not add to RAM"; - - num_bytes = buffering_append(small, sizeof(small)); - EXPECT_EQ(10, num_bytes) << "Could not add to RAM"; - - num_bytes = buffering_append(small, sizeof(small)); - EXPECT_EQ(10, num_bytes) << "Could not add to RAM"; - - auto state = buffering_get_buffer(); - EXPECT_EQ(30, state->pos) << "Invalid buffer size"; - - uint8_t small2[70]; - num_bytes = buffering_append(small2, sizeof(small2)); - EXPECT_EQ(70, num_bytes); - - state = buffering_get_buffer(); - EXPECT_EQ(100, state->pos) << "Invalid buffer size"; - - num_bytes = buffering_append(small, sizeof(small)); - EXPECT_EQ(0, num_bytes) << "Could add to RAM when it should have been impossible"; - } - - TEST(Buffering, NoFlash) { - uint8_t ram_buffer[100]; - - buffering_init(ram_buffer, - sizeof(ram_buffer), - nullptr, 0); - - uint8_t big[1101]; - auto num_bytes = buffering_append(big, sizeof(big)); - EXPECT_EQ(0, num_bytes) << "Appending outside the bounds of the buffer should return error"; - } -} diff --git a/deps/ledger-zxlib/tests/doubledabble.cpp b/deps/ledger-zxlib/tests/doubledabble.cpp deleted file mode 100644 index 4b7b048..0000000 --- a/deps/ledger-zxlib/tests/doubledabble.cpp +++ /dev/null @@ -1,164 +0,0 @@ -/******************************************************************************* -* (c) 2019 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "gmock/gmock.h" - -#include -#include "bignum.h" - -using ::testing::TestWithParam; -using ::testing::Values; - -typedef struct { - std::string hex; - std::string expectedOutput; -} bignum_testcase_t; - -class BignumLittleEndianTests : public ::testing::TestWithParam { -}; - -class BignumBigEndianTests : public ::testing::TestWithParam { -}; - -INSTANTIATE_TEST_SUITE_P - -( - BignumTestCases, BignumLittleEndianTests, testing::Values( - bignum_testcase_t{"00", "0"}, - bignum_testcase_t{"01", "1"}, - bignum_testcase_t{"0001", "256"}, - bignum_testcase_t{"03E8", "59395"}, - bignum_testcase_t{"E803", "1000"}, - bignum_testcase_t{"10", "16"}, - bignum_testcase_t{"FF01", "511"}, - bignum_testcase_t{"0102", "513"}, - bignum_testcase_t{"FFFF01", "131071"}, - bignum_testcase_t{"a08601", "100000"}, - bignum_testcase_t{"40420f", "1000000"}, - bignum_testcase_t{"809698", "10000000"}, - bignum_testcase_t{"002d3101", "20000000"}, - bignum_testcase_t{"00e1f505", "100000000"}, - bignum_testcase_t{"00407a10f35a", "100000000000000"}, - bignum_testcase_t{"d2029649", "1234567890"}, - bignum_testcase_t{"d20a3fce96f1cf8c9cb4378c37a4873f17621ebce404f5aa13", - "123456789012345678901234567890123456789012345678901234567890"} -)); - -// Check that bignums are printed properly (parametric tests) -TEST_P(BignumLittleEndianTests, print) { - auto testcase = GetParam(); - - uint8_t inBuffer[100]; - auto inBufferLen = parseHexString(inBuffer, sizeof(inBuffer), testcase.hex.c_str()); - - uint8_t bcdOut[100]; - uint16_t bcdOutLen = sizeof(bcdOut); - - bignumLittleEndian_to_bcd(bcdOut, bcdOutLen, inBuffer, static_cast(inBufferLen)); - - char bufferUI[300]; - bignumLittleEndian_bcdprint(bufferUI, sizeof(bufferUI), bcdOut, bcdOutLen); - EXPECT_THAT(std::string(bufferUI), testing::Eq(testcase.expectedOutput)); -} - -// Check that bignums are printed properly (range tests) -TEST(BignumLittleEndianTests, range) { - uint8_t inBuffer[100]; - - for (uint64_t i = 0; i < 10000; i++) { - std::stringstream s; - uint64_t tmp = i; - while (tmp != 0) { - s << std::setfill('0') << std::setw(2) << std::hex << tmp % 256; - tmp /= 256; - } - auto inBufferLen = parseHexString(inBuffer, sizeof(inBuffer), s.str().c_str()); - - uint8_t bcdOut[100]; - uint16_t bcdOutLen = sizeof(bcdOut); - bignumLittleEndian_to_bcd(bcdOut, bcdOutLen, inBuffer, static_cast(inBufferLen)); - char bufferUI[300]; - bignumLittleEndian_bcdprint(bufferUI, sizeof(bufferUI), bcdOut, bcdOutLen); - - std::stringstream expected; - expected << i; - EXPECT_THAT(std::string(bufferUI), testing::Eq(expected.str())) << s.str(); - } -} - -INSTANTIATE_TEST_SUITE_P - -( - BignumTestCases, BignumBigEndianTests, testing::Values( - bignum_testcase_t{"00", "0"}, - bignum_testcase_t{"01", "1"}, - bignum_testcase_t{"0001", "1"}, - bignum_testcase_t{"000001", "1"}, - bignum_testcase_t{"03E8", "1000"}, - bignum_testcase_t{"E803", "59395"}, - bignum_testcase_t{"10", "16"}, - bignum_testcase_t{"FF01", "65281"}, - bignum_testcase_t{"01FF", "511"}, - bignum_testcase_t{"0102", "258"}, - bignum_testcase_t{"FFFF01", "16776961"}, - bignum_testcase_t{"a08601", "10520065"}, - bignum_testcase_t{"40420f", "4211215"}, - bignum_testcase_t{"809698", "8427160"}, - bignum_testcase_t{"002d3101", "2961665"}, - bignum_testcase_t{"00e1f505", "14808325"}, - bignum_testcase_t{"00407a10f35a", "276925838170"}, - bignum_testcase_t{"d2029649", "3523384905"}, - bignum_testcase_t{"d20a3fce96f1cf8c9cb4378c37a4873f17621ebce404f5aa13", - "1318442675213289749221432902819395197389189473307425559128595"} -)); - -// Check that bignums are printed properly (parametric tests) -TEST_P(BignumBigEndianTests, print) { - auto testcase = GetParam(); - - uint8_t inBuffer[100]; - auto inBufferLen = parseHexString(inBuffer, sizeof(inBuffer), testcase.hex.c_str()); - - uint8_t bcdOut[100]; - uint16_t bcdOutLen = sizeof(bcdOut); - bignumBigEndian_to_bcd(bcdOut, bcdOutLen, inBuffer, static_cast(inBufferLen)); - - char bufferUI[300]; - bignumBigEndian_bcdprint(bufferUI, sizeof(bufferUI), bcdOut, bcdOutLen); - EXPECT_THAT(std::string(bufferUI), testing::Eq(testcase.expectedOutput)); -} - -// Check that bignums are printed properly (range tests) -TEST(BignumBigEndianTests, range) { - uint8_t inBuffer[100]; - - for (uint64_t i = 0; i < 2500; i += 7) { - std::stringstream s; - s << std::setfill('0') << std::setw(10) << std::hex << i; - std::cout << s.str() << std::endl; - auto inBufferLen = parseHexString(inBuffer, sizeof(inBuffer), s.str().c_str()); - - uint8_t bcdOut[100]; - uint16_t bcdOutLen = sizeof(bcdOut); - bignumBigEndian_to_bcd(bcdOut, bcdOutLen, inBuffer, static_cast(inBufferLen)); - char bufferUI[300]; - bignumBigEndian_bcdprint(bufferUI, sizeof(bufferUI), bcdOut, bcdOutLen); - - std::stringstream expected; - expected << i; - EXPECT_THAT(std::string(bufferUI), testing::Eq(expected.str())) << s.str(); - } -} diff --git a/deps/ledger-zxlib/tests/hexutils.cpp b/deps/ledger-zxlib/tests/hexutils.cpp deleted file mode 100644 index e9d3ae4..0000000 --- a/deps/ledger-zxlib/tests/hexutils.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "gmock/gmock.h" - -#include - -#include "hexutils.h" - -TEST(HEXUTILS, parseHexString) { - char s[] = "1234567890"; - uint8_t data[100]; - - auto length = parseHexString(data, sizeof(data), s); - - ASSERT_THAT(length, testing::Eq(5)); - - ASSERT_THAT(data[0], testing::Eq(0x12)); - ASSERT_THAT(data[1], testing::Eq(0x34)); - ASSERT_THAT(data[2], testing::Eq(0x56)); - ASSERT_THAT(data[3], testing::Eq(0x78)); - ASSERT_THAT(data[4], testing::Eq(0x90)); -} - -TEST(HEXUTILS, parseHexString2) { - char s[] = "be333be7ee"; - uint8_t data[100]; - - auto length = parseHexString(data, sizeof(data), s); - - ASSERT_THAT(length, testing::Eq(5)); - - ASSERT_THAT(data[0], testing::Eq(0xbe)); - ASSERT_THAT(data[1], testing::Eq(0x33)); - ASSERT_THAT(data[2], testing::Eq(0x3b)); - ASSERT_THAT(data[3], testing::Eq(0xe7)); - ASSERT_THAT(data[4], testing::Eq(0xee)); -} diff --git a/deps/ledger-zxlib/tests/macros.cpp b/deps/ledger-zxlib/tests/macros.cpp deleted file mode 100644 index a4a2e86..0000000 --- a/deps/ledger-zxlib/tests/macros.cpp +++ /dev/null @@ -1,502 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include -#include -#include - -namespace { - TEST(FORMAT, array_to_hexstr) { - uint8_t array1[] = {1, 3, 5}; - - char output[20]; - memset(output, 1, 20); - - array_to_hexstr(output, sizeof(output), array1, sizeof(array1)); - EXPECT_EQ(memcmp(output, "010305", 2 * sizeof(array1)), 0); - EXPECT_EQ(output[2 * sizeof(array1)], 0); - } - - TEST(FORMAT, fpuint64_to_str) { - char output[100]; - printf("\n"); - - fpuint64_to_str(output, sizeof(output), 123, 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.00123"); - - fpuint64_to_str(output, sizeof(output), 1234, 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.01234"); - - fpuint64_to_str(output, sizeof(output), 12345, 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.12345"); - - fpuint64_to_str(output, sizeof(output), 123456, 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "1.23456"); - - fpuint64_to_str(output, sizeof(output), 1234567, 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "12.34567"); - } - - TEST(FORMAT, fpstr_to_str) { - char output[100]; - printf("\n"); - - fpstr_to_str(output, sizeof(output), "", 0); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0"); - - fpstr_to_str(output, sizeof(output), "1", 0); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "1"); - - fpstr_to_str(output, sizeof(output), "123", 0); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "123"); - - fpstr_to_str(output, sizeof(output), "", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.00000"); - - fpstr_to_str(output, sizeof(output), "0", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.00000"); - - fpstr_to_str(output, sizeof(output), "123", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.00123"); - - fpstr_to_str(output, sizeof(output), "1234", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.01234"); - - fpstr_to_str(output, sizeof(output), "12345", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.12345"); - - fpstr_to_str(output, sizeof(output), "123456", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "1.23456"); - - fpstr_to_str(output, sizeof(output), "1234567", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "12.34567"); - } - - TEST(FORMAT, fpstr_to_str_BAD_zeros) { - char output[8]; - printf("\n"); - - fpstr_to_str(output, sizeof(output), "", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.00000"); - - fpstr_to_str(output, sizeof(output), "", 6); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "", 7); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "", 8); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - } - - TEST(FORMAT, fpstr_to_str_BAD_short) { - char output[8]; - printf("\n"); - - fpstr_to_str(output, sizeof(output), "123", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "0.00123"); - - fpstr_to_str(output, sizeof(output), "123", 6); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "123", 7); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "123", 8); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - } - - TEST(FORMAT, fpstr_to_str_BAD_long) { - char output[8]; - printf("\n"); - - fpstr_to_str(output, sizeof(output), "123456", 5); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "1.23456"); - - fpstr_to_str(output, sizeof(output), "123456", 6); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "123456", 7); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "12345", 7); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "12345", 2); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "123.45"); - - fpstr_to_str(output, sizeof(output), "123456", 2); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "1234.56"); - - fpstr_to_str(output, sizeof(output), "1234567", 2); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "12345678", 2); - printf("%10s\n", output); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "1234567890", 0); - EXPECT_EQ(std::string(output), "ERR"); - - fpstr_to_str(output, sizeof(output), "123456", 0); - EXPECT_EQ(std::string(output), "123456"); - } - - TEST(FORMAT, fpuint64_to_str_zeros) { - char output[100]; - printf("\n"); - - fpuint64_to_str(output, sizeof(output), 0, 9); - printf("%11s\n", output); - EXPECT_EQ(std::string(output), "0.000000000"); - - fpuint64_to_str(output, sizeof(output), 0, 1); - printf("%11s\n", output); - EXPECT_EQ(std::string(output), "0.0"); - - fpuint64_to_str(output, sizeof(output), 1, 1); - printf("%11s\n", output); - EXPECT_EQ(std::string(output), "0.1"); - - fpuint64_to_str(output, sizeof(output), 10, 1); - printf("%11s\n", output); - EXPECT_EQ(std::string(output), "1.0"); - } - - TEST(FORMAT, number_trimming) { - char output[100]; - - snprintf(output, sizeof(output), "0"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "0"); - - snprintf(output, sizeof(output), "10"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "10"); - - snprintf(output, sizeof(output), "10.10"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "10.1"); - - snprintf(output, sizeof(output), "0.0"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "0.0"); - - snprintf(output, sizeof(output), "0.00"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "0.0"); - - snprintf(output, sizeof(output), "0.01"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "0.01"); - - snprintf(output, sizeof(output), "0.010"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "0.01"); - - snprintf(output, sizeof(output), "0.010000"); - number_inplace_trimming(output, 1); - EXPECT_EQ(std::string(output), "0.01"); - } - - TEST(FORMAT, intstr_to_fpstr_inplace_trimming_leading) { - char number[100]; - printf("\n"); - - snprintf(number, sizeof(number), "0"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "0"); - - snprintf(number, sizeof(number), "00"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "0"); - - snprintf(number, sizeof(number), "0000"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "0"); - - snprintf(number, sizeof(number), "00001"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "1"); - - snprintf(number, sizeof(number), "000011"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "11"); - - snprintf(number, sizeof(number), "10000"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "10000"); - } - - TEST(FORMAT, intstr_to_fpstr_inplace_empty) { - char number[100]; - printf("\n"); - - MEMZERO(number, sizeof(number)); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "0"); - - MEMZERO(number, sizeof(number)); - intstr_to_fpstr_inplace(number, sizeof(number), 5); - EXPECT_EQ(std::string(number), "0.00000"); - - MEMZERO(number, sizeof(number)); - intstr_to_fpstr_inplace(number, sizeof(number), 10); - EXPECT_EQ(std::string(number), "0.0000000000"); - } - - TEST(FORMAT, intstr_to_fpstr_inplace) { - char number[100]; - printf("\n"); - - snprintf(number, sizeof(number), "1"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "1"); - - snprintf(number, sizeof(number), "123"); - intstr_to_fpstr_inplace(number, sizeof(number), 0); - EXPECT_EQ(std::string(number), "123"); - - snprintf(number, sizeof(number), "0"); - intstr_to_fpstr_inplace(number, sizeof(number), 5); - EXPECT_EQ(std::string(number), "0.00000"); - - snprintf(number, sizeof(number), "123"); - intstr_to_fpstr_inplace(number, sizeof(number), 5); - EXPECT_EQ(std::string(number), "0.00123"); - - snprintf(number, sizeof(number), "1234"); - intstr_to_fpstr_inplace(number, sizeof(number), 5); - EXPECT_EQ(std::string(number), "0.01234"); - - snprintf(number, sizeof(number), "12345"); - intstr_to_fpstr_inplace(number, sizeof(number), 5); - EXPECT_EQ(std::string(number), "0.12345"); - - snprintf(number, sizeof(number), "123456"); - intstr_to_fpstr_inplace(number, sizeof(number), 5); - EXPECT_EQ(std::string(number), "1.23456"); - - snprintf(number, sizeof(number), "1234567"); - intstr_to_fpstr_inplace(number, sizeof(number), 5); - EXPECT_EQ(std::string(number), "12.34567"); - } - - TEST(INT64_TO_STR, Zero) { - char temp[10]; - const char *error = int64_to_str(temp, sizeof(temp), int64_t(0)); - EXPECT_STREQ(temp, "0"); - EXPECT_TRUE(error == nullptr); - } - - TEST(INT64_TO_STR, Positive_1234) { - char temp[10]; - const char *error = int64_to_str(temp, sizeof(temp), int64_t(1234)); - EXPECT_STREQ(temp, "1234"); - EXPECT_TRUE(error == nullptr); - } - - TEST(INT64_TO_STR, Negative_1234) { - char temp[10]; - const char *error = int64_to_str(temp, sizeof(temp), int64_t(-1234)); - EXPECT_STREQ(temp, "-1234"); - EXPECT_TRUE(error == nullptr); - } - - TEST(INT64_TO_STR, TooSmall_0) { - char temp[1]; - const char *error = int64_to_str(temp, sizeof(temp), int64_t(0)); - EXPECT_STREQ("Buffer too small", error); - } - - TEST(INT64_TO_STR, FitsJust) { - char temp[4]; - const char *error = int64_to_str(temp, sizeof(temp), int64_t(999)); - EXPECT_STREQ(temp, "999"); - EXPECT_TRUE(error == nullptr); - } - - TEST(INT64_TO_STR, TooSmall_10) { - char temp[2]; - const char *error = int64_to_str(temp, sizeof(temp), int64_t(10)); - EXPECT_STREQ("Buffer too small", error); - } - - TEST(INT64_TO_STR, Max) { - char temp[20]; - const char *error = int64_to_str(temp, sizeof(temp), std::numeric_limits::max()); - EXPECT_STREQ(temp, "9223372036854775807"); - EXPECT_TRUE(error == nullptr); - } - - TEST(INT64_TO_STR, Min) { - char temp[21]; - const char *error = int64_to_str(temp, sizeof(temp), std::numeric_limits::min()); - EXPECT_STREQ(temp, "-9223372036854775808"); - EXPECT_TRUE(error == nullptr); - } - - TEST(STR_TO_INT8, Min) { - char numberStr[] = "-128"; - char error = 0; - int8_t number = str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(-128, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT8, Max) { - char numberStr[] = "127"; - char error = 0; - int8_t number = str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(127, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT8, Zero) { - char numberStr[] = "0"; - char error = 0; - int8_t number = str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(0, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT8, Hundred) { - char numberStr[] = "100"; - char error = 0; - int8_t number = str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(100, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT8, NegHundred) { - char numberStr[] = "-100"; - char error = 0; - int8_t number = str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(-100, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT8, OutsideBoundsPositive) { - char numberStr[] = "128"; - char error = 0; - str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(1, error); - } - - TEST(STR_TO_INT8, OutsideBoundsNegative) { - char numberStr[] = "-129"; - char error = 0; - str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(1, error); - } - - TEST(STR_TO_INT8, DummyData_Positive) { - char numberStr[] = "100b0"; - char error = 0; - str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(1, error); - } - - TEST(STR_TO_INT8, DummyData_Negative) { - char numberStr[] = "-1002xx"; - char error = 0; - str_to_int8(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(1, error); - } - - TEST(STR_TO_INT64, Min) { - char numberStr[] = "-9223372036854775807"; - char error = 0; - int64_t number = str_to_int64(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(-9223372036854775807, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT64, Max) { - char numberStr[] = "9223372036854775807"; - char error = 0; - int64_t number = str_to_int64(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(9223372036854775807, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT64, Zero) { - char numberStr[] = "0"; - char error = 0; - int64_t number = str_to_int64(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(0, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT64, Hundred) { - char numberStr[] = "100"; - char error = 0; - int64_t number = str_to_int64(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(100, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT64, NegHundred) { - char numberStr[] = "-100"; - char error = 0; - int64_t number = str_to_int64(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(-100, number); - EXPECT_EQ(0, error); - } - - TEST(STR_TO_INT64, DummyData_Positive) { - char numberStr[] = "100b0"; - char error = 0; - str_to_int64(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(1, error); - } - - TEST(STR_TO_INT64, DummyData_Negative) { - char numberStr[] = "-1002xx"; - char error = 0; - str_to_int64(numberStr, numberStr + strlen(numberStr), &error); - EXPECT_EQ(1, error); - } -} diff --git a/deps/ledger-zxlib/tests/sigutils.cpp b/deps/ledger-zxlib/tests/sigutils.cpp deleted file mode 100644 index 21fde60..0000000 --- a/deps/ledger-zxlib/tests/sigutils.cpp +++ /dev/null @@ -1,133 +0,0 @@ -/******************************************************************************* -* (c) 2020 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ - -#include "gmock/gmock.h" -#include -#include -#include -#include - -#include "sigutils.h" - -TEST(SIGUTILS, convertBasic) { - char inSignatureDERStr[] = "304402206878b5690514437a2342405029426cc2b25b4a03fc396fef845d656cf62bad2c022018610a8d37e3384245176ab49ddbdbe8da4133f661bf5ea7ad4e3d2b912d856f01"; - auto inSignatureDER = std::vector(71); - - auto length = parseHexString(inSignatureDER.data(), inSignatureDER.size(), inSignatureDERStr); - EXPECT_EQ(length, sizeof(inSignatureDERStr) / 2); - - uint8_t R[32]; - uint8_t S[32]; - uint8_t V; - - auto ret = convertDERtoRSV(inSignatureDER.data(), 0, R, S, &V); - EXPECT_EQ(ret, 0); - - char inSignatureDERStr_R[] = "6878b5690514437a2342405029426cc2b25b4a03fc396fef845d656cf62bad2c"; - char inSignatureDERStr_S[] = "18610a8d37e3384245176ab49ddbdbe8da4133f661bf5ea7ad4e3d2b912d856f"; - auto inSignatureDER_R = std::vector(32); - auto inSignatureDER_S = std::vector(32); - parseHexString(inSignatureDER_R.data(), inSignatureDER_R.size(), inSignatureDERStr_R); - parseHexString(inSignatureDER_S.data(), inSignatureDER_S.size(), inSignatureDERStr_S); - - EXPECT_THAT(R, ::testing::ElementsAreArray(inSignatureDER_R)); - EXPECT_THAT(S, ::testing::ElementsAreArray(inSignatureDER_S)); -} - -TEST(SIGUTILS, convertBasic2) { - char inSignatureDERStr[] = "30430220035942178e9e8d447cf9e6886f99c41bf942fb2880fd79aa2d4626489ec7821b021f6b3277dea0355c161d20a120ec9165500b5c9a2cd0fce1c4b8a5260bf6831f"; - auto inSignatureDER = std::vector(71); - - auto length = parseHexString(inSignatureDER.data(), inSignatureDER.size(), inSignatureDERStr); - EXPECT_EQ(length, sizeof(inSignatureDERStr) / 2); - - uint8_t R[32]; - uint8_t S[32]; - uint8_t V; - - auto ret = convertDERtoRSV(inSignatureDER.data(), 0, R, S, &V); - EXPECT_EQ(ret, 0); - - char inSignatureDERStr_R[] = "035942178e9e8d447cf9e6886f99c41bf942fb2880fd79aa2d4626489ec7821b"; - char inSignatureDERStr_S[] = "006b3277dea0355c161d20a120ec9165500b5c9a2cd0fce1c4b8a5260bf6831f"; - auto inSignatureDER_R = std::vector(32); - auto inSignatureDER_S = std::vector(32); - parseHexString(inSignatureDER_R.data(), inSignatureDER_R.size(), inSignatureDERStr_R); - parseHexString(inSignatureDER_S.data(), inSignatureDER_S.size(), inSignatureDERStr_S); - - char buffer[100]; - array_to_hexstr(buffer, sizeof(buffer), R, (int16_t) sizeof(R)); - std::cout << buffer << std::endl; - array_to_hexstr(buffer, sizeof(buffer), S, (int16_t) sizeof(S)); - std::cout << buffer << std::endl; - - EXPECT_THAT(R, ::testing::ElementsAreArray(inSignatureDER_R)); - EXPECT_THAT(S, ::testing::ElementsAreArray(inSignatureDER_S)); -} - -TEST(SIGUTILS, convertBasic3) { - char inSignatureDERStr[] = "3045022100e9b508a9cd66410b43992c01622cf9e1a6aa1353d836d7f428a6d1317f96f27d02200ca01cee5480388bad3802c08e0bcf357c091f3a5921e1e5d1e0e115dd14ff23"; - auto inSignatureDER = std::vector(71); - - auto length = parseHexString(inSignatureDER.data(), inSignatureDER.size(), inSignatureDERStr); - EXPECT_EQ(length, sizeof(inSignatureDERStr) / 2); - - uint8_t R[32]; - uint8_t S[32]; - uint8_t V; - - auto ret = convertDERtoRSV(inSignatureDER.data(), 0, R, S, &V); - EXPECT_EQ(ret, 0); - - char inSignatureDERStr_R[] = "e9b508a9cd66410b43992c01622cf9e1a6aa1353d836d7f428a6d1317f96f27d"; - char inSignatureDERStr_S[] = "0ca01cee5480388bad3802c08e0bcf357c091f3a5921e1e5d1e0e115dd14ff23"; - auto inSignatureDER_R = std::vector(32); - auto inSignatureDER_S = std::vector(32); - parseHexString(inSignatureDER_R.data(), inSignatureDER_R.size(), inSignatureDERStr_R); - parseHexString(inSignatureDER_S.data(), inSignatureDER_S.size(), inSignatureDERStr_S); - - EXPECT_THAT(R, ::testing::ElementsAreArray(inSignatureDER_R)); - EXPECT_THAT(S, ::testing::ElementsAreArray(inSignatureDER_S)); -} - -TEST(SIGUTILS, convertShort1) { - char inSignatureDERStr[] = "3041021e544670fe5627f2d483484582284f627d9cfd1e0ab123984e81611a8da4fc021f6d99f9afd3c4fa62cee8dff21786f9c23c8d2f524d8fd363acc6c6567dc380"; - char inSignatureDERStr_R[] = "0000544670fe5627f2d483484582284f627d9cfd1e0ab123984e81611a8da4fc"; - char inSignatureDERStr_S[] = "006d99f9afd3c4fa62cee8dff21786f9c23c8d2f524d8fd363acc6c6567dc380"; - - auto inSignatureDER = std::vector(sizeof(inSignatureDERStr)); - - auto length = parseHexString(inSignatureDER.data(), inSignatureDER.size(), inSignatureDERStr); - EXPECT_EQ(length, sizeof(inSignatureDERStr) / 2); - - uint8_t R[32]; - uint8_t S[32]; - uint8_t V; - - auto ret = convertDERtoRSV(inSignatureDER.data(), 0, R, S, &V); - EXPECT_EQ(ret, 0); - - - char R_str[200]; - char S_str[200]; - array_to_hexstr(R_str, sizeof(R_str), R, 32); - std::cout << R_str << std::endl; - array_to_hexstr(S_str, sizeof(S_str), S, 32); - std::cout << S_str << std::endl; - - EXPECT_STREQ(R_str, inSignatureDERStr_R); - EXPECT_STREQ(S_str, inSignatureDERStr_S); -} diff --git a/deps/ledger-zxlib/tests/zxformat.cpp b/deps/ledger-zxlib/tests/zxformat.cpp deleted file mode 100644 index 3ad7b24..0000000 --- a/deps/ledger-zxlib/tests/zxformat.cpp +++ /dev/null @@ -1,97 +0,0 @@ -/******************************************************************************* -* (c) 2018 Zondax GmbH -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -********************************************************************************/ -#include -#include -#include - -namespace { - TEST(FORMAT, nothingAdded) { - char buffer[10]; - - snprintf(buffer, sizeof(buffer), "TEST"); - auto prefix = std::string(""); - - EXPECT_EQ(zxerr_ok, z_str3join(buffer, sizeof(buffer), prefix.c_str(), "")); - EXPECT_STREQ(buffer, "TEST"); - } - - TEST(FORMAT, noPrefix) { - char buffer[10]; - - snprintf(buffer, sizeof(buffer), "TEST"); - auto prefix = std::string(""); - auto suffix = std::string("1"); - - EXPECT_EQ(zxerr_ok, z_str3join(buffer, sizeof(buffer), prefix.c_str(), suffix.c_str())); - EXPECT_STREQ(buffer, "TEST1"); - } - - TEST(FORMAT, prefixSimple) { - char buffer[10]; - - snprintf(buffer, sizeof(buffer), "TEST"); - auto prefix = std::string("xyz "); - auto suffix = std::string("1"); - - EXPECT_EQ(zxerr_ok, z_str3join(buffer, sizeof(buffer), prefix.c_str(), suffix.c_str())); - EXPECT_STREQ(buffer, "xyz TEST1"); - } - - TEST(FORMAT, limitBuffer0) { - char buffer[10]; - - snprintf(buffer, sizeof(buffer), "TEST"); - auto prefix = std::string("xyz"); - auto suffix = std::string("4"); - - EXPECT_EQ(zxerr_ok, z_str3join(buffer, sizeof(buffer), prefix.c_str(), suffix.c_str())); - EXPECT_STREQ(buffer, "xyzTEST4"); - } - - TEST(FORMAT, limitBuffer1) { - char buffer[9]; - - snprintf(buffer, sizeof(buffer), "TEST"); - auto prefix = std::string("xyz"); - auto suffix = std::string("4"); - - EXPECT_EQ(zxerr_ok, z_str3join(buffer, sizeof(buffer), prefix.c_str(), suffix.c_str())); - EXPECT_STREQ(buffer, "xyzTEST4"); - } - - TEST(FORMAT, limitBuffer2) { - char buffer[8]; - - snprintf(buffer, sizeof(buffer), "TEST"); - auto prefix = std::string("xyz"); - auto suffix = std::string("4"); - - EXPECT_EQ(zxerr_buffer_too_small, z_str3join(buffer, sizeof(buffer), prefix.c_str(), suffix.c_str())); - EXPECT_STREQ(buffer, "ERR???"); - } - - TEST(FORMAT, smallBuffer) { - char buffer[7]; - - snprintf(buffer, sizeof(buffer), "TEST"); - auto prefix = std::string("xyz"); - auto suffix = std::string("4"); - - EXPECT_EQ(zxerr_buffer_too_small, z_str3join(buffer, sizeof(buffer), prefix.c_str(), suffix.c_str())); - EXPECT_STREQ(buffer, "ERR???"); - } - -} diff --git a/deps/nanos-secure-sdk b/deps/nanos-secure-sdk index 1c16f9a..026a1f5 160000 --- a/deps/nanos-secure-sdk +++ b/deps/nanos-secure-sdk @@ -1 +1 @@ -Subproject commit 1c16f9ad50f792c62a948aacb650258660f262cb +Subproject commit 026a1f5cf55e68f74062cb2795804f9b4554ea15 diff --git a/deps/nanosplus-secure-sdk b/deps/nanosplus-secure-sdk index dd4684c..c2a17eb 160000 --- a/deps/nanosplus-secure-sdk +++ b/deps/nanosplus-secure-sdk @@ -1 +1 @@ -Subproject commit dd4684c36592ddbdfc1df78e045d93376beaba96 +Subproject commit c2a17ebe62395d7a7a36658bf4bec952c08d0df3 diff --git a/deps/nanox-secure-sdk b/deps/nanox-secure-sdk index ba45af7..86324f6 160000 --- a/deps/nanox-secure-sdk +++ b/deps/nanox-secure-sdk @@ -1 +1 @@ -Subproject commit ba45af7f4208b6c02ac35fb0d43a914228febd56 +Subproject commit 86324f6a73e269c04f6a3e3cf41f6569a8cc6c6b diff --git a/docs/img/clion_debugging.png b/docs/img/clion_debugging.png deleted file mode 100644 index f26ff75..0000000 Binary files a/docs/img/clion_debugging.png and /dev/null differ diff --git a/docs/img/cosmos_app1.png b/docs/img/cosmos_app1.png deleted file mode 100644 index d971cef..0000000 Binary files a/docs/img/cosmos_app1.png and /dev/null differ diff --git a/docs/img/cosmos_app2.png b/docs/img/cosmos_app2.png deleted file mode 100644 index 10c10ea..0000000 Binary files a/docs/img/cosmos_app2.png and /dev/null differ diff --git a/docs/img/cosmos_app3.png b/docs/img/cosmos_app3.png deleted file mode 100644 index 4011eea..0000000 Binary files a/docs/img/cosmos_app3.png and /dev/null differ diff --git a/docs/img/tendermint_app.png b/docs/img/tendermint_app.png deleted file mode 100644 index a21a37b..0000000 Binary files a/docs/img/tendermint_app.png and /dev/null differ diff --git a/docs/zondax_dark.png b/docs/zondax_dark.png new file mode 100644 index 0000000..a6c22df Binary files /dev/null and b/docs/zondax_dark.png differ diff --git a/docs/zondax_light.png b/docs/zondax_light.png new file mode 100644 index 0000000..181ce34 Binary files /dev/null and b/docs/zondax_light.png differ diff --git a/tests_zemu/package.json b/tests_zemu/package.json index 7dbade6..fd49ee9 100644 --- a/tests_zemu/package.json +++ b/tests_zemu/package.json @@ -19,7 +19,7 @@ }, "dependencies": { "@zondax/ledger-axelar": "^0.0.1", - "@zondax/zemu": "^0.29.0" + "@zondax/zemu": "^0.32.0" }, "devDependencies": { "@types/jest": "^27.4.0", diff --git a/tests_zemu/snapshots/s-mainmenu/00004.png b/tests_zemu/snapshots/s-mainmenu/00004.png index 131ba7b..c475aa1 100644 Binary files a/tests_zemu/snapshots/s-mainmenu/00004.png and b/tests_zemu/snapshots/s-mainmenu/00004.png differ diff --git a/tests_zemu/snapshots/s-mainmenu/00010.png b/tests_zemu/snapshots/s-mainmenu/00010.png index 131ba7b..c475aa1 100644 Binary files a/tests_zemu/snapshots/s-mainmenu/00010.png and b/tests_zemu/snapshots/s-mainmenu/00010.png differ diff --git a/tests_zemu/snapshots/sp-mainmenu/00004.png b/tests_zemu/snapshots/sp-mainmenu/00004.png index 63db780..308172d 100644 Binary files a/tests_zemu/snapshots/sp-mainmenu/00004.png and b/tests_zemu/snapshots/sp-mainmenu/00004.png differ diff --git a/tests_zemu/snapshots/sp-mainmenu/00010.png b/tests_zemu/snapshots/sp-mainmenu/00010.png index 63db780..308172d 100644 Binary files a/tests_zemu/snapshots/sp-mainmenu/00010.png and b/tests_zemu/snapshots/sp-mainmenu/00010.png differ diff --git a/tests_zemu/snapshots/x-mainmenu/00004.png b/tests_zemu/snapshots/x-mainmenu/00004.png index 63db780..308172d 100644 Binary files a/tests_zemu/snapshots/x-mainmenu/00004.png and b/tests_zemu/snapshots/x-mainmenu/00004.png differ diff --git a/tests_zemu/snapshots/x-mainmenu/00010.png b/tests_zemu/snapshots/x-mainmenu/00010.png index 63db780..308172d 100644 Binary files a/tests_zemu/snapshots/x-mainmenu/00010.png and b/tests_zemu/snapshots/x-mainmenu/00010.png differ