Review at  https://gerrit.osmocom.org/4292

Expand bitvec_write_field() function

Previously only boolean values were allowed while in practice it's
sometimes necessary to use L/H. Allow user to specify this explicitly
and make old function into wrapper around extended version.

Change-Id: Iee648d764de9f3e7ef850f40864ad701c83f61db
Related: OS#1526
---
M include/osmocom/core/bitvec.h
M src/bitvec.c
2 files changed, 26 insertions(+), 4 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/92/4292/1

diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
index d4c7d68..f31eb96 100644
--- a/include/osmocom/core/bitvec.h
+++ b/include/osmocom/core/bitvec.h
@@ -70,6 +70,8 @@
 unsigned int bitvec_unpack(struct bitvec *bv, const uint8_t *buffer);
 uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, 
unsigned int len);
 int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t 
val, unsigned int len);
+int bitvec_write_field_ext(struct bitvec *bv, unsigned int *write_index, 
uint64_t val, unsigned int len,
+                          bool use_boolean);
 int bitvec_fill(struct bitvec *bv, unsigned int num_bits, enum bit_value fill);
 char bit_value_to_char(enum bit_value v);
 void bitvec_to_string_r(const struct bitvec *bv, char *str);
diff --git a/src/bitvec.c b/src/bitvec.c
index f07b42c..20aa31b 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -483,23 +483,43 @@
 /*! write into the vector
  *  \param[in] bv The boolean vector to work on
  *  \param[in,out] write_index Where writing supposed to start in the vector
+ *  \param[in] val value to be written to the vector
  *  \param[in] len How many bits to write
  *  \returns next write index or negative value on error
  */
 int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t 
val, unsigned int len)
 {
+       return bitvec_write_field_ext(bv, write_index, val, len, true);
+}
+
+/*! write L/H value into the vector
+ *  \param[in] bv The boolean vector to work on
+ *  \param[in,out] write_index Where writing supposed to start in the vector
+ *  \param[in] val value to be written to the vector
+ *  \param[in] len How many bits to write
+ *  \param[in] use_boolean write val as 0/1 if set, as L/H otherwise
+ *  \returns 0 in case of success or negative value on error
+ */
+int bitvec_write_field_ext(struct bitvec *bv, unsigned int *write_index, 
uint64_t val, unsigned int len,
+                          bool use_boolean)
+{
        unsigned int i;
-       int rc;
+
        bv->cur_bit = *write_index;
+
        for (i = 0; i < len; i++) {
-               int bit = 0;
+               int rc;
                if (val & ((uint64_t)1 << (len - i - 1)))
-                       bit = 1;
-               rc = bitvec_set_bit(bv, bit);
+                       rc = bitvec_set_bit(bv, use_boolean ? 1 : H);
+               else
+                       rc = bitvec_set_bit(bv, use_boolean ? 0 : L);
+
                if (rc)
                        return rc;
        }
+
        *write_index += len;
+
        return 0;
 }
 

-- 
To view, visit https://gerrit.osmocom.org/4292
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iee648d764de9f3e7ef850f40864ad701c83f61db
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Max <[email protected]>

Reply via email to