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

misc.cpp

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

Generated on Tue Jul 8 23:34:20 2003 for Crypto++ by doxygen 1.3.2