cildec 0.1.0

Zero-dependency, panic-free decoder for ECMA-335 metadata and CIL method bodies
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
//! The metadata table stream: `#~` (compressed) and `#-` (uncompressed).
//!
//! Row sizes and column widths are computed from the stream header exactly as
//! ECMA-335 II.24.2.6 describes: heap indices are 2 or 4 bytes according to the
//! `HeapSizes` bits, a simple index is 2 bytes when its target table has fewer
//! than 2^16 rows, and a coded index is 2 bytes when the largest table it can
//! tag has fewer than 2^(16 - tag bits) rows.
//!
//! Nothing in this module copies row data. [`Tables`] stores one [`TableLayout`]
//! per table id and decodes a row on demand from the borrowed stream bytes.

pub mod coded;
mod lookup;
mod rows;

pub use coded::CodedIndex;
pub use lookup::sort_key_column;
pub use rows::*;

use alloc::vec::Vec;

use crate::error::{Diagnostic, DiagnosticCode, Error, ErrorKind, Result, Strictness};
use crate::reader::{Reader, read_uint_at};
use crate::token::{TableId, Token};

const CTX: &str = "table stream";

/// The largest number of columns any table has (`Assembly` and `AssemblyRef`).
pub const MAX_COLUMNS: usize = 9;

/// The number of table id slots; ids are one byte and the high bit is unused.
const TABLE_SLOTS: usize = 64;

/// What one column of a table holds.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ColumnKind {
    /// A 2-byte constant.
    U16,
    /// A 4-byte constant.
    U32,
    /// An index into `#Strings`.
    String,
    /// An index into `#Blob`.
    Blob,
    /// An index into `#GUID`.
    Guid,
    /// A simple index into another table.
    Rid(TableId),
    /// A coded index.
    Coded(CodedIndex),
}

/// The computed geometry of one table.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub struct TableLayout {
    /// The number of rows, after any clamping applied while parsing.
    pub row_count: u32,
    /// The size of one row in bytes.
    pub row_size: u16,
    /// The number of columns.
    pub column_count: u8,
    /// Byte offset of each column within a row.
    pub column_offsets: [u8; MAX_COLUMNS],
    /// Byte width of each column.
    pub column_widths: [u8; MAX_COLUMNS],
    /// Offset of the first row within the table stream.
    pub data_offset: usize,
}

impl Default for TableLayout {
    fn default() -> Self {
        TableLayout {
            row_count: 0,
            row_size: 0,
            column_count: 0,
            column_offsets: [0; MAX_COLUMNS],
            column_widths: [0; MAX_COLUMNS],
            data_offset: 0,
        }
    }
}

impl TableLayout {
    /// The total size of the table in bytes.
    pub const fn byte_size(&self) -> usize {
        (self.row_count as usize) * (self.row_size as usize)
    }
}

/// A cursor that walks the columns of one row.
///
/// Columns must be read in declaration order; each read advances to the next
/// column using the widths computed for the image.
#[derive(Debug)]
pub struct RowCursor<'a> {
    bytes: &'a [u8],
    layout: TableLayout,
    column: usize,
    base: usize,
}

impl RowCursor<'_> {
    /// Reads the next column as a zero-extended unsigned value.
    pub fn uint(&mut self) -> Result<u32> {
        let index = self.column;
        if index >= self.layout.column_count as usize {
            return Err(Error::new(ErrorKind::Malformed, self.base, "table row"));
        }
        let offset = self.layout.column_offsets[index] as usize;
        let width = self.layout.column_widths[index];
        self.column += 1;
        read_uint_at(self.bytes, offset, width).ok_or(Error::new(
            ErrorKind::Truncated,
            self.base.saturating_add(offset),
            "table row",
        ))
    }

    /// Reads the next column and decodes it as a coded index.
    pub fn coded(&mut self, kind: CodedIndex) -> Result<Token> {
        let offset = self.layout.column_offsets.get(self.column).copied().unwrap_or(0) as usize;
        let value = self.uint()?;
        kind.decode(value, self.base.saturating_add(offset))
    }
}

/// A metadata table row that can be decoded from a [`RowCursor`].
pub trait TableRow: Copy + Sized {
    /// The table this row belongs to.
    const TABLE: TableId;
    /// The column layout of the table, in declaration order.
    const COLUMNS: &'static [ColumnKind];

    /// Decodes one row, consuming every column in order.
    fn decode(cursor: &mut RowCursor<'_>) -> Result<Self>;
}

/// The parsed `#~` or `#-` stream.
#[derive(Clone, Debug)]
pub struct Tables<'a> {
    data: &'a [u8],
    base: usize,
    major_version: u8,
    minor_version: u8,
    heap_sizes: u8,
    valid: u64,
    declared_sorted: u64,
    verified_sorted: u64,
    uncompressed: bool,
    extra_data: Option<u32>,
    layouts: [TableLayout; TABLE_SLOTS],
}

impl<'a> Tables<'a> {
    /// An empty table stream, used when an image has no `#~` or `#-`.
    pub fn empty() -> Self {
        Tables {
            data: &[],
            base: 0,
            major_version: 2,
            minor_version: 0,
            heap_sizes: 0,
            valid: 0,
            declared_sorted: 0,
            verified_sorted: !0,
            uncompressed: false,
            extra_data: None,
            layouts: [TableLayout::default(); TABLE_SLOTS],
        }
    }

    /// Parses a table stream.
    ///
    /// `base` is the absolute offset of `data` in the input, used for error and
    /// diagnostic offsets. `uncompressed` selects `#-` semantics, which only
    /// affects how the stream is reported; the binary layout is identical.
    pub fn parse(
        data: &'a [u8],
        base: usize,
        uncompressed: bool,
        strictness: Strictness,
        diagnostics: &mut Vec<Diagnostic>,
    ) -> Result<Self> {
        let mut r = Reader::new(data, base, CTX);
        let _reserved = r.u32()?;
        let major_version = r.u8()?;
        let minor_version = r.u8()?;
        let heap_sizes = r.u8()?;
        let _reserved2 = r.u8()?;
        let valid = r.u64()?;
        let declared_sorted = r.u64()?;

        const KNOWN_HEAP_BITS: u8 = 0x01 | 0x02 | 0x04 | 0x08 | 0x20 | 0x40 | 0x80;
        if heap_sizes & !KNOWN_HEAP_BITS != 0 {
            if strictness.is_strict() {
                return Err(Error::new(ErrorKind::Malformed, base + 6, CTX));
            }
            diagnostics.push(Diagnostic::new(
                DiagnosticCode::UnknownHeapSizeBits,
                base + 6,
                u64::from(heap_sizes),
            ));
        }

        // Row counts: one u32 per set bit of `Valid`, in ascending table order.
        let mut row_counts = [0u32; TABLE_SLOTS];
        for (slot, count_slot) in row_counts.iter_mut().enumerate() {
            if valid & (1u64 << slot) == 0 {
                continue;
            }
            let at = r.absolute();
            let count = r.u32()?;
            match TableId::from_u8(slot as u8) {
                Some(_) => *count_slot = count,
                None => {
                    if count != 0 || strictness.is_strict() {
                        return Err(Error::new(ErrorKind::Unsupported, at, CTX));
                    }
                    diagnostics.push(Diagnostic::new(
                        DiagnosticCode::ReservedTablePresent,
                        at,
                        slot as u64,
                    ));
                }
            }
        }

        let extra_data = if heap_sizes & 0x40 != 0 { Some(r.u32()?) } else { None };

        let strings_wide = heap_sizes & 0x01 != 0;
        let guids_wide = heap_sizes & 0x02 != 0;
        let blobs_wide = heap_sizes & 0x04 != 0;

        let mut layouts = [TableLayout::default(); TABLE_SLOTS];
        let mut cursor = r.position();
        let mut exhausted = false;
        for &id in TableId::ALL {
            let slot = id.to_u8() as usize;
            let mut count = row_counts[slot];
            let columns = columns_of(id);
            let mut layout = TableLayout {
                row_count: 0,
                row_size: 0,
                column_count: columns.len() as u8,
                column_offsets: [0; MAX_COLUMNS],
                column_widths: [0; MAX_COLUMNS],
                data_offset: cursor,
            };
            let mut offset = 0u16;
            for (i, kind) in columns.iter().enumerate() {
                let width = column_width(*kind, &row_counts, strings_wide, guids_wide, blobs_wide);
                layout.column_offsets[i] = offset as u8;
                layout.column_widths[i] = width;
                offset += u16::from(width);
            }
            layout.row_size = offset;

            if count > 0 {
                let available = data.len().saturating_sub(cursor);
                let needed = (count as u64) * u64::from(layout.row_size);
                if exhausted || needed > available as u64 {
                    if strictness.is_strict() {
                        return Err(Error::new(ErrorKind::Truncated, base + cursor, CTX));
                    }
                    diagnostics.push(Diagnostic::new(
                        DiagnosticCode::StreamRangeClamped,
                        base + cursor,
                        needed,
                    ));
                    count = if layout.row_size == 0 {
                        0
                    } else {
                        (available / layout.row_size as usize) as u32
                    };
                    exhausted = true;
                }
            }
            layout.row_count = count;
            cursor = cursor.saturating_add(layout.byte_size());
            layouts[slot] = layout;
        }

        let mut tables = Tables {
            data,
            base,
            major_version,
            minor_version,
            heap_sizes,
            valid,
            declared_sorted,
            verified_sorted: 0,
            uncompressed,
            extra_data,
            layouts,
        };
        tables.verified_sorted = tables.verify_sortedness(strictness, diagnostics)?;
        Ok(tables)
    }

    /// The raw stream bytes.
    pub const fn data(&self) -> &'a [u8] {
        self.data
    }

    /// The absolute offset of the stream in the parsed input.
    pub const fn base(&self) -> usize {
        self.base
    }

    /// The stream schema major version, normally 2.
    pub const fn major_version(&self) -> u8 {
        self.major_version
    }

    /// The stream schema minor version, normally 0.
    pub const fn minor_version(&self) -> u8 {
        self.minor_version
    }

    /// The raw `HeapSizes` byte.
    pub const fn heap_sizes(&self) -> u8 {
        self.heap_sizes
    }

    /// True when `#Strings` indices are 4 bytes wide.
    pub const fn strings_wide(&self) -> bool {
        self.heap_sizes & 0x01 != 0
    }

    /// True when `#GUID` indices are 4 bytes wide.
    pub const fn guids_wide(&self) -> bool {
        self.heap_sizes & 0x02 != 0
    }

    /// True when `#Blob` indices are 4 bytes wide.
    pub const fn blobs_wide(&self) -> bool {
        self.heap_sizes & 0x04 != 0
    }

    /// True when the stream was `#-` rather than `#~`.
    pub const fn is_uncompressed(&self) -> bool {
        self.uncompressed
    }

    /// The extra 4 bytes present when `HeapSizes` bit `0x40` is set.
    pub const fn extra_data(&self) -> Option<u32> {
        self.extra_data
    }

    /// The raw `Valid` bitvector.
    pub const fn valid_bits(&self) -> u64 {
        self.valid
    }

    /// The raw `Sorted` bitvector.
    pub const fn sorted_bits(&self) -> u64 {
        self.declared_sorted
    }

    /// True when the `Valid` bitvector marks this table present.
    pub const fn is_present(&self, table: TableId) -> bool {
        self.valid & (1u64 << table.to_u8()) != 0
    }

    /// True when the `Sorted` bitvector marks this table sorted.
    pub const fn is_declared_sorted(&self, table: TableId) -> bool {
        self.declared_sorted & (1u64 << table.to_u8()) != 0
    }

    /// True when this table has a sort key and its rows really are ordered.
    ///
    /// Lookups fall back to a linear scan when this is false, so results stay
    /// correct on images whose `Sorted` bitvector lies.
    pub const fn is_sorted(&self, table: TableId) -> bool {
        self.verified_sorted & (1u64 << table.to_u8()) != 0
    }

    /// The number of rows in a table.
    pub const fn row_count(&self, table: TableId) -> u32 {
        self.layouts[table.to_u8() as usize].row_count
    }

    /// The number of logically addressable rows, which is the `*Ptr` table row
    /// count when an indirection table is present.
    pub fn logical_row_count(&self, table: TableId) -> u32 {
        match table.ptr_source() {
            Some(ptr) if self.row_count(ptr) > 0 => self.row_count(ptr),
            _ => self.row_count(table),
        }
    }

    /// The computed layout of a table.
    pub const fn layout(&self, table: TableId) -> &TableLayout {
        &self.layouts[table.to_u8() as usize]
    }

    /// The bytes of one row, by 1-based RID.
    pub fn row_bytes(&self, table: TableId, rid: u32) -> Result<&'a [u8]> {
        let layout = self.layout(table);
        if rid == 0 || rid > layout.row_count {
            return Err(Error::new(ErrorKind::OutOfRange, self.base, table.name()));
        }
        let start = layout.data_offset + (rid as usize - 1) * layout.row_size as usize;
        let end = start + layout.row_size as usize;
        self.data.get(start..end).ok_or(Error::new(
            ErrorKind::Truncated,
            self.base.saturating_add(start),
            table.name(),
        ))
    }

    /// Decodes one row without applying any `*Ptr` indirection.
    pub fn row<R: TableRow>(&self, rid: u32) -> Result<R> {
        let layout = *self.layout(R::TABLE);
        let bytes = self.row_bytes(R::TABLE, rid)?;
        let start = layout.data_offset + (rid as usize - 1) * layout.row_size as usize;
        let mut cursor =
            RowCursor { bytes, layout, column: 0, base: self.base.saturating_add(start) };
        R::decode(&mut cursor)
    }

    /// Decodes one row, following the `*Ptr` indirection when present.
    pub fn row_indirect<R: TableRow>(&self, rid: u32) -> Result<R> {
        let physical = self.resolve_indirection(R::TABLE, rid)?;
        self.row::<R>(physical)
    }

    /// Maps a logical RID through the matching `*Ptr` table, if there is one.
    pub fn resolve_indirection(&self, table: TableId, rid: u32) -> Result<u32> {
        let Some(ptr) = table.ptr_source() else { return Ok(rid) };
        if self.row_count(ptr) == 0 {
            return Ok(rid);
        }
        Ok(match ptr {
            TableId::FieldPtr => self.row::<FieldPtrRow>(rid)?.field.get(),
            TableId::MethodPtr => self.row::<MethodPtrRow>(rid)?.method.get(),
            TableId::ParamPtr => self.row::<ParamPtrRow>(rid)?.param.get(),
            TableId::EventPtr => self.row::<EventPtrRow>(rid)?.event.get(),
            TableId::PropertyPtr => self.row::<PropertyPtrRow>(rid)?.property.get(),
            _ => rid,
        })
    }

    /// Reads one raw column value of a row, without decoding it.
    pub fn raw_column(&self, table: TableId, rid: u32, column: usize) -> Result<u32> {
        let layout = self.layout(table);
        if column >= layout.column_count as usize {
            return Err(Error::new(ErrorKind::OutOfRange, self.base, table.name()));
        }
        let bytes = self.row_bytes(table, rid)?;
        read_uint_at(bytes, layout.column_offsets[column] as usize, layout.column_widths[column])
            .ok_or(Error::new(ErrorKind::Truncated, self.base, table.name()))
    }

    /// Iterates every row of a table in RID order, without indirection.
    pub fn iter<R: TableRow>(&self) -> RowIter<'_, 'a, R> {
        RowIter {
            tables: self,
            rid: 1,
            end: self.row_count(R::TABLE) + 1,
            _row: core::marker::PhantomData,
        }
    }

    /// Iterates every logical row of a table, applying `*Ptr` indirection.
    pub fn iter_indirect<R: TableRow>(&self) -> IndirectRowIter<'_, 'a, R> {
        IndirectRowIter {
            tables: self,
            rid: 1,
            end: self.logical_row_count(R::TABLE) + 1,
            _row: core::marker::PhantomData,
        }
    }

    fn verify_sortedness(
        &self,
        strictness: Strictness,
        diagnostics: &mut Vec<Diagnostic>,
    ) -> Result<u64> {
        let mut bits = 0u64;
        for &id in TableId::ALL {
            let Some(column) = lookup::sort_key_column(id) else { continue };
            let count = self.row_count(id);
            if count <= 1 {
                bits |= 1u64 << id.to_u8();
                continue;
            }
            let mut sorted = true;
            let mut previous = self.raw_column(id, 1, column)?;
            for rid in 2..=count {
                let value = self.raw_column(id, rid, column)?;
                if value < previous {
                    sorted = false;
                    break;
                }
                previous = value;
            }
            if sorted {
                bits |= 1u64 << id.to_u8();
            } else {
                if strictness.is_strict() && self.is_declared_sorted(id) {
                    return Err(Error::new(ErrorKind::Malformed, self.base, id.name()));
                }
                diagnostics.push(Diagnostic::new(
                    DiagnosticCode::TableNotSorted,
                    self.base,
                    u64::from(id.to_u8()),
                ));
            }
        }
        Ok(bits)
    }
}

/// Iterator over the rows of one table; see [`Tables::iter`].
#[derive(Debug)]
pub struct RowIter<'t, 'a, R> {
    tables: &'t Tables<'a>,
    rid: u32,
    end: u32,
    _row: core::marker::PhantomData<fn() -> R>,
}

impl<R: TableRow> Iterator for RowIter<'_, '_, R> {
    type Item = (u32, Result<R>);

    fn next(&mut self) -> Option<Self::Item> {
        if self.rid >= self.end {
            return None;
        }
        let rid = self.rid;
        self.rid += 1;
        Some((rid, self.tables.row::<R>(rid)))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = (self.end - self.rid) as usize;
        (n, Some(n))
    }
}

impl<R: TableRow> ExactSizeIterator for RowIter<'_, '_, R> {}

/// Iterator over the logical rows of one table; see [`Tables::iter_indirect`].
#[derive(Debug)]
pub struct IndirectRowIter<'t, 'a, R> {
    tables: &'t Tables<'a>,
    rid: u32,
    end: u32,
    _row: core::marker::PhantomData<fn() -> R>,
}

impl<R: TableRow> Iterator for IndirectRowIter<'_, '_, R> {
    type Item = (u32, Result<R>);

    fn next(&mut self) -> Option<Self::Item> {
        if self.rid >= self.end {
            return None;
        }
        let rid = self.rid;
        self.rid += 1;
        Some((rid, self.tables.row_indirect::<R>(rid)))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = (self.end - self.rid) as usize;
        (n, Some(n))
    }
}

impl<R: TableRow> ExactSizeIterator for IndirectRowIter<'_, '_, R> {}

fn column_width(
    kind: ColumnKind,
    row_counts: &[u32; TABLE_SLOTS],
    strings_wide: bool,
    guids_wide: bool,
    blobs_wide: bool,
) -> u8 {
    match kind {
        ColumnKind::U16 => 2,
        ColumnKind::U32 => 4,
        ColumnKind::String => {
            if strings_wide {
                4
            } else {
                2
            }
        }
        ColumnKind::Guid => {
            if guids_wide {
                4
            } else {
                2
            }
        }
        ColumnKind::Blob => {
            if blobs_wide {
                4
            } else {
                2
            }
        }
        ColumnKind::Rid(target) => {
            // A list column addresses the `*Ptr` table when one is present, and
            // both tables must be reachable, so take the larger row count.
            let mut rows = row_counts[target.to_u8() as usize];
            if let Some(ptr) = target.ptr_source() {
                rows = rows.max(row_counts[ptr.to_u8() as usize]);
            }
            if rows < 0x1_0000 { 2 } else { 4 }
        }
        ColumnKind::Coded(coded) => {
            let mut max = 0u32;
            for table in coded.tables().iter().flatten() {
                max = max.max(row_counts[table.to_u8() as usize]);
            }
            coded.column_width(max)
        }
    }
}

/// The column layout of a table id, from its row type.
pub fn columns_of(table: TableId) -> &'static [ColumnKind] {
    macro_rules! arm {
        ($($id:ident => $row:ident),* $(,)?) => {
            match table {
                $( TableId::$id => <$row as TableRow>::COLUMNS, )*
            }
        };
    }
    arm! {
        Module => ModuleRow,
        TypeRef => TypeRefRow,
        TypeDef => TypeDefRow,
        FieldPtr => FieldPtrRow,
        Field => FieldRow,
        MethodPtr => MethodPtrRow,
        MethodDef => MethodDefRow,
        ParamPtr => ParamPtrRow,
        Param => ParamRow,
        InterfaceImpl => InterfaceImplRow,
        MemberRef => MemberRefRow,
        Constant => ConstantRow,
        CustomAttribute => CustomAttributeRow,
        FieldMarshal => FieldMarshalRow,
        DeclSecurity => DeclSecurityRow,
        ClassLayout => ClassLayoutRow,
        FieldLayout => FieldLayoutRow,
        StandAloneSig => StandAloneSigRow,
        EventMap => EventMapRow,
        EventPtr => EventPtrRow,
        Event => EventRow,
        PropertyMap => PropertyMapRow,
        PropertyPtr => PropertyPtrRow,
        Property => PropertyRow,
        MethodSemantics => MethodSemanticsRow,
        MethodImpl => MethodImplRow,
        ModuleRef => ModuleRefRow,
        TypeSpec => TypeSpecRow,
        ImplMap => ImplMapRow,
        FieldRva => FieldRvaRow,
        EncLog => EncLogRow,
        EncMap => EncMapRow,
        Assembly => AssemblyRow,
        AssemblyProcessor => AssemblyProcessorRow,
        AssemblyOs => AssemblyOsRow,
        AssemblyRef => AssemblyRefRow,
        AssemblyRefProcessor => AssemblyRefProcessorRow,
        AssemblyRefOs => AssemblyRefOsRow,
        File => FileRow,
        ExportedType => ExportedTypeRow,
        ManifestResource => ManifestResourceRow,
        NestedClass => NestedClassRow,
        GenericParam => GenericParamRow,
        MethodSpec => MethodSpecRow,
        GenericParamConstraint => GenericParamConstraintRow,
        Document => DocumentRow,
        MethodDebugInformation => MethodDebugInformationRow,
        LocalScope => LocalScopeRow,
        LocalVariable => LocalVariableRow,
        LocalConstant => LocalConstantRow,
        ImportScope => ImportScopeRow,
        StateMachineMethod => StateMachineMethodRow,
        CustomDebugInformation => CustomDebugInformationRow,
    }
}

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

    #[test]
    fn every_table_has_at_most_max_columns() {
        for &id in TableId::ALL {
            let columns = columns_of(id);
            assert!(!columns.is_empty(), "{} has no columns", id.name());
            assert!(columns.len() <= MAX_COLUMNS, "{} has {} columns", id.name(), columns.len());
        }
    }

    #[test]
    fn simple_index_width_switches_at_65536_rows() {
        let mut counts = [0u32; TABLE_SLOTS];
        let kind = ColumnKind::Rid(TableId::TypeDef);
        counts[TableId::TypeDef.to_u8() as usize] = 0xFFFF;
        assert_eq!(column_width(kind, &counts, false, false, false), 2);
        counts[TableId::TypeDef.to_u8() as usize] = 0x1_0000;
        assert_eq!(column_width(kind, &counts, false, false, false), 4);
    }

    #[test]
    fn list_column_width_accounts_for_the_ptr_table() {
        let mut counts = [0u32; TABLE_SLOTS];
        let kind = ColumnKind::Rid(TableId::Field);
        counts[TableId::Field.to_u8() as usize] = 10;
        counts[TableId::FieldPtr.to_u8() as usize] = 0x1_0000;
        assert_eq!(column_width(kind, &counts, false, false, false), 4);
    }

    #[test]
    fn heap_index_widths_follow_heap_sizes() {
        let counts = [0u32; TABLE_SLOTS];
        for bits in 0u8..8 {
            let s = bits & 1 != 0;
            let g = bits & 2 != 0;
            let b = bits & 4 != 0;
            assert_eq!(column_width(ColumnKind::String, &counts, s, g, b), if s { 4 } else { 2 });
            assert_eq!(column_width(ColumnKind::Guid, &counts, s, g, b), if g { 4 } else { 2 });
            assert_eq!(column_width(ColumnKind::Blob, &counts, s, g, b), if b { 4 } else { 2 });
        }
    }

    #[test]
    fn coded_index_width_uses_the_largest_tagged_table() {
        let mut counts = [0u32; TABLE_SLOTS];
        let kind = ColumnKind::Coded(CodedIndex::TypeDefOrRef);
        counts[TableId::TypeSpec.to_u8() as usize] = 0x3FFF;
        assert_eq!(column_width(kind, &counts, false, false, false), 2);
        counts[TableId::TypeSpec.to_u8() as usize] = 0x4000;
        assert_eq!(column_width(kind, &counts, false, false, false), 4);
        // A table that is not tagged by this coded index has no effect.
        let mut counts = [0u32; TABLE_SLOTS];
        counts[TableId::Param.to_u8() as usize] = 0xFFFF;
        assert_eq!(column_width(kind, &counts, false, false, false), 2);
    }

    #[test]
    fn empty_stream_is_usable() {
        let tables = Tables::empty();
        assert_eq!(tables.row_count(TableId::TypeDef), 0);
        assert!(tables.row::<TypeDefRow>(1).is_err());
        assert_eq!(tables.iter::<TypeDefRow>().count(), 0);
    }
}