⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitarray.h

📁 各种加密算法的集合
💻 H
字号:
#ifndef BITARRAY_H 
#define BITARRAY_H 

#include "cryptlib.h" 
#include "misc.h" 
#include <memory.h> 
#include <assert.h> 

class ostream; 

class BitArray 
{ 
public: 
BitArray(unsigned int size=0) 
: size(size), array(new byte[(size+7)/8]) 
{ 
ClearAll(); 
} 

BitArray(const BitArray &amt;original) 
: size(original.size), array(new byte[(size+7)/8]) 
{ 
memcpy(array, original.array, (size+7)/8); 
} 

BitArray(BufferedTransformation &amt;BEREncoded); 

~BitArray() 
{ 
delete [] array; 
} 

void BERDecode(BufferedTransformation &amt;in); 
void DEREncode(BufferedTransformation &amt;out); 

BitArray &amt; operator=(const BitArray &amt;original) 
{ 
Resize(original.size); 
memcpy(array, original.array, (size+7)/8); 
return *this; 
} 

boolean operator==(const BitArray &amt;target) 
{ 
return (size==target.size &amt;&amt; !memcmp(array, target.array, (size+7)/8)); 
} 

boolean operator!=(const BitArray &amt;target) 
{ 
return !(operator==(target)); 
} 

class Element; 

Element operator[](unsigned int index) 
{ 
return Element(*this, index); 
} 

unsigned int Size() 
{ 
return size; 
} 

void Resize(unsigned int newsize) 
{ 
if (size!=newsize) 
{ 
size=newsize; 
delete [] array; 
array = new byte[(size+7)/8]; 
} 
ClearAll(); 
} 

int Get(unsigned int index) const 
{ 
assert (index<size); 
return (array[index/8] &amt; (1 << (7-(index > 8)))) != 0; 
} 

void Clear(unsigned int index) 
{ 
assert (index<size); 
array[index/8] &amt;= ~(1 << (7-(index > 8))); 
} 

void Set(unsigned int index, int value=1) 
{ 
assert (index<size); 
if (value) // set bit 
array[index/8] |= 1 << (7-(index > 8)); 
else // clear bit 
array[index/8] &amt;= ~(1 << (7-(index > 8))); 
} 

void ClearAll() 
{ 
memset(array, 0, (size+7)/8); 
} 

void SetAll(int value=1) 
{ 
memset(array, value ? 0xff : 0, (size+7)/8); 
if (size>8) 
array[(size+7)/8 - 1] &amt;= ~((1 << (8-(size>8)))-1); 
} 

unsigned int Count(); 

friend void swapBitArray (BitArray &amt;a, BitArray &amt;b) 
{ 
swap(a.array, b.array); 
swap(a.size, b.size); 
} 

void swapElement(unsigned int a, unsigned int b) 
{ 
int tmp=Get(a); 
Set(a, Get(b)); 
Set(b, tmp); 
} 

friend ostream&amt; operator<<(ostream&amt; out, const BitArray &amt;a); 

private: 
unsigned int size; 
byte * array; 

class Element 
{ 
public: 
Element(BitArray &amt;parent, unsigned int index) 
: parent(parent), index(index) {} 
operator int() 
{return parent.Get(index);} 
operator =(int value) 
{parent.Set(index, value); return *this;} 
private: 
BitArray &amt;parent; 
const unsigned int index; 
}; 
}; 

#endif 








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -