From 062c34d2c65b297a47dbaf355c4f38b3c8ce5305 Mon Sep 17 00:00:00 2001
From: Dominik Loidolt <dominik.loidolt@univie.ac.at>
Date: Tue, 1 Aug 2023 11:51:11 +0200
Subject: [PATCH] Add test cases for compression; add comments; some
 refactoring

---
 .gitignore                  |  2 +-
 include/byteorder.h         |  4 ++--
 lib/cmp_icu.c               |  3 +++
 lib/rdcu_rmap.c             |  4 ++--
 lib/rmap.c                  | 16 ----------------
 test/cmp_icu/test_cmp_icu.c | 28 ++++++++++++++++++++++++++--
 6 files changed, 34 insertions(+), 23 deletions(-)

diff --git a/.gitignore b/.gitignore
index 852f22f..d4e5f78 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,7 +3,7 @@
 cmp_tool
 !test/cmp_tool
 
-# cmp_tool files
+#cmp_tool files
 *.cfg
 *.info
 *.dat
diff --git a/include/byteorder.h b/include/byteorder.h
index 16d14a4..4b58a45 100644
--- a/include/byteorder.h
+++ b/include/byteorder.h
@@ -53,13 +53,13 @@
 #undef __LITTLE_ENDIAN
 #endif
 
-#if defined (__sparc__)
+#if defined(__sparc__)
 #ifndef __BIG_ENDIAN
 #define __BIG_ENDIAN 4321
 #endif
 #endif
 
-#if defined (__i386__) || defined (__x86_64__)
+#if defined(__i386__) || defined(__x86_64__)
 #ifndef __LITTLE_ENDIAN
 #define __LITTLE_ENDIAN 1234
 #endif
diff --git a/lib/cmp_icu.c b/lib/cmp_icu.c
index dea6ee5..4e57c01 100644
--- a/lib/cmp_icu.c
+++ b/lib/cmp_icu.c
@@ -460,12 +460,14 @@ static int put_n_bits32(uint32_t value, unsigned int n_bits, int bit_offset,
  *
  * @param value		value to be encoded
  * @param m		Golomb parameter, only m's which are power of 2 are allowed
+ *			maximum allowed Golomb parameter is 0x80000000
  * @param log2_m	Rice parameter, is log_2(m) calculate outside function
  *			for better performance
  * @param cw		address were the encode code word is stored
  *
  * @returns the length of the formed code word in bits; code word is invalid if
  *	the return value is greater than 32
+ * @warning no check of the validity of the input parameters!
  */
 
 static uint32_t rice_encoder(uint32_t value, uint32_t m, uint32_t log2_m,
@@ -505,6 +507,7 @@ static uint32_t rice_encoder(uint32_t value, uint32_t m, uint32_t log2_m,
  *
  * @returns the length of the formed code word in bits; code word is invalid if
  *	the return value is greater than 32
+ * @warning no check of the validity of the input parameters!
  */
 
 static uint32_t golomb_encoder(uint32_t value, uint32_t m, uint32_t log2_m,
diff --git a/lib/rdcu_rmap.c b/lib/rdcu_rmap.c
index 028a319..3358765 100644
--- a/lib/rdcu_rmap.c
+++ b/lib/rdcu_rmap.c
@@ -581,7 +581,7 @@ int rdcu_package(uint8_t *blob,
 {
 	int n;
 	int has_data_crc = 0;
-	struct rmap_instruction *ri;
+	const struct rmap_instruction *ri;
 
 
 	if (data_size & 0x3)	/* must be multiple of 4 */
@@ -599,7 +599,7 @@ int rdcu_package(uint8_t *blob,
 	/* allocate space for header, header crc, data, data crc */
 	n = cmd_size + 1;
 
-	ri = (struct rmap_instruction *) &cmd[non_crc_bytes + RMAP_INSTRUCTION];
+	ri = (const struct rmap_instruction *) &cmd[non_crc_bytes + RMAP_INSTRUCTION];
 
 	/* see if the type of command needs a data crc field at the end */
 	switch (ri->cmd) {
diff --git a/lib/rmap.c b/lib/rmap.c
index 2f810d0..3583576 100644
--- a/lib/rmap.c
+++ b/lib/rmap.c
@@ -184,20 +184,6 @@ struct rmap_pkt *rmap_create_packet(void)
 }
 
 
-/**
- * @brief destroys an RMAP packet
- *
- * @param pkt a struct rmap_pkt
- *
- * @note this will NOT deallocate and pointer references assigned by the user
- */
-
-void rmap_destroy_packet(struct rmap_pkt *pkt)
-{
-	free(pkt);
-}
-
-
 /**
  * @brief completely destroys an RMAP packet
  *
@@ -623,8 +609,6 @@ error:
 __extension__
 static int rmap_check_status(uint8_t status)
 {
-
-
 	printf("\tStatus: ");
 
 	switch (status) {
diff --git a/test/cmp_icu/test_cmp_icu.c b/test/cmp_icu/test_cmp_icu.c
index 3a338c4..199b7fe 100644
--- a/test/cmp_icu/test_cmp_icu.c
+++ b/test/cmp_icu/test_cmp_icu.c
@@ -1835,6 +1835,26 @@ void test_golomb_encoder(void)
 	TEST_ASSERT_EQUAL_INT(32, cw_len);
 	TEST_ASSERT_EQUAL_HEX(0xFFFFFFFB, cw);
 
+	/* test some arbitrary values with g_par = 0x7FFFFFFF */
+	value = 0; g_par = 0x7FFFFFFF; log2_g_par = (uint32_t)ilog_2(g_par); cw = ~0U;
+	cw_len = golomb_encoder(value, g_par, log2_g_par, &cw);
+	TEST_ASSERT_EQUAL_INT(31, cw_len);
+	TEST_ASSERT_EQUAL_HEX(0x0, cw);
+
+	value = 1;
+	cw_len = golomb_encoder(value, g_par, log2_g_par, &cw);
+	TEST_ASSERT_EQUAL_INT(32, cw_len);
+	TEST_ASSERT_EQUAL_HEX(0x2, cw);
+
+	value = 0x7FFFFFFE;
+	cw_len = golomb_encoder(value, g_par, log2_g_par, &cw);
+	TEST_ASSERT_EQUAL_INT(32, cw_len);
+	TEST_ASSERT_EQUAL_HEX(0x7FFFFFFF, cw);
+
+	value = 0x7FFFFFFF;
+	cw_len = golomb_encoder(value, g_par, log2_g_par, &cw);
+	TEST_ASSERT_EQUAL_INT(32, cw_len);
+	TEST_ASSERT_EQUAL_HEX(0x80000000, cw);
 
 	/* test maximum Golomb parameter for golomb_encoder */
 	value = 0; g_par = MAX_GOLOMB_PAR; log2_g_par = (uint32_t)ilog_2(g_par); cw = ~0U;
@@ -2850,12 +2870,14 @@ void test_compress_s_fx_staff(void)
 	TEST_ASSERT_EQUAL_INT(cmp_size_exp, cmp_size);
 	TEST_ASSERT_FALSE(memcmp(cfg.input_buf, cfg.icu_output_buf, MULTI_ENTRY_HDR_SIZE));
 	hdr = (void *)cfg.icu_output_buf;
-	cmp_data = (uint32_t *)hdr->entry;
+	cmp_data = calloc(4, sizeof(*cmp_data));
+	memcpy(cmp_data, hdr->entry, 4 * sizeof(*cmp_data));
 	TEST_ASSERT_EQUAL_HEX(0x00000080, be32_to_cpu(cmp_data[0]));
 	TEST_ASSERT_EQUAL_HEX(0x00060001, be32_to_cpu(cmp_data[1]));
 	TEST_ASSERT_EQUAL_HEX(0x1E000423, be32_to_cpu(cmp_data[2]));
 	TEST_ASSERT_EQUAL_HEX(0xFFFFE000, be32_to_cpu(cmp_data[3]));
 
+	free(cmp_data);
 	free(cfg.input_buf);
 	free(cfg.icu_output_buf);
 }
@@ -4260,9 +4282,11 @@ void test_cmp_data_to_big_endian_error_cases(void)
 	int cmp_size;
 	int cmp_size_return;
 	uint16_t cmp_data[3] = {0x0123, 0x4567, 0x89AB};
+	uint32_t output_buf[2] = {0};
 	uint8_t *p;
 
-	cfg.icu_output_buf = (uint32_t *)cmp_data;
+	memcpy(output_buf, cmp_data, sizeof(cmp_data));
+	cfg.icu_output_buf = output_buf;
 
 	/* this should work */
 	cfg.data_type = DATA_TYPE_IMAGETTE;
-- 
GitLab