fcoreutils 0.22.0

High-performance GNU coreutils replacement with SIMD and parallelism
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
use std::io::{self, Read, Write};

/// Address radix for the offset column.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AddressRadix {
    Octal,
    Decimal,
    Hex,
    None,
}

/// Byte order for multi-byte values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Endian {
    Little,
    Big,
    Native,
}

/// Output format specifier.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputFormat {
    /// Named character (a): nul, soh, stx, ...
    NamedChar,
    /// Printable character or backslash escape (c): \0, \a, \b, \t, \n, ...
    PrintableChar,
    /// Signed decimal integer of given byte size (d1, d2, d4, d8)
    SignedDec(usize),
    /// Floating point of given byte size (f4, f8)
    Float(usize),
    /// Octal integer of given byte size (o1, o2, o4)
    Octal(usize),
    /// Unsigned decimal integer of given byte size (u1, u2, u4, u8)
    UnsignedDec(usize),
    /// Hexadecimal integer of given byte size (x1, x2, x4, x8)
    Hex(usize),
}

/// Configuration for the od command.
#[derive(Debug, Clone)]
pub struct OdConfig {
    pub address_radix: AddressRadix,
    pub formats: Vec<OutputFormat>,
    /// Per-format flag: if true, append printable ASCII annotation (the 'z' suffix).
    pub z_flags: Vec<bool>,
    pub skip_bytes: u64,
    pub read_bytes: Option<u64>,
    pub width: usize,
    pub show_duplicates: bool,
    pub endian: Endian,
}

impl Default for OdConfig {
    fn default() -> Self {
        Self {
            address_radix: AddressRadix::Octal,
            formats: vec![OutputFormat::Octal(2)],
            z_flags: vec![false],
            skip_bytes: 0,
            read_bytes: None,
            width: 16,
            show_duplicates: false,
            endian: Endian::Native,
        }
    }
}

/// Named characters for -t a format (ASCII named characters).
/// Index 0..=127 maps to the name for that byte value.
const NAMED_CHARS: [&str; 128] = [
    "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", " bs", " ht", " nl", " vt", " ff",
    " cr", " so", " si", "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", "can", " em",
    "sub", "esc", " fs", " gs", " rs", " us", " sp", "!", "\"", "#", "$", "%", "&", "'", "(", ")",
    "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<",
    "=", ">", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O",
    "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a", "b",
    "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
    "v", "w", "x", "y", "z", "{", "|", "}", "~", "del",
];

/// Return the field width for a single value of the given format.
/// This matches GNU od's column widths.
fn field_width(fmt: OutputFormat) -> usize {
    match fmt {
        OutputFormat::NamedChar => 4, // 3 chars + leading space => " nul" = 4 wide
        OutputFormat::PrintableChar => 4, // 3 chars + leading space => "  \\n" = 4 wide
        OutputFormat::Octal(1) => 4,  // " 377"
        OutputFormat::Octal(2) => 7,  // " 177777"
        OutputFormat::Octal(4) => 12, // " 37777777777"
        OutputFormat::Octal(8) => 23, // " 1777777777777777777777"
        OutputFormat::Hex(1) => 3,    // " ff"
        OutputFormat::Hex(2) => 5,    // " ffff"
        OutputFormat::Hex(4) => 9,    // " ffffffff"
        OutputFormat::Hex(8) => 17,   // " ffffffffffffffff"
        OutputFormat::UnsignedDec(1) => 4, // " 255"
        OutputFormat::UnsignedDec(2) => 6, // " 65535"
        OutputFormat::UnsignedDec(4) => 11, // " 4294967295"
        OutputFormat::UnsignedDec(8) => 21, // " 18446744073709551615"
        OutputFormat::SignedDec(1) => 5, // " -128"
        OutputFormat::SignedDec(2) => 7, // " -32768"
        OutputFormat::SignedDec(4) => 12, // " -2147483648"
        OutputFormat::SignedDec(8) => 21, // " -9223372036854775808"
        OutputFormat::Float(4) => 16, // "   x.xxxxxxxe+xx" (3 leading spaces for positive max)
        OutputFormat::Float(8) => 25, // " -x.xxxxxxxxxxxxxxe+xxx"
        _ => 4,
    }
}

/// Get the byte size of a format element.
fn element_size(fmt: OutputFormat) -> usize {
    match fmt {
        OutputFormat::NamedChar | OutputFormat::PrintableChar => 1,
        OutputFormat::SignedDec(s)
        | OutputFormat::Float(s)
        | OutputFormat::Octal(s)
        | OutputFormat::UnsignedDec(s)
        | OutputFormat::Hex(s) => s,
    }
}

/// Format a float using C's %g format.
/// Uses libc snprintf on Unix and Rust formatting on Windows.
fn snprintf_g(v: f64, precision: usize) -> String {
    let precision = precision.min(50);
    #[cfg(unix)]
    {
        // Pre-built format strings for common precisions to avoid allocation
        static FMT_STRINGS: &[&std::ffi::CStr] = &[
            c"%.0g", c"%.1g", c"%.2g", c"%.3g", c"%.4g", c"%.5g", c"%.6g", c"%.7g", c"%.8g",
            c"%.9g", c"%.10g", c"%.11g", c"%.12g", c"%.13g", c"%.14g", c"%.15g", c"%.16g",
            c"%.17g", c"%.18g", c"%.19g", c"%.20g",
        ];
        let mut buf = [0u8; 64];
        let fmt_cstr: std::ffi::CString;
        let fmt_ptr = if precision < FMT_STRINGS.len() {
            FMT_STRINGS[precision].as_ptr()
        } else {
            fmt_cstr = std::ffi::CString::new(format!("%.{}g", precision)).unwrap();
            fmt_cstr.as_ptr()
        };
        let len =
            unsafe { libc::snprintf(buf.as_mut_ptr() as *mut libc::c_char, buf.len(), fmt_ptr, v) };
        if len > 0 && (len as usize) < buf.len() {
            return String::from_utf8_lossy(&buf[..len as usize]).into_owned();
        }
    }
    // Fallback / Windows: use Rust formatting with %g-like behavior
    let s = format!("{:.prec$e}", v, prec = precision.saturating_sub(1));
    // Convert scientific notation to shortest form like %g
    if let Some(e_pos) = s.find('e') {
        let exp: i32 = s[e_pos + 1..].parse().unwrap_or(0);
        if exp >= -(precision as i32) && exp < precision as i32 {
            // Use fixed notation
            let fixed = format!(
                "{:.prec$}",
                v,
                prec = (precision as i32 - 1 - exp).max(0) as usize
            );
            // Trim trailing zeros after decimal point
            if fixed.contains('.') {
                let trimmed = fixed.trim_end_matches('0').trim_end_matches('.');
                return trimmed.to_string();
            }
            return fixed;
        }
    }
    format!("{:.*e}", precision.saturating_sub(1), v)
}

/// Format f32 like GNU od: uses %.8g formatting (8 significant digits).
fn format_float_f32(v: f32) -> String {
    // Use shortest decimal representation that uniquely round-trips (like Ryu / GNU od).
    // Try increasing precisions from FLT_DIG (6) to FLT_DECIMAL_DIG (9).
    for prec in 6usize..=9 {
        let s = snprintf_g(v as f64, prec);
        if let Ok(reparsed) = s.trim().parse::<f32>() {
            if reparsed == v {
                return s;
            }
        }
    }
    snprintf_g(v as f64, 9)
}

/// Format f64 like GNU od: shortest representation that round-trips.
/// Try increasing precisions from DBL_DIG (15) to DBL_DECIMAL_DIG (17).
fn format_float_f64(v: f64) -> String {
    for prec in 15usize..=17 {
        let s = snprintf_g(v, prec);
        if let Ok(reparsed) = s.trim().parse::<f64>() {
            if reparsed.to_bits() == v.to_bits() {
                return s;
            }
        }
    }
    snprintf_g(v, 17)
}

/// Read a u16 from bytes with the specified endianness.
#[inline]
fn read_u16(bytes: &[u8], endian: Endian) -> u16 {
    let arr: [u8; 2] = bytes[..2].try_into().unwrap();
    match endian {
        Endian::Big => u16::from_be_bytes(arr),
        Endian::Little | Endian::Native => u16::from_le_bytes(arr),
    }
}

/// Read a u32 from bytes with the specified endianness.
#[inline]
fn read_u32(bytes: &[u8], endian: Endian) -> u32 {
    let arr: [u8; 4] = bytes[..4].try_into().unwrap();
    match endian {
        Endian::Big => u32::from_be_bytes(arr),
        Endian::Little | Endian::Native => u32::from_le_bytes(arr),
    }
}

/// Read a u64 from bytes with the specified endianness.
#[inline]
fn read_u64(bytes: &[u8], endian: Endian) -> u64 {
    let arr: [u8; 8] = bytes[..8].try_into().unwrap();
    match endian {
        Endian::Big => u64::from_be_bytes(arr),
        Endian::Little | Endian::Native => u64::from_le_bytes(arr),
    }
}

/// Hex digit table.
static HEX_DIGITS: &[u8; 16] = b"0123456789abcdef";

/// Format a u64 as zero-padded octal into a stack buffer.
/// Returns the number of bytes written (always `digits`).
#[inline]
fn fmt_octal(mut v: u64, buf: &mut [u8], digits: usize) -> usize {
    let mut i = digits;
    while i > 0 {
        i -= 1;
        buf[i] = b'0' + (v & 7) as u8;
        v >>= 3;
    }
    digits
}

/// Format a u64 as zero-padded hex into a stack buffer.
/// Returns the number of bytes written (always `digits`).
#[inline]
fn fmt_hex(mut v: u64, buf: &mut [u8], digits: usize) -> usize {
    let mut i = digits;
    while i > 0 {
        i -= 1;
        buf[i] = HEX_DIGITS[(v & 0xF) as usize];
        v >>= 4;
    }
    digits
}

/// Format a u64 as decimal into a stack buffer (right-aligned, no padding).
/// Returns the number of bytes written.
#[inline]
fn fmt_unsigned(mut v: u64, buf: &mut [u8]) -> usize {
    if v == 0 {
        buf[0] = b'0';
        return 1;
    }
    let mut i = 0;
    while v > 0 {
        buf[i] = b'0' + (v % 10) as u8;
        v /= 10;
        i += 1;
    }
    // Reverse in-place
    buf[..i].reverse();
    i
}

/// Format an i64 as decimal into a stack buffer.
/// Returns the number of bytes written.
#[inline]
fn fmt_signed(v: i64, buf: &mut [u8]) -> usize {
    if v >= 0 {
        return fmt_unsigned(v as u64, buf);
    }
    buf[0] = b'-';
    let len = fmt_unsigned((-(v as i128)) as u64, &mut buf[1..]);
    1 + len
}

/// Write a left-padded (right-aligned) value to output. `value_buf[..value_len]` contains the
/// formatted number, and `width` is the total field width (including leading spaces).
#[inline]
fn write_padded(
    out: &mut impl Write,
    value_buf: &[u8],
    value_len: usize,
    width: usize,
) -> io::Result<()> {
    const SPACES: [u8; 32] = [b' '; 32];
    let pad = width.saturating_sub(value_len);
    let mut remaining = pad;
    while remaining > 0 {
        let chunk = remaining.min(SPACES.len());
        out.write_all(&SPACES[..chunk])?;
        remaining -= chunk;
    }
    out.write_all(&value_buf[..value_len])
}

/// Write a formatted value directly to the output using stack buffers (zero heap allocation).
#[inline]
fn write_value(
    out: &mut impl Write,
    bytes: &[u8],
    fmt: OutputFormat,
    width: usize,
    endian: Endian,
) -> io::Result<()> {
    let mut buf = [0u8; 24]; // max: 22 octal digits for u64 + 2 spare
    match fmt {
        OutputFormat::NamedChar => {
            let b = bytes[0];
            if b < 128 {
                let s = NAMED_CHARS[b as usize].as_bytes();
                write_padded(out, s, s.len(), width)
            } else {
                let len = fmt_octal(b as u64, &mut buf, 3);
                write_padded(out, &buf, len, width)
            }
        }
        OutputFormat::PrintableChar => {
            let b = bytes[0];
            let s: &[u8] = match b {
                0x00 => b"\\0",
                0x07 => b"\\a",
                0x08 => b"\\b",
                0x09 => b"\\t",
                0x0a => b"\\n",
                0x0b => b"\\v",
                0x0c => b"\\f",
                0x0d => b"\\r",
                _ => b"",
            };
            if !s.is_empty() {
                write_padded(out, s, s.len(), width)
            } else if (0x20..=0x7e).contains(&b) {
                buf[0] = b;
                write_padded(out, &buf, 1, width)
            } else {
                buf[0] = b'0' + (b >> 6);
                buf[1] = b'0' + ((b >> 3) & 7);
                buf[2] = b'0' + (b & 7);
                write_padded(out, &buf, 3, width)
            }
        }
        OutputFormat::Octal(size) => {
            let (v, digits) = match size {
                1 => (bytes[0] as u64, 3),
                2 => (read_u16(bytes, endian) as u64, 6),
                4 => (read_u32(bytes, endian) as u64, 11),
                8 => (read_u64(bytes, endian), 22),
                _ => return Ok(()),
            };
            let len = fmt_octal(v, &mut buf, digits);
            write_padded(out, &buf, len, width)
        }
        OutputFormat::Hex(size) => {
            let (v, digits) = match size {
                1 => (bytes[0] as u64, 2),
                2 => (read_u16(bytes, endian) as u64, 4),
                4 => (read_u32(bytes, endian) as u64, 8),
                8 => (read_u64(bytes, endian), 16),
                _ => return Ok(()),
            };
            let len = fmt_hex(v, &mut buf, digits);
            write_padded(out, &buf, len, width)
        }
        OutputFormat::UnsignedDec(size) => {
            let v = match size {
                1 => bytes[0] as u64,
                2 => read_u16(bytes, endian) as u64,
                4 => read_u32(bytes, endian) as u64,
                8 => read_u64(bytes, endian),
                _ => return Ok(()),
            };
            let len = fmt_unsigned(v, &mut buf);
            write_padded(out, &buf, len, width)
        }
        OutputFormat::SignedDec(size) => {
            let v: i64 = match size {
                1 => bytes[0] as i8 as i64,
                2 => read_u16(bytes, endian) as i16 as i64,
                4 => read_u32(bytes, endian) as i32 as i64,
                8 => read_u64(bytes, endian) as i64,
                _ => return Ok(()),
            };
            let len = fmt_signed(v, &mut buf);
            write_padded(out, &buf, len, width)
        }
        OutputFormat::Float(size) => match size {
            4 => {
                let v = f32::from_bits(read_u32(bytes, endian));
                write!(out, "{:>w$}", format_float_f32(v), w = width)
            }
            8 => {
                let v = f64::from_bits(read_u64(bytes, endian));
                write!(out, "{:>w$}", format_float_f64(v), w = width)
            }
            _ => Ok(()),
        },
    }
}

/// Compute the effective field width for each format, ensuring multi-format alignment.
/// GNU od computes the total chars_per_block for each format (num_elements * field_width),
/// takes the maximum across all formats, then distributes that evenly back to each format.
fn compute_effective_widths(formats: &[OutputFormat], line_width: usize) -> Vec<usize> {
    if formats.len() <= 1 {
        return formats.iter().map(|f| field_width(*f)).collect();
    }

    let mut max_chars_per_block = 0usize;
    for fmt in formats {
        let es = element_size(*fmt);
        let fw = field_width(*fmt);
        let num_elems = line_width / es;
        let chars = num_elems * fw;
        if chars > max_chars_per_block {
            max_chars_per_block = chars;
        }
    }

    // Now compute effective field width for each format
    formats
        .iter()
        .map(|fmt| {
            let es = element_size(*fmt);
            let num_elems = line_width / es;
            if num_elems > 0 {
                max_chars_per_block / num_elems
            } else {
                field_width(*fmt)
            }
        })
        .collect()
}

/// Write one line of output for a given format type directly to the writer.
fn write_format_line(
    out: &mut impl Write,
    chunk: &[u8],
    fmt: OutputFormat,
    line_width: usize,
    is_first_format: bool,
    radix: AddressRadix,
    offset: u64,
    z_annotate: bool,
    effective_fw: usize,
    endian: Endian,
) -> io::Result<()> {
    // Address prefix — use stack buffer to avoid format!() allocation
    if is_first_format {
        let mut addr_buf = [0u8; 22];
        match radix {
            AddressRadix::Octal => {
                let len = fmt_octal(offset, &mut addr_buf, 7);
                out.write_all(&addr_buf[..len])?;
            }
            AddressRadix::Decimal => {
                let mut tmp = [0u8; 20];
                let vlen = fmt_unsigned(offset, &mut tmp);
                // Zero-pad to 7 digits
                let pad = 7usize.saturating_sub(vlen);
                for b in addr_buf.iter_mut().take(pad) {
                    *b = b'0';
                }
                addr_buf[pad..pad + vlen].copy_from_slice(&tmp[..vlen]);
                out.write_all(&addr_buf[..pad + vlen])?;
            }
            AddressRadix::Hex => {
                let len = fmt_hex(offset, &mut addr_buf, 6);
                out.write_all(&addr_buf[..len])?;
            }
            AddressRadix::None => {}
        }
    } else if radix != AddressRadix::None {
        let addr_width = match radix {
            AddressRadix::Octal | AddressRadix::Decimal => 7,
            AddressRadix::Hex => 6,
            AddressRadix::None => 0,
        };
        for _ in 0..addr_width {
            out.write_all(b" ")?;
        }
    }

    let elem_sz = element_size(fmt);
    let fw = effective_fw;
    let num_elems = line_width / elem_sz;
    let actual_full = chunk.len() / elem_sz;
    let remainder = chunk.len() % elem_sz;

    for i in 0..num_elems {
        if i < actual_full {
            let start = i * elem_sz;
            let end = start + elem_sz;
            write_value(out, &chunk[start..end], fmt, fw, endian)?;
        } else if i == actual_full && remainder > 0 {
            let start = i * elem_sz;
            let mut padded = [0u8; 8]; // max element size is 8
            padded[..remainder].copy_from_slice(&chunk[start..]);
            write_value(out, &padded[..elem_sz], fmt, fw, endian)?;
        }
    }

    // Append printable ASCII annotation if 'z' suffix was used
    if z_annotate {
        // Pad remaining columns to align the annotation
        let used_cols = actual_full + if remainder > 0 { 1 } else { 0 };
        for _ in used_cols..num_elems {
            for _ in 0..fw {
                out.write_all(b" ")?;
            }
        }
        out.write_all(b"  >")?;
        for &b in chunk {
            if b.is_ascii_graphic() || b == b' ' {
                out.write_all(&[b])?;
            } else {
                out.write_all(b".")?;
            }
        }
        out.write_all(b"<")?;
    }

    writeln!(out)?;
    Ok(())
}

/// Parse a format type string (the TYPE argument of -t).
/// Returns the format and whether the 'z' suffix was present.
pub fn parse_format_type(s: &str) -> Result<(OutputFormat, bool), String> {
    if s.is_empty() {
        return Err("empty format string".to_string());
    }

    // Strip trailing 'z' suffix (printable ASCII annotation)
    let (s, z_annotate) = if s.len() > 1 && s.ends_with('z') {
        (&s[..s.len() - 1], true)
    } else {
        (s, false)
    };

    let mut chars = s.chars();
    let type_char = chars.next().unwrap();
    let size_str: String = chars.collect();

    let fmt = match type_char {
        'a' => Ok(OutputFormat::NamedChar),
        'c' => Ok(OutputFormat::PrintableChar),
        'd' => {
            let size = if size_str.is_empty() {
                4
            } else {
                parse_size_spec(&size_str, "d")?
            };
            Ok(OutputFormat::SignedDec(size))
        }
        'f' => {
            let size = if size_str.is_empty() {
                4
            } else {
                parse_float_size(&size_str)?
            };
            Ok(OutputFormat::Float(size))
        }
        'o' => {
            let size = if size_str.is_empty() {
                2
            } else {
                parse_size_spec(&size_str, "o")?
            };
            Ok(OutputFormat::Octal(size))
        }
        'u' => {
            let size = if size_str.is_empty() {
                4
            } else {
                parse_size_spec(&size_str, "u")?
            };
            Ok(OutputFormat::UnsignedDec(size))
        }
        'x' => {
            let size = if size_str.is_empty() {
                2
            } else {
                parse_size_spec(&size_str, "x")?
            };
            Ok(OutputFormat::Hex(size))
        }
        _ => Err(format!("invalid type string '{}'", s)),
    }?;
    Ok((fmt, z_annotate))
}

fn parse_size_spec(s: &str, type_name: &str) -> Result<usize, String> {
    // Accept C, S, I, L or a number
    match s {
        "C" => Ok(1),
        "S" => Ok(2),
        "I" => Ok(4),
        "L" => Ok(8),
        _ => {
            let n: usize = s
                .parse()
                .map_err(|_| format!("invalid type string '{}{}': invalid size", type_name, s))?;
            match n {
                1 | 2 | 4 | 8 => Ok(n),
                _ => Err(format!(
                    "invalid type string '{}{}': invalid size",
                    type_name, s
                )),
            }
        }
    }
}

fn parse_float_size(s: &str) -> Result<usize, String> {
    match s {
        "F" | "4" => Ok(4),
        "D" | "8" => Ok(8),
        "L" | "16" => Err("16-byte float not supported".to_string()),
        _ => {
            let n: usize = s
                .parse()
                .map_err(|_| format!("invalid float size '{}'", s))?;
            match n {
                4 | 8 => Ok(n),
                _ => Err(format!("invalid float size '{}'", s)),
            }
        }
    }
}

/// Process input and produce od output.
pub fn od_process<R: Read, W: Write>(
    mut input: R,
    output: &mut W,
    config: &OdConfig,
) -> io::Result<()> {
    // Skip bytes
    if config.skip_bytes > 0 {
        let mut to_skip = config.skip_bytes;
        let mut skip_buf = [0u8; 8192];
        while to_skip > 0 {
            let chunk_size = std::cmp::min(to_skip, skip_buf.len() as u64) as usize;
            let n = input.read(&mut skip_buf[..chunk_size])?;
            if n == 0 {
                break;
            }
            to_skip -= n as u64;
        }
    }

    // Read all data (respecting read_bytes limit)
    let data = match config.read_bytes {
        Some(limit) => {
            let mut buf = Vec::new();
            let mut limited = input.take(limit);
            limited.read_to_end(&mut buf)?;
            buf
        }
        None => {
            let mut buf = Vec::new();
            input.read_to_end(&mut buf)?;
            buf
        }
    };

    let width = config.width;
    let mut offset = config.skip_bytes;
    let mut prev_chunk: Option<Vec<u8>> = None;
    let mut star_printed = false;

    // Compute effective field widths for multi-format alignment
    let effective_widths = compute_effective_widths(&config.formats, width);

    let mut pos = 0;
    while pos < data.len() {
        let end = std::cmp::min(pos + width, data.len());
        let chunk = &data[pos..end];

        // Duplicate suppression
        if !config.show_duplicates && chunk.len() == width {
            if let Some(ref prev) = prev_chunk {
                if prev.as_slice() == chunk {
                    if !star_printed {
                        writeln!(output, "*")?;
                        star_printed = true;
                    }
                    pos += width;
                    offset += width as u64;
                    continue;
                }
            }
        }

        star_printed = false;

        for (i, fmt) in config.formats.iter().enumerate() {
            let z = config.z_flags.get(i).copied().unwrap_or(false);
            let ew = effective_widths[i];
            write_format_line(
                output,
                chunk,
                *fmt,
                width,
                i == 0,
                config.address_radix,
                offset,
                z,
                ew,
                config.endian,
            )?;
        }

        prev_chunk = Some(chunk.to_vec());
        pos += width;
        offset += width as u64;
    }

    // Final address line
    if config.address_radix != AddressRadix::None {
        let final_offset = config.skip_bytes + data.len() as u64;
        match config.address_radix {
            AddressRadix::Octal => writeln!(output, "{:07o}", final_offset)?,
            AddressRadix::Decimal => writeln!(output, "{:07}", final_offset)?,
            AddressRadix::Hex => writeln!(output, "{:06x}", final_offset)?,
            AddressRadix::None => {}
        }
    }

    Ok(())
}