ds-rom 0.7.0

Library for extracting/building Nintendo DS ROMs.
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
use std::{borrow::Cow, io, mem::replace, ops::Range, str::Utf8Error};

use serde::{Deserialize, Serialize};
use snafu::{Backtrace, Snafu};

use super::{
    raw::{
        AutoloadInfo, AutoloadInfoEntry, AutoloadKind, BuildInfo, HmacSha1Signature, HmacSha1SignatureError,
        RawAutoloadInfoError, RawBuildInfoError, NITROCODE_BYTES,
    },
    Autoload, OverlayTable,
};
use crate::{
    compress::lz77::{Lz77, Lz77DecompressError},
    crc::CRC_16_MODBUS,
    crypto::blowfish::{Blowfish, BlowfishError, BlowfishKey, BlowfishLevel},
    rom::LibraryEntry,
};

/// ARM9 program.
#[derive(Clone)]
pub struct Arm9<'a> {
    data: Cow<'a, [u8]>,
    offsets: Arm9Offsets,
    originally_compressed: bool,
    originally_encrypted: bool,
}

/// Offsets in the ARM9 program.
#[derive(Serialize, Deserialize, Clone, Copy)]
pub struct Arm9Offsets {
    /// Base address.
    pub base_address: u32,
    /// Entrypoint function address.
    pub entry_function: u32,
    /// Build info offset.
    pub build_info: u32,
    /// Autoload callback address.
    pub autoload_callback: u32,
    /// Offset to overlay HMAC-SHA1 signature table.
    pub overlay_signatures: u32,
}

const SECURE_AREA_ID: [u8; 8] = [0xff, 0xde, 0xff, 0xe7, 0xff, 0xde, 0xff, 0xe7];
const SECURE_AREA_ENCRY_OBJ: &[u8] = "encryObj".as_bytes();

const LZ77: Lz77 = Lz77 {};

const COMPRESSION_START: usize = 0x4000;

/// Errors related to [`Arm9`].
#[derive(Debug, Snafu)]
pub enum Arm9Error {
    /// Occurs when the program is too small to contain a secure area.
    #[snafu(display("expected {expected:#x} bytes for secure area but had only {actual:#x}:\n{backtrace}"))]
    DataTooSmall {
        /// Expected minimum size.
        expected: usize,
        /// Actual size.
        actual: usize,
        /// Backtrace to the source of the error.
        backtrace: Backtrace,
    },
    /// See [`BlowfishError`].
    #[snafu(transparent)]
    Blowfish {
        /// Source error.
        source: BlowfishError,
    },
    /// Occurs when the string "encryObj" is not found when de/encrypting the secure area.
    #[snafu(display("invalid encryption, 'encryObj' not found"))]
    NotEncryObj {
        /// Backtrace to the source of the error.
        backtrace: Backtrace,
    },
    /// See [`RawBuildInfoError`].
    #[snafu(transparent)]
    RawBuildInfo {
        /// Source error.
        source: RawBuildInfoError,
    },
    /// See [`Lz77DecompressError`].
    #[snafu(transparent)]
    Lz77Decompress {
        /// Source error.
        source: Lz77DecompressError,
    },
    /// See [`io::Error`].
    #[snafu(transparent)]
    Io {
        /// Source error.
        source: io::Error,
    },
}

/// Errors related to ARM9 autoloads.
#[derive(Debug, Snafu)]
pub enum Arm9AutoloadError {
    /// See [`RawBuildInfoError`].
    #[snafu(transparent)]
    RawBuildInfo {
        /// Source error.
        source: RawBuildInfoError,
    },
    /// See [`RawAutoloadInfoError`].
    #[snafu(transparent)]
    RawAutoloadInfo {
        /// Source error.
        source: RawAutoloadInfoError,
    },
    /// Occurs when trying to access autoload blocks while the ARM9 program is compressed.
    #[snafu(display("ARM9 program must be decompressed before accessing autoload blocks:\n{backtrace}"))]
    Compressed {
        /// Backtrace to the source of the error.
        backtrace: Backtrace,
    },
    /// Occurs when trying to access a kind of autoload block which doesn't exist in the ARM9 program.
    #[snafu(display("autoload block {kind} could not be found:\n{backtrace}"))]
    NotFound {
        /// Kind of autoload.
        kind: AutoloadKind,
        /// Backtrace to the source of the error.
        backtrace: Backtrace,
    },
}

/// Errors related to [`Arm9::overlay_signatures`] and [`Arm9::overlay_signatures_mut`].
#[derive(Debug, Snafu)]
pub enum Arm9OverlaySignaturesError {
    /// See [`OverlaySignatureError`].
    #[snafu(transparent)]
    HmacSha1Signature {
        /// Source error.
        source: HmacSha1SignatureError,
    },
    /// See [`RawBuildInfoError`].
    #[snafu(transparent)]
    RawBuildInfo {
        /// Source error.
        source: RawBuildInfoError,
    },
    /// Occurs when trying to access overlay signatures while the ARM9 program is compressed.
    #[snafu(display("ARM9 program must be decompressed before accessing overlay signatures:\n{backtrace}"))]
    OverlaySignaturesCompressed {
        /// Backtrace to the source of the error.
        backtrace: Backtrace,
    },
}

/// Errors related to [`Arm9::hmac_sha1_key`].
#[derive(Debug, Snafu)]
pub enum Arm9HmacSha1KeyError {
    /// See [`RawBuildInfoError`].
    #[snafu(transparent)]
    RawBuildInfo {
        /// Source error.
        source: RawBuildInfoError,
    },
    /// Occurs when trying to access the HMAC-SHA1 key while the ARM9 program is compressed.
    #[snafu(display("ARM9 program must be decompressed before accessing HMAC-SHA1 key:\n{backtrace}"))]
    HmacSha1KeyCompressed {
        /// Backtrace to the source of the error.
        backtrace: Backtrace,
    },
}

/// Options for [`Arm9::with_two_tcms`].
pub struct Arm9WithTcmsOptions {
    /// Whether the program was compressed originally.
    pub originally_compressed: bool,
    /// Whether the program was encrypted originally.
    pub originally_encrypted: bool,
}

impl<'a> Arm9<'a> {
    /// Creates a new ARM9 program from raw data.
    pub fn new<T: Into<Cow<'a, [u8]>>>(data: T, offsets: Arm9Offsets) -> Result<Self, RawBuildInfoError> {
        let mut arm9 = Arm9 { data: data.into(), offsets, originally_compressed: false, originally_encrypted: false };
        arm9.originally_compressed = arm9.is_compressed()?;
        arm9.originally_encrypted = arm9.is_encrypted();
        Ok(arm9)
    }

    /// Creates a new ARM9 program with raw data and two autoloads (ITCM and DTCM).
    ///
    /// # Errors
    ///
    /// See [`Self::build_info_mut`].
    pub fn with_two_tcms(
        mut data: Vec<u8>,
        itcm: Autoload,
        dtcm: Autoload,
        offsets: Arm9Offsets,
        options: Arm9WithTcmsOptions,
    ) -> Result<Self, RawBuildInfoError> {
        let autoload_infos = [*itcm.info().entry(), *dtcm.info().entry()];

        let autoload_blocks = data.len() as u32 + offsets.base_address;
        data.extend(itcm.into_data().iter());
        data.extend(dtcm.into_data().iter());
        let autoload_infos_start = data.len() as u32 + offsets.base_address;
        data.extend(bytemuck::bytes_of(&autoload_infos));
        let autoload_infos_end = data.len() as u32 + offsets.base_address;

        let Arm9WithTcmsOptions { originally_compressed, originally_encrypted } = options;
        let mut arm9 = Self { data: data.into(), offsets, originally_compressed, originally_encrypted };

        let build_info = arm9.build_info_mut()?;
        build_info.autoload_blocks = autoload_blocks;
        build_info.autoload_infos_start = autoload_infos_start;
        build_info.autoload_infos_end = autoload_infos_end;

        Ok(arm9)
    }

    /// Creates a new ARM9 program with raw data and a list of autoloads.
    ///
    /// # Errors
    ///
    /// See [`Self::build_info_mut`].
    pub fn with_autoloads(
        mut data: Vec<u8>,
        autoloads: &[Autoload],
        offsets: Arm9Offsets,
        options: Arm9WithTcmsOptions,
    ) -> Result<Self, RawBuildInfoError> {
        let autoload_blocks = data.len() as u32 + offsets.base_address;

        for autoload in autoloads {
            data.extend(autoload.full_data());
        }

        let autoload_infos_start = data.len() as u32 + offsets.base_address;
        for autoload in autoloads {
            data.extend(bytemuck::bytes_of(autoload.info().entry()));
        }
        let autoload_infos_end = data.len() as u32 + offsets.base_address;

        let Arm9WithTcmsOptions { originally_compressed, originally_encrypted } = options;
        let mut arm9 = Self { data: data.into(), offsets, originally_compressed, originally_encrypted };

        let build_info = arm9.build_info_mut()?;
        build_info.autoload_blocks = autoload_blocks;
        build_info.autoload_infos_start = autoload_infos_start;
        build_info.autoload_infos_end = autoload_infos_end;

        Ok(arm9)
    }

    /// Returns whether the secure area is encrypted. See [`Self::originally_encrypted`] for whether the secure area was
    /// encrypted originally.
    pub fn is_encrypted(&self) -> bool {
        self.data.len() < 8 || self.data[0..8] != SECURE_AREA_ID
    }

    /// Decrypts the secure area. Does nothing if already decrypted.
    ///
    /// # Errors
    ///
    /// This function will return an error if the program is too small to contain a secure area, [`Blowfish::decrypt`] fails or
    /// "encryObj" was not found.
    pub fn decrypt(&mut self, key: &BlowfishKey, gamecode: u32) -> Result<(), Arm9Error> {
        if !self.is_encrypted() {
            return Ok(());
        }

        if self.data.len() < 0x4000 {
            DataTooSmallSnafu { expected: 0x4000usize, actual: self.data.len() }.fail()?;
        }

        let mut secure_area = [0u8; 0x4000];
        secure_area.clone_from_slice(&self.data[0..0x4000]);

        let blowfish = Blowfish::new(key, gamecode, BlowfishLevel::Level2);
        blowfish.decrypt(&mut secure_area[0..8])?;

        let blowfish = Blowfish::new(key, gamecode, BlowfishLevel::Level3);
        blowfish.decrypt(&mut secure_area[0..0x800])?;

        if &secure_area[0..8] != SECURE_AREA_ENCRY_OBJ {
            NotEncryObjSnafu {}.fail()?;
        }

        secure_area[0..8].copy_from_slice(&SECURE_AREA_ID);
        self.data.to_mut()[0..0x4000].copy_from_slice(&secure_area);
        Ok(())
    }

    /// Encrypts the secure area. Does nothing if already encrypted.
    ///
    /// # Errors
    ///
    /// This function will return an error if the program is too small to contain a secure area, or the secure area ID was not
    /// found.
    pub fn encrypt(&mut self, key: &BlowfishKey, gamecode: u32) -> Result<(), Arm9Error> {
        if self.is_encrypted() {
            return Ok(());
        }

        if self.data.len() < 0x4000 {
            DataTooSmallSnafu { expected: 0x4000usize, actual: self.data.len() }.fail()?;
        }

        if self.data[0..8] != SECURE_AREA_ID {
            NotEncryObjSnafu {}.fail()?;
        }

        let secure_area = self.encrypted_secure_area(key, gamecode);
        self.data.to_mut()[0..0x4000].copy_from_slice(&secure_area);
        Ok(())
    }

    /// Returns an encrypted copy of the secure area.
    pub fn encrypted_secure_area(&self, key: &BlowfishKey, gamecode: u32) -> [u8; 0x4000] {
        let mut secure_area = [0u8; 0x4000];
        secure_area.copy_from_slice(&self.data[0..0x4000]);
        if self.is_encrypted() {
            return secure_area;
        }

        secure_area[0..8].copy_from_slice(SECURE_AREA_ENCRY_OBJ);

        let blowfish = Blowfish::new(key, gamecode, BlowfishLevel::Level3);
        blowfish.encrypt(&mut secure_area[0..0x800]).unwrap();

        let blowfish = Blowfish::new(key, gamecode, BlowfishLevel::Level2);
        blowfish.encrypt(&mut secure_area[0..8]).unwrap();

        secure_area
    }

    /// Returns a CRC checksum of the encrypted secure area.
    pub fn secure_area_crc(&self, key: &BlowfishKey, gamecode: u32) -> u16 {
        let secure_area = self.encrypted_secure_area(key, gamecode);
        CRC_16_MODBUS.checksum(&secure_area)
    }

    /// Returns a reference to the build info.
    ///
    /// # Errors
    ///
    /// See [`BuildInfo::borrow_from_slice`].
    pub fn build_info(&self) -> Result<&BuildInfo, RawBuildInfoError> {
        BuildInfo::borrow_from_slice(&self.data[self.offsets.build_info as usize..])
    }

    /// Returns a mutable reference to the build info.
    ///
    /// # Errors
    ///
    /// See [`BuildInfo::borrow_from_slice_mut`].
    pub fn build_info_mut(&mut self) -> Result<&mut BuildInfo, RawBuildInfoError> {
        BuildInfo::borrow_from_slice_mut(&mut self.data.to_mut()[self.offsets.build_info as usize..])
    }

    /// Returns the library version strings in this [`Arm9`].
    ///
    /// # Errors
    ///
    /// This function will return an error if a library string is not valid UTF-8.
    pub fn libraries(&self) -> Result<Box<[LibraryEntry<'_>]>, Utf8Error> {
        let libraries_start = self.offsets.build_info as usize + size_of::<BuildInfo>();
        let mut address = self.base_address() + libraries_start as u32;
        let mut data = &self.data[libraries_start..];

        let mut libraries = Vec::new();

        'outer: while data[0] == b'[' {
            let Some((end_pos, _)) = data.iter().enumerate().find(|(_, c)| **c == b']') else {
                break;
            };
            let version_string = str::from_utf8(&data[..end_pos + 1])?;

            libraries.push(LibraryEntry::new(address, version_string));

            address += end_pos as u32 + 1;
            data = &data[end_pos + 1..];
            loop {
                match data[0] {
                    b'\0' => {
                        address += 1;
                        data = &data[1..]
                    }
                    b'[' => break,
                    _ => break 'outer,
                }
            }
        }

        Ok(libraries.into_boxed_slice())
    }

    /// Returns whether this ARM9 program is compressed. See [`Self::originally_compressed`] for whether the program was
    /// compressed originally.
    ///
    /// # Errors
    ///
    /// See [`Self::build_info`].
    pub fn is_compressed(&self) -> Result<bool, RawBuildInfoError> {
        Ok(self.build_info()?.is_compressed())
    }

    /// Decompresses this ARM9 program. Does nothing if already decompressed.
    ///
    /// # Errors
    ///
    /// See [`Self::is_compressed`] and [`Self::build_info_mut`].
    pub fn decompress(&mut self) -> Result<(), Arm9Error> {
        if !self.is_compressed()? {
            return Ok(());
        }

        let data: Cow<[u8]> = LZ77.decompress(&self.data)?.into_vec().into();
        let old_data = replace(&mut self.data, data);
        let build_info = match self.build_info_mut() {
            Ok(build_info) => build_info,
            Err(e) => {
                self.data = old_data;
                return Err(e.into());
            }
        };
        build_info.compressed_code_end = 0;
        Ok(())
    }

    /// Compresses this ARM9 program. Does nothing if already compressed.
    ///
    /// # Errors
    ///
    /// See [`Self::is_compressed`], [`Lz77::compress`] and [`Self::build_info_mut`].
    pub fn compress(&mut self) -> Result<(), Arm9Error> {
        if self.is_compressed()? {
            return Ok(());
        }

        let data: Cow<[u8]> = LZ77.compress(&self.data, COMPRESSION_START)?.into_vec().into();
        let length = data.len();
        let old_data = replace(&mut self.data, data);
        let base_address = self.base_address();
        let build_info = match self.build_info_mut() {
            Ok(build_info) => build_info,
            Err(e) => {
                self.data = old_data;
                return Err(e.into());
            }
        };
        build_info.compressed_code_end = base_address + length as u32;
        Ok(())
    }

    fn get_autoload_info_entries(&self, build_info: &BuildInfo) -> Result<&[AutoloadInfoEntry], Arm9AutoloadError> {
        let start = (build_info.autoload_infos_start - self.base_address()) as usize;
        let end = (build_info.autoload_infos_end - self.base_address()) as usize;
        let autoload_info = AutoloadInfoEntry::borrow_from_slice(&self.data[start..end])?;
        Ok(autoload_info)
    }

    /// Returns the autoload infos of this [`Arm9`].
    ///
    /// # Errors
    ///
    /// This function will return an error if [`Self::build_info`] or [`Self::get_autoload_infos`] fails or this ARM9 program
    /// is compressed.
    pub fn autoload_infos(&self) -> Result<Vec<AutoloadInfo>, Arm9AutoloadError> {
        let build_info: &BuildInfo = self.build_info()?;
        if build_info.is_compressed() {
            CompressedSnafu {}.fail()?;
        }
        Ok(self
            .get_autoload_info_entries(build_info)?
            .iter()
            .enumerate()
            .map(|(index, entry)| AutoloadInfo::new(*entry, index as u32))
            .collect())
    }

    /// Returns the autoloads of this [`Arm9`].
    ///
    /// # Errors
    ///
    /// This function will return an error if [`Self::build_info`] or [`Self::get_autoload_infos`] fails or this ARM9 program
    /// is compressed.
    pub fn autoloads(&self) -> Result<Box<[Autoload<'_>]>, Arm9AutoloadError> {
        let build_info = self.build_info()?;
        if build_info.is_compressed() {
            CompressedSnafu {}.fail()?;
        }
        let autoload_infos = self.autoload_infos()?;

        let mut autoloads = vec![];
        let mut load_offset = build_info.autoload_blocks - self.base_address();
        for autoload_info in autoload_infos {
            let start = load_offset as usize;
            let end = start + autoload_info.code_size() as usize;
            let data = &self.data[start..end];
            autoloads.push(Autoload::new(data, autoload_info));
            load_offset += autoload_info.code_size();
        }

        Ok(autoloads.into_boxed_slice())
    }

    /// Returns the number of unknown autoloads of this [`Arm9`].
    ///
    /// # Errors
    ///
    /// See [`Self::autoloads`].
    pub fn num_unknown_autoloads(&self) -> Result<usize, Arm9AutoloadError> {
        Ok(self.autoloads()?.iter().filter(|a| matches!(a.kind(), AutoloadKind::Unknown(_))).count())
    }

    /// Returns the HMAC-SHA1 key in this ARM9 program.
    pub fn hmac_sha1_key(&self) -> Result<Option<[u8; 64]>, Arm9HmacSha1KeyError> {
        if self.is_compressed()? {
            HmacSha1KeyCompressedSnafu {}.fail()?
        }

        // Credits to pleonex: https://scenegate.github.io/Ekona/docs/specs/cartridge/security.html#overlays
        let Some((i, _)) = self.data.chunks(4).enumerate().filter(|(_, chunk)| *chunk == NITROCODE_BYTES).nth(1) else {
            return Ok(None);
        };
        let start = i * 4;
        let end = start + 64;
        if end > self.data.len() {
            return Ok(None);
        }
        let mut key = [0u8; 64];
        key.copy_from_slice(&self.data[start..end]);
        Ok(Some(key))
    }

    fn overlay_table_signature_range(&self) -> Result<Option<Range<usize>>, Arm9OverlaySignaturesError> {
        let overlay_signatures_offset = self.overlay_signatures_offset() as usize;
        if overlay_signatures_offset == 0 {
            return Ok(None);
        }

        if self.is_compressed()? {
            OverlaySignaturesCompressedSnafu {}.fail()?;
        }

        // The overlay table signature is located right before the overlay signatures
        let start = overlay_signatures_offset - size_of::<HmacSha1Signature>();
        let end = overlay_signatures_offset;
        if end > self.data.len() {
            return Ok(None);
        }
        Ok(Some(start..end))
    }

    /// Returns the ARM9 overlay table signature.
    ///
    /// # Errors
    ///
    /// This function will return an error if the ARM9 program is compressed or if [`HmacSha1Signature::borrow_from_slice`] fails.
    pub fn overlay_table_signature(&self) -> Result<Option<&HmacSha1Signature>, Arm9OverlaySignaturesError> {
        let Some(range) = self.overlay_table_signature_range()? else {
            return Ok(None);
        };
        let data = &self.data[range];

        let signature = HmacSha1Signature::borrow_from_slice(data)?;
        Ok(Some(signature.first().unwrap()))
    }

    /// Returns a mutable reference to the ARM9 overlay table signature.
    ///
    /// # Errors
    ///
    /// This function will return an error if the ARM9 program is compressed or if [`HmacSha1Signature::borrow_from_slice_mut`]
    /// fails.
    pub fn overlay_table_signature_mut(&mut self) -> Result<Option<&mut HmacSha1Signature>, Arm9OverlaySignaturesError> {
        let Some(range) = self.overlay_table_signature_range()? else {
            return Ok(None);
        };
        let data = &mut self.data.to_mut()[range];

        let signature = HmacSha1Signature::borrow_from_slice_mut(data)?;
        Ok(Some(signature.first_mut().unwrap()))
    }

    fn overlay_signatures_range(&self, num_overlays: usize) -> Result<Option<Range<usize>>, Arm9OverlaySignaturesError> {
        let start = self.overlay_signatures_offset() as usize;
        if start == 0 {
            return Ok(None);
        }

        if self.is_compressed()? {
            OverlaySignaturesCompressedSnafu {}.fail()?;
        }

        let end = start + size_of::<HmacSha1Signature>() * num_overlays;
        if end > self.data.len() {
            return Ok(None);
        }
        Ok(Some(start..end))
    }

    /// Returns the ARM9 overlay signature table.
    ///
    /// # Errors
    ///
    /// This function will return an error if the ARM9 program is compressed or if [`HmacSha1Signature::borrow_from_slice`]
    /// fails.
    pub fn overlay_signatures(&self, num_overlays: usize) -> Result<Option<&[HmacSha1Signature]>, Arm9OverlaySignaturesError> {
        let Some(range) = self.overlay_signatures_range(num_overlays)? else {
            return Ok(None);
        };
        let data = &self.data[range];
        Ok(Some(HmacSha1Signature::borrow_from_slice(data)?))
    }

    /// Returns a mutable reference to the ARM9 overlay signature table.
    ///
    /// # Errors
    ///
    /// This function will return an error if the ARM9 program is compressed or if [`HmacSha1Signature::borrow_from_slice_mut`]
    /// fails.
    pub fn overlay_signatures_mut(
        &mut self,
        num_overlays: usize,
    ) -> Result<Option<&mut [HmacSha1Signature]>, Arm9OverlaySignaturesError> {
        let Some(range) = self.overlay_signatures_range(num_overlays)? else {
            return Ok(None);
        };
        let data = &mut self.data.to_mut()[range];
        Ok(Some(HmacSha1Signature::borrow_from_slice_mut(data)?))
    }

    /// Returns the code of this ARM9 program.
    ///
    /// # Errors
    ///
    /// See [`Self::build_info`].
    pub fn code(&self) -> Result<&[u8], RawBuildInfoError> {
        let build_info = self.build_info()?;
        Ok(&self.data[..(build_info.bss_start - self.base_address()) as usize])
    }

    /// Returns a reference to the full data.
    pub fn full_data(&self) -> &[u8] {
        &self.data
    }

    /// Returns the base address.
    pub fn base_address(&self) -> u32 {
        self.offsets.base_address
    }

    /// Returns the end address.
    pub fn end_address(&self) -> Result<u32, RawBuildInfoError> {
        let build_info = self.build_info()?;
        Ok(build_info.bss_end)
    }

    /// Returns the entry function address.
    pub fn entry_function(&self) -> u32 {
        self.offsets.entry_function
    }

    /// Returns the build info offset.
    pub fn build_info_offset(&self) -> u32 {
        self.offsets.build_info
    }

    /// Returns the autoload callback address.
    pub fn autoload_callback(&self) -> u32 {
        self.offsets.autoload_callback
    }

    /// Returns the offset to the overlay HMAC-SHA1 signature table.
    pub fn overlay_signatures_offset(&self) -> u32 {
        self.offsets.overlay_signatures
    }

    /// Returns the [`Range`] of uninitialized data in this ARM9 program.
    ///
    /// # Errors
    ///
    /// See [`Self::build_info`].
    pub fn bss(&self) -> Result<Range<u32>, RawBuildInfoError> {
        let build_info = self.build_info()?;
        Ok(build_info.bss_start..build_info.bss_end)
    }

    /// Returns a reference to the ARM9 offsets.
    pub fn offsets(&self) -> &Arm9Offsets {
        &self.offsets
    }

    /// Returns whether the ARM9 program was compressed originally. See [`Self::is_compressed`] for the current state.
    pub fn originally_compressed(&self) -> bool {
        self.originally_compressed
    }

    /// Returns whether the ARM9 program was encrypted originally. See [`Self::is_encrypted`] for the current state.
    pub fn originally_encrypted(&self) -> bool {
        self.originally_encrypted
    }

    pub(crate) fn update_overlay_signatures(
        &mut self,
        arm9_overlay_table: &OverlayTable,
    ) -> Result<(), Arm9OverlaySignaturesError> {
        let arm9_overlays = arm9_overlay_table.overlays();
        let Some(signatures) = self.overlay_signatures_mut(arm9_overlays.len())? else {
            return Ok(());
        };
        for overlay in arm9_overlays {
            if let Some(signature) = overlay.signature() {
                signatures[overlay.id() as usize] = signature;
            }
        }

        if let Some(signature) = arm9_overlay_table.signature() {
            let Some(table_signature) = self.overlay_table_signature_mut()? else {
                return Ok(());
            };
            *table_signature = signature;
        }

        Ok(())
    }
}

impl AsRef<[u8]> for Arm9<'_> {
    fn as_ref(&self) -> &[u8] {
        &self.data
    }
}