scarletbook 1.0.2

Clean-room reader for Super Audio CD (Scarletbook) disc images
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
// SPDX-License-Identifier: MIT OR Apache-2.0

//! ID3v2.4 tags as DSF carries them (specification 8, 8.3).

use super::tags::{TagItem, TagSet};
use crate::error::Error;

/// The largest tag written, header and footer included. A synchsafe size can say up to
/// 2^28 - 1; TOC strings are at most 4096 bytes each, so this bound matters only for a
/// caller's own values.
pub const MAX_TAG_BYTES: usize = 1 << 20;

/// The text encoding of ID3 frames (specification 8, 8.3.4; W7).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Id3Encoding {
    /// 0x03, UTF-8.
    Utf8,
    /// 0x00, ISO 8859-1, for each frame whose code points all allow it (8.3.4 item 2), and
    /// UTF-8 for the others.
    Latin1OrUtf8,
    /// 0x01, UTF-16 with a byte order mark before each string, little-endian.
    Utf16Le,
    /// 0x01, UTF-16 with a byte order mark before each string, big-endian.
    Utf16Be,
    /// 0x02, UTF-16BE without a byte order mark.
    Utf16BeNoBom,
}

/// How the arranger is written (8.3.5; W11).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ArrangerFrame {
    /// `TIPL`: the function `arranger`, then the name.
    Tipl,
    /// `TXXX` with [`Id3Options::arranger_description`].
    Txxx,
}

/// How the album or disc artist is written (8.3.5; W11): ID3 has no album-artist frame.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum AlbumArtistFrame {
    /// No frame.
    None,
    /// `TPE2` (band, orchestra or accompaniment).
    Tpe2,
    /// `TXXX` with [`Id3Options::album_artist_description`].
    Txxx,
}

/// The frame of the date (8.3.5, 8.3.8; W11).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum DateFrame {
    /// `TDRL`, the release time.
    Tdrl,
    /// `TDRC`, the recording time.
    Tdrc,
}

/// The ID3 writer's choices of specification 8, 8.6 (rows 4 to 12; W numbers), with their
/// defaults.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Id3Options {
    /// W4, a footer (and so no padding), as the ID3 document requires of a tag after the audio (8.3.1 item 2).
    /// Default `true`.
    pub footer: bool,
    /// W5, bytes of padding after the frames, without a footer. Default 0.
    pub padding: u32,
    /// W6, the frame order: frames are written in the order of their IDs here, and frames
    /// with the same ID in the order they are made. Default the order of the 8.3.5 table.
    /// Frames whose ID is missing are written last.
    pub frame_order: Vec<[u8; 4]>,
    /// W7, the text encoding. Default [`Id3Encoding::Utf8`].
    pub encoding: Id3Encoding,
    /// W8, whether the last string of a text frame, a `TXXX` value and a `COMM` text end
    /// with a terminator. Default `false`.
    pub terminate_last: bool,
    /// W9, what replaces a control character (8.3.4 item 1); `None` removes it. Default
    /// `None`.
    pub control_replacement: Option<char>,
    /// W10, a separator at which every value is split into several values. Default `None`.
    pub split: Option<String>,
    /// W11, the arranger's frame. Default [`ArrangerFrame::Tipl`].
    pub arranger: ArrangerFrame,
    /// W11 and W12, `None` for one `COMM` frame per message, or the separator that joins
    /// the message and the extra message in one. Default `None`.
    pub join_messages: Option<String>,
    /// W11, whether the phonetic title and performer are written (`TSOT`, `TSOP`).
    /// Default `false`.
    pub phonetic: bool,
    /// W11, whether `TRCK` has `/count`. Default `true`.
    pub track_count: bool,
    /// W11, the album or disc artist's frame. Default [`AlbumArtistFrame::None`].
    pub album_artist: AlbumArtistFrame,
    /// W11, whether the copyright is written, as `TCOP` with the year of the date and a
    /// space before it, and only when the date has a year (8.3.5). Default `true`.
    pub copyright: bool,
    /// W11, whether a sequence number outside `1..=set size` is written alone in `TPOS`.
    /// Default `false`.
    pub album_set_alone: bool,
    /// W11, the date's frame. Default [`DateFrame::Tdrl`].
    pub date: DateFrame,
    /// W12, the `COMM` language: three lower-case ISO 639-2 letters, or `XXX`. Default
    /// `XXX`.
    pub language: [u8; 3],
    /// W12, the description of the message's `COMM`. Default empty.
    pub message_description: String,
    /// W12, the description of the extra message's `COMM`. Default `extra message`.
    pub extra_message_description: String,
    /// W12, the `TXXX` description of the arranger. Default `arranger`.
    pub arranger_description: String,
    /// W12, the `TXXX` description of the album artist. Default `album artist`.
    pub album_artist_description: String,
}

/// The frame IDs in the order of the table of 8.3.5.
pub const TABLE_ORDER: [[u8; 4]; 18] = [
    *b"TIT2", *b"TPE1", *b"TEXT", *b"TCOM", *b"TIPL", *b"COMM", *b"TSOT", *b"TSOP", *b"TRCK", *b"TSRC",
    *b"TCON", *b"TALB", *b"TPE2", *b"TXXX", *b"TPUB", *b"TCOP", *b"TPOS", *b"TDRL",
];

impl Default for Id3Options {
    fn default() -> Self {
        let mut order = TABLE_ORDER.to_vec();
        order.push(*b"TDRC");
        Id3Options {
            footer: true,
            padding: 0,
            frame_order: order,
            encoding: Id3Encoding::Utf8,
            terminate_last: false,
            control_replacement: None,
            split: None,
            arranger: ArrangerFrame::Tipl,
            join_messages: None,
            phonetic: false,
            track_count: true,
            album_artist: AlbumArtistFrame::None,
            copyright: true,
            album_set_alone: false,
            date: DateFrame::Tdrl,
            language: *b"XXX",
            message_description: String::new(),
            extra_message_description: "extra message".to_owned(),
            arranger_description: "arranger".to_owned(),
            album_artist_description: "album artist".to_owned(),
        }
    }
}

impl Id3Options {
    /// The defaults.
    pub fn new() -> Self {
        Self::default()
    }

    /// These options with or without a footer (W4).
    pub fn with_footer(self, write: bool) -> Self {
        Id3Options { footer: write, ..self }
    }

    /// These options with `bytes` of padding (W5).
    pub fn with_padding(self, bytes: u32) -> Self {
        Id3Options { padding: bytes, ..self }
    }

    /// These options with frame order `order` (W6).
    pub fn with_frame_order(self, order: Vec<[u8; 4]>) -> Self {
        Id3Options { frame_order: order, ..self }
    }

    /// These options with text encoding `encoding` (W7).
    pub fn with_encoding(self, encoding: Id3Encoding) -> Self {
        Id3Options { encoding, ..self }
    }

    /// These options with or without a terminator after the last string (W8).
    pub fn with_terminate_last(self, terminate: bool) -> Self {
        Id3Options { terminate_last: terminate, ..self }
    }

    /// These options with control characters replaced by `replacement`, or removed (W9).
    pub fn with_control_replacement(self, replacement: Option<char>) -> Self {
        Id3Options { control_replacement: replacement, ..self }
    }

    /// These options with values split at `separator` (W10).
    pub fn with_split(self, separator: Option<&str>) -> Self {
        Id3Options { split: separator.map(str::to_owned), ..self }
    }

    /// These options with the arranger in `frame` (W11).
    pub fn with_arranger(self, frame: ArrangerFrame) -> Self {
        Id3Options { arranger: frame, ..self }
    }

    /// These options with the messages joined by `separator`, or one `COMM` each (W11 and W12).
    pub fn with_join_messages(self, separator: Option<&str>) -> Self {
        Id3Options { join_messages: separator.map(str::to_owned), ..self }
    }

    /// These options with or without the phonetic title and performer (W11).
    pub fn with_phonetic(self, write: bool) -> Self {
        Id3Options { phonetic: write, ..self }
    }

    /// These options with or without the track count in `TRCK` (W11).
    pub fn with_track_count(self, write: bool) -> Self {
        Id3Options { track_count: write, ..self }
    }

    /// These options with the album or disc artist in `frame` (W11).
    pub fn with_album_artist(self, frame: AlbumArtistFrame) -> Self {
        Id3Options { album_artist: frame, ..self }
    }

    /// These options with or without the copyright (W11).
    pub fn with_copyright(self, write: bool) -> Self {
        Id3Options { copyright: write, ..self }
    }

    /// These options with or without a sequence number outside the set size alone in `TPOS` (W11).
    pub fn with_album_set_alone(self, write: bool) -> Self {
        Id3Options { album_set_alone: write, ..self }
    }

    /// These options with the date in `frame` (W11).
    pub fn with_date(self, frame: DateFrame) -> Self {
        Id3Options { date: frame, ..self }
    }

    /// These options with `COMM` language `language` (W12).
    pub fn with_language(self, language: [u8; 3]) -> Self {
        Id3Options { language, ..self }
    }

    /// These options with the message's `COMM` description `description` (W12).
    pub fn with_message_description(self, description: &str) -> Self {
        Id3Options { message_description: description.to_owned(), ..self }
    }

    /// These options with the extra message's `COMM` description `description` (W12).
    pub fn with_extra_message_description(self, description: &str) -> Self {
        Id3Options { extra_message_description: description.to_owned(), ..self }
    }

    /// These options with the arranger's `TXXX` description `description` (W12).
    pub fn with_arranger_description(self, description: &str) -> Self {
        Id3Options { arranger_description: description.to_owned(), ..self }
    }

    /// These options with the album artist's `TXXX` description `description` (W12).
    pub fn with_album_artist_description(self, description: &str) -> Self {
        Id3Options { album_artist_description: description.to_owned(), ..self }
    }
}

/// Whether `c` is a control character (8.3.4 item 1).
fn control(c: char) -> bool {
    matches!(c, '\u{0}'..='\u{1F}' | '\u{7F}'..='\u{9F}')
}

/// A string with its control characters removed or replaced (8.3.4 item 1); line breaks
/// stay where `newlines` allows them.
fn clean(s: &str, newlines: bool, o: &Id3Options) -> String {
    s.chars()
        .filter_map(|c| match c {
            '\n' if newlines => Some(c),
            c if control(c) => o.control_replacement.filter(|r| !control(*r) || (*r == '\n' && newlines)),
            c => Some(c),
        })
        .collect()
}

/// The strings of a frame body, encoded with one encoding (8.3.4): the encoding byte, then
/// each string, separated by terminators, the last one terminated when `last` says.
fn text_body(prefix: &[u8], strings: &[String], last: bool, o: &Id3Options) -> Vec<u8> {
    // `prefix` goes after the encoding byte (the `COMM` language).
    let latin1 = strings.iter().all(|s| s.chars().all(|c| c == '\n' || ('\u{20}'..='\u{FF}').contains(&c)));
    let enc = match o.encoding {
        Id3Encoding::Utf8 => 3,
        Id3Encoding::Latin1OrUtf8 if latin1 => 0,
        Id3Encoding::Latin1OrUtf8 => 3,
        Id3Encoding::Utf16Le | Id3Encoding::Utf16Be => 1,
        Id3Encoding::Utf16BeNoBom => 2,
    };
    let mut out = vec![enc];
    out.extend_from_slice(prefix);
    for (i, s) in strings.iter().enumerate() {
        match o.encoding {
            Id3Encoding::Utf16Le => {
                out.extend(std::iter::once(0xFEFF).chain(s.encode_utf16()).flat_map(u16::to_le_bytes))
            }
            Id3Encoding::Utf16Be => {
                out.extend(std::iter::once(0xFEFF).chain(s.encode_utf16()).flat_map(u16::to_be_bytes))
            }
            Id3Encoding::Utf16BeNoBom => out.extend(s.encode_utf16().flat_map(u16::to_be_bytes)),
            // Every code point is below 0x100 (checked above).
            _ if enc == 0 => out.extend(s.chars().map(|c| c as u8)),
            _ => out.extend_from_slice(s.as_bytes()),
        }
        if i + 1 < strings.len() || last {
            out.extend_from_slice(if enc == 1 || enc == 2 { &[0, 0] } else { &[0] });
        }
    }
    out
}

/// A synchsafe integer (8.3.1); `v` is below 2^28.
fn synchsafe(v: usize) -> [u8; 4] {
    [(v >> 21) as u8 & 0x7F, (v >> 14) as u8 & 0x7F, (v >> 7) as u8 & 0x7F, v as u8 & 0x7F]
}

/// Whether a tag of a 10-byte header, `frames` bytes of frames and `tail` bytes of footer
/// or padding is at most [`MAX_TAG_BYTES`] (#343).
fn fits(frames: usize, tail: usize) -> bool {
    10usize.saturating_add(frames).saturating_add(tail) <= MAX_TAG_BYTES
}

/// The tag of `set` (8.3), or `None` when it has no frame (8.2.6 item 3).
///
/// # Errors
///
/// [`Error::InvalidArgument`] if the tag would exceed [`MAX_TAG_BYTES`] or the language
/// is not three ASCII letters.
pub(super) fn tag(set: &TagSet, o: &Id3Options) -> Result<Option<Vec<u8>>, Error> {
    if !(o.language.iter().all(u8::is_ascii_lowercase) || o.language == *b"XXX") {
        return Err(Error::InvalidArgument("the COMM language is three lower-case letters or XXX"));
    }
    let values = |item| -> Vec<String> {
        let v = set.get(item).iter().map(|s| clean(s, false, o));
        match &o.split {
            Some(sep) if !sep.is_empty() => {
                v.flat_map(|s| s.split(sep.as_str()).map(str::to_owned).collect::<Vec<_>>()).collect()
            }
            _ => v.collect(),
        }
    };
    let digits = |s: &String| !s.is_empty() && s.bytes().all(|b| b.is_ascii_digit());
    let mut frames: Vec<([u8; 4], Vec<u8>, Option<String>)> = Vec::new();
    let text = |frames: &mut Vec<([u8; 4], Vec<u8>, Option<String>)>, id: &[u8; 4], strings: Vec<String>| {
        if !strings.is_empty() {
            frames.push((*id, text_body(&[], &strings, o.terminate_last, o), None));
        }
    };
    let txxx = |frames: &mut Vec<([u8; 4], Vec<u8>, Option<String>)>, description: &str, value: &str| {
        let strings = [clean(description, false, o), clean(value, false, o)];
        let key = Some(format!("TXXX {}", strings[0]));
        frames.push((*b"TXXX", text_body(&[], &strings, o.terminate_last, o), key));
    };
    // A `TXXX` holds one value (8.3.6).
    let one_txxx = |frames: &mut Vec<([u8; 4], Vec<u8>, Option<String>)>,
                    description: &str,
                    values: &[String]| match values {
        [] => Ok(()),
        [v] => {
            txxx(frames, description, v);
            Ok(())
        }
        _ => Err(Error::InvalidArgument("a TXXX item holds one value")),
    };
    let comm = |frames: &mut Vec<([u8; 4], Vec<u8>, Option<String>)>, description: &str, value: &str| {
        let strings = [clean(description, false, o), clean(value, true, o)];
        let key = Some(format!("COMM {}", strings[0]));
        frames.push((*b"COMM", text_body(&o.language, &strings, o.terminate_last, o), key));
    };
    text(&mut frames, b"TIT2", values(TagItem::TrackTitle));
    text(&mut frames, b"TPE1", values(TagItem::Performer));
    text(&mut frames, b"TEXT", values(TagItem::Songwriter));
    text(&mut frames, b"TCOM", values(TagItem::Composer));
    let arrangers = values(TagItem::Arranger);
    match o.arranger {
        // One frame of function and name pairs (8.3.3 item 3).
        ArrangerFrame::Tipl => text(
            &mut frames,
            b"TIPL",
            arrangers.into_iter().flat_map(|a| ["arranger".to_owned(), a]).collect(),
        ),
        ArrangerFrame::Txxx => one_txxx(&mut frames, &o.arranger_description, &arrangers)?,
    }
    let message = set.get(TagItem::Message).join("\n");
    let extra = set.get(TagItem::ExtraMessage).join("\n");
    match &o.join_messages {
        Some(sep) if !message.is_empty() && !extra.is_empty() => {
            comm(&mut frames, &o.message_description, &format!("{message}{sep}{extra}"))
        }
        _ => {
            if !set.get(TagItem::Message).is_empty() {
                comm(&mut frames, &o.message_description, &message);
            }
            if !set.get(TagItem::ExtraMessage).is_empty() {
                let d = if o.join_messages.is_some() {
                    &o.message_description
                } else {
                    &o.extra_message_description
                };
                comm(&mut frames, d, &extra);
            }
        }
    }
    if o.phonetic {
        text(&mut frames, b"TSOT", values(TagItem::TitlePhonetic));
        text(&mut frames, b"TSOP", values(TagItem::PerformerPhonetic));
    }
    if let Some(n) = set.get(TagItem::TrackNumber).first().filter(|n| digits(n)) {
        let count = set.get(TagItem::TrackCount).first().filter(|c| o.track_count && digits(c));
        text(&mut frames, b"TRCK", vec![count.map_or(n.clone(), |c| format!("{n}/{c}"))]);
    }
    text(&mut frames, b"TSRC", values(TagItem::Isrc));
    text(&mut frames, b"TCON", values(TagItem::Genre));
    text(&mut frames, b"TALB", values(TagItem::Album));
    let artists = values(TagItem::AlbumArtist);
    match o.album_artist {
        AlbumArtistFrame::None => {}
        AlbumArtistFrame::Tpe2 => text(&mut frames, b"TPE2", artists),
        AlbumArtistFrame::Txxx => one_txxx(&mut frames, &o.album_artist_description, &artists)?,
    }
    for (d, v) in set.custom() {
        txxx(&mut frames, d, v);
    }
    text(&mut frames, b"TPUB", values(TagItem::Publisher));
    let date = set.get(TagItem::Date).first();
    let year =
        date.and_then(|d| d.get(..4)).filter(|y| y.bytes().all(|b| b.is_ascii_digit()) && *y != "0000");
    if let (true, Some(year)) = (o.copyright, year) {
        let v: Vec<String> = values(TagItem::Copyright).into_iter().map(|c| format!("{year} {c}")).collect();
        text(&mut frames, b"TCOP", v);
    }
    if let Some(q) = set.get(TagItem::DiscNumber).first().filter(|q| digits(q)) {
        let z = set.get(TagItem::DiscCount).first().filter(|z| digits(z));
        text(&mut frames, b"TPOS", vec![z.map_or(q.clone(), |z| format!("{q}/{z}"))]);
    }
    let date_id = match o.date {
        DateFrame::Tdrl => b"TDRL",
        DateFrame::Tdrc => b"TDRC",
    };
    text(&mut frames, date_id, date.map(|d| vec![clean(d, false, o)]).unwrap_or_default());

    if frames.is_empty() {
        return Ok(None);
    }
    // At most one `TXXX` per description and one `COMM` per language and description
    // (8.3.3 item 3, 8.3.6).
    let mut keys: Vec<&String> = frames.iter().filter_map(|(_, _, k)| k.as_ref()).collect();
    let n = keys.len();
    keys.sort_unstable();
    keys.dedup();
    if keys.len() != n {
        return Err(Error::InvalidArgument("two TXXX or COMM frames have one description"));
    }
    // W6: stable, so frames with one ID keep the order they were made in.
    frames.sort_by_key(|(id, _, _)| o.frame_order.iter().position(|x| x == id).unwrap_or(usize::MAX));
    // After the frames, a footer of 10 bytes or the padding (8.3.1). Each frame is checked
    // with all before it, so the last check is the whole tag's.
    let tail = if o.footer { 10 } else { o.padding as usize };
    let too_large = Err(Error::InvalidArgument("the tag is larger than MAX_TAG_BYTES"));
    let mut body = Vec::new();
    for (id, b, _) in &frames {
        if !fits(body.len() + 10 + b.len(), tail) {
            return too_large;
        }
        body.extend_from_slice(id);
        body.extend_from_slice(&synchsafe(b.len()));
        body.extend_from_slice(&[0, 0]);
        body.extend_from_slice(b);
    }
    if !o.footer {
        body.resize(body.len() + tail, 0);
    }
    let flags = if o.footer { 0x10 } else { 0 };
    let header = |id: &[u8; 3]| {
        let mut h = id.to_vec();
        h.extend_from_slice(&[4, 0, flags]);
        h.extend_from_slice(&synchsafe(body.len()));
        h
    };
    let mut out = header(b"ID3");
    out.extend_from_slice(&body);
    if o.footer {
        out.extend_from_slice(&header(b"3DI"));
    }
    Ok(Some(out))
}

/// Checks a caller's tag (W3), at most [`MAX_TAG_BYTES`]: an ID3v2.3 tag by
/// [`check_raw_v23`], or an ID3v2.4 header whose synchsafe size, with the footer its flags
/// announce, is the tag's length (8.3.1).
pub(super) fn check_raw(tag: &[u8]) -> Result<(), Error> {
    if tag.starts_with(b"ID3\x03") {
        return check_raw_v23(tag);
    }
    let bad = Err(Error::InvalidArgument("not an ID3v2.4 tag of its stated size"));
    if tag.len() < 10
        || tag.len() > MAX_TAG_BYTES
        || &tag[..5] != b"ID3\x04\x00"
        || tag[6..10].iter().any(|b| b & 0x80 != 0)
    {
        return bad;
    }
    let size = tag[6..10].iter().fold(0usize, |v, &b| v << 7 | usize::from(b));
    let footer = if tag[5] & 0x10 != 0 { 10 } else { 0 };
    if 10 + size + footer != tag.len() {
        return bad;
    }
    Ok(())
}

/// Checks a caller's ID3v2.3 tag `T` of `m` bytes by its header alone (specification 8,
/// 8.2.6 item 4, sacd-spec e069569; W3): `m >= 10`, `ID3`, version 03 00, the low five
/// flag bits 0, and four bytes each below 0x80 whose 28-bit size is `m - 10` (no footer).
/// The frames are not checked; the tag is written unchanged. [`MAX_TAG_BYTES`] bounds it
/// as it does every tag.
fn check_raw_v23(tag: &[u8]) -> Result<(), Error> {
    if tag.len() < 10
        || tag.len() > MAX_TAG_BYTES
        || &tag[..5] != b"ID3\x03\x00"
        || tag[5] & 0x1F != 0
        || tag[6..10].iter().any(|b| b & 0x80 != 0)
        || tag[6..10].iter().fold(0usize, |v, &b| v << 7 | usize::from(b)) != tag.len() - 10
    {
        return Err(Error::InvalidArgument("not an ID3v2.3 tag of its stated size"));
    }
    Ok(())
}

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

    /// The size check at its boundary, header and footer or padding counted (#343); the
    /// whole tags are in `tests/output_tags.rs`, too large for Miri.
    #[test]
    fn fits_counts_header_and_tail() {
        assert!(fits(MAX_TAG_BYTES - 20, 10));
        assert!(!fits(MAX_TAG_BYTES - 19, 10));
        assert!(fits(MAX_TAG_BYTES - 10, 0));
        assert!(!fits(MAX_TAG_BYTES - 9, 0));
        assert!(fits(0, MAX_TAG_BYTES - 10));
        assert!(!fits(0, MAX_TAG_BYTES - 9));
        assert!(!fits(usize::MAX, 10));
        assert!(!fits(10, usize::MAX));
    }
}