1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-28 02:30:05 +00:00

Tidied a few static functions away into private namespaces.

This commit is contained in:
Julian Storer 2010-02-20 18:57:36 +00:00
parent e1f3c2df6f
commit 038886510a
15 changed files with 1564 additions and 1594 deletions

View file

@ -36,7 +36,7 @@ BEGIN_JUCE_NAMESPACE
//==============================================================================
MD5::MD5()
{
zeromem (result, sizeof (result));
zerostruct (result);
}
MD5::MD5 (const MD5& other)
@ -121,13 +121,62 @@ MD5::MD5 (const File& file)
if (fin != 0)
processStream (*fin, -1);
else
zeromem (result, sizeof (result));
zerostruct (result);
}
MD5::~MD5()
{
}
//==============================================================================
namespace MD5Functions
{
static void encode (uint8* const output, const uint32* const input, const int numBytes) throw()
{
uint32* const o = (uint32*) output;
for (int i = 0; i < (numBytes >> 2); ++i)
o[i] = ByteOrder::swapIfBigEndian (input [i]);
}
static void decode (uint32* const output, const uint8* const input, const int numBytes) throw()
{
for (int i = 0; i < (numBytes >> 2); ++i)
output[i] = ByteOrder::littleEndianInt ((const char*) input + (i << 2));
}
static inline uint32 F (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & y) | (~x & z); }
static inline uint32 G (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & z) | (y & ~z); }
static inline uint32 H (const uint32 x, const uint32 y, const uint32 z) throw() { return x ^ y ^ z; }
static inline uint32 I (const uint32 x, const uint32 y, const uint32 z) throw() { return y ^ (x | ~z); }
static inline uint32 rotateLeft (const uint32 x, const uint32 n) throw() { return (x << n) | (x >> (32 - n)); }
static void FF (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += F (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
static void GG (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += G (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
static void HH (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += H (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
static void II (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += I (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
}
//==============================================================================
MD5::ProcessContext::ProcessContext()
{
@ -175,30 +224,11 @@ void MD5::ProcessContext::processBlock (const uint8* const data, size_t dataSize
memcpy (buffer + bufferPos, data + i, dataSize - i);
}
//==============================================================================
static void encode (uint8* const output,
const uint32* const input,
const int numBytes)
{
uint32* const o = (uint32*) output;
for (int i = 0; i < (numBytes >> 2); ++i)
o[i] = ByteOrder::swapIfBigEndian (input [i]);
}
static void decode (uint32* const output,
const uint8* const input,
const int numBytes)
{
for (int i = 0; i < (numBytes >> 2); ++i)
output[i] = ByteOrder::littleEndianInt ((const char*) input + (i << 2));
}
//==============================================================================
void MD5::ProcessContext::finish (uint8* const result)
{
unsigned char encodedLength[8];
encode (encodedLength, count, 8);
MD5Functions::encode (encodedLength, count, 8);
// Pad out to 56 mod 64.
const int index = (uint32) ((count[0] >> 3) & 0x3f);
@ -213,48 +243,14 @@ void MD5::ProcessContext::finish (uint8* const result)
processBlock (encodedLength, 8);
encode (result, state, 16);
zeromem (buffer, sizeof (buffer));
}
//==============================================================================
namespace MD5Functions
{
static inline uint32 F (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & y) | (~x & z); }
static inline uint32 G (const uint32 x, const uint32 y, const uint32 z) throw() { return (x & z) | (y & ~z); }
static inline uint32 H (const uint32 x, const uint32 y, const uint32 z) throw() { return x ^ y ^ z; }
static inline uint32 I (const uint32 x, const uint32 y, const uint32 z) throw() { return y ^ (x | ~z); }
static inline uint32 rotateLeft (const uint32 x, const uint32 n) throw() { return (x << n) | (x >> (32 - n)); }
static void FF (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += F (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
static void GG (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += G (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
static void HH (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += H (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
static void II (uint32& a, const uint32 b, const uint32 c, const uint32 d, const uint32 x, const uint32 s, const uint32 ac) throw()
{
a += I (b, c, d) + x + ac;
a = rotateLeft (a, s) + b;
}
MD5Functions::encode (result, state, sizeof (result));
zerostruct (buffer);
}
void MD5::ProcessContext::transform (const uint8* const bufferToTransform)
{
using namespace MD5Functions;
uint32 a = state[0];
uint32 b = state[1];
uint32 c = state[2];
@ -269,7 +265,6 @@ void MD5::ProcessContext::transform (const uint8* const bufferToTransform)
S31 = 4, S32 = 11, S33 = 16, S34 = 23, S41 = 6, S42 = 10, S43 = 15, S44 = 21
};
using namespace MD5Functions;
FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
@ -349,18 +344,18 @@ void MD5::ProcessContext::transform (const uint8* const bufferToTransform)
//==============================================================================
const MemoryBlock MD5::getRawChecksumData() const
{
return MemoryBlock (result, 16);
return MemoryBlock (result, sizeof (result));
}
const String MD5::toHexString() const
{
return String::toHexString (result, 16, 0);
return String::toHexString (result, sizeof (result), 0);
}
//==============================================================================
bool MD5::operator== (const MD5& other) const
{
return memcmp (result, other.result, 16) == 0;
return memcmp (result, other.result, sizeof (result)) == 0;
}
bool MD5::operator!= (const MD5& other) const

View file

@ -33,85 +33,88 @@ BEGIN_JUCE_NAMESPACE
//==============================================================================
static void createSmallSieve (const int numBits, BitArray& result) throw()
namespace PrimesHelpers
{
result.setBit (numBits);
result.clearBit (numBits); // to enlarge the array
result.setBit (0);
int n = 2;
do
static void createSmallSieve (const int numBits, BitArray& result) throw()
{
for (int i = n + n; i < numBits; i += n)
result.setBit (i);
result.setBit (numBits);
result.clearBit (numBits); // to enlarge the array
n = result.findNextClearBit (n + 1);
}
while (n <= (numBits >> 1));
}
result.setBit (0);
int n = 2;
static void bigSieve (const BitArray& base,
const int numBits,
BitArray& result,
const BitArray& smallSieve,
const int smallSieveSize) throw()
{
jassert (! base[0]); // must be even!
result.setBit (numBits);
result.clearBit (numBits); // to enlarge the array
int index = smallSieve.findNextClearBit (0);
do
{
const int prime = (index << 1) + 1;
BitArray r (base);
BitArray remainder;
r.divideBy (prime, remainder);
int i = prime - remainder.getBitRangeAsInt (0, 32);
if (r.isEmpty())
i += prime;
if ((i & 1) == 0)
i += prime;
i = (i - 1) >> 1;
while (i < numBits)
do
{
result.setBit (i);
i += prime;
for (int i = n + n; i < numBits; i += n)
result.setBit (i);
n = result.findNextClearBit (n + 1);
}
while (n <= (numBits >> 1));
}
static void bigSieve (const BitArray& base,
const int numBits,
BitArray& result,
const BitArray& smallSieve,
const int smallSieveSize) throw()
{
jassert (! base[0]); // must be even!
result.setBit (numBits);
result.clearBit (numBits); // to enlarge the array
int index = smallSieve.findNextClearBit (0);
do
{
const int prime = (index << 1) + 1;
BitArray r (base);
BitArray remainder;
r.divideBy (prime, remainder);
int i = prime - remainder.getBitRangeAsInt (0, 32);
if (r.isEmpty())
i += prime;
if ((i & 1) == 0)
i += prime;
i = (i - 1) >> 1;
while (i < numBits)
{
result.setBit (i);
i += prime;
}
index = smallSieve.findNextClearBit (index + 1);
}
while (index < smallSieveSize);
}
static bool findCandidate (const BitArray& base,
const BitArray& sieve,
const int numBits,
BitArray& result,
const int certainty) throw()
{
for (int i = 0; i < numBits; ++i)
{
if (! sieve[i])
{
result = base;
result.add (BitArray ((unsigned int) ((i << 1) + 1)));
if (Primes::isProbablyPrime (result, certainty))
return true;
}
}
index = smallSieve.findNextClearBit (index + 1);
return false;
}
while (index < smallSieveSize);
}
static bool findCandidate (const BitArray& base,
const BitArray& sieve,
const int numBits,
BitArray& result,
const int certainty) throw()
{
for (int i = 0; i < numBits; ++i)
{
if (! sieve[i])
{
result = base;
result.add (BitArray ((unsigned int) ((i << 1) + 1)));
if (Primes::isProbablyPrime (result, certainty))
return true;
}
}
return false;
}
//==============================================================================
@ -120,6 +123,7 @@ const BitArray Primes::createProbablePrime (const int bitLength,
const int* randomSeeds,
int numRandomSeeds) throw()
{
using namespace PrimesHelpers;
int defaultSeeds [16];
if (numRandomSeeds <= 0)
@ -178,6 +182,8 @@ const BitArray Primes::createProbablePrime (const int bitLength,
static bool passesMillerRabin (const BitArray& n, int iterations) throw()
{
using namespace PrimesHelpers;
const BitArray one (1);
const BitArray two (2);