Skip to content

Commit

Permalink
cleanup: Adds 'Default' trait to many structs (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgmcdona authored Dec 5, 2024
1 parent 945545f commit f9ce1c9
Show file tree
Hide file tree
Showing 20 changed files with 90 additions and 764 deletions.
65 changes: 10 additions & 55 deletions src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::{
util::{extract_string, padding_size},
};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct CatalogChunk {
pub chunk_tag: u32,
pub chunk_sub_tag: u32,
Expand All @@ -37,7 +37,7 @@ pub struct CatalogChunk {
pub catalog_subchunks: Vec<CatalogSubchunk>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct ProcessInfoEntry {
pub index: u16,
pub unknown: u16, // flags?
Expand All @@ -59,7 +59,7 @@ pub struct ProcessInfoEntry {
}

// Part of ProcessInfoEntry
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct ProcessUUIDEntry {
pub size: u32,
pub unknown: u32,
Expand All @@ -69,15 +69,15 @@ pub struct ProcessUUIDEntry {
}

// Part of ProcessInfoEntry
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct ProcessInfoSubsystem {
pub identifer: u16,
pub subsystem_offset: u16, // Represents the offset to the subsystem from the start of the subsystem entries
pub category_offset: u16, // Represents the offset to the subsystem category from the start of the subsystem entries
}

// Part of CatalogChunk, possible 64-bit alignment padding at end
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct CatalogSubchunk {
pub start: u64,
pub end: u64,
Expand All @@ -89,7 +89,7 @@ pub struct CatalogSubchunk {
pub string_offsets: Vec<u16>, // string_offsets size = number_string_offsets * u16
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct SubsystemInfo {
pub subsystem: String,
pub category: String,
Expand All @@ -98,22 +98,7 @@ pub struct SubsystemInfo {
impl CatalogChunk {
/// Parse log Catalog data. The log Catalog contains metadata related to log entries such as Process info, Subsystem info, and the compressed log entries
pub fn parse_catalog(data: &[u8]) -> nom::IResult<&[u8], CatalogChunk> {
let mut catalog_chunk = CatalogChunk {
chunk_tag: 0,
chunk_sub_tag: 0,
chunk_data_size: 0,
catalog_subsystem_strings_offset: 0,
catalog_process_info_entries_offset: 0,
number_process_information_entries: 0,
catalog_offset_sub_chunks: 0,
number_sub_chunks: 0,
unknown: Vec::new(),
earliest_firehose_timestamp: 0,
catalog_uuids: Vec::new(),
catalog_subsystem_strings: Vec::new(),
catalog_process_info_entries: Vec::new(),
catalog_subchunks: Vec::new(),
};
let mut catalog_chunk = CatalogChunk::default();

// Parse initial part Catalog chunk based on known sizes
let (input, chunk_tag) = take(size_of::<u32>())(data)?;
Expand Down Expand Up @@ -217,25 +202,7 @@ impl CatalogChunk {
data: &'a [u8],
uuids: &[String],
) -> nom::IResult<&'a [u8], ProcessInfoEntry> {
let mut catalog_process_entry = ProcessInfoEntry {
index: 0,
unknown: 0,
catalog_main_uuid_index: 0,
catalog_dsc_uuid_index: 0,
first_number_proc_id: 0,
second_number_proc_id: 0,
pid: 0,
effective_user_id: 0,
unknown2: 0,
number_uuids_entries: 0,
unknown3: 0,
uuid_info_entries: Vec::new(),
number_subsystems: 0,
unknown4: 0,
subsystem_entries: Vec::new(),
main_uuid: String::new(),
dsc_uuid: String::new(),
};
let mut catalog_process_entry = ProcessInfoEntry::default();

// Get all static sized data
let (input, index) = take(size_of::<u16>())(data)?;
Expand Down Expand Up @@ -396,16 +363,7 @@ impl CatalogChunk {

// Parse the Catalog Subchunk metadata. This metadata is related to the compressed (typically) Chunkset data
fn parse_catalog_subchunk(data: &[u8]) -> nom::IResult<&[u8], CatalogSubchunk> {
let mut catalog_subchunk = CatalogSubchunk {
start: 0,
end: 0,
uncompressed_size: 0,
compression_algorithm: 0,
number_index: 0,
indexes: Vec::new(),
number_string_offsets: 0,
string_offsets: Vec::new(),
};
let mut catalog_subchunk = CatalogSubchunk::default();

// Get static size subchunk data
let (input, start) = take(size_of::<u64>())(data)?;
Expand Down Expand Up @@ -478,10 +436,7 @@ impl CatalogChunk {
second_proc_id: &u32,
catalog: &'a CatalogChunk,
) -> nom::IResult<&'a [u8], SubsystemInfo> {
let mut subsystem_info = SubsystemInfo {
subsystem: String::new(),
category: String::new(),
};
let mut subsystem_info = SubsystemInfo::default();

// Go through catalog entries until first and second proc id match the log entry
for process_info in &catalog.catalog_process_info_entries {
Expand Down
25 changes: 2 additions & 23 deletions src/chunks/firehose/activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use nom::{
};
use std::mem::size_of;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct FirehoseActivity {
pub unknown_activity_id: u32,
pub unknown_sentinal: u32, // always 0x80000000?
Expand All @@ -40,28 +40,7 @@ impl FirehoseActivity {
firehose_flags: &u16,
firehose_log_type: &u8,
) -> nom::IResult<&'a [u8], FirehoseActivity> {
let mut activity = FirehoseActivity {
unknown_activity_id: 0,
unknown_sentinal: 0,
pid: 0,
unknown_activity_id_2: 0,
unknown_sentinal_2: 0,
unknown_activity_id_3: 0,
unknown_sentinal_3: 0,
unknown_message_string_ref: 0,
unknown_pc_id: 0,
firehose_formatters: FirehoseFormatters {
main_exe: false,
shared_cache: false,
has_large_offset: 0,
large_shared_cache: 0,
absolute: false,
uuid_relative: String::new(),
main_plugin: false,
pc_style: false,
main_exe_alt_index: 0,
},
};
let mut activity = FirehoseActivity::default();
let mut input = data;

// Useraction activity type does not have first Activity ID or sentinel
Expand Down
139 changes: 11 additions & 128 deletions src/chunks/firehose/firehose_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// See the License for the specific language governing permissions and limitations under the License.

use crate::chunks::firehose::activity::FirehoseActivity;
use crate::chunks::firehose::flags::FirehoseFormatters;
use crate::chunks::firehose::loss::FirehoseLoss;
use crate::chunks::firehose::nonactivity::FirehoseNonActivity;
use crate::chunks::firehose::signpost::FirehoseSignpost;
Expand All @@ -22,7 +21,7 @@ use nom::{
use serde::Serialize;
use std::mem::size_of;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct FirehosePreamble {
pub chunk_tag: u32,
pub chunk_sub_tag: u32,
Expand All @@ -40,7 +39,7 @@ pub struct FirehosePreamble {
pub public_data: Vec<Firehose>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct Firehose {
pub unknown_log_activity_type: u8, // 0x2 is Activity, 0x4 is non-activity, 0x6 is signpost, 0x3 trace
pub unknown_log_type: u8, // Unkonwn but possibly log type (Info/Activity, Debug, Error, Fault, Signpost, System, Default)
Expand All @@ -60,7 +59,7 @@ pub struct Firehose {
pub message: FirehoseItemData, // Log values extracted
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct FirehoseItemType {
pub item_type: u8,
item_size: u8,
Expand All @@ -69,13 +68,13 @@ pub struct FirehoseItemType {
pub message_strings: String,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct FirehoseItemData {
pub item_info: Vec<FirehoseItemInfo>,
pub backtrace_strings: Vec<String>,
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, Default)]
pub struct FirehoseItemInfo {
pub message_strings: String, // The message entry.
pub item_type: u8, // Type of item: strings, numbers, objects, precision
Expand All @@ -87,22 +86,7 @@ impl FirehosePreamble {
pub fn parse_firehose_preamble(
firehose_input_data: &[u8],
) -> nom::IResult<&[u8], FirehosePreamble> {
let mut firehose_data = FirehosePreamble {
chunk_tag: 0,
chunk_sub_tag: 0,
chunk_data_size: 0,
first_number_proc_id: 0,
second_number_proc_id: 0,
collapsed: 0,
unknown: Vec::new(),
public_data_size: 0,
private_data_virtual_offset: 0,
unkonwn2: 0,
unknown3: 0,
base_continous_time: 0,
public_data: Vec::new(),
ttl: 0,
};
let mut firehose_data = FirehosePreamble::default();

let (input, chunk_tag) = take(size_of::<u32>())(firehose_input_data)?;
let (input, chunk_sub_tag) = take(size_of::<u32>())(input)?;
Expand Down Expand Up @@ -261,10 +245,7 @@ impl FirehosePreamble {
let mut items_data: Vec<FirehoseItemType> = Vec::new();

let mut firehose_input = data;
let mut firehose_item_data = FirehoseItemData {
item_info: Vec::new(),
backtrace_strings: Vec::new(),
};
let mut firehose_item_data = FirehoseItemData::default();

// Firehose number item values
let number_item_type: Vec<u8> = vec![0x0, 0x2];
Expand Down Expand Up @@ -450,101 +431,7 @@ impl FirehosePreamble {

// Parse all the different types of Firehose data (activity, non-activity, loss, trace, signpost)
fn parse_firehose(data: &[u8]) -> nom::IResult<&[u8], Firehose> {
let mut firehose_results = Firehose {
unknown_log_activity_type: 0,
unknown_log_type: 0,
flags: 0,
format_string_location: 0,
thread_id: 0,
continous_time_delta: 0,
continous_time_delta_upper: 0,
data_size: 0,
firehose_activity: FirehoseActivity {
unknown_activity_id: 0,
unknown_sentinal: 0,
pid: 0,
unknown_activity_id_2: 0,
unknown_sentinal_2: 0,
unknown_activity_id_3: 0,
unknown_sentinal_3: 0,
unknown_message_string_ref: 0,
unknown_pc_id: 0,
firehose_formatters: FirehoseFormatters {
main_exe: false,
shared_cache: false,
has_large_offset: 0,
large_shared_cache: 0,
absolute: false,
uuid_relative: String::new(),
main_plugin: false,
pc_style: false,
main_exe_alt_index: 0,
},
},
firehose_non_activity: FirehoseNonActivity {
unknown_activity_id: 0,
unknown_sentinal: 0,
private_strings_offset: 0,
private_strings_size: 0,
unknown_message_string_ref: 0,
subsystem_value: 0,
ttl_value: 0,
data_ref_value: 0,
unknown_pc_id: 0,
firehose_formatters: FirehoseFormatters {
main_exe: false,
shared_cache: false,
has_large_offset: 0,
large_shared_cache: 0,
absolute: false,
uuid_relative: String::new(),
main_plugin: false,
pc_style: false,
main_exe_alt_index: 0,
},
},
firehose_loss: FirehoseLoss {
start_time: 0,
end_time: 0,
count: 0,
},
firehose_trace: FirehoseTrace {
unknown_pc_id: 0,
message_data: FirehoseItemData {
item_info: Vec::new(),
backtrace_strings: Vec::new(),
},
},
firehose_signpost: FirehoseSignpost {
unknown_pc_id: 0,
unknown_activity_id: 0,
unknown_sentinel: 0,
subsystem: 0,
signpost_id: 0,
signpost_name: 0,
private_strings_offset: 0,
private_strings_size: 0,
ttl_value: 0,
firehose_formatters: FirehoseFormatters {
main_exe: false,
shared_cache: false,
has_large_offset: 0,
large_shared_cache: 0,
absolute: false,
uuid_relative: String::new(),
main_plugin: false,
pc_style: false,
main_exe_alt_index: 0,
},
data_ref_value: 0,
},
unknown_item: 0,
number_items: 0,
message: FirehoseItemData {
item_info: Vec::new(),
backtrace_strings: Vec::new(),
},
};
let mut firehose_results = Firehose::default();

let (input, unknown_log_activity_type) = take(size_of::<u8>())(data)?;
let (input, unknown_log_type) = take(size_of::<u8>())(input)?;
Expand Down Expand Up @@ -728,13 +615,9 @@ impl FirehosePreamble {
let (_, item_type) = le_u8(item_type)?;
let (_, item_size) = le_u8(item_size)?;

let mut item = FirehoseItemType {
item_type,
item_size,
offset: 0,
message_string_size: 0,
message_strings: String::new(),
};
let mut item = FirehoseItemType::default();
item.item_type = item_type;
item.item_size = item_size;

// Firehose string item values
let string_item: Vec<u8> = vec![
Expand Down
Loading

0 comments on commit f9ce1c9

Please sign in to comment.