:: commit fe996e05e1469f5bdc9794e1a6e6c19474a447e8

Kamila Szewczyk <kspalaiologos@gmail.com> — 2023-03-19 13:53

parents: 414b0f0315

reformat the code, BE CRC32 table

diff --git a/Makefile.am b/Makefile.am
index 5dde405..c2bed82 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -59,11 +59,11 @@ dist-hook:
 # Begin developer convenience targets
 
 .PHONY: format
-format: $(bzip3_SOURCES) $(include_HEADERS) $(noinst_HEADERS)
+format: $(bzip3_SOURCES) $(libbzip3_la_SOURCES) $(include_HEADERS) $(noinst_HEADERS)
 	clang-format -i $^ examples/*.c
 
 .PHONY: cloc
-cloc: $(bzip3_SOURCES) $(include_HEADERS) $(noinst_HEADERS)
+cloc: $(bzip3_SOURCES) $(libbzip3_la_SOURCES) $(include_HEADERS) $(noinst_HEADERS)
 	cloc $^
 
 CLEANFILES += LICENSE2
diff --git a/examples/fuzz.c b/examples/fuzz.c
index e33464a..84eadf6 100644
--- a/examples/fuzz.c
+++ b/examples/fuzz.c
@@ -28,7 +28,7 @@ int main(int argc, char ** argv) {
     fread(buffer, 1, size, fp);
     fclose(fp);
 
-    if(size < 64) {
+    if (size < 64) {
         // Too small.
         return 0;
     }
diff --git a/include/libsais.h b/include/libsais.h
index 47f5373..6cdad57 100644
--- a/include/libsais.h
+++ b/include/libsais.h
@@ -22,7 +22,7 @@ Please see the file LICENSE for full copyright information.
 The stability patches that fix undefined behaviour in unbwt routines:
 
    Copyright (c) 2022 Kamila Szewczyk <kspalaiologos@gmail.com>
-   
+
    Licensed under the same license as the original software.
 
 --*/
diff --git a/src/libbz3.c b/src/libbz3.c
index 313dfe6..1e5830d 100644
--- a/src/libbz3.c
+++ b/src/libbz3.c
@@ -60,14 +60,46 @@ static const u32 crc32Table[256] = {
     0xBE2DA0A5L, 0x4C4623A6L, 0x5F16D052L, 0xAD7D5351L
 };
 
+static const u32 crc32TableBig[256] = {
+    0x00000000L, 0x03836BF2L, 0xF7703BE1L, 0xF4F35013L, 0x1F979AC7L, 0x1C14F135L, 0xE8E7A126L, 0xEB64CAD4L, 0xCF58D98AL,
+    0xCCDBB278L, 0x3828E26BL, 0x3BAB8999L, 0xD0CF434DL, 0xD34C28BFL, 0x27BF78ACL, 0x243C135EL, 0x6FC75E10L, 0x6C4435E2L,
+    0x98B765F1L, 0x9B340E03L, 0x7050C4D7L, 0x73D3AF25L, 0x8720FF36L, 0x84A394C4L, 0xA09F879AL, 0xA31CEC68L, 0x57EFBC7BL,
+    0x546CD789L, 0xBF081D5DL, 0xBC8B76AFL, 0x487826BCL, 0x4BFB4D4EL, 0xDE8EBD20L, 0xDD0DD6D2L, 0x29FE86C1L, 0x2A7DED33L,
+    0xC11927E7L, 0xC29A4C15L, 0x36691C06L, 0x35EA77F4L, 0x11D664AAL, 0x12550F58L, 0xE6A65F4BL, 0xE52534B9L, 0x0E41FE6DL,
+    0x0DC2959FL, 0xF931C58CL, 0xFAB2AE7EL, 0xB149E330L, 0xB2CA88C2L, 0x4639D8D1L, 0x45BAB323L, 0xAEDE79F7L, 0xAD5D1205L,
+    0x59AE4216L, 0x5A2D29E4L, 0x7E113ABAL, 0x7D925148L, 0x8961015BL, 0x8AE26AA9L, 0x6186A07DL, 0x6205CB8FL, 0x96F69B9CL,
+    0x9575F06EL, 0xBC1D7B41L, 0xBF9E10B3L, 0x4B6D40A0L, 0x48EE2B52L, 0xA38AE186L, 0xA0098A74L, 0x54FADA67L, 0x5779B195L,
+    0x7345A2CBL, 0x70C6C939L, 0x8435992AL, 0x87B6F2D8L, 0x6CD2380CL, 0x6F5153FEL, 0x9BA203EDL, 0x9821681FL, 0xD3DA2551L,
+    0xD0594EA3L, 0x24AA1EB0L, 0x27297542L, 0xCC4DBF96L, 0xCFCED464L, 0x3B3D8477L, 0x38BEEF85L, 0x1C82FCDBL, 0x1F019729L,
+    0xEBF2C73AL, 0xE871ACC8L, 0x0315661CL, 0x00960DEEL, 0xF4655DFDL, 0xF7E6360FL, 0x6293C661L, 0x6110AD93L, 0x95E3FD80L,
+    0x96609672L, 0x7D045CA6L, 0x7E873754L, 0x8A746747L, 0x89F70CB5L, 0xADCB1FEBL, 0xAE487419L, 0x5ABB240AL, 0x59384FF8L,
+    0xB25C852CL, 0xB1DFEEDEL, 0x452CBECDL, 0x46AFD53FL, 0x0D549871L, 0x0ED7F383L, 0xFA24A390L, 0xF9A7C862L, 0x12C302B6L,
+    0x11406944L, 0xE5B33957L, 0xE63052A5L, 0xC20C41FBL, 0xC18F2A09L, 0x357C7A1AL, 0x36FF11E8L, 0xDD9BDB3CL, 0xDE18B0CEL,
+    0x2AEBE0DDL, 0x29688B2FL, 0x783BF682L, 0x7BB89D70L, 0x8F4BCD63L, 0x8CC8A691L, 0x67AC6C45L, 0x642F07B7L, 0x90DC57A4L,
+    0x935F3C56L, 0xB7632F08L, 0xB4E044FAL, 0x401314E9L, 0x43907F1BL, 0xA8F4B5CFL, 0xAB77DE3DL, 0x5F848E2EL, 0x5C07E5DCL,
+    0x17FCA892L, 0x147FC360L, 0xE08C9373L, 0xE30FF881L, 0x086B3255L, 0x0BE859A7L, 0xFF1B09B4L, 0xFC986246L, 0xD8A47118L,
+    0xDB271AEAL, 0x2FD44AF9L, 0x2C57210BL, 0xC733EBDFL, 0xC4B0802DL, 0x3043D03EL, 0x33C0BBCCL, 0xA6B54BA2L, 0xA5362050L,
+    0x51C57043L, 0x52461BB1L, 0xB922D165L, 0xBAA1BA97L, 0x4E52EA84L, 0x4DD18176L, 0x69ED9228L, 0x6A6EF9DAL, 0x9E9DA9C9L,
+    0x9D1EC23BL, 0x767A08EFL, 0x75F9631DL, 0x810A330EL, 0x828958FCL, 0xC97215B2L, 0xCAF17E40L, 0x3E022E53L, 0x3D8145A1L,
+    0xD6E58F75L, 0xD566E487L, 0x2195B494L, 0x2216DF66L, 0x062ACC38L, 0x05A9A7CAL, 0xF15AF7D9L, 0xF2D99C2BL, 0x19BD56FFL,
+    0x1A3E3D0DL, 0xEECD6D1EL, 0xED4E06ECL, 0xC4268DC3L, 0xC7A5E631L, 0x3356B622L, 0x30D5DDD0L, 0xDBB11704L, 0xD8327CF6L,
+    0x2CC12CE5L, 0x2F424717L, 0x0B7E5449L, 0x08FD3FBBL, 0xFC0E6FA8L, 0xFF8D045AL, 0x14E9CE8EL, 0x176AA57CL, 0xE399F56FL,
+    0xE01A9E9DL, 0xABE1D3D3L, 0xA862B821L, 0x5C91E832L, 0x5F1283C0L, 0xB4764914L, 0xB7F522E6L, 0x430672F5L, 0x40851907L,
+    0x64B90A59L, 0x673A61ABL, 0x93C931B8L, 0x904A5A4AL, 0x7B2E909EL, 0x78ADFB6CL, 0x8C5EAB7FL, 0x8FDDC08DL, 0x1AA830E3L,
+    0x192B5B11L, 0xEDD80B02L, 0xEE5B60F0L, 0x053FAA24L, 0x06BCC1D6L, 0xF24F91C5L, 0xF1CCFA37L, 0xD5F0E969L, 0xD673829BL,
+    0x2280D288L, 0x2103B97AL, 0xCA6773AEL, 0xC9E4185CL, 0x3D17484FL, 0x3E9423BDL, 0x756F6EF3L, 0x76EC0501L, 0x821F5512L,
+    0x819C3EE0L, 0x6AF8F434L, 0x697B9FC6L, 0x9D88CFD5L, 0x9E0BA427L, 0xBA37B779L, 0xB9B4DC8BL, 0x4D478C98L, 0x4EC4E76AL,
+    0xA5A02DBEL, 0xA623464CL, 0x52D0165FL, 0x51537DADL
+}
+
 static u32 crc32sum(u32 crc, u8 * RESTRICT buf, size_t size) {
     // Test endianness. The code needs to be different for LE and BE systems.
     u32 test = 1;
-    if (*(u8 *) &test) {
+    if (*(u8 *)&test) {
         while (size--) crc = crc32Table[(crc ^ *(buf++)) & 0xff] ^ (crc >> 8);
         return crc;
     } else {
-        while (size--) crc = crc32Table[((crc >> 24) ^ *(buf++)) & 0xff] ^ (crc << 8);
+        while (size--) crc = crc32TableBig[((crc >> 24) ^ *(buf++)) & 0xff] ^ (crc << 8);
         return crc;
     }
 }
diff --git a/src/main.c b/src/main.c
index f993b13..cd438a4 100644
--- a/src/main.c
+++ b/src/main.c
@@ -141,7 +141,8 @@ static void close_out_file(FILE * des) {
     }
 }
 
-static int process(FILE * input_des, FILE * output_des, int mode, int block_size, int workers, int verbose, char * file_name) {
+static int process(FILE * input_des, FILE * output_des, int mode, int block_size, int workers, int verbose,
+                   char * file_name) {
     uint64_t bytes_read = 0, bytes_written = 0;
 
     if ((mode == MODE_ENCODE && isatty(fileno(output_des))) ||
@@ -217,8 +218,7 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
                 read_count = xread(buffer, 1, block_size, input_des);
                 bytes_read += read_count;
 
-                if(read_count == 0)
-                    break;
+                if (read_count == 0) break;
 
                 s32 new_size = bz3_encode_block(state, buffer, read_count);
                 if (new_size == -1) {
@@ -242,7 +242,7 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
                 new_size = read_neutral_s32(byteswap_buf);
                 xread_noeof(&byteswap_buf, 1, 4, input_des);
                 old_size = read_neutral_s32(byteswap_buf);
-                if(old_size > block_size + 31) {
+                if (old_size > block_size + 31) {
                     fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
                     return 1;
                 }
@@ -263,7 +263,7 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
                 new_size = read_neutral_s32(byteswap_buf);
                 xread_noeof(&byteswap_buf, 1, 4, input_des);
                 old_size = read_neutral_s32(byteswap_buf);
-                if(old_size > block_size + 31) {
+                if (old_size > block_size + 31) {
                     fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
                     return 1;
                 }
@@ -340,7 +340,7 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
                     sizes[i] = read_neutral_s32(byteswap_buf);
                     xread_noeof(&byteswap_buf, 1, 4, input_des);
                     old_sizes[i] = read_neutral_s32(byteswap_buf);
-                    if(old_sizes[i] > block_size + 31) {
+                    if (old_sizes[i] > block_size + 31) {
                         fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
                         return 1;
                     }
@@ -368,7 +368,7 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
                     sizes[i] = read_neutral_s32(byteswap_buf);
                     xread_noeof(&byteswap_buf, 1, 4, input_des);
                     old_sizes[i] = read_neutral_s32(byteswap_buf);
-                    if(old_sizes[i] > block_size + 31) {
+                    if (old_sizes[i] > block_size + 31) {
                         fprintf(stderr, "Failed to decode a block: Inconsistent headers.\n");
                         return 1;
                     }
@@ -392,14 +392,16 @@ static int process(FILE * input_des, FILE * output_des, int mode, int block_size
     }
 #endif
 
-    if(verbose) {
-        if(file_name) fprintf(stderr, " %s:", file_name);
-        if(mode == MODE_ENCODE)
-            fprintf(stderr, "\t%"PRIu64" -> %"PRIu64" bytes, %.2f%%, %.2f bpb\n", bytes_read, bytes_written, (double)bytes_written * 100.0 / bytes_read, (double)bytes_written * 8.0 / bytes_read);
-        else if(mode == MODE_DECODE)
-            fprintf(stderr, "\t%"PRIu64" -> %"PRIu64" bytes, %.2f%%, %.2f bpb\n", bytes_read, bytes_written, (double)bytes_read * 100.0 / bytes_written, (double)bytes_read * 8.0 / bytes_written);
+    if (verbose) {
+        if (file_name) fprintf(stderr, " %s:", file_name);
+        if (mode == MODE_ENCODE)
+            fprintf(stderr, "\t%" PRIu64 " -> %" PRIu64 " bytes, %.2f%%, %.2f bpb\n", bytes_read, bytes_written,
+                    (double)bytes_written * 100.0 / bytes_read, (double)bytes_written * 8.0 / bytes_read);
+        else if (mode == MODE_DECODE)
+            fprintf(stderr, "\t%" PRIu64 " -> %" PRIu64 " bytes, %.2f%%, %.2f bpb\n", bytes_read, bytes_written,
+                    (double)bytes_read * 100.0 / bytes_written, (double)bytes_read * 8.0 / bytes_written);
         else
-            fprintf(stderr, "OK, %"PRIu64" bytes read.\n", bytes_read);
+            fprintf(stderr, "OK, %" PRIu64 " bytes read.\n", bytes_read);
     }
 
     return 0;
@@ -511,7 +513,8 @@ int main(int argc, char * argv[]) {
             case '?':
                 fprintf(stderr, "Try 'bzip3 --help' for more information.\n");
                 return 1;
-            case 'e': case 'z':
+            case 'e':
+            case 'z':
                 mode = MODE_ENCODE;
                 break;
             case 'd':
@@ -707,8 +710,7 @@ int main(int argc, char * argv[]) {
     output_des = mode != MODE_TEST ? open_output(output, force) : NULL;
     input_des = open_input(input);
 
-    if(output != f2)
-        free(output);
+    if (output != f2) free(output);
 
     int r = process(input_des, output_des, mode, block_size, workers, verbose, input);
 
tab: 248 wrap: offon