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 869170a8a..9a1dfe0d8 100644
--- a/src/rps/rps-test_util.c
+++ b/src/rps/rps-test_util.c
@@ -2,20 +2,18 @@
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 3 Copyright (C)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software: you can redistribute it and/or modify it
6 it under the terms of the GNU General Public License as published 6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation; either version 3, or (at your 7 by the Free Software Foundation, either version 3 of the License,
8 option) any later version. 8 or (at your option) any later version.
9 9
10 GNUnet is distributed in the hope that it will be useful, but 10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of 11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details. 13 Affero General Public License for more details.
14 14
15 You should have received a copy of the GNU General Public License 15 You should have received a copy of the GNU Affero General Public License
16 along with GNUnet; see the file COPYING. If not, write to the 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/ 17*/
20 18
21/** 19/**
@@ -31,13 +29,26 @@
31 29
32#include <inttypes.h> 30#include <inttypes.h>
33 31
34#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)
35 33
36#ifndef TO_FILE 34#ifndef TO_FILE
37#define TO_FILE 35#define TO_FILE
38#endif /* TO_FILE */ 36#endif /* TO_FILE */
39 37
40#ifdef TO_FILE 38#ifdef TO_FILE
39
40#define min(x,y) ((x) > (y) ? (y) : (x))
41
42/**
43 * @brief buffer for storing the unaligned bits for the next write
44 */
45static char buf_unaligned;
46
47/**
48 * @brief number of bits in unaligned buffer
49 */
50static unsigned num_bits_buf_unaligned;
51
41void 52void
42to_file_ (const char *file_name, char *line) 53to_file_ (const char *file_name, char *line)
43{ 54{
@@ -109,6 +120,147 @@ to_file_ (const char *file_name, char *line)
109 "Unable to close file\n"); 120 "Unable to close file\n");
110} 121}
111 122
123void
124to_file_raw (const char *file_name, const char *buf, size_t size_buf)
125{
126 struct GNUNET_DISK_FileHandle *f;
127 size_t size_written;
128
129 if (NULL == (f = GNUNET_DISK_file_open (file_name,
130 GNUNET_DISK_OPEN_APPEND |
131 GNUNET_DISK_OPEN_WRITE |
132 GNUNET_DISK_OPEN_CREATE,
133 GNUNET_DISK_PERM_USER_READ |
134 GNUNET_DISK_PERM_USER_WRITE |
135 GNUNET_DISK_PERM_GROUP_READ |
136 GNUNET_DISK_PERM_OTHER_READ)))
137 {
138 LOG (GNUNET_ERROR_TYPE_WARNING,
139 "Not able to open file %s\n",
140 file_name);
141 return;
142 }
143
144 size_written = GNUNET_DISK_file_write (f, buf, size_buf);
145 if (size_buf != size_written)
146 {
147 LOG (GNUNET_ERROR_TYPE_WARNING,
148 "Unable to write to file! (Size: %u, size_written: %u)\n",
149 size_buf,
150 size_written);
151
152 if (GNUNET_YES != GNUNET_DISK_file_close (f))
153 LOG (GNUNET_ERROR_TYPE_WARNING,
154 "Unable to close file\n");
155
156 return;
157 }
158}
159
160void
161to_file_raw_unaligned (const char *file_name,
162 const char *buf,
163 size_t size_buf,
164 unsigned bits_needed)
165{
166 // TODO endianness!
167 GNUNET_assert (size_buf >= (bits_needed/8));
168 //if (0 == num_bits_buf_unaligned)
169 //{
170 // if (0 == (bits_needed % 8))
171 // {
172 // to_file_raw (file_name, buf, size_buf);
173 // return;
174 // }
175 // to_file_raw (file_name, buf, size_buf - 1);
176 // buf_unaligned = buf[size_buf - 1];
177 // num_bits_buf_unaligned = bits_needed % 8;
178 // return;
179 //}
180
181 char buf_write[size_buf + 1];
182 const unsigned bytes_iter = (0 != bits_needed % 8?
183 (bits_needed/8)+1:
184 bits_needed/8);
185 // TODO what if no iteration happens?
186 unsigned size_buf_write = 0;
187 buf_write[0] = buf_unaligned;
188 /* Iterate over input bytes */
189 for (unsigned i = 0; i < bytes_iter; i++)
190 {
191 /* Number of bits needed in this iteration - 8 for all except last iter */
192 unsigned num_bits_needed_iter;
193 /* Mask for bits to actually use */
194 unsigned mask_bits_needed_iter;
195 char byte_input;
196 /* Number of bits needed to align unaligned byte */
197 unsigned num_bits_to_align;
198 /* Number of bits that are to be moved */
199 unsigned num_bits_to_move;
200 /* Mask for bytes to be moved */
201 char mask_input_to_move;
202 /* Masked bits to be moved */
203 char bits_to_move;
204 /* The amount of bits needed to fit the bits to shift to the nearest spot */
205 unsigned distance_shift_bits;
206 /* Shifted bits on the move */
207 char bits_moving;
208 /* (unaligned) byte being filled with bits */
209 char byte_to_fill;
210 /* mask for needed bits of the input byte that have not been moved */
211 char mask_input_leftover;
212 /* needed bits of the input byte that have not been moved */
213 char byte_input_leftover;
214 unsigned num_bits_leftover;
215 unsigned num_bits_discard;
216 char byte_unaligned_new;
217
218 if ( (bits_needed - (i * 8)) <= 8)
219 {
220 /* last iteration */
221 num_bits_needed_iter = bits_needed - (i * 8);
222 }
223 else
224 {
225 num_bits_needed_iter = 8;
226 }
227 mask_bits_needed_iter = ((char) 1 << num_bits_needed_iter) - 1;
228 byte_input = buf[i];
229 byte_input &= mask_bits_needed_iter;
230 num_bits_to_align = 8 - num_bits_buf_unaligned;
231 num_bits_to_move = min (num_bits_to_align, num_bits_needed_iter);
232 mask_input_to_move = ((char) 1 << num_bits_to_move) - 1;
233 bits_to_move = byte_input & mask_input_to_move;
234 distance_shift_bits = num_bits_buf_unaligned;
235 bits_moving = bits_to_move << distance_shift_bits;
236 byte_to_fill = buf_unaligned | bits_moving;
237 if (num_bits_buf_unaligned + num_bits_needed_iter > 8)
238 {
239 /* buf_unaligned was aligned by filling
240 * -> can be written to storage */
241 buf_write[i] = byte_to_fill;
242 size_buf_write++;
243
244 /* store the leftover, unaligned bits in buffer */
245 mask_input_leftover = mask_bits_needed_iter & (~ mask_input_to_move);
246 byte_input_leftover = byte_input & mask_input_leftover;
247 num_bits_leftover = num_bits_needed_iter - num_bits_to_move;
248 num_bits_discard = 8 - num_bits_needed_iter;
249 byte_unaligned_new = byte_input_leftover >> num_bits_to_move;
250 buf_unaligned = byte_unaligned_new;
251 num_bits_buf_unaligned = num_bits_leftover % 8;
252 }
253 else
254 {
255 /* unaligned buffer still unaligned but 'fuller' */
256 buf_unaligned = byte_to_fill;
257 num_bits_buf_unaligned = (num_bits_buf_unaligned + bits_needed) % 8;
258 }
259 }
260 to_file_raw (file_name, buf_write, size_buf_write);
261 LOG (GNUNET_ERROR_TYPE_DEBUG, "\n");
262}
263
112char * 264char *
113auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key) 265auth_key_to_string (struct GNUNET_CRYPTO_AuthKey auth_key)
114{ 266{