|
| 1 | +// SPDX-License-Identifier: MPL-2.0 |
| 2 | +// SPDX-FileCopyrightText: 2026 hyperpolymath |
| 3 | + |
| 4 | +module ByteDetector; |
| 5 | + |
| 6 | +use prelude::{Option, Some, None}; |
| 7 | +use SafeHex::{encode_bytes, encode_string, encode_byte}; |
| 8 | +use SafeWhitespace::{detect_invisibles}; |
| 9 | + |
| 10 | +pub type Severity = Critical | SevError | Warning | Info; |
| 11 | + |
| 12 | +pub type ArtifactDef = { |
| 13 | + name: String, |
| 14 | + byte_value: Int, |
| 15 | + severity: Severity, |
| 16 | + fix_action: String |
| 17 | +} |
| 18 | + |
| 19 | +pub type Artifact = { |
| 20 | + line: Int, |
| 21 | + column: Int, |
| 22 | + byte_value: Int, |
| 23 | + hex_value: String, |
| 24 | + name: String, |
| 25 | + severity: Severity, |
| 26 | + fix_action: String |
| 27 | +} |
| 28 | + |
| 29 | +pub fn known_artifacts() -> [ArtifactDef] { |
| 30 | + [ |
| 31 | + #{ name: "NULL", byte_value: 0, severity: Critical, fix_action: "remove" }, |
| 32 | + #{ name: "NBSP", byte_value: 160, severity: SevError, fix_action: "replace:20" }, |
| 33 | + #{ name: "ZWSP", byte_value: 8203, severity: SevError, fix_action: "remove" }, |
| 34 | + #{ name: "BOM", byte_value: 65279, severity: Warning, fix_action: "remove" }, |
| 35 | + #{ name: "SHY", byte_value: 173, severity: Info, fix_action: "remove" }, |
| 36 | + #{ name: "LRM", byte_value: 8206, severity: Info, fix_action: "remove" }, |
| 37 | + #{ name: "RLM", byte_value: 8207, severity: Info, fix_action: "remove" }, |
| 38 | + #{ name: "WJ", byte_value: 8288, severity: Info, fix_action: "remove" }, |
| 39 | + #{ name: "ZWNJ", byte_value: 8204, severity: Warning, fix_action: "keep" }, |
| 40 | + #{ name: "ZWJ", byte_value: 8205, severity: Warning, fix_action: "keep" } |
| 41 | + ] |
| 42 | +} |
| 43 | + |
| 44 | +pub fn get_artifact_def(byte_val: Int) -> Option<ArtifactDef> { |
| 45 | + let defs = known_artifacts(); |
| 46 | + for d in defs { |
| 47 | + if d.byte_value == byte_val { |
| 48 | + return Some(d); |
| 49 | + } |
| 50 | + } |
| 51 | + None |
| 52 | +} |
| 53 | + |
| 54 | +pub fn byte_to_hex(v: Int) -> String { |
| 55 | + encode_byte(v & 255) |
| 56 | +} |
| 57 | + |
| 58 | +pub fn scan(content: String) -> [Artifact] { |
| 59 | + let mut results = []; |
| 60 | + let mut line = 1; |
| 61 | + let mut col = 1; |
| 62 | + let n = len(content); |
| 63 | + let mut i = 0; |
| 64 | + while i < n { |
| 65 | + let c = string_get(content, i); |
| 66 | + let code = char_to_int(c); |
| 67 | + if code == 10 { |
| 68 | + line = line + 1; |
| 69 | + col = 1; |
| 70 | + } else { |
| 71 | + match get_artifact_def(code) { |
| 72 | + Some(def) => { |
| 73 | + results = results ++ [#{ |
| 74 | + line: line, |
| 75 | + column: col, |
| 76 | + byte_value: code, |
| 77 | + hex_value: byte_to_hex(code), |
| 78 | + name: def.name, |
| 79 | + severity: def.severity, |
| 80 | + fix_action: def.fix_action |
| 81 | + }]; |
| 82 | + col = col + 1; |
| 83 | + }, |
| 84 | + None => { |
| 85 | + col = col + 1; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + i = i + 1; |
| 90 | + } |
| 91 | + results |
| 92 | +} |
| 93 | + |
| 94 | +pub fn scan_to_hex(content: String) -> String { |
| 95 | + let artifacts = scan(content); |
| 96 | + let mut lines = ""; |
| 97 | + let mut first = true; |
| 98 | + for a in artifacts { |
| 99 | + if !first { |
| 100 | + lines = lines ++ "\n"; |
| 101 | + } |
| 102 | + lines = lines ++ "0x" ++ to_uppercase(a.hex_value) ++ " [" ++ a.name ++ "] at L:" ++ int_to_string(a.line) ++ " C:" ++ int_to_string(a.column); |
| 103 | + first = false; |
| 104 | + } |
| 105 | + lines |
| 106 | +} |
| 107 | + |
| 108 | +pub fn apply_fixes(content: String) -> (String, Int) { |
| 109 | + let mut result = content; |
| 110 | + let mut count = 0; |
| 111 | + let defs = known_artifacts(); |
| 112 | + for def in defs { |
| 113 | + if def.fix_action == "remove" { |
| 114 | + let parts_count = len(result); |
| 115 | + let fixed = replace_char(result, def.byte_value, ""); |
| 116 | + let new_count = len(fixed); |
| 117 | + count = count + (parts_count - new_count); |
| 118 | + result = fixed; |
| 119 | + } else if def.fix_action == "replace:20" { |
| 120 | + result = replace_char(result, def.byte_value, " "); |
| 121 | + } |
| 122 | + } |
| 123 | + (result, count) |
| 124 | +} |
| 125 | + |
| 126 | +fn replace_char(s: String, target_code: Int, replacement: String) -> String { |
| 127 | + let n = len(s); |
| 128 | + let mut result = ""; |
| 129 | + let mut i = 0; |
| 130 | + while i < n { |
| 131 | + let c = string_get(s, i); |
| 132 | + let code = char_to_int(c); |
| 133 | + if code == target_code { |
| 134 | + result = result ++ replacement; |
| 135 | + } else { |
| 136 | + result = result ++ show(c); |
| 137 | + } |
| 138 | + i = i + 1; |
| 139 | + } |
| 140 | + result |
| 141 | +} |
| 142 | + |
| 143 | +fn severity_order(s: Severity) -> Int { |
| 144 | + match s { |
| 145 | + Critical => 4, |
| 146 | + SevError => 3, |
| 147 | + Warning => 2, |
| 148 | + Info => 1 |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +pub fn filter_by_severity(artifacts: [Artifact], min_severity: Severity) -> [Artifact] { |
| 153 | + let min_order = severity_order(min_severity); |
| 154 | + let mut result = []; |
| 155 | + for a in artifacts { |
| 156 | + if severity_order(a.severity) >= min_order { |
| 157 | + result = result ++ [a]; |
| 158 | + } |
| 159 | + } |
| 160 | + result |
| 161 | +} |
| 162 | + |
| 163 | +pub fn generate_report(artifacts: [Artifact]) -> String { |
| 164 | + if len(artifacts) == 0 { |
| 165 | + "No invisible artifacts detected." |
| 166 | + } else { |
| 167 | + let header = "Found " ++ int_to_string(len(artifacts)) ++ " invisible artifact(s):\n"; |
| 168 | + let mut lines = header; |
| 169 | + for a in artifacts { |
| 170 | + let sev_str = match a.severity { |
| 171 | + Critical => "CRITICAL", |
| 172 | + SevError => "ERROR", |
| 173 | + Warning => "WARNING", |
| 174 | + Info => "INFO" |
| 175 | + }; |
| 176 | + lines = lines ++ "[" ++ sev_str ++ "] " ++ a.name ++ " (0x" ++ to_uppercase(a.hex_value) ++ ") at L:" ++ int_to_string(a.line) ++ " C:" ++ int_to_string(a.column) ++ " - " ++ a.fix_action ++ "\n"; |
| 177 | + } |
| 178 | + lines |
| 179 | + } |
| 180 | +} |
0 commit comments