Skip to content

Commit 0fca875

Browse files
committed
process rol: precomputing single rotations in static code
1 parent 1d6f614 commit 0fca875

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

kaitai/kaitaistream.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,19 @@ std::string kaitai::kstream::process_xor_many(std::string data, std::string key)
460460
return result;
461461
}
462462

463+
// NOTE: perhaps it should be heap allocated?
464+
uint8_t precomputedSingleRotations[8][256];
465+
466+
// NOTE: code based on StackOverflow answer at https://stackoverflow.com/a/34321324/2375119
467+
computeSingleRotations {
468+
for (int amount = 1; amount < 8; amount++) {
469+
int anti_amount = 8 - amount;
470+
for (uint8_t i = 0; i < 256; i++) {
471+
precomputedSingleRotations[amount][i] = (uint8_t)((i << amount) | (i >> anti_amount));
472+
}
473+
}
474+
}
475+
463476
std::string kaitai::kstream::process_rotate_left(std::string data, int amount, int groupSize = 1) {
464477
if (groupSize < 1)
465478
throw std::runtime_error("process_rotate_left: groupSize must be at least 1");
@@ -473,11 +486,8 @@ std::string kaitai::kstream::process_rotate_left(std::string data, int amount, i
473486
std::string result(len, ' ');
474487

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

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

0 commit comments

Comments
 (0)