jojodiff 0.1.2

Rust library for handling JojoDiff files, a diff utility for binary files.
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
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
use log::debug;
use std::io;
use std::io::{Read, Seek, Write};

/// 167 Escape
const ESC: u8 = 0xA7;
/// 166 Modify
const MOD: u8 = 0xA6;
/// 165 Insert
const INS: u8 = 0xA5;
/// 164 Delete
const DEL: u8 = 0xA4;
/// 163 Equal
const EQL: u8 = 0xA3;
/// 162 Backtrace
const BKT: u8 = 0xA2;

// read next operand from input
const READ_NEXT_OPERAND: u8 = 0;

fn operand_name(op: u8) -> String {
    match op {
        ESC => "ESC".into(),
        MOD => "MOD".into(),
        INS => "INS".into(),
        DEL => "DEL".into(),
        EQL => "EQL".into(),
        BKT => "BKT".into(),
        other => format!("UNKNOWN {}", other),
    }
}

type Offset = u64;
type Operand = u8;

/// Read a byte from the input.
/// return the byte read, or EOF if end of stream
/// * `read` Input reader
fn read_byte<R: Read>(read: &mut R) -> io::Result<Option<u8>> {
    let mut byte_buf = [0; 1];
    let n = read.read(&mut byte_buf)?;
    let result = if n == 0 { None } else { Some(byte_buf[0]) };
    Ok(result)
}

/// Copy a series of bytes from input to output.
/// * `source`     Input reader
/// * `dest`       Output writer
/// * `source_pos` Position to copy from
/// * `len`        Number of bytes to copy
fn copy_bytes<R: Read + Seek, W: Write>(
    source: &mut R,
    dest: &mut W,
    source_pos: u64,
    len: usize,
) -> io::Result<()> {
    let mut buf = vec![0; len];
    source.seek(std::io::SeekFrom::Start(source_pos))?;
    source.read_exact(&mut buf)?;
    dest.write_all(&buf)?;
    Ok(())
}

/// Get an offset/len from the input stream
fn get_int<T: Read>(mut input_stream: T) -> io::Result<Offset> {
    let mut buf = [0; 1];
    input_stream.read_exact(&mut buf).map_err(|e| {
        io::Error::new(e.kind(), format!("Failed to read first length byte: {}", e))
    })?;
    let first_byte = buf[0];
    if first_byte < 252 {
        Ok(first_byte as Offset + 1)
    } else if first_byte == 252 {
        let mut buf = [0; 1];
        input_stream.read_exact(&mut buf).map_err(|e| {
            io::Error::new(
                e.kind(),
                format!("Failed to read 1 byte for 253 + 8-bit length: {}", e),
            )
        })?;
        let val = 253 + u8::from_be_bytes(buf) as Offset;
        Ok(val)
    } else if first_byte == 253 {
        let mut buf = [0; 2];
        input_stream.read_exact(&mut buf).map_err(|e| {
            io::Error::new(
                e.kind(),
                format!("Failed to read 2 bytes for 16-bit length: {}", e),
            )
        })?;
        let val = u16::from_be_bytes(buf) as Offset;
        Ok(val)
    } else if first_byte == 254 {
        let mut buf = [0; 4];
        input_stream.read_exact(&mut buf).map_err(|e| {
            io::Error::new(
                e.kind(),
                format!("Failed to read 4 bytes for 32-bit length: {}", e),
            )
        })?;
        let val = u32::from_be_bytes(buf) as Offset;
        Ok(val)
    } else {
        let mut buf = [0; 8];
        input_stream.read_exact(&mut buf).map_err(|e| {
            io::Error::new(
                e.kind(),
                format!("Failed to read 8 bytes for 64-bit length: {}", e),
            )
        })?;
        let val = u64::from_be_bytes(buf) as Offset;
        Ok(val)
    }
}

/// Put one byte of output data
///
/// * `writer` output writer
/// * `data` output byte
/// * `position_original` position on source stream
/// * `position_out` position on output stream
/// * `operand` MOD or INS
/// * `offset` offset
///
/// returns 1
fn write_byte<W: Write>(
    writer: &mut W,
    byte: u8,
    position_original: Offset,
    position_out: Offset,
    operand: u8,
    offset: Offset,
) -> io::Result<Offset> {
    writer.write_all(&[byte])?;
    debug!(
        "put_data {} {} {} {} {}",
        position_original + (if operand == MOD { offset } else { 0 }),
        position_out + offset,
        operand_name(operand),
        byte,
        render_ascii(byte)
    );
    Ok(1)
}

fn render_ascii(data: u8) -> char {
    if (32..=127).contains(&data) {
        data as char
    } else {
        ' '
    }
}

/// Read a data sequence (INS or MOD)
///
/// * `patch_reader`         patch stream
/// * `out_stream`           output stream
/// * `position_original`    position on source stream
/// * `position_out`         position on output stream
/// * `operand`              INS or MOD
/// * `pending`              First pending byte (EOF = no pending byte)
/// * `dbl`                  Second pending byte (EOF = no pending byte)
///
/// returns the next operand and the bytes read
fn copy_data<R: Read, W: Write>(
    patch_reader: &mut R,
    out_writer: &mut W,
    position_original: Offset,
    position_out: Offset,
    operand: Operand,
    pending: Option<u8>,
    pending2: Option<u8>,
) -> io::Result<(Option<Operand>, Offset)> {
    let mut bytes_read: Offset = 0;

    /* First, output the pending bytes:
       pending  pending2     Output
        ESC      ESC          ESC ESC
        ESC      xxx          ESC xxx
        xxx      EOF          xxx
    */
    if let Some(pending_byte) = pending {
        bytes_read += write_byte(
            out_writer,
            pending_byte,
            position_original,
            position_out,
            operand,
            bytes_read,
        )?;
        if pending_byte == ESC {
            if let Some(pending_byte2) = pending2 {
                bytes_read += write_byte(
                    out_writer,
                    pending_byte2,
                    position_original,
                    position_out,
                    operand,
                    bytes_read,
                )?;
            }
        }
    }

    /* Read loop */
    while let Some(mut input) = read_byte(patch_reader)? {
        // Handle ESC-code
        if input == ESC {
            if let Some(next_operand) = read_byte(patch_reader)? {
                match next_operand {
                    DEL | EQL | BKT | MOD | INS => (),
                    ESC => {
                        // Double ESC: drop one
                        debug!(
                            "{} {} ESC ESC",
                            position_original + (if operand == MOD { bytes_read } else { 0 }),
                            position_out + bytes_read
                        );

                        // Write the single ESC and continue
                        bytes_read += write_byte(
                            out_writer,
                            input,
                            position_original,
                            position_out,
                            operand,
                            bytes_read,
                        )?;
                        continue;
                    }
                    _ => {
                        // ESC <xxx> with <xxx> not an opcode: output as they are
                        debug!(
                            "{} {} ESC XXX",
                            position_original + (if operand == MOD { bytes_read } else { 0 }),
                            position_out + bytes_read
                        );

                        // Write the escape, the <xxx> and continue
                        bytes_read += write_byte(
                            out_writer,
                            input,
                            position_original,
                            position_out,
                            operand,
                            bytes_read,
                        )?;
                        bytes_read += write_byte(
                            out_writer,
                            next_operand,
                            position_original,
                            position_out,
                            operand,
                            bytes_read,
                        )?;
                        continue;
                    }
                }
                if next_operand == operand {
                    // <ESC> MOD within an <ESC> MOD is meaningless: handle as data
                    // <ESC> INS within an <ESC> INS is meaningless: handle as data
                    debug!(
                        "{} {} ESC {}",
                        position_original + (if operand == MOD { bytes_read } else { 0 }),
                        position_out + bytes_read,
                        next_operand
                    );

                    bytes_read += write_byte(
                        out_writer,
                        ESC,
                        position_original,
                        position_out,
                        operand,
                        bytes_read,
                    )?;
                    input = next_operand; // will be output below
                } else {
                    return Ok((Some(next_operand), bytes_read));
                }
            } else {
                return Err(io::Error::new(
                    io::ErrorKind::UnexpectedEof,
                    "ESC at end of stream",
                ));
            }
        }

        // Handle data
        bytes_read += write_byte(
            out_writer,
            input,
            position_original,
            position_out,
            operand,
            bytes_read,
        )?;
    } /* while ! EOF */

    Ok((None, bytes_read))
}

/// Patch function
///
/// * `in_reader`  Input reader
/// * `patch_reader` Patch reader
/// * `out_writer` Output writer
///
/// *Note: we suggest using `BufReader` and `BufWriter` to improve performance*
///
pub fn patch<R: Read + Seek, W: Write>(
    in_reader: &mut R,
    patch_reader: &mut R,
    out_writer: &mut W,
) -> io::Result<()> {
    // Position in source stream
    let mut position_original: Offset = 0;
    // Position in destination stream
    let mut position_out: Offset = 0;

    // Current operand
    let mut operand = READ_NEXT_OPERAND; // no operand
    loop {
        // 1st Pending byte
        let pending1: Option<u8>;
        // 2nd Pending byte
        let pending2: Option<u8>;
        // Read operand from input, unless this has already been done
        if operand == READ_NEXT_OPERAND {
            let next_byte = if let Some(byte) = read_byte(patch_reader)? {
                byte
            } else {
                // end of stream
                break;
            };

            // Handle ESC <opr>
            if next_byte == ESC {
                let escaped_byte = if let Some(byte) = read_byte(patch_reader)? {
                    byte
                } else {
                    return Err(io::Error::new(
                        io::ErrorKind::UnexpectedEof,
                        "ESC at end of stream",
                    ));
                };

                match escaped_byte {
                    EQL | DEL | BKT | MOD | INS => {
                        // new operand found, all ok !
                        operand = escaped_byte;
                        pending1 = None;
                        pending2 = None;
                    }
                    _ => {
                        // ESC xxx or ESC ESC at the start of a sequence
                        // Resolve by double pending bytes
                        operand = MOD;
                        pending1 = Some(ESC);
                        pending2 = Some(escaped_byte);
                    }
                }
            } else {
                operand = MOD; // If an ESC <opr> is missing, set default operand (gaining two bytes)
                pending1 = Some(next_byte);
                pending2 = None;
            }
        } else {
            // There is no lookahead so checking if we should stop copying data involves reading the
            // next operand. This is done by the copy_data function used by the MOD and INS operands.
            pending1 = None;
            pending2 = None;
        }

        match operand {
            MOD => {
                // insert data from the patch file into the output file while also advancing the source file
                let (next_operand, bytes_read) = copy_data(
                    patch_reader,
                    out_writer,
                    position_original,
                    position_out,
                    operand,
                    pending1,
                    pending2,
                )?;
                debug!(
                    "{} {} MOD {} byte(s)",
                    position_original, position_out, bytes_read
                );
                position_original += bytes_read;
                position_out += bytes_read;
                if let Some(o) = next_operand {
                    operand = o;
                } else {
                    // end of stream
                    break;
                }
            }
            INS => {
                // insert data from the patch file at the current position in the output file
                let (next_operand, bytes_read) = copy_data(
                    patch_reader,
                    out_writer,
                    position_original,
                    position_out,
                    operand,
                    pending1,
                    pending2,
                )?;
                debug!(
                    "{} {} INS {} byte(s)",
                    position_original, position_out, bytes_read
                );
                position_out += bytes_read;
                if let Some(o) = next_operand {
                    operand = o;
                } else {
                    // end of stream
                    break;
                }
            }
            DEL => {
                // skip the next <len> bytes in the source file
                let len = get_int(&mut *patch_reader)?;
                debug!("{} {} DEL {} byte(s)", position_original, position_out, len);
                position_original += len;
                operand = READ_NEXT_OPERAND;
            }
            EQL => {
                /* copy the next <len> bytes from the source file to the output file */
                let len = get_int(&mut *patch_reader)?;
                debug!("{} {} EQL {} byte(s)", position_original, position_out, len);
                copy_bytes(in_reader, out_writer, position_original, len as usize)?;
                position_original += len;
                position_out += len;
                operand = READ_NEXT_OPERAND;
            }
            BKT => {
                // go back <offset> bytes in the source file
                let offset = get_int(&mut *patch_reader)?;
                debug!("{} {} BKT {}", position_original, position_out, offset);
                position_original -= offset;
                operand = READ_NEXT_OPERAND;
            }
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Unknown operand {}", operand),
                ));
            }
        }
    }
    out_writer.flush()?;
    debug!("{} {} EOF", position_original, position_out);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use std::sync::Once;

    static INIT: Once = Once::new();

    /// Setup function that is only run once, even if called multiple times.
    fn setup() {
        INIT.call_once(|| {
            env_logger::init();
        });
    }

    #[test]
    fn get_int_1_byte() -> io::Result<()> {
        let mut cursor = io::Cursor::new(vec![1]);
        let result = get_int(&mut cursor)?;
        Ok(assert_eq!(result, 2))
    }

    #[test]
    fn get_int_2_bytes() -> io::Result<()> {
        let mut cursor = io::Cursor::new(vec![252, 1]);
        let result = get_int(&mut cursor)?;
        Ok(assert_eq!(result, 254))
    }

    #[test]
    fn get_int_3_bytes() -> io::Result<()> {
        let mut cursor = io::Cursor::new(vec![253, 1, 2]);
        let result = get_int(&mut cursor)?;
        Ok(assert_eq!(result, 258))
    }

    #[test]
    fn get_int_4_bytes() -> io::Result<()> {
        let mut cursor = io::Cursor::new(vec![254, 1, 0, 0, 0]);
        let result = get_int(&mut cursor)?;
        Ok(assert_eq!(result, 1 << 24))
    }

    #[test]
    fn get_int_8_bytes() -> io::Result<()> {
        let mut cursor = io::Cursor::new(vec![255, 1, 0, 0, 0, 0, 0, 0, 0]);
        let result = get_int(&mut cursor)?;
        Ok(assert_eq!(result, 1 << 56))
    }

    #[test]
    fn get_int_8_bytes_missing() -> io::Result<()> {
        let mut cursor = io::Cursor::new(vec![255, 1, 0]);
        let result = get_int(&mut cursor);
        Ok(assert_eq!(
            result.unwrap_err().to_string(),
            "Failed to read 8 bytes for 64-bit length: failed to fill whole buffer"
        ))
    }

    #[test]
    fn patch_empty_file_and_empty_patch() -> io::Result<()> {
        setup();
        let mut empty_input_stream = io::Cursor::new(vec![]);
        let mut empty_patch_stream = io::Cursor::new(vec![]);
        let mut empty_output_stream = io::Cursor::new(vec![]);

        patch(
            &mut empty_input_stream,
            &mut empty_patch_stream,
            &mut empty_output_stream,
        )?;

        let out_len = empty_output_stream.get_ref().len();
        Ok(assert_eq!(out_len, 0))
    }

    #[test]
    fn patch_trailing_esc() {
        setup();
        let in_data = vec![];
        let patch_data = vec![ESC];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        let result = patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor);

        assert_eq!(result.is_err(), true);
        assert_eq!(result.unwrap_err().to_string(), "ESC at end of stream");
    }

    #[test]
    fn patch_trailing_esc_with_body() {
        setup();
        let in_data = vec![];
        let patch_data = vec![1, 2, 3, ESC];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        let result = patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor);

        assert_eq!(result.is_err(), true);
        assert_eq!(result.unwrap_err().to_string(), "ESC at end of stream");
    }

    #[test]
    fn patch_delete_1_byte() -> io::Result<()> {
        setup();
        let in_data = vec![1];
        let patch_data = vec![ESC, DEL, 0];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data: [u8; 0] = [];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_identical_copy() -> io::Result<()> {
        setup();
        let in_data = vec![1, 2, 3];
        let patch_data = vec![ESC, EQL, 2];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = [1, 2, 3];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_copy_byte_3_times() -> io::Result<()> {
        setup();
        let in_data = vec![1];
        #[rustfmt::skip]
        let patch_data = vec![
            ESC, EQL, 0,
            ESC, BKT, 0,
            ESC, EQL, 0,
            ESC, BKT, 0,
            ESC, EQL, 0,
        ];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = [1, 1, 1];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_copy_seq_3_times() -> io::Result<()> {
        setup();
        let in_data = vec![1, 2, 3];
        #[rustfmt::skip]
        let patch_data = vec![
            ESC, EQL, 2,
            ESC, BKT, 2,
            ESC, EQL, 2,
            ESC, BKT, 2,
            ESC, EQL, 2,
        ];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = [1, 2, 3, 1, 2, 3, 1, 2, 3];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_insert_into_empty_file_including_escaped_value() -> io::Result<()> {
        setup();
        let in_data = vec![];
        #[rustfmt::skip]
    let patch_data = vec![
        ESC, INS, 1, 2, ESC, ESC, 3
    ];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = [1, 2, ESC, 3];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_overwrite_middle_part() -> io::Result<()> {
        setup();
        let in_data = vec![1, 2, 3, 4, 5, 6, 7];
        #[rustfmt::skip]
        let patch_data = vec![
            ESC, EQL, 1,
            ESC, MOD, 0, 0, 0,
            ESC, EQL, 1,
        ];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = [1, 2, 0, 0, 0, 6, 7];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_mod_ins() -> io::Result<()> {
        // this is a special case where MOD follows INS or the other way round
        setup();
        let in_data = vec![1, 2, 3];
        #[rustfmt::skip]
        let patch_data = vec![
            ESC, MOD, 10,
            ESC, INS, 11,
            ESC, MOD, 12,
            ESC, INS, 13,
            ESC, MOD, 14,
        ];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = [10, 11, 12, 13, 14];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_apply_mod_when_when_operand_missing() -> io::Result<()> {
        setup();
        let in_data = vec![1, 2, 3];
        #[rustfmt::skip]
        let patch_data = vec![
            3,2,1
        ];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = [3, 2, 1];
        Ok(assert_eq!(out_data, expected_data))
    }

    #[test]
    fn patch_ascii() -> io::Result<()> {
        setup();
        let in_data = b"Hello, World!".to_vec();
        // replace World with Universe
        #[rustfmt::skip]
        let patch_data = vec![
            ESC, EQL, 6,
            ESC, MOD, b'U', b'n', b'i', b'v', b'e',
            ESC, INS, b'r', b's', b'e',
            ESC, EQL, 0,
        ];
        let out_data = vec![];

        let mut in_cursor = io::Cursor::new(in_data);
        let mut patch_cursor = io::Cursor::new(patch_data);
        let mut out_cursor = io::Cursor::new(out_data);

        patch(&mut in_cursor, &mut patch_cursor, &mut out_cursor)?;

        let out_data = out_cursor.into_inner();
        let expected_data = "Hello, Universe!";
        Ok(assert_eq!(
            String::from_utf8_lossy(&out_data),
            expected_data
        ))
    }
}