xref: /OK3568_Linux_fs/buildroot/utils/checkpackagelib/lib_hash.py (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1# See utils/checkpackagelib/readme.txt before editing this file.
2# The validity of the hashes itself is checked when building, so below check
3# functions don't need to check for things already checked by running
4# "make package-dirclean package-source".
5
6import re
7
8from checkpackagelib.base import _CheckFunction
9from checkpackagelib.lib import ConsecutiveEmptyLines  # noqa: F401
10from checkpackagelib.lib import EmptyLastLine          # noqa: F401
11from checkpackagelib.lib import NewlineAtEof           # noqa: F401
12from checkpackagelib.lib import TrailingSpace          # noqa: F401
13
14
15def _empty_line_or_comment(text):
16    return text.strip() == "" or text.startswith("#")
17
18
19class HashNumberOfFields(_CheckFunction):
20    def check_line(self, lineno, text):
21        if _empty_line_or_comment(text):
22            return
23
24        fields = text.split()
25        if len(fields) != 3:
26            return ["{}:{}: expected three fields ({}#adding-packages-hash)"
27                    .format(self.filename, lineno, self.url_to_manual),
28                    text]
29
30
31class HashType(_CheckFunction):
32    len_of_hash = {"md5": 32, "sha1": 40, "sha224": 56, "sha256": 64,
33                   "sha384": 96, "sha512": 128}
34
35    def check_line(self, lineno, text):
36        if _empty_line_or_comment(text):
37            return
38
39        fields = text.split()
40        if len(fields) < 2:
41            return
42
43        htype, hexa = fields[:2]
44        if htype == "none":
45            return
46        if htype not in self.len_of_hash.keys():
47            return ["{}:{}: unexpected type of hash ({}#adding-packages-hash)"
48                    .format(self.filename, lineno, self.url_to_manual),
49                    text]
50        if not re.match("^[0-9A-Fa-f]{%s}$" % self.len_of_hash[htype], hexa):
51            return ["{}:{}: hash size does not match type "
52                    "({}#adding-packages-hash)"
53                    .format(self.filename, lineno, self.url_to_manual),
54                    text,
55                    "expected {} hex digits".format(self.len_of_hash[htype])]
56