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.c133
1 files changed, 120 insertions, 13 deletions
diff --git a/src/rps/rps-test_util.c b/src/rps/rps-test_util.c
index ea55deac5..9a1dfe0d8 100644
--- a/src/rps/rps-test_util.c
+++ b/src/rps/rps-test_util.c
@@ -29,13 +29,26 @@
29 29
30#include <inttypes.h> 30#include <inttypes.h>
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind,"rps-sampler",__VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from(kind,"rps-test_util",__VA_ARGS__)
33 33
34#ifndef TO_FILE 34#ifndef TO_FILE
35#define TO_FILE 35#define TO_FILE
36#endif /* TO_FILE */ 36#endif /* TO_FILE */
37 37
38#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
39void 52void
40to_file_ (const char *file_name, char *line) 53to_file_ (const char *file_name, char *line)
41{ 54{
@@ -108,11 +121,10 @@ to_file_ (const char *file_name, char *line)
108} 121}
109 122
110void 123void
111to_file_raw (const char *file_name, void *buf, size_t size_buf) 124to_file_raw (const char *file_name, const char *buf, size_t size_buf)
112{ 125{
113 struct GNUNET_DISK_FileHandle *f; 126 struct GNUNET_DISK_FileHandle *f;
114 size_t size2; 127 size_t size_written;
115
116 128
117 if (NULL == (f = GNUNET_DISK_file_open (file_name, 129 if (NULL == (f = GNUNET_DISK_file_open (file_name,
118 GNUNET_DISK_OPEN_APPEND | 130 GNUNET_DISK_OPEN_APPEND |
@@ -129,13 +141,13 @@ to_file_raw (const char *file_name, void *buf, size_t size_buf)
129 return; 141 return;
130 } 142 }
131 143
132 size2 = GNUNET_DISK_file_write (f, buf, size_buf); 144 size_written = GNUNET_DISK_file_write (f, buf, size_buf);
133 if (size_buf != size2) 145 if (size_buf != size_written)
134 { 146 {
135 LOG (GNUNET_ERROR_TYPE_WARNING, 147 LOG (GNUNET_ERROR_TYPE_WARNING,
136 "Unable to write to file! (Size: %u, size2: %u)\n", 148 "Unable to write to file! (Size: %u, size_written: %u)\n",
137 size_buf, 149 size_buf,
138 size2); 150 size_written);
139 151
140 if (GNUNET_YES != GNUNET_DISK_file_close (f)) 152 if (GNUNET_YES != GNUNET_DISK_file_close (f))
141 LOG (GNUNET_ERROR_TYPE_WARNING, 153 LOG (GNUNET_ERROR_TYPE_WARNING,
@@ -143,15 +155,110 @@ to_file_raw (const char *file_name, void *buf, size_t size_buf)
143 155
144 return; 156 return;
145 } 157 }
158}
146 159
147 //if (512 < size_buf) 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)
148 //{ 169 //{
149 // GNUNET_free (output_buffer_p); 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;
150 //} 179 //}
151 180
152 //if (GNUNET_YES != GNUNET_DISK_file_close (f)) 181 char buf_write[size_buf + 1];
153 // LOG (GNUNET_ERROR_TYPE_WARNING, 182 const unsigned bytes_iter = (0 != bits_needed % 8?
154 // "Unable to close file\n"); 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");
155} 262}
156 263
157char * 264char *