Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

misc.cpp

00001 // misc.cpp - written and placed in the public domain by Wei Dai 00002 00003 #include "pch.h" 00004 #include "misc.h" 00005 #include "words.h" 00006 00007 NAMESPACE_BEGIN(CryptoPP) 00008 00009 byte OAEP_P_DEFAULT[1]; 00010 00011 template<> void ByteReverse(word16 *, const word16 *, unsigned int); 00012 template<> void ByteReverse(word32 *, const word32 *, unsigned int); 00013 #ifdef WORD64_AVAILABLE 00014 template<> void ByteReverse(word64 *, const word64 *, unsigned int); 00015 #endif 00016 00017 void xorbuf(byte *buf, const byte *mask, unsigned int count) 00018 { 00019 if (((unsigned int)buf | (unsigned int)mask | count) % WORD_SIZE == 0) 00020 XorWords((word *)buf, (const word *)mask, count/WORD_SIZE); 00021 else 00022 { 00023 for (unsigned int i=0; i<count; i++) 00024 buf[i] ^= mask[i]; 00025 } 00026 } 00027 00028 void xorbuf(byte *output, const byte *input, const byte *mask, unsigned int count) 00029 { 00030 if (((unsigned int)output | (unsigned int)input | (unsigned int)mask | count) % WORD_SIZE == 0) 00031 XorWords((word *)output, (const word *)input, (const word *)mask, count/WORD_SIZE); 00032 else 00033 { 00034 for (unsigned int i=0; i<count; i++) 00035 output[i] = input[i] ^ mask[i]; 00036 } 00037 } 00038 00039 unsigned int Parity(unsigned long value) 00040 { 00041 for (unsigned int i=8*sizeof(value)/2; i>0; i/=2) 00042 value ^= value >> i; 00043 return (unsigned int)value&1; 00044 } 00045 00046 unsigned int BytePrecision(unsigned long value) 00047 { 00048 unsigned int i; 00049 for (i=sizeof(value); i; --i) 00050 if (value >> (i-1)*8) 00051 break; 00052 00053 return i; 00054 } 00055 00056 unsigned int BitPrecision(unsigned long value) 00057 { 00058 if (!value) 00059 return 0; 00060 00061 unsigned int l=0, h=8*sizeof(value); 00062 00063 while (h-l > 1) 00064 { 00065 unsigned int t = (l+h)/2; 00066 if (value >> t) 00067 l = t; 00068 else 00069 h = t; 00070 } 00071 00072 return h; 00073 } 00074 00075 unsigned long Crop(unsigned long value, unsigned int size) 00076 { 00077 if (size < 8*sizeof(value)) 00078 return (value & ((1L << size) - 1)); 00079 else 00080 return value; 00081 } 00082 00083 NAMESPACE_END

Generated on Fri Aug 13 09:56:54 2004 for Crypto++ by doxygen 1.3.7