File: magic-gen.py

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (63 lines) | stat: -rwxr-xr-x 1,626 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/env python3
import sys


# This program parses criu magic.h file and produces
# magic.py with all *_MAGIC constants except RAW and V1.
def main(argv):
    if len(argv) != 3:
        print("Usage: magic-gen.py path/to/image.h path/to/magic.py")
        exit(1)

    magic_c_header = argv[1]
    magic_py = argv[2]

    out = open(magic_py, 'w+')

    # all_magic is used to parse constructions like:
    # #define PAGEMAP_MAGIC          0x56084025
    # #define SHMEM_PAGEMAP_MAGIC    PAGEMAP_MAGIC
    all_magic = {}
    # and magic is used to store only unique magic.
    magic = {}

    f = open(magic_c_header, 'r')
    for line in f:
        split = line.split()

        if len(split) < 3:
            continue

        if not '#define' in split[0]:
            continue

        key = split[1]
        value = split[2]

        if value in all_magic:
            value = all_magic[value]
        else:
            magic[key] = value

        all_magic[key] = value

    out.write('#Autogenerated. Do not edit!\n')
    out.write('by_name = {}\n')
    out.write('by_val = {}\n')
    for k, v in list(magic.items()):
        # We don't need RAW or V1 magic, because
        # they can't be used to identify images.
        if v == '0x0' or v == '1' or k == '0x0' or v == '1':
            continue
        if k.endswith("_MAGIC"):
            # Just cutting _MAGIC suffix
            k = k[:-6]
        v = int(v, 16)
        out.write("by_name['" + k + "'] = " + str(v) + "\n")
        out.write("by_val[" + str(v) + "] = '" + k + "'\n")
    f.close()
    out.close()


if __name__ == "__main__":
    main(sys.argv)