aboutsummaryrefslogtreecommitdiff
path: root/src/rps/rps-test_util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/rps/rps-test_util.c')
-rw-r--r--src/rps/rps-test_util.c174
1 files changed, 163 insertions, 11 deletions
diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c
index 869170a8ae..9a1dfe0d83 100644
--- a/src/rps/rps-test_util.c
+++ b/src/rps/rps-test_util.c
@@ -2,20 +2,18 @@
This file is part of GNUnet.
Copyright (C)
- GNUnet 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, or (at your
- option) any later version.
+ GNUnet 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.
GNUnet 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 GNUnet; see the file COPYING. If not, write to the
- Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
- Boston, MA 02110-1301, USA.
+ 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/>.
*/
/**
@@ -31,13 +29,26 @@
#include <inttypes.h>
-#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__)
+#define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)
#ifndef TO_FILE
#define TO_FILE
#endif /* TO_FILE */
#ifdef TO_FILE
+
+#define min(x,y) ((x) > (y) ? (y) : (x))
+
+/**
+ * @brief buffer for storing the unaligned bits for the next write
+ */
+static char buf_unaligned;
+
+/**
+ * @brief number of bits in unaligned buffer
+ */
+static unsigned num_bits_buf_unaligned;
+
void
to_file_ (const char *file_name, char *line)
{
@@ -109,6 +120,147 @@ to_file_ (const char *file_name, char *line)
"Unable to close file\n");
}
+void
+to_file_raw (const char *file_name, const char *buf, size_t size_buf)
+{
+ struct GNUNET_DISK_FileHandle *f;
+ size_t size_written;
+
+ if (NULL == (f = GNUNET_DISK_file_open (file_name,
+ GNUNET_DISK_OPEN_APPEND |
+ GNUNET_DISK_OPEN_WRITE |
+ GNUNET_DISK_OPEN_CREATE,
+ GNUNET_DISK_PERM_USER_READ |
+ GNUNET_DISK_PERM_USER_WRITE |
+ GNUNET_DISK_PERM_GROUP_READ |
+ GNUNET_DISK_PERM_OTHER_READ)))
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "Not able to open file %s\n",
+ file_name);
+ return;
+ }
+
+ size_written = GNUNET_DISK_file_write (f, buf, size_buf);
+ if (size_buf != size_written)
+ {
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "Unable to write to file! (Size: %u, size_written: %u)\n",
+ size_buf,
+ size_written);
+
+ if (GNUNET_YES != GNUNET_DISK_file_close (f))
+ LOG (GNUNET_ERROR_TYPE_WARNING,
+ "Unable to close file\n");
+
+ return;
+ }
+}
+
+void
+to_file_raw_unaligned (const char *file_name,
+ const char *buf,
+ size_t size_buf,
+ unsigned bits_needed)
+{
+ // TODO endianness!
+ GNUNET_assert (size_buf >= (bits_needed/8));
+ //if (0 == num_bits_buf_unaligned)
+ //{
+ // if (0 == (bits_needed % 8))
+ // {
+ // to_file_raw (file_name, buf, size_buf);
+ // return;
+ // }
+ // to_file_raw (file_name, buf, size_buf - 1);
+ // buf_unaligned = buf[size_buf - 1];
+ // num_bits_buf_unaligned = bits_needed % 8;
+ // return;
+ //}
+
+ char buf_write[size_buf + 1];
+ const unsigned bytes_iter = (0 != bits_needed % 8?
+ (bits_needed/8)+1:
+ bits_needed/8);
+ // TODO what if no iteration happens?
+ unsigned size_buf_write = 0;
+ buf_write[0] = buf_unaligned;
+ /* Iterate over input bytes */
+ for (unsigned i = 0; i < bytes_iter; i++)
+ {
+ /* Number of bits needed in this iteration - 8 for all except last iter */
+ unsigned num_bits_needed_iter;
+ /* Mask for bits to actually use */
+ unsigned mask_bits_needed_iter;
+ char byte_input;
+ /* Number of bits needed to align unaligned byte */
+ unsigned num_bits_to_align;
+ /* Number of bits that are to be moved */
+ unsigned num_bits_to_move;
+ /* Mask for bytes to be moved */
+ char mask_input_to_move;
+ /* Masked bits to be moved */
+ char bits_to_move;
+ /* The amount of bits needed to fit the bits to shift to the nearest spot */
+ unsigned distance_shift_bits;
+ /* Shifted bits on the move */
+ char bits_moving;
+ /* (unaligned) byte being filled with bits */
+ char byte_to_fill;
+ /* mask for needed bits of the input byte that have not been moved */
+ char mask_input_leftover;
+ /* needed bits of the input byte that have not been moved */
+ char byte_input_leftover;
+ unsigned num_bits_leftover;
+ unsigned num_bits_discard;
+ char byte_unaligned_new;
+
+ if ( (bits_needed - (i * 8)) <= 8)
+ {
+ /* last iteration */
+ num_bits_needed_iter = bits_needed - (i * 8);
+ }
+ else
+ {
+ num_bits_needed_iter = 8;
+ }
+ mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
+ byte_input = buf[i];
+ byte_input &= mask_bits_needed_iter;
+ num_bits_to_align = 8 - num_bits_buf_unaligned;
+ num_bits_to_move = min (num_bits_to_align, num_bits_needed_iter);
+ mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
+ bits_to_move = byte_input & mask_input_to_move;
+ distance_shift_bits = num_bits_buf_unaligned;
+ bits_moving = bits_to_move << distance_shift_bits;
+ byte_to_fill = buf_unaligned | bits_moving;
+ if (num_bits_buf_unaligned + num_bits_needed_iter > 8)
+ {
+ /* buf_unaligned was aligned by filling
+ * -> can be written to storage */
+ buf_write[i] = byte_to_fill;
+ size_buf_write++;
+
+ /* store the leftover, unaligned bits in buffer */
+ mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
+ byte_input_leftover = byte_input & mask_input_leftover;
+ num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
+ num_bits_discard = 8 - num_bits_needed_iter;
+ byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
+ buf_unaligned = byte_unaligned_new;
+ num_bits_buf_unaligned = num_bits_leftover % 8;
+ }
+ else
+ {
+ /* unaligned buffer still unaligned but 'fuller' */
+ buf_unaligned = byte_to_fill;
+ num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
+ }
+ }
+ to_file_raw (file_name, buf_write, size_buf_write);
+ LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
+}
+
char *
auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
{