monotone

monotone Mtn Source Tree

Root/cryptopp/misc.cpp

1// misc.cpp - written and placed in the public domain by Wei Dai
2
3#include "pch.h"
4#include "misc.h"
5#include "words.h"
6
7NAMESPACE_BEGIN(CryptoPP)
8
9byte OAEP_P_DEFAULT[1];
10
11template<> void ByteReverse(word16 *, const word16 *, unsigned int);
12template<> void ByteReverse(word32 *, const word32 *, unsigned int);
13#ifdef WORD64_AVAILABLE
14template<> void ByteReverse(word64 *, const word64 *, unsigned int);
15#endif
16
17void xorbuf(byte *buf, const byte *mask, unsigned int count)
18{
19if (((unsigned int)buf | (unsigned int)mask | count) % WORD_SIZE == 0)
20XorWords((word *)buf, (const word *)mask, count/WORD_SIZE);
21else
22{
23for (unsigned int i=0; i<count; i++)
24buf[i] ^= mask[i];
25}
26}
27
28void xorbuf(byte *output, const byte *input, const byte *mask, unsigned int count)
29{
30if (((unsigned int)output | (unsigned int)input | (unsigned int)mask | count) % WORD_SIZE == 0)
31XorWords((word *)output, (const word *)input, (const word *)mask, count/WORD_SIZE);
32else
33{
34for (unsigned int i=0; i<count; i++)
35output[i] = input[i] ^ mask[i];
36}
37}
38
39unsigned int Parity(unsigned long value)
40{
41for (unsigned int i=8*sizeof(value)/2; i>0; i/=2)
42value ^= value >> i;
43return (unsigned int)value&1;
44}
45
46unsigned int BytePrecision(unsigned long value)
47{
48unsigned int i;
49for (i=sizeof(value); i; --i)
50if (value >> (i-1)*8)
51break;
52
53return i;
54}
55
56unsigned int BitPrecision(unsigned long value)
57{
58if (!value)
59return 0;
60
61unsigned int l=0, h=8*sizeof(value);
62
63while (h-l > 1)
64{
65unsigned int t = (l+h)/2;
66if (value >> t)
67l = t;
68else
69h = t;
70}
71
72return h;
73}
74
75unsigned long Crop(unsigned long value, unsigned int size)
76{
77if (size < 8*sizeof(value))
78 return (value & ((1L << size) - 1));
79else
80return value;
81}
82
83NAMESPACE_END

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status