Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 1 | #include <inttypes.h> |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <stdint.h> |
| 5 | #include <string.h> |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 6 | #include <time.h> |
| 7 | #include <stdbool.h> |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 8 | |
| 9 | #include <osmocom/core/utils.h> |
| 10 | #include <osmocom/core/bits.h> |
| 11 | |
| 12 | static const uint8_t input[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; |
| 13 | static const uint8_t exp_out[] = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 14 | static char s[18]; |
| 15 | |
| 16 | enum END {LE, BE}; |
| 17 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 18 | static inline const char * |
| 19 | end2str(enum END e) |
| 20 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 21 | if (e == LE) return "LE"; |
| 22 | return "BE"; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /* convenience wrappers */ |
| 27 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 28 | static inline uint64_t |
| 29 | load64(enum END e, const uint8_t *buf, unsigned nbytes) |
| 30 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 31 | return (e == BE) ? osmo_load64be_ext(buf, nbytes) : osmo_load64le_ext(buf, nbytes); |
| 32 | } |
| 33 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 34 | static inline uint32_t |
| 35 | load32(enum END e, const uint8_t *buf, unsigned nbytes) |
| 36 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 37 | return (e == BE) ? osmo_load32be_ext(buf, nbytes) : osmo_load32le_ext(buf, nbytes); |
| 38 | } |
| 39 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 40 | static inline uint16_t |
| 41 | load16(enum END e, const uint8_t *buf) |
| 42 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 43 | return (e == BE) ? osmo_load16be(buf) : osmo_load16le(buf); |
| 44 | } |
| 45 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 46 | static inline void |
| 47 | store64(enum END e, uint64_t t, uint8_t *buf, unsigned nbytes) |
| 48 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 49 | (e == BE) ? osmo_store64be_ext(t, buf, nbytes) : osmo_store64le_ext(t, buf, nbytes); |
| 50 | } |
| 51 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 52 | static inline void |
| 53 | store32(enum END e, uint64_t t, uint8_t *buf, unsigned nbytes) |
| 54 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 55 | (e == BE) ? osmo_store32be_ext(t, buf, nbytes) : osmo_store32le_ext(t, buf, nbytes); |
| 56 | } |
| 57 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 58 | static inline void |
| 59 | store16(enum END e, uint64_t t, uint8_t *buf) |
| 60 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 61 | (e == BE) ? osmo_store16be(t, buf) : osmo_store16le(t, buf); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | /* helper functions */ |
| 66 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 67 | static inline bool |
| 68 | printcheck(bool chk, unsigned nbytes, enum END e, bool b) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 69 | { |
| 70 | if (!chk) { |
| 71 | printf("%u %s FAILED", nbytes * 8, end2str(e)); |
| 72 | return true; |
| 73 | } |
| 74 | printf("%u %s OK", nbytes * 8, end2str(e)); |
| 75 | return b; |
| 76 | } |
| 77 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 78 | static inline bool |
| 79 | dumpcheck(const char *dump, const char *s, unsigned nbytes, bool chk, enum END e, bool b) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 80 | { |
| 81 | bool x = printcheck(chk, nbytes, e, b); |
| 82 | if (!dump) return x; |
| 83 | |
| 84 | int m = memcmp(s, dump, nbytes); |
| 85 | if (0 == m) { |
| 86 | printf(", storage OK"); |
| 87 | return x; |
| 88 | } |
| 89 | printf(", [%d]", m); |
| 90 | |
| 91 | return true; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /* printcheckXX(): load/store 'test' and check against 'expected' value, compare to 'dump' buffer if given and print if necessary */ |
| 96 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 97 | static inline void |
| 98 | printcheck64(enum END e, unsigned nbytes, uint64_t test, uint64_t expected, const char *dump, bool print) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 99 | { |
| 100 | uint8_t buf[nbytes]; |
| 101 | |
| 102 | store64(e, test, buf, nbytes); |
| 103 | |
| 104 | char *s = osmo_hexdump_nospc(buf, nbytes); |
| 105 | uint64_t result = load64(e, buf, nbytes); |
| 106 | |
| 107 | print = dumpcheck(dump, s, nbytes, result == expected, e, print); |
| 108 | |
| 109 | if (print) |
| 110 | printf(": buffer %s known buffer %s loaded %.16" PRIx64 " expected %.16" PRIx64, s, dump, result, expected); |
| 111 | printf("\n"); |
| 112 | } |
| 113 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 114 | static inline void |
| 115 | printcheck32(enum END e, unsigned nbytes, uint32_t test, uint32_t expected, const char *dump, bool print) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 116 | { |
| 117 | uint8_t buf[nbytes]; |
| 118 | |
| 119 | store32(e, test, buf, nbytes); |
| 120 | |
| 121 | char *s = osmo_hexdump_nospc(buf, nbytes); |
| 122 | uint32_t result = load32(e, buf, nbytes); |
| 123 | |
| 124 | print = dumpcheck(dump, s, nbytes, result == expected, e, print); |
| 125 | |
| 126 | if (print) |
| 127 | printf(": buffer %s known buffer %s loaded %.8" PRIx32 " expected %.8" PRIx32, s, dump, result, expected); |
| 128 | printf("\n"); |
| 129 | } |
| 130 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 131 | static inline void |
| 132 | printcheck16(enum END e, uint32_t test, uint32_t expected, const char *dump, bool print) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 133 | { |
| 134 | uint8_t buf[2]; |
| 135 | |
| 136 | store16(e, test, buf); |
| 137 | |
| 138 | char *s = osmo_hexdump_nospc(buf, 2); |
| 139 | uint16_t result = load16(e, buf); |
| 140 | |
| 141 | print = dumpcheck(dump, s, 2, result == expected, e, print); |
| 142 | |
| 143 | if (print) |
| 144 | printf(": buffer %s known buffer %s loaded %.4" PRIx16 " expected %.4" PRIx16, s, dump, result, expected); |
| 145 | printf("\n"); |
| 146 | } |
| 147 | |
| 148 | |
| 149 | /* compute expected value - zero excessive bytes */ |
| 150 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 151 | static inline uint64_t |
| 152 | exp64(enum END e, unsigned nbytes, uint64_t value) |
| 153 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 154 | uint8_t adj = 64 - nbytes * 8; |
| 155 | uint64_t v = value << adj; |
| 156 | return (e == LE) ? v >> adj : v; |
| 157 | } |
| 158 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 159 | static inline uint32_t |
| 160 | exp32(enum END e, unsigned nbytes, uint32_t value) |
| 161 | { |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 162 | uint8_t adj = 32 - nbytes * 8; |
| 163 | uint32_t v = value << adj; |
| 164 | return (e == LE) ? v >> adj : v; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | /* run actual tests - if 'test' is 0 than generate random test value internally */ |
| 169 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 170 | static inline void |
| 171 | check64(uint64_t test, uint64_t expected, unsigned nbytes, enum END e) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 172 | { |
| 173 | bool print = true; |
| 174 | if (0 == test && 0 == expected) { |
| 175 | test = ((uint64_t)rand() << 32) + rand(); |
| 176 | expected = exp64(e, nbytes, test); |
| 177 | print = false; |
| 178 | } |
| 179 | snprintf(s, 17, "%.16" PRIx64, expected); |
| 180 | printcheck64(e, nbytes, test, expected, (BE == e) ? s : NULL, print); |
| 181 | } |
| 182 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 183 | static inline void |
| 184 | check32(uint32_t test, uint32_t expected, unsigned nbytes, enum END e) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 185 | { |
| 186 | bool print = true; |
| 187 | if (0 == test && 0 == expected) { |
| 188 | test = rand(); |
| 189 | expected = exp32(e, nbytes, test); |
| 190 | print = false; |
| 191 | } |
| 192 | snprintf(s, 17, "%.8" PRIx32, expected); |
| 193 | printcheck32(e, nbytes, test, expected, (BE == e) ? s : NULL, print); |
| 194 | } |
| 195 | |
Sylvain Munaut | 26ea706 | 2014-06-16 15:57:36 +0200 | [diff] [blame] | 196 | static inline void |
| 197 | check16(uint16_t test, enum END e) |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 198 | { |
| 199 | bool print = true; |
| 200 | if (0 == test) { |
| 201 | test = (uint16_t)rand(); |
| 202 | print = false; |
| 203 | } |
| 204 | snprintf(s, 17, "%.4" PRIx16, test); |
| 205 | printcheck16(e, test, test, (BE == e) ? s : NULL, print); |
| 206 | } |
| 207 | |
Max | e0a7d9e | 2016-06-17 17:58:52 +0200 | [diff] [blame] | 208 | static void sh_chk(const uint8_t *in, uint8_t len, unsigned int nib, bool r) |
| 209 | { |
| 210 | uint8_t x[len]; |
Neels Hofmeyr | 8319a67 | 2016-09-21 18:30:01 +0200 | [diff] [blame] | 211 | int bytes = nib/2 + (nib & 1); |
| 212 | OSMO_ASSERT(len >= bytes); |
| 213 | memset(x, 0xcc, len); |
Max | e0a7d9e | 2016-06-17 17:58:52 +0200 | [diff] [blame] | 214 | if (r) |
| 215 | osmo_nibble_shift_right(x, in, nib); |
| 216 | else |
| 217 | osmo_nibble_shift_left_unal(x, in, nib); |
| 218 | |
| 219 | printf("[%u] %s IN: %s, nibble %u:", len, r ? "R" : "L", |
| 220 | osmo_hexdump_nospc(in, len), nib); |
| 221 | /* do NOT combine those printfs: osmo_hexdump* use static buffer which |
| 222 | WILL screw things up in that case */ |
Neels Hofmeyr | 8319a67 | 2016-09-21 18:30:01 +0200 | [diff] [blame] | 223 | printf("\n OUT: %s\n", osmo_hexdump_nospc(x, bytes)); |
Max | e0a7d9e | 2016-06-17 17:58:52 +0200 | [diff] [blame] | 224 | } |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 225 | |
| 226 | int main(int argc, char **argv) |
| 227 | { |
| 228 | uint8_t out[ARRAY_SIZE(input)]; |
| 229 | unsigned int offs; |
| 230 | |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 231 | srand(time(NULL)); |
| 232 | |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 233 | for (offs = 0; offs < sizeof(out); offs++) { |
| 234 | uint8_t *start = out + offs; |
| 235 | uint8_t len = sizeof(out) - offs; |
| 236 | |
| 237 | memcpy(out, input, sizeof(out)); |
| 238 | |
| 239 | printf("INORDER: %s\n", osmo_hexdump(start, len)); |
| 240 | osmo_revbytebits_buf(start, len); |
| 241 | printf("REVERSED: %s\n", osmo_hexdump(start, len)); |
| 242 | if (memcmp(start, exp_out + offs, len)) { |
| 243 | printf("EXPECTED: %s\n", osmo_hexdump(exp_out+offs, len)); |
| 244 | fprintf(stderr, "REVERSED != EXPECTED!\n"); |
| 245 | exit(1); |
| 246 | } |
| 247 | printf("\n"); |
| 248 | } |
| 249 | |
Max | 5377701 | 2014-06-04 19:07:41 +0200 | [diff] [blame] | 250 | printf("checking byte packing...\n"); |
| 251 | |
| 252 | printf("running static tests...\n"); |
| 253 | |
| 254 | check64(0xDEADBEEFF00DCAFE, 0xDEADBEEFF00DCAFE, 8, BE); |
| 255 | check64(0xDEADBEEFF00DCAFE, 0xADBEEFF00DCAFE00, 7, BE); |
| 256 | check64(0xDEADBEEFF00DCAFE, 0xBEEFF00DCAFE0000, 6, BE); |
| 257 | check64(0xDEADBEEFF00DCAFE, 0xEFF00DCAFE000000, 5, BE); |
| 258 | |
| 259 | check64(0xDEADBEEFF00DCAFE, 0xDEADBEEFF00DCAFE, 8, LE); |
| 260 | check64(0xDEADBEEFF00DCAFE, 0x00ADBEEFF00DCAFE, 7, LE); |
| 261 | check64(0xDEADBEEFF00DCAFE, 0x0000BEEFF00DCAFE, 6, LE); |
| 262 | check64(0xDEADBEEFF00DCAFE, 0x000000EFF00DCAFE, 5, LE); |
| 263 | |
| 264 | check32(0xBABEFACE, 0xBABEFACE, 4, BE); |
| 265 | check32(0xBABEFACE, 0xBEFACE00, 3, BE); |
| 266 | |
| 267 | check32(0xBABEFACE, 0xBABEFACE, 4, LE); |
| 268 | check32(0xBABEFACE, 0x00BEFACE, 3, LE); |
| 269 | |
| 270 | check16(0xB00B, BE); |
| 271 | check16(0xB00B, LE); |
| 272 | |
| 273 | printf("running random tests...\n"); |
| 274 | |
| 275 | check64(0, 0, 8, BE); |
| 276 | check64(0, 0, 7, BE); |
| 277 | check64(0, 0, 6, BE); |
| 278 | check64(0, 0, 5, BE); |
| 279 | |
| 280 | check64(0, 0, 8, LE); |
| 281 | check64(0, 0, 7, LE); |
| 282 | check64(0, 0, 6, LE); |
| 283 | check64(0, 0, 5, LE); |
| 284 | |
| 285 | check32(0, 0, 4, BE); |
| 286 | check32(0, 0, 3, BE); |
| 287 | |
| 288 | check32(0, 0, 4, LE); |
| 289 | check32(0, 0, 3, LE); |
| 290 | |
| 291 | check16(0, BE); |
| 292 | check16(0, LE); |
| 293 | |
Max | e0a7d9e | 2016-06-17 17:58:52 +0200 | [diff] [blame] | 294 | printf("running nibble tests...\n"); |
| 295 | |
| 296 | const uint8_t in1[] = { 0xF0, 0x0D, 0xCA, 0xFE, 0xDE, 0xAD, 0xBE, 0xEF }, |
| 297 | in2[] = { 0xB0, 0x0B, 0xBA, 0xBE, 0xFA, 0xCE }; |
| 298 | |
| 299 | for (offs = 0; offs < 13; offs++) { |
| 300 | sh_chk(in1, ARRAY_SIZE(in1), offs, true); |
| 301 | sh_chk(in1, ARRAY_SIZE(in1), offs, false); |
Max | 4fd6023 | 2016-09-20 16:33:50 +0200 | [diff] [blame] | 302 | /* in2 is too short to shift left 12 nibbles */ |
Pau Espin Pedrol | c5c955c | 2018-05-16 17:07:27 +0200 | [diff] [blame] | 303 | if (offs < 12) { |
| 304 | sh_chk(in2, ARRAY_SIZE(in2), offs, true); |
Max | 4fd6023 | 2016-09-20 16:33:50 +0200 | [diff] [blame] | 305 | sh_chk(in2, ARRAY_SIZE(in2), offs, false); |
Pau Espin Pedrol | c5c955c | 2018-05-16 17:07:27 +0200 | [diff] [blame] | 306 | } |
Max | e0a7d9e | 2016-06-17 17:58:52 +0200 | [diff] [blame] | 307 | } |
Harald Welte | 712691d | 2011-09-01 14:47:31 +0200 | [diff] [blame] | 308 | return 0; |
| 309 | } |