Skip to content

Commit

Permalink
process rol: precomputing single rotations in static code
Browse files Browse the repository at this point in the history
  • Loading branch information
arekbulski committed Apr 9, 2018
1 parent 1d6f614 commit 0fca875
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions kaitai/kaitaistream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,19 @@ std::string kaitai::kstream::process_xor_many(std::string data, std::string key)
return result;
}

// NOTE: perhaps it should be heap allocated?
uint8_t precomputedSingleRotations[8][256];

// NOTE: code based on StackOverflow answer at https://stackoverflow.com/a/34321324/2375119
computeSingleRotations {
for (int amount = 1; amount < 8; amount++) {
int anti_amount = 8 - amount;
for (uint8_t i = 0; i < 256; i++) {
precomputedSingleRotations[amount][i] = (uint8_t)((i << amount) | (i >> anti_amount));
}
}
}

std::string kaitai::kstream::process_rotate_left(std::string data, int amount, int groupSize = 1) {
if (groupSize < 1)
throw std::runtime_error("process_rotate_left: groupSize must be at least 1");
Expand All @@ -473,11 +486,8 @@ std::string kaitai::kstream::process_rotate_left(std::string data, int amount, i
std::string result(len, ' ');

if (groupSize == 1) {
int anti_amount = 8 - amount;
uint8_t translate[256];
for (uint8_t i = 0; i < 256; i++) {
translate[i] = (uint8_t)((i << amount) | (i >> anti_amount));
}
// NOTE: perhaps its `amount * 256` in the index?
uint8_t *translate = &precomputedSingleRotations[amount];

for (size_t i = 0; i < len; i++) {
result[i] = translate[data[i]];
Expand Down

0 comments on commit 0fca875

Please sign in to comment.