fixeria has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/30574 )


Change subject: [WIP] gsm0808: add gsm0808_enc_speech_codec[_list]2()
......................................................................

[WIP] gsm0808: add gsm0808_enc_speech_codec[_list]2()

Change-Id: I199ffa0ba4a64813238519178155dfc767aa3975
---
M include/osmocom/gsm/gsm0808_utils.h
M src/gsm/gsm0808_utils.c
2 files changed, 61 insertions(+), 27 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/74/30574/1

diff --git a/include/osmocom/gsm/gsm0808_utils.h 
b/include/osmocom/gsm/gsm0808_utils.h
index 6abfeec..269ffe3 100644
--- a/include/osmocom/gsm/gsm0808_utils.h
+++ b/include/osmocom/gsm/gsm0808_utils.h
@@ -112,11 +112,16 @@

 uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
                                 const struct gsm0808_speech_codec *sc);
+       OSMO_DEPRECATED("use gsm0808_enc_speech_codec2() instead")
+int gsm0808_enc_speech_codec2(struct msgb *msg,
+                             const struct gsm0808_speech_codec *sc);
 int gsm0808_dec_speech_codec(struct gsm0808_speech_codec *sc,
                             const uint8_t *elem, uint8_t len);
 uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
-                                     const struct gsm0808_speech_codec_list
-                                     *scl);
+                                     const struct gsm0808_speech_codec_list 
*scl)
+       OSMO_DEPRECATED("use gsm0808_enc_speech_codec_list2() instead");
+int gsm0808_enc_speech_codec_list2(struct msgb *msg,
+                                  const struct gsm0808_speech_codec_list *scl);
 int gsm0808_dec_speech_codec_list(struct gsm0808_speech_codec_list *scl,
                                  const uint8_t *elem, uint8_t len);
 uint8_t gsm0808_enc_channel_type(struct msgb *msg,
diff --git a/src/gsm/gsm0808_utils.c b/src/gsm/gsm0808_utils.c
index f1569c9..96a97dc 100644
--- a/src/gsm/gsm0808_utils.c
+++ b/src/gsm/gsm0808_utils.c
@@ -213,10 +213,10 @@
        gsm48_decode_lai2(&lai, decoded);
 }

-/* Helper function for gsm0808_enc_speech_codec()
- * and gsm0808_enc_speech_codec_list() */
-static uint8_t enc_speech_codec(struct msgb *msg,
-                               const struct gsm0808_speech_codec *sc)
+/* Helper function for gsm0808_enc_speech_codec[_list]().
+ * Returns number of bytes appended; negative on error. */
+static int enc_speech_codec(struct msgb *msg,
+                           const struct gsm0808_speech_codec *sc)
 {
        /* See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
        uint8_t header = 0;
@@ -244,8 +244,7 @@
                break;
        default:
                /* Invalid codec type specified */
-               OSMO_ASSERT(false);
-               break;
+               return -EINVAL;
        }
 
        old_tail = msg->tail;
@@ -264,7 +263,6 @@
                msgb_put_u8(msg, header);
                msgb_put_u8(msg, sc->type);
        } else {
-               OSMO_ASSERT(sc->type < 0x0f);
                header |= sc->type;
                msgb_put_u8(msg, header);
        }
@@ -282,11 +280,13 @@
        case GSM0808_SCT_FR5:
        case GSM0808_SCT_HR4:
        case GSM0808_SCT_CSD:
-               OSMO_ASSERT((sc->cfg & 0xff00) == 0);
+               if (sc->cfg >> 8)
+                       return -EINVAL;
                msgb_put_u8(msg, (uint8_t) sc->cfg & 0xff);
                break;
        default:
-               OSMO_ASSERT(sc->cfg == 0);
+               if (sc->cfg != 0)
+                       return -EINVAL;
                break;
        }

@@ -296,27 +296,41 @@
 /*! Encode TS 08.08 Speech Codec IE
  *  \param[out] msg Message Buffer to which IE will be appended
  *  \param[in] sc Speech Codec to be encoded into IE
- *  \returns number of bytes appended to \a msg */
-uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
-                                const struct gsm0808_speech_codec *sc)
+ *  \returns number of bytes appended to \a msg; negative on error */
+int gsm0808_enc_speech_codec2(struct msgb *msg,
+                             const struct gsm0808_speech_codec *sc)
 {
        /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
-       uint8_t *old_tail;
        uint8_t *tlv_len;
+       int rc;

-       OSMO_ASSERT(msg);
-       OSMO_ASSERT(sc);
+       if (OSMO_UNLIKELY(msg == NULL || sc == NULL))
+               return -EINVAL;

        msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC);
        tlv_len = msgb_put(msg, 1);
-       old_tail = msg->tail;

-       enc_speech_codec(msg, sc);
+       rc = enc_speech_codec(msg, sc);
+       if (rc < 0)
+               return rc;

-       *tlv_len = (uint8_t) (msg->tail - old_tail);
+       *tlv_len = rc;
        return *tlv_len + 2;
 }

+/*! Deprecated: gsm0808_enc_speech_codec2() wrapper for backwards 
compatibility.
+ * Mimics the old behavior: crash on missing and/or invalid input. */
+uint8_t gsm0808_enc_speech_codec(struct msgb *msg,
+                                const struct gsm0808_speech_codec *sc)
+{
+       int rc;
+
+       rc = gsm0808_enc_speech_codec2(msg, sc);
+       OSMO_ASSERT(rc > 0);
+
+       return rc;
+}
+
 /*! Decode TS 08.08 Speech Codec IE
  *  \param[out] sc Caller-allocated memory for Speech Codec
  *  \param[in] elem IE value to be decoded
@@ -401,9 +415,9 @@
 /*! Encode TS 08.08 Speech Codec list
  *  \param[out] msg  Message Buffer to which IE is to be appended
  *  \param[in] scl Speech Codec List to be encoded into IE
- *  \returns number of bytes added to \a msg */
-uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
-                                     const struct gsm0808_speech_codec_list 
*scl)
+ *  \returns number of bytes added to \a msg; negative on error */
+int gsm0808_enc_speech_codec_list2(struct msgb *msg,
+                                  const struct gsm0808_speech_codec_list *scl)
 {
        /*! See also 3GPP TS 48.008 3.2.2.103 Speech Codec List */
        uint8_t *old_tail;
@@ -412,8 +426,8 @@
        uint8_t rc;
        unsigned int bytes_used = 0;

-       OSMO_ASSERT(msg);
-       OSMO_ASSERT(scl);
+       if (OSMO_UNLIKELY(msg == NULL || scl == NULL))
+               return -EINVAL;

        msgb_put_u8(msg, GSM0808_IE_SPEECH_CODEC_LIST);
        tlv_len = msgb_put(msg, 1);
@@ -421,15 +435,30 @@

        for (i = 0; i < scl->len; i++) {
                rc = enc_speech_codec(msg, &scl->codec[i]);
-               OSMO_ASSERT(rc >= 1);
+               if (rc < 1)
+                       return rc;
                bytes_used += rc;
-               OSMO_ASSERT(bytes_used <= 255);
+               if (bytes_used > 0xff)
+                       return -EOVERFLOW;
        }

        *tlv_len = (uint8_t) (msg->tail - old_tail);
        return *tlv_len + 2;
 }

+/*! Deprecated: gsm0808_enc_speech_codec_list2() wrapper for backwards 
compatibility.
+ * Mimics the old behavior: crash on missing and/or invalid input. */
+uint8_t gsm0808_enc_speech_codec_list(struct msgb *msg,
+                                     const struct gsm0808_speech_codec_list 
*scl)
+{
+       int rc;
+
+       rc = gsm0808_enc_speech_codec_list2(msg, scl);
+       OSMO_ASSERT(rc > 0);
+
+       return rc;
+}
+
 /*! Decode TS 08.08 Speech Codec list IE
  *  \param[out] scl Caller-provided memory to store codec list
  *  \param[in] elem IE value to be decoded

--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/30574
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I199ffa0ba4a64813238519178155dfc767aa3975
Gerrit-Change-Number: 30574
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <[email protected]>
Gerrit-MessageType: newchange

Reply via email to