Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove string.c from the package. #655

Merged
merged 9 commits into from
May 11, 2020
Merged
115 changes: 0 additions & 115 deletions cve_bin_tool/string.c

This file was deleted.

34 changes: 17 additions & 17 deletions cve_bin_tool/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,31 @@
and MacOS.
"""

import sys
import string
import sys

try:
import cve_bin_tool.pstring as pstring
except ImportError:
pstring = None

class Strings:
# printable characters
PRINTABLE = set(range(32, 128))
# add tab to the printable character
PRINTABLE.add(9)

class Strings(object):
def __init__(self, filename=""):
self.filename = filename
self.output = ""
self.printable = set(string.printable)
self.output = [""]

def parse(self):
# Use c extention if available
if pstring is not None:
return pstring.string(self.filename)
else:
return self.parse_3()

def parse_3(self):
with open(self.filename, "r", encoding="ascii", errors="surrogateescape") as f:
self.output = "".join(filter(lambda x: x in self.printable, f.read()))
with open(self.filename, "rb") as f:
pdxjohnny marked this conversation as resolved.
Show resolved Hide resolved
tmp = []
for line in f.readlines():
for char in line:
# remove all unprintable characters
if char in Strings.PRINTABLE:
tmp.append(chr(char))
elif tmp:
self.output.append("".join(tmp))
tmp = []
return self.output


Expand Down
11 changes: 3 additions & 8 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import sys
import ast
import os
from io import open
from setuptools import find_packages, setup, Extension

from setuptools import find_packages, setup

with open("README.md", "r", encoding="utf-8") as f:
readme = f.read()
Expand Down Expand Up @@ -55,14 +55,9 @@
"checkers",
)
)
if filename[::-1].startswith("yp.") and not "__init__" in filename
if filename.endswith(".py") and "__init__" not in filename
],
},
)

if sys.version_info.major == 3:
setup_kwargs["ext_modules"] = [
Extension("cve_bin_tool.pstring", [os.path.join("cve_bin_tool", "string.c")])
]

setup(**setup_kwargs)
19 changes: 4 additions & 15 deletions test/test_strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
CVE-bin-tool Strings tests
"""
import os
import subprocess
import sys
import tempfile
import unittest
import subprocess
from time import perf_counter

from cve_bin_tool.strings import Strings

Expand All @@ -30,18 +28,9 @@ def _parse_test(self, filename):
"""Helper function to parse a binary file and check whether
the given string is in the parsed result"""
self.strings.filename = os.path.join(BINARIES_PATH, filename)
f = tempfile.TemporaryFile()
subprocess.call(["strings", self.strings.filename], stdout=f)
binutils_strings = f.readlines()
t1 = perf_counter()
ours = self.strings.parse().split("\n")
t2 = perf_counter()
print(f"pstring: {t2 - t1}")
t1 = perf_counter()
ours = self.strings.parse_3().split("\n")
t2 = perf_counter()
print(f"string: {t2 - t1}")
for theirs in binutils_strings:
binutils_strings = subprocess.check_output(["strings", self.strings.filename])
ours = self.strings.parse()
for theirs in binutils_strings.splitlines():
self.assertIn(theirs.decode("utf-8"), ours)

def test_curl_7_34_0(self):
Expand Down