├── LICENSE ├── README.md ├── examples └── Proverbs │ ├── Proverbs.ino │ └── Proverbs_Uni.h ├── library.properties ├── src ├── unishox1_progmem.cpp └── unishox1_progmem.h └── ss_proverbs.png /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright 2019 Siara Logics (cc) 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Compression and decompression Library for use with Arduino Flash Memory (PROGMEM) 2 | 3 | [Unishox](https://github.com/siara-cc/Unishox) is a technique used for compressing and decompressing short Unicode strings. This library allows storing of compressed text strings in Arduino Program Memory and provides API to retrieve each string individually using index. 4 | 5 | Unishox can achieve upto 60% compression depending on the composition of text. This can be especially useful in constrained environments like Arduino Uno where only 32kb of Flash memory is available. 6 | 7 | To find out how Unishox works, read [this article](https://github.com/siara-cc/Unishox/blob/master/Unishox_Article_1.pdf?raw=true). 8 | 9 | ## Applications 10 | 11 | - Displaying descriptive error or warning messages 12 | - Storing UTF-8 file content (such as html) 13 | - Transferring compressed content over low speed networks 14 | 15 | ## Usage 16 | 17 | First, the strings that need to compressed should to be listed in a file (usually with .txt extension). 18 | 19 | Download [Unishox](https://github.com/siara-cc/Unishox) and compile it using instructions provided in the repository. Then create compressed PROGMEM header using following command: 20 | 21 | ``` 22 | ./unishox1 -g 23 | ``` 24 | 25 | or 26 | 27 | ``` 28 | ./unishox -G 29 | ``` 30 | 31 | The command with capital G provides more compression taking into account repeating texts across the file. 32 | 33 | For example, if we use the file `Proverbs1.txt` given in the repository and the command `./unishox1 -g Proverbs.txt Proverbs_Uni`, a file by name `proverbs.h` will be generated. 34 | 35 | This file can be copied alongside any `.ino` file and included. Then the `Unishox1_pgm_decompress()` API from `unishox_progmem.h` in this library can be called to uncompress the strings. 36 | 37 | Please try the examples provided along with this library to see how it works. 38 | 39 | ## Compatibility 40 | 41 | This library can be used with Arduino Duemilanove, Uno upwards. It is useful only if saving by compressing text content is over 3000 bytes since the decompressor takes as much space. 42 | 43 | ## Limitations 44 | 45 | - Unicode (UTF-8) text is compressed well, but binary data (ASCII 0-31, ASCII 128-255) is not compressed well. 46 | 47 | ## Examples and screenshots 48 | 49 | ### Proverbs 50 | 51 | This example demonstrates de-compression of compressed proverbs from different languages. Source: Wikipedia 52 | 53 | ![](ss_proverbs.png?raw=true) 54 | 55 | ## Issues 56 | 57 | If you face any problems, please create issue on GitHub. 58 | -------------------------------------------------------------------------------- /examples/Proverbs/Proverbs.ino: -------------------------------------------------------------------------------- 1 | /* 2 | This example demonstrates de-compression of compressed proverbs 3 | Source: Wikipedia 4 | Decompresses data from Proverbs_Uni.h, which was generated using Unishox compression utility 5 | found at https://github.com/siara-cc/Unishox 6 | Command to generate Html.h: `./unishox1 -g Proverbs_Uni.sample Proverbs_Uni 7 | 8 | Original size : 4459 bytes 9 | Compressed size: 2793 bytes 10 | Savings: 37.36% 11 | 12 | How Unishox works: 13 | https://github.com/siara-cc/Unishox/blob/master/Unishox_Article_1.pdf?raw=true 14 | 15 | Other projects using Unishox: 16 | Compression library for Arduino - https://github.com/siara-cc/Unishox_Arduino_lib 17 | As SQLite loadable extension - https://github.com/siara-cc/Unishox_Sqlite_UDF 18 | Sqlite3 Library for ESP32 - https://github.com/siara-cc/esp32_arduino_sqlite3_lib 19 | Sqlite3 Library for ESP8266 - https://github.com/siara-cc/esp_arduino_sqlite3_lib 20 | Sqlite3 Library for ESP-IDF - https://github.com/siara-cc/esp32-idf-sqlite3 21 | */ 22 | #include "unishox1_progmem.h" 23 | #include "Proverbs_Uni.h" 24 | 25 | void setup() { 26 | 27 | Serial.begin(115200); 28 | randomSeed(analogRead(0)); 29 | 30 | } 31 | 32 | void loop() { 33 | 34 | // buffer for decompression 35 | // since -G option was used to generate Proverbs.h, the buffer size has to be 36 | // atleast Proverbs0_2_max_len * 2). 37 | // The call to unishox1_pgm_decompress() can be kept inside a function with this buffer 38 | // to release it immediately. 39 | char out[Proverbs_Uni_max_len * 2]; 40 | 41 | Serial.write("Welcome\n"); 42 | Serial.write("-------\n"); 43 | 44 | Serial.write("1. Print all Proverbs\n"); 45 | Serial.write("2. Print random proverb\n\n"); 46 | Serial.write("Enter choice:\n"); 47 | int ch = 0; 48 | while (true) { 49 | if (Serial.available()) { 50 | ch = Serial.read(); 51 | if (ch == '1' || ch == '2') 52 | break; 53 | } 54 | } 55 | 56 | if (ch == '1') { 57 | for (int i=0; i < Proverbs_Uni_line_count; i++) { 58 | int len = unishox1_pgm_decompress(Proverbs_Uni, i, out, 0); 59 | out[len] = 0; 60 | Serial.write(out); 61 | Serial.write("\n"); 62 | } 63 | } else if (ch == '2') { 64 | int len = unishox1_pgm_decompress(Proverbs_Uni, random(0, Proverbs_Uni_line_count - 1), out, 0); 65 | out[len] = 0; 66 | Serial.write(out); 67 | Serial.write("\n\n"); 68 | } 69 | 70 | } 71 | -------------------------------------------------------------------------------- /examples/Proverbs/Proverbs_Uni.h: -------------------------------------------------------------------------------- 1 | #ifndef __Proverbs_Uni_UNISHOX1_COMPRESSED__ 2 | #define __Proverbs_Uni_UNISHOX1_COMPRESSED__ 3 | const byte Proverbs_Uni_0[] PROGMEM = {13, 35, 93, 140, 190, 126, 104, 134, 127, 12, 223, 199, 120, 243}; 4 | const byte Proverbs_Uni_1[] PROGMEM = {4, 58, 200, 221, 36}; 5 | const byte Proverbs_Uni_2[] PROGMEM = {58, 41, 193, 128, 141, 158, 184, 197, 12, 11, 52, 89, 221, 24, 221, 50, 88, 125, 149, 243, 253, 251, 248, 235, 68, 61, 166, 123, 195, 200, 118, 254, 133, 42, 197, 104, 117, 142, 50, 91, 228, 239, 218, 254, 168, 85, 242, 31, 207, 252, 203, 120, 121, 88, 236, 23, 75, 104, 232}; 6 | const byte Proverbs_Uni_3[] PROGMEM = {48, 41, 192, 27, 173, 68, 191, 38, 162, 217, 175, 178, 26, 77, 134, 208, 181, 7, 241, 180, 127, 138, 133, 53, 234, 179, 70, 168, 85, 210, 245, 104, 102, 158, 66, 198, 33, 231, 94, 66, 172, 104, 215, 246, 34, 19, 7, 248, 232}; 7 | const byte Proverbs_Uni_4[] PROGMEM = {44, 41, 194, 122, 93, 220, 113, 117, 131, 14, 67, 252, 106, 31, 226, 236, 164, 105, 216, 63, 188, 60, 183, 200, 135, 11, 16, 65, 86, 116, 71, 90, 45, 30, 236, 253, 240, 109, 111, 144, 224, 183, 222, 58, 27}; 8 | const byte Proverbs_Uni_5[] PROGMEM = {48, 41, 195, 148, 191, 198, 17, 173, 53, 250, 12, 179, 253, 230, 192, 172, 155, 70, 2, 193, 247, 243, 162, 58, 209, 125, 216, 91, 70, 72, 59, 227, 238, 144, 120, 52, 99, 166, 135, 246, 133, 45, 27, 99, 219, 215, 91, 156, 116}; 9 | const byte Proverbs_Uni_6[] PROGMEM = {54, 41, 195, 39, 30, 129, 1, 100, 245, 180, 61, 52, 130, 237, 56, 47, 42, 183, 208, 142, 254, 254, 58, 209, 97, 128, 43, 167, 199, 82, 161, 77, 6, 41, 255, 230, 72, 112, 89, 239, 96, 211, 201, 97, 132, 53, 240, 14, 2, 7, 239, 255, 220, 249, 208}; 10 | const byte Proverbs_Uni_7[] PROGMEM = {4, 58, 200, 221, 36}; 11 | const byte Proverbs_Uni_8[] PROGMEM = {13, 32, 197, 111, 63, 52, 67, 63, 134, 111, 227, 188, 121, 155}; 12 | const byte Proverbs_Uni_9[] PROGMEM = {4, 58, 200, 221, 32}; 13 | const byte Proverbs_Uni_10[] PROGMEM = {75, 41, 192, 188, 252, 3, 63, 78, 209, 202, 134, 56, 76, 207, 213, 90, 92, 14, 223, 72, 219, 199, 168, 254, 254, 58, 209, 90, 240, 219, 8, 36, 43, 2, 107, 178, 144, 215, 194, 49, 22, 242, 173, 26, 208, 254, 132, 18, 28, 195, 193, 173, 90, 29, 163, 192, 56, 8, 26, 44, 49, 72, 110, 125, 161, 52, 193, 141, 12, 88, 84, 171, 0, 48, 160, 116}; 14 | const byte Proverbs_Uni_11[] PROGMEM = {62, 41, 195, 187, 189, 182, 210, 41, 133, 77, 26, 3, 50, 205, 113, 232, 173, 168, 23, 51, 133, 167, 59, 247, 241, 214, 139, 238, 196, 222, 30, 84, 43, 5, 250, 248, 79, 44, 48, 135, 102, 26, 121, 111, 151, 101, 43, 242, 30, 204, 17, 95, 149, 126, 204, 216, 64, 145, 128, 129, 186, 19, 241}; 15 | const byte Proverbs_Uni_12[] PROGMEM = {50, 41, 194, 148, 253, 13, 191, 27, 173, 191, 53, 254, 88, 59, 214, 202, 226, 132, 100, 150, 255, 127, 29, 104, 140, 103, 232, 95, 52, 38, 48, 28, 100, 135, 131, 90, 161, 253, 13, 157, 17, 13, 223, 24, 4, 203, 34, 194, 206, 103, 67}; 16 | const byte Proverbs_Uni_13[] PROGMEM = {83, 41, 196, 136, 237, 209, 247, 43, 27, 218, 213, 48, 29, 239, 242, 6, 158, 144, 31, 142, 43, 234, 79, 31, 148, 177, 147, 163, 200, 110, 178, 81, 247, 241, 214, 136, 124, 83, 229, 66, 154, 31, 222, 17, 6, 42, 248, 121, 248, 234, 94, 193, 173, 12, 120, 48, 38, 135, 11, 208, 132, 67, 246, 16, 94, 87, 194, 33, 86, 102, 213, 254, 180, 198, 135, 182, 16, 6, 38, 6, 232, 65, 120, 232}; 17 | const byte Proverbs_Uni_14[] PROGMEM = {35, 41, 194, 31, 109, 70, 242, 86, 182, 72, 145, 247, 241, 214, 136, 197, 208, 167, 243, 239, 200, 70, 58, 142, 165, 236, 229, 88, 135, 180, 205, 161, 214, 25, 161, 208}; 18 | const byte Proverbs_Uni_15[] PROGMEM = {44, 41, 192, 183, 59, 151, 52, 11, 240, 19, 141, 5, 218, 45, 22, 5, 181, 247, 241, 214, 136, 76, 93, 148, 133, 95, 199, 96, 191, 173, 234, 125, 91, 157, 229, 88, 133, 11, 253, 44, 161, 79, 254, 58, 27}; 19 | const byte Proverbs_Uni_16[] PROGMEM = {42, 41, 159, 69, 96, 219, 122, 56, 215, 24, 16, 127, 16, 58, 133, 239, 247, 241, 214, 138, 133, 111, 15, 121, 97, 133, 88, 37, 153, 29, 74, 195, 15, 6, 140, 11, 124, 41, 136, 3, 116, 29, 13}; 20 | const byte Proverbs_Uni_17[] PROGMEM = {42, 41, 194, 163, 164, 109, 11, 63, 33, 41, 255, 54, 22, 74, 228, 177, 118, 239, 96, 125, 252, 117, 162, 31, 20, 254, 213, 10, 127, 103, 223, 151, 134, 225, 163, 58, 129, 2, 232, 49, 191, 103, 67}; 21 | const byte Proverbs_Uni_18[] PROGMEM = {30, 41, 195, 34, 248, 24, 20, 18, 117, 236, 63, 191, 142, 180, 67, 217, 130, 33, 172, 77, 15, 121, 170, 191, 83, 242, 29, 181, 79, 231, 67}; 22 | const byte Proverbs_Uni_19[] PROGMEM = {35, 41, 159, 61, 99, 147, 186, 234, 84, 179, 90, 3, 56, 145, 119, 223, 199, 90, 33, 183, 200, 103, 240, 102, 121, 128, 231, 16, 136, 123, 4, 66, 111, 128, 44, 3}; 23 | const byte Proverbs_Uni_20[] PROGMEM = {58, 41, 192, 220, 139, 168, 121, 190, 13, 134, 184, 185, 188, 24, 228, 234, 246, 246, 80, 61, 176, 114, 88, 190, 128, 161, 247, 241, 214, 138, 157, 134, 41, 255, 203, 6, 62, 84, 41, 161, 51, 62, 116, 68, 101, 130, 211, 234, 127, 67, 140, 145, 150, 11, 255, 152, 70, 14, 134}; 24 | const byte Proverbs_Uni_21[] PROGMEM = {57, 41, 193, 52, 141, 18, 56, 175, 61, 87, 223, 139, 229, 154, 201, 198, 209, 63, 96, 45, 97, 221, 255, 157, 211, 247, 245, 161, 183, 202, 177, 85, 154, 53, 104, 198, 248, 82, 223, 46, 202, 76, 212, 46, 194, 0, 194, 0, 33, 94, 2, 176, 125, 86, 0, 98, 64, 232}; 25 | const byte Proverbs_Uni_22[] PROGMEM = {40, 41, 193, 125, 29, 30, 255, 18, 208, 112, 186, 159, 247, 241, 214, 136, 124, 83, 229, 66, 154, 175, 182, 140, 88, 65, 120, 234, 0, 164, 6, 231, 26, 51, 47, 47, 7, 117, 79, 231, 67}; 26 | const byte Proverbs_Uni_23[] PROGMEM = {55, 41, 193, 191, 76, 18, 38, 114, 5, 209, 238, 22, 86, 245, 128, 155, 34, 23, 144, 147, 92, 159, 239, 227, 173, 23, 101, 44, 224, 140, 144, 119, 200, 126, 233, 253, 24, 11, 46, 149, 161, 217, 116, 117, 45, 160, 12, 8, 123, 120, 209, 109, 43, 11, 216, 116}; 27 | const byte Proverbs_Uni_24[] PROGMEM = {60, 41, 193, 109, 220, 69, 58, 141, 38, 195, 183, 197, 162, 14, 173, 197, 202, 243, 130, 195, 160, 160, 100, 3, 223, 127, 29, 104, 179, 131, 104, 118, 213, 62, 67, 140, 149, 175, 117, 154, 52, 99, 62, 3, 133, 129, 136, 79, 187, 19, 202, 215, 240, 96, 30, 36, 4, 97, 216, 140, 232}; 28 | const byte Proverbs_Uni_25[] PROGMEM = {50, 41, 158, 224, 67, 198, 199, 115, 192, 248, 93, 105, 248, 129, 212, 153, 108, 57, 7, 239, 227, 173, 22, 152, 89, 205, 9, 143, 203, 178, 144, 192, 176, 124, 234, 84, 41, 253, 161, 253, 8, 36, 57, 174, 196, 42, 254, 102, 253, 71, 67}; 29 | const byte Proverbs_Uni_26[] PROGMEM = {58, 41, 193, 108, 156, 117, 163, 67, 116, 216, 79, 179, 183, 221, 157, 83, 5, 0, 252, 107, 159, 227, 44, 22, 133, 89, 155, 67, 115, 153, 144, 5, 96, 29, 242, 27, 102, 141, 80, 166, 132, 102, 122, 18, 24, 47, 194, 145, 150, 11, 250, 30, 254, 240, 139, 213, 156, 206, 134}; 30 | const byte Proverbs_Uni_27[] PROGMEM = {61, 41, 158, 224, 74, 241, 201, 16, 181, 182, 59, 209, 176, 24, 102, 34, 113, 57, 147, 171, 167, 241, 68, 97, 11, 68, 126, 194, 19, 52, 124, 172, 127, 223, 239, 227, 173, 21, 175, 111, 15, 4, 215, 130, 209, 136, 109, 153, 32, 236, 95, 154, 10, 253, 91, 195, 203, 217, 255, 224, 249, 208}; 31 | const byte Proverbs_Uni_28[] PROGMEM = {43, 41, 195, 26, 173, 124, 7, 29, 28, 213, 167, 43, 32, 125, 252, 117, 162, 161, 89, 230, 135, 197, 98, 20, 194, 24, 168, 83, 27, 230, 17, 155, 241, 212, 136, 123, 4, 67, 250, 16, 64, 21, 193, 208}; 32 | const byte Proverbs_Uni_29[] PROGMEM = {39, 41, 193, 90, 156, 93, 59, 70, 179, 197, 214, 247, 202, 12, 22, 43, 15, 34, 216, 138, 62, 254, 58, 209, 59, 248, 70, 72, 118, 252, 68, 60, 235, 202, 133, 52, 63, 159, 26, 116}; 33 | const byte Proverbs_Uni_30[] PROGMEM = {4, 58, 200, 221, 36}; 34 | const byte Proverbs_Uni_31[] PROGMEM = {13, 47, 12, 190, 223, 10, 68, 51, 248, 102, 254, 59, 199, 153}; 35 | const byte Proverbs_Uni_32[] PROGMEM = {4, 58, 200, 221, 36}; 36 | const byte Proverbs_Uni_33[] PROGMEM = {47, 32, 236, 75, 67, 133, 138, 206, 127, 197, 97, 88, 235, 94, 23, 229, 99, 194, 205, 119, 227, 162, 58, 209, 90, 24, 196, 16, 39, 244, 51, 247, 190, 126, 66, 183, 203, 155, 16, 39, 228, 119, 71, 239, 239, 199, 120, 232}; 37 | const byte Proverbs_Uni_34[] PROGMEM = {43, 0, 4, 122, 194, 11, 16, 119, 213, 105, 242, 240, 180, 52, 88, 86, 37, 252, 232, 142, 180, 78, 195, 20, 191, 153, 234, 127, 94, 222, 30, 242, 206, 91, 94, 66, 11, 225, 227, 172, 83, 177, 135, 67}; 38 | const byte Proverbs_Uni_35[] PROGMEM = {34, 0, 4, 109, 254, 159, 149, 107, 230, 188, 45, 9, 131, 119, 135, 176, 16, 45, 18, 161, 111, 194, 89, 146, 21, 126, 0, 196, 201, 120, 95, 9, 102, 71, 67}; 39 | const byte Proverbs_Uni_36[] PROGMEM = {76, 0, 4, 95, 98, 21, 99, 130, 180, 35, 65, 90, 48, 255, 182, 131, 193, 184, 130, 104, 206, 211, 228, 194, 10, 206, 136, 235, 69, 130, 254, 135, 236, 201, 97, 133, 86, 17, 166, 240, 242, 177, 216, 46, 149, 10, 222, 30, 242, 251, 48, 19, 14, 188, 176, 95, 215, 216, 76, 206, 250, 147, 54, 12, 127, 215, 193, 179, 169, 15, 102, 8, 179, 232, 117, 136, 116}; 40 | const byte Proverbs_Uni_37[] PROGMEM = {67, 33, 52, 107, 65, 231, 35, 80, 189, 124, 175, 132, 191, 35, 37, 77, 9, 191, 144, 42, 81, 168, 94, 192, 14, 79, 239, 199, 67, 162, 58, 209, 83, 8, 36, 53, 161, 241, 88, 140, 176, 90, 31, 176, 141, 16, 254, 208, 163, 169, 95, 8, 139, 80, 254, 132, 18, 166, 16, 64, 56, 184, 2, 197, 69, 253, 157, 13}; 41 | const byte Proverbs_Uni_38[] PROGMEM = {54, 33, 51, 132, 104, 60, 26, 249, 9, 158, 58, 128, 160, 138, 251, 241, 209, 29, 104, 140, 176, 90, 14, 249, 88, 130, 44, 230, 134, 63, 251, 57, 13, 207, 181, 188, 175, 140, 151, 182, 133, 221, 110, 112, 6, 108, 111, 237, 13, 190, 177, 237, 225, 227, 161}; 42 | const byte Proverbs_Uni_39[] PROGMEM = {21, 38, 18, 177, 185, 244, 252, 131, 214, 61, 135, 68, 117, 162, 11, 12, 218, 223, 33, 193, 111, 8}; 43 | const byte Proverbs_Uni_40[] PROGMEM = {28, 3, 6, 176, 76, 65, 98, 209, 225, 118, 21, 157, 17, 214, 136, 44, 51, 107, 195, 115, 40, 83, 242, 11, 76, 53, 130, 232, 232}; 44 | const byte Proverbs_Uni_41[] PROGMEM = {25, 38, 18, 173, 99, 70, 98, 172, 19, 16, 88, 131, 23, 240, 10, 36, 171, 70, 180, 24, 191, 191, 43, 8, 35, 161}; 45 | const byte Proverbs_Uni_42[] PROGMEM = {33, 32, 173, 89, 157, 255, 67, 219, 95, 193, 163, 26, 97, 47, 88, 120, 116, 71, 90, 33, 239, 182, 34, 28, 47, 254, 175, 42, 20, 215, 172, 51, 67, 161}; 46 | const byte Proverbs_Uni_43[] PROGMEM = {42, 32, 173, 7, 127, 180, 252, 152, 73, 248, 99, 53, 226, 17, 132, 172, 33, 182, 116, 71, 90, 42, 20, 209, 139, 13, 173, 242, 161, 77, 13, 223, 255, 62, 176, 192, 6, 16, 61, 130, 194, 58, 27}; 47 | const byte Proverbs_Uni_44[] PROGMEM = {52, 32, 173, 9, 161, 104, 243, 224, 246, 211, 242, 10, 208, 219, 19, 237, 8, 208, 86, 135, 11, 125, 104, 205, 245, 11, 253, 157, 17, 214, 136, 64, 40, 1, 173, 242, 161, 77, 13, 202, 20, 254, 176, 192, 14, 157, 16, 198, 126, 133, 243, 58, 27}; 48 | const byte Proverbs_Uni_45[] PROGMEM = {32, 32, 173, 24, 104, 104, 182, 102, 208, 220, 234, 185, 2, 198, 252, 116, 71, 90, 35, 23, 180, 41, 13, 195, 55, 228, 55, 5, 245, 94, 247, 142, 134}; 49 | const byte Proverbs_Uni_46[] PROGMEM = {56, 32, 173, 13, 102, 188, 250, 25, 97, 88, 239, 218, 246, 177, 163, 62, 211, 242, 10, 209, 168, 91, 70, 161, 108, 75, 67, 51, 232, 70, 30, 252, 232, 142, 180, 84, 41, 161, 207, 212, 135, 243, 226, 45, 242, 161, 77, 103, 52, 19, 49, 72, 95, 122, 244, 67, 161}; 50 | const byte Proverbs_Uni_47[] PROGMEM = {45, 32, 173, 12, 121, 244, 61, 97, 5, 189, 96, 133, 80, 141, 144, 40, 124, 131, 176, 239, 238, 192, 240, 226, 63, 159, 245, 33, 79, 129, 15, 31, 52, 42, 233, 6, 16, 176, 205, 0, 113, 80, 55, 61, 71, 67}; 51 | const byte Proverbs_Uni_48[] PROGMEM = {36, 32, 173, 122, 248, 123, 237, 123, 121, 24, 22, 199, 177, 10, 215, 243, 54, 116, 71, 90, 33, 193, 102, 8, 135, 102, 133, 228, 63, 180, 43, 5, 210, 49, 191, 103, 67}; 52 | const byte Proverbs_Uni_49[] PROGMEM = {38, 32, 173, 25, 232, 150, 187, 19, 242, 23, 200, 59, 27, 189, 136, 70, 191, 158, 191, 29, 17, 214, 136, 45, 48, 214, 249, 118, 82, 180, 50, 232, 82, 195, 11, 249, 231, 227, 161}; 53 | const byte Proverbs_Uni_50[] PROGMEM = {35, 32, 175, 200, 112, 191, 133, 126, 94, 104, 204, 91, 8, 205, 242, 51, 127, 1, 180, 72, 117, 136, 141, 120, 104, 252, 135, 48, 240, 107, 95, 118, 22, 209, 145, 208}; 54 | const byte Proverbs_Uni_51[] PROGMEM = {44, 32, 175, 200, 71, 225, 239, 216, 123, 126, 92, 47, 131, 180, 103, 175, 159, 47, 96, 87, 227, 162, 58, 209, 13, 223, 24, 207, 208, 190, 126, 92, 204, 223, 208, 120, 53, 172, 230, 174, 149, 170, 209, 172, 232}; 55 | const byte Proverbs_Uni_52[] PROGMEM = {57, 32, 177, 7, 131, 116, 76, 79, 200, 44, 94, 99, 194, 255, 135, 68, 117, 162, 50, 193, 104, 59, 229, 156, 17, 146, 28, 215, 221, 132, 17, 146, 13, 254, 175, 121, 97, 132, 62, 42, 233, 111, 149, 133, 247, 98, 25, 45, 228, 101, 130, 254, 135, 48, 130, 50, 58, 27}; 56 | const byte Proverbs_Uni_53[] PROGMEM = {36, 32, 179, 202, 228, 11, 28, 242, 236, 67, 47, 215, 201, 242, 29, 97, 8, 206, 136, 235, 68, 100, 239, 254, 66, 103, 31, 116, 134, 95, 188, 183, 145, 157, 239, 29, 13}; 57 | const byte Proverbs_Uni_54[] PROGMEM = {40, 32, 179, 200, 54, 22, 121, 123, 57, 13, 176, 179, 200, 60, 239, 51, 91, 254, 120, 232, 142, 180, 70, 183, 97, 96, 191, 25, 0, 99, 137, 14, 177, 16, 120, 47, 188, 194, 11, 62, 116}; 58 | const byte Proverbs_Uni_55[] PROGMEM = {24, 32, 179, 202, 181, 141, 25, 158, 65, 216, 209, 219, 124, 116, 71, 90, 42, 209, 175, 200, 49, 87, 195, 204, 232}; 59 | const byte Proverbs_Uni_56[] PROGMEM = {37, 33, 183, 32, 88, 223, 90, 243, 252, 80, 89, 95, 196, 36, 12, 179, 162, 58, 209, 86, 13, 207, 255, 7, 208, 254, 132, 18, 28, 213, 246, 80, 167, 244, 37, 153, 29, 13}; 60 | const byte Proverbs_Uni_57[] PROGMEM = {42, 46, 222, 30, 144, 46, 185, 26, 243, 248, 58, 196, 196, 102, 215, 133, 129, 67, 7, 68, 117, 162, 236, 66, 133, 240, 98, 135, 88, 102, 139, 205, 249, 10, 223, 44, 31, 228, 40, 88, 209, 135, 67}; 61 | const byte Proverbs_Uni_58[] PROGMEM = {46, 46, 196, 29, 245, 95, 33, 51, 200, 122, 194, 11, 60, 159, 33, 124, 1, 36, 111, 63, 225, 209, 29, 104, 168, 127, 23, 243, 61, 79, 254, 66, 98, 236, 164, 31, 240, 125, 111, 43, 66, 159, 10, 193, 126, 103, 67}; 62 | const byte Proverbs_Uni_59[] PROGMEM = {32, 46, 196, 42, 204, 144, 171, 254, 245, 175, 111, 47, 88, 70, 118, 32, 5, 242, 93, 136, 192, 88, 47, 232, 127, 104, 86, 11, 164, 59, 247, 199, 67}; 63 | const byte Proverbs_Uni_60[] PROGMEM = {68, 46, 196, 51, 62, 134, 139, 12, 86, 33, 182, 39, 197, 143, 95, 206, 165, 99, 111, 152, 109, 13, 144, 40, 124, 169, 141, 25, 251, 236, 58, 29, 17, 214, 139, 177, 13, 186, 167, 245, 166, 0, 51, 234, 127, 223, 52, 239, 225, 25, 33, 204, 59, 23, 230, 182, 144, 152, 252, 187, 41, 120, 95, 2, 6, 60, 53, 252, 232}; 64 | const byte Proverbs_Uni_61[] PROGMEM = {62, 46, 197, 230, 134, 11, 98, 52, 42, 195, 127, 85, 159, 173, 8, 43, 94, 222, 95, 193, 163, 31, 208, 89, 228, 40, 91, 51, 207, 29, 17, 214, 139, 57, 160, 239, 143, 186, 67, 108, 209, 171, 229, 131, 88, 78, 169, 161, 253, 161, 88, 46, 144, 239, 187, 52, 222, 30, 76, 120, 123, 199, 67}; 65 | const byte Proverbs_Uni_62[] PROGMEM = {44, 46, 197, 88, 37, 249, 5, 126, 70, 111, 226, 88, 143, 203, 217, 200, 101, 254, 208, 154, 24, 171, 0, 194, 201, 118, 82, 102, 111, 230, 74, 190, 46, 133, 47, 10, 193, 97, 8, 135, 53, 235, 209, 14, 134}; 66 | const byte Proverbs_Uni_63[] PROGMEM = {44, 1, 7, 19, 9, 13, 206, 166, 159, 150, 126, 64, 169, 30, 190, 195, 162, 58, 209, 80, 166, 135, 197, 96, 77, 10, 208, 130, 246, 136, 214, 249, 118, 82, 15, 12, 223, 216, 139, 121, 120, 102, 134, 206, 134}; 67 | const byte Proverbs_Uni_64[] PROGMEM = {41, 46, 23, 193, 218, 8, 33, 108, 205, 171, 67, 194, 253, 88, 132, 106, 176, 76, 241, 209, 29, 104, 182, 151, 51, 55, 245, 251, 222, 242, 172, 76, 205, 252, 203, 57, 159, 126, 85, 126, 166, 116}; 68 | const byte Proverbs_Uni_65[] PROGMEM = {45, 33, 143, 255, 196, 21, 137, 251, 19, 62, 117, 33, 152, 30, 33, 185, 241, 24, 153, 243, 161, 209, 29, 104, 173, 9, 131, 202, 133, 93, 33, 215, 243, 79, 44, 49, 79, 151, 152, 66, 96, 212, 59, 105, 248, 232}; 69 | const byte Proverbs_Uni_66[] PROGMEM = {37, 42, 193, 51, 200, 44, 242, 14, 198, 239, 103, 144, 65, 51, 59, 229, 104, 191, 131, 108, 232, 142, 180, 86, 16, 75, 249, 98, 121, 4, 236, 69, 88, 139, 248, 53, 157, 13}; 70 | const byte * const Proverbs_Uni[] PROGMEM = {Proverbs_Uni_0, Proverbs_Uni_1, Proverbs_Uni_2, Proverbs_Uni_3, Proverbs_Uni_4, Proverbs_Uni_5, Proverbs_Uni_6, Proverbs_Uni_7, Proverbs_Uni_8, Proverbs_Uni_9, Proverbs_Uni_10, Proverbs_Uni_11, Proverbs_Uni_12, Proverbs_Uni_13, Proverbs_Uni_14, Proverbs_Uni_15, Proverbs_Uni_16, Proverbs_Uni_17, Proverbs_Uni_18, Proverbs_Uni_19, Proverbs_Uni_20, Proverbs_Uni_21, Proverbs_Uni_22, Proverbs_Uni_23, Proverbs_Uni_24, Proverbs_Uni_25, Proverbs_Uni_26, Proverbs_Uni_27, Proverbs_Uni_28, Proverbs_Uni_29, Proverbs_Uni_30, Proverbs_Uni_31, Proverbs_Uni_32, Proverbs_Uni_33, Proverbs_Uni_34, Proverbs_Uni_35, Proverbs_Uni_36, Proverbs_Uni_37, Proverbs_Uni_38, Proverbs_Uni_39, Proverbs_Uni_40, Proverbs_Uni_41, Proverbs_Uni_42, Proverbs_Uni_43, Proverbs_Uni_44, Proverbs_Uni_45, Proverbs_Uni_46, Proverbs_Uni_47, Proverbs_Uni_48, Proverbs_Uni_49, Proverbs_Uni_50, Proverbs_Uni_51, Proverbs_Uni_52, Proverbs_Uni_53, Proverbs_Uni_54, Proverbs_Uni_55, Proverbs_Uni_56, Proverbs_Uni_57, Proverbs_Uni_58, Proverbs_Uni_59, Proverbs_Uni_60, Proverbs_Uni_61, Proverbs_Uni_62, Proverbs_Uni_63, Proverbs_Uni_64, Proverbs_Uni_65, Proverbs_Uni_66}; 71 | #define Proverbs_Uni_line_count 67 72 | #define Proverbs_Uni_max_len 127 73 | #endif 74 | -------------------------------------------------------------------------------- /library.properties: -------------------------------------------------------------------------------- 1 | name=Unishox Progmem Decompressor 2 | version=1.0 3 | author=Arundale Ramanathan 4 | maintainer=Arun 5 | sentence=Decompress short Unicode strings and messages compressed using Unishox technique and save space on Flash (PROGMEM) 6 | paragraph=This library allows decompressing UTF-8 strings from Arduino Program Memory and provides API to retrieve each string individually using index. It is based on Unishox hybrid encoding technique. 7 | category=Data Storage 8 | url=https://github.com/siara-cc/Unishox_Arduino_Progmem_lib 9 | architectures=* 10 | -------------------------------------------------------------------------------- /src/unishox1_progmem.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Siara Logics (cc) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * @author Arundale R. 17 | * 18 | */ 19 | #include 20 | #include "unishox1_progmem.h" 21 | 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include 27 | #include 28 | 29 | typedef unsigned char byte; 30 | 31 | #define NICE_LEN 5 32 | 33 | const byte uni_bit_len[5] = {6, 12, 14, 16, 21}; 34 | const int32_t uni_adder[5] = {0, 64, 4160, 20544, 86080}; 35 | 36 | // Decoder is designed for using less memory, not speed 37 | // Decode lookup table for code index and length 38 | // First 2 bits 00, Next 3 bits indicate index of code from 0, 39 | // last 3 bits indicate code length in bits 40 | // 0, 1, 2, 3, 4, 41 | char vcode1_pgm[32] = {2 + (0 << 3), 3 + (3 << 3), 3 + (1 << 3), 4 + (6 << 3), 0, 42 | // 5, 6, 7, 8, 9, 10 43 | 4 + (4 << 3), 3 + (2 << 3), 4 + (8 << 3), 0, 0, 0, 44 | // 11, 12, 13, 14, 15 45 | 4 + (7 << 3), 0, 4 + (5 << 3), 0, 5 + (9 << 3), 46 | // 16, 17, 18, 19, 20, 21, 22, 23 47 | 0, 0, 0, 0, 0, 0, 0, 0, 48 | // 24, 25, 26, 27, 28, 29, 30, 31 49 | 0, 0, 0, 0, 0, 0, 0, 5 + (10 << 3)}; 50 | // 0, 1, 2, 3, 4, 5, 6, 7, 51 | char hcode1_pgm[32] = {1 + (1 << 3), 2 + (0 << 3), 0, 3 + (2 << 3), 0, 0, 0, 5 + (3 << 3), 52 | // 8, 9, 10, 11, 12, 13, 14, 15, 53 | 0, 0, 0, 0, 0, 0, 0, 5 + (5 << 3), 54 | // 16, 17, 18, 19, 20, 21, 22, 23 55 | 0, 0, 0, 0, 0, 0, 0, 5 + (4 << 3), 56 | // 24, 25, 26, 27, 28, 29, 30, 31 57 | 0, 0, 0, 0, 0, 0, 0, 5 + (6 << 3)}; 58 | 59 | enum {SHX_SET1 = 0, SHX_SET1A, SHX_SET1B, SHX_SET2, SHX_SET3, SHX_SET4, SHX_SET4A}; 60 | char sets1_pgm[][11] = {{ 0, ' ', 'e', 0, 't', 'a', 'o', 'i', 'n', 's', 'r'}, 61 | { 0, 'l', 'c', 'd', 'h', 'u', 'p', 'm', 'b', 'g', 'w'}, 62 | {'f', 'y', 'v', 'k', 'q', 'j', 'x', 'z', 0, 0, 0}, 63 | { 0, '9', '0', '1', '2', '3', '4', '5', '6', '7', '8'}, 64 | {'.', ',', '-', '/', '=', '+', ' ', '(', ')', '$', '%'}, 65 | {'&', ';', ':', '<', '>', '*', '"', '{', '}', '[', ']'}, 66 | {'@', '?', '\'', '^', '#', '_', '!', '\\', '|', '~', '`'}}; 67 | 68 | int getBitVal(const byte *in, int bit_no, int count) { 69 | return (pgm_read_byte(in + (bit_no >> 3)) & (0x80 >> (bit_no % 8)) ? 1 << count : 0); 70 | } 71 | 72 | int getCodeIdx(char *code_type, const byte *in, int len, int *bit_no_p) { 73 | int code = 0; 74 | int count = 0; 75 | do { 76 | if (*bit_no_p >= len) 77 | return 199; 78 | code += getBitVal(in, *bit_no_p, count); 79 | (*bit_no_p)++; 80 | count++; 81 | if (code_type[code] && 82 | (code_type[code] & 0x07) == count) { 83 | return code_type[code] >> 3; 84 | } 85 | } while (count < 5); 86 | return 1; // skip if code not found 87 | } 88 | 89 | int32_t getNumFromBits(const byte *in, int bit_no, int count) { 90 | int32_t ret = 0; 91 | while (count--) { 92 | ret += getBitVal(in, bit_no, count); 93 | bit_no++; 94 | } 95 | return ret; 96 | } 97 | 98 | int readCount(const byte *in, int *bit_no_p, int len) { 99 | const byte bit_len[7] = {5, 2, 7, 9, 12, 16, 17}; 100 | const uint16_t adder[7] = {4, 0, 36, 164, 676, 4772, 0}; 101 | int idx = getCodeIdx(hcode1_pgm, in, len, bit_no_p); 102 | if (idx > 6) 103 | return 0; 104 | int count = getNumFromBits(in, *bit_no_p, bit_len[idx]) + adder[idx]; 105 | (*bit_no_p) += bit_len[idx]; 106 | return count; 107 | } 108 | 109 | int32_t readUnicode(const byte *in, int *bit_no_p, int len) { 110 | int code = 0; 111 | for (int i = 0; i < 5; i++) { 112 | code += getBitVal(in, *bit_no_p, i); 113 | (*bit_no_p)++; 114 | //int idx = (code == 0 && i == 1 ? 0 : (code == 2 && i == 1 ? 1 : 115 | // (code == 1 && i == 2 ? 2 : (code == 5 && i == 2 ? 3 : 116 | // (code == 3 && i == 2 ? 4 : (code == 7 && i == 3 ? 5 : 117 | // (code == 15 && i == 4 ? 6 : 118 | // (code == 31 && i == 4 ? 7 : -1)))))))); 119 | int idx = (code == 0 && i == 0 ? 0 : (code == 1 && i == 1 ? 1 : 120 | (code == 3 && i == 2 ? 2 : (code == 7 && i == 3 ? 3 : 121 | (code == 15 && i == 4 ? 4 : 122 | (code == 31 && i == 4 ? 5 : -1)))))); 123 | //printf("%d\n", code); 124 | //if (idx == 0) 125 | // return 0; 126 | if (idx == 5) { 127 | int idx = getCodeIdx(hcode1_pgm, in, len, bit_no_p); 128 | return 0x7FFFFF00 + idx; 129 | } 130 | if (idx >= 0) { 131 | int sign = getBitVal(in, *bit_no_p, 1); 132 | (*bit_no_p)++; 133 | int32_t count = getNumFromBits(in, *bit_no_p, uni_bit_len[idx]); 134 | count += uni_adder[idx]; 135 | (*bit_no_p) += uni_bit_len[idx]; 136 | return sign ? -count : count; 137 | } 138 | } 139 | return 0; 140 | } 141 | 142 | void writeUTF8(char *out, int *ol, int32_t uni) { 143 | int32_t limit11 = 1; 144 | int32_t limit16 = 1; 145 | limit11 <<= 11; 146 | limit16 <<= 16; 147 | if (uni < limit11) { 148 | out[(*ol)++] = (0xC0 + (uni >> 6)); 149 | out[(*ol)++] = (0x80 + (uni & 63)); 150 | } else 151 | if (uni < limit16) { 152 | out[(*ol)++] = (0xE0 + (uni >> 12)); 153 | out[(*ol)++] = (0x80 + ((uni >> 6) & 63)); 154 | out[(*ol)++] = (0x80 + (uni & 63)); 155 | } else { 156 | out[(*ol)++] = (0xF0 + (uni >> 18)); 157 | out[(*ol)++] = (0x80 + ((uni >> 12) & 63)); 158 | out[(*ol)++] = (0x80 + ((uni >> 6) & 63)); 159 | out[(*ol)++] = (0x80 + (uni & 63)); 160 | } 161 | //Serial.print("ol:"); 162 | //Serial.print(uni); 163 | //Serial.println(*ol); 164 | } 165 | 166 | int decodeRepeat(const byte *in, const byte * const in_list[], int idx, int len, char *out, int ol, int *bit_no) { 167 | int dict_len = readCount(in, bit_no, len) + NICE_LEN; 168 | int dist = readCount(in, bit_no, len); 169 | int ctx = readCount(in, bit_no, len); 170 | //Serial.print("OL:"); 171 | //Serial.print(ol); 172 | //Serial.print("Dict Len:"); 173 | //Serial.print(dict_len); 174 | //Serial.print(":Dist:"); 175 | //Serial.print(dist); 176 | //Serial.print(":Ctx:"); 177 | //Serial.println(ctx); 178 | if (ctx > 0) { 179 | unishox1_pgm_decompress(in_list, idx - ctx, out + ol + dict_len, dist + dict_len + 1); 180 | memcpy(out + ol, out + ol + dict_len + dist, dict_len); 181 | } else { 182 | memcpy(out + ol, out + dist, dict_len); 183 | } 184 | ol += dict_len; 185 | return ol; 186 | } 187 | 188 | int unishox1_pgm_decompress(const byte * const in_list[], int idx, char *out, int end_len) { 189 | 190 | int dstate; 191 | int bit_no; 192 | byte is_all_upper; 193 | 194 | int ol = 0; 195 | bit_no = 0; 196 | dstate = SHX_SET1; 197 | is_all_upper = 0; 198 | 199 | byte *in; 200 | if (sizeof(byte *) == 2) 201 | in = (byte *) pgm_read_word(&(in_list[idx])); 202 | else 203 | in = (byte *) pgm_read_dword(&(in_list[idx])); 204 | int len = pgm_read_byte(&(in[0])); 205 | in++; 206 | 207 | int32_t prev_uni = 0; 208 | 209 | len <<= 3; 210 | out[ol] = 0; 211 | while (bit_no < len) { 212 | if (end_len && ol >= end_len) 213 | return ol; 214 | int h, v; 215 | char c = 0; 216 | byte is_upper = is_all_upper; 217 | int orig_bit_no = bit_no; 218 | v = getCodeIdx(vcode1_pgm, in, len, &bit_no); 219 | if (v == 199) { 220 | bit_no = orig_bit_no; 221 | break; 222 | } 223 | h = dstate; 224 | if (v == 0) { 225 | h = getCodeIdx(hcode1_pgm, in, len, &bit_no); 226 | if (h == 199) { 227 | bit_no = orig_bit_no; 228 | break; 229 | } 230 | if (h == SHX_SET1) { 231 | if (dstate == SHX_SET1) { 232 | if (is_all_upper) { 233 | is_upper = is_all_upper = 0; 234 | continue; 235 | } 236 | v = getCodeIdx(vcode1_pgm, in, len, &bit_no); 237 | if (v == 199) { 238 | bit_no = orig_bit_no; 239 | break; 240 | } 241 | if (v == 0) { 242 | h = getCodeIdx(hcode1_pgm, in, len, &bit_no); 243 | if (h == 199) { 244 | bit_no = orig_bit_no; 245 | break; 246 | } 247 | if (h == SHX_SET1) { 248 | is_all_upper = 1; 249 | continue; 250 | } 251 | } 252 | is_upper = 1; 253 | } else { 254 | dstate = SHX_SET1; 255 | continue; 256 | } 257 | } else 258 | if (h == SHX_SET2) { 259 | if (dstate == SHX_SET1) 260 | dstate = SHX_SET2; 261 | continue; 262 | } 263 | if (h != SHX_SET1) { 264 | v = getCodeIdx(vcode1_pgm, in, len, &bit_no); 265 | if (v == 199) { 266 | bit_no = orig_bit_no; 267 | break; 268 | } 269 | } 270 | } 271 | if (v == 0 && h == SHX_SET1A) { 272 | if (is_upper) { 273 | out[ol++] = readCount(in, &bit_no, len); 274 | } else { 275 | ol = decodeRepeat(in, in_list, idx, len, out, ol, &bit_no); 276 | } 277 | continue; 278 | } 279 | if (h == SHX_SET1 && v == 3) { 280 | do { 281 | int32_t delta = readUnicode(in, &bit_no, len); 282 | if ((delta >> 8) == 0x7FFFFF) { 283 | int spl_code_idx = delta & 0x000000FF; 284 | if (spl_code_idx == 2) 285 | break; 286 | switch (spl_code_idx) { 287 | case 1: 288 | out[ol++] = ' '; 289 | break; 290 | case 0: 291 | ol = decodeRepeat(in, in_list, idx, len, out, ol, &bit_no); 292 | break; 293 | case 3: 294 | out[ol++] = ','; 295 | break; 296 | case 4: 297 | if (prev_uni > 0x3000) 298 | writeUTF8(out, &ol, 0x3002); 299 | else 300 | out[ol++] = '.'; 301 | break; 302 | case 5: 303 | out[ol++] = 13; 304 | break; 305 | case 6: 306 | out[ol++] = 10; 307 | } 308 | } else { 309 | prev_uni += delta; 310 | writeUTF8(out, &ol, prev_uni); 311 | } 312 | } while (is_upper); 313 | //printf("Sign: %d, bitno: %d\n", sign, bit_no); 314 | //printf("Code: %d\n", prev_uni); 315 | //printf("BitNo: %d\n", bit_no); 316 | continue; 317 | } 318 | if (h < 64 && v < 32) 319 | c = sets1_pgm[h][v]; 320 | if (c >= 'a' && c <= 'z') { 321 | if (is_upper) 322 | c -= 32; 323 | } else { 324 | if (is_upper && dstate == SHX_SET1 && v == 1) 325 | c = '\t'; 326 | if (h == SHX_SET1B) { 327 | switch (v) { 328 | case 9: 329 | out[ol++] = '\r'; 330 | out[ol++] = '\n'; 331 | continue; 332 | case 8: 333 | if (is_upper) { // rpt 334 | int count = readCount(in, &bit_no, len); 335 | count += 4; 336 | char rpt_c = out[ol - 1]; 337 | while (count--) 338 | out[ol++] = rpt_c; 339 | } else { 340 | out[ol++] = '\n'; 341 | } 342 | continue; 343 | case 10: 344 | continue; 345 | } 346 | } 347 | } 348 | out[ol++] = c; 349 | } 350 | 351 | return ol; 352 | 353 | } 354 | -------------------------------------------------------------------------------- /src/unishox1_progmem.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2019 Siara Logics (cc) 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | * @author Arundale R. 17 | * 18 | */ 19 | #ifndef __UNISHOX_1_PGM_COMPRESS__ 20 | #define __UNISHOX_1_PGM_COMPRESS__ 21 | 22 | typedef unsigned char byte; 23 | 24 | int unishox1_pgm_decompress(const byte * const in_list[], int idx, char *out, int end_len); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /ss_proverbs.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/siara-cc/Unishox_Arduino_Progmem_lib/cd19d33dab81b15c2d10178b7ee62370683ab9c4/ss_proverbs.png --------------------------------------------------------------------------------