:: commit 3f309e7bac1be984e24582adce9107917aa37bff

Kamila Szewczyk <kspalaiologos@gmail.com> — 2022-05-08 10:12

parents: b1846fbb9e

`clang-format`

diff --git a/include/common.h b/include/common.h
index b77f880..108129a 100644
--- a/include/common.h
+++ b/include/common.h
@@ -35,16 +35,13 @@ typedef int8_t s8;
 typedef int16_t s16;
 typedef int32_t s32;
 
-static s32 read_neutral_s32(u8 * data) {
-    return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24);
-}
+static s32 read_neutral_s32(u8 * data) { return data[0] | (data[1] << 8) | (data[2] << 16) | (data[3] << 24); }
 
 static void write_neutral_s32(u8 * data, s32 value) {
     data[0] = value & 0xFF;
     data[1] = (value >> 8) & 0xFF;
     data[2] = (value >> 16) & 0xFF;
     data[3] = (value >> 24) & 0xFF;
-
 }
 
 #define PUBLIC_API
diff --git a/include/libbz3.h b/include/libbz3.h
index a523e93..688b213 100644
--- a/include/libbz3.h
+++ b/include/libbz3.h
@@ -74,9 +74,9 @@ int32_t bz3_decode_block(struct bz3_state * state, uint8_t * buffer, int32_t siz
  * All specifics of the `bz3_encode_block' still hold. The function will launch a thread for each block.
  * The compressed sizes are written to the `sizes' array. Every buffer is overwritten and none of them can overlap.
  * Precisely `n' states, buffers and sizes must be supplied.
- * 
+ *
  * Expects `n' between 2 and 16.
- * 
+ *
  * Present in the shared library only if -lpthread was present during building.
  */
 void bz3_encode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t n);
@@ -85,6 +85,7 @@ void bz3_encode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t
  * @brief Decode `n' blocks, all in parallel.
  * Same specifics as `bz3_encode_blocks', but doesn't overwrite `sizes'.
  */
-void bz3_decode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t orig_sizes[], int32_t n);
+void bz3_decode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t orig_sizes[],
+                       int32_t n);
 
 #endif
diff --git a/include/rle.h b/include/rle.h
index 490c3fb..db8f95e 100644
--- a/include/rle.h
+++ b/include/rle.h
@@ -2,7 +2,7 @@
 /*
  * BZip3 - A spiritual successor to BZip2.
  * Copyright (C) 2022 Kamila Szewczyk
- * 
+ *
  * An implementation of Mespotine RLE.
  *
  * This program is free software: you can redistribute it and/or modify it
diff --git a/src/libbz3.c b/src/libbz3.c
index 3563a9d..1f8b9a9 100644
--- a/src/libbz3.c
+++ b/src/libbz3.c
@@ -78,14 +78,10 @@ PUBLIC_API 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->swap_buffer || !bz3_state->sais_array || !bz3_state->lzp_lut) {
-        if(bz3_state->cm_state)
-            free(bz3_state->cm_state);
-        if(bz3_state->swap_buffer)
-            free(bz3_state->swap_buffer);
-        if(bz3_state->sais_array)
-            free(bz3_state->sais_array);
-        if(bz3_state->lzp_lut)
-            free(bz3_state->lzp_lut);
+        if (bz3_state->cm_state) free(bz3_state->cm_state);
+        if (bz3_state->swap_buffer) free(bz3_state->swap_buffer);
+        if (bz3_state->sais_array) free(bz3_state->sais_array);
+        if (bz3_state->lzp_lut) free(bz3_state->lzp_lut);
 
         return NULL;
     }
@@ -284,41 +280,39 @@ static void * bz3_init_encode_thread(void * _msg) {
     encode_thread_msg * msg = _msg;
     msg->size = bz3_encode_block(msg->state, msg->buffer, msg->size);
     pthread_exit(NULL);
-    return NULL; // unreachable
+    return NULL;  // unreachable
 }
 
 static void * bz3_init_decode_thread(void * _msg) {
     decode_thread_msg * msg = _msg;
     bz3_decode_block(msg->state, msg->buffer, msg->size, msg->orig_size);
     pthread_exit(NULL);
-    return NULL; // unreachable
+    return NULL;  // unreachable
 }
 
 PUBLIC_API void bz3_encode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t n) {
     encode_thread_msg messages[n];
     pthread_t threads[n];
-    for(int32_t i = 0; i < n; i++) {
+    for (int32_t i = 0; i < n; i++) {
         messages[i].state = states[i];
         messages[i].buffer = buffers[i];
         messages[i].size = sizes[i];
         pthread_create(&threads[i], NULL, bz3_init_encode_thread, &messages[i]);
     }
-    for(int32_t i = 0; i < n; i++)
-        pthread_join(threads[i], NULL);
-    for(int32_t i = 0; i < n; i++)
-        sizes[i] = messages[i].size;
+    for (int32_t i = 0; i < n; i++) pthread_join(threads[i], NULL);
+    for (int32_t i = 0; i < n; i++) sizes[i] = messages[i].size;
 }
 
-PUBLIC_API void bz3_decode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[], int32_t orig_sizes[], int32_t n) {
+PUBLIC_API void bz3_decode_blocks(struct bz3_state * states[], uint8_t * buffers[], int32_t sizes[],
+                                  int32_t orig_sizes[], int32_t n) {
     decode_thread_msg messages[n];
     pthread_t threads[n];
-    for(int32_t i = 0; i < n; i++) {
+    for (int32_t i = 0; i < n; i++) {
         messages[i].state = states[i];
         messages[i].buffer = buffers[i];
         messages[i].size = sizes[i];
         messages[i].orig_size = orig_sizes[i];
         pthread_create(&threads[i], NULL, bz3_init_decode_thread, &messages[i]);
     }
-    for(int32_t i = 0; i < n; i++)
-        pthread_join(threads[i], NULL);
+    for (int32_t i = 0; i < n; i++) pthread_join(threads[i], NULL);
 }
diff --git a/src/main.c b/src/main.c
index 3d12c6c..8ec734a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -58,7 +58,7 @@ int main(int argc, char * argv[]) {
                 i++;
             }
         } else {
-            if(bz3_file != NULL && regular_file != NULL) {
+            if (bz3_file != NULL && regular_file != NULL) {
                 fprintf(stderr, "Error: too many files specified.\n");
                 return 1;
             }
@@ -89,11 +89,11 @@ int main(int argc, char * argv[]) {
         return 1;
     }
 
-    if(mode != 2) {
+    if (mode != 2) {
         if (no_bz3_suffix || mode == 1) {
             input = regular_file;
             output = bz3_file;
-            if(!force_stdstreams && !no_bz3_suffix && output == NULL && input != NULL) {
+            if (!force_stdstreams && !no_bz3_suffix && output == NULL && input != NULL) {
                 // add the bz3 extension
                 output = malloc(strlen(input) + 5);
                 strcpy(output, input);
@@ -102,7 +102,7 @@ int main(int argc, char * argv[]) {
         } else {
             input = bz3_file;
             output = regular_file;
-            if(!force_stdstreams && output == NULL && input != NULL) {
+            if (!force_stdstreams && output == NULL && input != NULL) {
                 // strip the bz3 extension
                 output = malloc(strlen(input) - 4);
                 strncpy(output, input, strlen(input) - 4);
@@ -180,12 +180,12 @@ int main(int argc, char * argv[]) {
         }
     }
 
-    if(workers > 16 || workers < 0) {
+    if (workers > 16 || workers < 0) {
         fprintf(stderr, "Number of workers must be between 2 and 16.\n");
         return 1;
     }
 
-    if(workers <= 1) {
+    if (workers <= 1) {
         struct bz3_state * state = bz3_new(block_size);
 
         if (state == NULL) {
@@ -195,7 +195,7 @@ int main(int argc, char * argv[]) {
 
         u8 * buffer = malloc(block_size + block_size / 50 + 16);
 
-        if(!buffer) {
+        if (!buffer) {
             fprintf(stderr, "Failed to allocate memory.\n");
             return 1;
         }
@@ -274,36 +274,38 @@ int main(int argc, char * argv[]) {
         u8 * buffers[workers];
         s32 sizes[workers];
         s32 old_sizes[workers];
-        for(s32 i = 0; i < workers; i++) {
+        for (s32 i = 0; i < workers; i++) {
             states[i] = bz3_new(block_size);
             if (states[i] == NULL) {
                 fprintf(stderr, "Failed to create a block encoder state.\n");
                 return 1;
             }
             buffers[i] = malloc(block_size + block_size / 50 + 16);
-            if(!buffers[i]) {
+            if (!buffers[i]) {
                 fprintf(stderr, "Failed to allocate memory.\n");
                 return 1;
             }
         }
 
         if (mode == 1) {
-            while(!feof(input_des)) {
+            while (!feof(input_des)) {
                 s32 i = 0;
-                for(; i < workers; i++) {
+                for (; i < workers; i++) {
                     size_t read_count = fread(buffers[i], 1, block_size, input_des);
                     sizes[i] = old_sizes[i] = read_count;
-                    if(read_count < block_size)
-                        { i++; break; }
+                    if (read_count < block_size) {
+                        i++;
+                        break;
+                    }
                 }
                 bz3_encode_blocks(states, buffers, sizes, i);
-                for(s32 j = 0; j < i; j++) {
+                for (s32 j = 0; j < i; j++) {
                     if (bz3_last_error(states[j]) != BZ3_OK) {
                         fprintf(stderr, "Failed to encode data: %s\n", bz3_strerror(states[j]));
                         return 1;
                     }
                 }
-                for(s32 j = 0; j < i; j++) {
+                for (s32 j = 0; j < i; j++) {
                     write_neutral_s32(byteswap_buf, sizes[j]);
                     fwrite(byteswap_buf, 4, 1, output_des);
                     write_neutral_s32(byteswap_buf, old_sizes[j]);
@@ -311,49 +313,45 @@ int main(int argc, char * argv[]) {
                     fwrite(buffers[j], sizes[j], 1, output_des);
                 }
             }
-        } else if(mode == -1) {
-            while(!feof(input_des)) {
+        } else if (mode == -1) {
+            while (!feof(input_des)) {
                 s32 i = 0;
-                for(; i < workers; i++) {
-                    if(fread(&byteswap_buf, 1, 4, input_des) != 4)
-                        break;
+                for (; i < workers; i++) {
+                    if (fread(&byteswap_buf, 1, 4, input_des) != 4) break;
                     sizes[i] = read_neutral_s32(byteswap_buf);
-                    if(fread(&byteswap_buf, 1, 4, input_des) != 4)
-                        break;
+                    if (fread(&byteswap_buf, 1, 4, input_des) != 4) break;
                     old_sizes[i] = read_neutral_s32(byteswap_buf);
-                    if(fread(buffers[i], 1, sizes[i], input_des) != sizes[i]) {
+                    if (fread(buffers[i], 1, sizes[i], input_des) != sizes[i]) {
                         fprintf(stderr, "I/O error.\n");
                         return 1;
                     }
                 }
                 bz3_decode_blocks(states, buffers, sizes, old_sizes, i);
-                for(s32 j = 0; j < i; j++) {
+                for (s32 j = 0; j < i; j++) {
                     if (bz3_last_error(states[j]) != BZ3_OK) {
                         fprintf(stderr, "Failed to decode data: %s\n", bz3_strerror(states[j]));
                         return 1;
                     }
                 }
-                for(s32 j = 0; j < i; j++) {
+                for (s32 j = 0; j < i; j++) {
                     fwrite(buffers[j], old_sizes[j], 1, output_des);
                 }
             }
-        } else if(mode == 2) {
-            while(!feof(input_des)) {
+        } else if (mode == 2) {
+            while (!feof(input_des)) {
                 s32 i = 0;
-                for(; i < workers; i++) {
-                    if(fread(&byteswap_buf, 1, 4, input_des) != 4)
-                        break;
+                for (; i < workers; i++) {
+                    if (fread(&byteswap_buf, 1, 4, input_des) != 4) break;
                     sizes[i] = read_neutral_s32(byteswap_buf);
-                    if(fread(&byteswap_buf, 1, 4, input_des) != 4)
-                        break;
+                    if (fread(&byteswap_buf, 1, 4, input_des) != 4) break;
                     old_sizes[i] = read_neutral_s32(byteswap_buf);
-                    if(fread(buffers[i], 1, sizes[i], input_des) != sizes[i]) {
+                    if (fread(buffers[i], 1, sizes[i], input_des) != sizes[i]) {
                         fprintf(stderr, "I/O error.\n");
                         return 1;
                     }
                 }
                 bz3_decode_blocks(states, buffers, sizes, old_sizes, i);
-                for(s32 j = 0; j < i; j++) {
+                for (s32 j = 0; j < i; j++) {
                     if (bz3_last_error(states[j]) != BZ3_OK) {
                         fprintf(stderr, "Failed to decode data: %s\n", bz3_strerror(states[j]));
                         return 1;
@@ -362,7 +360,7 @@ int main(int argc, char * argv[]) {
             }
         }
 
-        for(s32 i = 0; i < workers; i++) {
+        for (s32 i = 0; i < workers; i++) {
             free(buffers[i]);
             bz3_free(states[i]);
         }
diff --git a/src/rle.c b/src/rle.c
index afed93c..8a16e10 100644
--- a/src/rle.c
+++ b/src/rle.c
@@ -2,7 +2,7 @@
 /*
  * BZip3 - A spiritual successor to BZip2.
  * Copyright (C) 2022 Kamila Szewczyk
- * 
+ *
  * An implementation of Mespotine RLE.
  *
  * This program is free software: you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
 #include "rle.h"
 
 s32 mrlec(u8 * in, s32 inlen, u8 * out) {
-    u8 * ip = in, * in_end = in + inlen;
+    u8 *ip = in, *in_end = in + inlen;
     s32 op = 0;
     s32 c, pc = -1;
     s32 t[256] = { 0 };
@@ -77,8 +77,7 @@ void mrled(u8 * restrict in, u8 * restrict out, s32 outlen) {
             for (run = 0; (pc = in[ip++]) == 255; run += 255)
                 ;
             run += pc + 1;
-            for (; run > 0; --run)
-                out[op++] = c;
+            for (; run > 0; --run) out[op++] = c;
         } else
             out[op++] = c;
     }
tab: 248 wrap: offon