Browse Source

stun: fix padd macros

The PADDED_TO_* macros did not work when the length was a multiple
of the pad value.
(FS#129)

Reported-by: Francesco Castellano
Andrei Pelinescu-Onciul 14 years ago
parent
commit
263ee122bc
1 changed files with 7 additions and 2 deletions
  1. 7 2
      ser_stun.h

+ 7 - 2
ser_stun.h

@@ -124,8 +124,13 @@ typedef unsigned long	ULONG_T;
 #define XOR				1
 #define TRANSACTION_ID	12
 
-#define PADDED_TO_FOUR(len) (len == 0) ? 0 : len + (PAD4 - len%PAD4)
-#define PADDED_TO_SIXTYFOUR(len) (len == 0) ? 0 : len + (PAD64 - len%PAD64)
+/** padd len to a multiple of sz.
+ *  sz must be a power of the form 2^k (e.g. 2, 4, 8, 16 ...)
+ */
+#define PADD_TO(len, sz)	(((len) + (sz)-1) & (~((sz) - 1)))
+
+#define PADDED_TO_FOUR(len) PADD_TO(len, 4)
+#define PADDED_TO_SIXTYFOUR(len) PADD_TO(len, 64)
 
 struct transaction_id {
 	UINT_T	magic_cookie;