From 53329b73a3f1e2082253674eb9f2890f74ab11c9 Mon Sep 17 00:00:00 2001
From: "raphael.bach" <raphael.bach@etu.hesge.ch>
Date: Sun, 20 Jan 2019 23:44:36 +0100
Subject: [PATCH] Add Sparse_GuessBlockSize()

---
 include/sparse.h |  4 ++++
 src/sparse.c     | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/include/sparse.h b/include/sparse.h
index 635f05d..3d2d8f6 100644
--- a/include/sparse.h
+++ b/include/sparse.h
@@ -157,6 +157,10 @@ void Sparse_PrintChunkHeader(const struct Sparse_Chunk * const header);
     Sparse_DumpInfo()
 ------------------------------------------------------------------------------*/
 void Sparse_DumpInfo(const struct Sparse * const sparse);
+/*------------------------------------------------------------------------------
+    Sparse_GuessBlockSize()
+------------------------------------------------------------------------------*/
+size_t Sparse_GuessBlockSize(size_t img_size);
 /*==============================================================================
     GUARD
 ==============================================================================*/
diff --git a/src/sparse.c b/src/sparse.c
index 0450a87..abef8ee 100644
--- a/src/sparse.c
+++ b/src/sparse.c
@@ -58,6 +58,9 @@ static struct Logger s_logger = {
     .level_flag = LOG_LEVEL_ALL,
     .output     = LOG_OUTPUT_STDERR
 };
+static const size_t s_block_size_list[] = {
+    4096, 2048, 1024
+};
 /*==============================================================================
     PUBLIC FUNCTION DEFINITION
 ==============================================================================*/
@@ -333,3 +336,18 @@ void Sparse_DumpInfo(const struct Sparse * const sparse)
         printf("%zu\n"    , (sparse->chunks + i)->size + CHUNK_HEADER_SIZE);
     }
 }
+/*------------------------------------------------------------------------------
+    Sparse_GuessBlockSize()
+------------------------------------------------------------------------------*/
+size_t Sparse_GuessBlockSize(size_t img_size)
+{
+    size_t block_size = 0;
+    size_t array_size = sizeof(s_block_size_list) / sizeof(size_t);
+    for(size_t i = 0; i < array_size; i++) {
+        if((img_size % s_block_size_list[i]) == 0) {
+            block_size = s_block_size_list[i];
+            break;
+        }
+    }
+    return block_size;
+}
-- 
GitLab