1
0
mirror of https://git.disroot.org/80486DX2-66/polonium.git synced 2024-12-27 07:30:23 +05:30

corrupter.c: corrupt_byte(): optimization: use bitwise mask

This commit is contained in:
Intel A80486DX2-66 2024-11-20 20:09:43 +03:00
parent 0e1400fb80
commit 1890b3d294
Signed by: 80486DX2-66
GPG Key ID: 83631EF27054609B

View File

@ -59,27 +59,23 @@ enum Interoperation_Result corrupt_byte(Interoperation_Input_Vars) {
return INTEROPERATION_SUCCESS;
// generate bit mask
byte damage_left = param->threshold;
bool bit_mask[CHAR_BIT] = {false};
byte threshold = param->threshold,
damage_left = threshold,
bit_mask = 0;
uint16_t probability = param->probability;
for (byte bit = 0; bit < CHAR_BIT; bit++)
if (get_chance(probability) && damage_left > 0) {
bit_mask[bit] = true;
bit_mask |= 1 << bit;
damage_left--;
}
bool damaged_byte = false;
file_offset_t threshold = (file_offset_t) param->threshold;
bit_mask &= (1 << threshold) - 1;
for (byte bit = 0; bit < threshold; bit++) {
if (bit_mask[bit] == false)
continue;
bool damaged_byte = (byte_value ^ bit_mask) != byte_value;
byte_value = FLIP_BIT(byte_value, bit);
damaged_byte = true;
}
// apply bit mask
byte_value ^= bit_mask;
if (damaged_byte) {
// write the modified byte back to the file