|
| 1 | +from shared import OpenPAYGOTokenShared |
| 2 | +from shared_extended import OpenPAYGOTokenSharedExtended |
| 3 | + |
| 4 | + |
| 5 | +class OpenPAYGOTokenDecoder(object): |
| 6 | + MAX_TOKEN_JUMP = 64 |
| 7 | + MAX_TOKEN_JUMP_COUNTER_SYNC = 100 |
| 8 | + MAX_UNUSED_OLDER_TOKENS = 8*2 |
| 9 | + |
| 10 | + @classmethod |
| 11 | + def get_activation_value_count_and_type_from_token(cls, token, starting_code, key, last_count, |
| 12 | + restricted_digit_set=False, used_counts=None): |
| 13 | + if restricted_digit_set: |
| 14 | + token = OpenPAYGOTokenShared.convert_from_4_digit_token(token) |
| 15 | + valid_older_token = False |
| 16 | + token_base = OpenPAYGOTokenShared.get_token_base(token) # We get the base of the token |
| 17 | + current_code = OpenPAYGOTokenShared.put_base_in_token(starting_code, token_base) # We put it into the starting code |
| 18 | + starting_code_base = OpenPAYGOTokenShared.get_token_base(starting_code) # We get the base of the starting code |
| 19 | + value = cls._decode_base(starting_code_base, token_base) # If there is a match we get the value from the token |
| 20 | + # We try all combination up until last_count + TOKEN_JUMP, or to the larger jump if syncing counter |
| 21 | + # We could start directly the loop at the last count if we kept the token value for the last count |
| 22 | + if value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE: |
| 23 | + max_count_try = last_count + cls.MAX_TOKEN_JUMP_COUNTER_SYNC + 1 |
| 24 | + else: |
| 25 | + max_count_try = last_count + cls.MAX_TOKEN_JUMP + 1 |
| 26 | + for count in range(0, max_count_try): |
| 27 | + masked_token = OpenPAYGOTokenShared.put_base_in_token(current_code, token_base) |
| 28 | + if count % 2: |
| 29 | + this_type = OpenPAYGOTokenShared.TOKEN_TYPE_SET_TIME |
| 30 | + else: |
| 31 | + this_type = OpenPAYGOTokenShared.TOKEN_TYPE_ADD_TIME |
| 32 | + if masked_token == token: |
| 33 | + if cls._count_is_valid(count, last_count, value, this_type, used_counts): |
| 34 | + return value, count, this_type |
| 35 | + else: |
| 36 | + valid_older_token = True |
| 37 | + current_code = OpenPAYGOTokenShared.generate_next_token(current_code, key) # If not we go to the next token |
| 38 | + if valid_older_token: |
| 39 | + return -2, None, None |
| 40 | + return None, None, None |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def _count_is_valid(cls, count, last_count, value, type, used_counts): |
| 44 | + if value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE: |
| 45 | + if count > last_count - 30: |
| 46 | + return True |
| 47 | + elif count > last_count: |
| 48 | + return True |
| 49 | + elif cls.MAX_UNUSED_OLDER_TOKENS > 0: |
| 50 | + if count > last_count - cls.MAX_UNUSED_OLDER_TOKENS: |
| 51 | + if count not in used_counts and type == OpenPAYGOTokenShared.TOKEN_TYPE_ADD_TIME: |
| 52 | + return True |
| 53 | + return False |
| 54 | + |
| 55 | + @classmethod |
| 56 | + def update_used_counts(cls, past_used_counts, value, new_count, type): |
| 57 | + highest_count = max(past_used_counts) if past_used_counts else 0 |
| 58 | + if new_count > highest_count: |
| 59 | + highest_count = new_count |
| 60 | + bottom_range = highest_count-cls.MAX_UNUSED_OLDER_TOKENS |
| 61 | + used_counts = [] |
| 62 | + if type != OpenPAYGOTokenShared.TOKEN_TYPE_ADD_TIME or value == OpenPAYGOTokenShared.COUNTER_SYNC_VALUE or value == OpenPAYGOTokenShared.PAYG_DISABLE_VALUE: |
| 63 | + # If it is not an Add-Time token, we mark all the past tokens as used in the range |
| 64 | + for count in range(bottom_range, highest_count+1): |
| 65 | + used_counts.append(count) |
| 66 | + else: |
| 67 | + # If it is an Add-Time token, we just mark the tokens actually used in the range |
| 68 | + for count in range(bottom_range, highest_count+1): |
| 69 | + if count == new_count or count in past_used_counts: |
| 70 | + used_counts.append(count) |
| 71 | + return used_counts |
| 72 | + |
| 73 | + @classmethod |
| 74 | + def _decode_base(cls, starting_code_base, token_base): |
| 75 | + decoded_value = token_base - starting_code_base |
| 76 | + if decoded_value < 0: |
| 77 | + return decoded_value + 1000 |
| 78 | + else: |
| 79 | + return decoded_value |
| 80 | + |
| 81 | + @classmethod |
| 82 | + def get_activation_value_count_from_extended_token(cls, token, starting_code, key, last_count, |
| 83 | + restricted_digit_set=False): |
| 84 | + if restricted_digit_set: |
| 85 | + token = OpenPAYGOTokenSharedExtended.convert_from_4_digit_token(token) |
| 86 | + token_base = OpenPAYGOTokenSharedExtended.get_token_base(token) # We get the base of the token |
| 87 | + current_code = OpenPAYGOTokenSharedExtended.put_base_in_token(starting_code, token_base) # We put it into the starting code |
| 88 | + starting_code_base = OpenPAYGOTokenSharedExtended.get_token_base(starting_code) # We get the base of the starting code |
| 89 | + value = cls._decode_base_extended(starting_code_base, token_base) # If there is a match we get the value from the token |
| 90 | + for count in range(0, 30): |
| 91 | + masked_token = OpenPAYGOTokenSharedExtended.put_base_in_token(current_code, token_base) |
| 92 | + if masked_token == token and count > last_count: |
| 93 | + clean_count = count-1 |
| 94 | + return value, clean_count |
| 95 | + current_code = OpenPAYGOTokenSharedExtended.generate_next_token(current_code, key) # If not we go to the next token |
| 96 | + return None, None, None |
| 97 | + |
| 98 | + @classmethod |
| 99 | + def _decode_base_extended(cls, starting_code_base, token_base): |
| 100 | + decoded_value = token_base - starting_code_base |
| 101 | + if decoded_value < 0: |
| 102 | + return decoded_value + 1000000 |
| 103 | + else: |
| 104 | + return decoded_value |
0 commit comments