|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# Copyright 2017 Zachary Marv (马子昂) |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# Utility for repackage detection use |
| 18 | + |
| 19 | +from _settings import * |
| 20 | + |
| 21 | +class Util(object): |
| 22 | + @staticmethod |
| 23 | + def dict2str(kvd): |
| 24 | + """ |
| 25 | + Convert Key-Value Dict to String |
| 26 | + :param kvd: Key-Value Dict |
| 27 | + :return: feature_str String |
| 28 | + """ |
| 29 | + feature_str = "" |
| 30 | + assert type(kvd) == dict, 'kvd is a dictionary' |
| 31 | + items = kvd.items() |
| 32 | + items.sort() |
| 33 | + for item in items: |
| 34 | + if type(item[0]) is not int: |
| 35 | + logger.error("Key not int") |
| 36 | + if item[0] >= 256 * 256: |
| 37 | + logger.error("Key too Large") |
| 38 | + s1 = chr(item[0] / 256) |
| 39 | + s2 = chr(item[0] % 256) |
| 40 | + if type(item[1]) is not int: |
| 41 | + logger.error("Value not int.") |
| 42 | + if item[1] >= 256 * 256: |
| 43 | + logger.warning("Value too Large.") |
| 44 | + s3 = chr(255) |
| 45 | + s4 = chr(255) |
| 46 | + else: |
| 47 | + s3 = chr(item[1] / 256) |
| 48 | + s4 = chr(item[1] % 256) |
| 49 | + feature_str += s1 + s2 + s3 + s4 |
| 50 | + return feature_str |
| 51 | + |
| 52 | + @staticmethod |
| 53 | + def str2dict(feature_str): |
| 54 | + """ |
| 55 | + Convert String to Key-Value Dict |
| 56 | + :param feature_str: |
| 57 | + :return: kvd |
| 58 | + """ |
| 59 | + kvd = dict() |
| 60 | + feature_mod = len(feature_str) % 4 |
| 61 | + if feature_mod != 0: |
| 62 | + logger.error("feature_str length is not 4X") |
| 63 | + feature_length = len(feature_str) |
| 64 | + for i in range(0, feature_length, 4): |
| 65 | + i1 = ord(feature_str[i]) |
| 66 | + i2 = ord(feature_str[i + 1]) |
| 67 | + i3 = ord(feature_str[i + 2]) |
| 68 | + i4 = ord(feature_str[i + 3]) |
| 69 | + key_int = i1 * 256 + i2 |
| 70 | + value_int = i3 * 256 + i4 |
| 71 | + kvd[key_int] = value_int |
| 72 | + return kvd |
0 commit comments