summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHolger Hans Peter Freyther <holger@moiji-mobile.com>2016-01-30 10:54:43 +0100
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2016-01-30 10:54:43 +0100
commita9301a1a1f2ac0c73aff2075c2361d54a6fc8675 (patch)
treef12b465a403dc925bb279f138784594ae92ce1c7
parent912bc6ff5047f08bb55c1d08be5fe4485e37e5d6 (diff)
bitvec: Test and fix regression for C++->C conversion
bitvec_read_field/bitvec_write_field in the PCU used a C++ reference and when porting to C it was decided to pass the parameter by value and this lost the "back propagation" of the new index. Change the parameter to be an in/out parameter and this way do not have a silent semantic break in the osmo-pcu (where we copy the reference in csn.1 by value) and have a true compile failure. Add Max's simple test for bitvec_unhex function leaving the checking of bitvec_read_field and the side effect in the datastructure about the number of bits still open.
-rw-r--r--include/osmocom/core/bitvec.h4
-rw-r--r--src/bitvec.c18
-rw-r--r--tests/bitvec/bitvec_test.c16
-rw-r--r--tests/bitvec/bitvec_test.ok15
4 files changed, 42 insertions, 11 deletions
diff --git a/include/osmocom/core/bitvec.h b/include/osmocom/core/bitvec.h
index d64d69dc..a7e6fc4d 100644
--- a/include/osmocom/core/bitvec.h
+++ b/include/osmocom/core/bitvec.h
@@ -80,7 +80,7 @@ void bitvec_free(struct bitvec *bv);
int bitvec_unhex(struct bitvec *bv, const char *src);
unsigned int bitvec_pack(const struct bitvec *bv, uint8_t *buffer);
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);
+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);
/*! @} */
diff --git a/src/bitvec.c b/src/bitvec.c
index 46e83f36..b5d2c243 100644
--- a/src/bitvec.c
+++ b/src/bitvec.c
@@ -394,22 +394,22 @@ int bitvec_unhex(struct bitvec *bv, const char *src)
if (sscanf(src + i, "%1x", &val) < 1) {
return 1;
}
- bitvec_write_field(bv, write_index,val, 4);
+ bitvec_write_field(bv, &write_index, val, 4);
}
return 0;
}
/*! \brief read part of the vector
* \param[in] bv The boolean vector to work on
- * \param[in] read_index Where reading supposed to start in the vector
+ * \param[in,out] read_index Where reading supposed to start in the vector
* \param[in] len How many bits to read from vector
* \returns read bits or negative value on error
*/
-uint64_t bitvec_read_field(struct bitvec *bv, unsigned int read_index, unsigned int len)
+uint64_t bitvec_read_field(struct bitvec *bv, unsigned int *read_index, unsigned int len)
{
unsigned int i;
uint64_t ui = 0;
- bv->cur_bit = read_index;
+ bv->cur_bit = *read_index;
for (i = 0; i < len; i++) {
int bit = bitvec_get_bit_pos((const struct bitvec *)bv, bv->cur_bit);
@@ -419,21 +419,21 @@ uint64_t bitvec_read_field(struct bitvec *bv, unsigned int read_index, unsigned
ui |= ((uint64_t)1 << (len - i - 1));
bv->cur_bit++;
}
- read_index += len;
+ *read_index += len;
return ui;
}
/*! \brief write into the vector
* \param[in] bv The boolean vector to work on
- * \param[in] write_index Where writing supposed to start in the vector
+ * \param[in,out] write_index Where writing supposed to start in 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)
+int bitvec_write_field(struct bitvec *bv, unsigned int *write_index, uint64_t val, unsigned int len)
{
unsigned int i;
int rc;
- bv->cur_bit = write_index;
+ bv->cur_bit = *write_index;
for (i = 0; i < len; i++) {
int bit = 0;
if (val & ((uint64_t)1 << (len - i - 1)))
@@ -442,7 +442,7 @@ int bitvec_write_field(struct bitvec *bv, unsigned int write_index, uint64_t val
if (rc)
return rc;
}
- write_index += len;
+ *write_index += len;
return 0;
}
diff --git a/tests/bitvec/bitvec_test.c b/tests/bitvec/bitvec_test.c
index 624e3346..789df75a 100644
--- a/tests/bitvec/bitvec_test.c
+++ b/tests/bitvec/bitvec_test.c
@@ -55,8 +55,24 @@ static void test_byte_ops()
printf("=== end %s ===\n", __func__);
}
+static void test_unhex(const char *hex)
+{
+ struct bitvec b;
+ uint8_t d[64] = {0};
+ b.data = d;
+ b.data_len = sizeof(d);
+ b.cur_bit = 0;
+ printf("%d -=>\n", bitvec_unhex(&b, hex));
+ printf("%s\n%s\n", osmo_hexdump_nospc(d, 64), osmo_hexdump_nospc((const unsigned char *)hex, 23));
+}
+
int main(int argc, char **argv)
{
test_byte_ops();
+ test_unhex("48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b");
+ test_unhex("47240c00400000000000000079eb2ac9402b2b2b2b2b2b");
+ test_unhex("47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b");
+ test_unhex("DEADFACE000000000000000000000000000000BEEFFEED");
+ test_unhex("FFFFFAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
return 0;
}
diff --git a/tests/bitvec/bitvec_test.ok b/tests/bitvec/bitvec_test.ok
index 1f329aff..8d944bc4 100644
--- a/tests/bitvec/bitvec_test.ok
+++ b/tests/bitvec/bitvec_test.ok
@@ -1,2 +1,17 @@
=== start test_byte_ops ===
=== end test_byte_ops ===
+1 -=>
+48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+48282407a6a074227201000b2b2b2b2b2b2b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+1 -=>
+47240c00400000000000000079eb2ac9402b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+47240c00400000000000000079eb2ac9402b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+1 -=>
+47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+47283c367513ba333004242b2b2b2b2b2b2b2b2b2b2b2b0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+1 -=>
+deadface000000000000000000000000000000beeffeed0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+deadface000000000000000000000000000000beeffeed0000000000000000000000000000000000000000000000000000000000000000000000000000000000
+0 -=>
+fffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+fffffaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa