aboutsummaryrefslogtreecommitdiffstats
path: root/openbsc/src/libcommon
diff options
context:
space:
mode:
authorNeels Hofmeyr <nhofmeyr@sysmocom.de>2015-10-12 11:57:33 +0200
committerHolger Hans Peter Freyther <holger@moiji-mobile.com>2015-11-02 12:56:40 +0100
commitd48f057328cfb82b192d935325eb5af1162c0ecc (patch)
treee8e91969b6b2c5186b77572973fc222e35275c54 /openbsc/src/libcommon
parentfe60cfb1d63d1c3b61c12eee78308f7985c66c1d (diff)
libcommon: soak up three static functions.
Add new kitchen sink openbsc/utils.h and libcommon/utils.c to make three so far static functions public (so I can use them in the upcoming OAP code). A place to put them could have been the gprs_utils.h, but all general functions in there have a gprs_ prefix, and todo markings to move them away. All other libcommon headers are too specific, so I opened up this kitchen sink header. Replace the implementation of encode_big_endian() with a call to osmo_store64be_ext(). See comments. Apply the change in Makefiles and C files.
Diffstat (limited to 'openbsc/src/libcommon')
-rw-r--r--openbsc/src/libcommon/Makefile.am2
-rw-r--r--openbsc/src/libcommon/utils.c58
2 files changed, 59 insertions, 1 deletions
diff --git a/openbsc/src/libcommon/Makefile.am b/openbsc/src/libcommon/Makefile.am
index 75f40eea7..84c754452 100644
--- a/openbsc/src/libcommon/Makefile.am
+++ b/openbsc/src/libcommon/Makefile.am
@@ -6,4 +6,4 @@ noinst_LIBRARIES = libcommon.a
libcommon_a_SOURCES = bsc_version.c common_vty.c debug.c gsm_data.c \
gsm_data_shared.c socket.c talloc_ctx.c \
- gsm_subscriber_base.c
+ gsm_subscriber_base.c utils.c
diff --git a/openbsc/src/libcommon/utils.c b/openbsc/src/libcommon/utils.c
new file mode 100644
index 000000000..c47dcaee2
--- /dev/null
+++ b/openbsc/src/libcommon/utils.c
@@ -0,0 +1,58 @@
+/* OpenBSC kitchen sink */
+
+/* (C) 2015 by sysmocom s.m.f.c GmbH <info@sysmocom.de>
+ * All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
+ */
+
+#include <openbsc/utils.h>
+#include <osmocom/core/utils.h>
+#include <osmocom/core/bit64gen.h>
+
+/* Wishful thinking to generate a constant time compare */
+int constant_time_cmp(const uint8_t *exp, const uint8_t *rel, const int count)
+{
+ int x = 0, i;
+
+ for (i = 0; i < count; ++i)
+ x |= exp[i] ^ rel[i];
+
+ /* if x is zero, all data was identical */
+ return x? 1 : 0;
+}
+
+
+uint64_t decode_big_endian(const uint8_t *data, size_t data_len)
+{
+ uint64_t value = 0;
+
+ while (data_len > 0) {
+ value = (value << 8) + *data;
+ data += 1;
+ data_len -= 1;
+ }
+
+ return value;
+}
+
+uint8_t *encode_big_endian(uint64_t value, size_t data_len)
+{
+ static uint8_t buf[sizeof(uint64_t)];
+ OSMO_ASSERT(data_len <= ARRAY_SIZE(buf));
+ osmo_store64be_ext(value, buf, data_len);
+ return buf;
+}
+