|
| 1 | +#!/usr/bin/env python3 |
| 2 | +############################################################################ |
| 3 | +# tools/coredump.py |
| 4 | +# |
| 5 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 6 | +# contributor license agreements. See the NOTICE file distributed with |
| 7 | +# this work for additional information regarding copyright ownership. The |
| 8 | +# ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 9 | +# "License"); you may not use this file except in compliance with the |
| 10 | +# License. You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | +# License for the specific language governing permissions and limitations |
| 18 | +# under the License. |
| 19 | +# |
| 20 | +############################################################################ |
| 21 | + |
| 22 | +import argparse |
| 23 | +import binascii |
| 24 | +import os |
| 25 | +import struct |
| 26 | +import sys |
| 27 | + |
| 28 | +import lzf |
| 29 | + |
| 30 | + |
| 31 | +def decompress(lzffile, outfile): |
| 32 | + chunk_number = 1 |
| 33 | + |
| 34 | + while True: |
| 35 | + prefix = lzffile.read(2) |
| 36 | + |
| 37 | + if len(prefix) == 0: |
| 38 | + break |
| 39 | + elif prefix != b"ZV": |
| 40 | + break |
| 41 | + |
| 42 | + typ = lzffile.read(1) |
| 43 | + clen = struct.unpack(">H", lzffile.read(2))[0] |
| 44 | + |
| 45 | + if typ == b"\x00": |
| 46 | + chunk = lzffile.read(clen) |
| 47 | + elif typ == b"\x01": |
| 48 | + uncompressed_len = struct.unpack(">H", lzffile.read(2))[0] |
| 49 | + cdata = lzffile.read(clen) |
| 50 | + chunk = lzf.decompress(cdata, uncompressed_len) |
| 51 | + else: |
| 52 | + return |
| 53 | + |
| 54 | + outfile.write(chunk) |
| 55 | + chunk_number += 1 |
| 56 | + |
| 57 | + |
| 58 | +def unhexlify(infile, outfile): |
| 59 | + for line in infile.readlines(): |
| 60 | + line = line.strip() |
| 61 | + if line == "": |
| 62 | + break |
| 63 | + index = line.rfind(" ") |
| 64 | + if index > 0: |
| 65 | + line = line[index + 1 :] |
| 66 | + |
| 67 | + outfile.write(binascii.unhexlify(line)) |
| 68 | + |
| 69 | + |
| 70 | +def parse_args(): |
| 71 | + global args |
| 72 | + parser = argparse.ArgumentParser( |
| 73 | + description=__doc__, |
| 74 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 75 | + allow_abbrev=False, |
| 76 | + ) |
| 77 | + parser.add_argument("input") |
| 78 | + parser.add_argument("-o", "--output", help="Output file in hex.") |
| 79 | + args = parser.parse_args() |
| 80 | + |
| 81 | + |
| 82 | +def main(): |
| 83 | + parse_args() |
| 84 | + if not os.path.isfile(args.input): |
| 85 | + sys.exit(1) |
| 86 | + |
| 87 | + tmp = os.path.splitext(args.input)[0] + ".tmp" |
| 88 | + |
| 89 | + if args.output is None: |
| 90 | + args.output = os.path.splitext(args.input)[0] + ".core" |
| 91 | + |
| 92 | + infile = open(args.input, "r") |
| 93 | + tmpfile = open(tmp, "wb+") |
| 94 | + |
| 95 | + unhexlify(infile, tmpfile) |
| 96 | + |
| 97 | + infile.close() |
| 98 | + |
| 99 | + tmpfile.seek(0, 0) |
| 100 | + |
| 101 | + lzfhdr = tmpfile.read(2) |
| 102 | + |
| 103 | + if lzfhdr == b"ZV": |
| 104 | + outfile = open(args.output, "wb") |
| 105 | + tmpfile.seek(0, 0) |
| 106 | + decompress(tmpfile, outfile) |
| 107 | + tmpfile.close() |
| 108 | + outfile.close() |
| 109 | + os.unlink(tmp) |
| 110 | + else: |
| 111 | + tmpfile.rename(args.output) |
| 112 | + tmpfile.close() |
| 113 | + |
| 114 | + print("Core file conversion completed: " + args.output) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + main() |
0 commit comments