summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Josefsson <simon@josefsson.org>2008-08-29 14:51:37 +0200
committerSimon Josefsson <simon@josefsson.org>2008-08-29 14:51:37 +0200
commitca19d955ebaa690b5580bb8c18ca20ab5daf8fff (patch)
tree1b7360d5ccfd1d32eed7fb0cdae4e6f8b9530b9e
parent2477d329d432a5969750486f2ea7657f5c54abae (diff)
downloadgnulib-ca19d955ebaa690b5580bb8c18ca20ab5daf8fff.tar.gz
Add bitrotate module.
-rw-r--r--ChangeLog17
-rwxr-xr-xMODULES.html.sh1
-rw-r--r--lib/arctwo.c7
-rw-r--r--lib/bitrotate.h60
-rw-r--r--modules/bitrotate21
-rw-r--r--modules/bitrotate-tests10
-rw-r--r--modules/crypto/arctwo1
-rw-r--r--modules/crypto/gc-arctwo1
-rw-r--r--tests/test-bitrotate.c91
9 files changed, 205 insertions, 4 deletions
diff --git a/ChangeLog b/ChangeLog
index 35260b7757..be14c9523b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,20 @@
+2008-08-29 Simon Josefsson <simon@josefsson.org>
+
+ * MODULES.html.sh (Misc): Add bitrotate.
+
+ * modules/bitrotate: New file.
+
+ * lib/bitrotate.h: New file.
+
+ * modules/bitrotate-tests: New file.
+
+ * tests/test-bitrotate.c: New file.
+
+ * modules/crypto/gc-arctwo, modules/crypto/arctwo: Add dependency
+ on the bitrotate module.
+
+ * lib/arctwo.c: Use new bitrotate module.
+
2008-08-29 Jim Meyering <meyering@redhat.com>
bootstrap: merge changes from coreutils
diff --git a/MODULES.html.sh b/MODULES.html.sh
index 66b9924d46..e35b510ee6 100755
--- a/MODULES.html.sh
+++ b/MODULES.html.sh
@@ -2887,6 +2887,7 @@ func_all_modules ()
func_begin_table
func_module argp
func_module argz
+ func_module bitrotate
func_module byteswap
func_module exitfail
func_module error
diff --git a/lib/arctwo.c b/lib/arctwo.c
index ccaa4b6c28..baf3c615e5 100644
--- a/lib/arctwo.c
+++ b/lib/arctwo.c
@@ -1,5 +1,5 @@
/* arctwo.c --- The RC2 cipher as described in RFC 2268.
- * Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
+ * Copyright (C) 2003, 2004, 2005, 2006, 2008 Free Software Foundation, Inc.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
@@ -31,6 +31,8 @@
#include "arctwo.h"
+#include "bitrotate.h"
+
static const uint8_t arctwo_sbox[] = {
217, 120, 249, 196, 25, 221, 181, 237,
40, 233, 253, 121, 74, 160, 216, 157,
@@ -66,9 +68,6 @@ static const uint8_t arctwo_sbox[] = {
10, 166, 32, 104, 254, 127, 193, 173
};
-#define rotl16(x,n) (((x) << ((uint16_t)(n))) | ((x) >> (16 - (uint16_t)(n))))
-#define rotr16(x,n) (((x) >> ((uint16_t)(n))) | ((x) << (16 - (uint16_t)(n))))
-
/* C89 compliant way to cast 'char' to 'unsigned char'. */
static inline unsigned char
to_uchar (char ch)
diff --git a/lib/bitrotate.h b/lib/bitrotate.h
new file mode 100644
index 0000000000..f3b6a66477
--- /dev/null
+++ b/lib/bitrotate.h
@@ -0,0 +1,60 @@
+/* bitrotate.h - Rotate bits in integers
+ Copyright (C) 2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
+
+#ifndef _GL_BITROTATE_H
+#define _GL_BITROTATE_H
+
+#include <stdint.h>
+
+/* Given an unsigned 32-bit argument X, return the value corresponding
+ to rotating the bits N steps to the left. N must be between 1 and
+ 31 inclusive. */
+static inline uint32_t
+rotl32 (uint32_t x, int n)
+{
+ return ((x << n) | (x >> (32 - n))) & 0xFFFFFFFF;
+}
+
+/* Given an unsigned 32-bit argument X, return the value corresponding
+ to rotating the bits N steps to the right. N must be between 1 to
+ 31 inclusive.*/
+static inline uint32_t
+rotr32 (uint32_t x, int n)
+{
+ return ((x >> n) | (x << (32 - n))) & 0xFFFFFFFF;
+}
+
+/* Given an unsigned 16-bit argument X, return the value corresponding
+ to rotating the bits N steps to the left. N must be between 1 to
+ 15 inclusive. */
+static inline uint16_t
+rotl16 (uint16_t x, int n)
+{
+ return ((x << n) | (x >> (16 - n))) & 0xFFFFFFFF;
+}
+
+/* Given an unsigned 16-bit argument X, return the value corresponding
+ to rotating the bits N steps to the right. N must be in 1 to 15
+ inclusive. */
+static inline uint16_t
+rotr16 (uint16_t x, int n)
+{
+ return ((x >> n) | (x << (16 - n))) & 0xFFFFFFFF;
+}
+
+#endif /* _GL_BITROTATE_H */
diff --git a/modules/bitrotate b/modules/bitrotate
new file mode 100644
index 0000000000..df94a61c20
--- /dev/null
+++ b/modules/bitrotate
@@ -0,0 +1,21 @@
+Description:
+Rotate bits in 16 and 32 bit integers.
+
+Files:
+lib/bitrotate.h
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+lib_SOURCES += bitrotate.h
+
+Include:
+"bitrotate.h"
+
+License:
+LGPLv2+
+
+Maintainer:
+Simon Josefsson
diff --git a/modules/bitrotate-tests b/modules/bitrotate-tests
new file mode 100644
index 0000000000..405607a2bf
--- /dev/null
+++ b/modules/bitrotate-tests
@@ -0,0 +1,10 @@
+Files:
+tests/test-bitrotate.c
+
+Depends-on:
+
+configure.ac:
+
+Makefile.am:
+TESTS += test-bitrotate
+check_PROGRAMS += test-bitrotate
diff --git a/modules/crypto/arctwo b/modules/crypto/arctwo
index 502b471704..c6ac35fcf4 100644
--- a/modules/crypto/arctwo
+++ b/modules/crypto/arctwo
@@ -8,6 +8,7 @@ m4/arctwo.m4
Depends-on:
stdint
+bitrotate
configure.ac:
gl_ARCTWO
diff --git a/modules/crypto/gc-arctwo b/modules/crypto/gc-arctwo
index 479120afb8..b26540203c 100644
--- a/modules/crypto/gc-arctwo
+++ b/modules/crypto/gc-arctwo
@@ -10,6 +10,7 @@ m4/arctwo.m4
Depends-on:
stdint
crypto/gc
+bitrotate
configure.ac:
gl_GC_ARCTWO
diff --git a/tests/test-bitrotate.c b/tests/test-bitrotate.c
new file mode 100644
index 0000000000..619ed41d6b
--- /dev/null
+++ b/tests/test-bitrotate.c
@@ -0,0 +1,91 @@
+/* Test of <bitrotate.h> substitute.
+ Copyright (C) 2007-2008 Free Software Foundation, Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU 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 General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+/* Written by Simon Josefsson <simon@josefsson.org>, 2008. */
+
+#include <config.h>
+
+#include "bitrotate.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#define ASSERT(expr) \
+ do \
+ { \
+ if (!(expr)) \
+ { \
+ fprintf (stderr, "%s:%d: assertion failed\n", \
+ __FILE__, __LINE__); \
+ fflush (stderr); \
+ abort (); \
+ } \
+ } \
+ while (0)
+
+int
+main (void)
+{
+ ASSERT (rotl16 (43981, 1) == 22427);
+ ASSERT (rotl16 (43981, 2) == 44854);
+ ASSERT (rotl16 (43981, 3) == 24173);
+ ASSERT (rotl16 (43981, 4) == 48346);
+ ASSERT (rotl16 (43981, 5) == 31157);
+ ASSERT (rotl16 (43981, 6) == 62314);
+ ASSERT (rotl16 (43981, 7) == 59093);
+ ASSERT (rotl16 (43981, 8) == 52651);
+ ASSERT (rotl16 (43981, 9) == 39767);
+ ASSERT (rotl16 (43981, 10) == 13999);
+ ASSERT (rotl16 (43981, 11) == 27998);
+ ASSERT (rotl16 (43981, 12) == 55996);
+ ASSERT (rotl16 (43981, 13) == 46457);
+ ASSERT (rotl16 (43981, 14) == 27379);
+ ASSERT (rotl16 (43981, 15) == 54758);
+
+ ASSERT (rotl32 (2309737967U, 1) == 324508639U);
+ ASSERT (rotl32 (2309737967U, 2) == 649017278U);
+ ASSERT (rotl32 (2309737967U, 3) == 1298034556U);
+ ASSERT (rotl32 (2309737967U, 4) == 2596069112U);
+ ASSERT (rotl32 (2309737967U, 5) == 897170929U);
+ ASSERT (rotl32 (2309737967U, 6) == 1794341858U);
+ ASSERT (rotl32 (2309737967U, 7) == 3588683716U);
+ ASSERT (rotl32 (2309737967U, 8) == 2882400137U);
+ ASSERT (rotl32 (2309737967U, 9) == 1469832979U);
+ ASSERT (rotl32 (2309737967U, 10) == 2939665958U);
+ ASSERT (rotl32 (2309737967U, 11) == 1584364621U);
+ ASSERT (rotl32 (2309737967U, 12) == 3168729242U);
+ ASSERT (rotl32 (2309737967U, 13) == 2042491189U);
+ ASSERT (rotl32 (2309737967U, 14) == 4084982378U);
+ ASSERT (rotl32 (2309737967U, 15) == 3874997461U);
+ ASSERT (rotl32 (2309737967U, 16) == 3455027627U);
+ ASSERT (rotl32 (2309737967U, 17) == 2615087959U);
+ ASSERT (rotl32 (2309737967U, 18) == 935208623U);
+ ASSERT (rotl32 (2309737967U, 19) == 1870417246U);
+ ASSERT (rotl32 (2309737967U, 20) == 3740834492U);
+ ASSERT (rotl32 (2309737967U, 21) == 3186701689U);
+ ASSERT (rotl32 (2309737967U, 22) == 2078436083U);
+ ASSERT (rotl32 (2309737967U, 23) == 4156872166U);
+ ASSERT (rotl32 (2309737967U, 24) == 4018777037U);
+ ASSERT (rotl32 (2309737967U, 25) == 3742586779U);
+ ASSERT (rotl32 (2309737967U, 26) == 3190206263U);
+ ASSERT (rotl32 (2309737967U, 27) == 2085445231U);
+ ASSERT (rotl32 (2309737967U, 28) == 4170890462U);
+ ASSERT (rotl32 (2309737967U, 29) == 4046813629U);
+ ASSERT (rotl32 (2309737967U, 30) == 3798659963U);
+ ASSERT (rotl32 (2309737967U, 31) == 3302352631U);
+
+ return 0;
+}