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
    00005 #ifndef CRYPTOPP_IMPORTS
    00006
    00007 #include "misc.h"
    00008 #include "words.h"
    00009 #include <new>
    00010
    00011 NAMESPACE_BEGIN(CryptoPP)
    00012
    00013 void xorbuf(byte *buf, const byte *mask, unsigned int count)
    00014 {
    00015         if (((size_t)buf | (size_t)mask | count) % WORD_SIZE == 0)
    00016                 XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
    00017         else
    00018         {
    00019                 for (unsigned int i=0; i<count; i++)
    00020                         buf[i] ^= mask[i];
    00021         }
    00022 }
    00023
    00024 void xorbuf(byte *output, const byte *input, const byte *mask, unsigned int count)
    00025 {
    00026         if (((size_t)output | (size_t)input | (size_t)mask | count) % WORD_SIZE == 0)
    00027                 XorWords((word *)output, (const word *)input, (const word *)mask, count/WORD_SIZE);
    00028         else
    00029         {
    00030                 for (unsigned int i=0; i<count; i++)
    00031                         output[i] = input[i] ^ mask[i];
    00032         }
    00033 }
    00034
    00035 unsigned int Parity(unsigned long value)
    00036 {
    00037         for (unsigned int i=8*sizeof(value)/2; i>0; i/=2)
    00038                 value ^= value >> i;
    00039         return (unsigned int)value&1;
    00040 }
    00041
    00042 unsigned int BytePrecision(unsigned long value)
    00043 {
    00044         unsigned int i;
    00045         for (i=sizeof(value); i; --i)
    00046                 if (value >> (i-1)*8)
    00047                         break;
    00048
    00049         return i;
    00050 }
    00051
    00052 unsigned int BitPrecision(unsigned long value)
    00053 {
    00054         if (!value)
    00055                 return 0;
    00056
    00057         unsigned int l=0, h=8*sizeof(value);
    00058
    00059         while (h-l > 1)
    00060         {
    00061                 unsigned int t = (l+h)/2;
    00062                 if (value >> t)
    00063                         l = t;
    00064                 else
    00065                         h = t;
    00066         }
    00067
    00068         return h;
    00069 }
    00070
    00071 unsigned long Crop(unsigned long value, unsigned int size)
    00072 {
    00073         if (size < 8*sizeof(value))
    00074         return (value & ((1L << size) - 1));
    00075         else
    00076                 return value;
    00077 }
    00078
    00079 #if !(defined(_MSC_VER) && (_MSC_VER < 1300))
    00080 using std::new_handler;
    00081 using std::set_new_handler;
    00082 #endif
    00083
    00084 void CallNewHandler()
    00085 {
    00086         new_handler newHandler = set_new_handler(NULL);
    00087         if (newHandler)
    00088                 set_new_handler(newHandler);
    00089
    00090         if (newHandler)
    00091                 newHandler();
    00092         else
    00093                 throw std::bad_alloc();
    00094 }
    00095
    00096 NAMESPACE_END
    00097
    00098 #endif