cbitvector.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /**
  2. \file cbitvector.h
  3. \author michael.zohner@ec-spride.de
  4. \copyright ABY - A Framework for Efficient Mixed-protocol Secure Two-party Computation
  5. Copyright (C) 2019 ENCRYPTO Group, TU Darmstadt
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published
  8. by the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. ABY is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. \brief CBitVector Implementation
  17. */
  18. #ifndef CBITVECTOR_H_
  19. #define CBITVECTOR_H_
  20. #include "typedefs.h"
  21. #include <cassert>
  22. #include <cstddef>
  23. // forward declarations
  24. class crypto;
  25. /** Class which defines the functionality of storing C-based Bits in vector type format.*/
  26. class CBitVector {
  27. public:
  28. //Constructor code begins here...
  29. /** Constructor which initializes the member variables bit pointer and size to NULL and zero respectively. */
  30. CBitVector();
  31. /**
  32. Overloaded constructor of class \link CBitVector \endlink which calls internally \link Create(std::size_t bits) \endlink
  33. \param bits - It is the number of bits which will be used to allocate the CBitVector with. For more info on how these bits are allocated refer to \link Create(std::size_t bits) \endlink
  34. */
  35. CBitVector(std::size_t bits);
  36. /**
  37. Overloaded constructor of class \link CBitVector \endlink which calls internally \link Create(std::size_t bits,crypto* crypt) \endlink
  38. \param bits - It is the number of bits which will be used to allocate the CBitVector with. For more info on how these bits are allocated refer to \link Create(std::size_t bits,crypto* crypt) \endlink
  39. \param crypt - This object from crypto class is used to generate pseudo random values for the cbitvector.
  40. */
  41. CBitVector(std::size_t bits, crypto* crypt);
  42. //Constructor code ends here...
  43. //Basic Primitive function of allocation and deallocation begins here.
  44. /**
  45. Function which gets called initially when the cbitvector object is created. This method is mostly called from constructor of CBitVector class.
  46. The method sets bit pointer and size to NULL and zero respectively.
  47. */
  48. void Init();
  49. /**
  50. Destructor which internally calls the delCBitVector for deallocating the space. This method internally calls
  51. \link delCBitVector() \endlink.
  52. */
  53. ~CBitVector();
  54. /**
  55. This method is used to deallocate the bit pointer and size explicitly. This method needs to be called by the programmer explicitly.
  56. */
  57. void delCBitVector();
  58. //Basic Primitive function of allocation and deallocation ends here.
  59. //Create function supported by CBitVector starts here...
  60. /**
  61. This method generates random values and assigns it to the bitvector using crypto object. If the bits provided in the params are greater
  62. than the bit size of the bitvector, then the bit vector is recreated with new bit size and filled in with random values.
  63. \param bits - It is the number of bits which will be used to allocate and assign random values of the CBitVector with. For more info on how these bits are allocated refer to \link Create(std::size_t bits) \endlink
  64. \param crypt - It is the crypto class object which is used to generate random values for the bit size.
  65. */
  66. void FillRand(std::size_t bits, crypto* crypt);
  67. /* Create in bits and bytes */
  68. /**
  69. This method is used to create the CBitVector with the provided bits. The method creates a bit vector of exactly ceil_divide(bits) size.
  70. For example, if bit size provided is 3 after this method is called it will be 8 bits = 1 byte.
  71. \param bits - It is the number of bits which will be used to allocate the CBitVector with.
  72. */
  73. void CreateExact(std::size_t bits);
  74. /**
  75. This method is used to create the CBitVector with the provided bits. The method creates a bit vector with a size close to AES Bitsize.
  76. For example, if bit size provided is 110. After this method is called it will be 128 bits. It will perform a ceil of provided_bit_size
  77. to AES bit size and multiply that ceiled value with AES bits size. (For reference, AES Bit size is taken as 128 bits)
  78. \param bits - It is the number of bits which will be used to allocate the CBitVector with.
  79. */
  80. void Create(std::size_t bits);
  81. /**
  82. This method is used to create the CBitVector with the provided byte size. The method creates a bit vector with a size close to AES Bytesize.
  83. For example, if byte size provided is 9. After this method is called it will be 16 bytes. It will perform a ceil of provided_byte_size
  84. to AES byte size and multiply that ceiled value with AES byte size. (For reference, AES Byte size is taken as 16 bytes). Internally, this method
  85. calls \link Create(std::size_t bits) \endlink. Therefore, for further info please refer to the internal method provided.
  86. \param bytes - It is the number of bytes which will be used to allocate the CBitVector with.
  87. */
  88. void CreateBytes(std::size_t bytes);
  89. /**
  90. This method is used to create the CBitVector with the provided byte size and fills it with random data from the crypt object. The method creates a
  91. bit vector with a size close to AES Bytesize. For example, if byte size provided is 9. After this method is called it will be 16 bytes. It will perform a ceil of provided_byte_size
  92. to AES byte size and multiply that ceiled value with AES byte size. (For reference, AES Byte size is taken as 16 bytes). Internally, this method
  93. calls \link Create(std::size_t bits, crypto* crypt) \endlink. Therefore, for further info please refer to the internal method provided.
  94. \param bytes - It is the number of bytes which will be used to allocate the CBitVector with.
  95. \param crypt - Reference to a crypto object from which fresh randomness is sampled
  96. */
  97. void CreateBytes(std::size_t bytes, crypto* crypt);
  98. /**
  99. This method is used to create the CBitVector with the provided bits and set them to value zero. The method creates a bit vector with a size close to AES Bitsize.
  100. And performs an assignment of zero to each bit being allocated. Internally, this method calls \link Create(std::size_t bits) \endlink. Therefore, for further info
  101. please refer to the internal method provided.
  102. \param bits - It is the number of bits which will be used to allocate and assign zero values of the CBitVector with.
  103. */
  104. void CreateZeros(std::size_t bits);
  105. /**
  106. This method is used to create the CBitVector with the provided bits and set them to some random values. The method creates a bit vector with a size close to AES Bitsize.
  107. And performs an assignment of random values to each bit being allocated. Internally, this method calls \link Create(std::size_t bits) \endlink and
  108. \link FillRand(std::size_t bits, crypto* crypt) \endlink. Therefore, for further info please refer to the internal method provided.
  109. \param bits - It is the number of bits which will be used to allocate and assign random values of the CBitVector with.
  110. \param crypt - It is the crypto class object which is used to generate random values for the bit size.
  111. */
  112. void Create(std::size_t bits, crypto* crypt);
  113. /**
  114. This method is used create the CBitVector with the provided number of elements and element length. This method basically creates a 1-dimensional array/vector with the provided
  115. element size and number of elements. This method internally calls \link Create(std::size_t bits) \endlink with arguments as elementlength*numelements.
  116. \param numelements - The number of elements in the 1-dimensional array/vector which gets created.
  117. \param elementlength - The size of element in the provided cbitvector.
  118. */
  119. void Create(std::size_t numelements, std::size_t elementlength);
  120. /**
  121. This method is used create the CBitVector with the provided number of elements and element length and then assign random values to them. This method basically creates
  122. a 1-dimensional array/vector with the provided element size and number of elements and assign some random values based on crypt object provided. This method internally
  123. calls \link Create(std::size_t bits, crypto* crypt) \endlink for creation of 1-d vector.
  124. \param numelements - The number of elements in the 1-dimensional array/vector which gets created.
  125. \param elementlength - The size of element in the provided cbitvector.
  126. \param crypt - It is the crypto class object which is used to generate random values for the provided bit size.
  127. */
  128. void Create(std::size_t numelements, std::size_t elementlength, crypto* crypt);
  129. /**
  130. This method is used create the CBitVector with the provided number of elements of 2 dimensions and element length. This method basically creates a 2-dimensional array/vector
  131. with the provided element size and number of elements in two dimensions. This method internally calls \link Create(std::size_t bits) \endlink with arguments as
  132. elementlength*numelementsDimA*numelementsDimB.
  133. \param numelementsDimA - The number of elements in the 1st-dimension of the 2d array/vector which gets created.
  134. \param numelementsDimB - The number of elements in the 2nd-dimension of the 2d array/vector which gets created.
  135. \param elementlength - The size of element in the provided cbitvector.
  136. */
  137. void Create(std::size_t numelementsDimA, std::size_t numelementsDimB, std::size_t elementlength);
  138. /**
  139. This method is used create the CBitVector with the provided number of elements of 2 dimensions and element length, and then assign random values to them. This method basically
  140. creates a 2-dimensional array/vector with the provided element size and number of elements in two dimensions and assign some random values based on crypt object provided.
  141. This method internally calls \link Create(std::size_t bits, crypto* crypt) \endlink.
  142. \param numelementsDimA - The number of elements in the 1st-dimension of the 2d array/vector which gets created.
  143. \param numelementsDimB - The number of elements in the 2nd-dimension of the 2d array/vector which gets created.
  144. \param elementlength - The size of element in the provided cbitvector.
  145. \param crypt - It is the crypto class object which is used to generate random values for the provided bit size.
  146. */
  147. void Create(std::size_t numelementsDimA, std::size_t numelementsDimB, std::size_t elementlength, crypto* crypt);
  148. //Create function supported by CBitVector ends here...
  149. /*
  150. * Management operations
  151. */
  152. /**
  153. This method is used to resize the bytes allocated to CBitVector with newly provided size. And also accommodate the data from previous allocation to new one.
  154. \param newSizeBytes - This variable provides the new size to which the cbitvector needs to be modified to user's needs.
  155. */
  156. void ResizeinBytes(std::size_t newSizeBytes);
  157. /**
  158. This method is used to reset the values in the given CBitVector. This method sets all bit values to zeros. This is a slight variant of the method
  159. \link CreateZeros(std::size_t bits) \endlink. The create method mentioned above allocates and sets value to zero. Whereas the provided method only
  160. sets the value to zero.
  161. */
  162. void Reset();
  163. /**
  164. This method is used to reset the values in the given CBitVector for specific byte range.
  165. \param frombyte - The source byte position from which the values needs to be reset.
  166. \param tobyte - The destination byte position until which the values needs to be reset to.
  167. */
  168. void ResetFromTo(std::size_t frombyte, std::size_t tobyte);
  169. /**
  170. This method sets all bit position values in a CBitVector to One.
  171. */
  172. void SetToOne();
  173. /**
  174. This method sets all bits in the CBitVector to the inverse
  175. */
  176. void Invert();
  177. /**
  178. This is a getter method which returns the size of the CBitVector in bytes.
  179. \return the byte size of CBitVector.
  180. */
  181. std::size_t GetSize() const;
  182. /**
  183. This method checks if two CBitVectors are equal or not.
  184. \param vec - Vector to be checked with current one for the case of equality.
  185. \return boolean value which says whether it is equal or not.
  186. */
  187. BOOL IsEqual(const CBitVector& vec) const;
  188. /**
  189. This method checks if two CBitVectors are equal or not for a given range of bit positions.
  190. \param vec - Vector to be checked with current one for the case of equality.
  191. \param from - Bit Position from which the vectors need to be checked for equality.
  192. \param to - Bit Position until which the vectors need to be checked for equality.
  193. \return boolean value which says whether the vectors are equal or not in the provided range of bits.
  194. */
  195. BOOL IsEqual(const CBitVector& vec, std::size_t from, std::size_t to) const;
  196. /**
  197. This method sets the element length of the CBitVector. It can be used to modify the object size in a CBitVector when
  198. around with the multi dimensional arrays/vectors.
  199. \param elelen - New element length which can be used to set the object size in a CBitVector.
  200. */
  201. void SetElementLength(std::size_t elelen);
  202. /**
  203. This method gets the element length of the CBitVector.
  204. \return element length of the elements in CBitVector.
  205. */
  206. std::size_t GetElementLength() const;
  207. /*
  208. * Copy operations
  209. */
  210. /**
  211. This method is used to copy the provided CBitVector to itself. It internally calls
  212. \link Copy(BYTE* p, int pos, int len) \endlink for copying bytewise.
  213. \param vec - The vector from which the copying needs to be performed.
  214. */
  215. void Copy(const CBitVector& vec);
  216. /**
  217. This method is used to copy the provided CBitVector to itself for a given range. It internally calls \link Copy(BYTE* p, int pos, int len) \endlink
  218. for copying bytewise. Copying is done in a slightly different way. Here the range is pos and len. The offset is defined for the base vector and not
  219. for the copying vector. So if the method is called as B.Copy(A,5,10) then, values of vector A will be copied from first index location for length 10
  220. to the vector B from position 5 for length 10. Unlike copying values from 5 position in vector A to vector B for length 10.
  221. \param vec - The vector from which the copying needs to be performed.
  222. \param pos - The positional offset for copying into current vector.
  223. \param len - Length or amount of values to be copied to the current vector from provided vector.
  224. */
  225. void Copy(const CBitVector& vec, std::size_t pos, std::size_t len);
  226. /**
  227. This method is used to copy the current CBitVector with some ByteLocation with positional shift and length. This method is the base method for methods
  228. \link Copy(CBitVector& vec, int pos, int len) \endlink and \link Copy(CBitVector& vec) \endlink.
  229. \param p - Pointer to the byte location to be copied to the CBitVector.
  230. \param pos - Positional offset for copying into current CBitVector.
  231. \param len - Length or amount of values to be copied to the current vector from provided byte location.
  232. */
  233. void Copy(const BYTE* p, std::size_t pos, std::size_t len);
  234. /**
  235. This method performs OR operation bytewise with the current CBitVector at the provided byte position with another Byte object.
  236. \param pos - Byte position in the CBitVector which is used to perform OR operation with.
  237. \param p - Byte with which the OR operation is performed to get the result.
  238. */
  239. void ORByte(std::size_t pos, BYTE p);
  240. /*
  241. * Bitwise operations
  242. */
  243. /**
  244. This method gets the bit in the provided index by using the maskbits. The maskbits brings the concept of
  245. endianness in the vector. In this method MASK_BIT is used to extract the bits which are assumed to be
  246. organized in Little Endian form.
  247. \param idx - Bit Index which needs to be fetched from the CBitVector.
  248. \return The byte which has got just the bit in it.
  249. */
  250. BYTE GetBit(std::size_t idx) const;
  251. /**
  252. This method sets the bit in the provided index by using the maskbits and the provided bit. The maskbits brings the concept of
  253. endianness in the vector. In this method C_MASK_BIT is used to figure out the bits which are assumed to be
  254. organized in Little Endian form.
  255. \param idx - Bit Index which needs to be written to in the CBitVector.
  256. \param b - The bit which being written in the provided index.
  257. */
  258. void SetBit(std::size_t idx, BYTE b);
  259. /**
  260. This method gets the bit in the provided index without using the maskbits. The maskbits brings the concept of
  261. endianness in the vector. In this method mask bits are not used so the vector is treated in Big Endian form.
  262. \param idx - Bit Index which needs to be fetched from the CBitVector.
  263. \return The byte which has got just the bit in it.
  264. */
  265. BYTE GetBitNoMask(std::size_t idx) const;
  266. /**
  267. This method sets the bit in the provided index without using the maskbits. The maskbits brings the concept of
  268. endianness in the vector. In this method mask bits are not used so the vector is treated in Big Endian form.
  269. \param idx - Bit Index which needs to be written to in the CBitVector.
  270. \param b - The bit which being written in the provided index.
  271. */
  272. void SetBitNoMask(std::size_t idx, BYTE b);
  273. /**
  274. This method XORs the bit in the provided index without using the maskbits. The maskbits brings the concept of
  275. endianness in the vector. In this method mask bits are not used so the vector is treated in Big Endian form.
  276. \param idx - Bit Index which needs to be XORed to in the CBitVector.
  277. \param b - The bit which being XORed in the provided index.
  278. */
  279. void XORBitNoMask(std::size_t idx, BYTE b);
  280. /*
  281. * Single byte operations
  282. */
  283. /**
  284. This method sets a byte in a given index of the CBitVector with the provided Byte.
  285. \param idx - Index where the byte needs to be set.
  286. \param p - Byte which needs to be copied to.
  287. */
  288. void SetByte(std::size_t idx, BYTE p);
  289. /**
  290. This method returns the byte at the given index in the CBitVector. Here the index is w.r.t bytes.
  291. \param idx - Index of the byte which needs to be returned from the CBitVector.
  292. \return Byte is returned from CBitVector at the given index.
  293. */
  294. BYTE GetByte(std::size_t idx) const;
  295. /**
  296. Not Used Currently in Framework.
  297. This method performs XOR operation at the given index in the CBitVector with a provided Byte.
  298. \param idx - Index of the byte which needs to be XORed inside the CBitVector.
  299. \param b - Byte to be XORed with the CBitVector.
  300. */
  301. void XORByte(std::size_t idx, BYTE b);
  302. /**
  303. This method performs AND operation at the given index in the CBitVector with a provided Byte.
  304. \param idx - Index of the byte which needs to be ANDed inside the CBitVector.
  305. \param b - Byte to be ANDed with the CBitVector.
  306. */
  307. void ANDByte(std::size_t idx, BYTE b);
  308. /*
  309. * Get Operations
  310. */
  311. /**
  312. This method gets elements from the CBitVector bitwise from a given offset for a given length. And stores the result
  313. in the provided byte pointer. This method is used by the generic method \link Get(int pos, int len) \endlink
  314. \param p - The resulting bits for the given range in the CBitVector is stored in the byte pointer p.
  315. \param pos - The positional offset in the CBitVector from which the data needs to obtained.
  316. \param len - The range limit of obtaining the data from the CBitVector.
  317. */
  318. void GetBits(BYTE* p, std::size_t pos, std::size_t len) const;
  319. /**
  320. This method gets elements from the CBitVector bytewise from a given offset for a given length. And stores the result
  321. in the provided byte pointer.
  322. \param p - The resulting bits for the given range in the CBitVector is stored in the byte pointer p.
  323. \param pos - The positional offset in the CBitVector from which the data needs to obtained.
  324. \param len - The range limit of obtaining the data from the CBitVector.
  325. */
  326. void GetBytes(BYTE* p, std::size_t pos, std::size_t len) const;
  327. /**
  328. Generic method which performs the operation of getting values from a CBitVector for a given bit position and length.
  329. This method internally calls \link GetBits(BYTE* p, int pos, int len) \endlink.
  330. \param pos - The positional offset in the CBitVector from which the data needs to obtained.
  331. \param len - The range limit of obtaining the data from the CBitVector.
  332. \return returns the value/values for the provided range.
  333. */
  334. template<class T> T Get(std::size_t pos, std::size_t len) const {
  335. assert(len <= sizeof(T) * 8);
  336. T val = 0;
  337. GetBits((BYTE*) &val, pos, len);
  338. return val;
  339. }
  340. /*
  341. * Set Operations
  342. */
  343. /**
  344. The method for setting CBitVector for a given bit range with offset and length in unsigned 64bit integer format. This method
  345. is called from \link SetBits(BYTE* p, int pos, int len) \endlink and \link Set(T val, int pos, int len) \endlink.
  346. \param p - Byte array passed to be set to the current CBitVector.
  347. \param pos - Positional offset in the CBitVector, where data will be set from the provided byte array.
  348. \param len - The range limit of obtaining the data from the CBitVector.
  349. */
  350. void SetBits(const BYTE* p, std::size_t pos, std::size_t len);
  351. /**
  352. The method for setting CBitVector for a given bit range with offsets and length with another Byte Array.
  353. \param p - Byte array passed to be set with the current CBitVector.
  354. \param ppos - Positional offset in the Byte Array.
  355. \param pos - Positional offset in the CBitVector, where data will be set from the provided byte array.
  356. \param len - The range limit of obtaining the data from the CBitVector.
  357. */
  358. void SetBitsPosOffset(const BYTE* p, std::size_t ppos, std::size_t pos, std::size_t len);
  359. /**
  360. The method for setting CBitVector for a given byte range with offset and length. This method internally calls the method
  361. \link SetBytes(T* dst, T* src, T* lim) \endlink.
  362. \param src - Byte array passed to be set to the current CBitVector.
  363. \param pos - Byte position offset in the CBitVector, where data will be set from the provided byte array.
  364. \param len - The number of bytes to be set.
  365. */
  366. void SetBytes(const BYTE* src, std::size_t pos, std::size_t len);
  367. /**
  368. This method sets the values in a given byte range to Zero in the current CBitVector.
  369. \param bytepos - Byte Positional offset in the CBitVector.
  370. \param bytelen - Byte Length in the CBitVector until which the value needs to be set to zero.
  371. */
  372. void SetBytesToZero(std::size_t bytepos, std::size_t bytelen);
  373. /**
  374. Generic method which performs the operation of setting values to a CBitVector for a given bit position and length.
  375. This method internally calls \link SetBits(BYTE* p, std::size_t pos, std::size_t len) \endlink.
  376. \param pos - The positional offset in the CBitVector from which the data needs to obtained.
  377. \param len - The range limit of obtaining the data from the CBitVector.
  378. */
  379. template<class T> void Set(T val, std::size_t pos, std::size_t len) {
  380. assert(len <= sizeof(T) * 8);
  381. SetBits((BYTE*) &val, pos, len);
  382. }
  383. /**
  384. This method sets the values in a given bit range to Zero in the current CBitVector.
  385. \param bitpos - Bit Positional offset in the CBitVector.
  386. \param bitlen - Bit Length in the CBitVector until which the value needs to be set to zero.
  387. */
  388. void SetBitsToZero(std::size_t bitpos, std::size_t bitlen);
  389. /*
  390. * XOR Operations
  391. */
  392. /**
  393. This method performs XOR operation from a given position in the CBitVector with a provided Byte Array with a length.
  394. This method is called from \link XORBytes(BYTE* p, int len) \endlink. This method internally calls \link XORBytes(T* dst, T* src, T* lim) \endlink.
  395. \param p - Byte Array to be XORed with the CBitVector range.
  396. \param pos - Positional offset for XORing into current CBitVector.
  397. \param len - Length or amount of values to be XORed to the current vector from provided byte location.
  398. */
  399. void XORBytes(const BYTE* p, std::size_t pos, std::size_t len);
  400. /**
  401. This method performs XOR operation for a given length in the CBitVector with a provided Byte Array. This method internally calls
  402. \link XORBytes(BYTE* p, int pos, int len) \endlink.
  403. \param p - Byte Array to be XORed with the CBitVector range.
  404. \param len - Length or amount of values to be XORed to the current vector from provided byte location.
  405. */
  406. void XORBytes(const BYTE* p, std::size_t len);
  407. /**
  408. Not Used in the Framework.
  409. This method performs XOR operation from a given position in the CBitVector with another CBitVector with a length.
  410. This method internally calls \link XORBytes(BYTE* p, int pos, int len) \endlink.
  411. \param vec - Provided Array to be XORed with the CBitVector.
  412. \param pos - Positional offset for XORing into current CBitVector.
  413. \param len - Length or amount of values to be XORed to the current vector from provided byte location.
  414. */
  415. void XORVector(const CBitVector &vec, std::size_t pos, std::size_t len);
  416. /**
  417. Generic method which is used to XOR bit wise the CBitVector. This method internally calls
  418. \link XORBits(BYTE* p, int pos, int len) \endlink.
  419. */
  420. template<class T> void XOR(T val, std::size_t pos, std::size_t len) {
  421. assert(len <= sizeof(T) * 8);
  422. XORBits((BYTE*) &val, pos, len);
  423. }
  424. /**
  425. The method for XORing CBitVector for a given bit range with offset and length. This method is called from
  426. \link XOR(T val, int pos, int len) \endlink.
  427. \param p - Byte array passed to be XORed with the current CBitVector.
  428. \param pos - Positional offset in the CBitVector, where data will be XORed from the provided byte array.
  429. \param len - The range limit of obtaining the data from the CBitVector.
  430. */
  431. void XORBits(const BYTE* p, std::size_t pos, std::size_t len);
  432. /**
  433. The method for XORing CBitVector for a given bit range with offsets and length with another Byte Array.
  434. \param p - Byte array passed to be XORed with the current CBitVector.
  435. \param ppos - Positional offset in the Byte Array.
  436. \param pos - Positional offset in the CBitVector, where data will be XORed from the provided byte array.
  437. \param len - The range limit of obtaining the data from the CBitVector.
  438. */
  439. void XORBitsPosOffset(const BYTE* p, std::size_t ppos, std::size_t pos, std::size_t len);
  440. /**
  441. Set the value of this CBitVector to this XOR b
  442. \param b - Pointer to a CBitVector which is XORed on this CBitVector
  443. */
  444. void XOR(const CBitVector* b);
  445. /**
  446. This method performs XOR operation from a given position in the CBitVector with a provided Byte Array with a length.
  447. The XORing is performed in a slightly different way. The byte array is reversed before it is XORed with the CBitVector.
  448. This method is called from \link XORBytes(BYTE* p, int len) \endlink. This method internally calls \link XORBytes(T* dst, T* src, T* lim) \endlink.
  449. \param p - Byte Array to be XORed with the CBitVector range.
  450. \param pos - Positional offset for XORing into current CBitVector.
  451. \param len - Length or amount of values to be XORed to the current vector from provided byte location.
  452. */
  453. void XORBytesReverse(const BYTE* p, std::size_t pos, std::size_t len);
  454. /*
  455. * AND Operations
  456. */
  457. /**
  458. This method performs AND operation from a given position in the CBitVector with a provided Byte Array with a length.
  459. This method internally calls \link ANDBytes(T* dst, T* src, T* lim) \endlink.
  460. \param p - Byte Array to be ANDed with the CBitVector range.
  461. \param pos - Positional offset for ANDing into current CBitVector.
  462. \param len - Length or amount of values to be ANDed to the current vector from provided byte location.
  463. */
  464. void ANDBytes(const BYTE* p, std::size_t pos, std::size_t len);
  465. /*
  466. * Set operations
  467. */
  468. /**
  469. This method is used to set and XOR a CBitVector with a byte array and then XOR it with another byte array
  470. for a given range. This method internally calls \link Copy(BYTE* p, int pos, int len) \endlink and
  471. \link XORBytes(BYTE* p, int pos, int len) \endlink.
  472. \param p - Pointer to the byte location to be copied to the CBitVector.
  473. \param q - Pointer to the byte location with which the CBitVector is XORed with.
  474. \param pos - Positional offset for copying and XORing into current CBitVector.
  475. \param len - Length or amount of values to be copied and XORed to the current vector from provided byte location.
  476. */
  477. void SetXOR(const BYTE* p, const BYTE* q, std::size_t pos, std::size_t len);
  478. /**
  479. This method is used to set and AND a CBitVector with a byte array and then AND it with another byte array
  480. for a given range. This method internally calls \link Copy(BYTE* p, int pos, int len) \endlink and
  481. \link ANDBytes(BYTE* p, int pos, int len) \endlink.
  482. \param p - Pointer to the byte location to be copied to the CBitVector.
  483. \param q - Pointer to the byte location with which the CBitVector is ANDed with.
  484. \param pos - Positional offset for copying and ANDing into current CBitVector.
  485. \param len - Length or amount of values to be copied and ANDed to the current vector from provided byte location.
  486. */
  487. void SetAND(const BYTE* p, const BYTE* q, std::size_t pos, std::size_t len);
  488. /**
  489. Set the value of this CBitVector to this AND b
  490. \param b - Pointer to a CBitVector which is ANDed on this CBitVector
  491. */
  492. void AND(const CBitVector* b);
  493. /**
  494. Cyclic shift left by pos positions
  495. \param pos - the left shift value
  496. */
  497. void CLShift(std::size_t pos);
  498. /*
  499. * Buffer access operations
  500. */
  501. /**
  502. This method returns CBitVector in byte array format. This is very widely used method.
  503. */
  504. BYTE* GetArr();
  505. const BYTE* GetArr() const;
  506. /**
  507. This method is used to attach a new buffer into the CBitVector provided as arguments to this method.
  508. \param p - Pointer to the byte location to be attached to the CBitVector.
  509. \param size - Number of bytes attached from the provided buffer.
  510. */
  511. void AttachBuf(BYTE* p, std::size_t size = 0);
  512. /**
  513. This method is used to detach the buffer from the CBitVector. */
  514. void DetachBuf();
  515. /*
  516. * Print Operations
  517. */
  518. /**
  519. This method prints the CBitVector bitwise for provided bit range. This method internally calls \link GetBitNoMask(int idx) \endlink.
  520. This method is called from \link PrintBinary() \endlink.
  521. \param fromBit - The bit from which the printing starts in a CBitVector.
  522. \param toBit - The bit until which the printing in a CBitVector is done.
  523. */
  524. void Print(std::size_t fromBit, std::size_t toBit);
  525. /**
  526. This method prints the CBitVector in Hexadecimal format.
  527. */
  528. void PrintHex(bool linebreak = true);
  529. /**
  530. This method prints the CBitVector in Hexadecimal format for the provided byte range.
  531. \param fromByte - The byte from which the printing of CBitVector begins.
  532. \param toByte - The byte until which the printing of CBitVector is done.
  533. */
  534. void PrintHex(std::size_t fromByte, std::size_t toByte, bool linebreak = true);
  535. /**
  536. This method prints the CBitVector in Binary format. This method internally calls \link Print(int fromBit, int toBit) \endlink.
  537. */
  538. void PrintBinary();
  539. /**
  540. This method is a more abstract printing method which is used to print the CBitVector even if the vector is a simple 1 bit based
  541. vector or 1-d array/vector or even a 2-d vector/array. This method internally calls methods \link Get(int i) \endlink and
  542. \link Get2D(int i, int j) \endlink.
  543. */
  544. void PrintContent();
  545. /**
  546. This method prints the CBitVector bitwise for provided bit range with mask. This method internally calls \link GetBit(int idx) \endlink.
  547. \param fromBit - The bit from which the printing starts in a CBitVector.
  548. \param toBit - The bit until which the printing in a CBitVector is done.
  549. */
  550. void PrintBinaryMasked(std::size_t from, std::size_t to);
  551. /*
  552. * If the cbitvector is abstracted to an array of elements with m_nElementLength bits size, these methods can be used for easier access
  553. */
  554. /**
  555. Generic method which provides more abstraction for getting elements in the CBitVector. It is mainly used for getting values which are
  556. 1-dimensional in nature. This method internally calls \link Get(int pos, int len) \endlink.
  557. \param i - Index from which data needs to be fetched.
  558. */
  559. template<class T> T Get(std::size_t i) const {
  560. return Get<T>(i * m_nElementLength, m_nElementLength);
  561. }
  562. /**
  563. Generic method which provides more abstraction for setting elements in the CBitVector. It is mainly used for getting values which are
  564. 1-dimensional in nature. This method internally calls \link Set(int pos, int len) \endlink.
  565. \param val - Value which needs to be written to the given location.
  566. \param i - Index to which data needs to be written to.
  567. */
  568. template<class T> void Set(T val, std::size_t i) {
  569. Set<T>(val, i * m_nElementLength, m_nElementLength);
  570. }
  571. /*
  572. * The same as the above methods only for two-dimensional access
  573. */
  574. /**
  575. Generic method which provides more abstraction for getting elements in the CBitVector. It is mainly used for getting values which are
  576. 2-dimensional in nature. This method internally calls \link Get(int pos, int len) \endlink.
  577. \param i - Row index from which the data needs to be read.
  578. \param j - Column index from which the data needs to be read.
  579. */
  580. template<class T> T Get2D(std::size_t i, std::size_t j) const {
  581. return Get<T>((i * m_nNumElementsDimB + j) * m_nElementLength, m_nElementLength);
  582. }
  583. /**
  584. Generic method which provides more abstraction for setting elements in the CBitVector. It is mainly used for getting values which are
  585. 2-dimensional in nature. This method internally calls \link Set(int pos, int len) \endlink.
  586. \param val - Value which needs to be written to the given location.
  587. \param i - Row index from which the data needs to be written.
  588. \param j - Column index from which the data needs to be written.
  589. */
  590. template<class T> void Set2D(T val, std::size_t i, std::size_t j) {
  591. Set<T>(val, (i * m_nNumElementsDimB + j) * m_nElementLength, m_nElementLength);
  592. }
  593. //useful when accessing elements using an index
  594. //View the cbitvector as a rows x columns matrix and transpose
  595. void Transpose(std::size_t rows, std::size_t columns);
  596. void SimpleTranspose(std::size_t rows, std::size_t columns);
  597. void EklundhBitTranspose(std::size_t rows, std::size_t columns);
  598. //private:
  599. public:
  600. BYTE* m_pBits; /** Byte pointer which stores the CBitVector as simple byte array. */
  601. std::size_t m_nByteSize; /** Byte size variable which stores the size of CBitVector in bytes. */
  602. std::size_t m_nBits; //The exact number of bits
  603. std::size_t m_nElementLength; /** Size of elements in the CBitVector. By default, it is set to 1. It is used
  604. differently when it is used as 1-d or 2-d custom vector/array. */
  605. std::size_t m_nNumElements; /** Number elements in the first dimension in the CBitVector. */
  606. std::size_t m_nNumElementsDimB;/** Number elements in the second dimension in the CBitVector. */
  607. };
  608. #endif /* BITVECTOR_H_ */