From 1dea131e6f5a7d436ac77379f65b498cbbeb0068 Mon Sep 17 00:00:00 2001
From: Dominik Loidolt <dominik.loidolt@univie.ac.at>
Date: Sat, 16 Mar 2024 21:32:36 +0100
Subject: [PATCH] Update: Adapt to C standard GNU89 with GCC extensions

---
 examples/example_cmp_icu.c              |  2 +-
 examples/example_cmp_rdcu.c             |  7 ++++---
 lib/common/byteorder.h                  |  4 ++--
 lib/common/cmp_data_types.c             | 11 +++++++++--
 lib/common/cmp_entity.c                 |  4 +++-
 lib/decompress/cmp_max_used_bits_list.c |  1 +
 lib/decompress/decmp.c                  |  3 ++-
 lib/icu_compress/cmp_icu.c              |  2 +-
 lib/rdcu_compress/cmp_rdcu.c            |  1 +
 lib/rdcu_compress/rmap.c                |  2 +-
 meson.build                             |  2 +-
 programs/cmp_io.c                       |  4 ++--
 programs/cmp_tool.c                     |  2 +-
 13 files changed, 29 insertions(+), 16 deletions(-)

diff --git a/examples/example_cmp_icu.c b/examples/example_cmp_icu.c
index ba704fc..f0d783e 100644
--- a/examples/example_cmp_icu.c
+++ b/examples/example_cmp_icu.c
@@ -39,7 +39,7 @@
 #define MODEL_COUNTER 1
 
 
-int demo_icu_compression(void)
+static int demo_icu_compression(void)
 {
 	struct cmp_cfg example_cfg;
 	struct cmp_entity *cmp_entity = NULL;
diff --git a/examples/example_cmp_rdcu.c b/examples/example_cmp_rdcu.c
index 9bf81ce..93c76df 100644
--- a/examples/example_cmp_rdcu.c
+++ b/examples/example_cmp_rdcu.c
@@ -23,6 +23,7 @@
 
 #include <cmp_rdcu.h>
 #include <cmp_entity.h>
+#include <leon_inttypes.h>
 
 #define MAX_PAYLOAD_SIZE 4096
 #define DATA_SAMPLES 6  /* number of 16 bit samples to compress */
@@ -148,7 +149,7 @@ int demo_rdcu_compression(void)
 		/* get the size of the compression entity */
 		cmp_ent_size = cmp_ent_build(NULL, CMP_ASW_VERSION_ID,
 					     START_TIME, END_TIME, MODEL_ID, MODEL_COUNTER,
-					     &example_cfg, example_info.cmp_size);
+					     &example_cfg, (int)example_info.cmp_size);
 		if (!cmp_ent_size) {
 			printf("Error occurred during cmp_ent_build()\n");
 			return -1;
@@ -164,7 +165,7 @@ int demo_rdcu_compression(void)
 		/* now let us build the compression entity */
 		cmp_ent_size = cmp_ent_build(cmp_ent, CMP_ASW_VERSION_ID,
 					     START_TIME, END_TIME, MODEL_ID, MODEL_COUNTER,
-					     &example_cfg, example_info.cmp_size);
+					     &example_cfg, (int)example_info.cmp_size);
 		if (!cmp_ent_size) {
 			printf("Error occurred during cmp_ent_build()\n");
 			return -1;
@@ -186,7 +187,7 @@ int demo_rdcu_compression(void)
 		}
 
 		s = cmp_ent_get_size(cmp_ent);
-		printf("\n\nHere's the compressed data including the header (size %lu):\n"
+		printf("\n\nHere's the compressed data including the header (size %"PRIu32"):\n"
 		       "============================================================\n", s);
 		for (i = 0; i < s; i++) {
 			uint8_t *p = (uint8_t *)cmp_ent;
diff --git a/lib/common/byteorder.h b/lib/common/byteorder.h
index 634e184..fc5ff35 100644
--- a/lib/common/byteorder.h
+++ b/lib/common/byteorder.h
@@ -56,7 +56,7 @@
 #ifndef __BIG_ENDIAN
 #  if defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
 #    define __BIG_ENDIAN 4321
-#  elif defined(__clang__) && __BIG_ENDIAN__
+#  elif defined(__clang__) && defined(__BIG_ENDIAN__)
 #    define __BIG_ENDIAN 4321
 #  elif defined(__sparc__)
 #    define __BIG_ENDIAN 4321
@@ -66,7 +66,7 @@
 #ifndef __LITTLE_ENDIAN
 #  if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
 #    define __LITTLE_ENDIAN 1234
-#  elif defined(__clang__) && __LITTLE_ENDIAN__
+#  elif defined(__clang__) & defined(__LITTLE_ENDIAN__)
 #    define __LITTLE_ENDIAN 1234
 #  elif defined(_MSC_VER) && (_M_AMD64 || _M_IX86)
 #    define __LITTLE_ENDIAN 1234
diff --git a/lib/common/cmp_data_types.c b/lib/common/cmp_data_types.c
index f548702..11ebe41 100644
--- a/lib/common/cmp_data_types.c
+++ b/lib/common/cmp_data_types.c
@@ -26,6 +26,12 @@
 #include "cmp_data_types.h"
 
 
+#ifdef __BIG_ENDIAN
+#  define CMP_IS_BIG_ENDIAN 1
+#else
+#  define CMP_IS_BIG_ENDIAN 0
+#endif
+
 /**
  * @brief get the collection timestamp from the collection header
  *
@@ -885,7 +891,9 @@ int be_to_cpu_data_type(void *data, uint32_t data_size_byte, enum cmp_data_type
 	}
 	samples = data_size_byte / sample_size;
 
-#ifdef __LITTLE_ENDIAN
+	if (CMP_IS_BIG_ENDIAN)
+		return 0;
+
 	switch (data_type) {
 	case DATA_TYPE_IMAGETTE:
 	case DATA_TYPE_IMAGETTE_ADAPTIVE:
@@ -949,7 +957,6 @@ int be_to_cpu_data_type(void *data, uint32_t data_size_byte, enum cmp_data_type
 		return -1;
 	/* LCOV_EXCL_STOP */
 	}
-#endif /* __LITTLE_ENDIAN */
 
 	return 0;
 }
diff --git a/lib/common/cmp_entity.c b/lib/common/cmp_entity.c
index 73f9167..056b41f 100644
--- a/lib/common/cmp_entity.c
+++ b/lib/common/cmp_entity.c
@@ -2174,11 +2174,13 @@ static time_t my_timegm(struct tm *tm)
 uint64_t cmp_ent_create_timestamp(const struct timespec *ts)
 {
 	struct tm epoch_date = PLATO_EPOCH_DATE;
-	struct timespec epoch = { my_timegm(&epoch_date), 0 };
+	struct timespec epoch = {0, 0 };
 	struct timespec now = { 0, 0 };
 	double seconds;
 	uint64_t coarse, fine;
 
+	epoch.tv_sec = my_timegm(&epoch_date);
+
 	/* LCOV_EXCL_START */
 	/* if time cannot be represented as a time_t object epoch.tv_sec = -1 */
 	if (epoch.tv_sec == -1)
diff --git a/lib/decompress/cmp_max_used_bits_list.c b/lib/decompress/cmp_max_used_bits_list.c
index f4844ff..31c14b2 100644
--- a/lib/decompress/cmp_max_used_bits_list.c
+++ b/lib/decompress/cmp_max_used_bits_list.c
@@ -24,6 +24,7 @@
 
 #include "../common/list.h"
 #include "../common/cmp_max_used_bits.h"
+#include "cmp_max_used_bits_list.h"
 
 
 struct list_item {
diff --git a/lib/decompress/decmp.c b/lib/decompress/decmp.c
index e212350..27fde09 100644
--- a/lib/decompress/decmp.c
+++ b/lib/decompress/decmp.c
@@ -33,6 +33,7 @@
 
 #include "read_bitstream.h"
 #include "cmp_max_used_bits_list.h"
+#include "decmp.h"
 #include "../common/cmp_debug.h"
 #include "../common/cmp_support.h"
 #include "../common/cmp_entity.h"
@@ -400,7 +401,7 @@ static void configure_decoder_setup(struct decoder_setup *setup, struct bit_deco
  * @returns pointer to the collection data; NULL if col is NULL
  */
 
-void *get_collection_data(void *col)
+static void *get_collection_data(void *col)
 {
 	if (col)
 		col = (uint8_t *)col + COLLECTION_HDR_SIZE;
diff --git a/lib/icu_compress/cmp_icu.c b/lib/icu_compress/cmp_icu.c
index 36837c6..2291927 100644
--- a/lib/icu_compress/cmp_icu.c
+++ b/lib/icu_compress/cmp_icu.c
@@ -109,7 +109,7 @@ enum chunk_type {
 	CHUNK_TYPE_SAT_IMAGETTE,
 	CHUNK_TYPE_OFFSET_BACKGROUND, /* N-CAM */
 	CHUNK_TYPE_SMEARING,
-	CHUNK_TYPE_F_CHAIN,
+	CHUNK_TYPE_F_CHAIN
 };
 
 
diff --git a/lib/rdcu_compress/cmp_rdcu.c b/lib/rdcu_compress/cmp_rdcu.c
index 7feeda4..6bfbffb 100644
--- a/lib/rdcu_compress/cmp_rdcu.c
+++ b/lib/rdcu_compress/cmp_rdcu.c
@@ -34,6 +34,7 @@
 #include "cmp_rdcu_cfg.h"
 #include "rdcu_ctrl.h"
 #include "rdcu_rmap.h"
+#include "cmp_rdcu.h"
 
 
 #define RDCU_INTR_SIG_ENA 1 /* RDCU interrupt signal enabled */
diff --git a/lib/rdcu_compress/rmap.c b/lib/rdcu_compress/rmap.c
index 2361329..1ef7811 100644
--- a/lib/rdcu_compress/rmap.c
+++ b/lib/rdcu_compress/rmap.c
@@ -571,7 +571,7 @@ struct rmap_pkt *rmap_pkt_from_buffer(uint8_t *buf, uint32_t len)
 		size_t pkt_size = RMAP_DATA_START + n + pkt->data_len + 1; /* +1 for data CRC */
 
 		if (len < pkt_size) {
-			debug_print("buffer len is smaller than the contained RMAP packet; buf len: %" PRIu32 " bytes vs RMAP: %zu bytes needed",
+			debug_print("buffer len is smaller than the contained RMAP packet; buf len: %" PRIu32 " bytes vs RMAP: %lu bytes needed",
 				len, pkt_size);
 			goto error;
 		}
diff --git a/meson.build b/meson.build
index 86680ee..0bc9844 100644
--- a/meson.build
+++ b/meson.build
@@ -4,7 +4,7 @@ project('cmp_tool', 'c',
   license : 'GPL-2.0',
   default_options : [
     'warning_level=3',
-    'c_std=gnu99'
+    'c_std=gnu89'
   ]
 )
 
diff --git a/programs/cmp_io.c b/programs/cmp_io.c
index fd11008..da755f7 100644
--- a/programs/cmp_io.c
+++ b/programs/cmp_io.c
@@ -1201,7 +1201,7 @@ static __inline ssize_t str2uint8_arr(const char *str, uint8_t *data, uint32_t b
 			if (!data)  /* finished counting the sample */
 				break;
 
-			fprintf(stderr, "%s: %s: Error: The files do not contain enough data. Expected: 0x%x, has 0x%zx.\n",
+			fprintf(stderr, "%s: %s: Error: The files do not contain enough data. Expected: 0x%x, has 0x%lx.\n",
 				PROGRAM_NAME, file_name, buf_size, i);
 			return -1;
 		}
@@ -1459,7 +1459,7 @@ ssize_t read_file_cmp_entity(const char *file_name, struct cmp_entity *ent,
 			return -1;
 		}
 		if (size != (ssize_t)cmp_ent_get_size(ent)) {
-			fprintf(stderr, "%s: %s: The size of the compression entity set in the header of the compression entity is not the same size as the read-in file has. Expected: 0x%x, has 0x%zx.\n",
+			fprintf(stderr, "%s: %s: The size of the compression entity set in the header of the compression entity is not the same size as the read-in file has. Expected: 0x%x, has 0x%lx.\n",
 				PROGRAM_NAME, file_name, cmp_ent_get_size(ent), (size_t)size);
 			return -1;
 		}
diff --git a/programs/cmp_tool.c b/programs/cmp_tool.c
index 3dd5a9a..8f77e62 100644
--- a/programs/cmp_tool.c
+++ b/programs/cmp_tool.c
@@ -76,7 +76,7 @@ enum {
 	LAST_INFO,
 	NO_HEADER,
 	MODEL_ID,
-	MODEL_COUTER,
+	MODEL_COUTER
 };
 
 static const struct option long_options[] = {
-- 
GitLab