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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
//! JEDEC programming file format parser and writer

#![no_std]
extern crate alloc;

use bitvec::prelude::*;

use alloc::borrow::Cow;
use alloc::vec;
use alloc::vec::Vec;
use core::fmt;
use core::num;
use core::num::Wrapping;
use core::str;

#[cfg(feature = "std")]
extern crate std;

/// Errors that can occur when parsing a .jed file
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum JedParserError {
    /// No STX byte found
    MissingSTX,
    /// No ETX byte found
    MissingETX,
    /// A field contains a character not appropriate for that field
    /// (e.g. non-ASCII character, non-hex digit in a hex field)
    InvalidCharacter,
    /// An unexpected end of file was encountered in the file checksum
    UnexpectedEnd,
    /// The file checksum was nonzero and incorrect
    BadFileChecksum,
    /// The fuse checksum (`C` command) was incorrect
    BadFuseChecksum,
    /// A `L` field index was out of range
    InvalidFuseIndex,
    /// There was no `QF` field
    MissingQF,
    /// There was no `F` field, but not all fuses had a value specified
    MissingF,
}

#[cfg(feature = "std")]
impl std::error::Error for JedParserError {}

impl fmt::Display for JedParserError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &JedParserError::MissingSTX => write!(f, "STX not found"),
            &JedParserError::MissingETX => write!(f, "ETX not found"),
            &JedParserError::InvalidCharacter => write!(f, "invalid character in field"),
            &JedParserError::UnexpectedEnd => write!(f, "unexpected end of file"),
            &JedParserError::BadFileChecksum => write!(f, "invalid file checksum"),
            &JedParserError::BadFuseChecksum => write!(f, "invalid fuse checksum"),
            &JedParserError::InvalidFuseIndex => write!(f, "invalid fuse index value"),
            &JedParserError::MissingQF => write!(f, "missing QF field"),
            &JedParserError::MissingF => write!(f, "missing F field"),
        }
    }
}

impl From<str::Utf8Error> for JedParserError {
    fn from(_: str::Utf8Error) -> Self {
        JedParserError::InvalidCharacter
    }
}

impl From<num::ParseIntError> for JedParserError {
    fn from(_: num::ParseIntError) -> Self {
        JedParserError::InvalidCharacter
    }
}

const STX: u8 = 0x02;
const ETX: u8 = 0x03;

/// Struct representing quirk options
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
pub struct Quirks {
    no_design_spec: bool,
}

impl Quirks {
    /// Construct a new object with default quirk settings
    pub fn new() -> Self {
        Self {
            no_design_spec: false,
        }
    }

    /// Xilinx is known to output JEDEC files that do not contain a
    /// design specification block
    pub fn no_design_spec(mut self, no_design_spec: bool) -> Self {
        self.no_design_spec = no_design_spec;
        self
    }
}

impl Default for Quirks {
    fn default() -> Self {
        Self::new()
    }
}

/// Struct representing a JEDEC programming file
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct JEDECFile<'a> {
    /// Fuse array
    pub f: BitVec,
    /// Data that appears before the STX byte
    pub header: Cow<'a, [u8]>,
    /// Data that appears after the ETX byte
    pub footer: Cow<'a, [u8]>,
    /// Design specification field
    pub design_spec: Cow<'a, [u8]>,
    /// Parsed N (Note) commands. This does _not_ round-trip.
    /// When writing, all notes will be written before all fuses
    pub notes: Vec<Cow<'a, [u8]>>,
    /// Security `G` fuse
    pub secure_fuse: Option<bool>,
}

fn trim_slice_start(mut in_: &[u8]) -> &[u8] {
    while in_.len() > 0 {
        match in_[0] {
            b' ' | b'\r' | b'\n' => {
                in_ = &in_[1..];
            }
            _ => {
                break;
            }
        }
    }
    in_
}

fn trim_slice_end(mut in_: &[u8]) -> &[u8] {
    while in_.len() > 0 {
        match in_[in_.len() - 1] {
            b' ' | b'\r' | b'\n' => {
                in_ = &in_[..in_.len() - 1];
            }
            _ => {
                break;
            }
        }
    }
    in_
}

fn trim_slice(in_: &[u8]) -> &[u8] {
    trim_slice_end(trim_slice_start(in_))
}

const fn width_calc(len: usize) -> usize {
    // needed because no_std has no log10
    match len {
        0..=9 => 1,
        10..=99 => 2,
        100..=999 => 3,
        1000..=9999 => 4,
        10000..=99999 => 5,
        100000..=999999 => 6,
        1000000..=9999999 => 7,
        10000000..=99999999 => 8,
        100000000..=999999999 => 9,
        1000000000..=9999999999 => 10,
        10000000000..=99999999999 => 11,
        100000000000..=999999999999 => 12,
        1000000000000..=9999999999999 => 13,
        10000000000000..=99999999999999 => 14,
        100000000000000..=999999999999999 => 15,
        1000000000000000..=9999999999999999 => 16,
        10000000000000000..=99999999999999999 => 17,
        100000000000000000..=999999999999999999 => 18,
        1000000000000000000..=9999999999999999999 => 19,
        10000000000000000000..=18446744073709551615 => 20,
        _ => unreachable!(),
    }
}

impl<'a> JEDECFile<'a> {
    /// Reads a .jed file
    pub fn from_bytes(in_bytes: &'a [u8], quirks: &Quirks) -> Result<Self, JedParserError> {
        // Find STX
        let jed_stx = in_bytes
            .iter()
            .position(|&x| x == STX)
            .ok_or(JedParserError::MissingSTX)?;

        // Checksum and find ETX
        let mut file_csum = Wrapping(0u16);
        let jed_etx = in_bytes
            .iter()
            .position(|&x| {
                file_csum += Wrapping(x as u16);
                x == ETX
            })
            .ok_or(JedParserError::MissingETX)?;

        // Add the ETX to the checksum too
        file_csum += Wrapping(ETX as u16);

        // Check the checksum
        if jed_etx + 4 >= in_bytes.len() {
            return Err(JedParserError::UnexpectedEnd);
        }
        let csum_expected = &in_bytes[jed_etx + 1..jed_etx + 5];
        let csum_expected = str::from_utf8(csum_expected)?;
        let csum_expected = u16::from_str_radix(csum_expected, 16)?;
        if csum_expected != 0 && csum_expected != file_csum.0 {
            return Err(JedParserError::BadFileChecksum);
        }

        // slice out header/footer/body
        let header = &in_bytes[..jed_stx];
        let jed_body = &in_bytes[jed_stx + 1..jed_etx];
        let footer = &in_bytes[jed_etx + 5..];

        // state
        let mut fuse_expected_csum = None;
        let mut num_fuses = 0;
        let mut fuses = bitvec![];
        let mut fuses_written = bitvec![];
        let mut default_fuse = None;
        let mut vecs_alloced = false;

        // storage for various fields that get captured
        let mut notes = Vec::new();
        let mut secure_fuse = None;

        // Ready to parse each line
        let mut jed_fields = jed_body.split(|&c| c == b'*');

        let design_spec = if quirks.no_design_spec {
            &[]
        } else {
            jed_fields.next().unwrap_or(&[])
        };

        for l in jed_fields {
            let l = trim_slice_start(l);
            if l.len() == 0 {
                // FIXME: Should we do something else here?
                // ignore empty fields
                continue;
            }

            // Now we can look at the first byte to figure out what we have
            match l[0] {
                b'F' => {
                    // Default state
                    let default_state_str = trim_slice(&l[1..]);
                    default_fuse = Some(match default_state_str {
                        b"0" => false,
                        b"1" => true,
                        _ => return Err(JedParserError::InvalidCharacter),
                    });
                }
                b'G' => {
                    // Secure fuse
                    let secure_fuse_str = trim_slice(&l[1..]);
                    secure_fuse = Some(match secure_fuse_str {
                        b"0" => false,
                        b"1" => true,
                        _ => return Err(JedParserError::InvalidCharacter),
                    });
                }
                b'N' => {
                    // Notes
                    notes.push(Cow::Borrowed(&l[1..]));
                }
                b'Q' => {
                    // Look for QF
                    if &l[0..2] == b"QF" {
                        let num_fuses_str = trim_slice(&l[2..]);
                        let num_fuses_str = str::from_utf8(num_fuses_str)?;
                        num_fuses = usize::from_str_radix(num_fuses_str, 10)?;
                    }
                }
                b'L' => {
                    // A set of fuses
                    if num_fuses == 0 {
                        return Err(JedParserError::MissingQF);
                    }

                    if !vecs_alloced {
                        if let Some(default_fuse) = default_fuse {
                            fuses = BitVec::repeat(default_fuse, num_fuses);
                        } else {
                            fuses = BitVec::repeat(false, num_fuses);
                            fuses_written = BitVec::repeat(false, num_fuses);
                        }
                        vecs_alloced = true;
                    }

                    // l is something like "L012345 1010101"
                    let fuse_data = trim_slice(&l[1..]);

                    let mut fuse_split =
                        fuse_data.splitn(2, |&c| c == b' ' || c == b'\r' || c == b'\n');

                    let fuse_idx_str = fuse_split.next().unwrap();
                    let fuse_idx_str = str::from_utf8(fuse_idx_str)?;
                    let mut fuse_idx = usize::from_str_radix(fuse_idx_str, 10)?;

                    let fuse_bits = fuse_split.next().ok_or(JedParserError::InvalidFuseIndex)?;
                    for fusec in fuse_bits {
                        match &fusec {
                            b'0' => {
                                if fuse_idx >= num_fuses {
                                    return Err(JedParserError::InvalidFuseIndex);
                                }
                                fuses.set(fuse_idx, false);
                                if default_fuse.is_none() {
                                    fuses_written.set(fuse_idx, true);
                                }
                                fuse_idx += 1;
                            }
                            b'1' => {
                                if fuse_idx >= num_fuses {
                                    return Err(JedParserError::InvalidFuseIndex);
                                }
                                fuses.set(fuse_idx, true);
                                if default_fuse.is_none() {
                                    fuses_written.set(fuse_idx, true);
                                }
                                fuse_idx += 1;
                            }
                            b' ' | b'\r' | b'\n' => {} // Do nothing
                            _ => return Err(JedParserError::InvalidCharacter),
                        }
                    }
                }
                b'C' => {
                    // Checksum
                    let csum_str = trim_slice(&l[1..]);
                    if csum_str.len() != 4 {
                        return Err(JedParserError::BadFuseChecksum);
                    }
                    let csum_str = str::from_utf8(csum_str)?;
                    fuse_expected_csum = Some(u16::from_str_radix(csum_str, 16)?);
                }
                _ => {
                    // ignore unsupported fields
                }
            }
        }

        // Check to make sure all fuses were written
        if !vecs_alloced && num_fuses > 0 {
            return Err(JedParserError::MissingF);
        }
        if default_fuse.is_none() {
            if !fuses_written.all() {
                return Err(JedParserError::MissingF);
            }
        }

        // Fuse checksum
        let mut fuse_csum = Wrapping(0u16);
        if let Some(fuse_expected_csum) = fuse_expected_csum {
            for i in 0..num_fuses {
                if fuses[i as usize] {
                    // Fuse is a 1 and contributes to the sum
                    fuse_csum += Wrapping(1u16 << (i % 8));
                }
            }

            if fuse_expected_csum != fuse_csum.0 {
                return Err(JedParserError::BadFuseChecksum);
            }
        }

        Ok(Self {
            f: fuses,
            header: Cow::Borrowed(header),
            footer: Cow::Borrowed(footer),
            design_spec: Cow::Borrowed(design_spec),
            notes,
            secure_fuse,
        })
    }

    /// Writes the contents to a JEDEC file. Linebreaks will be inserted at
    /// the fuse indices returned by the `linebreaks` iterator.
    pub fn write_fmt_custom_linebreaks<W, I>(
        &self,
        mut writer: W,
        quirks: &Quirks,
        linebreaks: I,
    ) -> fmt::Result
    where
        W: fmt::Write,
        I: Iterator<Item = usize>,
    {
        // FIXME: The number of 0s in the fuse index isn't minimal because of linebreaks
        // FIXME: utf8 handing

        write!(writer, "{}", str::from_utf8(&self.header).unwrap())?;
        write!(writer, "\x02")?;

        if !quirks.no_design_spec {
            write!(writer, "{}*\n", str::from_utf8(&self.design_spec).unwrap())?;
        }

        if let Some(secure_fuse) = self.secure_fuse {
            write!(writer, "G{}*\n", if secure_fuse { "1" } else { "0" })?;
        }
        write!(writer, "QF{}*\n", self.f.len())?;
        write!(writer, "\n")?;

        for note in &self.notes {
            write!(writer, "N{}*\n", str::from_utf8(note).unwrap())?;
        }
        if self.notes.len() > 0 {
            write!(writer, "\n")?;
        }

        let fuse_idx_width = width_calc(self.f.len());

        let mut next_written_fuse = 0;
        for linebreak in linebreaks {
            // Write one line
            if next_written_fuse == linebreak {
                // One or more duplicate breaks.
                write!(writer, "\n")?;
            } else {
                write!(
                    writer,
                    "L{:0width$} ",
                    next_written_fuse,
                    width = fuse_idx_width
                )?;
                for i in next_written_fuse..linebreak {
                    write!(writer, "{}", if self.f[i] { "1" } else { "0" })?;
                }
                write!(writer, "*\n")?;
                next_written_fuse = linebreak;
            }
        }

        // Last chunk
        if next_written_fuse < self.f.len() {
            write!(
                writer,
                "L{:0width$} ",
                next_written_fuse,
                width = fuse_idx_width
            )?;
            for i in next_written_fuse..self.f.len() {
                write!(writer, "{}", if self.f[i] { "1" } else { "0" })?;
            }
            write!(writer, "*\n")?;
        }

        write!(writer, "\x030000\n")?;
        write!(writer, "{}", str::from_utf8(&self.footer).unwrap())?;

        Ok(())
    }

    /// Writes the contents to a JEDEC file. Fuses will be broken up into
    /// lines of `break_interval` fuses each.
    pub fn write_fmt_with_linebreaks<W>(
        &self,
        writer: W,
        quirks: &Quirks,
        break_inverval: usize,
    ) -> fmt::Result
    where
        W: fmt::Write,
    {
        self.write_fmt_custom_linebreaks(
            writer,
            quirks,
            (0..self.f.len()).step_by(break_inverval).skip(1),
        )
    }

    /// Writes the contents to a JEDEC file. Fuses will be broken up into
    /// lines of 16 fuses each.
    pub fn write_fmt<W>(&self, writer: W, quirks: &Quirks) -> fmt::Result
    where
        W: fmt::Write,
    {
        self.write_fmt_with_linebreaks(writer, quirks, 16)
    }

    // FIXME: code duplication
    /// Writes the contents to a JEDEC file. Linebreaks will be inserted at
    /// the fuse indices returned by the `linebreaks` iterator.
    #[cfg(feature = "std")]
    pub fn write_io_custom_linebreaks<W, I>(
        &self,
        mut writer: W,
        quirks: &Quirks,
        linebreaks: I,
    ) -> std::io::Result<()>
    where
        W: std::io::Write,
        I: Iterator<Item = usize>,
    {
        // FIXME: The number of 0s in the fuse index isn't minimal because of linebreaks

        writer.write(&self.header)?;
        write!(writer, "\x02")?;

        if !quirks.no_design_spec {
            writer.write(&self.design_spec)?;
            write!(writer, "*\n")?;
        }

        if let Some(secure_fuse) = self.secure_fuse {
            write!(writer, "G{}*\n", if secure_fuse { "1" } else { "0" })?;
        }
        write!(writer, "QF{}*\n", self.f.len())?;
        write!(writer, "\n")?;

        for note in &self.notes {
            write!(writer, "N")?;
            writer.write(note)?;
            write!(writer, "*\n")?;
        }
        if self.notes.len() > 0 {
            write!(writer, "\n")?;
        }

        let fuse_idx_width = width_calc(self.f.len());

        let mut next_written_fuse = 0;
        for linebreak in linebreaks {
            // Write one line
            if next_written_fuse == linebreak {
                // One or more duplicate breaks.
                write!(writer, "\n")?;
            } else {
                write!(
                    writer,
                    "L{:0width$} ",
                    next_written_fuse,
                    width = fuse_idx_width
                )?;
                for i in next_written_fuse..linebreak {
                    write!(writer, "{}", if self.f[i] { "1" } else { "0" })?;
                }
                write!(writer, "*\n")?;
                next_written_fuse = linebreak;
            }
        }

        // Last chunk
        if next_written_fuse < self.f.len() {
            write!(
                writer,
                "L{:0width$} ",
                next_written_fuse,
                width = fuse_idx_width
            )?;
            for i in next_written_fuse..self.f.len() {
                write!(writer, "{}", if self.f[i] { "1" } else { "0" })?;
            }
            write!(writer, "*\n")?;
        }

        write!(writer, "\x030000\n")?;
        writer.write(&self.footer)?;

        Ok(())
    }

    /// Writes the contents to a JEDEC file. Fuses will be broken up into
    /// lines of `break_interval` fuses each.
    #[cfg(feature = "std")]
    pub fn write_io_with_linebreaks<W>(
        &self,
        writer: W,
        quirks: &Quirks,
        break_inverval: usize,
    ) -> std::io::Result<()>
    where
        W: std::io::Write,
    {
        self.write_io_custom_linebreaks(
            writer,
            quirks,
            (0..self.f.len()).step_by(break_inverval).skip(1),
        )
    }

    /// Writes the contents to a JEDEC file. Fuses will be broken up into
    /// lines of 16 fuses each.
    #[cfg(feature = "std")]
    pub fn write_io<W>(&self, writer: W, quirks: &Quirks) -> std::io::Result<()>
    where
        W: std::io::Write,
    {
        self.write_io_with_linebreaks(writer, quirks, 16)
    }

    /// Constructs a fuse array with the given number of fuses
    pub fn new(size: usize) -> Self {
        let f = BitVec::repeat(false, size);

        Self {
            f,
            header: vec![].into(),
            footer: vec![].into(),
            design_spec: vec![].into(),
            notes: vec![],
            secure_fuse: None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::string::String;

    #[test]
    fn read_no_stx() {
        let ret = JEDECFile::from_bytes(b"asdf", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::MissingSTX));
    }

    #[test]
    fn read_no_etx() {
        let ret = JEDECFile::from_bytes(b"asdf\x02fdsa", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::MissingETX));
    }

    #[test]
    fn read_no_csum() {
        let ret = JEDECFile::from_bytes(b"asdf\x02fdsa\x03", &Quirks::new());
        assert_eq!(ret, Err(JedParserError::UnexpectedEnd));

        let ret = JEDECFile::from_bytes(b"asdf\x02fdsa\x03AAA", &Quirks::new());
        assert_eq!(ret, Err(JedParserError::UnexpectedEnd));
    }

    #[test]
    fn read_bad_csum() {
        let ret = JEDECFile::from_bytes(b"asdf\x02fdsa\x03AAAA", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::BadFileChecksum));
    }

    #[test]
    fn read_malformed_csum() {
        let ret = JEDECFile::from_bytes(b"asdf\x02fdsa\x03AAAZ", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::InvalidCharacter));
    }

    #[test]
    fn read_no_f() {
        let ret = JEDECFile::from_bytes(b"\x02*QF1*\x030000", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::MissingF));
    }

    #[test]
    fn read_empty_no_fuses() {
        let ret = JEDECFile::from_bytes(b"\x02\x030000", &Quirks::new()).unwrap();

        assert_eq!(ret.f, bitvec![]);
    }

    #[test]
    fn read_header_trailer() {
        let ret = JEDECFile::from_bytes(b"asdf\nfdsa\x02\x030000zzzzzz", &Quirks::new()).unwrap();

        assert_eq!(ret.header, b"asdf\nfdsa".as_slice());
        assert_eq!(ret.footer, b"zzzzzz".as_slice());
    }

    #[test]
    fn read_design_spec() {
        let ret = JEDECFile::from_bytes(b"\x02hello world!\n*\x030000", &Quirks::new()).unwrap();

        assert_eq!(ret.design_spec, b"hello world!\n".as_slice());
    }

    #[test]
    fn read_bogus_f_command() {
        let ret = JEDECFile::from_bytes(b"\x02*F2*\x030000", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::InvalidCharacter));
    }

    #[test]
    fn read_notes() {
        let ret = JEDECFile::from_bytes(
            b"\x02*Note test1*  N DEVICE asdf*  Note 3 \r  \x030000",
            &Quirks::new(),
        )
        .unwrap();

        assert_eq!(
            ret.notes,
            vec![b"ote test1".as_slice(), b" DEVICE asdf", b"ote 3 \r  "]
        );
    }

    #[test]
    fn read_l_without_qf() {
        let ret = JEDECFile::from_bytes(b"\x02*L0 0*\x030000", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::MissingQF));
    }

    #[test]
    fn read_one_fuse() {
        let ret = JEDECFile::from_bytes(b"\x02*QF1*L0 1*\x030000", &Quirks::new()).unwrap();

        assert_eq!(ret.f, bitvec![1]);
    }

    #[test]
    fn read_no_spec_quirk() {
        let ret = JEDECFile::from_bytes(
            b"\x02QF69420*QF1*L0 1*\x030000",
            &Quirks::new().no_design_spec(true),
        )
        .unwrap();

        assert_eq!(ret.f, bitvec![1]);
    }

    #[test]
    fn read_one_fuse_csum_good() {
        let ret = JEDECFile::from_bytes(b"\x02*QF1*L0 1*C0001*\x030000", &Quirks::new()).unwrap();

        assert_eq!(ret.f, bitvec![1]);
    }

    #[test]
    fn read_one_fuse_csum_bad() {
        let ret = JEDECFile::from_bytes(b"\x02*QF1*L0 1*C0002*\x030000", &Quirks::new());

        assert_eq!(ret, Err(JedParserError::BadFuseChecksum));
    }

    #[test]
    fn read_two_fuses_space() {
        let ret = JEDECFile::from_bytes(b"\x02*QF2*L 0 0 1*\x030000", &Quirks::new()).unwrap();

        assert_eq!(ret.f, bitvec![0, 1]);
    }

    #[test]
    fn read_secure_fuse() {
        let ret = JEDECFile::from_bytes(b"\x02*\x030000", &Quirks::new()).unwrap();
        assert_eq!(ret.secure_fuse, None);
        let ret = JEDECFile::from_bytes(b"\x02*G0*\x030000", &Quirks::new()).unwrap();
        assert_eq!(ret.secure_fuse, Some(false));
        let ret = JEDECFile::from_bytes(b"\x02*G1*\x030000", &Quirks::new()).unwrap();
        assert_eq!(ret.secure_fuse, Some(true));
    }

    #[test]
    fn write_empty() {
        let x = JEDECFile::new(0);

        let mut out = String::new();
        x.write_fmt(&mut out, &Quirks::new()).unwrap();

        assert_eq!(
            out,
            "\x02*
QF0*

\x030000
"
        );
    }

    #[test]
    fn write_nine_fuses() {
        let mut x = JEDECFile::new(9);
        x.f.set(1, true);
        x.f.set(8, true);

        let mut out = String::new();
        x.write_fmt(&mut out, &Quirks::new()).unwrap();

        assert_eq!(
            out,
            "\x02*
QF9*

L0 010000001*
\x030000
"
        );
    }

    #[test]
    fn write_nine_fuses_linebreaks() {
        let mut x = JEDECFile::new(9);
        x.f.set(1, true);
        x.f.set(8, true);

        let mut out = String::new();
        x.write_fmt_with_linebreaks(&mut out, &Quirks::new(), 4)
            .unwrap();

        assert_eq!(
            out,
            "\x02*
QF9*

L0 0100*
L4 0000*
L8 1*
\x030000
"
        );
    }

    #[test]
    fn write_nine_fuses_linebreaks2() {
        let mut x = JEDECFile::new(9);
        x.f.set(1, true);
        x.f.set(8, true);

        let mut out = String::new();
        x.write_fmt_with_linebreaks(&mut out, &Quirks::new(), 3)
            .unwrap();

        assert_eq!(
            out,
            "\x02*
QF9*

L0 010*
L3 000*
L6 001*
\x030000
"
        );
    }

    #[test]
    fn write_with_human_noise() {
        let mut x = JEDECFile::new(100);
        x.header = Cow::Borrowed(b"this is a header\n");
        x.footer = Cow::Borrowed(b"this is a footer");
        x.design_spec = Cow::Borrowed(b"this design is for a blah blah blah device");
        x.notes = vec![
            Cow::Borrowed(b"lolol note 1"),
            Cow::Borrowed(b"lolol note 2"),
        ];

        let mut out = String::new();
        x.write_fmt(&mut out, &Quirks::new()).unwrap();

        assert_eq!(
            out,
            "this is a header
\x02this design is for a blah blah blah device*
QF100*

Nlolol note 1*
Nlolol note 2*

L000 0000000000000000*
L016 0000000000000000*
L032 0000000000000000*
L048 0000000000000000*
L064 0000000000000000*
L080 0000000000000000*
L096 0000*
\x030000
this is a footer"
        );

        // make sure the quirk works
        let mut out = String::new();
        x.write_fmt(&mut out, &Quirks::new().no_design_spec(true))
            .unwrap();

        assert_eq!(
            out,
            "this is a header
\x02QF100*

Nlolol note 1*
Nlolol note 2*

L000 0000000000000000*
L016 0000000000000000*
L032 0000000000000000*
L048 0000000000000000*
L064 0000000000000000*
L080 0000000000000000*
L096 0000*
\x030000
this is a footer"
        );
    }

    #[test]
    fn write_io_nonutf8() {
        let mut x = JEDECFile::new(100);
        x.header = Cow::Borrowed(b"this\xFF is a header\n");
        x.footer = Cow::Borrowed(b"this is a footer");
        x.design_spec = Cow::Borrowed(b"this design is for a blah blah blah device");
        x.notes = vec![
            Cow::Borrowed(b"lolol note 1"),
            Cow::Borrowed(b"lolol note 2"),
        ];

        let mut out: Vec<u8> = Vec::new();
        x.write_io(&mut out, &Quirks::new()).unwrap();

        assert_eq!(
            out,
            b"this\xFF is a header
\x02this design is for a blah blah blah device*
QF100*

Nlolol note 1*
Nlolol note 2*

L000 0000000000000000*
L016 0000000000000000*
L032 0000000000000000*
L048 0000000000000000*
L064 0000000000000000*
L080 0000000000000000*
L096 0000*
\x030000
this is a footer"
        );
    }
}