X-Git-Url: https://projects.mako.cc/source/wikiq/blobdiff_plain/86aeece4b3a7ffa495b4c09441cffeec8e579f34..ae96c7a9d99998ad0be4bb8aa382b92df32533a5:/disorder.c diff --git a/disorder.c b/disorder.c new file mode 100644 index 0000000..a5f7c35 --- /dev/null +++ b/disorder.c @@ -0,0 +1,192 @@ +/*************************************************************************** + * libdisorder: A Library for Measuring Byte Stream Entropy + * Copyright (C) 2010 Michael E. Locasto + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the: + * Free Software Foundation, Inc. + * 59 Temple Place, Suite 330 + * Boston, MA 02111-1307 USA + * + * $Id$ + **************************************************************************/ + +#include //for log2() +#include //for NULL +#include "disorder.h" + +#if defined(__FreeBSD__) +#define log2(x) (log((x)) * (1./M_LN2)) +#endif + +/** Frequecies for each byte */ +static int m_token_freqs[LIBDO_MAX_BYTES]; //frequency of each token in sample +static float m_token_probs[LIBDO_MAX_BYTES]; //P(each token appearing) +static int m_num_tokens = 0; //actual number of `seen' tokens, max 256 +static float m_maxent = 0.0; +static float m_ratio = 0.0; +static int LIBDISORDER_INITIALIZED = 0; + +static void +initialize_lib() +{ + int i = 0; + if(1==LIBDISORDER_INITIALIZED) + return; + + m_num_tokens = 0; + + for(i=0;iLIBDO_MAX_BYTES) + { + //report error somehow? + return 0.0; + } + + //iterate through whole m_token_freq array, but only count + //spots that have a registered token (i.e., freq>0) + for(i=0;i