aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTobias Brunner <tobias@strongswan.org>2013-10-12 01:01:06 +0200
committerTobias Brunner <tobias@strongswan.org>2013-10-17 10:25:34 +0200
commit812ae898bfa4917e2d69b17b0c3949bb2c2c2b18 (patch)
treef50f6ec9c8aba0490257d77401578815ff873c58
parent32fef0c6e9e934eef2e63286eef13ff491df5aaf (diff)
downloadstrongswan-812ae898bfa4917e2d69b17b0c3949bb2c2c2b18.tar.bz2
strongswan-812ae898bfa4917e2d69b17b0c3949bb2c2c2b18.tar.xz
utils: Add utility function to calculate padding length
-rw-r--r--src/libstrongswan/tests/test_utils.c9
-rw-r--r--src/libstrongswan/utils/utils.h28
2 files changed, 24 insertions, 13 deletions
diff --git a/src/libstrongswan/tests/test_utils.c b/src/libstrongswan/tests/test_utils.c
index 506b3a174..3ca0412b4 100644
--- a/src/libstrongswan/tests/test_utils.c
+++ b/src/libstrongswan/tests/test_utils.c
@@ -167,11 +167,18 @@ START_TEST(test_untoh)
END_TEST
/*******************************************************************************
- * round_up/down
+ * pad_len/round_up/down
*/
START_TEST(test_round)
{
+ ck_assert_int_eq(pad_len(0, 4), 0);
+ ck_assert_int_eq(pad_len(1, 4), 3);
+ ck_assert_int_eq(pad_len(2, 4), 2);
+ ck_assert_int_eq(pad_len(3, 4), 1);
+ ck_assert_int_eq(pad_len(4, 4), 0);
+ ck_assert_int_eq(pad_len(5, 4), 3);
+
ck_assert_int_eq(round_up(0, 4), 0);
ck_assert_int_eq(round_up(1, 4), 4);
ck_assert_int_eq(round_up(2, 4), 4);
diff --git a/src/libstrongswan/utils/utils.h b/src/libstrongswan/utils/utils.h
index 8fe741211..5c237a7f2 100644
--- a/src/libstrongswan/utils/utils.h
+++ b/src/libstrongswan/utils/utils.h
@@ -679,26 +679,30 @@ static inline u_int64_t untoh64(void *network)
}
/**
- * Round up size to be multiple of alignement
+ * Get the padding required to make size a multiple of alignment
*/
-static inline size_t round_up(size_t size, int alignement)
+static inline size_t pad_len(size_t size, size_t alignment)
{
- int remainder;
+ size_t remainder;
- remainder = size % alignement;
- if (remainder)
- {
- size += alignement - remainder;
- }
- return size;
+ remainder = size % alignment;
+ return remainder ? alignment - remainder : 0;
+}
+
+/**
+ * Round up size to be multiple of alignment
+ */
+static inline size_t round_up(size_t size, size_t alignment)
+{
+ return size + pad_len(size, alignment);
}
/**
- * Round down size to be a multiple of alignement
+ * Round down size to be a multiple of alignment
*/
-static inline size_t round_down(size_t size, int alignement)
+static inline size_t round_down(size_t size, size_t alignment)
{
- return size - (size % alignement);
+ return size - (size % alignment);
}
/**