- Main Page
- Namespaces
- Classes
- Files

# 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 (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
    00020                 {
    00021                         for (i=0; i<count/8; i++)
    00022                                 ((word64*)buf)[i] ^= ((word64*)mask)[i];
    00023                         count -= 8*i;
    00024                         if (!count)
    00025                                 return;
    00026                         buf += 8*i;
    00027                         mask += 8*i;
    00028                 }
    00029
    00030                 for (i=0; i<count/4; i++)
    00031                         ((word32*)buf)[i] ^= ((word32*)mask)[i];
    00032                 count -= 4*i;
    00033                 if (!count)
    00034                         return;
    00035                 buf += 4*i;
    00036                 mask += 4*i;
    00037         }
    00038
    00039         for (i=0; i<count; i++)
    00040                 buf[i] ^= mask[i];
    00041 }
    00042
    00043 void xorbuf(byte *output, const byte *input, const byte *mask, size_t count)
    00044 {
    00045         size_t i;
    00046
    00047         if (IsAligned<word32>(output) && IsAligned<word32>(input) && IsAligned<word32>(mask))
    00048         {
    00049                 if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(output) && IsAligned<word64>(input) && IsAligned<word64>(mask))
    00050                 {
    00051                         for (i=0; i<count/8; i++)
    00052                                 ((word64*)output)[i] = ((word64*)input)[i] ^ ((word64*)mask)[i];
    00053                         count -= 8*i;
    00054                         if (!count)
    00055                                 return;
    00056                         output += 8*i;
    00057                         input += 8*i;
    00058                         mask += 8*i;
    00059                 }
    00060
    00061                 for (i=0; i<count/4; i++)
    00062                         ((word32*)output)[i] = ((word32*)input)[i] ^ ((word32*)mask)[i];
    00063                 count -= 4*i;
    00064                 if (!count)
    00065                         return;
    00066                 output += 4*i;
    00067                 input += 4*i;
    00068                 mask += 4*i;
    00069         }
    00070
    00071         for (i=0; i<count; i++)
    00072                 output[i] = input[i] ^ mask[i];
    00073 }
    00074
    00075 bool VerifyBufsEqual(const byte *buf, const byte *mask, size_t count)
    00076 {
    00077         size_t i;
    00078         byte acc8 = 0;
    00079
    00080         if (IsAligned<word32>(buf) && IsAligned<word32>(mask))
    00081         {
    00082                 word32 acc32 = 0;
    00083                 if (!CRYPTOPP_BOOL_SLOW_WORD64 && IsAligned<word64>(buf) && IsAligned<word64>(mask))
    00084                 {
    00085                         word64 acc64 = 0;
    00086                         for (i=0; i<count/8; i++)
    00087                                 acc64 |= ((word64*)buf)[i] ^ ((word64*)mask)[i];
    00088                         count -= 8*i;
    00089                         if (!count)
    00090                                 return acc64 == 0;
    00091                         buf += 8*i;
    00092                         mask += 8*i;
    00093                         acc32 = word32(acc64) | word32(acc64>>32);
    00094                 }
    00095
    00096                 for (i=0; i<count/4; i++)
    00097                         acc32 |= ((word32*)buf)[i] ^ ((word32*)mask)[i];
    00098                 count -= 4*i;
    00099                 if (!count)
    00100                         return acc32 == 0;
    00101                 buf += 4*i;
    00102                 mask += 4*i;
    00103                 acc8 = byte(acc32) | byte(acc32>>8) | byte(acc32>>16) | byte(acc32>>24);
    00104         }
    00105
    00106         for (i=0; i<count; i++)
    00107                 acc8 |= buf[i] ^ mask[i];
    00108         return acc8 == 0;
    00109 }
    00110
    00111 #if !(defined(_MSC_VER) && (_MSC_VER < 1300))
    00112 using std::new_handler;
    00113 using std::set_new_handler;
    00114 #endif
    00115
    00116 void CallNewHandler()
    00117 {
    00118         new_handler newHandler = set_new_handler(NULL);
    00119         if (newHandler)
    00120                 set_new_handler(newHandler);
    00121
    00122         if (newHandler)
    00123                 newHandler();
    00124         else
    00125                 throw std::bad_alloc();
    00126 }
    00127
    00128 NAMESPACE_END
    00129
    00130 #endif