- Main Page
- Namespaces
- Classes
- Files

- File List
- 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, size_t count)
    00014 {
    00015         size_t i;
    00016
    00017         if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
    00018         {
    00019                 #if defined(WORD64_AVAILABLE) && !defined(CRYPTOPP_SLOW_WORD64)
    00020                 if (IsAligned<word64>(buf) && IsAligned<word64>(mask))
    00021                 {
    00022                         for (i=0; i<count/8; i++)
    00023                                 ((word64*)buf)[i] ^= ((word64*)mask)[i];
    00024                         count -= 8*i;
    00025                         if (!count)
    00026                                 return;
    00027                         buf += 8*i;
    00028                         mask += 8*i;
    00029                 }
    00030                 #endif
    00031
    00032                 for (i=0; i<count/4; i++)
    00033                         ((word32*)buf)[i] ^= ((word32*)mask)[i];
    00034                 count -= 4*i;
    00035                 if (!count)
    00036                         return;
    00037                 buf += 4*i;
    00038                 mask += 4*i;
    00039         }
    00040
    00041         for (i=0; i<count; i++)
    00042                 buf[i] ^= mask[i];
    00043 }
    00044
    00045 void xorbuf(byte *output, const byte *input, const byte *mask, size_t count)
    00046 {
    00047         size_t i;
    00048
    00049         if (IsAligned<word32>(output) && IsAligned<word32>(input) && IsAligned<word32>(mask))
    00050         {
    00051                 #if defined(WORD64_AVAILABLE) && !defined(CRYPTOPP_SLOW_WORD64)
    00052                 if (IsAligned<word64>(output) && IsAligned<word64>(input) && IsAligned<word64>(mask))
    00053                 {
    00054                         for (i=0; i<count/8; i++)
    00055                                 ((word64*)output)[i] = ((word64*)input)[i] ^ ((word64*)mask)[i];
    00056                         count -= 8*i;
    00057                         if (!count)
    00058                                 return;
    00059                         output += 8*i;
    00060                         input += 8*i;
    00061                         mask += 8*i;
    00062                 }
    00063                 #endif
    00064
    00065                 for (i=0; i<count/4; i++)
    00066                         ((word32*)output)[i] = ((word32*)input)[i] ^ ((word32*)mask)[i];
    00067                 count -= 4*i;
    00068                 if (!count)
    00069                         return;
    00070                 output += 4*i;
    00071                 input += 4*i;
    00072                 mask += 4*i;
    00073         }
    00074
    00075         for (i=0; i<count; i++)
    00076                 output[i] = input[i] ^ mask[i];
    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