Word Unperfect
public
Read
Owner: themaster
Branch: main
Commits: 0
Git CLI clone URL
git clone https://www.xt-emporium.com/git/word-unperfect.git
Fullscreen desktop URL
Code
Commits
History
Branches
Bug Reports
Discussions
Compare
Settings
word-unperfect
/
rev
/
wp_parser_classify.c
File editor
#include "wp_parser_classify.h" #include <string.h> static uint16_t wp_classify_u16(unsigned value) { return (uint16_t)(value & 0xffffU); } static uint16_t wp_classify_record_metric(const WpLayoutGlobals *wl, const WpRecord *rec) { if (rec == NULL) { return 1U; } if (rec->code == 0x20U && wl != NULL && wl->span_metric_delta_4bd8 > 0) { return (uint16_t)wl->span_metric_delta_4bd8; } if (rec->code >= 0x21U && rec->code < 0x80U && wl != NULL && wl->span_metric_delta_4bda > 0) { return (uint16_t)wl->span_metric_delta_4bda; } if (rec->code == 0xC0U) { return 1U; } return 1U; } static void wp_classify_decrement_cursor(WpLayoutGlobals *runtime, const WpRecord *rec) { uint16_t metric; if (runtime == NULL) { return; } metric = wp_classify_record_metric(runtime, rec); runtime->cursor = wp_classify_u16((uint16_t)runtime->cursor - metric); runtime->record_aux_4b34 -= 1; runtime->span_aux_word_53f6 = wp_classify_u16((uint16_t)runtime->span_aux_word_53f6 - 1U); } static void wp_classify_status_extend_update(WpLayoutGlobals *runtime) { uint16_t metric; if (runtime == NULL) { return; } metric = (uint16_t)(runtime->explicit_span_total != 0 ? runtime->explicit_span_total : wp_span_word_as_int(runtime->default_span_a) + wp_span_word_as_int(runtime->default_span_b)); if (metric == 0U) { metric = 1U; } if ((uint16_t)runtime->carry_width < metric) { runtime->carry_width = 0; } else { runtime->carry_width = (int)((uint16_t)runtime->carry_width - metric); } runtime->status_extend_4976 |= 0x04U; } static void wp_classify_count_record_status(const WpRecord *rec, WpParserClassifyStats *stats) { if (rec == NULL || stats == NULL) { return; } if (!rec->is_complete && !rec->trailer_present) { stats->incomplete_records++; } if (rec->trailer_present && !rec->trailer_matches) { stats->mismatched_trailers++; } } void wp_parser_classify_stats_clear(WpParserClassifyStats *stats) { if (stats != NULL) { memset(stats, 0, sizeof(*stats)); } } const char *wp_parser_classify_route_name(WpParserClassifyRoute route) { switch (route) { case WP_PARSER_CLASSIFY_ROUTE_SPACE_BACKTRACK: return "space backtrack"; case WP_PARSER_CLASSIFY_ROUTE_PRINTABLE_BACKTRACK: return "printable backtrack"; case WP_PARSER_CLASSIFY_ROUTE_C0_EXTENDED_REPLAY: return "C0 extended replay"; case WP_PARSER_CLASSIFY_ROUTE_LOW_CONTROL_RESTORE: return "low-control restore"; case WP_PARSER_CLASSIFY_ROUTE_SINGLE_BYTE_CONTROL: return "single-byte control"; case WP_PARSER_CLASSIFY_ROUTE_FIXED_PACKET: return "fixed packet"; case WP_PARSER_CLASSIFY_ROUTE_VARIABLE_PACKET: return "variable packet"; case WP_PARSER_CLASSIFY_ROUTE_DE_SCANNER_BYPASS: return "DE scanner bypass"; case WP_PARSER_CLASSIFY_ROUTE_RANGE_CHECK_FALLBACK: return "range-check fallback"; case WP_PARSER_CLASSIFY_ROUTE_NONE: default: return "none"; } } const char *wp_parser_classify_handler_name(WpParserClassifyHandler handler) { switch (handler) { case WP_PARSER_CLASSIFY_HANDLER_79FA_TEXT_SINGLE: return "0x79FA text/single handler"; case WP_PARSER_CLASSIFY_HANDLER_7B6F_FIXED: return "0x7B6F fixed handler"; case WP_PARSER_CLASSIFY_HANDLER_7B91_VARIABLE: return "0x7B91 variable handler"; case WP_PARSER_CLASSIFY_HANDLER_NONE: default: return "none"; } } bool wp_parser_classify_plan_record(const WpLayoutGlobals *runtime, const WpRecord *rec, WpParserClassifyStep *out_step) { WpParserClassifyStep step; if (out_step != NULL) { memset(out_step, 0, sizeof(*out_step)); } if (rec == NULL) { return false; } memset(&step, 0, sizeof(step)); step.code = rec->code; step.sub_code = rec->sub_code; step.record_type = rec->type; step.consumed_record_bytes = rec->length; if (rec->code == 0x20U) { step.route = WP_PARSER_CLASSIFY_ROUTE_SPACE_BACKTRACK; step.prepends_token = true; step.decrements_update_depth = true; step.decrements_processed_segments = true; step.clears_emit_bit1 = true; step.cursor_decrement_stage1 = true; } else if (rec->code >= 0x21U && rec->code < 0x80U) { step.route = WP_PARSER_CLASSIFY_ROUTE_PRINTABLE_BACKTRACK; step.prepends_token = true; step.cursor_decrement_stage1 = true; } else if (rec->code == 0xC0U) { step.route = WP_PARSER_CLASSIFY_ROUTE_C0_EXTENDED_REPLAY; step.prepends_token = true; step.c0_word_replay = true; step.cursor_decrement_stage1 = true; step.cursor_decrement_stage2 = true; if (rec->data != NULL && rec->data_length >= 2U) { step.c0_payload_word = (uint16_t)rec->data[0] | ((uint16_t)rec->data[1] << 8); } } else { step.parse_depth_pulse = true; step.runtime_bit0_set = true; step.prepends_token = true; step.emit_flags_clear_0x14 = true; step.record_wide_temp_snapshot = true; /* check_layout_state_update_gate() sets carry when runtime bit 0x80 is * active, or when state bit 1 and update depth are active. Without the * gate, the decompiled path decrements update_depth and may invalidate * cursor_window on zero/borrow transitions. */ if (runtime == NULL || (((runtime->runtime_flags & 0x80U) == 0U) && !(((runtime->state_flags & 0x02U) != 0U) && runtime->update_depth > 0))) { step.state_update_gate_blocked_decrement = true; step.decrements_update_depth = true; if (runtime == NULL || runtime->update_depth <= 1) { step.cursor_window_reset = true; } } if (rec->code < 0x20U) { step.route = WP_PARSER_CLASSIFY_ROUTE_LOW_CONTROL_RESTORE; step.low_control_restore_path = true; step.runtime_bit2_set = true; step.state_flags_cleared = true; step.status_extend_update = true; step.sub_metric_update = true; step.render_dirty_set = true; } else if (rec->code < 0xC0U) { step.route = WP_PARSER_CLASSIFY_ROUTE_SINGLE_BYTE_CONTROL; step.handler = WP_PARSER_CLASSIFY_HANDLER_79FA_TEXT_SINGLE; step.variant_balance_check = true; /* The 1000:2447 path falls through to the low-control restore block * for the single-byte range after the stride scan comparison. */ step.low_control_restore_path = true; step.runtime_bit2_set = true; step.state_flags_cleared = true; step.status_extend_update = true; step.sub_metric_update = true; step.render_dirty_set = true; } else if (rec->code < 0xD0U) { step.route = WP_PARSER_CLASSIFY_ROUTE_FIXED_PACKET; step.handler = WP_PARSER_CLASSIFY_HANDLER_7B6F_FIXED; step.variant_balance_check = true; step.normalize_below_d0 = true; step.reverse_transfer_hint = true; step.range_check = true; if (rec->length < 3U) { step.overlay_consume_hint = true; } } else { step.variant_balance_check = true; step.reverse_transfer_hint = true; if (rec->code == 0xDEU) { step.route = WP_PARSER_CLASSIFY_ROUTE_DE_SCANNER_BYPASS; step.de_scanner_bypass = true; step.handler = WP_PARSER_CLASSIFY_HANDLER_7B91_VARIABLE; } else { step.route = WP_PARSER_CLASSIFY_ROUTE_VARIABLE_PACKET; step.handler = WP_PARSER_CLASSIFY_HANDLER_7B91_VARIABLE; step.range_check = true; } if (rec->length < 3U) { step.overlay_consume_hint = true; } } if (rec->code != 0x96U && rec->code != 0x97U) { step.runtime_mismatch_dirty_possible = true; } } if (out_step != NULL) { *out_step = step; } return true; } void wp_parser_classify_apply_step(WpLayoutGlobals *runtime, const WpParserClassifyStep *step, WpParserClassifyStats *stats) { WpRecord metric_record; if (step == NULL || stats == NULL) { return; } memset(&metric_record, 0, sizeof(metric_record)); metric_record.code = step->code; stats->records_seen++; stats->bytes_consumed += step->consumed_record_bytes; if (step->prepends_token) stats->prepended_tokens++; if (step->decrements_update_depth) stats->update_depth_decrements++; if (step->decrements_processed_segments) stats->processed_segment_decrements++; if (step->clears_emit_bit1) stats->emit_bit1_clears++; if (step->cursor_decrement_stage1) stats->cursor_stage1_decrements++; if (step->cursor_decrement_stage2) stats->cursor_stage2_decrements++; if (step->parse_depth_pulse) stats->parse_depth_pulses++; if (step->runtime_bit0_set) stats->runtime_bit0_sets++; if (step->runtime_bit2_set) stats->runtime_bit2_sets++; if (step->cursor_window_reset) stats->cursor_window_resets++; if (step->state_flags_cleared) stats->state_flags_clears++; if (step->status_extend_update) stats->status_extend_updates++; if (step->sub_metric_update) stats->sub_metric_updates++; if (step->render_dirty_set) stats->render_dirty_sets++; if (step->variant_balance_check) stats->variant_balance_checks++; if (step->normalize_below_d0) stats->normalize_below_d0_calls++; if (step->reverse_transfer_hint) stats->reverse_transfer_hints++; if (step->overlay_consume_hint) stats->overlay_consume_hints++; if (step->range_check) stats->range_checks++; if (step->emit_flags_clear_0x14) stats->emit_flags_clear_0x14++; if (step->record_wide_temp_snapshot) stats->record_wide_temp_snapshots++; if (step->runtime_mismatch_dirty_possible) stats->runtime_mismatch_dirty_hints++; switch (step->route) { case WP_PARSER_CLASSIFY_ROUTE_SPACE_BACKTRACK: stats->space_backtracks++; break; case WP_PARSER_CLASSIFY_ROUTE_PRINTABLE_BACKTRACK: stats->printable_backtracks++; break; case WP_PARSER_CLASSIFY_ROUTE_C0_EXTENDED_REPLAY: stats->c0_extended_replays++; if (step->c0_word_replay) stats->c0_replayed_words++; break; case WP_PARSER_CLASSIFY_ROUTE_LOW_CONTROL_RESTORE: stats->deep_classifications++; stats->low_control_restores++; break; case WP_PARSER_CLASSIFY_ROUTE_SINGLE_BYTE_CONTROL: stats->deep_classifications++; stats->single_byte_controls++; break; case WP_PARSER_CLASSIFY_ROUTE_FIXED_PACKET: stats->deep_classifications++; stats->fixed_packets++; break; case WP_PARSER_CLASSIFY_ROUTE_VARIABLE_PACKET: stats->deep_classifications++; stats->variable_packets++; break; case WP_PARSER_CLASSIFY_ROUTE_DE_SCANNER_BYPASS: stats->deep_classifications++; stats->variable_packets++; stats->de_scanner_bypass_packets++; break; case WP_PARSER_CLASSIFY_ROUTE_RANGE_CHECK_FALLBACK: stats->deep_classifications++; stats->range_check_fallbacks++; break; case WP_PARSER_CLASSIFY_ROUTE_NONE: default: break; } switch (step->handler) { case WP_PARSER_CLASSIFY_HANDLER_79FA_TEXT_SINGLE: stats->handler_79fa_text_single++; break; case WP_PARSER_CLASSIFY_HANDLER_7B6F_FIXED: stats->handler_7b6f_fixed++; break; case WP_PARSER_CLASSIFY_HANDLER_7B91_VARIABLE: stats->handler_7b91_variable++; break; case WP_PARSER_CLASSIFY_HANDLER_NONE: default: break; } if (runtime != NULL) { if (step->decrements_update_depth) { runtime->update_depth = (char)(runtime->update_depth - 1); } if (step->decrements_processed_segments) { runtime->processed_segments -= 1; } if (step->clears_emit_bit1) { runtime->emit_flags &= (uint8_t)~0x02U; } if (step->cursor_decrement_stage2) { wp_classify_decrement_cursor(runtime, &metric_record); } if (step->cursor_decrement_stage1) { wp_classify_decrement_cursor(runtime, &metric_record); } if (step->parse_depth_pulse) { runtime->parse_depth_counter = (char)(runtime->parse_depth_counter + 1); } if (step->runtime_bit0_set) { runtime->runtime_flags |= 0x01U; } if (step->cursor_window_reset) { runtime->cursor_window = 0xffffU; } if (step->runtime_bit2_set) { runtime->runtime_flags |= 0x04U; } if (step->state_flags_cleared) { runtime->state_flags = 0; } if (step->status_extend_update) { wp_classify_status_extend_update(runtime); } if (step->sub_metric_update) { runtime->variant_total -= runtime->metrics_line_accum_5037; runtime->variant_anchor = wp_classify_u16((uint16_t)runtime->variant_anchor - (uint16_t)runtime->metrics_line_accum_5037); } if (step->render_dirty_set) { runtime->render_dirty_flags |= 0x01U; } if (step->emit_flags_clear_0x14) { runtime->emit_flags &= 0xebU; } if (step->runtime_mismatch_dirty_possible && (runtime->runtime_flags & 0x01U) == 0U) { runtime->runtime_flags |= 0x08U; } if (step->record_wide_temp_snapshot) { runtime->record_wide_temp_lo_4b26 = (uint16_t)runtime->cursor; runtime->record_wide_temp_hi_4b28 = (uint16_t)runtime->carry_width; } if (step->parse_depth_pulse) { runtime->parse_depth_counter = (char)(runtime->parse_depth_counter - 1); } stats->final_runtime_flags = runtime->runtime_flags; stats->final_emit_flags = runtime->emit_flags; stats->final_parse_depth_counter = (uint8_t)runtime->parse_depth_counter; stats->final_update_depth = runtime->update_depth; stats->final_processed_segments = runtime->processed_segments; stats->final_cursor = (uint16_t)runtime->cursor; stats->final_cursor_window = runtime->cursor_window; stats->final_span_aux = (uint16_t)runtime->span_aux_word_53f6; stats->final_record_aux = runtime->record_aux_4b34; stats->final_state_flags = runtime->state_flags; stats->final_render_dirty_flags = runtime->render_dirty_flags; } } bool wp_parser_classify_apply_record(WpLayoutGlobals *runtime, const WpRecord *rec, WpParserClassifyStats *stats) { WpParserClassifyStep step; if (rec == NULL || stats == NULL) { return false; } if (!wp_parser_classify_plan_record(runtime, rec, &step)) { return false; } wp_parser_classify_apply_step(runtime, &step, stats); wp_classify_count_record_status(rec, stats); return true; } bool wp_parser_classify_run_stream(WpLayoutGlobals *wl, WpParserClassifyStats *stats) { WpLayoutGlobals runtime; if (wl == NULL || stats == NULL) { return false; } wp_parser_classify_stats_clear(stats); memset(&runtime, 0, sizeof(runtime)); runtime.cursor = 0x4000U; runtime.cursor_window = 0U; runtime.span_aux_word_53f6 = 0x4000U; runtime.record_aux_4b34 = 0; runtime.update_depth = 0; runtime.processed_segments = 0; runtime.default_span_a = 1U; runtime.default_span_b = 0U; runtime.span_metric_delta_4bd8 = 1; runtime.span_metric_delta_4bda = 1; while (wl->record_used_bytes > 0) { WpRecord rec; wp_parser_consume_record(wl, &rec); (void)wp_parser_classify_apply_record(&runtime, &rec, stats); wp_record_free(&rec); } return true; } bool wp_parser_classify_run_loaded_file(WpLoadedFile *file, WpParserClassifyStats *stats) { WpLayoutGlobals wl; if (file == NULL || stats == NULL) { return false; } memset(&wl, 0, sizeof(wl)); if (!wp_file_bind_primary_stream(file, &wl, 0x4000U)) { return false; } return wp_parser_classify_run_stream(&wl, stats); } bool wp_parser_classify_run_file(const char *filename, WpParserClassifyStats *stats) { WpLoadedFile file; bool ok; if (filename == NULL || stats == NULL) { return false; } if (!wp_file_load_body(filename, &file)) { return false; } ok = wp_parser_classify_run_loaded_file(&file, stats); wp_file_free(&file); return ok; }
Commit message
This repository is read-only for this account.
Repository snapshot
Current branch
main
Visibility
public
Your access
Read
Remote
None
File activity
View file history