{% extends "analysis_plugins/general_information.html" %}


{% block styles %}
<style>
    .qemu-pre {
        padding: 5px;
        width: 100%;
        white-space: pre-wrap;
        overflow-y: scroll;
        overflow-x: hidden;
        display: block;
        font-family: monospace;
        margin: 0 0;
        color: #f8f8f8;
        background-color: #333;
        max-height: 500px;
    }
    .qemu-td {
        width: 100%;
        max-width: 0;
    }
</style>
{% endblock %}


{% block analysis_result_details %}

    {% if 'files' in firmware.processed_analysis[selected_analysis] %}

        <th class="success" colspan="2">
            Results for Included Files
        </th>

        {% for uid, result in firmware.processed_analysis[selected_analysis]['files'].items() %}

            <tr data-toggle="collapse" data-target="#collapse-{{ uid }}">
                <td class="clickable" colspan=2>
                    <i class="fas fa-angle-down"></i> {{ result['path'] }}
                    {% if result['executable'] %}
                        <i class="fas fa-check text-success"></i>
                    {% else %}
                        <i class="fas fa-times text-danger"></i>
                    {% endif %}
                </td>
            </tr>

            <tr>
                <td colspan="2" class="qemu-td p-0">
                    <div class="collapse" id="collapse-{{ uid }}">

                        <table class="table table-bordered m-0" style="width: 100%">
                            <tr>
                                <td width="175px">Executable in QEMU:</td>
                                <td>{{ result['executable'] }}</td>
                            </tr>

                            {% if result['results'] %}
                            <tr>
                                <td>Individual Results:</td>
                                <td class="p-0">
                                    <table class="table table-bordered m-0" style="width: 100%">
                                    {% for arch in result['results'] %}
                                        <tr>
                                            {% if "strace" in result['results'][arch] %}
                                                <td width="1px" rowspan="{{ (result['results'][arch] | length - 1) * 3  + 1 }}">
                                            {% else %}
                                                <td width="1px" rowspan="{{ (result['results'][arch] | length) * 3 }}">
                                            {% endif %}
                                                {{ arch }}
                                            </td>
                                            {% for option in result['results'][arch] %}
                                                {% if option != 'strace' %}
                                                    {% set option_result = result['results'][arch][option] %}
                                                    <td width="1px" rowspan="3">{{ option }}</td>
                                                    <td>stdout</td>
                                                    <td class="qemu-td" style="padding: 5px;">
                                                        {% if option_result['stdout'] %}
                                                        <pre class="qemu-pre">{{ '$ .' + result['path'] + ' ' + option + '\n' + option_result['stdout'] }}</pre>
                                                        {% else %}Empty{% endif %}
                                                    </td>
                                                    </tr><tr>
                                                        <td>stderr</td>
                                                        <td>
                                                            {% if option_result['stderr'] %}
                                                            <pre class="qemu-pre">{{ '$ .' + result['path'] + ' ' + option + '\n' + option_result['stderr'] }}</pre>
                                                            {% else %}Empty{% endif %}
                                                        </td>
                                                    </tr><tr>
                                                        <td>return code</td>
                                                        <td>{{ option_result['return_code'] }}</td>
                                                    </tr><tr>
                                                {% endif %}
                                            {% endfor %}
                                            {% if 'strace' in result['results'][arch] %}
                                                    <td width="1px">strace</td>
                                                    <td colspan="2">
                                                        {% if result['results'][arch]['strace'] %}
                                                            <pre class="qemu-pre">{{ result['results'][arch]['strace'] | decompress }}</pre>
                                                        {% else %}Empty{% endif %}
                                                    </td>
                                                </tr>
                                            {% else %}
                                                </tr>
                                            {% endif %}
                                    {% endfor %}
                                    </table>
                                </td>
                            </tr>
                            {% endif %}
                        </table>

                    </div>
                </td>
            </tr>

        {% endfor %}
    {% endif %}

{% endblock %}


{% block ajax %}
    {% set analysis = firmware.processed_analysis[selected_analysis] %}

    {% if 'parent_flag' in analysis and analysis.parent_flag == True %}
        <div id="file_results"></div>
        <script>
            $(document).ready(function() {
                $('#file_results').load('/plugins/qemu_exec/ajax/{{ firmware.uid }}');
            });
        </script>
    {% endif %}

{% endblock %}