Files
itgmania212121/stepmania/src/crypto51/misc.cpp
T

85 lines
1.7 KiB
C++
Raw Normal View History

2004-05-13 06:02:32 +00:00
// misc.cpp - written and placed in the public domain by Wei Dai
2006-08-13 20:13:07 +00:00
#include "global.h"
2004-05-13 06:02:32 +00:00
#include "pch.h"
#include "misc.h"
#include "words.h"
2006-02-03 19:58:38 +00:00
namespace CryptoPP {
2004-05-13 06:02:32 +00:00
byte OAEP_P_DEFAULT[1];
template<> void ByteReverse(word16 *, const word16 *, unsigned int);
template<> void ByteReverse(word32 *, const word32 *, unsigned int);
#ifdef WORD64_AVAILABLE
template<> void ByteReverse(word64 *, const word64 *, unsigned int);
#endif
void xorbuf(byte *buf, const byte *mask, unsigned int count)
{
2005-06-23 04:51:12 +00:00
if (((uintptr_t)buf | (uintptr_t)mask | count) % WORD_SIZE == 0)
2004-05-13 06:02:32 +00:00
XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
else
{
for (unsigned int i=0; i<count; i++)
buf[i] ^= mask[i];
}
}
void xorbuf(byte *output, const byte *input, const byte *mask, unsigned int count)
{
2005-06-23 04:51:12 +00:00
if (((uintptr_t)output | (uintptr_t)input | (uintptr_t)mask | count) % WORD_SIZE == 0)
2004-05-13 06:02:32 +00:00
XorWords((word *)output, (const word *)input, (const word *)mask, count/WORD_SIZE);
else
{
for (unsigned int i=0; i<count; i++)
output[i] = input[i] ^ mask[i];
}
}
unsigned int Parity(unsigned long value)
{
for (unsigned int i=8*sizeof(value)/2; i>0; i/=2)
value ^= value >> i;
return (unsigned int)value&1;
}
unsigned int BytePrecision(unsigned long value)
{
unsigned int i;
for (i=sizeof(value); i; --i)
if (value >> (i-1)*8)
break;
return i;
}
unsigned int BitPrecision(unsigned long value)
{
if (!value)
return 0;
unsigned int l=0, h=8*sizeof(value);
while (h-l > 1)
{
unsigned int t = (l+h)/2;
if (value >> t)
l = t;
else
h = t;
}
return h;
}
unsigned long Crop(unsigned long value, unsigned int size)
{
if (size < 8*sizeof(value))
return (value & ((1L << size) - 1));
else
return value;
}
2006-02-03 19:58:38 +00:00
}