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
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
//! ID3 file handling implementation
//!
//! Provides [`ID3`], a standalone ID3v2 tag file type that can load and save
//! ID3 tags independently of any specific audio format. Also provides
//! [`ID3FileType`], which wraps `ID3` to implement the [`FileType`] trait
//! for use with the dynamic format detection system.
use crate::id3::frames::{Frame, FrameRegistry};
#[cfg(feature = "async")]
use crate::id3::id3v1::find_id3v1;
use crate::id3::id3v1::find_id3v1_from_reader;
use crate::id3::specs::ID3Header;
use crate::id3::tags::{ID3SaveConfig, ID3Tags};
use crate::id3::util::BitPaddedInt;
use crate::tags::PaddingInfo;
use crate::util::{delete_bytes, insert_bytes, read_full};
use crate::{AudexError, FileType, Metadata, ReadWriteSeek, Result, StreamInfo, Tags};
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
use std::time::Duration;
#[cfg(feature = "async")]
use crate::util::{delete_bytes_async, insert_bytes_async};
#[cfg(feature = "async")]
use tokio::fs::{File as TokioFile, OpenOptions as TokioOpenOptions};
/// Controls how ID3v1 tags are handled during save operations
///
/// ID3v1 is a legacy tag format stored in the last 128 bytes of an MP3 file.
/// This enum controls whether ID3v1 tags are created, updated, or removed
/// when saving ID3v2 tags.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ID3v1SaveOptions {
/// Remove any existing ID3v1 tag from the file
REMOVE = 0,
/// Update an existing ID3v1 tag if present, but don't create one
UPDATE = 1,
/// Create a new ID3v1 tag or update an existing one
CREATE = 2,
}
/// Standalone ID3v2 tag container with file I/O
///
/// Wraps [`ID3Tags`] with file loading and saving capabilities. Implements
/// both [`Tags`] (for key-value access) and [`Metadata`] (for file
/// I/O operations).
///
/// The `tags` field provides direct access to the underlying frame dictionary
/// for advanced frame-level manipulation beyond what the `Tags` trait offers.
#[derive(Debug)]
pub struct ID3 {
/// Internal ID3Tags frame container — publicly accessible for direct
/// frame dictionary access (e.g. `id3.tags.dict`)
pub tags: ID3Tags,
/// File path this tag was loaded from (used for save operations)
pub filename: Option<String>,
/// Parsed header from the loaded file
_header: Option<ID3Header>,
/// ID3 version as (2, major, revision) for ID3v2 — e.g. (2, 4, 0) for ID3v2.4.
/// Set to (1, 1, 0) when only an ID3v1 tag is present.
_version: (u8, u8, u8),
/// Raw byte data of frames not recognized by the parser
pub unknown_frames: Vec<Vec<u8>>,
/// Padding bytes found after the last frame in the loaded tag
_padding: usize,
/// Strict-parsing mode (defaults to `true`)
pub pedantic: bool,
/// Cached text values extracted from frames, enabling `Tags::get`
/// to return borrowed `&[String]` slices. Kept in sync with the
/// underlying frame dictionary by `set`, `remove`, and load paths.
values_cache: HashMap<String, Vec<String>>,
}
/// Implement Tags trait for ID3 to provide basic tag functionality
impl crate::Tags for ID3 {
fn get(&self, key: &str) -> Option<&[String]> {
self.values_cache.get(key).map(|v| v.as_slice())
}
fn set(&mut self, key: &str, values: Vec<String>) {
self.tags.set(key, values);
// Re-read from the frame to capture any encoding transformations
if let Some(text_values) = self.tags.get_text_values(key) {
self.values_cache.insert(key.to_string(), text_values);
}
}
fn remove(&mut self, key: &str) {
self.tags.remove(key);
self.values_cache.remove(key);
}
fn keys(&self) -> Vec<String> {
self.tags.keys()
}
fn pprint(&self) -> String {
format!("ID3 tags: {} frames", Tags::keys(self).len())
}
}
/// Implement Metadata trait for ID3 tag handling
impl Metadata for ID3 {
type Error = AudexError;
fn new() -> Self
where
Self: Sized,
{
Self::new()
}
fn load_from_fileobj(filething: &mut crate::util::AnyFileThing) -> Result<Self>
where
Self: Sized,
{
// Convert AnyFileThing to a path-like interface
let mut instance = Self::new();
// Get the path from the file thing
let path = filething.display_name();
instance.load(path, None, true, 4, true)?;
Ok(instance)
}
fn save_to_fileobj(&self, _filething: &mut crate::util::AnyFileThing) -> Result<()> {
// For ID3, saving requires mutable self, which we don't have here
// Users should use the save() method on the ID3 instance instead
Err(AudexError::Unsupported(
"Use save() method directly instead".to_string(),
))
}
fn delete_from_fileobj(filething: &mut crate::util::AnyFileThing) -> Result<()>
where
Self: Sized,
{
let path = filething.display_name();
clear(path, true, true)
}
}
impl ID3 {
/// Create a new empty ID3 instance
pub fn new() -> Self {
Self {
tags: ID3Tags::new(),
filename: None,
_header: None,
_version: (2, 4, 0), // Default to ID3v2.4.0
unknown_frames: Vec::new(),
_padding: 0,
pedantic: true, // Pedantic mode for strict parsing
values_cache: HashMap::new(),
}
}
/// Rebuild the text values cache from the current frame dictionary.
/// Called after loading or any bulk frame mutation to keep the cache
/// in sync with the underlying data.
fn refresh_values_cache(&mut self) {
self.values_cache.clear();
for key in self.tags.frame_keys() {
if let Some(values) = self.tags.get_text_values(&key) {
self.values_cache.insert(key, values);
}
}
}
/// Creates a new `ID3` instance by reading and parsing tags from the given file.
///
/// The file is loaded with default settings: ID3v2.4, pedantic mode enabled,
/// and frame translation turned on.
pub fn with_file<P: AsRef<Path>>(filething: P) -> Result<Self> {
let mut instance = Self::new();
instance.load(filething, None, true, 4, true)?;
Ok(instance)
}
/// Loads and parses ID3 tags from the given file path.
///
/// This is an alias for [`with_file`](Self::with_file).
pub fn load_from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
Self::with_file(path)
}
/// Loads and parses ID3 tags from the given file path (legacy compatibility alias).
///
/// Delegates to [`load_from_file`](Self::load_from_file).
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> {
Self::load_from_file(path)
}
/// Returns a reference to the parsed ID3 tags.
///
/// Currently always returns `Some`; the `Option` wrapper is kept for API
/// compatibility with callers that expect it.
pub fn tags(&self) -> Option<&ID3Tags> {
Some(&self.tags)
}
/// Returns a mutable reference to the parsed ID3 tags, allowing in-place edits.
///
/// Currently always returns `Some`; the `Option` wrapper is kept for API
/// compatibility with callers that expect it.
pub fn tags_mut(&mut self) -> Option<&mut ID3Tags> {
Some(&mut self.tags)
}
/// Simple save method without parameters
pub fn save(&mut self) -> Result<()> {
debug_event!("saving ID3 file");
// Use the version from tags if available, otherwise default to v2.4
// Only v2.3 and v2.4 are supported for writing
let tag_version = self.tags.version().1;
let v2_version = if tag_version == 3 || tag_version == 4 {
tag_version
} else {
4 // Default to v2.4 for unsupported versions
};
self.save_with_options(None, ID3v1SaveOptions::UPDATE, v2_version, Some("/"))
}
/// Removes all ID3v1 and ID3v2 tags from the associated file.
///
/// Delegates to [`delete_full`](Self::delete_full) with both `delete_v1`
/// and `delete_v2` set to `true`, using the stored filename.
pub fn clear(&mut self) -> Result<()> {
self.delete_full(None, true, true)
}
/// Full save method with all parameters
pub fn save_with_options(
&mut self,
filething: Option<&Path>,
v1: ID3v1SaveOptions,
v2_version: u8,
v23_sep: Option<&str>,
) -> Result<()> {
self.save_with_padding(filething, v1, v2_version, v23_sep, None)
}
/// Internal save method with padding support
fn save_with_padding(
&mut self,
filething: Option<&Path>,
v1: ID3v1SaveOptions,
v2_version: u8,
v23_sep: Option<&str>,
padding: Option<fn(&PaddingInfo) -> i64>,
) -> Result<()> {
let filename = filething
.map(Path::to_path_buf)
.or_else(|| self.filename.as_ref().map(PathBuf::from));
if let Some(path) = filename {
trace_event!(
path = %path.display(),
v2_version = v2_version,
"writing ID3 tags to file"
);
let mut file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(&path)?;
self.save_to_writer_inner(&mut file, v1, v2_version, v23_sep, padding)
} else {
warn_event!("no filename provided for ID3 save");
Err(AudexError::InvalidData("No filename provided".to_string()))
}
}
/// Core save implementation that writes to any seekable reader/writer.
///
/// This contains the shared logic used by both file-based saving
/// (`save_with_padding`) and writer-based saving (`save_to_writer`).
/// The caller is responsible for providing an already-opened handle.
///
/// For concrete `Sized + 'static` types (e.g. `File`), this method uses
/// `insert_bytes` / `delete_bytes` for efficient in-place byte
/// manipulation.
fn save_to_writer_inner<W: Read + Write + Seek + 'static>(
&mut self,
writer: &mut W,
v1: ID3v1SaveOptions,
v2_version: u8,
v23_sep: Option<&str>,
padding: Option<fn(&PaddingInfo) -> i64>,
) -> Result<()> {
// Try to read existing header
let old_size = {
writer.seek(SeekFrom::Start(0))?;
let mut header_data = [0u8; 10];
match writer.read_exact(&mut header_data) {
Ok(()) if &header_data[0..3] == b"ID3" => {
let header = ID3Header::from_bytes(&header_data)?;
header.size
}
_ => 0, // No existing header
}
};
// Prepare new tag data - use 0 for available to allow natural shrinking
let data = self._prepare_data(
writer, 0, 0, // Don't constrain by old size, allow natural shrinking
v2_version, v23_sep, padding,
)?;
let new_size = data.len();
// Widen to u64 before adding the 10-byte header length to prevent
// overflow on 32-bit platforms where usize is 32 bits
let old_total_size = if old_size > 0 {
let total = (old_size as u64).checked_add(10).ok_or_else(|| {
AudexError::InvalidData(
"Old tag size overflow when adding header length".to_string(),
)
})?;
usize::try_from(total).map_err(|_| {
AudexError::InvalidData(format!(
"Old tag total size ({} bytes) exceeds addressable range",
total
))
})?
} else {
0
}; // Include 10-byte header in old size only if tag exists
// Adjust file size if needed
if old_total_size < new_size {
// Need to insert bytes
insert_bytes(
writer,
(new_size - old_total_size) as u64,
old_total_size as u64,
None,
)?;
} else if old_total_size > new_size {
// Need to delete bytes
delete_bytes(
writer,
(old_total_size - new_size) as u64,
new_size as u64,
None,
)?;
}
// Write the new tag data
writer.seek(SeekFrom::Start(0))?;
writer.write_all(&data)?;
// Handle ID3v1 tag
self.__save_v1(writer, v1)?;
Ok(())
}
/// Writer-based save that operates on any `Read + Write + Seek` trait
/// object.
///
/// Unlike `save_to_writer_inner`, this works with unsized trait objects
/// (`dyn ReadWriteSeek`) by copying all data into an intermediate
/// `Cursor<Vec<u8>>`, performing the in-place tag update there (which
/// supports both growing and shrinking via `insert_bytes` /
/// `delete_bytes`), and then writing the result back to the original
/// writer.
///
/// # Limitation: trailing zeros after tag shrink
///
/// When the updated tag is smaller than the original, the stream's
/// logical content shrinks but the writer cannot be truncated (trait
/// objects do not expose `set_len`). The stale tail is overwritten
/// with zeros to prevent data leakage, but the stream retains its
/// original length. This is safe for all supported audio formats
/// because the file structure's own size fields are authoritative —
/// parsers ignore trailing zeros beyond the declared boundaries.
///
/// If exact file sizing is required, use the file-path-based
/// [`save`](Self::save) method instead, which can truncate the
/// underlying file.
///
/// # Memory usage
///
/// This method reads the entire file into an in-memory buffer, applies
/// tag modifications on a `Cursor`, then writes the result back. Peak
/// memory consumption is approximately **2x the file size** (the read
/// buffer plus the modified output). A 512 MB hard ceiling is enforced
/// before allocation. For large files, prefer the file-path-based
/// [`save`](Self::save) method which operates directly on the file
/// handle without buffering the full stream.
fn save_to_writer_dyn(
&mut self,
writer: &mut dyn ReadWriteSeek,
v1: ID3v1SaveOptions,
v2_version: u8,
v23_sep: Option<&str>,
padding: Option<fn(&PaddingInfo) -> i64>,
) -> Result<()> {
// Determine total file size before buffering to prevent OOM
// on very large files. This path reads the entire stream into
// memory, so enforce a hard ceiling independent of tag-size limits.
// 512 MB is generous for any audio file that needs in-memory saving.
const MAX_IN_MEMORY_FILE: u64 = 512 * 1024 * 1024;
let file_size = writer.seek(SeekFrom::End(0))?;
if file_size > MAX_IN_MEMORY_FILE {
return Err(crate::AudexError::InvalidData(format!(
"file size ({} bytes) exceeds the {} byte limit for the in-memory save path; \
consider saving to a file directly instead",
file_size, MAX_IN_MEMORY_FILE
)));
}
// Read all existing data into memory.
// NOTE: This buffers the entire file, which is safe given the size guard
// above. A future optimisation could stream the audio data tail instead
// of buffering it, reducing peak memory from O(file_size) to O(tag_size).
writer.seek(SeekFrom::Start(0))?;
let mut buf = Vec::new();
writer.read_to_end(&mut buf)?;
let original_len = buf.len();
// Perform the save on an in-memory Cursor (Sized + 'static)
let mut cursor = Cursor::new(buf);
self.save_to_writer_inner(&mut cursor, v1, v2_version, v23_sep, padding)?;
// Write the modified data back to the original writer
let result = cursor.into_inner();
let new_len = result.len();
writer.seek(SeekFrom::Start(0))?;
writer.write_all(&result)?;
// If the new data is shorter than the original, zero out the
// stale trailing bytes. We cannot call set_len() on a trait
// object, but zeroing ensures no leftover content leaks.
// Write in fixed-size chunks to avoid a single large allocation
// when the gap is very large.
if new_len < original_len {
let mut remaining = original_len - new_len;
const ZERO_CHUNK: [u8; 8192] = [0u8; 8192];
while remaining > 0 {
let chunk = remaining.min(ZERO_CHUNK.len());
writer.write_all(&ZERO_CHUNK[..chunk])?;
remaining -= chunk;
}
}
Ok(())
}
/// Get module name identifier
pub const MODULE: &'static str = "audex.id3";
/// Get ID3 version as (2, major, revision) -- e.g. (2, 4, 0) for ID3v2.4
pub fn version(&self) -> (u8, u8, u8) {
if let Some(ref header) = self._header {
(2, header.major_version, header.revision)
} else {
self._version
}
}
/// Set ID3 version
pub fn set_version(&mut self, value: (u8, u8, u8)) {
self._version = value;
}
/// Check if unsynchronization flag is set
pub fn f_unsynch(&self) -> bool {
if let Some(ref header) = self._header {
(header.flags & 0x80) != 0 // Check unsynchronization flag
} else {
false
}
}
/// Check if extended header flag is set
pub fn f_extended(&self) -> bool {
if let Some(ref header) = self._header {
(header.flags & 0x40) != 0 // Check extended header flag
} else {
false
}
}
/// Get total size of ID3 tag body (excludes the 10-byte header)
pub fn size(&self) -> u32 {
if let Some(ref header) = self._header {
header.size
} else {
0
}
}
/// Pre-load header hook for format-specific adjustments
fn _pre_load_header<R: Read + Seek>(&mut self, _fileobj: &mut R) -> Result<()> {
Ok(())
}
/// Load tags from a filename
///
/// Args:
/// filething: filename or file object to load tag data from
/// known_frames: map of frame IDs to Frame objects
/// translate: Update all tags to ID3v2.3/4 internally. If you
/// intend to save, this must be true or you have to
/// call update_to_v23() / update_to_v24() manually.
/// v2_version: if update_to_v23 or update_to_v24 get called (3 or 4)
/// load_v1: Load tags from ID3v1 header if present. If both
/// ID3v1 and ID3v2 headers are present, combine the tags from
/// the two, with ID3v2 having precedence.
#[cfg_attr(feature = "tracing", tracing::instrument(skip_all, fields(path = %filething.as_ref().display())))]
pub fn load<P: AsRef<Path>>(
&mut self,
filething: P,
known_frames: Option<HashMap<String, String>>,
translate: bool,
v2_version: u8,
load_v1: bool,
) -> Result<()> {
debug_event!("parsing ID3v2 tags");
if v2_version != 3 && v2_version != 4 {
return Err(AudexError::InvalidData(
"Only 3 and 4 possible for v2_version".to_string(),
));
}
// Store filename for later use
self.filename = Some(filething.as_ref().to_string_lossy().to_string());
// Clear existing state
self.unknown_frames.clear();
self._header = None;
self._padding = 0;
// Open file for reading
let mut file = File::open(filething.as_ref())?;
// Pre-load header hook for format-specific adjustments
self._pre_load_header(&mut file)?;
// Try to parse ID3v2 header
file.seek(SeekFrom::Start(0))?;
let mut header_data = [0u8; 10];
match file.read_exact(&mut header_data) {
Ok(()) if &header_data[0..3] == b"ID3" => {
let header = ID3Header::from_bytes(&header_data)?;
// Successfully parsed ID3v2 header
self._header = Some(header);
// Store known frames in header if provided
// Read the full tag data (header.size excludes the 10-byte tag header)
let size = self.size();
// Enforce the library-wide tag allocation ceiling
crate::limits::ParseLimits::default().check_tag_size(size as u64, "ID3v2")?;
// Cross-validate against actual stream size to prevent
// allocating far more memory than the file contains
let current_pos = file.stream_position()?;
let stream_end = file.seek(SeekFrom::End(0))?;
let available = stream_end.saturating_sub(current_pos);
if (size as u64) > available {
return Err(AudexError::ParseError(format!(
"ID3 tag size ({} bytes) exceeds remaining file data ({} bytes)",
size, available
)));
}
file.seek(SeekFrom::Start(current_pos))?;
let data = read_full(&mut file, size as usize)?;
// Skip the extended header if present. Its size is encoded
// in the first 4 bytes of the tag data:
// ID3v2.3: regular BE u32 (excludes the 4-byte size field)
// ID3v2.4: syncsafe u32 (includes the 4-byte size field)
let header_ref = self._header.as_ref().ok_or_else(|| {
AudexError::InvalidData("ID3 header not set during load".to_string())
})?;
let frame_data = if self.f_extended() && data.len() >= 4 {
let ext_size = if header_ref.major_version == 4 {
crate::id3::util::decode_synchsafe_int_checked(&data[0..4])? as usize
} else {
// Use checked_add to prevent overflow on 32-bit platforms
// when the declared size is near u32::MAX
let raw = u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as u64;
match raw.checked_add(4) {
Some(size) => size.min(data.len() as u64) as usize,
None => data.len(), // overflow: treat entire block as header
}
};
let skip = ext_size.min(data.len());
&data[skip..]
} else {
&data
};
let header_clone = header_ref.clone();
let remaining_data = self._read(&header_clone, frame_data)?;
self._padding = remaining_data.len();
// Load ID3v1 if requested and present — only read the tail
if load_v1 {
let v1v2_ver = if self.version().1 == 4 { 4 } else { 3 };
if let Ok((Some(frames), _offset)) =
find_id3v1_from_reader(&mut file, v1v2_ver, known_frames.clone())
{
for (_, frame) in frames {
// Only add if no existing frame with same hash key
if self.tags.getall(&frame.hash_key()).is_empty() {
let _ = self.tags.add(frame);
}
}
}
}
}
_ => {
// No ID3v2 header found, try ID3v1 if requested
if !load_v1 {
return Err(AudexError::ID3NoHeaderError);
}
// Only read the tail of the file for ID3v1 detection
match find_id3v1_from_reader(&mut file, v2_version, known_frames) {
Ok((frames, _offset)) => {
if let Some(frames) = frames {
self._version = (1, 1, 0); // ID3v1.1
for (_, frame) in frames {
if self.tags.getall(&frame.hash_key()).is_empty() {
let _ = self.tags.add(frame);
}
}
} else {
return Err(AudexError::InvalidData("No ID3 tags found".to_string()));
}
}
Err(e) => return Err(e),
}
}
}
// Log frame count summary after parsing
debug_event!(
frame_count = self.tags.frames_by_id.len(),
"ID3 frames loaded"
);
// Translate to requested version if needed
if translate {
if v2_version == 3 {
self.tags.update_to_v23();
self._version = (2, 3, 0);
} else {
self.tags.update_to_v24();
self._version = (2, 4, 0);
}
}
// Populate the text values cache so Tags::get returns real data
self.refresh_values_cache();
Ok(())
}
/// Prepare tag data for writing
fn _prepare_data<R, F>(
&self,
fileobj: &mut R,
start: u64,
available: usize,
v2_version: u8,
v23_sep: Option<&str>,
pad_func: Option<F>,
) -> Result<Vec<u8>>
where
R: Read + Seek,
F: FnOnce(&PaddingInfo) -> i64,
{
if v2_version != 3 && v2_version != 4 {
return Err(AudexError::InvalidData(
"Only 3 or 4 allowed for v2_version".to_string(),
));
}
// Create save config
let config = ID3SaveConfig::simple(v2_version, v23_sep.map(|s| s.to_string()))?;
// Write frame data
let framedata = self.tags.write_tags(&config)?;
let needed = framedata.len() + 10;
// Get file size for padding calculation
fileobj.seek(SeekFrom::End(0))?;
let file_end = fileobj.stream_position()?;
// Use saturating subtraction to avoid panic if start exceeds
// file_end due to corrupt metadata declaring an offset past EOF
let trailing_size = file_end.saturating_sub(start);
// Calculate padding using safe conversions to prevent silent
// truncation of large usize values when cast to i64.
let available_i64 = i64::try_from(available).map_err(|_| {
AudexError::InvalidData("Available space too large for padding calculation".to_string())
})?;
let needed_i64 = i64::try_from(needed).map_err(|_| {
AudexError::InvalidData("Needed space too large for padding calculation".to_string())
})?;
let trailing_i64 = i64::try_from(trailing_size).map_err(|_| {
AudexError::InvalidData("Trailing size too large for padding calculation".to_string())
})?;
let info = PaddingInfo::new(available_i64 - needed_i64, trailing_i64);
let new_padding = info.get_padding_with(pad_func);
if new_padding < 0 {
return Err(AudexError::InvalidData("Invalid padding".to_string()));
}
let padding_usize = usize::try_from(new_padding)
.map_err(|_| AudexError::InvalidData("Padding value out of usize range".to_string()))?;
let new_size = needed.checked_add(padding_usize).ok_or_else(|| {
AudexError::InvalidData(
"Tag size overflow: frame data + padding exceeds maximum".to_string(),
)
})?;
// The ID3v2 header stores the tag body size (excluding the 10-byte
// header) as a synchsafe 4-byte integer, which can represent at
// most 2^28 - 1 = 268_435_455 bytes. Reject sizes that would
// silently truncate during encoding.
let body_size = new_size - 10; // safe: needed >= 10, new_padding >= 0
if body_size > 0x0FFF_FFFF {
return Err(AudexError::InvalidData(format!(
"Tag body size {} exceeds the ID3v2 synchsafe maximum (268_435_455 bytes)",
body_size,
)));
}
// Create synchsafe size for header
let new_framesize =
BitPaddedInt::to_str(body_size as u32, Some(7), Some(true), Some(4), Some(4))?;
// Build header: ID3 + version + flags + size
let mut header = Vec::new();
header.extend_from_slice(b"ID3");
header.push(v2_version);
header.push(0); // revision
header.push(0); // flags
header.extend_from_slice(&new_framesize);
// Combine header + frame data + padding
let mut data = header;
data.extend_from_slice(&framedata);
// Add padding (zeros)
let padding_needed = new_size - data.len();
data.extend(vec![0u8; padding_needed]);
if new_size != data.len() {
return Err(AudexError::InvalidData(format!(
"ID3 tag size mismatch: expected {} bytes but produced {} bytes",
new_size,
data.len()
)));
}
Ok(data)
}
/// Save ID3v1 tag
fn __save_v1<W: Write + Seek + Read + 'static>(
&self,
f: &mut W,
v1: ID3v1SaveOptions,
) -> Result<()> {
// Only read the tail to check for an existing ID3v1 tag.
// Use i64::try_from to avoid silent truncation for files
// whose length exceeds i64::MAX.
let file_len = i64::try_from(f.seek(SeekFrom::End(0))?).map_err(|_| {
AudexError::InvalidData(
"File length exceeds i64::MAX; cannot safely compute seek positions".to_string(),
)
})?;
let (existing_tag, offset) = match find_id3v1_from_reader(f, 4, None) {
Ok((frames, offset)) => (frames.is_some(), offset),
// No tag found — offset is unused since the !existing_tag
// branch seeks to file_len directly, but use 0 for correctness.
Err(_) => (false, 0),
};
// Position at the ID3v1 location (or end of file).
// The offset from find_id3v1 is relative to the tail buffer end
// and is typically negative (e.g., -128).
// Use checked arithmetic to prevent signed integer overflow
// when combining file length with the tag offset.
let seek_pos = if existing_tag {
file_len.checked_add(offset).ok_or_else(|| {
AudexError::InvalidData(
"Seek position overflow when calculating ID3v1 tag location".to_string(),
)
})?
} else {
file_len
};
if seek_pos < 0 {
return Err(AudexError::InvalidData(
"ID3v1 seek position would be negative".to_string(),
));
}
f.seek(SeekFrom::Start(seek_pos as u64))?;
match v1 {
ID3v1SaveOptions::UPDATE if existing_tag => {
// Update existing ID3v1 tag
let id3v1_data = self.create_id3v1_data()?;
f.write_all(&id3v1_data)?;
}
ID3v1SaveOptions::CREATE => {
// Create or update ID3v1 tag
let id3v1_data = self.create_id3v1_data()?;
f.write_all(&id3v1_data)?;
}
ID3v1SaveOptions::REMOVE => {
// Remove the existing 128-byte ID3v1 tag by truncating the file
if existing_tag {
crate::util::delete_bytes(f, 128, seek_pos as u64, None)?;
}
}
_ => {
// UPDATE but no existing tag - do nothing
}
}
Ok(())
}
/// Remove tags from a file with full parameters
pub fn delete_full(
&mut self,
filething: Option<&Path>,
delete_v1: bool,
delete_v2: bool,
) -> Result<()> {
let filename = filething.or_else(|| self.filename.as_ref().map(Path::new));
if let Some(path) = filename {
clear(path, delete_v1, delete_v2)?;
}
// Reset in-memory state directly to avoid infinite recursion
// (self.clear() calls delete_full(), which calls self.clear(), ...)
self.tags = ID3Tags::new();
self.unknown_frames.clear();
self._header = None;
self._padding = 0;
Ok(())
}
/// Save ID3 tags to a writer that implements `Read + Write + Seek`.
///
/// The writer must contain the complete original file data (audio + any
/// existing tags). The method modifies the writer in-place, identical to
/// how [`save`](Self::save) modifies a file on disk.
pub fn save_to_writer(&mut self, writer: &mut dyn ReadWriteSeek) -> Result<()> {
let tag_version = self.tags.version().1;
let v2_version = if tag_version == 3 || tag_version == 4 {
tag_version
} else {
4
};
self.save_to_writer_dyn(
writer,
ID3v1SaveOptions::UPDATE,
v2_version,
Some("/"),
None,
)
}
/// Remove all ID3v1 and ID3v2 tags from a writer that implements
/// `Read + Write + Seek`.
///
/// The writer must contain the complete original file data.
pub fn clear_writer(&mut self, writer: &mut dyn ReadWriteSeek) -> Result<()> {
clear_from_writer(writer, true, true)
}
/// Create ID3v1 data from current tags
fn create_id3v1_data(&self) -> Result<Vec<u8>> {
let v1_data = crate::id3::id3v1::make_id3v1_from_dict(&self.tags.dict);
Ok(v1_data.to_vec())
}
/// Read frames from tag data
fn _read(&mut self, header: &crate::id3::specs::ID3Header, data: &[u8]) -> Result<Vec<u8>> {
let mut cursor = Cursor::new(data);
let mut remaining_data = Vec::new();
let mut frame_count: usize = 0;
// Legitimate tags rarely exceed a few hundred frames. Cap the total
// to prevent excessive allocations from crafted tags packed with
// millions of tiny frames (up to ~23 million within the 256 MB
// syncsafe ceiling). This matches the limit used in the tags-level
// parser for consistency.
const MAX_FRAMES_PER_TAG: usize = 50_000;
// Parse frames until we hit padding, end of data, or the frame cap
while (cursor.position() as usize) < data.len() && frame_count < MAX_FRAMES_PER_TAG {
let pos = cursor.position() as usize;
// Check if we've hit padding by examining the next 10 bytes.
// A full scan of all remaining bytes would be O(n) per iteration,
// potentially O(n²) overall. Checking a small prefix is sufficient
// since valid frame headers always have non-zero bytes.
let check_len = 10.min(data.len() - pos);
if data[pos..pos + check_len].iter().all(|&b| b == 0) {
remaining_data = data[pos..].to_vec();
break;
}
// Try to read frame header
match self._read_frame_header(&mut cursor, header.major_version) {
Ok(Some(frame_header)) => {
frame_count += 1;
// A zero-size frame indicates the start of padding; stop parsing.
let frame_size = frame_header.size as usize;
if frame_size == 0 {
remaining_data = data[pos..].to_vec();
break;
}
if (cursor.position() as usize) + frame_size > data.len() {
// Invalid frame size, treat as padding
remaining_data = data[pos..].to_vec();
break;
}
let mut frame_data = vec![0u8; frame_size];
cursor.read_exact(&mut frame_data)?;
// Try to parse the frame
match self._parse_frame(&frame_header, &frame_data) {
Ok(Some(frame)) => {
let _ = self.tags.add(frame);
}
Ok(None) => {
// Frame parsing succeeded but returned no frame (e.g., empty frame)
}
Err(_e) => {
// Unknown or invalid frame, store as unknown.
// Note: `_parse_frame` takes `&[u8]` (immutable reference),
// so `frame_data` still contains the original pre-processing
// bytes read from the cursor. Any unsync decoding or
// decompression happens on internal copies within `_parse_frame`,
// ensuring we store the untouched raw bytes here.
let mut unknown_frame = Vec::new();
unknown_frame.extend_from_slice(frame_header.frame_id.as_bytes());
// ID3v2.4 requires synchsafe frame sizes; v2.3 uses plain big-endian.
// Safety: frame_data was allocated from frame_header.size (u32),
// so its length always fits in u32. Use try_from as a defensive
// guard in case the allocation logic is ever changed.
let frame_len = u32::try_from(frame_data.len()).map_err(|_| {
AudexError::InvalidData(format!(
"Unknown frame data length {} exceeds u32::MAX",
frame_data.len(),
))
})?;
let size_bytes = if frame_header.version == (2, 4) {
crate::id3::util::encode_synchsafe_int(frame_len)
.unwrap_or(frame_len.to_be_bytes())
} else {
frame_len.to_be_bytes()
};
unknown_frame.extend_from_slice(&size_bytes);
unknown_frame.extend_from_slice(
&frame_header
.flags
.to_raw(frame_header.version)
.to_be_bytes(),
);
unknown_frame.extend_from_slice(&frame_data);
self.unknown_frames.push(unknown_frame);
}
}
}
Ok(None) => {
// No more valid frame headers, rest is padding
remaining_data = data[pos..].to_vec();
break;
}
Err(_) => {
// Invalid frame header, treat rest as padding
remaining_data = data[pos..].to_vec();
break;
}
}
}
Ok(remaining_data)
}
/// Read frame header from cursor
fn _read_frame_header(
&self,
cursor: &mut Cursor<&[u8]>,
version: u8,
) -> Result<Option<crate::id3::specs::FrameHeader>> {
use crate::id3::specs::FrameHeader;
let pos = cursor.position() as usize;
let data = cursor.get_ref();
if pos >= data.len() {
return Ok(None);
}
// Check for valid frame ID based on version
let id_len = if version == 2 { 3 } else { 4 };
if pos + id_len > data.len() {
return Ok(None);
}
let frame_id = String::from_utf8_lossy(&data[pos..pos + id_len]);
// Validate frame ID: spec requires uppercase ASCII letters and digits only.
// Accepting lowercase would misidentify corrupted data as valid frames.
if frame_id
.chars()
.any(|c| !(c.is_ascii_uppercase() || c.is_ascii_digit()))
{
return Ok(None);
}
// Read frame header based on version
match version {
2 => {
if pos + 6 > data.len() {
return Ok(None);
}
let size_bytes = &data[pos + 3..pos + 6];
let size = u32::from_be_bytes([0, size_bytes[0], size_bytes[1], size_bytes[2]]);
// Validate that the claimed frame size does not exceed the
// remaining tag data after the 6-byte v2.2 header.
let remaining_after_header = data.len().saturating_sub(pos + 6);
if (size as usize) > remaining_after_header {
return Ok(None);
}
cursor.set_position((pos + 6) as u64);
let mut header = FrameHeader::new(frame_id.to_string(), size, 0, (2, 2));
// Propagate the tag-level unsynchronization flag so that
// frame processors can apply unsync decoding when needed
header.global_unsync = self.f_unsynch();
Ok(Some(header))
}
3 | 4 => {
if pos + 10 > data.len() {
return Ok(None);
}
let size = if version == 4 {
// ID3v2.4 uses synchsafe integers
BitPaddedInt::new((&data[pos + 4..pos + 8]).into(), Some(7), Some(true))?
.value()
} else {
// ID3v2.3 uses regular big-endian integers
u32::from_be_bytes([data[pos + 4], data[pos + 5], data[pos + 6], data[pos + 7]])
};
let flags = u16::from_be_bytes([data[pos + 8], data[pos + 9]]);
cursor.set_position((pos + 10) as u64);
let mut header = FrameHeader::new(frame_id.to_string(), size, flags, (2, version));
// Propagate the tag-level unsynchronization flag so that
// frame processors can apply unsync decoding when needed
header.global_unsync = self.f_unsynch();
Ok(Some(header))
}
_ => Ok(None),
}
}
/// Parse frame data into a Frame object
fn _parse_frame(
&self,
header: &crate::id3::specs::FrameHeader,
data: &[u8],
) -> Result<Option<Box<dyn Frame>>> {
// Process frame data (handle compression, unsync, group ID, data length indicator, etc.)
use crate::id3::specs::FrameProcessor;
let processed_data = match FrameProcessor::process_read(header, data.to_vec()) {
Ok(data) => data,
Err(_) => {
// If processing fails, skip this frame
return Ok(None);
}
};
// Use frame registry to create appropriate frame type
match FrameRegistry::create_frame(&header.frame_id, &processed_data) {
Ok(frame) => Ok(Some(frame)),
Err(_) => Ok(None),
}
}
/// Creates a new `ID3` instance by reading and parsing tags from the given
/// file using non-blocking I/O.
///
/// Both ID3v2 (at the start of the file) and ID3v1 (last 128 bytes) are
/// checked. If only an ID3v1 tag is present, its frames are loaded into the
/// tag collection. Requires the `async` feature and a Tokio runtime.
#[cfg(feature = "async")]
pub async fn with_file_async<P: AsRef<Path>>(filething: P) -> Result<Self> {
use tokio::io::{AsyncReadExt, AsyncSeekExt};
let mut instance = Self::new();
instance.filename = Some(filething.as_ref().to_string_lossy().to_string());
// Open file asynchronously
let mut file = TokioFile::open(filething.as_ref()).await?;
// Check for ID3v2 header
let mut header_data = [0u8; 10];
file.read_exact(&mut header_data).await?;
// Track the file's ID3v2 major version (used for v1 loading below)
let mut file_v2_major: u8 = 4; // Default to 4 if no v2 tag found
if &header_data[0..3] == b"ID3" {
let vmaj = header_data[3];
file_v2_major = vmaj;
let _vrev = header_data[4];
let _flags = header_data[5];
let size_bytes = &header_data[6..10];
// Parse header
let header = ID3Header::from_bytes(&header_data)?;
// Get tag size
let size = BitPaddedInt::new(size_bytes.into(), Some(7), Some(true))?.value();
// Enforce the library-wide tag allocation ceiling
crate::limits::ParseLimits::default().check_tag_size(size as u64, "ID3v2 async")?;
// Cross-validate against actual stream size
let current_pos = file.stream_position().await?;
let stream_end = file.seek(SeekFrom::End(0)).await?;
let available = stream_end.saturating_sub(current_pos);
if (size as u64) > available {
return Err(AudexError::ParseError(format!(
"ID3 tag size ({} bytes) exceeds remaining file data ({} bytes)",
size, available
)));
}
file.seek(SeekFrom::Start(current_pos)).await?;
// Read tag data
let mut tag_data = vec![0u8; size as usize];
file.read_exact(&mut tag_data).await?;
// Skip extended header if present (flag bit 6)
let frame_data = if (_flags & 0x40) != 0 && tag_data.len() >= 4 {
let ext_size = if vmaj == 4 {
crate::id3::util::decode_synchsafe_int_checked(&tag_data[0..4])? as usize
} else {
// Use checked arithmetic to prevent overflow on 32-bit platforms
// when the declared size is near u32::MAX
let raw =
u32::from_be_bytes([tag_data[0], tag_data[1], tag_data[2], tag_data[3]])
as u64;
match raw.checked_add(4) {
Some(size) => size.min(tag_data.len() as u64) as usize,
None => tag_data.len(), // overflow: treat entire block as header
}
};
let skip = ext_size.min(tag_data.len());
&tag_data[skip..]
} else {
&tag_data
};
// Parse frames
let _ = instance._read(&header, frame_data)?;
instance._header = Some(header);
instance._version = (2, vmaj, 0);
}
// Check for ID3v1 at end of file
let file_size = file.seek(SeekFrom::End(0)).await?;
if file_size >= 128 {
file.seek(SeekFrom::End(-128)).await?;
let mut v1_data = [0u8; 128];
file.read_exact(&mut v1_data).await?;
if &v1_data[0..3] == b"TAG" {
// Load ID3v1 tags and merge with ID3v2 tags
// Use the actual file version (not always 4) to match sync load behavior
let v1v2_ver = if file_v2_major == 4 { 4 } else { 3 };
if let Ok((Some(v1_frames), _)) = find_id3v1(&v1_data, v1v2_ver, None) {
for (_, frame) in v1_frames {
// Only add if no existing frame with same hash key
if instance.tags.getall(&frame.hash_key()).is_empty() {
let _ = instance.tags.add(frame);
}
}
}
}
}
// Translate to v2.4 (matching sync load behavior)
instance.tags.update_to_v24();
instance._version = (2, 4, 0);
// Populate the text values cache so Tags::get returns real data
instance.refresh_values_cache();
Ok(instance)
}
/// Loads and parses ID3 tags from the given file path asynchronously.
///
/// This is an async alias for [`with_file_async`](Self::with_file_async).
#[cfg(feature = "async")]
pub async fn load_from_file_async<P: AsRef<Path>>(path: P) -> Result<Self> {
Self::with_file_async(path).await
}
/// Loads and parses ID3 tags from the given file path asynchronously
/// (legacy compatibility alias).
///
/// Delegates to [`load_from_file_async`](Self::load_from_file_async).
#[cfg(feature = "async")]
pub async fn from_file_async<P: AsRef<Path>>(path: P) -> Result<Self> {
Self::load_from_file_async(path).await
}
/// Save ID3 tags to file asynchronously.
///
/// Writes the current tags back to the file using non-blocking I/O.
#[cfg(feature = "async")]
pub async fn save_async(&mut self) -> Result<()> {
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
let filename = self
.filename
.clone()
.ok_or_else(|| AudexError::InvalidData("No filename set".to_string()))?;
// Generate tag data
let tag_data = self.tags.to_bytes()?;
// Open file for read/write
let mut file = TokioOpenOptions::new()
.read(true)
.write(true)
.open(&filename)
.await?;
// Find existing ID3v2 tag size and cross-validate against the
// actual file length to reject corrupt or oversized headers.
let mut header = [0u8; 10];
let old_size = if file.read_exact(&mut header).await.is_ok() && &header[0..3] == b"ID3" {
let size = BitPaddedInt::new((&header[6..10]).into(), Some(7), Some(true))?.value();
let total = (size as u64).checked_add(10).ok_or_else(|| {
AudexError::InvalidData(
"Old tag size overflow when adding header length".to_string(),
)
})?;
let stream_end = file.seek(SeekFrom::End(0)).await?;
if total > stream_end {
return Err(AudexError::ParseError(format!(
"ID3 tag size ({total} bytes) exceeds file size ({stream_end} bytes)",
)));
}
total
} else {
0
};
let new_size = tag_data.len() as u64;
// Resize file if needed
if old_size != new_size {
resize_bytes_async(&mut file, old_size, new_size, 0).await?;
}
// Write new tag
file.seek(SeekFrom::Start(0)).await?;
file.write_all(&tag_data).await?;
file.flush().await?;
Ok(())
}
/// Full save method with all parameters asynchronously.
///
/// Writes the current tags back to the file using non-blocking I/O with
/// custom ID3v1 and ID3v2 configuration options.
///
/// # Arguments
/// * `filething` - Optional path to save to (uses stored filename if None)
/// * `v1` - ID3v1 save options (REMOVE, UPDATE, or CREATE)
/// * `v2_version` - ID3v2 version (3 or 4)
/// * `v23_sep` - Optional separator for multi-value fields in v2.3
///
/// # Returns
/// * `Result<()>` - Success or an error if save fails
#[cfg(feature = "async")]
pub async fn save_with_options_async(
&mut self,
filething: Option<&Path>,
v1: ID3v1SaveOptions,
v2_version: u8,
v23_sep: Option<&str>,
) -> Result<()> {
use tokio::io::{AsyncReadExt, AsyncSeekExt, AsyncWriteExt};
// Validate version before building config (must be 3 or 4)
if v2_version != 3 && v2_version != 4 {
return Err(AudexError::InvalidData(
"Only 3 or 4 allowed for v2_version".to_string(),
));
}
let filename = filething
.map(|p| p.to_path_buf())
.or_else(|| {
self.filename
.as_ref()
.map(Path::new)
.map(|p| p.to_path_buf())
})
.ok_or_else(|| AudexError::InvalidData("No filename set".to_string()))?;
// Build save configuration
let config = ID3SaveConfig {
v2_version,
v2_minor: 0,
v23_sep: v23_sep.unwrap_or("/").to_string(),
v23_separator: v23_sep.unwrap_or("/").chars().next().unwrap_or('/') as u8,
padding: None,
merge_frames: false,
preserve_unknown: false,
compress_frames: false,
write_v1: v1,
unsync: false,
extended_header: false,
convert_v24_frames: true,
};
// Generate frame data with config
let frame_data = self.tags.write_with_config(&config)?;
// Build complete ID3v2 tag with header
let mut tag_data = Vec::new();
tag_data.extend_from_slice(b"ID3");
tag_data.push(v2_version);
tag_data.push(0); // revision
tag_data.push(0); // flags
// Write synchsafe size — validate the frame data fits
if frame_data.len() > 0x0FFF_FFFF {
return Err(AudexError::InvalidData(format!(
"Tag data size {} exceeds the ID3v2 synchsafe maximum (268_435_455 bytes)",
frame_data.len(),
)));
}
let size = frame_data.len() as u32;
let synchsafe = [
((size >> 21) & 0x7F) as u8,
((size >> 14) & 0x7F) as u8,
((size >> 7) & 0x7F) as u8,
(size & 0x7F) as u8,
];
tag_data.extend_from_slice(&synchsafe);
tag_data.extend_from_slice(&frame_data);
// Open file for read/write
let mut file = TokioOpenOptions::new()
.read(true)
.write(true)
.open(&filename)
.await?;
// Find existing ID3v2 tag size and cross-validate against the
// actual file length to reject corrupt or oversized headers.
let mut header = [0u8; 10];
let old_size = if file.read_exact(&mut header).await.is_ok() && &header[0..3] == b"ID3" {
let size = BitPaddedInt::new((&header[6..10]).into(), Some(7), Some(true))?.value();
let total = (size as u64).checked_add(10).ok_or_else(|| {
AudexError::InvalidData(
"Old tag size overflow when adding header length".to_string(),
)
})?;
let stream_end = file.seek(SeekFrom::End(0)).await?;
if total > stream_end {
return Err(AudexError::ParseError(format!(
"ID3 tag size ({total} bytes) exceeds file size ({stream_end} bytes)",
)));
}
total
} else {
0
};
let new_size = tag_data.len() as u64;
// Resize file if needed
if old_size != new_size {
resize_bytes_async(&mut file, old_size, new_size, 0).await?;
}
// Write new tag
file.seek(SeekFrom::Start(0)).await?;
file.write_all(&tag_data).await?;
file.flush().await?;
// Handle ID3v1 tag at end of file
{
let file_len = file.seek(SeekFrom::End(0)).await?;
let has_existing_v1 = if file_len >= 128 {
let mut tag_header = [0u8; 3];
file.seek(SeekFrom::End(-128)).await?;
file.read_exact(&mut tag_header).await.is_ok() && &tag_header == b"TAG"
} else {
false
};
match config.write_v1 {
ID3v1SaveOptions::CREATE => {
let v1_data = crate::id3::id3v1::make_id3v1_from_dict(&self.tags.dict);
if has_existing_v1 {
file.seek(SeekFrom::End(-128)).await?;
} else {
file.seek(SeekFrom::End(0)).await?;
}
file.write_all(&v1_data).await?;
file.flush().await?;
}
ID3v1SaveOptions::UPDATE if has_existing_v1 => {
let v1_data = crate::id3::id3v1::make_id3v1_from_dict(&self.tags.dict);
file.seek(SeekFrom::End(-128)).await?;
file.write_all(&v1_data).await?;
file.flush().await?;
}
_ => {}
}
}
Ok(())
}
/// Clear all tags from the file asynchronously.
#[cfg(feature = "async")]
pub async fn clear_async(&mut self) -> Result<()> {
self.tags.clear();
self.save_async().await
}
/// Delete the file asynchronously.
#[cfg(feature = "async")]
pub async fn delete_async(&mut self) -> Result<()> {
if let Some(filename) = &self.filename {
tokio::fs::remove_file(filename).await?;
}
Ok(())
}
}
/// Async resize bytes helper for ID3
#[cfg(feature = "async")]
async fn resize_bytes_async(
file: &mut TokioFile,
old_size: u64,
new_size: u64,
offset: u64,
) -> Result<()> {
if new_size > old_size {
// Need to insert bytes
insert_bytes_async(file, new_size - old_size, offset, None).await?;
} else if new_size < old_size {
// Need to delete bytes
delete_bytes_async(file, old_size - new_size, offset, None).await?;
}
Ok(())
}
impl Default for ID3 {
fn default() -> Self {
Self::new()
}
}
/// Removes ID3 tags directly from a file on disk without loading them into memory.
///
/// When `clear_v1` is `true`, any ID3v1 tag (last 128 bytes starting with `TAG`)
/// is truncated from the end of the file. When `clear_v2` is `true`, the ID3v2
/// header and its tag data at the start of the file are removed and the remaining
/// audio bytes are shifted forward.
pub fn clear(filething: &Path, clear_v1: bool, clear_v2: bool) -> Result<()> {
let mut file = OpenOptions::new().read(true).write(true).open(filething)?;
// Clear ID3v1 if requested — only read the tail to locate the tag
if clear_v1 {
// Use try_from to avoid silent truncation for files whose
// length exceeds i64::MAX.
let file_len = i64::try_from(file.seek(SeekFrom::End(0))?).map_err(|_| {
crate::AudexError::InvalidData(
"File length exceeds i64::MAX; cannot safely compute seek positions".to_string(),
)
})?;
if let Ok((frames, offset)) = find_id3v1_from_reader(&mut file, 4, None) {
if frames.is_some() {
// Truncate file to remove the ID3v1 tag at the tail.
// Guard against underflow: if offset is more negative
// than file_len, the file is too small to contain a
// valid tag at that position.
if file_len + offset < 0 {
return Err(crate::AudexError::ParseError(
"ID3v1 tag offset exceeds file size".to_string(),
));
}
let new_size = (file_len + offset) as u64;
file.set_len(new_size)?;
}
}
}
// Clear ID3v2 if requested
if clear_v2 {
file.seek(SeekFrom::Start(0))?;
// Read potential ID3v2 header
let mut header_data = [0u8; 10];
match file.read_exact(&mut header_data) {
Ok(()) => {
// Check if this is an ID3v2 header
if &header_data[0..3] == b"ID3" {
let vmaj = header_data[3];
let _vrev = header_data[4];
let _flags = header_data[5];
let size_bytes = &header_data[6..10];
if [2, 3, 4].contains(&vmaj) {
// Parse synchsafe integer for size
let size =
BitPaddedInt::new(size_bytes.into(), Some(7), Some(true))?.value();
// BitPaddedInt value should always be valid here
// Delete the entire ID3v2 tag (header + data)
delete_bytes(&mut file, size as u64 + 10, 0, None)?;
}
}
}
Err(err) if err.kind() == std::io::ErrorKind::UnexpectedEof => {
// A short file cannot contain a full ID3v2 header.
}
Err(err) => return Err(err.into()),
}
}
Ok(())
}
/// Removes ID3 tags from a writer that implements `Read + Write + Seek`.
///
/// Writer-based equivalent of [`clear`]. When `clear_v1` is `true`, any ID3v1
/// tag (last 128 bytes starting with `TAG`) is removed. When `clear_v2` is
/// `true`, the ID3v2 header and its tag data at the start are removed and the
/// remaining bytes are shifted forward.
///
/// The data is copied into an intermediate `Cursor<Vec<u8>>` so that both
/// growing and shrinking operations work regardless of the concrete writer
/// type.
pub fn clear_from_writer(
writer: &mut dyn ReadWriteSeek,
clear_v1: bool,
clear_v2: bool,
) -> Result<()> {
// Check the total file size before reading into memory to prevent
// OOM on multi-gigabyte audio files. We only need to manipulate tags
// (typically a few MB), not the entire audio payload.
let file_size = writer.seek(SeekFrom::End(0))?;
let max_read_size = crate::limits::MAX_IN_MEMORY_WRITER_FILE;
if file_size > max_read_size {
return Err(AudexError::InvalidData(format!(
"File size ({} bytes) exceeds maximum for in-memory tag clearing ({} bytes). \
Use file-based clearing instead.",
file_size, max_read_size
)));
}
// Read all data into memory
writer.seek(SeekFrom::Start(0))?;
let mut buf = Vec::new();
writer.read_to_end(&mut buf)?;
// Perform clearing on the in-memory cursor
let mut cursor = Cursor::new(buf);
// Clear ID3v1 if requested — use the tail reader on the cursor
if clear_v1 {
let cursor_len = i64::try_from(cursor.get_ref().len()).map_err(|_| {
AudexError::InvalidData(
"Buffer length exceeds i64::MAX; cannot safely compute seek positions".to_string(),
)
})?;
if let Ok((frames, offset)) = find_id3v1_from_reader(&mut cursor, 4, None) {
if frames.is_some() {
if cursor_len + offset < 0 {
return Err(AudexError::InvalidData(
"ID3v1 offset points before the start of the buffer".to_string(),
));
}
let v1_start = (cursor_len + offset) as u64;
let v1_len = (cursor_len as u64).saturating_sub(v1_start);
delete_bytes(&mut cursor, v1_len, v1_start, None)?;
}
}
}
// Clear ID3v2 if requested
if clear_v2 {
cursor.seek(SeekFrom::Start(0))?;
let mut header_data = [0u8; 10];
match cursor.read_exact(&mut header_data) {
Ok(()) => {
if &header_data[0..3] == b"ID3" {
let vmaj = header_data[3];
let _vrev = header_data[4];
let _flags = header_data[5];
let size_bytes = &header_data[6..10];
if [2, 3, 4].contains(&vmaj) {
let size =
BitPaddedInt::new(size_bytes.into(), Some(7), Some(true))?.value();
delete_bytes(&mut cursor, size as u64 + 10, 0, None)?;
}
}
}
Err(err) if err.kind() == std::io::ErrorKind::UnexpectedEof => {
// A short buffer cannot contain a full ID3v2 header.
}
Err(err) => return Err(err.into()),
}
}
// Write modified data back to the writer
let result = cursor.into_inner();
writer.seek(SeekFrom::Start(0))?;
writer.write_all(&result)?;
// Zero out any stale trailing bytes from the original content.
// When tags are removed the output is shorter than the input, but
// Cursor/File writers do not auto-truncate on write_all — the old
// bytes remain accessible beyond the new content boundary.
let written_end = writer.stream_position()?;
crate::util::truncate_writer_dyn(writer, written_end)?;
Ok(())
}
/// Native async version of [`clear`]. Removes ID3 tags from a file using tokio I/O.
#[cfg(feature = "async")]
pub async fn clear_async(filething: &Path, clear_v1: bool, clear_v2: bool) -> Result<()> {
use tokio::io::{AsyncReadExt, AsyncSeekExt};
let mut file = TokioOpenOptions::new()
.read(true)
.write(true)
.open(filething)
.await?;
// Clear ID3v1 if requested — only read the tail to locate the tag
if clear_v1 {
// Use try_from to avoid silent truncation for files whose
// length exceeds i64::MAX (mirrors the sync clear() path).
let file_len = i64::try_from(file.seek(SeekFrom::End(0)).await?).map_err(|_| {
crate::AudexError::InvalidData(
"File length exceeds i64::MAX; cannot safely compute seek positions".to_string(),
)
})?;
let tail_size = std::cmp::min(file_len, 256) as usize;
if tail_size > 0 {
file.seek(SeekFrom::End(-(tail_size as i64))).await?;
let mut tail = vec![0u8; tail_size];
file.read_exact(&mut tail).await?;
if let Ok((frames, offset)) = find_id3v1(&tail, 4, None) {
if frames.is_some() {
// Guard against underflow: reject if offset is more
// negative than file_len (same check as sync path).
if file_len + offset < 0 {
return Err(crate::AudexError::ParseError(
"ID3v1 tag offset exceeds file size".to_string(),
));
}
let new_size = (file_len + offset) as u64;
file.set_len(new_size).await?;
}
}
}
}
// Clear ID3v2 if requested
if clear_v2 {
file.seek(SeekFrom::Start(0)).await?;
let mut header_data = [0u8; 10];
match file.read_exact(&mut header_data).await {
Ok(_) => {
if &header_data[0..3] == b"ID3" {
let vmaj = header_data[3];
let size_bytes = &header_data[6..10];
if [2, 3, 4].contains(&vmaj) {
let size =
BitPaddedInt::new(size_bytes.into(), Some(7), Some(true))?.value();
delete_bytes_async(&mut file, size as u64 + 10, 0, None).await?;
}
}
}
Err(err) if err.kind() == std::io::ErrorKind::UnexpectedEof => {
// A short file cannot contain a full ID3v2 header.
}
Err(err) => return Err(err.into()),
}
}
Ok(())
}
/// A no-op [`StreamInfo`] implementation for files that contain only ID3 tags
/// and no audio stream (e.g., standalone `.id3` files).
///
/// Every accessor returns `None`, indicating that no stream metadata is available.
#[derive(Debug, Default)]
pub struct EmptyStreamInfo;
impl StreamInfo for EmptyStreamInfo {
fn length(&self) -> Option<Duration> {
None
}
fn bitrate(&self) -> Option<u32> {
None
}
fn sample_rate(&self) -> Option<u32> {
None
}
fn channels(&self) -> Option<u16> {
None
}
fn bits_per_sample(&self) -> Option<u16> {
None
}
}
/// Stream info for ID3FileType
#[derive(Debug)]
pub struct _Info {
pub length: Duration,
}
impl _Info {
pub fn new<R: Read + Seek>(_fileobj: &mut R, _offset: Option<u64>) -> Self {
Self {
length: Duration::from_secs(0),
}
}
pub fn pprint() -> String {
"Unknown format with ID3 tag".to_string()
}
}
impl StreamInfo for _Info {
fn length(&self) -> Option<Duration> {
if self.length.as_secs() == 0 {
None
} else {
Some(self.length)
}
}
fn bitrate(&self) -> Option<u32> {
None
}
fn sample_rate(&self) -> Option<u32> {
None
}
fn channels(&self) -> Option<u16> {
None
}
fn bits_per_sample(&self) -> Option<u16> {
None
}
}
/// ID3FileType implementation
///
/// An unknown type of file with ID3 tags.
///
/// Args:
/// filething: A filename or file handle
/// id3: An ID3 type to use for tags.
///
/// Load stream and tag information from a file.
///
/// A custom tag reader may be used in instead of the default
/// ID3 implementation, e.g. a custom ID3 reader.
#[derive(Debug)]
pub struct ID3FileType {
/// ID3 tags
pub tags: Option<ID3>,
/// Stream info
pub info: _Info,
/// ID3 type to use
pub id3: fn() -> ID3,
}
impl ID3FileType {
/// Create new ID3FileType
pub fn new() -> Self {
Self {
tags: None,
info: _Info::new(&mut std::io::Cursor::new(Vec::new()), None),
id3: ID3::new,
}
}
/// Score file compatibility
pub fn score(_filename: &str, header_data: &[u8]) -> i32 {
if header_data.starts_with(b"ID3") {
1
} else {
0
}
}
/// Add an empty ID3 tag to the file
pub fn add_tags(&mut self, id3_class: Option<fn() -> ID3>) -> Result<()> {
let id3_fn = id3_class.unwrap_or(self.id3);
if self.tags.is_none() {
self.id3 = id3_fn;
self.tags = Some(id3_fn());
} else {
return Err(AudexError::InvalidOperation(
"Tags already exist".to_string(),
));
}
Ok(())
}
/// Load file
pub fn load<P: AsRef<Path>>(
&mut self,
filething: P,
id3_class: Option<fn() -> ID3>,
) -> Result<()> {
let id3_fn = id3_class.unwrap_or(self.id3);
self.id3 = id3_fn;
let mut id3_instance = id3_fn();
match id3_instance.load(filething, None, true, 4, true) {
Ok(()) => {
self.tags = Some(id3_instance);
}
Err(_) => {
self.tags = None; // ID3NoHeaderError
}
}
Ok(())
}
}
impl FileType for ID3FileType {
type Tags = ID3;
type Info = _Info;
fn format_id() -> &'static str {
"ID3FileType"
}
fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
let mut instance = Self::new();
instance.load(path, None)?;
Ok(instance)
}
fn save(&mut self) -> Result<()> {
if let Some(ref mut tags) = self.tags {
tags.save()
} else {
Err(AudexError::InvalidData("No tags to save".to_string()))
}
}
fn clear(&mut self) -> Result<()> {
if let Some(ref mut tags) = self.tags {
tags.clear()?;
self.tags = None;
}
Ok(())
}
fn save_to_writer(&mut self, writer: &mut dyn ReadWriteSeek) -> Result<()> {
if let Some(ref mut tags) = self.tags {
tags.save_to_writer(writer)
} else {
Err(AudexError::InvalidData("No tags to save".to_string()))
}
}
fn clear_writer(&mut self, writer: &mut dyn ReadWriteSeek) -> Result<()> {
if let Some(ref mut tags) = self.tags {
tags.clear_writer(writer)?;
self.tags = None;
}
Ok(())
}
fn save_to_path(&mut self, path: &Path) -> Result<()> {
if let Some(ref mut tags) = self.tags {
tags.save_to_path(path)
} else {
Err(AudexError::InvalidData("No tags to save".to_string()))
}
}
fn tags(&self) -> Option<&Self::Tags> {
self.tags.as_ref()
}
fn tags_mut(&mut self) -> Option<&mut Self::Tags> {
self.tags.as_mut()
}
fn info(&self) -> &Self::Info {
&self.info
}
fn add_tags(&mut self) -> Result<()> {
// Delegate to the existing public add_tags method with default ID3 version
ID3FileType::add_tags(self, None)
}
fn get(&self, key: &str) -> Option<Vec<String>> {
// ID3Tags has a special get_text_values method that handles the mapping
self.tags.as_ref()?.tags.get_text_values(key)
}
fn score(_filename: &str, header: &[u8]) -> i32 {
if header.starts_with(b"ID3") { 10 } else { 0 }
}
fn mime_types() -> &'static [&'static str] {
&["audio/mpeg", "audio/mp3", "audio/x-aiff"]
}
}
impl Default for ID3FileType {
fn default() -> Self {
Self::new()
}
}
/// Implement FileType for ID3 directly to match existing patterns
impl FileType for ID3 {
type Tags = ID3Tags;
type Info = EmptyStreamInfo;
fn format_id() -> &'static str {
"ID3"
}
fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
debug_event!("parsing standalone ID3 file");
Self::load_from_file(path)
}
fn load_from_reader(reader: &mut dyn crate::ReadSeek) -> Result<Self> {
debug_event!("parsing standalone ID3 file from reader");
let mut instance = Self::new();
let mut reader = reader;
// Clear existing state
instance.unknown_frames.clear();
instance._header = None;
instance._padding = 0;
// Pre-load header hook
instance._pre_load_header(&mut reader)?;
// Try to parse ID3v2 header
reader.seek(SeekFrom::Start(0))?;
let mut header_data = [0u8; 10];
match reader.read_exact(&mut header_data) {
Ok(()) if &header_data[0..3] == b"ID3" => {
let header = ID3Header::from_bytes(&header_data)?;
instance._header = Some(header);
let size = instance.size();
// Enforce the library-wide tag allocation ceiling
crate::limits::ParseLimits::default()
.check_tag_size(size as u64, "ID3v2 reader")?;
// Cross-validate against actual stream size
let current_pos = reader.stream_position()?;
let stream_end = reader.seek(SeekFrom::End(0))?;
let available = stream_end.saturating_sub(current_pos);
if (size as u64) > available {
return Err(AudexError::ParseError(format!(
"ID3 tag size ({} bytes) exceeds remaining stream data ({} bytes)",
size, available
)));
}
reader.seek(SeekFrom::Start(current_pos))?;
// Read the full tag data
let mut data = vec![0u8; size as usize];
reader.read_exact(&mut data).map_err(|e| {
AudexError::InvalidData(format!("Cannot read {} bytes: {}", size, e))
})?;
// Skip the extended header if present
let header_ref = instance._header.as_ref().ok_or_else(|| {
AudexError::InvalidData("ID3 header not set during reader load".to_string())
})?;
let frame_data = if instance.f_extended() && data.len() >= 4 {
// Check if the first 4 bytes look like a frame ID rather
// than an extended header size (common tagger bug).
let looks_like_frame = std::str::from_utf8(&data[0..4])
.map(|s| {
s.chars()
.all(|c| c.is_ascii_uppercase() || c.is_ascii_digit())
})
.unwrap_or(false);
if looks_like_frame {
// Extended header flag was likely set incorrectly;
// treat the data as starting with frame headers.
&data
} else {
let ext_size = if header_ref.major_version == 4 {
crate::id3::util::decode_synchsafe_int_checked(&data[0..4])? as usize
} else {
// Use checked_add to prevent overflow on 32-bit platforms
// when the declared size is near u32::MAX
let raw =
u32::from_be_bytes([data[0], data[1], data[2], data[3]]) as u64;
match raw.checked_add(4) {
Some(size) => size as usize,
None => {
return Err(AudexError::InvalidData(
"Extended header size overflow".to_string(),
));
}
}
};
// Reject extended header sizes that exceed the available data.
// A crafted file could set a huge size to skip all valid frames.
if ext_size > data.len() {
return Err(AudexError::InvalidData(
"Extended header size exceeds available tag data".to_string(),
));
}
&data[ext_size..]
}
} else {
&data
};
let header_clone = header_ref.clone();
let remaining_data = instance._read(&header_clone, frame_data)?;
instance._padding = remaining_data.len();
// Load ID3v1 if present — only read the tail
let v1v2_ver = if instance.version().1 == 4 { 4 } else { 3 };
if let Ok((Some(frames), _offset)) = find_id3v1_from_reader(reader, v1v2_ver, None)
{
for (_, frame) in frames {
if instance.tags.getall(&frame.hash_key()).is_empty() {
let _ = instance.tags.add(frame);
}
}
}
}
_ => {
// No ID3v2 header found, try ID3v1 from the tail
match find_id3v1_from_reader(reader, 4, None) {
Ok((frames, _offset)) => {
if let Some(frames) = frames {
instance._version = (1, 1, 0);
for (_, frame) in frames {
if instance.tags.getall(&frame.hash_key()).is_empty() {
let _ = instance.tags.add(frame);
}
}
} else {
return Err(AudexError::InvalidData("No ID3 tags found".to_string()));
}
}
Err(e) => return Err(e),
}
}
}
// Translate to v2.4
instance.tags.update_to_v24();
instance._version = (2, 4, 0);
// Populate the text values cache so Tags::get returns real data
instance.refresh_values_cache();
Ok(instance)
}
fn save(&mut self) -> Result<()> {
self.save()
}
fn clear(&mut self) -> Result<()> {
self.delete_full(None, true, true)
}
fn save_to_writer(&mut self, writer: &mut dyn ReadWriteSeek) -> Result<()> {
self.save_to_writer(writer)
}
fn clear_writer(&mut self, writer: &mut dyn ReadWriteSeek) -> Result<()> {
self.clear_writer(writer)
}
fn save_to_path(&mut self, path: &Path) -> Result<()> {
let tag_version = self.tags.version().1;
let v2_version = if tag_version == 3 || tag_version == 4 {
tag_version
} else {
4
};
self.save_with_options(Some(path), ID3v1SaveOptions::UPDATE, v2_version, Some("/"))
}
fn tags(&self) -> Option<&Self::Tags> {
self.tags()
}
fn tags_mut(&mut self) -> Option<&mut Self::Tags> {
self.tags_mut()
}
fn info(&self) -> &Self::Info {
&EmptyStreamInfo
}
fn add_tags(&mut self) -> Result<()> {
// Tags always exist for ID3 - they are created when the struct is instantiated
Err(AudexError::InvalidOperation(
"Tags already exist".to_string(),
))
}
fn get(&self, key: &str) -> Option<Vec<String>> {
// ID3Tags has a special get_text_values method that handles the mapping
self.tags().and_then(|tags| tags.get_text_values(key))
}
fn score(_filename: &str, header: &[u8]) -> i32 {
if header.starts_with(b"ID3") { 10 } else { 0 }
}
fn mime_types() -> &'static [&'static str] {
&["audio/mpeg", "audio/mp3", "audio/aiff", "audio/x-aiff"]
}
}