Skip to content

Commit b040980

Browse files
committed
Util for repackage
1 parent 905bd1e commit b040980

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

LibRadar/util.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

LibRadar/util_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import unittest
2+
3+
from util import Util
4+
5+
class TestUtil(unittest.TestCase):
6+
7+
def test_dict2str(self):
8+
self.d = dict()
9+
self.d[14] = 21
10+
self.d[35] = 6
11+
self.d[2] = 42
12+
self.feature = Util.dict2str(self.d)
13+
self.assertIsInstance(self.feature, str)
14+
15+
def test_str2dict(self):
16+
d = dict()
17+
d[14] = 21257
18+
d[35] = 68
19+
d[2] = 42
20+
feature = Util.dict2str(d)
21+
kvd = Util.str2dict(feature)
22+
self.assertIsInstance(kvd, dict)
23+
self.assertEqual(d, kvd)
24+
25+
def test_key_large(self):
26+
d = dict()
27+
d[14] = 21257
28+
d[35234234325325234] = 68
29+
d[2] = 42
30+
feature = Util.dict2str(d)
31+
kvd = Util.str2dict(feature)
32+
self.assertIsInstance(kvd, dict)
33+
self.assertEqual(d, kvd)
34+
35+
def test_value_large(self):
36+
d = dict()
37+
d[14] = 21257323423235235
38+
d[35] = 68
39+
d[2] = 42
40+
feature = Util.dict2str(d)
41+
kvd = Util.str2dict(feature)
42+
self.assertIsInstance(kvd, dict)
43+
self.assertEqual(d, kvd)
44+
45+
if __name__ == '__main__':
46+
unittest.main()

0 commit comments

Comments
 (0)