Revert "drop (now) unused code"

This reverts commit 2b30cbdfa83c20045ef92f53e88441dda185591b.

Reason for revert: Older versions of osmo-msc were actually calling map_codec_to_pt().

Change-Id: Ifff31012b327d40ed0b1559d5cf4f320784a4061
Related: https://jenkins.osmocom.org/jenkins/job/Osmocom-build-tags-against-master/1792/console
diff --git a/src/libosmo-mgcp-client/mgcp_client.c b/src/libosmo-mgcp-client/mgcp_client.c
index bfe69e6..489ce69 100644
--- a/src/libosmo-mgcp-client/mgcp_client.c
+++ b/src/libosmo-mgcp-client/mgcp_client.c
@@ -105,6 +105,94 @@
 	return -1;
 }
 
+/* Check the ptmap for illegal mappings */
+static int check_ptmap(const struct ptmap *ptmap)
+{
+	/* Check if there are mappings that leave the IANA assigned dynamic
+	 * payload type range. Under normal conditions such mappings should
+	 * not occur */
+
+	/* Its ok to have a 1:1 mapping in the statically defined
+	 * range, this won't hurt */
+	if (ptmap->codec == ptmap->pt)
+		return 0;
+
+	if (ptmap->codec < 96 || ptmap->codec > 127)
+		goto error;
+	if (ptmap->pt < 96 || ptmap->pt > 127)
+		goto error;
+
+	return 0;
+error:
+	LOGP(DLMGCP, LOGL_ERROR,
+	     "ptmap contains illegal mapping: codec=%u maps to pt=%u\n",
+	     ptmap->codec, ptmap->pt);
+	return -1;
+}
+
+/*! Map a codec to a payload type.
+ *  \ptmap[in] payload pointer to payload type map with specified payload types.
+ *  \ptmap[in] ptmap_len length of the payload type map.
+ *  \ptmap[in] codec the codec for which the payload type should be looked up.
+ *  \returns assigned payload type */
+unsigned int map_codec_to_pt(const struct ptmap *ptmap, unsigned int ptmap_len,
+			     enum mgcp_codecs codec)
+{
+	unsigned int i;
+
+	/*! Note: If the payload type map is empty or the codec is not found
+	 *  in the map, then a 1:1 mapping is performed. If the codec falls
+	 *  into the statically defined range or if the mapping table isself
+	 *  tries to map to the statically defined range, then the mapping
+	 *  is also ignored and a 1:1 mapping is performed instead. */
+
+	/* we may return the codec directly since enum mgcp_codecs directly
+	 * corresponds to the statically assigned payload types */
+	if (codec < 96 || codec > 127)
+		return codec;
+
+	for (i = 0; i < ptmap_len; i++) {
+		/* Skip illegal map entries */
+		if (check_ptmap(ptmap) == 0 && ptmap->codec == codec)
+			return ptmap->pt;
+		ptmap++;
+	}
+
+	/* If nothing is found, do not perform any mapping */
+	return codec;
+}
+
+/*! Map a payload type to a codec.
+ *  \ptmap[in] payload pointer to payload type map with specified payload types.
+ *  \ptmap[in] ptmap_len length of the payload type map.
+ *  \ptmap[in] payload type for which the codec should be looked up.
+ *  \returns codec that corresponds to the specified payload type */
+enum mgcp_codecs map_pt_to_codec(struct ptmap *ptmap, unsigned int ptmap_len,
+				 unsigned int pt)
+{
+	unsigned int i;
+
+	/*! Note: If the payload type map is empty or the payload type is not
+	 *  found in the map, then a 1:1 mapping is performed. If the payload
+	 *  type falls into the statically defined range or if the mapping
+	 *  table isself tries to map to the statically defined range, then
+	 *  the mapping is also ignored and a 1:1 mapping is performed
+	 *  instead. */
+
+	/* See also note in map_codec_to_pt() */
+	if (pt < 96 || pt > 127)
+		return pt;
+
+	for (i = 0; i < ptmap_len; i++) {
+		if (check_ptmap(ptmap) == 0 && ptmap->pt == pt)
+			return ptmap->codec;
+		ptmap++;
+	}
+
+	/* If nothing is found, do not perform any mapping */
+	return pt;
+}
+
 static void _mgcp_client_conf_init(struct mgcp_client_conf *conf)
 {
 	/* NULL and -1 default to MGCP_CLIENT_*_DEFAULT values */