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:
parent
0e1400fb80
commit
1890b3d294
@ -59,27 +59,23 @@ enum Interoperation_Result corrupt_byte(Interoperation_Input_Vars) {
|
|||||||
return INTEROPERATION_SUCCESS;
|
return INTEROPERATION_SUCCESS;
|
||||||
|
|
||||||
// generate bit mask
|
// generate bit mask
|
||||||
byte damage_left = param->threshold;
|
byte threshold = param->threshold,
|
||||||
bool bit_mask[CHAR_BIT] = {false};
|
damage_left = threshold,
|
||||||
|
bit_mask = 0;
|
||||||
uint16_t probability = param->probability;
|
uint16_t probability = param->probability;
|
||||||
|
|
||||||
for (byte bit = 0; bit < CHAR_BIT; bit++)
|
for (byte bit = 0; bit < CHAR_BIT; bit++)
|
||||||
if (get_chance(probability) && damage_left > 0) {
|
if (get_chance(probability) && damage_left > 0) {
|
||||||
bit_mask[bit] = true;
|
bit_mask |= 1 << bit;
|
||||||
damage_left--;
|
damage_left--;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool damaged_byte = false;
|
bit_mask &= (1 << threshold) - 1;
|
||||||
file_offset_t threshold = (file_offset_t) param->threshold;
|
|
||||||
|
|
||||||
for (byte bit = 0; bit < threshold; bit++) {
|
bool damaged_byte = (byte_value ^ bit_mask) != byte_value;
|
||||||
if (bit_mask[bit] == false)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
byte_value = FLIP_BIT(byte_value, bit);
|
// apply bit mask
|
||||||
|
byte_value ^= bit_mask;
|
||||||
damaged_byte = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (damaged_byte) {
|
if (damaged_byte) {
|
||||||
// write the modified byte back to the file
|
// write the modified byte back to the file
|
||||||
|
Loading…
Reference in New Issue
Block a user