Commit dbca2316 authored by Marek Vavruša's avatar Marek Vavruša

contrib: created 'contrib' directory with ccan/isaac as csprng

previously cryptolib random function was used to generate message id,
this works well but it is slow especially when the entropy is low,
replaced with cryptographically safe prng ISAAC

the ccan directory is going to be used in the future, as it's include
structure makes it easy to embed C snippets instead of reimplementing
them
parent b27133ce
......@@ -12,7 +12,7 @@ MODULEDIR := $(LIBDIR)/kdns_modules
# Tools
CC ?= cc
CFLAGS += -std=c99 -D_GNU_SOURCE -Wall -fPIC -I$(abspath .) -I$(abspath lib/generic)
CFLAGS += -std=c99 -D_GNU_SOURCE -Wall -fPIC -I$(abspath .) -I$(abspath lib/generic) -I$(abspath contrib)
CFLAGS += -DPACKAGE_VERSION="\"$(MAJOR).$(MINOR)\"" -DPREFIX="\"$(PREFIX)\"" -DMODULEDIR="\"$(MODULEDIR)\""
RM := rm -f
LN := ln -s
......
../../licenses/CC0
\ No newline at end of file
#include "config.h"
#include <string.h>
#include <stdio.h>
/**
* compiler - macros for common compiler extensions
*
* Abstracts away some compiler hints. Currently these include:
* - COLD
* For functions not called in fast paths (aka. cold functions)
* - PRINTF_FMT
* For functions which take printf-style parameters.
* - CONST_FUNCTION
* For functions which return the same value for same parameters.
* - NEEDED
* For functions and variables which must be emitted even if unused.
* - UNNEEDED
* For functions and variables which need not be emitted if unused.
* - UNUSED
* For parameters which are not used.
* - IS_COMPILE_CONSTANT()
* For using different tradeoffs for compiletime vs runtime evaluation.
*
* License: CC0 (Public domain)
* Author: Rusty Russell <rusty@rustcorp.com.au>
*
* Example:
* #include <ccan/compiler/compiler.h>
* #include <stdio.h>
* #include <stdarg.h>
*
* // Example of a (slow-path) logging function.
* static int log_threshold = 2;
* static void COLD PRINTF_FMT(2,3)
* logger(int level, const char *fmt, ...)
* {
* va_list ap;
* va_start(ap, fmt);
* if (level >= log_threshold)
* vfprintf(stderr, fmt, ap);
* va_end(ap);
* }
*
* int main(int argc, char *argv[])
* {
* if (argc != 1) {
* logger(3, "Don't want %i arguments!\n", argc-1);
* return 1;
* }
* return 0;
* }
*/
int main(int argc, char *argv[])
{
/* Expect exactly one argument */
if (argc != 2)
return 1;
if (strcmp(argv[1], "depends") == 0) {
return 0;
}
return 1;
}
/* CC0 (Public domain) - see LICENSE file for details */
#ifndef CCAN_COMPILER_H
#define CCAN_COMPILER_H
#include "config.h"
#ifndef COLD
#if HAVE_ATTRIBUTE_COLD
/**
* COLD - a function is unlikely to be called.
*
* Used to mark an unlikely code path and optimize appropriately.
* It is usually used on logging or error routines.
*
* Example:
* static void COLD moan(const char *reason)
* {
* fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
* }
*/
#define COLD __attribute__((__cold__))
#else
#define COLD
#endif
#endif
#ifndef NORETURN
#if HAVE_ATTRIBUTE_NORETURN
/**
* NORETURN - a function does not return
*
* Used to mark a function which exits; useful for suppressing warnings.
*
* Example:
* static void NORETURN fail(const char *reason)
* {
* fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno));
* exit(1);
* }
*/
#define NORETURN __attribute__((__noreturn__))
#else
#define NORETURN
#endif
#endif
#ifndef PRINTF_FMT
#if HAVE_ATTRIBUTE_PRINTF
/**
* PRINTF_FMT - a function takes printf-style arguments
* @nfmt: the 1-based number of the function's format argument.
* @narg: the 1-based number of the function's first variable argument.
*
* This allows the compiler to check your parameters as it does for printf().
*
* Example:
* void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...);
*/
#define PRINTF_FMT(nfmt, narg) \
__attribute__((format(__printf__, nfmt, narg)))
#else
#define PRINTF_FMT(nfmt, narg)
#endif
#endif
#ifndef CONST_FUNCTION
#if HAVE_ATTRIBUTE_CONST
/**
* CONST_FUNCTION - a function's return depends only on its argument
*
* This allows the compiler to assume that the function will return the exact
* same value for the exact same arguments. This implies that the function
* must not use global variables, or dereference pointer arguments.
*/
#define CONST_FUNCTION __attribute__((__const__))
#else
#define CONST_FUNCTION
#endif
#ifndef PURE_FUNCTION
#if HAVE_ATTRIBUTE_PURE
/**
* PURE_FUNCTION - a function is pure
*
* A pure function is one that has no side effects other than it's return value
* and uses no inputs other than it's arguments and global variables.
*/
#define PURE_FUNCTION __attribute__((__pure__))
#else
#define PURE_FUNCTION
#endif
#endif
#endif
#if HAVE_ATTRIBUTE_UNUSED
#ifndef UNNEEDED
/**
* UNNEEDED - a variable/function may not be needed
*
* This suppresses warnings about unused variables or functions, but tells
* the compiler that if it is unused it need not emit it into the source code.
*
* Example:
* // With some preprocessor options, this is unnecessary.
* static UNNEEDED int counter;
*
* // With some preprocessor options, this is unnecessary.
* static UNNEEDED void add_to_counter(int add)
* {
* counter += add;
* }
*/
#define UNNEEDED __attribute__((__unused__))
#endif
#ifndef NEEDED
#if HAVE_ATTRIBUTE_USED
/**
* NEEDED - a variable/function is needed
*
* This suppresses warnings about unused variables or functions, but tells
* the compiler that it must exist even if it (seems) unused.
*
* Example:
* // Even if this is unused, these are vital for debugging.
* static NEEDED int counter;
* static NEEDED void dump_counter(void)
* {
* printf("Counter is %i\n", counter);
* }
*/
#define NEEDED __attribute__((__used__))
#else
/* Before used, unused functions and vars were always emitted. */
#define NEEDED __attribute__((__unused__))
#endif
#endif
#ifndef UNUSED
/**
* UNUSED - a parameter is unused
*
* Some compilers (eg. gcc with -W or -Wunused) warn about unused
* function parameters. This suppresses such warnings and indicates
* to the reader that it's deliberate.
*
* Example:
* // This is used as a callback, so needs to have this prototype.
* static int some_callback(void *unused UNUSED)
* {
* return 0;
* }
*/
#define UNUSED __attribute__((__unused__))
#endif
#else
#ifndef UNNEEDED
#define UNNEEDED
#endif
#ifndef NEEDED
#define NEEDED
#endif
#ifndef UNUSED
#define UNUSED
#endif
#endif
#ifndef IS_COMPILE_CONSTANT
#if HAVE_BUILTIN_CONSTANT_P
/**
* IS_COMPILE_CONSTANT - does the compiler know the value of this expression?
* @expr: the expression to evaluate
*
* When an expression manipulation is complicated, it is usually better to
* implement it in a function. However, if the expression being manipulated is
* known at compile time, it is better to have the compiler see the entire
* expression so it can simply substitute the result.
*
* This can be done using the IS_COMPILE_CONSTANT() macro.
*
* Example:
* enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON };
*
* // Out-of-line version.
* const char *greek_name(enum greek greek);
*
* // Inline version.
* static inline const char *_greek_name(enum greek greek)
* {
* switch (greek) {
* case ALPHA: return "alpha";
* case BETA: return "beta";
* case GAMMA: return "gamma";
* case DELTA: return "delta";
* case EPSILON: return "epsilon";
* default: return "**INVALID**";
* }
* }
*
* // Use inline if compiler knows answer. Otherwise call function
* // to avoid copies of the same code everywhere.
* #define greek_name(g) \
* (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g))
*/
#define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr)
#else
/* If we don't know, assume it's not. */
#define IS_COMPILE_CONSTANT(expr) 0
#endif
#endif
#ifndef WARN_UNUSED_RESULT
#if HAVE_WARN_UNUSED_RESULT
/**
* WARN_UNUSED_RESULT - warn if a function return value is unused.
*
* Used to mark a function where it is extremely unlikely that the caller
* can ignore the result, eg realloc().
*
* Example:
* // buf param may be freed by this; need return value!
* static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size)
* {
* return realloc(buf, (*size) *= 2);
* }
*/
#define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
#else
#define WARN_UNUSED_RESULT
#endif
#endif
#endif /* CCAN_COMPILER_H */
#include <ccan/compiler/compiler.h>
static void PRINTF_FMT(2,3) my_printf(int x, const char *fmt, ...)
{
}
int main(int argc, char *argv[])
{
unsigned int i = 0;
my_printf(1, "Not a pointer "
#ifdef FAIL
"%p",
#if !HAVE_ATTRIBUTE_PRINTF
#error "Unfortunately we don't fail if !HAVE_ATTRIBUTE_PRINTF."
#endif
#else
"%i",
#endif
i);
return 0;
}
#include <ccan/compiler/compiler.h>
#include <ccan/tap/tap.h>
int main(int argc, char *argv[])
{
plan_tests(2);
ok1(!IS_COMPILE_CONSTANT(argc));
#if HAVE_BUILTIN_CONSTANT_P
ok1(IS_COMPILE_CONSTANT(7));
#else
pass("If !HAVE_BUILTIN_CONSTANT_P, IS_COMPILE_CONSTANT always false");
#endif
return exit_status();
}
../../licenses/CC0
\ No newline at end of file
/**
* ilog - Integer logarithm.
*
* ilog_32() and ilog_64() compute the minimum number of bits required to store
* an unsigned 32-bit or 64-bit value without any leading zero bits.
*
* This can also be thought of as the location of the highest set bit, with
* counting starting from one (so that 0 returns 0, 1 returns 1, and 2**31
* returns 32).
*
* When the value is known to be non-zero ilog32_nz() and ilog64_nz() can
* compile into as few as two instructions, one of which may get optimized out
* later.
*
* STATIC_ILOG_32 and STATIC_ILOG_64 allow computation on compile-time
* constants, so other compile-time constants can be derived from them.
*
* Example:
* #include <stdio.h>
* #include <limits.h>
* #include <ccan/ilog/ilog.h>
*
* int main(void){
* int i;
* printf("ilog32(0x%08X)=%i\n",0,ilog32(0));
* for(i=1;i<=STATIC_ILOG_32(USHRT_MAX);i++){
* uint32_t v;
* v=(uint32_t)1U<<(i-1);
* //Here we know v is non-zero, so we can use ilog32_nz().
* printf("ilog32(0x%08X)=%i\n",v,ilog32_nz(v));
* }
* return 0;
* }
*
* License: CC0 (Public domain)
* Author: Timothy B. Terriberry <tterribe@xiph.org>
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
int main(int _argc,const char *_argv[]){
/*Expect exactly one argument.*/
if(_argc!=2)return 1;
if(strcmp(_argv[1],"depends")==0){
printf("ccan/compiler\n");
return 0;
}
return 1;
}
/*(C) Timothy B. Terriberry (tterribe@xiph.org) 2001-2009 CC0 (Public domain).
* See LICENSE file for details. */
#include "ilog.h"
#include <limits.h>
/*The fastest fallback strategy for platforms with fast multiplication appears
to be based on de Bruijn sequences~\cite{LP98}.
Tests confirmed this to be true even on an ARM11, where it is actually faster
than using the native clz instruction.
Define ILOG_NODEBRUIJN to use a simpler fallback on platforms where
multiplication or table lookups are too expensive.
@UNPUBLISHED{LP98,
author="Charles E. Leiserson and Harald Prokop",
title="Using de {Bruijn} Sequences to Index a 1 in a Computer Word",
month=Jun,
year=1998,
note="\url{http://supertech.csail.mit.edu/papers/debruijn.pdf}"
}*/
static UNNEEDED const unsigned char DEBRUIJN_IDX32[32]={
0, 1,28, 2,29,14,24, 3,30,22,20,15,25,17, 4, 8,
31,27,13,23,21,19,16, 7,26,12,18, 6,11, 5,10, 9
};
/* We always compile these in, in case someone takes address of function. */
#undef ilog32_nz
#undef ilog32
#undef ilog64_nz
#undef ilog64
int ilog32(uint32_t _v){
/*On a Pentium M, this branchless version tested as the fastest version without
multiplications on 1,000,000,000 random 32-bit integers, edging out a
similar version with branches, and a 256-entry LUT version.*/
# if defined(ILOG_NODEBRUIJN)
int ret;
int m;
ret=_v>0;
m=(_v>0xFFFFU)<<4;
_v>>=m;
ret|=m;
m=(_v>0xFFU)<<3;
_v>>=m;
ret|=m;
m=(_v>0xFU)<<2;
_v>>=m;
ret|=m;
m=(_v>3)<<1;
_v>>=m;
ret|=m;
ret+=_v>1;
return ret;
/*This de Bruijn sequence version is faster if you have a fast multiplier.*/
# else
int ret;
ret=_v>0;
_v|=_v>>1;
_v|=_v>>2;
_v|=_v>>4;
_v|=_v>>8;
_v|=_v>>16;
_v=(_v>>1)+1;
ret+=DEBRUIJN_IDX32[_v*0x77CB531U>>27&0x1F];
return ret;
# endif
}
int ilog32_nz(uint32_t _v)
{
return ilog32(_v);
}
int ilog64(uint64_t _v){
# if defined(ILOG_NODEBRUIJN)
uint32_t v;
int ret;
int m;
ret=_v>0;
m=(_v>0xFFFFFFFFU)<<5;
v=(uint32_t)(_v>>m);
ret|=m;
m=(v>0xFFFFU)<<4;
v>>=m;
ret|=m;
m=(v>0xFFU)<<3;
v>>=m;
ret|=m;
m=(v>0xFU)<<2;
v>>=m;
ret|=m;
m=(v>3)<<1;
v>>=m;
ret|=m;
ret+=v>1;
return ret;
# else
/*If we don't have a 64-bit word, split it into two 32-bit halves.*/
# if LONG_MAX<9223372036854775807LL
uint32_t v;
int ret;
int m;
ret=_v>0;
m=(_v>0xFFFFFFFFU)<<5;
v=(uint32_t)(_v>>m);
ret|=m;
v|=v>>1;
v|=v>>2;
v|=v>>4;
v|=v>>8;
v|=v>>16;
v=(v>>1)+1;
ret+=DEBRUIJN_IDX32[v*0x77CB531U>>27&0x1F];
return ret;
/*Otherwise do it in one 64-bit operation.*/
# else
static const unsigned char DEBRUIJN_IDX64[64]={
0, 1, 2, 7, 3,13, 8,19, 4,25,14,28, 9,34,20,40,
5,17,26,38,15,46,29,48,10,31,35,54,21,50,41,57,
63, 6,12,18,24,27,33,39,16,37,45,47,30,53,49,56,
62,11,23,32,36,44,52,55,61,22,43,51,60,42,59,58
};
int ret;
ret=_v>0;
_v|=_v>>1;
_v|=_v>>2;
_v|=_v>>4;
_v|=_v>>8;
_v|=_v>>16;
_v|=_v>>32;
_v=(_v>>1)+1;
ret+=DEBRUIJN_IDX64[_v*0x218A392CD3D5DBF>>58&0x3F];
return ret;
# endif
# endif
}
int ilog64_nz(uint64_t _v)
{
return ilog64(_v);
}
/* CC0 (Public domain) - see LICENSE file for details */
#if !defined(_ilog_H)
# define _ilog_H (1)
# include "config.h"
# include <stdint.h>
# include <limits.h>
# include <ccan/compiler/compiler.h>
/**
* ilog32 - Integer binary logarithm of a 32-bit value.
* @_v: A 32-bit value.
* Returns floor(log2(_v))+1, or 0 if _v==0.
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
* Note that many uses will resolve to the fast macro version instead.
*
* See Also:
* ilog32_nz(), ilog64()
*
* Example:
* // Rounds up to next power of 2 (if not a power of 2).
* static uint32_t round_up32(uint32_t i)
* {
* assert(i != 0);
* return 1U << ilog32(i-1);
* }
*/
int ilog32(uint32_t _v) CONST_FUNCTION;
/**
* ilog32_nz - Integer binary logarithm of a non-zero 32-bit value.
* @_v: A 32-bit value.
* Returns floor(log2(_v))+1, or undefined if _v==0.
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
* Note that many uses will resolve to the fast macro version instead.
* See Also:
* ilog32(), ilog64_nz()
* Example:
* // Find Last Set (ie. highest bit set, 0 to 31).
* static uint32_t fls32(uint32_t i)
* {
* assert(i != 0);
* return ilog32_nz(i) - 1;
* }
*/
int ilog32_nz(uint32_t _v) CONST_FUNCTION;
/**
* ilog64 - Integer binary logarithm of a 64-bit value.
* @_v: A 64-bit value.
* Returns floor(log2(_v))+1, or 0 if _v==0.
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
* Note that many uses will resolve to the fast macro version instead.
* See Also:
* ilog64_nz(), ilog32()
*/
int ilog64(uint64_t _v) CONST_FUNCTION;
/**
* ilog64_nz - Integer binary logarithm of a non-zero 64-bit value.
* @_v: A 64-bit value.
* Returns floor(log2(_v))+1, or undefined if _v==0.
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
* Note that many uses will resolve to the fast macro version instead.
* See Also:
* ilog64(), ilog32_nz()
*/
int ilog64_nz(uint64_t _v) CONST_FUNCTION;
/**
* STATIC_ILOG_32 - The integer logarithm of an (unsigned, 32-bit) constant.
* @_v: A non-negative 32-bit constant.
* Returns floor(log2(_v))+1, or 0 if _v==0.
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
* This macro should only be used when you need a compile-time constant,
* otherwise ilog32 or ilog32_nz are just as fast and more flexible.
*
* Example:
* #define MY_PAGE_SIZE 4096
* #define MY_PAGE_BITS (STATIC_ILOG_32(PAGE_SIZE) - 1)
*/
#define STATIC_ILOG_32(_v) (STATIC_ILOG5((uint32_t)(_v)))
/**
* STATIC_ILOG_64 - The integer logarithm of an (unsigned, 64-bit) constant.
* @_v: A non-negative 64-bit constant.
* Returns floor(log2(_v))+1, or 0 if _v==0.
* This is the number of bits that would be required to represent _v in two's
* complement notation with all of the leading zeros stripped.
* This macro should only be used when you need a compile-time constant,
* otherwise ilog64 or ilog64_nz are just as fast and more flexible.
*/
#define STATIC_ILOG_64(_v) (STATIC_ILOG6((uint64_t)(_v)))
/* Private implementation details */
/*Note the casts to (int) below: this prevents "upgrading"
the type of an entire expression to an (unsigned) size_t.*/
#if INT_MAX>=2147483647 && HAVE_BUILTIN_CLZ
#define builtin_ilog32_nz(v) \
(((int)sizeof(unsigned)*CHAR_BIT) - __builtin_clz(v))
#elif LONG_MAX>=2147483647L && HAVE_BUILTIN_CLZL
#define builtin_ilog32_nz(v) \
(((int)sizeof(unsigned)*CHAR_BIT) - __builtin_clzl(v))
#endif
#if INT_MAX>=9223372036854775807LL && HAVE_BUILTIN_CLZ
#define builtin_ilog64_nz(v) \
(((int)sizeof(unsigned)*CHAR_BIT) - __builtin_clz(v))
#elif LONG_MAX>=9223372036854775807LL && HAVE_BUILTIN_CLZL
#define builtin_ilog64_nz(v) \
(((int)sizeof(unsigned long)*CHAR_BIT) - __builtin_clzl(v))
#elif HAVE_BUILTIN_CLZLL
#define builtin_ilog64_nz(v) \
(((int)sizeof(unsigned long long)*CHAR_BIT) - __builtin_clzll(v))
#endif
#ifdef builtin_ilog32_nz
#define ilog32(_v) (builtin_ilog32_nz(_v)&-!!(_v))
#define ilog32_nz(_v) builtin_ilog32_nz(_v)
#else
#define ilog32_nz(_v) ilog32(_v)
#define ilog32(_v) (IS_COMPILE_CONSTANT(_v) ? STATIC_ILOG_32(_v) : ilog32(_v))
#endif /* builtin_ilog32_nz */
#ifdef builtin_ilog64_nz
#define ilog64(_v) (builtin_ilog64_nz(_v)&-!!(_v))
#define ilog64_nz(_v) builtin_ilog64_nz(_v)
#else
#define ilog64_nz(_v) ilog64(_v)
#define ilog64(_v) (IS_COMPILE_CONSTANT(_v) ? STATIC_ILOG_64(_v) : ilog64(_v))
#endif /* builtin_ilog64_nz */
/* Macros for evaluating compile-time constant ilog. */
# define STATIC_ILOG0(_v) (!!(_v))
# define STATIC_ILOG1(_v) (((_v)&0x2)?2:STATIC_ILOG0(_v))
# define STATIC_ILOG2(_v) (((_v)&0xC)?2+STATIC_ILOG1((_v)>>2):STATIC_ILOG1(_v))
# define STATIC_ILOG3(_v) \
(((_v)&0xF0)?4+STATIC_ILOG2((_v)>>4):STATIC_ILOG2(_v))
# define STATIC_ILOG4(_v) \
(((_v)&0xFF00)?8+STATIC_ILOG3((_v)>>8):STATIC_ILOG3(_v))
# define STATIC_ILOG5(_v) \
(((_v)&0xFFFF0000)?16+STATIC_ILOG4((_v)>>16):STATIC_ILOG4(_v))
# define STATIC_ILOG6(_v) \
(((_v)&0xFFFFFFFF00000000ULL)?32+STATIC_ILOG5((_v)>>32):STATIC_ILOG5(_v))
#endif /* _ilog_H */
#include <ccan/ilog/ilog.h>
#include <ccan/ilog/ilog.c>
#include <stdio.h>
#include <ccan/tap/tap.h>
/*Dead simple (but slow) versions to compare against.*/
static int test_ilog32(uint32_t _v){
int ret;
for(ret=0;_v;ret++)_v>>=1;
return ret;
}
static int test_ilog64(uint64_t _v){
int ret;
for(ret=0;_v;ret++)_v>>=1;
return ret;
}
#define NTRIALS (64)
int main(int _argc,const char *_argv[]){
int i;
int j;
int (*il32)(uint32_t) = ilog32;
int (*il64)(uint64_t) = ilog64;
int (*il32_nz)(uint32_t) = ilog32_nz;
int (*il64_nz)(uint64_t) = ilog64_nz;
/*This is how many tests you plan to run.*/
plan_tests(33 * NTRIALS * 3 + 65 * NTRIALS * 3);
for(i=0;i<=32;i++){
uint32_t v;
/*Test each bit in turn (and 0).*/
v=i?(uint32_t)1U<<(i-1):0;
for(j=0;j<NTRIALS;j++){
int l;
l=test_ilog32(v);
ok1(STATIC_ILOG_32(v)==l);
ok1(il32(v)==l);
ok1(il32_nz(v) == l || v == 0);
/*Also try a few more pseudo-random values with at most the same number
of bits.*/
v=(1103515245U*v+12345U)&0xFFFFFFFFU>>((33-i)>>1)>>((32-i)>>1);
}
}
for(i=0;i<=64;i++){
uint64_t v;