tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 1 | #include "Bitvec.h"
|
| 2 |
|
| 3 | #include "Random.h"
|
| 4 |
|
| 5 | #include <assert.h>
|
| 6 | #include <stdio.h>
|
| 7 |
|
| 8 | #ifndef DEBUG
|
| 9 | #undef assert
|
| 10 | void assert ( bool )
|
| 11 | {
|
| 12 | }
|
| 13 | #endif
|
| 14 |
|
| 15 | //----------------------------------------------------------------------------
|
| 16 |
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 17 | void printbits ( const void * blob, int len )
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 18 | {
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 19 | const uint8_t * data = (const uint8_t *)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 20 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 21 | printf("[");
|
| 22 | for(int i = 0; i < len; i++)
|
| 23 | {
|
| 24 | unsigned char byte = data[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 25 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 26 | int hi = (byte >> 4);
|
| 27 | int lo = (byte & 0xF);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 28 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 29 | if(hi) printf("%01x",hi);
|
| 30 | else printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 31 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 32 | if(lo) printf("%01x",lo);
|
| 33 | else printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 34 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 35 | if(i != len-1) printf(" ");
|
| 36 | }
|
| 37 | printf("]");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 38 | }
|
| 39 |
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 40 | void printbits2 ( const uint8_t * k, int nbytes )
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 41 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 42 | printf("[");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 43 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 44 | for(int i = nbytes-1; i >= 0; i--)
|
| 45 | {
|
| 46 | uint8_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 47 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 48 | for(int j = 7; j >= 0; j--)
|
| 49 | {
|
| 50 | uint8_t c = (b & (1 << j)) ? '#' : ' ';
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 51 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 52 | putc(c,stdout);
|
| 53 | }
|
| 54 | }
|
| 55 | printf("]");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 56 | }
|
| 57 |
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 58 | void printhex32 ( const void * blob, int len )
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 59 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 60 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 61 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 62 | uint32_t * d = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 63 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 64 | printf("{ ");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 65 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 66 | for(int i = 0; i < len/4; i++)
|
| 67 | {
|
| 68 | printf("0x%08x, ",d[i]);
|
| 69 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 70 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 71 | printf("}");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 72 | }
|
| 73 |
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 74 | void printbytes ( const void * blob, int len )
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 75 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 76 | uint8_t * d = (uint8_t*)blob;
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 77 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 78 | printf("{ ");
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 79 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 80 | for(int i = 0; i < len; i++)
|
| 81 | {
|
| 82 | printf("0x%02x, ",d[i]);
|
| 83 | }
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 84 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 85 | printf(" };");
|
tanjent@gmail.com | babb553 | 2011-02-28 06:03:12 +0000 | [diff] [blame] | 86 | }
|
| 87 |
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 88 | void printbytes2 ( const void * blob, int len )
|
| 89 | {
|
| 90 | uint8_t * d = (uint8_t*)blob;
|
| 91 |
|
| 92 | for(int i = 0; i < len; i++)
|
| 93 | {
|
| 94 | printf("%02x ",d[i]);
|
| 95 | }
|
| 96 | }
|
| 97 |
|
| 98 | //-----------------------------------------------------------------------------
|
| 99 | // Bit-level manipulation
|
| 100 |
|
| 101 | // These two are from the "Bit Twiddling Hacks" webpage
|
| 102 |
|
| 103 | uint32_t popcount ( uint32_t v )
|
| 104 | {
|
| 105 | v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
|
| 106 | v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
|
aappleby@google.com | cc59216 | 2011-04-08 20:49:43 +0000 | [diff] [blame] | 107 | uint32_t c = ((v + ((v >> 4) & 0xF0F0F0F)) * 0x1010101) >> 24; // count
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 108 |
|
| 109 | return c;
|
| 110 | }
|
| 111 |
|
| 112 | uint32_t parity ( uint32_t v )
|
| 113 | {
|
| 114 | v ^= v >> 1;
|
| 115 | v ^= v >> 2;
|
| 116 | v = (v & 0x11111111U) * 0x11111111U;
|
| 117 | return (v >> 28) & 1;
|
| 118 | }
|
| 119 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 120 | //-----------------------------------------------------------------------------
|
| 121 |
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 122 | uint32_t getbit ( const void * block, int len, uint32_t bit )
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 123 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 124 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 125 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 126 | int byte = bit >> 3;
|
| 127 | bit = bit & 0x7;
|
| 128 |
|
| 129 | if(byte < len) return (b[byte] >> bit) & 1;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 130 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 131 | return 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 132 | }
|
| 133 |
|
tanjent@gmail.com | 603c878 | 2011-04-01 08:50:06 +0000 | [diff] [blame] | 134 | uint32_t getbit_wrap ( const void * block, int len, uint32_t bit )
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 135 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 136 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 137 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 138 | int byte = bit >> 3;
|
| 139 | bit = bit & 0x7;
|
| 140 |
|
| 141 | byte %= len;
|
| 142 |
|
| 143 | return (b[byte] >> bit) & 1;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 144 | }
|
| 145 |
|
| 146 | void setbit ( void * block, int len, uint32_t bit )
|
| 147 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 148 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 149 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 150 | int byte = bit >> 3;
|
| 151 | bit = bit & 0x7;
|
| 152 |
|
| 153 | if(byte < len) b[byte] |= (1 << bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 154 | }
|
| 155 |
|
| 156 | void setbit ( void * block, int len, uint32_t bit, uint32_t val )
|
| 157 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 158 | val ? setbit(block,len,bit) : clearbit(block,len,bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 159 | }
|
| 160 |
|
| 161 | void clearbit ( void * block, int len, uint32_t bit )
|
| 162 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 163 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 164 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 165 | int byte = bit >> 3;
|
| 166 | bit = bit & 0x7;
|
| 167 |
|
| 168 | if(byte < len) b[byte] &= ~(1 << bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 169 | }
|
| 170 |
|
| 171 | void flipbit ( void * block, int len, uint32_t bit )
|
| 172 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 173 | uint8_t * b = (uint8_t*)block;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 174 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 175 | int byte = bit >> 3;
|
| 176 | bit = bit & 0x7;
|
| 177 |
|
| 178 | if(byte < len) b[byte] ^= (1 << bit);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 179 | }
|
| 180 |
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 181 | // from the "Bit Twiddling Hacks" webpage
|
| 182 |
|
| 183 | int countbits ( uint32_t v )
|
| 184 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 185 | v = v - ((v >> 1) & 0x55555555); // reuse input as temporary
|
| 186 | v = (v & 0x33333333) + ((v >> 2) & 0x33333333); // temp
|
aappleby@google.com | cc59216 | 2011-04-08 20:49:43 +0000 | [diff] [blame] | 187 | int c = ((v + ((v >> 4) & 0xF0F0F0F)) * 0x1010101) >> 24; // count
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 188 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 189 | return c;
|
tanjent@gmail.com | f67ce94 | 2011-03-14 09:11:18 +0000 | [diff] [blame] | 190 | }
|
| 191 |
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 192 | //-----------------------------------------------------------------------------
|
| 193 |
|
| 194 | void lshift1 ( void * blob, int len, int c )
|
| 195 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 196 | int nbits = len*8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 197 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 198 | for(int i = nbits-1; i >= 0; i--)
|
| 199 | {
|
| 200 | setbit(blob,len,i,getbit(blob,len,i-c));
|
| 201 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 202 | }
|
| 203 |
|
| 204 |
|
| 205 | void lshift8 ( void * blob, int nbytes, int c )
|
| 206 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 207 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 208 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 209 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 210 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 211 | int b = c >> 3;
|
| 212 | c &= 7;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 213 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 214 | for(int i = nbytes-1; i >= b; i--)
|
| 215 | {
|
| 216 | k[i] = k[i-b];
|
| 217 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 218 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 219 | for(int i = b-1; i >= 0; i--)
|
| 220 | {
|
| 221 | k[i] = 0;
|
| 222 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 223 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 224 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 225 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 226 | for(int i = nbytes-1; i >= 0; i--)
|
| 227 | {
|
| 228 | uint8_t a = k[i];
|
| 229 | uint8_t b = (i == 0) ? 0 : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 230 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 231 | k[i] = (a << c) | (b >> (8-c));
|
| 232 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 233 | }
|
| 234 |
|
| 235 | void lshift32 ( void * blob, int len, int c )
|
| 236 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 237 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 238 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 239 | int nbytes = len;
|
| 240 | int ndwords = nbytes / 4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 241 |
|
aappleby@google.com | a27c281 | 2011-04-13 23:23:14 +0000 | [diff] [blame^] | 242 | uint32_t * k = reinterpret_cast<uint32_t*>(blob);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 243 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 244 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 245 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 246 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 247 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 248 | int b = c / 32;
|
| 249 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 250 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 251 | for(int i = ndwords-1; i >= b; i--)
|
| 252 | {
|
| 253 | k[i] = k[i-b];
|
| 254 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 255 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 256 | for(int i = b-1; i >= 0; i--)
|
| 257 | {
|
| 258 | k[i] = 0;
|
| 259 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 260 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 261 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 262 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 263 | for(int i = ndwords-1; i >= 0; i--)
|
| 264 | {
|
| 265 | uint32_t a = k[i];
|
| 266 | uint32_t b = (i == 0) ? 0 : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 267 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 268 | k[i] = (a << c) | (b >> (32-c));
|
| 269 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 270 | }
|
| 271 |
|
| 272 | //-----------------------------------------------------------------------------
|
| 273 |
|
| 274 | void rshift1 ( void * blob, int len, int c )
|
| 275 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 276 | int nbits = len*8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 277 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 278 | for(int i = 0; i < nbits; i++)
|
| 279 | {
|
| 280 | setbit(blob,len,i,getbit(blob,len,i+c));
|
| 281 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 282 | }
|
| 283 |
|
| 284 | void rshift8 ( void * blob, int nbytes, int c )
|
| 285 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 286 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 287 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 288 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 289 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 290 | int b = c >> 3;
|
| 291 | c &= 7;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 292 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 293 | for(int i = 0; i < nbytes-b; i++)
|
| 294 | {
|
| 295 | k[i] = k[i+b];
|
| 296 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 297 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 298 | for(int i = nbytes-b; i < nbytes; i++)
|
| 299 | {
|
| 300 | k[i] = 0;
|
| 301 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 302 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 303 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 304 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 305 | for(int i = 0; i < nbytes; i++)
|
| 306 | {
|
| 307 | uint8_t a = (i == nbytes-1) ? 0 : k[i+1];
|
| 308 | uint8_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 309 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 310 | k[i] = (a << (8-c) ) | (b >> c);
|
| 311 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 312 | }
|
| 313 |
|
| 314 | void rshift32 ( void * blob, int len, int c )
|
| 315 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 316 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 317 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 318 | int nbytes = len;
|
| 319 | int ndwords = nbytes / 4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 320 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 321 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 322 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 323 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 324 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 325 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 326 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 327 | int b = c / 32;
|
| 328 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 329 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 330 | for(int i = 0; i < ndwords-b; i++)
|
| 331 | {
|
| 332 | k[i] = k[i+b];
|
| 333 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 334 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 335 | for(int i = ndwords-b; i < ndwords; i++)
|
| 336 | {
|
| 337 | k[i] = 0;
|
| 338 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 339 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 340 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 341 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 342 | for(int i = 0; i < ndwords; i++)
|
| 343 | {
|
| 344 | uint32_t a = (i == ndwords-1) ? 0 : k[i+1];
|
| 345 | uint32_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 346 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 347 | k[i] = (a << (32-c) ) | (b >> c);
|
| 348 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 349 | }
|
| 350 |
|
| 351 | //-----------------------------------------------------------------------------
|
| 352 |
|
| 353 | void lrot1 ( void * blob, int len, int c )
|
| 354 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 355 | int nbits = len * 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 356 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 357 | for(int i = 0; i < c; i++)
|
| 358 | {
|
| 359 | uint32_t bit = getbit(blob,len,nbits-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 360 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 361 | lshift1(blob,len,1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 362 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 363 | setbit(blob,len,0,bit);
|
| 364 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 365 | }
|
| 366 |
|
| 367 | void lrot8 ( void * blob, int len, int c )
|
| 368 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 369 | int nbytes = len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 370 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 371 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 372 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 373 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 374 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 375 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 376 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 377 | int b = c / 8;
|
| 378 | c &= (8-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 379 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 380 | for(int j = 0; j < b; j++)
|
| 381 | {
|
| 382 | uint8_t t = k[nbytes-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 383 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 384 | for(int i = nbytes-1; i > 0; i--)
|
| 385 | {
|
| 386 | k[i] = k[i-1];
|
| 387 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 388 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 389 | k[0] = t;
|
| 390 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 391 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 392 | uint8_t t = k[nbytes-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 393 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 394 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 395 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 396 | for(int i = nbytes-1; i >= 0; i--)
|
| 397 | {
|
| 398 | uint8_t a = k[i];
|
| 399 | uint8_t b = (i == 0) ? t : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 400 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 401 | k[i] = (a << c) | (b >> (8-c));
|
| 402 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 403 | }
|
| 404 |
|
| 405 | void lrot32 ( void * blob, int len, int c )
|
| 406 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 407 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 408 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 409 | int nbytes = len;
|
| 410 | int ndwords = nbytes/4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 411 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 412 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 413 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 414 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 415 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 416 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 417 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 418 | int b = c / 32;
|
| 419 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 420 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 421 | for(int j = 0; j < b; j++)
|
| 422 | {
|
| 423 | uint32_t t = k[ndwords-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 424 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 425 | for(int i = ndwords-1; i > 0; i--)
|
| 426 | {
|
| 427 | k[i] = k[i-1];
|
| 428 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 429 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 430 | k[0] = t;
|
| 431 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 432 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 433 | uint32_t t = k[ndwords-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 434 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 435 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 436 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 437 | for(int i = ndwords-1; i >= 0; i--)
|
| 438 | {
|
| 439 | uint32_t a = k[i];
|
| 440 | uint32_t b = (i == 0) ? t : k[i-1];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 441 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 442 | k[i] = (a << c) | (b >> (32-c));
|
| 443 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 444 | }
|
| 445 |
|
| 446 | //-----------------------------------------------------------------------------
|
| 447 |
|
| 448 | void rrot1 ( void * blob, int len, int c )
|
| 449 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 450 | int nbits = len * 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 451 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 452 | for(int i = 0; i < c; i++)
|
| 453 | {
|
| 454 | uint32_t bit = getbit(blob,len,0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 455 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 456 | rshift1(blob,len,1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 457 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 458 | setbit(blob,len,nbits-1,bit);
|
| 459 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 460 | }
|
| 461 |
|
| 462 | void rrot8 ( void * blob, int len, int c )
|
| 463 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 464 | int nbytes = len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 465 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 466 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 467 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 468 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 469 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 470 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 471 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 472 | int b = c / 8;
|
| 473 | c &= (8-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 474 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 475 | for(int j = 0; j < b; j++)
|
| 476 | {
|
| 477 | uint8_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 478 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 479 | for(int i = 0; i < nbytes-1; i++)
|
| 480 | {
|
| 481 | k[i] = k[i+1];
|
| 482 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 483 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 484 | k[nbytes-1] = t;
|
| 485 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 486 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 487 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 488 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 489 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 490 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 491 | uint8_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 492 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 493 | for(int i = 0; i < nbytes; i++)
|
| 494 | {
|
| 495 | uint8_t a = (i == nbytes-1) ? t : k[i+1];
|
| 496 | uint8_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 497 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 498 | k[i] = (a << (8-c)) | (b >> c);
|
| 499 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 500 | }
|
| 501 |
|
| 502 | void rrot32 ( void * blob, int len, int c )
|
| 503 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 504 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 505 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 506 | int nbytes = len;
|
| 507 | int ndwords = nbytes/4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 508 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 509 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 510 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 511 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 512 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 513 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 514 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 515 | int b = c / 32;
|
| 516 | c &= (32-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 517 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 518 | for(int j = 0; j < b; j++)
|
| 519 | {
|
| 520 | uint32_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 521 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 522 | for(int i = 0; i < ndwords-1; i++)
|
| 523 | {
|
| 524 | k[i] = k[i+1];
|
| 525 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 526 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 527 | k[ndwords-1] = t;
|
| 528 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 529 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 530 | if(c == 0) return;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 531 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 532 | //----------
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 533 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 534 | uint32_t t = k[0];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 535 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 536 | for(int i = 0; i < ndwords; i++)
|
| 537 | {
|
| 538 | uint32_t a = (i == ndwords-1) ? t : k[i+1];
|
| 539 | uint32_t b = k[i];
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 540 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 541 | k[i] = (a << (32-c)) | (b >> c);
|
| 542 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 543 | }
|
| 544 |
|
| 545 | //-----------------------------------------------------------------------------
|
| 546 |
|
| 547 | uint32_t window1 ( void * blob, int len, int start, int count )
|
| 548 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 549 | int nbits = len*8;
|
| 550 | start %= nbits;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 551 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 552 | uint32_t t = 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 553 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 554 | for(int i = 0; i < count; i++)
|
| 555 | {
|
| 556 | setbit(&t,sizeof(t),i, getbit_wrap(blob,len,start+i));
|
| 557 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 558 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 559 | return t;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 560 | }
|
| 561 |
|
| 562 | uint32_t window8 ( void * blob, int len, int start, int count )
|
| 563 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 564 | int nbits = len*8;
|
| 565 | start %= nbits;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 566 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 567 | uint32_t t = 0;
|
| 568 | uint8_t * k = (uint8_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 569 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 570 | if(count == 0) return 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 571 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 572 | int c = start & (8-1);
|
| 573 | int d = start / 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 574 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 575 | for(int i = 0; i < 4; i++)
|
| 576 | {
|
| 577 | int ia = (i + d + 1) % len;
|
| 578 | int ib = (i + d + 0) % len;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 579 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 580 | uint32_t a = k[ia];
|
| 581 | uint32_t b = k[ib];
|
| 582 |
|
| 583 | uint32_t m = (a << (8-c)) | (b >> c);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 584 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 585 | t |= (m << (8*i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 586 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 587 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 588 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 589 | t &= ((1 << count)-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 590 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 591 | return t;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 592 | }
|
| 593 |
|
| 594 | uint32_t window32 ( void * blob, int len, int start, int count )
|
| 595 | {
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 596 | int nbits = len*8;
|
| 597 | start %= nbits;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 598 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 599 | assert((len & 3) == 0);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 600 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 601 | int ndwords = len / 4;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 602 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 603 | uint32_t * k = (uint32_t*)blob;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 604 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 605 | if(count == 0) return 0;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 606 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 607 | int c = start & (32-1);
|
| 608 | int d = start / 32;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 609 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 610 | if(c == 0) return (k[d] & ((1 << count) - 1));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 611 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 612 | int ia = (d + 1) % ndwords;
|
| 613 | int ib = (d + 0) % ndwords;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 614 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 615 | uint32_t a = k[ia];
|
| 616 | uint32_t b = k[ib];
|
| 617 |
|
| 618 | uint32_t t = (a << (32-c)) | (b >> c);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 619 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 620 | t &= ((1 << count)-1);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 621 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 622 | return t;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 623 | }
|
| 624 |
|
| 625 | //-----------------------------------------------------------------------------
|
| 626 |
|
| 627 | bool test_shift ( void )
|
| 628 | {
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 629 | Rand r(1123);
|
| 630 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 631 | int nbits = 64;
|
| 632 | int nbytes = nbits / 8;
|
| 633 | int reps = 10000;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 634 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 635 | for(int j = 0; j < reps; j++)
|
| 636 | {
|
| 637 | if(j % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 638 |
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 639 | uint64_t a = r.rand_u64();
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 640 | uint64_t b;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 641 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 642 | for(int i = 0; i < nbits; i++)
|
| 643 | {
|
| 644 | b = a; lshift1 (&b,nbytes,i); assert(b == (a << i));
|
| 645 | b = a; lshift8 (&b,nbytes,i); assert(b == (a << i));
|
| 646 | b = a; lshift32 (&b,nbytes,i); assert(b == (a << i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 647 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 648 | b = a; rshift1 (&b,nbytes,i); assert(b == (a >> i));
|
| 649 | b = a; rshift8 (&b,nbytes,i); assert(b == (a >> i));
|
| 650 | b = a; rshift32 (&b,nbytes,i); assert(b == (a >> i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 651 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 652 | b = a; lrot1 (&b,nbytes,i); assert(b == ROTL64(a,i));
|
| 653 | b = a; lrot8 (&b,nbytes,i); assert(b == ROTL64(a,i));
|
| 654 | b = a; lrot32 (&b,nbytes,i); assert(b == ROTL64(a,i));
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 655 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 656 | b = a; rrot1 (&b,nbytes,i); assert(b == ROTR64(a,i));
|
| 657 | b = a; rrot8 (&b,nbytes,i); assert(b == ROTR64(a,i));
|
| 658 | b = a; rrot32 (&b,nbytes,i); assert(b == ROTR64(a,i));
|
| 659 | }
|
| 660 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 661 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 662 | printf("PASS\n");
|
| 663 | return true;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 664 | }
|
| 665 |
|
| 666 | //-----------------------------------------------------------------------------
|
| 667 |
|
| 668 | template < int nbits >
|
| 669 | bool test_window2 ( void )
|
| 670 | {
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 671 | Rand r(83874);
|
| 672 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 673 | struct keytype
|
| 674 | {
|
| 675 | uint8_t bytes[nbits/8];
|
| 676 | };
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 677 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 678 | int nbytes = nbits / 8;
|
| 679 | int reps = 10000;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 680 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 681 | for(int j = 0; j < reps; j++)
|
| 682 | {
|
| 683 | if(j % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 684 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 685 | keytype k;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 686 |
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 687 | r.rand_p(&k,nbytes);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 688 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 689 | for(int start = 0; start < nbits; start++)
|
| 690 | {
|
| 691 | for(int count = 0; count < 32; count++)
|
| 692 | {
|
| 693 | uint32_t a = window1(&k,nbytes,start,count);
|
| 694 | uint32_t b = window8(&k,nbytes,start,count);
|
| 695 | uint32_t c = window(&k,nbytes,start,count);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 696 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 697 | assert(a == b);
|
| 698 | assert(a == c);
|
| 699 | }
|
| 700 | }
|
| 701 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 702 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 703 | printf("PASS %d\n",nbits);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 704 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 705 | return true;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 706 | }
|
| 707 |
|
| 708 | bool test_window ( void )
|
| 709 | {
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 710 | Rand r(48402);
|
| 711 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 712 | int reps = 10000;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 713 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 714 | for(int j = 0; j < reps; j++)
|
| 715 | {
|
| 716 | if(j % (reps/10) == 0) printf(".");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 717 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 718 | int nbits = 64;
|
| 719 | int nbytes = nbits / 8;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 720 |
|
aappleby@google.com | 7f20a31 | 2011-03-21 20:55:06 +0000 | [diff] [blame] | 721 | uint64_t x = r.rand_u64();
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 722 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 723 | for(int start = 0; start < nbits; start++)
|
| 724 | {
|
| 725 | for(int count = 0; count < 32; count++)
|
| 726 | {
|
| 727 | uint32_t a = (uint32_t)ROTR64(x,start);
|
| 728 | a &= ((1 << count)-1);
|
| 729 |
|
| 730 | uint32_t b = window1 (&x,nbytes,start,count);
|
| 731 | uint32_t c = window8 (&x,nbytes,start,count);
|
| 732 | uint32_t d = window32(&x,nbytes,start,count);
|
| 733 | uint32_t e = window (x,start,count);
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 734 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 735 | assert(a == b);
|
| 736 | assert(a == c);
|
| 737 | assert(a == d);
|
| 738 | assert(a == e);
|
| 739 | }
|
| 740 | }
|
| 741 | }
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 742 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 743 | printf("PASS 64\n");
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 744 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 745 | test_window2<8>();
|
| 746 | test_window2<16>();
|
| 747 | test_window2<24>();
|
| 748 | test_window2<32>();
|
| 749 | test_window2<40>();
|
| 750 | test_window2<48>();
|
| 751 | test_window2<56>();
|
| 752 | test_window2<64>();
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 753 |
|
tanjent@gmail.com | 6ffe010 | 2011-03-19 21:28:26 +0000 | [diff] [blame] | 754 | return true;
|
tanjent@gmail.com | 7e5c363 | 2010-11-02 00:50:04 +0000 | [diff] [blame] | 755 | }
|
| 756 |
|
| 757 | //-----------------------------------------------------------------------------
|