Skip to content
API Documentation
BastionGuard™
report_exporter.cpp
Go to the documentation of this file.
1#include "report_exporter.hpp"
2#include "i18n.hpp"
3
4#include <fstream>
5#include <sstream>
6
7namespace {
8
9 std::string escape_json(const std::string& s) {
10 std::ostringstream oss;
11 for (char c : s) {
12 switch (c) {
13 case '"': oss << "\\\""; break;
14 case '\\': oss << "\\\\"; break;
15 case '\b': oss << "\\b"; break;
16 case '\f': oss << "\\f"; break;
17 case '\n': oss << "\\n"; break;
18 case '\r': oss << "\\r"; break;
19 case '\t': oss << "\\t"; break;
20 default:
21 if (static_cast<unsigned char>(c) < 0x20) {
22 oss << "\\u";
23 const char* hex = "0123456789abcdef";
24 oss << '0' << '0'
25 << hex[(static_cast<unsigned char>(c) >> 4) & 0xF]
26 << hex[static_cast<unsigned char>(c) & 0xF];
27 } else {
28 oss << c;
29 }
30 }
31 }
32 return oss.str();
33 }
34
35 const char* kind_to_string(ResultKind kind) {
36 switch (kind) {
38 return "Partition";
40 return "LostPartitionCandidate";
42 return "FilesystemCandidate";
44 return "FilesystemVolume";
46 return "NtfsVolume";
48 return "NtfsMetadata";
50 return "Info";
52 return "Warning";
54 return "Error";
55 default:
56 return tr("Unknown");
57 }
58 }
59
60} // namespace
61
63 const std::string& path,
64 std::string& error_out) {
65 std::ofstream out(path, std::ios::binary);
66 if (!out) {
67 error_out = trs("Unable to open destination file");
68 return false;
69 }
70
71 out << "{\n";
72 out << " \"input_path\": \"" << escape_json(report.input_path) << "\",\n";
73 out << " \"input_size\": " << report.input_size << ",\n";
74 out << " \"logs\": [\n";
75 for (size_t i = 0; i < report.logs.size(); ++i) {
76 out << " \"" << escape_json(report.logs[i]) << "\""
77 << (i + 1 < report.logs.size() ? "," : "") << "\n";
78 }
79 out << " ],\n";
80 out << " \"results\": [\n";
81 for (size_t i = 0; i < report.results.size(); ++i) {
82 const auto& r = report.results[i];
83 out << " {\n";
84 out << " \"kind\": \"" << kind_to_string(r.kind) << "\",\n";
85 out << " \"offset_bytes\": " << r.offset_bytes << ",\n";
86 out << " \"size_bytes\": " << r.size_bytes << ",\n";
87 out << " \"type\": \"" << escape_json(r.type) << "\",\n";
88 out << " \"source\": \"" << escape_json(r.source) << "\",\n";
89 out << " \"detail\": \"" << escape_json(r.detail) << "\",\n";
90 out << " \"confidence\": " << r.confidence << "\n";
91 out << " }" << (i + 1 < report.results.size() ? "," : "") << "\n";
92 }
93 out << " ]\n";
94 out << "}\n";
95 return true;
96 }
97
99 const std::string& path,
100 std::string& error_out) {
101 std::ofstream out(path);
102 if (!out) {
103 error_out = trs("Unable to open destination file");
104 return false;
105 }
106
107 out << "BastionGuard Recovery Data\n";
108 out << "Input path: " << report.input_path << "\n";
109 out << "Input size: " << report.input_size << " bytes\n\n";
110 out << "Logs\n----\n";
111 for (const auto& line : report.logs) {
112 out << line << '\n';
113 }
114
115 out << "\nResults\n-------\n";
116 for (const auto& r : report.results) {
117 out << '[' << kind_to_string(r.kind) << "] "
118 << r.type
119 << " | source=" << r.source
120 << " | offset=" << r.offset_bytes
121 << " | size=" << r.size_bytes
122 << " | confidence=" << r.confidence
123 << " | detail=" << r.detail
124 << '\n';
125 }
126
127 return true;
128 }
static bool save_json(const ScanReport &report, const std::string &path, std::string &error_out)
static bool save_text(const ScanReport &report, const std::string &path, std::string &error_out)
const char * tr(const char *msgid)
Definition i18n.cpp:18
std::string trs(const char *msgid)
Definition i18n.cpp:22
ResultKind
Definition models.hpp:7
@ Partition
Definition models.hpp:8
@ LostPartitionCandidate
Definition models.hpp:9
@ NtfsMetadata
Definition models.hpp:13
@ FilesystemCandidate
Definition models.hpp:10
@ FilesystemVolume
Definition models.hpp:11
const char * kind_to_string(ResultKind kind)
std::string escape_json(const std::string &s)
std::vector< ScanResult > results
Definition models.hpp:40
std::vector< std::string > logs
Definition models.hpp:41
std::string input_path
Definition models.hpp:38
uint64_t input_size
Definition models.hpp:39