retop(b);
return 0;
}
-
-#ifdef BITMAP_TEST
-
-/* main() test against OpenSSL BN */
-#include <err.h>
-#include <openssl/bn.h>
-#include <stdio.h>
-#include <stdarg.h>
-
-#define LIM 131
-#define FAIL(...) fail(__FILE__, __LINE__, __VA_ARGS__)
-
-void
-bitmap_dump(struct bitmap *b, FILE *f)
-{
- size_t i;
-
- fprintf(f, "bitmap %p len=%zu top=%zu d =", b, b->len, b->top);
- for (i = 0; i < b->len; i++) {
- fprintf(f, " %0*llx", (int)BITMAP_BITS / 4,
- (unsigned long long)b->d[i]);
- }
- fputc('\n', f);
-}
-
-static void
-fail(char *file, int line, char *fmt, ...)
-{
- va_list ap;
-
- fprintf(stderr, "%s:%d ", file, line);
- va_start(ap, fmt);
- vfprintf(stderr, fmt, ap);
- va_end(ap);
- fputc('\n', stdout);
- /* abort(); */
- exit(1);
-}
-
-static void
-dump(const char *s, const u_char *buf, size_t l)
-{
- size_t i;
-
- fprintf(stderr, "%s len %zu = ", s, l);
- for (i = 0; i < l; i++)
- fprintf(stderr, "%02x ", buf[i]);
- fputc('\n', stderr);
-}
-
-int main(void) {
- struct bitmap *b = bitmap_new();
- BIGNUM *bn = BN_new();
- size_t len;
- int i, j, k, n;
- u_char bbuf[1024], bnbuf[1024];
- int r;
-
- for (i = -1; i < LIM; i++) {
- fputc('i', stdout);
- fflush(stdout);
- for (j = -1; j < LIM; j++) {
- for (k = -1; k < LIM; k++) {
- bitmap_zero(b);
- BN_clear(bn);
- /* Test setting bits */
- if (i > 0 && bitmap_set_bit(b, i) != 0)
- FAIL("bitmap_set_bit %d fail", i);
- if (j > 0 && bitmap_set_bit(b, j) != 0)
- FAIL("bitmap_set_bit %d fail", j);
- if (k > 0 && bitmap_set_bit(b, k) != 0)
- FAIL("bitmap_set_bit %d fail", k);
- if ((i > 0 && BN_set_bit(bn, i) != 1) ||
- (j > 0 && BN_set_bit(bn, j) != 1) ||
- (k > 0 && BN_set_bit(bn, k) != 1))
- FAIL("BN_set_bit fail");
- for (n = 0; n < LIM; n++) {
- if (BN_is_bit_set(bn, n) !=
- bitmap_test_bit(b, n)) {
- FAIL("miss %d/%d/%d %d "
- "%d %d", i, j, k, n,
- BN_is_bit_set(bn, n),
- bitmap_test_bit(b, n));
- }
- }
- /* Test length calculations */
- if (BN_num_bytes(bn) != (int)bitmap_nbytes(b)) {
- FAIL("bytes %d/%d/%d %d != %zu",
- i, j, k, BN_num_bytes(bn),
- bitmap_nbytes(b));
- }
- if (BN_num_bits(bn) != (int)bitmap_nbits(b)) {
- FAIL("bits %d/%d/%d %d != %zu",
- i, j, k, BN_num_bits(bn),
- bitmap_nbits(b));
- }
- /* Test serialisation */
- len = bitmap_nbytes(b);
- memset(bbuf, 0xfc, sizeof(bbuf));
- if (bitmap_to_string(b, bbuf,
- sizeof(bbuf)) != 0)
- FAIL("bitmap_to_string %d/%d/%d",
- i, j, k);
- for (n = len; n < (int)sizeof(bbuf); n++) {
- if (bbuf[n] != 0xfc)
- FAIL("bad string "
- "%d/%d/%d %d 0x%02x",
- i, j, k, n, bbuf[n]);
- }
- if ((r = BN_bn2bin(bn, bnbuf)) < 0)
- FAIL("BN_bn2bin %d/%d/%d",
- i, j, k);
- if ((size_t)r != len)
- FAIL("len bad %d/%d/%d", i, j, k);
- if (memcmp(bbuf, bnbuf, len) != 0) {
- dump("bbuf", bbuf, sizeof(bbuf));
- dump("bnbuf", bnbuf, sizeof(bnbuf));
- FAIL("buf bad %d/%d/%d", i, j, k);
- }
- /* Test deserialisation */
- bitmap_zero(b);
- if (bitmap_from_string(b, bnbuf, len) != 0)
- FAIL("bitmap_to_string %d/%d/%d",
- i, j, k);
- for (n = 0; n < LIM; n++) {
- if (BN_is_bit_set(bn, n) !=
- bitmap_test_bit(b, n)) {
- FAIL("miss %d/%d/%d %d "
- "%d %d", i, j, k, n,
- BN_is_bit_set(bn, n),
- bitmap_test_bit(b, n));
- }
- }
- /* Test clearing bits */
- for (n = 0; n < LIM; n++) {
- if (bitmap_set_bit(b, n) != 0) {
- bitmap_dump(b, stderr);
- FAIL("bitmap_set_bit %d "
- "fail", n);
- }
- if (BN_set_bit(bn, n) != 1)
- FAIL("BN_set_bit fail");
- }
- if (i > 0) {
- bitmap_clear_bit(b, i);
- BN_clear_bit(bn, i);
- }
- if (j > 0) {
- bitmap_clear_bit(b, j);
- BN_clear_bit(bn, j);
- }
- if (k > 0) {
- bitmap_clear_bit(b, k);
- BN_clear_bit(bn, k);
- }
- for (n = 0; n < LIM; n++) {
- if (BN_is_bit_set(bn, n) !=
- bitmap_test_bit(b, n)) {
- bitmap_dump(b, stderr);
- FAIL("cmiss %d/%d/%d %d "
- "%d %d", i, j, k, n,
- BN_is_bit_set(bn, n),
- bitmap_test_bit(b, n));
- }
- }
- }
- }
- }
- fputc('\n', stdout);
- bitmap_free(b);
- BN_free(bn);
-
- return 0;
-}
-#endif