//--------------------------------------------------------------------------- /* 010 Editor Script to calculate the entropy of the open file or a selection https://en.wikipedia.org/wiki/Entropy_%28information_theory%29 2012/06/20 v0.0.1 Source code put in public domain by Didier Stevens, no Copyright https://DidierStevens.com Use at your own risk History: 2012/06/20: start development with 010 Editor v3.2.2 Todo: */ //--------------------------------------------------------------------------- const int BUCKET_SIZE = 256; // Workaround for floating point negation bug fixed in 010 Editor v4.0.0 double NegateDouble(double dIn) { return dIn * -1; } double CalculateEntropy(int64 iStart, int64 iSize) { int64 iIter; int64 aiPrevelance[BUCKET_SIZE]; double dEntropy; double dPrevalence; for (iIter = iStart; iIter - iStart < iSize; iIter++) aiPrevelance[ReadUByte(iIter)] += 1; for (iIter = 0; iIter < BUCKET_SIZE; iIter++) if (aiPrevelance[iIter] > 0) { dPrevalence = (double) aiPrevelance[iIter] / (double) iSize; dEntropy += NegateDouble(dPrevalence * Log(dPrevalence) / Log(2.0)); } return dEntropy; } void Main(void) { int64 iStart; int64 iSize; iSize = GetSelSize(); if (0 == iSize) { iStart = 0; iSize = FileSize(); } else iStart = GetSelStart(); MessageBox(idOk, "Entropy", "The entropy is %f", CalculateEntropy(iStart, iSize)); } Main();