123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- #include <jose/jose.h>
- #include <limits.h>
- #include <string.h>
- #include <assert.h>
- #define NBYTES(size) ((((unsigned long long) size) + 1ULL) / 8ULL)
- #ifdef __MINGW32__
- #define fprintf __mingw_fprintf
- #endif
- union encoding {
- uint32_t idx;
- uint8_t enc[4];
- };
- static inline void
- set(uint8_t *val, uint32_t idx)
- {
- const uint8_t n = 1 << (idx % 8);
- uint8_t *p = &val[idx / 8];
- #pragma omp atomic update
- *p |= n;
- }
- static inline bool
- get(uint8_t *val, uint32_t idx)
- {
- const uint8_t n = 1 << (idx % 8);
- uint8_t *p = &val[idx / 8];
- uint8_t v;
- #pragma omp atomic read
- v = *p;
- return (v & n) == n;
- }
- int
- main(int argc, char *argv[])
- {
- uint8_t *val = NULL;
-
- #pragma omp parallel for
- for (uint16_t i = 0; i <= UINT8_MAX; i++) {
- union encoding enc = { i };
- uint8_t dec[3] = {};
- assert(jose_b64_dec_buf(enc.enc, 1, dec, sizeof(dec)) == SIZE_MAX);
- }
-
- val = calloc(NBYTES(UINT32_MAX), sizeof(uint8_t));
- if (!val)
- return EXIT_FAILURE;
- #pragma omp parallel for shared(val)
- for (uint16_t i = 0; i <= UINT8_MAX; i++) {
- uint8_t dec[3] = { i };
- union encoding enc = {};
- assert(jose_b64_enc_buf(dec, 1, enc.enc, sizeof(enc.enc)) == 2);
- set(val, enc.idx);
- }
- #pragma omp parallel for shared(val)
- for (uint16_t i = 0; i <= UINT8_MAX; i++) {
- for (uint16_t j = 0; j <= UINT8_MAX; j++) {
- union encoding enc = { .enc = { i, j } };
- uint8_t dec[3] = {};
- size_t len = 0;
- len = get(val, enc.idx) ? 1 : SIZE_MAX;
- if (jose_b64_dec_buf(enc.enc, 2, dec, sizeof(dec)) != len) {
- fprintf(stderr, "{%hx,%hx}\"%c%c\" != %zu\n",
- i, j, enc.enc[0], enc.enc[1], len);
- assert(false);
- }
- }
- }
- free(val);
-
- val = calloc(NBYTES(UINT32_MAX), sizeof(uint8_t));
- if (!val)
- return EXIT_FAILURE;
- #pragma omp parallel for shared(val)
- for (uint16_t i = 0; i <= UINT8_MAX; i++) {
- for (uint16_t j = 0; j <= UINT8_MAX; j++) {
- uint8_t dec[3] = { i, j };
- union encoding enc = {};
- assert(jose_b64_enc_buf(dec, 2, enc.enc, sizeof(enc.enc)) == 3);
- set(val, enc.idx);
- }
- }
- #pragma omp parallel for shared(val)
- for (uint16_t i = 0; i <= UINT8_MAX; i++) {
- for (uint16_t j = 0; j <= UINT8_MAX; j++) {
- for (uint16_t k = 0; k <= UINT8_MAX; k++) {
- union encoding enc = { .enc = { i, j, k } };
- uint8_t dec[3] = {};
- size_t len = 0;
- len = get(val, enc.idx) ? 2 : SIZE_MAX;
- if (jose_b64_dec_buf(enc.enc, 3, dec, sizeof(dec)) != len) {
- fprintf(stderr, "{%hu,%hu,%hu}\"%c%c%c\" != %zu\n",
- i, j, k, enc.enc[0], enc.enc[1], enc.enc[2], len);
- assert(false);
- }
- }
- }
- }
- free(val);
-
- #if defined(_OPENMP) && _OPENMP >= 201107
- val = calloc(NBYTES(UINT32_MAX), sizeof(uint8_t));
- if (!val)
- return EXIT_FAILURE;
- #pragma omp parallel for shared(val)
- for (uint16_t i = 0; i <= UINT8_MAX; i++) {
- for (uint16_t j = 0; j <= UINT8_MAX; j++) {
- for (uint16_t k = 0; k <= UINT8_MAX; k++) {
- uint8_t dec[3] = { i, j, k };
- union encoding enc = {};
- assert(jose_b64_enc_buf(dec, 3, enc.enc, sizeof(enc.enc)) == 4);
- set(val, enc.idx);
- }
- }
- }
- #pragma omp parallel for shared(val)
- for (uint16_t i = 0; i <= UINT8_MAX; i++) {
- for (uint16_t j = 0; j <= UINT8_MAX; j++) {
- for (uint16_t k = 0; k <= UINT8_MAX; k++) {
- for (uint16_t l = 0; l <= UINT8_MAX; l++) {
- union encoding enc = { .enc = { i, j, k, l } };
- uint8_t dec[3] = {};
- size_t len = 0;
- len = get(val, enc.idx) ? 3 : SIZE_MAX;
- if (jose_b64_dec_buf(enc.enc, 4, dec, sizeof(dec)) != len) {
- fprintf(stderr, "{%hu,%hu,%hu,%hu}\"%c%c%c%c\" != %zu\n",
- i, j, k, l, enc.enc[0], enc.enc[1], enc.enc[2], enc.enc[3], len);
- assert(false);
- }
- }
- }
- }
- }
- free(val);
- #endif
- return 0;
- }
|