From 8ba76f758c4252e3f517a4cd8745192e61a6f30a Mon Sep 17 00:00:00 2001
From: Dominik Loidolt <dominik.loidolt@univie.ac.at>
Date: Thu, 29 Sep 2022 13:15:25 +0200
Subject: [PATCH] fix some issues with sparc and windows compiler

---
 lib/cmp_entity.c              |  2 +-
 lib/cmp_io.c                  |  2 +-
 lib/decmp.c                   | 10 +++----
 test/cmp_icu/test_cmp_decmp.c | 53 +++++++++++++++++++++++++++++++++--
 test/meson.build              |  2 +-
 5 files changed, 59 insertions(+), 10 deletions(-)

diff --git a/lib/cmp_entity.c b/lib/cmp_entity.c
index b66216c..daa1e11 100644
--- a/lib/cmp_entity.c
+++ b/lib/cmp_entity.c
@@ -1692,7 +1692,7 @@ void *cmp_ent_get_data_buf(struct cmp_entity *ent)
  *	compressed data is copied to (can be NULL)
  * @param data_buf_size	size of the destination data buffer
  *
- * @returns the size in bytes to store the compressed data; negative on error
+ * @returns the size in bytes to store the compressed data; -1 on error
  *
  * @note the destination and source buffer can overlap
  * @note converts the data to the correct endianness
diff --git a/lib/cmp_io.c b/lib/cmp_io.c
index d8772fd..0dfcc8d 100644
--- a/lib/cmp_io.c
+++ b/lib/cmp_io.c
@@ -1334,7 +1334,7 @@ ssize_t read_file8(const char *file_name, uint8_t *buf, uint32_t buf_size, int v
 		fclose(fp);
 		return 0;
 	}
-	if (file_size < buf_size) {
+	if ((unsigned long)file_size < buf_size) {
 		fprintf(stderr, "%s: %s: Error: The files do not contain enough data as requested.\n", PROGRAM_NAME, file_name);
 		goto fail;
 	}
diff --git a/lib/decmp.c b/lib/decmp.c
index 7bea1e1..3be77c9 100644
--- a/lib/decmp.c
+++ b/lib/decmp.c
@@ -40,7 +40,7 @@
 extern struct cmp_max_used_bits max_used_bits;
 
 /* function pointer to a code word decoder function */
-typedef int (*decoder_ptr)(unsigned int, unsigned int, unsigned int, unsigned int *);
+typedef int (*decoder_ptr)(uint32_t, unsigned int, unsigned int, uint32_t *);
 
 /* structure to hold a setup to encode a value */
 struct decoder_setup {
@@ -89,7 +89,7 @@ static unsigned int count_leading_ones(uint32_t value)
  */
 
 static int rice_decoder(uint32_t code_word, unsigned int m, unsigned int log2_m,
-			unsigned int *decoded_cw)
+			uint32_t *decoded_cw)
 {
 	unsigned int q; /* quotient code */
 	unsigned int ql; /* length of the quotient code */
@@ -138,8 +138,8 @@ static int rice_decoder(uint32_t code_word, unsigned int m, unsigned int log2_m,
  *	0 on failure
  */
 
-static int golomb_decoder(unsigned int code_word, unsigned int m,
-			  unsigned int log2_m, unsigned int *decoded_cw)
+static int golomb_decoder(uint32_t code_word, unsigned int m,
+			  unsigned int log2_m, uint32_t *decoded_cw)
 {
 	unsigned int q; /* quotient code */
 	unsigned int r1; /* remainder code group 1 */
@@ -219,7 +219,7 @@ static decoder_ptr select_decoder(unsigned int golomb_par)
 static int get_n_bits32(uint32_t *p_value, unsigned int n_bits, int bit_offset,
 			uint32_t *bitstream_adr, unsigned int max_stream_len)
 {
-	const unsigned int *local_adr;
+	uint32_t *local_adr;
 	unsigned int bitsLeft, bitsRight, localEndPos;
 	unsigned int mask;
 	int stream_len = (int)(n_bits + (unsigned int)bit_offset); /* overflow results in a negative return value */
diff --git a/test/cmp_icu/test_cmp_decmp.c b/test/cmp_icu/test_cmp_decmp.c
index e961ffc..9eb587e 100644
--- a/test/cmp_icu/test_cmp_decmp.c
+++ b/test/cmp_icu/test_cmp_decmp.c
@@ -41,7 +41,7 @@
 #define IMAX_BITS(m) ((m)/((m)%255+1) / 255%255*8 + 7-86/((m)%255+12))
 #define RAND_MAX_WIDTH IMAX_BITS(RAND_MAX)
 
-#define set_n_bits(n)  (~(~0UL << (n)))
+#define set_n_bits(n)  (n!=32?~(~0UL << (n)):0xFFFFFFFF)
 
 
 /**
@@ -588,7 +588,7 @@ void test_random_compression_decompression(void)
 		void *data_to_compress2 = generate_random_test_data(samples, data_type);
 		void *updated_model = calloc(1, cmp_cal_size_of_data(samples, data_type));
 		/* for (cmp_mode = CMP_MODE_RAW; cmp_mode <= CMP_MODE_STUFF; cmp_mode++) { */
-		for (cmp_mode = CMP_MODE_RAW; cmp_mode < CMP_MODE_STUFF; cmp_mode++) {
+		for (cmp_mode = CMP_MODE_RAW; cmp_mode <= CMP_MODE_DIFF_MULTI; cmp_mode++) {
 			/* printf("cmp_mode: %i\n", cmp_mode); */
 			cfg = cmp_cfg_icu_create(data_type, cmp_mode, model_value,
 						 CMP_LOSSLESS);
@@ -612,3 +612,52 @@ void test_random_compression_decompression(void)
 		free(updated_model);
 	}
 }
+
+#define N_SAMPLES 5
+
+void test_random_compression_decompression2(void)
+{
+	struct cmp_cfg cfg;
+	struct cmp_info info = {0};
+	uint32_t cmp_buffer_size;
+	int s, i, cmp_size_bits;
+	void *compressed_data;
+	uint16_t *decompressed_data;
+	uint16_t data[N_SAMPLES] = {0, UINT16_MAX, INT16_MAX, 42, 23};
+
+	cfg = cmp_cfg_icu_create(DATA_TYPE_IMAGETTE, CMP_MODE_RAW, 8, CMP_LOSSLESS);
+	TEST_ASSERT_NOT_EQUAL_INT(cfg.data_type, DATA_TYPE_UNKNOWN);
+
+	cmp_buffer_size = cmp_cfg_icu_buffers(&cfg, data, N_SAMPLES, NULL, NULL,
+					      NULL, N_SAMPLES*CMP_BUFFER_FAKTOR);
+	compressed_data = malloc(cmp_buffer_size);
+	cmp_buffer_size = cmp_cfg_icu_buffers(&cfg, data, N_SAMPLES, NULL, NULL,
+					      compressed_data, N_SAMPLES*CMP_BUFFER_FAKTOR);
+	TEST_ASSERT_EQUAL_INT(cmp_buffer_size, cmp_cal_size_of_data(CMP_BUFFER_FAKTOR*N_SAMPLES, DATA_TYPE_IMAGETTE));
+
+	cmp_size_bits = icu_compress_data(&cfg);
+	TEST_ASSERT(cmp_size_bits > 0);
+	info.cmp_size = cmp_size_bits;
+	info.cmp_mode_used = (uint8_t)cfg.cmp_mode;
+	info.model_value_used = (uint8_t)cfg.model_value;
+	info.round_used = (uint8_t)cfg.round;
+	info.spill_used = cfg.spill;
+	info.golomb_par_used = cfg.golomb_par;
+	info.samples_used = cfg.samples;
+	info.rdcu_new_model_adr_used = cfg.rdcu_new_model_adr;
+	info.rdcu_cmp_adr_used = cfg.rdcu_buffer_adr;
+
+	s = decompress_rdcu_data(compressed_data, &info, NULL, NULL, NULL);
+	TEST_ASSERT(s > 0);
+	decompressed_data = malloc(s);
+	s = decompress_rdcu_data(compressed_data, &info, NULL, NULL, decompressed_data);
+	TEST_ASSERT(s > 0);
+
+	for (i = 0; i < N_SAMPLES; i++) {
+		TEST_ASSERT_EQUAL_HEX16(data[i], decompressed_data[i]);
+	}
+
+
+	free(compressed_data);
+	free(decompressed_data);
+}
diff --git a/test/meson.build b/test/meson.build
index 11ddac0..36fee6c 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -8,7 +8,7 @@ if checkpatch.found()
     '--ignore', 'SPDX_LICENSE_TAG,PREFER_DEFINED_ATTRIBUTE_MACRO,EMBEDDED_FILENAME,BLOCK_COMMENT_STYLE,EMBEDDED_FUNCTION_NAME',
   ]
   run_target('syntax-check',
-   command : [checkpatch, checkpatch_args, main, cmplib_sources])
+  command : [checkpatch, checkpatch_args, main, cmplib_sources])
 endif
 
 # add cppcheck inspector target
-- 
GitLab