:: commit 99deeee22cff5bf5580153207447cdd2fd35f4b5

Kamila Szewczyk <kspalaiologos@gmail.com> — 2022-05-06 15:55

parents: 9052ea0fbb

autoformat

diff --git a/include/cm.h b/include/cm.h
index e2bcec0..3a6fdb3 100644
--- a/include/cm.h
+++ b/include/cm.h
@@ -12,7 +12,7 @@ typedef struct {
     s32 c1, c2, run;
     u8 *in_queue, *out_queue;
     s32 input_ptr, output_ptr, input_max;
-    
+
     u16 C0[256], C1[256][256], C2[2][256][17];
 } state;
 
diff --git a/include/common.h b/include/common.h
index 1773fe3..9e77b48 100644
--- a/include/common.h
+++ b/include/common.h
@@ -45,19 +45,16 @@ typedef int32_t s32;
         #include <string.h>
     #endif
 
-    static u32 ntohl(u32 value) {
-        #ifdef WORDS_BIGENDIAN
-            return value;
-        #else
-            u8 data[4];
-            memcpy(&data, &value, sizeof(data));
-
-            return ((u32) data[3])
-                 | ((u32) data[2] << 8)
-                 | ((u32) data[1] << 16)
-                 | ((u32) data[0] << 24);
-        #endif
-    }
+static u32 ntohl(u32 value) {
+    #ifdef WORDS_BIGENDIAN
+    return value;
+    #else
+    u8 data[4];
+    memcpy(&data, &value, sizeof(data));
+
+    return ((u32)data[3]) | ((u32)data[2] << 8) | ((u32)data[1] << 16) | ((u32)data[0] << 24);
+    #endif
+}
 
     #define htonl ntohl
 #endif
diff --git a/src/libbz3.c b/src/libbz3.c
index 330137b..267cd0d 100644
--- a/src/libbz3.c
+++ b/src/libbz3.c
@@ -35,9 +35,9 @@
 #define LZP_MIN_MATCH 40
 
 struct bz3_state {
-    u8 *swap_buffer;
+    u8 * swap_buffer;
     s32 block_size;
-    s32 * sais_array, * lzp_lut;
+    s32 *sais_array, *lzp_lut;
     struct srt_state * srt_state;
     state * cm_state;
     s8 last_error;
@@ -81,7 +81,8 @@ struct bz3_state * bz3_new(s32 block_size) {
 
     bz3_state->lzp_lut = calloc(1 << LZP_DICTIONARY, sizeof(s32));
 
-    if (!bz3_state->cm_state || !bz3_state->srt_state || !bz3_state->swap_buffer || !bz3_state->sais_array || !bz3_state->lzp_lut) {
+    if (!bz3_state->cm_state || !bz3_state->srt_state || !bz3_state->swap_buffer || !bz3_state->sais_array ||
+        !bz3_state->lzp_lut) {
         return NULL;
     }
 
@@ -101,22 +102,27 @@ void bz3_free(struct bz3_state * state) {
     free(state);
 }
 
-#define swap(x, y) { u8 * tmp = x; x = y; y = tmp; }
+#define swap(x, y)    \
+    {                 \
+        u8 * tmp = x; \
+        x = y;        \
+        y = tmp;      \
+    }
 
 s32 bz3_encode_block(struct bz3_state * state, u8 * buffer, s32 data_size) {
-    u8 * b1 = buffer, * b2 = state->swap_buffer;
+    u8 *b1 = buffer, *b2 = state->swap_buffer;
 
-    if(data_size > state->block_size) {
+    if (data_size > state->block_size) {
         state->last_error = BZ3_ERR_DATA_TOO_BIG;
         return -1;
     }
-    
+
     u32 crc32 = crc32sum(1, b1, data_size);
 
     // Ignore small blocks. They won't benefit from the entropy coding step.
-    if(data_size < 64) {
-        ((u32 *) (b1))[0] = htonl(crc32);
-        ((s32 *) (b1))[1] = htonl(-1);
+    if (data_size < 64) {
+        ((u32 *)(b1))[0] = htonl(crc32);
+        ((s32 *)(b1))[1] = htonl(-1);
         memmove(b1 + 8, b1, data_size);
         return data_size + 8;
     }
@@ -128,66 +134,66 @@ s32 bz3_encode_block(struct bz3_state * state, u8 * buffer, s32 data_size) {
     s32 lzp_size, rle_size;
 
     rle_size = mrlec(b1, data_size, b2);
-    if(rle_size < data_size) {
+    if (rle_size < data_size) {
         swap(b1, b2);
         data_size = rle_size;
         model |= 4;
     }
 
     lzp_size = lzp_compress(b1, b2, data_size, LZP_DICTIONARY, LZP_MIN_MATCH, state->lzp_lut);
-    if(lzp_size > 0) {
+    if (lzp_size > 0) {
         swap(b1, b2);
         data_size = lzp_size;
         model |= 2;
     }
 
     s32 bwt_idx = libsais_bwt(b1, b2, state->sais_array, data_size, 0, NULL);
-    if(bwt_idx < 0) {
+    if (bwt_idx < 0) {
         state->last_error = BZ3_ERR_BWT;
         return -1;
     }
 
     // Compute the amount of overhead dwords.
-    s32 overhead = 2; // CRC32 + BWT index
-    if(model & 2) overhead++; // LZP
-    if(model & 4) overhead++; // RLE
+    s32 overhead = 2;           // CRC32 + BWT index
+    if (model & 2) overhead++;  // LZP
+    if (model & 4) overhead++;  // RLE
 
     begin(state->cm_state);
     state->cm_state->out_queue = b1 + overhead * 4 + 1;
     state->cm_state->output_ptr = 0;
+#pragma GCC unroll 16
     for (s32 i = 0; i < data_size; i++) encode_byte(state->cm_state, b2[i]);
     flush(state->cm_state);
     data_size = state->cm_state->output_ptr;
 
     // Write the header. Starting with common entries.
-    ((u32 *) (b1))[0] = htonl(crc32);
-    ((s32 *) (b1))[1] = htonl(bwt_idx);
+    ((u32 *)(b1))[0] = htonl(crc32);
+    ((s32 *)(b1))[1] = htonl(bwt_idx);
     b1[8] = model;
 
     s32 p = 0;
-    if(model & 2) ((s32 *)(b1 + 9))[p++] = htonl(lzp_size);
-    if(model & 4) ((s32 *)(b1 + 9))[p++] = htonl(rle_size);
+    if (model & 2) ((s32 *)(b1 + 9))[p++] = htonl(lzp_size);
+    if (model & 4) ((s32 *)(b1 + 9))[p++] = htonl(rle_size);
 
     state->last_error = BZ3_OK;
 
     // XXX: Better solution
-    if(b1 != buffer)
-        memcpy(buffer, b1, data_size + overhead * 4 + 1);
+    if (b1 != buffer) memcpy(buffer, b1, data_size + overhead * 4 + 1);
 
     return data_size + overhead * 4 + 1;
 }
 
 s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_size, s32 orig_size) {
     // Read the header.
-    u32 crc32 = ntohl(((u32 *) buffer)[0]);
-    s32 bwt_idx = ntohl(((s32 *) buffer)[1]);
+    u32 crc32 = ntohl(((u32 *)buffer)[0]);
+    s32 bwt_idx = ntohl(((s32 *)buffer)[1]);
 
-    if(bwt_idx == -1) {
+    if (bwt_idx == -1) {
         memmove(buffer, buffer + 8, data_size - 8);
         return data_size - 8;
     }
 
-    if(orig_size > state->block_size) {
+    if (orig_size > state->block_size) {
         state->last_error = BZ3_ERR_DATA_TOO_BIG;
         return -1;
     }
@@ -195,15 +201,15 @@ s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_size, s32 o
     s8 model = buffer[8];
     s32 lzp_size = -1, rle_size, p = 0;
 
-    if(model & 2) lzp_size = ntohl(((s32 *) (buffer + 9))[p++]);
-    if(model & 4) rle_size = ntohl(((s32 *) (buffer + 9))[p++]);
+    if (model & 2) lzp_size = ntohl(((s32 *)(buffer + 9))[p++]);
+    if (model & 4) rle_size = ntohl(((s32 *)(buffer + 9))[p++]);
 
     p += 2;
 
     data_size -= p * 4 + 1;
 
     // Decode the data.
-    u8 * b1 = buffer, * b2 = state->swap_buffer;
+    u8 *b1 = buffer, *b2 = state->swap_buffer;
 
     begin(state->cm_state);
     state->cm_state->in_queue = b1 + p * 4 + 1;
@@ -213,15 +219,15 @@ s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_size, s32 o
 
     s32 size_src;
 
-    if(model & 2)
+    if (model & 2)
         size_src = lzp_size;
-    else if(model & 4)
+    else if (model & 4)
         size_src = rle_size;
     else
         size_src = orig_size;
 
-    for (s32 i = 0; i < size_src; i++)
-        b2[i] = decode_byte(state->cm_state);
+#pragma GCC unroll 16
+    for (s32 i = 0; i < size_src; i++) b2[i] = decode_byte(state->cm_state);
     swap(b1, b2);
 
     // Undo BWT
@@ -232,12 +238,12 @@ s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_size, s32 o
     swap(b1, b2);
 
     // Undo LZP
-    if(model & 2) {
+    if (model & 2) {
         size_src = lzp_decompress(b1, b2, lzp_size, LZP_DICTIONARY, LZP_MIN_MATCH, state->lzp_lut);
         swap(b1, b2);
     }
 
-    if(model & 4) {
+    if (model & 4) {
         mrled(b1, b2, orig_size);
         size_src = orig_size;
         swap(b1, b2);
@@ -246,10 +252,9 @@ s32 bz3_decode_block(struct bz3_state * state, u8 * buffer, s32 data_size, s32 o
     state->last_error = BZ3_OK;
 
     // XXX: Better solution
-    if(b1 != buffer)
-        memcpy(buffer, b1, size_src);
-    
-    if(crc32 != crc32sum(1, buffer, size_src)) {
+    if (b1 != buffer) memcpy(buffer, b1, size_src);
+
+    if (crc32 != crc32sum(1, buffer, size_src)) {
         state->last_error = BZ3_ERR_CRC;
         return -1;
     }
diff --git a/src/lzp.c b/src/lzp.c
index b35177b..f5ceceb 100644
--- a/src/lzp.c
+++ b/src/lzp.c
@@ -89,7 +89,8 @@ static s32 lzp_encode_block(const u8 * restrict in, const u8 * in_end, u8 * rest
     return out >= out_eob ? -1 : (s32)(out - outs);
 }
 
-static s32 lzp_decode_block(const u8 * restrict in, const u8 * in_end, s32 * restrict lut, u8 * restrict out, s32 hash, s32 m_len) {
+static s32 lzp_decode_block(const u8 * restrict in, const u8 * in_end, s32 * restrict lut, u8 * restrict out, s32 hash,
+                            s32 m_len) {
     if (in_end - in < 4) return -1;
 
     memset(lut, 0, sizeof(s32) * (1 << hash));
@@ -166,7 +167,8 @@ s32 lzp_compress(const u8 * in, u8 * out, s32 n, s32 hash, s32 m_len, s32 * lut)
         else {
             memset(lut, 0, sizeof(s32) * (1 << hash));
 
-            r = lzp_encode_block(in + ins, in + ins + insz, out + out_ptr, out + out_ptr + outsz, lut, (s32)(1 << hash) - 1, m_len);
+            r = lzp_encode_block(in + ins, in + ins + insz, out + out_ptr, out + out_ptr + outsz, lut,
+                                 (s32)(1 << hash) - 1, m_len);
         }
 
         if (r < 0) {
diff --git a/src/main.c b/src/main.c
index 744ca59..10271d1 100644
--- a/src/main.c
+++ b/src/main.c
@@ -54,7 +54,7 @@ int main(int argc, char * argv[]) {
                 force_stdstreams = 1;
             }
         } else {
-            if(strlen(argv[i]) > 4 && !strcmp(argv[i] + strlen(argv[i]) - 4, ".bz3")) {
+            if (strlen(argv[i]) > 4 && !strcmp(argv[i] + strlen(argv[i]) - 4, ".bz3")) {
                 bz3_file = argv[i];
             } else {
                 regular_file = argv[i];
@@ -76,7 +76,7 @@ int main(int argc, char * argv[]) {
         return 1;
     }
 
-    if(mode == 1) {
+    if (mode == 1) {
         input = regular_file;
         output = bz3_file;
     } else {
@@ -84,7 +84,7 @@ int main(int argc, char * argv[]) {
         output = regular_file;
     }
 
-    FILE * input_des, * output_des;
+    FILE *input_des, *output_des;
 
     if (input != NULL) {
         input_des = fopen(input, "rb");
@@ -92,7 +92,7 @@ int main(int argc, char * argv[]) {
             perror("fopen");
             return 1;
         }
-    } else if(force_stdstreams) {
+    } else if (force_stdstreams) {
         input_des = stdin;
     }
 
@@ -102,7 +102,7 @@ int main(int argc, char * argv[]) {
             perror("open");
             return 1;
         }
-    } else if(force_stdstreams) {
+    } else if (force_stdstreams) {
         output_des = stdout;
     }
 
@@ -111,12 +111,12 @@ int main(int argc, char * argv[]) {
         return 1;
     }
 
-    #if HAVE_ISATTY == 1 && HAVE_FILENO == 1
-    if((isatty(fileno(output_des)) && mode == 1) || (isatty(fileno(input_des)) && mode == -1)) {
+#if HAVE_ISATTY == 1 && HAVE_FILENO == 1
+    if ((isatty(fileno(output_des)) && mode == 1) || (isatty(fileno(input_des)) && mode == -1)) {
         fprintf(stderr, "Refusing to read/write binary data from/to the terminal.\n");
         return 1;
     }
-    #endif
+#endif
 
     switch (mode) {
         case 1:
@@ -171,7 +171,8 @@ int main(int argc, char * argv[]) {
                 return 1;
             }
 
-            read_count = htonl(read_count); new_size = ntohl(new_size);
+            read_count = htonl(read_count);
+            new_size = ntohl(new_size);
             fwrite(&new_size, 4, 1, output_des);
             fwrite(&read_count, 4, 1, output_des);
             fwrite(buffer, ntohl(new_size), 1, output_des);
@@ -179,17 +180,18 @@ int main(int argc, char * argv[]) {
     } else if (mode == -1) {
         s32 new_size, old_size;
         while (!feof(input_des)) {
-            if(fread(&new_size, 1, 4, input_des) != 4) {
+            if (fread(&new_size, 1, 4, input_des) != 4) {
                 // Assume that the file has no more data.
                 break;
             }
-            if(fread(&old_size, 1, 4, input_des) != 4) {
+            if (fread(&old_size, 1, 4, input_des) != 4) {
                 fprintf(stderr, "I/O error.\n");
                 return 1;
             }
-            new_size = ntohl(new_size); old_size = ntohl(old_size);
+            new_size = ntohl(new_size);
+            old_size = ntohl(old_size);
             fread(buffer, 1, new_size, input_des);
-            if(bz3_decode_block(state, buffer, new_size, old_size) == -1) {
+            if (bz3_decode_block(state, buffer, new_size, old_size) == -1) {
                 fprintf(stderr, "Failed to decode a block: %s\n", bz3_strerror(state));
                 return 1;
             }
@@ -198,15 +200,16 @@ int main(int argc, char * argv[]) {
     } else if (mode == -2) {
         s32 new_size, old_size;
         while (!feof(input_des)) {
-            if(fread(&new_size, 4, 1, input_des) != 4) {
+            if (fread(&new_size, 4, 1, input_des) != 4) {
                 fprintf(stderr, "I/O error.\n");
             }
-            if(fread(&old_size, 4, 1, input_des) != 4) {
+            if (fread(&old_size, 4, 1, input_des) != 4) {
                 fprintf(stderr, "I/O error.\n");
             }
-            new_size = ntohl(new_size); old_size = ntohl(old_size);
+            new_size = ntohl(new_size);
+            old_size = ntohl(old_size);
             fread(buffer, 1, new_size, input_des);
-            if(bz3_decode_block(state, buffer, new_size, old_size) == -1) {
+            if (bz3_decode_block(state, buffer, new_size, old_size) == -1) {
                 fprintf(stderr, "Failed to decode a block: %s\n", bz3_strerror(state));
                 return 1;
             }
tab: 248 wrap: offon