rhwp 0.9.0

HWP/HWPX document engine for Rust applications, WebAssembly, and CLI automation
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Targeted CTRL_DATA contract tracer.
//!
//! This command does not generate HWP probes. It decodes CTRL_DATA payloads from
//! a Hancom oracle HWP and compares their presence against a generated HWP.

use std::fmt::Write as _;
use std::fs;
use std::path::PathBuf;

use crate::diagnostics::hwp5_inventory::{build_inventory, Hwp5InventoryItem};
use crate::parser::tags;

#[derive(Debug)]
struct Options {
    oracle: PathBuf,
    generated: PathBuf,
    out: PathBuf,
    section: Option<u32>,
    record_index: Option<usize>,
}

pub fn run(args: &[String]) -> i32 {
    // 종료 코드 계약(mydocs/manual/cli_commands.md): 명시적 `--help` 만 성공(0)이고,
    // 인자 누락·파싱 실패는 사용법 오류(2), 실행 실패는 런타임 오류(1)다. 종전에는
    // 반환형이 `()` 라 main 의 exit_with 를 통과하지 못해 무슨 일이 나든 0으로 끝났다.
    if matches!(args.first().map(String::as_str), Some("--help" | "-h")) {
        print_usage();
        return super::EXIT_OK;
    }
    if args.is_empty() {
        print_usage();
        return super::EXIT_USAGE;
    }

    let options = match parse_args(args) {
        Ok(options) => options,
        Err(message) => {
            eprintln!("{message}");
            print_usage();
            return super::EXIT_USAGE;
        }
    };

    if let Err(error) = run_inner(options) {
        eprintln!("오류: {error}");
        return super::EXIT_RUNTIME;
    }
    super::EXIT_OK
}

fn parse_args(args: &[String]) -> Result<Options, String> {
    let mut inputs = Vec::new();
    let mut out = None;
    let mut section = None;
    let mut record_index = None;

    let mut i = 0;
    while i < args.len() {
        match args[i].as_str() {
            "--out" | "-o" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--out 뒤에 경로가 필요합니다".to_string())?;
                out = Some(PathBuf::from(value));
                i += 2;
            }
            "--section" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--section 뒤에 번호가 필요합니다".to_string())?;
                section = Some(
                    value
                        .parse::<u32>()
                        .map_err(|_| format!("section 번호가 올바르지 않습니다: {value}"))?,
                );
                i += 2;
            }
            "--record-index" => {
                let value = args
                    .get(i + 1)
                    .ok_or_else(|| "--record-index 뒤에 번호가 필요합니다".to_string())?;
                record_index = Some(
                    value
                        .parse::<usize>()
                        .map_err(|_| format!("record-index가 올바르지 않습니다: {value}"))?,
                );
                i += 2;
            }
            value if value.starts_with('-') => {
                return Err(format!("알 수 없는 옵션: {value}"));
            }
            value => {
                inputs.push(PathBuf::from(value));
                i += 1;
            }
        }
    }

    if inputs.len() != 2 {
        return Err("oracle HWP와 generated HWP 경로가 필요합니다".to_string());
    }

    Ok(Options {
        oracle: inputs[0].clone(),
        generated: inputs[1].clone(),
        out: out.ok_or_else(|| "--out 경로가 필요합니다".to_string())?,
        section,
        record_index,
    })
}

fn print_usage() {
    // 안내는 stderr 로 나간다(계약). stdout 은 프로브 데이터 전용이라
    // 섞이면 JSONL 을 파이프로 받는 소비자가 파싱에서 깨진다.
    eprintln!("사용법:");
    eprintln!(
        "  rhwp hwp5-ctrl-data-trace <oracle.hwp> <generated.hwp> --out <path> [--section N] [--record-index N]"
    );
}

fn run_inner(options: Options) -> Result<(), String> {
    let oracle = build_inventory(&options.oracle, options.section)?;
    let generated = build_inventory(&options.generated, options.section)?;

    let oracle_ctrl_data = filter_ctrl_data(&oracle.items, options.record_index);
    let generated_ctrl_data = filter_ctrl_data(&generated.items, options.record_index);

    let mut output = String::new();
    let _ = writeln!(output, "# HWP5 CTRL_DATA Trace");
    let _ = writeln!(output);
    let _ = writeln!(output, "- oracle: `{}`", options.oracle.display());
    let _ = writeln!(output, "- generated: `{}`", options.generated.display());
    if let Some(section) = options.section {
        let _ = writeln!(output, "- section: `{section}`");
    }
    if let Some(record_index) = options.record_index {
        let _ = writeln!(output, "- record_index filter: `{record_index}`");
    }
    let _ = writeln!(output);

    let _ = writeln!(output, "## Summary");
    let _ = writeln!(output);
    let _ = writeln!(
        output,
        "| side | CTRL_DATA records | total bytes | payload hashes |"
    );
    let _ = writeln!(output, "|---|---:|---:|---|");
    render_summary_row(&mut output, "oracle", &oracle_ctrl_data);
    render_summary_row(&mut output, "generated", &generated_ctrl_data);
    let _ = writeln!(output);

    let _ = writeln!(output, "## Oracle CTRL_DATA Records");
    let _ = writeln!(output);
    if oracle_ctrl_data.is_empty() {
        let _ = writeln!(output, "oracle CTRL_DATA record가 없다.");
    } else {
        for item in &oracle_ctrl_data {
            render_ctrl_data_item(&mut output, item);
        }
    }

    let _ = writeln!(output);
    let _ = writeln!(output, "## Generated CTRL_DATA Records");
    let _ = writeln!(output);
    if generated_ctrl_data.is_empty() {
        let _ = writeln!(output, "generated CTRL_DATA record가 없다.");
    } else {
        for item in &generated_ctrl_data {
            render_ctrl_data_item(&mut output, item);
        }
    }

    if let Some(parent) = options.out.parent() {
        fs::create_dir_all(parent)
            .map_err(|error| format!("출력 폴더 생성 실패 - {}: {error}", parent.display()))?;
    }
    fs::write(&options.out, output)
        .map_err(|error| format!("출력 파일 쓰기 실패 - {}: {error}", options.out.display()))?;

    println!("generated: {}", options.out.display());
    Ok(())
}

fn filter_ctrl_data(
    items: &[Hwp5InventoryItem],
    record_index: Option<usize>,
) -> Vec<&Hwp5InventoryItem> {
    items
        .iter()
        .filter(|item| item.tag_id == tags::HWPTAG_CTRL_DATA)
        .filter(|item| record_index.is_none_or(|index| item.record_index == index))
        .collect()
}

fn render_summary_row(output: &mut String, side: &str, items: &[&Hwp5InventoryItem]) {
    let total_bytes: usize = items.iter().map(|item| item.payload_bytes.len()).sum();
    let hashes = if items.is_empty() {
        "-".to_string()
    } else {
        items
            .iter()
            .map(|item| short_hash(&item.payload_hash).to_string())
            .collect::<Vec<_>>()
            .join(", ")
    };
    let _ = writeln!(
        output,
        "| {side} | {} | {} | `{}` |",
        items.len(),
        total_bytes,
        escape_md(&hashes)
    );
}

fn render_ctrl_data_item(output: &mut String, item: &Hwp5InventoryItem) {
    let _ = writeln!(output, "### `{}`", item.record_uid);
    let _ = writeln!(output);
    let _ = writeln!(output, "| field | value |");
    let _ = writeln!(output, "|---|---|");
    let _ = writeln!(output, "| stream | `{}` |", escape_md(&item.stream_path));
    let _ = writeln!(output, "| record_index | `{}` |", item.record_index);
    let _ = writeln!(output, "| level | `{}` |", item.level);
    let _ = writeln!(output, "| size | `{}` |", item.size);
    let _ = writeln!(
        output,
        "| parent | `{}` |",
        escape_md(item.parent_scope.as_deref().unwrap_or("-"))
    );
    let _ = writeln!(output, "| scope | `{}` |", escape_md(&item.scope_path));
    let _ = writeln!(
        output,
        "| payload_head | `{}` |",
        escape_md(&item.payload_head_hex)
    );
    let _ = writeln!(output, "| hash | `{}` |", short_hash(&item.payload_hash));
    let _ = writeln!(output);
    let _ = writeln!(output, "#### ParameterSet decode");
    let _ = writeln!(output);
    let mut lines = Vec::new();
    match decode_parameter_set(&item.payload_bytes, 0, 0, &mut lines) {
        Ok(consumed) => {
            for line in lines {
                let _ = writeln!(output, "- {}", escape_md(&line));
            }
            if consumed < item.payload_bytes.len() {
                let _ = writeln!(
                    output,
                    "- trailing bytes: offset={} len={} `{}`",
                    consumed,
                    item.payload_bytes.len() - consumed,
                    hex_bytes(&item.payload_bytes[consumed..])
                );
            }
        }
        Err(error) => {
            let _ = writeln!(output, "- decode error: {}", escape_md(&error));
            let _ = writeln!(output, "- raw: `{}`", hex_bytes(&item.payload_bytes));
        }
    }
    let _ = writeln!(output);
}

fn decode_parameter_set(
    data: &[u8],
    offset: usize,
    depth: usize,
    lines: &mut Vec<String>,
) -> Result<usize, String> {
    if offset + 6 > data.len() {
        return Err(format!("ParameterSet header 부족: offset={offset}"));
    }

    let ps_id = read_u16(data, offset)?;
    let count = read_u16(data, offset + 2)?;
    let dummy = read_u16(data, offset + 4)?;
    lines.push(format!(
        "{}ParameterSet ps_id=0x{ps_id:04x} count={count} dummy=0x{dummy:04x}",
        indent(depth)
    ));

    let mut cursor = offset + 6;
    for index in 0..count {
        if cursor + 4 > data.len() {
            return Err(format!(
                "ParameterItem header 부족: index={index} offset={cursor}"
            ));
        }
        let item_id = read_u16(data, cursor)?;
        let item_type = read_u16(data, cursor + 2)?;
        cursor += 4;
        lines.push(format!(
            "{}item#{index} id=0x{item_id:04x} type=0x{item_type:04x}({})",
            indent(depth + 1),
            param_type_name(item_type)
        ));

        match item_type {
            0x0000 => {}
            0x0001 => {
                let len = read_u16(data, cursor)? as usize;
                cursor += 2;
                let byte_len = len.saturating_mul(2);
                if cursor + byte_len > data.len() {
                    return Err(format!("String value 부족: len={len} offset={cursor}"));
                }
                let value = decode_utf16le(&data[cursor..cursor + byte_len]);
                lines.push(format!(
                    "{}string len={len} value=\"{value}\"",
                    indent(depth + 2)
                ));
                cursor += byte_len;
            }
            0x0002..=0x0009 => {
                if cursor + 4 > data.len() {
                    return Err(format!("integer value 부족: offset={cursor}"));
                }
                let value = read_u32(data, cursor)?;
                lines.push(format!("{}u32=0x{value:08x} ({value})", indent(depth + 2)));
                cursor += 4;
            }
            0x8000 => {
                cursor = decode_parameter_set(data, cursor, depth + 2, lines)?;
            }
            0x8001 => {
                if cursor + 4 > data.len() {
                    return Err(format!("array header 부족: offset={cursor}"));
                }
                let array_count = read_u16(data, cursor)?;
                let array_item_id = read_u16(data, cursor + 2)?;
                lines.push(format!(
                    "{}array count={array_count} item_id=0x{array_item_id:04x} (raw tail not expanded)",
                    indent(depth + 2)
                ));
                cursor += 4;
            }
            0x8002 => {
                let value = read_u16(data, cursor)?;
                lines.push(format!("{}bindata_id={value}", indent(depth + 2)));
                cursor += 2;
            }
            _ => {
                return Err(format!(
                    "지원하지 않는 ParameterItem type=0x{item_type:04x} offset={cursor}"
                ));
            }
        }
    }

    Ok(cursor)
}

fn read_u16(data: &[u8], offset: usize) -> Result<u16, String> {
    if offset + 2 > data.len() {
        return Err(format!("u16 읽기 범위 초과: offset={offset}"));
    }
    Ok(u16::from_le_bytes([data[offset], data[offset + 1]]))
}

fn read_u32(data: &[u8], offset: usize) -> Result<u32, String> {
    if offset + 4 > data.len() {
        return Err(format!("u32 읽기 범위 초과: offset={offset}"));
    }
    Ok(u32::from_le_bytes([
        data[offset],
        data[offset + 1],
        data[offset + 2],
        data[offset + 3],
    ]))
}

fn decode_utf16le(data: &[u8]) -> String {
    let units: Vec<u16> = data
        .chunks_exact(2)
        .map(|chunk| u16::from_le_bytes([chunk[0], chunk[1]]))
        .collect();
    String::from_utf16_lossy(&units)
}

fn param_type_name(item_type: u16) -> &'static str {
    match item_type {
        0x0000 => "NULL",
        0x0001 => "String",
        0x0002 => "Integer1",
        0x0003 => "Integer2",
        0x0004 => "Integer4",
        0x0005 => "Integer",
        0x0006 => "UnsignedInteger1",
        0x0007 => "UnsignedInteger2",
        0x0008 => "UnsignedInteger4",
        0x0009 => "UnsignedInteger",
        0x8000 => "ParameterSet",
        0x8001 => "Array",
        0x8002 => "BINDataID",
        _ => "Unknown",
    }
}

fn indent(depth: usize) -> String {
    "  ".repeat(depth)
}

fn hex_bytes(bytes: &[u8]) -> String {
    let mut result = String::new();
    for (index, byte) in bytes.iter().enumerate() {
        if index > 0 {
            result.push(' ');
        }
        let _ = write!(result, "{byte:02x}");
    }
    result
}

fn short_hash(hash: &str) -> &str {
    let stripped = hash.strip_prefix("blake3:").unwrap_or(hash);
    stripped.get(..16).unwrap_or(stripped)
}

fn escape_md(value: &str) -> String {
    value.replace('|', "\\|").replace('\n', " ")
}