diff --git a/core/SConscript.boardloader b/core/SConscript.boardloader index 02614ca4f..7f63250f3 100644 --- a/core/SConscript.boardloader +++ b/core/SConscript.boardloader @@ -31,6 +31,9 @@ SOURCE_MOD += [ ] # modtrezorui +CPPPATH_MOD += [ + 'vendor/micropython/extmod/uzlib', +] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', 'embed/extmod/modtrezorui/font_bitmap.c', diff --git a/core/SConscript.bootloader b/core/SConscript.bootloader index f81cf6766..092c351a2 100644 --- a/core/SConscript.bootloader +++ b/core/SConscript.bootloader @@ -33,17 +33,22 @@ SOURCE_MOD += [ ] # modtrezorui +CPPPATH_MOD += [ + 'vendor/micropython/extmod/uzlib', +] CPPDEFINES_MOD += [ 'TREZOR_FONT_NORMAL_ENABLE', 'TREZOR_FONT_MONO_ENABLE', ] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/inflate.c', 'embed/extmod/modtrezorui/font_bitmap.c', 'embed/extmod/modtrezorui/font_roboto_regular_20.c', 'embed/extmod/modtrezorui/font_robotomono_regular_20.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', + 'vendor/micropython/extmod/uzlib/adler32.c', + 'vendor/micropython/extmod/uzlib/crc32.c', + 'vendor/micropython/extmod/uzlib/tinflate.c', ] SOURCE_STMHAL = [ diff --git a/core/SConscript.firmware b/core/SConscript.firmware index 367ae96b8..e83c765d5 100644 --- a/core/SConscript.firmware +++ b/core/SConscript.firmware @@ -136,6 +136,9 @@ SOURCE_MOD += [ ] # modtrezorui +CPPPATH_MOD += [ + 'vendor/micropython/extmod/uzlib', +] CPPDEFINES_MOD += [ 'TREZOR_FONT_BOLD_ENABLE', 'TREZOR_FONT_NORMAL_ENABLE', @@ -149,9 +152,11 @@ SOURCE_MOD += [ 'embed/extmod/modtrezorui/font_roboto_regular_20.c', 'embed/extmod/modtrezorui/font_robotomono_bold_20.c', 'embed/extmod/modtrezorui/font_robotomono_regular_20.c', - 'embed/extmod/modtrezorui/inflate.c', 'embed/extmod/modtrezorui/modtrezorui.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', + 'vendor/micropython/extmod/uzlib/adler32.c', + 'vendor/micropython/extmod/uzlib/crc32.c', + 'vendor/micropython/extmod/uzlib/tinflate.c', ] # modtrezorutils diff --git a/core/SConscript.prodtest b/core/SConscript.prodtest index ec1b264ac..2307c42a2 100644 --- a/core/SConscript.prodtest +++ b/core/SConscript.prodtest @@ -19,15 +19,20 @@ SOURCE_MOD += [ ] # modtrezorui +CPPPATH_MOD += [ + 'vendor/micropython/extmod/uzlib', +] CPPDEFINES_MOD += [ 'TREZOR_FONT_BOLD_ENABLE', ] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/inflate.c', 'embed/extmod/modtrezorui/font_bitmap.c', 'embed/extmod/modtrezorui/font_roboto_bold_20.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', + 'vendor/micropython/extmod/uzlib/adler32.c', + 'vendor/micropython/extmod/uzlib/crc32.c', + 'vendor/micropython/extmod/uzlib/tinflate.c', ] SOURCE_STMHAL = [ diff --git a/core/SConscript.reflash b/core/SConscript.reflash index d4a06d3e9..51c2c0e9b 100644 --- a/core/SConscript.reflash +++ b/core/SConscript.reflash @@ -16,15 +16,20 @@ SOURCE_MOD += [ ] # modtrezorui +CPPPATH_MOD += [ + 'vendor/micropython/extmod/uzlib', +] CPPDEFINES_MOD += [ 'TREZOR_FONT_BOLD_ENABLE', ] SOURCE_MOD += [ 'embed/extmod/modtrezorui/display.c', - 'embed/extmod/modtrezorui/inflate.c', 'embed/extmod/modtrezorui/font_bitmap.c', 'embed/extmod/modtrezorui/font_roboto_bold_20.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', + 'vendor/micropython/extmod/uzlib/adler32.c', + 'vendor/micropython/extmod/uzlib/crc32.c', + 'vendor/micropython/extmod/uzlib/tinflate.c', ] SOURCE_STMHAL = [ diff --git a/core/SConscript.unix b/core/SConscript.unix index d2ed5ff20..61d0c8d4b 100644 --- a/core/SConscript.unix +++ b/core/SConscript.unix @@ -131,6 +131,9 @@ SOURCE_MOD += [ ] # modtrezorui +CPPPATH_MOD += [ + 'vendor/micropython/extmod/uzlib', +] CPPDEFINES_MOD += [ 'TREZOR_FONT_BOLD_ENABLE', 'TREZOR_FONT_NORMAL_ENABLE', @@ -144,9 +147,11 @@ SOURCE_MOD += [ 'embed/extmod/modtrezorui/font_roboto_regular_20.c', 'embed/extmod/modtrezorui/font_robotomono_bold_20.c', 'embed/extmod/modtrezorui/font_robotomono_regular_20.c', - 'embed/extmod/modtrezorui/inflate.c', 'embed/extmod/modtrezorui/modtrezorui.c', 'embed/extmod/modtrezorui/qr-code-generator/qrcodegen.c', + 'vendor/micropython/extmod/uzlib/adler32.c', + 'vendor/micropython/extmod/uzlib/crc32.c', + 'vendor/micropython/extmod/uzlib/tinflate.c', ] if FROZEN: CPPDEFINES_MOD += ['TREZOR_EMULATOR_FROZEN'] diff --git a/core/embed/extmod/modtrezorui/display.c b/core/embed/extmod/modtrezorui/display.c index 5b9a9b8f4..e938b67ee 100644 --- a/core/embed/extmod/modtrezorui/display.c +++ b/core/embed/extmod/modtrezorui/display.c @@ -20,7 +20,6 @@ #define _GNU_SOURCE #include "font_bitmap.h" -#include "inflate.h" #ifdef TREZOR_FONT_NORMAL_ENABLE #include "font_roboto_regular_20.h" #endif @@ -36,6 +35,8 @@ #include "qr-code-generator/qrcodegen.h" +#include "uzlib.h" + #include "common.h" #include "display.h" @@ -91,19 +92,18 @@ static inline void clamp_coords(int x, int y, int w, int h, int *x0, int *y0, void display_clear(void) { const int saved_orientation = DISPLAY_ORIENTATION; - display_orientation( - 0); // set MADCTL first so that we can set the window correctly next - display_set_window( - 0, 0, MAX_DISPLAY_RESX - 1, - MAX_DISPLAY_RESY - 1); // address the complete frame memory + // set MADCTL first so that we can set the window correctly next + display_orientation(0); + // address the complete frame memory + display_set_window(0, 0, MAX_DISPLAY_RESX - 1, MAX_DISPLAY_RESY - 1); for (uint32_t i = 0; i < MAX_DISPLAY_RESX * MAX_DISPLAY_RESY; i++) { - PIXELDATA( - 0x0000); // 2 bytes per pixel because we're using RGB 5-6-5 format + // 2 bytes per pixel because we're using RGB 5-6-5 format + PIXELDATA(0x0000); } - display_set_window(0, 0, DISPLAY_RESX - 1, - DISPLAY_RESY - 1); // go back to restricted window - display_orientation( - saved_orientation); // if valid, go back to the saved orientation + // go back to restricted window + display_set_window(0, 0, DISPLAY_RESX - 1, DISPLAY_RESY - 1); + // if valid, go back to the saved orientation + display_orientation(saved_orientation); } void display_bar(int x, int y, int w, int h, uint16_t c) { @@ -174,96 +174,64 @@ void display_bar_radius(int x, int y, int w, int h, uint16_t c, uint16_t b, } } -#if TREZOR_MODEL == T - -static void inflate_callback_image(uint8_t byte1, uint32_t pos, - void *userdata) { - static uint8_t byte0; - if (pos % 2 == 0) { - byte0 = byte1; - return; - } - const int w = ((const int *)userdata)[0]; - const int x0 = ((const int *)userdata)[1]; - const int x1 = ((const int *)userdata)[2]; - const int y0 = ((const int *)userdata)[3]; - const int y1 = ((const int *)userdata)[4]; - const int px = (pos / 2) % w; - const int py = (pos / 2) / w; - if (px >= x0 && px <= x1 && py >= y0 && py <= y1) { - PIXELDATA((byte0 << 8) | byte1); - } +#define UZLIB_WINDOW_SIZE (1 << 10) + +static void uzlib_prepare(struct uzlib_uncomp *decomp, uint8_t *window, + const void *src, uint32_t srcsize, void *dest, + uint32_t destsize) { + memzero(decomp, sizeof(struct uzlib_uncomp)); + if (window) { + memzero(window, UZLIB_WINDOW_SIZE); + } + memzero(dest, destsize); + decomp->source = (const uint8_t *)src; + decomp->source_limit = decomp->source + srcsize; + decomp->dest = (uint8_t *)dest; + decomp->dest_limit = decomp->dest + destsize; + uzlib_uncompress_init(decomp, window, window ? UZLIB_WINDOW_SIZE : 0); } -#endif - -void display_image(int x, int y, int w, int h, const void *data, int datalen) { +void display_image(int x, int y, int w, int h, const void *data, + uint32_t datalen) { #if TREZOR_MODEL == T x += DISPLAY_OFFSET.x; y += DISPLAY_OFFSET.y; int x0, y0, x1, y1; clamp_coords(x, y, w, h, &x0, &y0, &x1, &y1); display_set_window(x0, y0, x1, y1); - int userdata[5] = {w, x0 - x, x1 - x, y0 - y, y1 - y}; - sinf_inflate(data, datalen, inflate_callback_image, userdata); + x0 -= x; + x1 -= x; + y0 -= y; + y1 -= y; + + struct uzlib_uncomp decomp; + uint8_t decomp_window[UZLIB_WINDOW_SIZE]; + uint8_t decomp_out[2]; + uzlib_prepare(&decomp, decomp_window, data, datalen, decomp_out, + sizeof(decomp_out)); + + for (uint32_t pos = 0; pos < w * h; pos++) { + int st = uzlib_uncompress(&decomp); + if (st == TINF_DONE) break; // all OK + if (st < 0) break; // error + const int px = pos % w; + const int py = pos / w; + if (px >= x0 && px <= x1 && py >= y0 && py <= y1) { + PIXELDATA((decomp_out[0] << 8) | decomp_out[1]); + } + decomp.dest = (uint8_t *)&decomp_out; + } #endif } -#if TREZOR_MODEL == T - -static void inflate_callback_avatar(uint8_t byte1, uint32_t pos, - void *userdata) { #define AVATAR_BORDER_SIZE 4 #define AVATAR_BORDER_LOW \ (AVATAR_IMAGE_SIZE / 2 - AVATAR_BORDER_SIZE) * \ (AVATAR_IMAGE_SIZE / 2 - AVATAR_BORDER_SIZE) #define AVATAR_BORDER_HIGH (AVATAR_IMAGE_SIZE / 2) * (AVATAR_IMAGE_SIZE / 2) #define AVATAR_ANTIALIAS 1 - static uint8_t byte0; - if (pos % 2 == 0) { - byte0 = byte1; - return; - } - const int w = ((const int *)userdata)[0]; - const int x0 = ((const int *)userdata)[1]; - const int x1 = ((const int *)userdata)[2]; - const int y0 = ((const int *)userdata)[3]; - const int y1 = ((const int *)userdata)[4]; - const int fgcolor = ((const int *)userdata)[5]; - const int bgcolor = ((const int *)userdata)[6]; - const int px = (pos / 2) % w; - const int py = (pos / 2) / w; - if (px >= x0 && px <= x1 && py >= y0 && py <= y1) { - int d = (px - w / 2) * (px - w / 2) + (py - w / 2) * (py - w / 2); - // inside border area - if (d < AVATAR_BORDER_LOW) { - PIXELDATA((byte0 << 8) | byte1); - } else - // outside border area - if (d > AVATAR_BORDER_HIGH) { - PIXELDATA(bgcolor); - // border area - } else { -#if AVATAR_ANTIALIAS - d = 31 * (d - AVATAR_BORDER_LOW) / - (AVATAR_BORDER_HIGH - AVATAR_BORDER_LOW); - uint16_t c; - if (d >= 16) { - c = interpolate_color(bgcolor, fgcolor, d - 16); - } else { - c = interpolate_color(fgcolor, (byte0 << 8) | byte1, d); - } - PIXELDATA(c); -#else - PIXELDATA(fgcolor); -#endif - } - } -} -#endif - -void display_avatar(int x, int y, const void *data, int datalen, +void display_avatar(int x, int y, const void *data, uint32_t datalen, uint16_t fgcolor, uint16_t bgcolor) { #if TREZOR_MODEL == T x += DISPLAY_OFFSET.x; @@ -271,51 +239,94 @@ void display_avatar(int x, int y, const void *data, int datalen, int x0, y0, x1, y1; clamp_coords(x, y, AVATAR_IMAGE_SIZE, AVATAR_IMAGE_SIZE, &x0, &y0, &x1, &y1); display_set_window(x0, y0, x1, y1); - int userdata[7] = {AVATAR_IMAGE_SIZE, x0 - x, x1 - x, y0 - y, y1 - y, - fgcolor, bgcolor}; - sinf_inflate(data, datalen, inflate_callback_avatar, userdata); + x0 -= x; + x1 -= x; + y0 -= y; + y1 -= y; + + struct uzlib_uncomp decomp; + uint8_t decomp_window[UZLIB_WINDOW_SIZE]; + uint8_t decomp_out[2]; + uzlib_prepare(&decomp, decomp_window, data, datalen, decomp_out, + sizeof(decomp_out)); + + for (uint32_t pos = 0; pos < AVATAR_IMAGE_SIZE * AVATAR_IMAGE_SIZE; pos++) { + int st = uzlib_uncompress(&decomp); + if (st == TINF_DONE) break; // all OK + if (st < 0) break; // error + const int px = pos % AVATAR_IMAGE_SIZE; + const int py = pos / AVATAR_IMAGE_SIZE; + if (px >= x0 && px <= x1 && py >= y0 && py <= y1) { + int d = (px - AVATAR_IMAGE_SIZE / 2) * (px - AVATAR_IMAGE_SIZE / 2) + + (py - AVATAR_IMAGE_SIZE / 2) * (py - AVATAR_IMAGE_SIZE / 2); + // inside border area + if (d < AVATAR_BORDER_LOW) { + PIXELDATA((decomp_out[0] << 8) | decomp_out[1]); + } else + // outside border area + if (d > AVATAR_BORDER_HIGH) { + PIXELDATA(bgcolor); + // border area + } else { +#if AVATAR_ANTIALIAS + d = 31 * (d - AVATAR_BORDER_LOW) / + (AVATAR_BORDER_HIGH - AVATAR_BORDER_LOW); + uint16_t c; + if (d >= 16) { + c = interpolate_color(bgcolor, fgcolor, d - 16); + } else { + c = interpolate_color(fgcolor, (decomp_out[0] << 8) | decomp_out[1], + d); + } + PIXELDATA(c); +#else + PIXELDATA(fgcolor); #endif -} - -static void inflate_callback_icon(uint8_t byte, uint32_t pos, void *userdata) { - const uint16_t *colortable = (const uint16_t *)(((const int *)userdata) + 5); - const int w = ((const int *)userdata)[0]; - const int x0 = ((const int *)userdata)[1]; - const int x1 = ((const int *)userdata)[2]; - const int y0 = ((const int *)userdata)[3]; - const int y1 = ((const int *)userdata)[4]; - const int px = (pos * 2) % w; - const int py = (pos * 2) / w; - if (px >= x0 && px <= x1 && py >= y0 && py <= y1) { - PIXELDATA(colortable[byte >> 4]); - PIXELDATA(colortable[byte & 0x0F]); + } + } + decomp.dest = (uint8_t *)&decomp_out; } +#endif } -void display_icon(int x, int y, int w, int h, const void *data, int datalen, - uint16_t fgcolor, uint16_t bgcolor) { +void display_icon(int x, int y, int w, int h, const void *data, + uint32_t datalen, uint16_t fgcolor, uint16_t bgcolor) { x += DISPLAY_OFFSET.x; y += DISPLAY_OFFSET.y; x &= ~1; // cannot draw at odd coordinate int x0, y0, x1, y1; clamp_coords(x, y, w, h, &x0, &y0, &x1, &y1); display_set_window(x0, y0, x1, y1); - int userdata[5 + 16 * sizeof(uint16_t) / sizeof(int)] = {w, x0 - x, x1 - x, - y0 - y, y1 - y}; - set_color_table((uint16_t *)(userdata + 5), fgcolor, bgcolor); - sinf_inflate(data, datalen, inflate_callback_icon, userdata); -} + x0 -= x; + x1 -= x; + y0 -= y; + y1 -= y; -#if TREZOR_MODEL == T - -#include "loader.h" + uint16_t colortable[16]; + set_color_table(colortable, fgcolor, bgcolor); -static void inflate_callback_loader(uint8_t byte, uint32_t pos, - void *userdata) { - uint8_t *out = (uint8_t *)userdata; - out[pos] = byte; + struct uzlib_uncomp decomp; + uint8_t decomp_window[UZLIB_WINDOW_SIZE]; + uint8_t decomp_out; + uzlib_prepare(&decomp, decomp_window, data, datalen, &decomp_out, + sizeof(decomp_out)); + + for (uint32_t pos = 0; pos < w * h / 2; pos++) { + int st = uzlib_uncompress(&decomp); + if (st == TINF_DONE) break; // all OK + if (st < 0) break; // error + const int px = pos % w; + const int py = pos / w; + if (px >= x0 && px <= x1 && py >= y0 && py <= y1) { + PIXELDATA(colortable[decomp_out >> 4]); + PIXELDATA(colortable[decomp_out & 0x0F]); + } + decomp.dest = (uint8_t *)&decomp_out; + } } +#if TREZOR_MODEL == T +#include "loader.h" #endif void display_loader(uint16_t progress, bool indeterminate, int yoffset, @@ -340,7 +351,11 @@ void display_loader(uint16_t progress, bool indeterminate, int yoffset, LOADER_ICON_SIZE == *(uint16_t *)(icon + 6) && iconlen == 12 + *(uint32_t *)(icon + 8)) { uint8_t icondata[LOADER_ICON_SIZE * LOADER_ICON_SIZE / 2]; - sinf_inflate(icon + 12, iconlen - 12, inflate_callback_loader, icondata); + memzero(&icondata, sizeof(icondata)); + struct uzlib_uncomp decomp; + uzlib_prepare(&decomp, NULL, icon + 12, iconlen - 12, icondata, + sizeof(icondata)); + uzlib_uncompress(&decomp); icon = icondata; } else { icon = NULL; @@ -679,7 +694,7 @@ int display_text_width(const char *text, int textlen, int font) { #define QR_MAX_VERSION 9 -void display_qrcode(int x, int y, const char *data, int datalen, +void display_qrcode(int x, int y, const char *data, uint32_t datalen, uint8_t scale) { if (scale < 1 || scale > 10) return; diff --git a/core/embed/extmod/modtrezorui/display.h b/core/embed/extmod/modtrezorui/display.h index 75dbc596a..1145cfa2f 100644 --- a/core/embed/extmod/modtrezorui/display.h +++ b/core/embed/extmod/modtrezorui/display.h @@ -79,11 +79,12 @@ void display_bar(int x, int y, int w, int h, uint16_t c); void display_bar_radius(int x, int y, int w, int h, uint16_t c, uint16_t b, uint8_t r); -void display_image(int x, int y, int w, int h, const void *data, int datalen); -void display_avatar(int x, int y, const void *data, int datalen, +void display_image(int x, int y, int w, int h, const void *data, + uint32_t datalen); +void display_avatar(int x, int y, const void *data, uint32_t datalen, uint16_t fgcolor, uint16_t bgcolor); -void display_icon(int x, int y, int w, int h, const void *data, int datalen, - uint16_t fgcolor, uint16_t bgcolor); +void display_icon(int x, int y, int w, int h, const void *data, + uint32_t datalen, uint16_t fgcolor, uint16_t bgcolor); void display_loader(uint16_t progress, bool indeterminate, int yoffset, uint16_t fgcolor, uint16_t bgcolor, const uint8_t *icon, uint32_t iconlen, uint16_t iconfgcolor); @@ -103,7 +104,8 @@ void display_text_right(int x, int y, const char *text, int textlen, int font, uint16_t fgcolor, uint16_t bgcolor); int display_text_width(const char *text, int textlen, int font); -void display_qrcode(int x, int y, const char *data, int datalen, uint8_t scale); +void display_qrcode(int x, int y, const char *data, uint32_t datalen, + uint8_t scale); void display_offset(int set_xy[2], int *get_x, int *get_y); int display_orientation(int degrees); diff --git a/core/embed/extmod/modtrezorui/inflate.c b/core/embed/extmod/modtrezorui/inflate.c deleted file mode 100644 index eeacd25fe..000000000 --- a/core/embed/extmod/modtrezorui/inflate.c +++ /dev/null @@ -1,468 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -// clang-format off - -/* - * stream_inflate - tiny inflate library with output streaming - * - * Copyright (c) 2003 by Joergen Ibsen / Jibz - * All Rights Reserved - * http://www.ibsensoftware.com/ - * - * Copyright (c) 2014 by Paul Sokolovsky - * - * Copyright (c) 2016 by Pavol Rusnak - * - * This software is provided 'as-is', without any express - * or implied warranty. In no event will the authors be - * held liable for any damages arising from the use of - * this software. - * - * Permission is granted to anyone to use this software - * for any purpose, including commercial applications, - * and to alter it and redistribute it freely, subject to - * the following restrictions: - * - * 1. The origin of this software must not be - * misrepresented; you must not claim that you - * wrote the original software. If you use this - * software in a product, an acknowledgment in - * the product documentation would be appreciated - * but is not required. - * - * 2. Altered source versions must be plainly marked - * as such, and must not be misrepresented as - * being the original software. - * - * 3. This notice may not be removed or altered from - * any source distribution. - */ - -#include "inflate.h" - -// maximum possible window size (in bits) used during compression/deflate -#define SINF_WBITS 10 - -#define SINF_OK 0 -#define SINF_ERROR (-3) - -typedef struct { - uint16_t table[16]; /* table of code length counts */ - uint16_t trans[288]; /* code -> symbol translation table */ -} SINF_TREE; - -typedef struct { - const uint8_t *source; - uint32_t sourcelen; - uint32_t tag; - uint32_t bitcount; - uint8_t cbuf[1 << SINF_WBITS]; - int cbufi; - SINF_TREE ltree; /* dynamic length/symbol tree */ - SINF_TREE dtree; /* dynamic distance tree */ - void (* write)(uint8_t byte, uint32_t pos, void *userdata); - void *userdata; - uint32_t written; -} SINF_CTX; - -/* --------------------------------------------------- * - * -- uninitialized global data (static structures) -- * - * --------------------------------------------------- */ - -static const uint8_t SINF_LENGTH_BITS[30] = { - 0, 0, 0, 0, 0, 0, 0, 0, - 1, 1, 1, 1, 2, 2, 2, 2, - 3, 3, 3, 3, 4, 4, 4, 4, - 5, 5, 5, 5 -}; -static const uint16_t SINF_LENGTH_BASE[30] = { - 3, 4, 5, 6, 7, 8, 9, 10, - 11, 13, 15, 17, 19, 23, 27, 31, - 35, 43, 51, 59, 67, 83, 99, 115, - 131, 163, 195, 227, 258 -}; - -static const uint8_t SINF_DIST_BITS[30] = { - 0, 0, 0, 0, 1, 1, 2, 2, - 3, 3, 4, 4, 5, 5, 6, 6, - 7, 7, 8, 8, 9, 9, 10, 10, - 11, 11, 12, 12, 13, 13 -}; -static const uint16_t SINF_DIST_BASE[30] = { - 1, 2, 3, 4, 5, 7, 9, 13, - 17, 25, 33, 49, 65, 97, 129, 193, - 257, 385, 513, 769, 1025, 1537, 2049, 3073, - 4097, 6145, 8193, 12289, 16385, 24577 -}; - -/* special ordering of code length codes */ -static const uint8_t SINF_CLCIDX[] = { - 16, 17, 18, 0, 8, 7, 9, 6, - 10, 5, 11, 4, 12, 3, 13, 2, - 14, 1, 15 -}; - -/* ----------------------- * - * -- utility functions -- * - * ----------------------- */ - -static void sinf_write(SINF_CTX *ctx, uint8_t byte) -{ - ctx->cbuf[ctx->cbufi] = byte; - ctx->cbufi = (ctx->cbufi + 1) % (1 << SINF_WBITS); - ctx->write(byte, ctx->written, ctx->userdata); - ctx->written++; -} - -/* build the fixed huffman trees */ -static void sinf_build_fixed_trees(SINF_TREE *lt, SINF_TREE *dt) -{ - int i; - - /* build fixed length tree */ - for (i = 0; i < 7; ++i) lt->table[i] = 0; - - lt->table[7] = 24; - lt->table[8] = 152; - lt->table[9] = 112; - - for (i = 0; i < 24; ++i) lt->trans[i] = 256 + i; - for (i = 0; i < 144; ++i) lt->trans[24 + i] = i; - for (i = 0; i < 8; ++i) lt->trans[24 + 144 + i] = 280 + i; - for (i = 0; i < 112; ++i) lt->trans[24 + 144 + 8 + i] = 144 + i; - - /* build fixed distance tree */ - for (i = 0; i < 5; ++i) dt->table[i] = 0; - - dt->table[5] = 32; - - for (i = 0; i < 32; ++i) dt->trans[i] = i; -} - -/* given an array of code lengths, build a tree */ -static void sinf_build_tree(SINF_TREE *t, const uint8_t *lengths, uint32_t num) -{ - uint16_t offs[16]; - uint32_t i, sum; - - /* clear code length count table */ - for (i = 0; i < 16; ++i) t->table[i] = 0; - - /* scan symbol lengths, and sum code length counts */ - for (i = 0; i < num; ++i) t->table[lengths[i]]++; - - t->table[0] = 0; - - /* compute offset table for distribution sort */ - for (sum = 0, i = 0; i < 16; ++i) - { - offs[i] = sum; - sum += t->table[i]; - } - - /* create code->symbol translation table (symbols sorted by code) */ - for (i = 0; i < num; ++i) - { - if (lengths[i]) t->trans[offs[lengths[i]]++] = i; - } -} - -/* ---------------------- * - * -- decode functions -- * - * ---------------------- */ - -/* get one bit from source stream */ -static int sinf_getbit(SINF_CTX *ctx) -{ - uint32_t bit; - - /* check if tag is empty */ - if (!ctx->bitcount--) - { - /* load next tag */ - ctx->tag = *ctx->source++; - ctx->bitcount = 7; - } - - /* shift bit out of tag */ - bit = ctx->tag & 0x01; - ctx->tag >>= 1; - - return bit; -} - -/* read a num bit value from a stream and add base */ -static uint32_t sinf_read_bits(SINF_CTX *ctx, int num, int base) -{ - uint32_t val = 0; - - /* read num bits */ - if (num) - { - uint32_t limit = 1 << (num); - uint32_t mask; - - for (mask = 1; mask < limit; mask *= 2) - if (sinf_getbit(ctx)) val += mask; - } - - return val + base; -} - -/* given a data stream and a tree, decode a symbol */ -static int sinf_decode_symbol(SINF_CTX *ctx, SINF_TREE *t) -{ - int sum = 0, cur = 0, len = 0; - - /* get more bits while code value is above sum */ - do { - - cur = 2*cur + sinf_getbit(ctx); - - ++len; - - sum += t->table[len]; - cur -= t->table[len]; - - } while (cur >= 0); - - return t->trans[sum + cur]; -} - -/* given a data stream, decode dynamic trees from it */ -static void sinf_decode_trees(SINF_CTX *ctx, SINF_TREE *lt, SINF_TREE *dt) -{ - uint8_t lengths[288+32]; - uint32_t hlit, hdist, hclen; - uint32_t i, num, length; - - /* get 5 bits HLIT (257-286) */ - hlit = sinf_read_bits(ctx, 5, 257); - - /* get 5 bits HDIST (1-32) */ - hdist = sinf_read_bits(ctx, 5, 1); - - /* get 4 bits HCLEN (4-19) */ - hclen = sinf_read_bits(ctx, 4, 4); - - for (i = 0; i < 19; ++i) lengths[i] = 0; - - /* read code lengths for code length alphabet */ - for (i = 0; i < hclen; ++i) - { - /* get 3 bits code length (0-7) */ - uint32_t clen = sinf_read_bits(ctx, 3, 0); - - lengths[SINF_CLCIDX[i]] = clen; - } - - /* build code length tree, temporarily use length tree */ - sinf_build_tree(lt, lengths, 19); - - /* decode code lengths for the dynamic trees */ - for (num = 0; num < hlit + hdist; ) - { - int sym = sinf_decode_symbol(ctx, lt); - - switch (sym) - { - case 16: - /* copy previous code length 3-6 times (read 2 bits) */ - { - uint8_t prev = lengths[num - 1]; - for (length = sinf_read_bits(ctx, 2, 3); length; --length) - { - lengths[num++] = prev; - } - } - break; - case 17: - /* repeat code length 0 for 3-10 times (read 3 bits) */ - for (length = sinf_read_bits(ctx, 3, 3); length; --length) - { - lengths[num++] = 0; - } - break; - case 18: - /* repeat code length 0 for 11-138 times (read 7 bits) */ - for (length = sinf_read_bits(ctx, 7, 11); length; --length) - { - lengths[num++] = 0; - } - break; - default: - /* values 0-15 represent the actual code lengths */ - lengths[num++] = sym; - break; - } - } - - /* build dynamic trees */ - sinf_build_tree(lt, lengths, hlit); - sinf_build_tree(dt, lengths + hlit, hdist); -} - -/* ----------------------------- * - * -- block inflate functions -- * - * ----------------------------- */ - -/* given a stream and two trees, inflate a block of data */ -static int sinf_inflate_block_data(SINF_CTX *ctx, SINF_TREE *lt, SINF_TREE *dt) -{ - while (1) - { - int sym = sinf_decode_symbol(ctx, lt); - - /* check for end of block */ - if (sym == 256) - { - return SINF_OK; - } - - if (sym < 256) - { - sinf_write(ctx, sym); - } else { - - uint32_t length, offs, i; - int dist; - - sym -= 257; - - /* possibly get more bits from length code */ - length = sinf_read_bits(ctx, SINF_LENGTH_BITS[sym], SINF_LENGTH_BASE[sym]); - - dist = sinf_decode_symbol(ctx, dt); - - /* possibly get more bits from distance code */ - offs = sinf_read_bits(ctx, SINF_DIST_BITS[dist], SINF_DIST_BASE[dist]); - - /* copy match */ - for (i = 0; i < length; ++i) - { - sinf_write(ctx, ctx->cbuf[(ctx->cbufi + (1 << SINF_WBITS) - offs) % (1 << SINF_WBITS)]); - } - } - } -} - -/* inflate an uncompressed block of data */ -static int sinf_inflate_uncompressed_block(SINF_CTX *ctx) -{ - uint32_t length, invlength; - uint32_t i; - - /* get length */ - length = ctx->source[1]; - length = 256*length + ctx->source[0]; - - /* get one's complement of length */ - invlength = ctx->source[3]; - invlength = 256*invlength + ctx->source[2]; - - /* check length */ - if (length != (~invlength & 0x0000ffff)) return SINF_ERROR; - - ctx->source += 4; - - /* copy block */ - for (i = length; i; --i) sinf_write(ctx, *ctx->source++); - - /* make sure we start next block on a byte boundary */ - ctx->bitcount = 0; - - return SINF_OK; -} - -/* inflate a block of data compressed with fixed huffman trees */ -static int sinf_inflate_fixed_block(SINF_CTX *ctx) -{ - /* build fixed huffman trees */ - sinf_build_fixed_trees(&ctx->ltree, &ctx->dtree); - - /* decode block using fixed trees */ - return sinf_inflate_block_data(ctx, &ctx->ltree, &ctx->dtree); -} - -/* inflate a block of data compressed with dynamic huffman trees */ -static int sinf_inflate_dynamic_block(SINF_CTX *ctx) -{ - /* decode trees from stream */ - sinf_decode_trees(ctx, &ctx->ltree, &ctx->dtree); - - /* decode block using decoded trees */ - return sinf_inflate_block_data(ctx, &ctx->ltree, &ctx->dtree); -} - -/* ---------------------- * - * -- public functions -- * - * ---------------------- */ - -/* inflate stream from source */ -int sinf_inflate(const uint8_t *data, uint32_t datalen, void (*write_callback)(uint8_t byte, uint32_t pos, void *userdata), void *userdata) -{ - SINF_CTX ctx; - int bfinal; - - /* initialise data */ - ctx.bitcount = 0; - ctx.cbufi = 0; - ctx.source = data; - ctx.sourcelen = datalen; - ctx.write = write_callback; - ctx.userdata = userdata; - ctx.written = 0; - - do { - - uint32_t btype; - int res; - - /* read final block flag */ - bfinal = sinf_getbit(&ctx); - - /* read block type (2 bits) */ - btype = sinf_read_bits(&ctx, 2, 0); - - /* decompress block */ - switch (btype) - { - case 0: - /* decompress uncompressed block */ - res = sinf_inflate_uncompressed_block(&ctx); - break; - case 1: - /* decompress block with fixed huffman trees */ - res = sinf_inflate_fixed_block(&ctx); - break; - case 2: - /* decompress block with dynamic huffman trees */ - res = sinf_inflate_dynamic_block(&ctx); - break; - default: - return SINF_ERROR; - } - - if (res != SINF_OK) return SINF_ERROR; - - } while (!bfinal); - - return SINF_OK; -} diff --git a/core/embed/extmod/modtrezorui/inflate.h b/core/embed/extmod/modtrezorui/inflate.h deleted file mode 100644 index f40ba9235..000000000 --- a/core/embed/extmod/modtrezorui/inflate.h +++ /dev/null @@ -1,30 +0,0 @@ -/* - * This file is part of the Trezor project, https://trezor.io/ - * - * Copyright (c) SatoshiLabs - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -#ifndef __INFLATE_H__ -#define __INFLATE_H__ - -#include - -int sinf_inflate(const uint8_t *data, uint32_t datalen, - void (*write_callback)(uint8_t byte, uint32_t pos, - void *userdata), - void *userdata); - -#endif diff --git a/core/embed/extmod/modtrezorui/modtrezorui-display.h b/core/embed/extmod/modtrezorui/modtrezorui-display.h index 8c9752dea..234187fe0 100644 --- a/core/embed/extmod/modtrezorui/modtrezorui-display.h +++ b/core/embed/extmod/modtrezorui/modtrezorui-display.h @@ -17,8 +17,6 @@ * along with this program. If not, see . */ -#include "inflate.h" - #include "display.h" /// class Display: @@ -139,7 +137,7 @@ STATIC mp_obj_t mod_trezorui_Display_image(size_t n_args, } mp_int_t w = *(uint16_t *)(data + 4); mp_int_t h = *(uint16_t *)(data + 6); - mp_int_t datalen = *(uint32_t *)(data + 8); + uint32_t datalen = *(uint32_t *)(data + 8); if (datalen != image.len - 12) { mp_raise_ValueError("Invalid size of data"); } @@ -173,7 +171,7 @@ STATIC mp_obj_t mod_trezorui_Display_avatar(size_t n_args, if (w != AVATAR_IMAGE_SIZE || h != AVATAR_IMAGE_SIZE) { mp_raise_ValueError("Invalid image size"); } - mp_int_t datalen = *(uint32_t *)(data + 8); + uint32_t datalen = *(uint32_t *)(data + 8); if (datalen != image.len - 12) { mp_raise_ValueError("Invalid size of data"); } @@ -204,7 +202,7 @@ STATIC mp_obj_t mod_trezorui_Display_icon(size_t n_args, const mp_obj_t *args) { } mp_int_t w = *(uint16_t *)(data + 4); mp_int_t h = *(uint16_t *)(data + 6); - mp_int_t datalen = *(uint32_t *)(data + 8); + uint32_t datalen = *(uint32_t *)(data + 8); if (datalen != icon.len - 12) { mp_raise_ValueError("Invalid size of data"); } @@ -250,7 +248,7 @@ STATIC mp_obj_t mod_trezorui_Display_loader(size_t n_args, } mp_int_t w = *(uint16_t *)(data + 4); mp_int_t h = *(uint16_t *)(data + 6); - mp_int_t datalen = *(uint32_t *)(data + 8); + uint32_t datalen = *(uint32_t *)(data + 8); if (w != LOADER_ICON_SIZE || h != LOADER_ICON_SIZE) { mp_raise_ValueError("Invalid icon size"); }