๐Ÿ“ฆ apache / nuttx

๐Ÿ“„ coredump.py ยท 145 lines
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145#!/usr/bin/env python3
############################################################################
# tools/coredump.py
#
# SPDX-License-Identifier: Apache-2.0
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.  The
# ASF licenses this file to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance with the
# License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
# License for the specific language governing permissions and limitations
# under the License.
#
############################################################################

import argparse
import base64
import binascii
import os
import struct
import sys

import lzf


def decompress(lzffile, outfile):
    chunk_number = 1

    while True:
        prefix = lzffile.read(2)

        if len(prefix) == 0:
            break
        elif prefix != b"ZV":
            break

        typ = lzffile.read(1)
        clen = struct.unpack(">H", lzffile.read(2))[0]

        if typ == b"\x00":
            chunk = lzffile.read(clen)
        elif typ == b"\x01":
            uncompressed_len = struct.unpack(">H", lzffile.read(2))[0]
            cdata = lzffile.read(clen)
            chunk = lzf.decompress(cdata, uncompressed_len)
        else:
            return

        outfile.write(chunk)
        chunk_number += 1


def unhexlify(infile, outfile):
    for line in infile.readlines():
        line = line.strip()
        if line == "":
            break
        index = line.rfind(" ")
        if index > 0:
            line = line[index + 1 :]

        outfile.write(binascii.unhexlify(line))


def unbase64file(infile, outfile):
    input = ""
    for line in infile.readlines():
        line = line.strip()
        if line == "":
            break
        index = line.rfind(" ")
        if index > 0:
            line = line[index + 1 :]

        input += line
    outfile.write(base64.b64decode(input))


def parse_args():
    global args
    parser = argparse.ArgumentParser(
        description=__doc__,
        formatter_class=argparse.RawDescriptionHelpFormatter,
        allow_abbrev=False,
    )
    parser.add_argument("input")
    parser.add_argument("-o", "--output", help="Output file in hex.")
    parser.add_argument(
        "--base64",
        action="store_true",
        default=False,
        help="Set when input file is base64 encoded.",
    )
    args = parser.parse_args()


def main():
    parse_args()
    if not os.path.isfile(args.input):
        sys.exit(1)

    tmp = os.path.splitext(args.input)[0] + ".tmp"

    if args.output is None:
        args.output = os.path.splitext(args.input)[0] + ".core"

    infile = open(args.input, "r")
    tmpfile = open(tmp, "wb+")

    if args.base64:
        unbase64file(infile, tmpfile)
    else:
        unhexlify(infile, tmpfile)

    infile.close()

    tmpfile.seek(0, 0)

    lzfhdr = tmpfile.read(2)

    if lzfhdr == b"ZV":
        outfile = open(args.output, "wb")
        tmpfile.seek(0, 0)
        decompress(tmpfile, outfile)
        tmpfile.close()
        outfile.close()
        os.unlink(tmp)
    else:
        tmpfile.close()
        os.rename(tmp, args.output)

    print("Core file conversion completed: " + args.output)


if __name__ == "__main__":
    main()