changeset 512:4f0ab833bec0

libgsmhr1: add parameter validation function
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 25 Aug 2024 18:42:27 +0000
parents a5d61331b675
children 03a40ac2e931
files libgsmhr1/Makefile libgsmhr1/paramval_common.c libgsmhr1/tw_gsmhr.h
diffstat 3 files changed, 98 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/libgsmhr1/Makefile	Sun Aug 25 02:50:43 2024 +0000
+++ b/libgsmhr1/Makefile	Sun Aug 25 18:42:27 2024 +0000
@@ -1,6 +1,6 @@
 OBJS=	dhf_packed.o dhf_params.o enc_out_order.o mathdp31.o mathhalf.o \
-	pack_frame.o rtp_in.o rtp_in_direct.o sid_detect.o sid_reset.o sp_rom.o\
-	twts002_in.o twts002_out.o unpack_frame.o
+	pack_frame.o paramval_common.o rtp_in.o rtp_in_direct.o sid_detect.o \
+	sid_reset.o sp_rom.o twts002_in.o twts002_out.o unpack_frame.o
 HDRS=	enc_out_order.h mathdp31.h mathhalf.h namespace.h sp_rom.h tw_gsmhr.h \
 	typedefs.h
 LIB=	libgsmhr1.a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/libgsmhr1/paramval_common.c	Sun Aug 25 18:42:27 2024 +0000
@@ -0,0 +1,90 @@
+/*
+ * The function implemented in this module examines an array of 18 codec
+ * parameters in the int16_t representation used in this library, and checks
+ * if the unused upper bits of each int16_t word are cleared as they should be.
+ * This function, or higher-level wrappers around it, should be used when
+ * reading from ETSI-format *.cod and *.dec files, to guard against reading
+ * garbage or wrong endian.
+ */
+
+#include <stdint.h>
+#include "tw_gsmhr.h"
+
+int gsmhr_check_common_params(const int16_t *params)
+{
+	/* common parameters for all modes */
+	if (params[0] & 0xFFE0)
+		return -1;
+	if (params[1] & 0xF800)
+		return -1;
+	if (params[2] & 0xFE00)
+		return -1;
+	if (params[3] & 0xFF00)
+		return -1;
+	if (params[4] & 0xFFFE)
+		return -1;
+	if (params[5] & 0xFFFC)
+		return -1;
+	if (params[5]) {
+		/* voiced modes */
+		/* subframe 1 */
+		if (params[6] & 0xFF00)
+			return -1;
+		if (params[7] & 0xFE00)
+			return -1;
+		if (params[8] & 0xFFE0)
+			return -1;
+		/* subframe 2 */
+		if (params[9] & 0xFFF0)
+			return -1;
+		if (params[10] & 0xFE00)
+			return -1;
+		if (params[11] & 0xFFE0)
+			return -1;
+		/* subframe 3 */
+		if (params[12] & 0xFFF0)
+			return -1;
+		if (params[13] & 0xFE00)
+			return -1;
+		if (params[14] & 0xFFE0)
+			return -1;
+		/* subframe 4 */
+		if (params[15] & 0xFFF0)
+			return -1;
+		if (params[16] & 0xFE00)
+			return -1;
+		if (params[17] & 0xFFE0)
+			return -1;
+	} else {
+		/* unvoiced mode */
+		/* subframe 1 */
+		if (params[6] & 0xFF80)
+			return -1;
+		if (params[7] & 0xFF80)
+			return -1;
+		if (params[8] & 0xFFE0)
+			return -1;
+		/* subframe 2 */
+		if (params[9] & 0xFF80)
+			return -1;
+		if (params[10] & 0xFF80)
+			return -1;
+		if (params[11] & 0xFFE0)
+			return -1;
+		/* subframe 3 */
+		if (params[12] & 0xFF80)
+			return -1;
+		if (params[13] & 0xFF80)
+			return -1;
+		if (params[14] & 0xFFE0)
+			return -1;
+		/* subframe 4 */
+		if (params[15] & 0xFF80)
+			return -1;
+		if (params[16] & 0xFF80)
+			return -1;
+		if (params[17] & 0xFFE0)
+			return -1;
+	}
+	return 0;
+}
--- a/libgsmhr1/tw_gsmhr.h	Sun Aug 25 02:50:43 2024 +0000
+++ b/libgsmhr1/tw_gsmhr.h	Sun Aug 25 18:42:27 2024 +0000
@@ -51,6 +51,12 @@
 int gsmhr_rtp_in_direct(const uint8_t *rtp_in, unsigned rtp_in_len,
 			int16_t *param);
 
+/* reading parameter arrays from files: validation functions */
+
+int gsmhr_check_common_params(const int16_t *params);
+int gsmhr_check_encoder_params(const int16_t *params);
+int gsmhr_check_decoder_params(const int16_t *params);
+
 /* perfect SID detection and regeneration */
 
 int gsmhr_ts101318_is_perfect_sid(const uint8_t *payload);