python-registry icon indicating copy to clipboard operation
python-registry copied to clipboard

Use mask and flags to check if value data is resident

Open williballenthin opened this issue 10 years ago • 5 comments

Use mask and flags to check if value data is resident, rather than weird arithmetic operations modulo 0x80000000.

for example: https://github.com/williballenthin/python-registry/blob/master/Registry/RegistryParse.py#L762

williballenthin avatar Feb 25 '15 13:02 williballenthin

I just set a bool, dataIsResident, in my project based on the high bit being set. much easier vs >= stuff

EricZimmerman avatar Feb 25 '15 14:02 EricZimmerman

0x80000000 is used in multiple places and should probably be declared as an const instead? In some places maybe decoding as an int instead of dword and then checking for negative would be preferable?

NiKiZe avatar Feb 25 '15 16:02 NiKiZe

best IMO to convert it to 32 char binary string and check the left most flag. if 1, then its resident. Thats the point of the thing. checking > than 0x80000000 just makes it muddy

bool dataIsResident = Convert.ToString(_dataLengthInternal, 2).PadLeft(32, '0').StartsWith("1");

EricZimmerman avatar Feb 25 '15 17:02 EricZimmerman

Involving strings is an terrible idea when working with bits/flags.

instead check for the bit (psuedo/C mix)

Const DWORD_SIGN_MASK = 0x80000000
Const DWORD_VALUE_MASK = 0xefffffff
....
dataIsResident = _dataLengthInternal & DWORD_SIGN_MASK == DWORD_SIGN_MASK
dataLength = _dataLengthInternal & DWORD_VALUE_MASK

Using absolute value of signed ints in some places where the flag is not used (an example of this is cell size) would be clerear to a reader that does not understand binary operations.

NiKiZe avatar Feb 25 '15 18:02 NiKiZe

swapped out string stuff for bitwise ANDING. thanks!

EricZimmerman avatar Feb 25 '15 19:02 EricZimmerman