I have some file. In the loop I write a 128 bit block from it, into an array of 4 * 32 bits (say, for subsequent AES encryption).
It is necessary to implement the method of addition 2 (one bit + zeros to the end of the block). I wrote this code (of course, inside the loop as the file is read):
std::array<int32_t, 4> block; input.read(reinterpret_cast<char *>(&block), sizeof block); if (static_cast<unsigned long>(input.gcount()) < sizeof block) { int16_t *tmp = reinterpret_cast<int16_t *>(block.begin()); tmp[input.gcount() - 1] = 0x1000; for (auto i = input.gcount(); i < 16; ++i) { tmp[i] = 0x0000; } } But it turns out incorrectly - too much is cut off, or the data from the previous block generally gets into the block. What have I done wrong? Maybe there is a more elegant method?
int16_twith a block size of 128 bits. Those. going beyond the boundaries, which means UB. Well,std::arraysomehow out of place here. Plus, what happens ifgcountreturns 0? Another go beyond the boundaries. - 伪位蔚蠂慰位蠀蟿std::arraydidn't please? - ixScibeginto replace with[0], but this is not a reason to refusestd::array. - ixSci