Skip to content

Commit 040fa83

Browse files
authored
Merge pull request #461 from schreibfaul1/only_for_testing
fix ESP crashes during playing local MP3 file with ID3 Tag v2.2 #460
2 parents 32385d0 + fe1f37c commit 040fa83

File tree

2 files changed

+56
-34
lines changed

2 files changed

+56
-34
lines changed

src/Audio.cpp

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*7d
1+
/*
22
* Audio.cpp
33
*
44
* Created on: Oct 26.2018
55
*
6-
* Version 2.0.8b
7-
* Updated on: Jan 10.2023
6+
* Version 2.0.8c
7+
* Updated on: Jan 11.2023
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -959,7 +959,7 @@ void Audio::showID3Tag(const char* tag, const char* value){
959959
if(!strcmp(tag, "IPL")) sprintf(m_chbuf, "Involved people list: %s", value);
960960
if(!strcmp(tag, "PIC")) sprintf(m_chbuf, "Attached picture: %s", value);
961961
if(!strcmp(tag, "SLT")) sprintf(m_chbuf, "Synchronized lyric/text: %s", value);
962-
// if(!strcmp(tag, "TAL")) sprintf(m_chbuf, "Album/Movie/Show title: %s", value);
962+
if(!strcmp(tag, "TAL")) sprintf(m_chbuf, "Album/Movie/Show title: %s", value);
963963
if(!strcmp(tag, "TBP")) sprintf(m_chbuf, "BPM (Beats Per Minute): %s", value);
964964
if(!strcmp(tag, "TCM")) sprintf(m_chbuf, "Composer: %s", value);
965965
if(!strcmp(tag, "TCO")) sprintf(m_chbuf, "Content type: %s", value);
@@ -1524,7 +1524,8 @@ int Audio::read_FLAC_Header(uint8_t *data, size_t len) {
15241524
int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15251525

15261526
static size_t id3Size;
1527-
static size_t headerSize;
1527+
static size_t remainingHeaderBytes;
1528+
static size_t universal_tmp = 0;
15281529
static uint8_t ID3version;
15291530
static int ehsz = 0;
15301531
static char tag[5];
@@ -1543,7 +1544,7 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15431544
}
15441545
m_controlCounter ++;
15451546
APIC_seen = false;
1546-
headerSize = 0;
1547+
remainingHeaderBytes = 0;
15471548
ehsz = 0;
15481549
if(specialIndexOf(data, "ID3", 4) != 0) { // ID3 not found
15491550
if(!m_f_m3u8data) AUDIO_INFO("file has no mp3 tag, skip metadata");
@@ -1573,9 +1574,9 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15731574
if(ID3version == 2){
15741575
m_controlCounter = 10;
15751576
}
1576-
headerSize = id3Size;
1577+
remainingHeaderBytes = id3Size;
15771578
m_ID3Size = id3Size;
1578-
headerSize -= 10;
1579+
remainingHeaderBytes -= 10;
15791580

15801581
return 10;
15811582
}
@@ -1585,7 +1586,7 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15851586
if(m_f_exthdr) {
15861587
AUDIO_INFO("ID3 extended header");
15871588
ehsz = bigEndian(data, 4);
1588-
headerSize -= 4;
1589+
remainingHeaderBytes -= 4;
15891590
ehsz -= 4;
15901591
return 4;
15911592
}
@@ -1598,16 +1599,16 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
15981599
if(m_controlCounter == 2){ // skip extended header if exists
15991600
if(ehsz > 256) {
16001601
ehsz -=256;
1601-
headerSize -= 256;
1602+
remainingHeaderBytes -= 256;
16021603
return 256;} // Throw it away
16031604
else {
16041605
m_controlCounter ++;
1605-
headerSize -= ehsz;
1606+
remainingHeaderBytes -= ehsz;
16061607
return ehsz;} // Throw it away
16071608
}
16081609
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
16091610
if(m_controlCounter == 3){ // read a ID3 frame, get the tag
1610-
if(headerSize == 0){
1611+
if(remainingHeaderBytes == 0){
16111612
m_controlCounter = 99;
16121613
return 0;
16131614
}
@@ -1619,7 +1620,7 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
16191620
frameid[4] = 0;
16201621
for(uint8_t i = 0; i < 4; i++) tag[i] = frameid[i]; // tag = frameid
16211622

1622-
headerSize -= 4;
1623+
remainingHeaderBytes -= 4;
16231624
if(frameid[0] == 0 && frameid[1] == 0 && frameid[2] == 0 && frameid[3] == 0) {
16241625
// We're in padding
16251626
m_controlCounter = 98; // all ID3 metadata processed
@@ -1636,18 +1637,18 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
16361637
else {
16371638
framesize = bigEndian(data, 4); // << 8
16381639
}
1639-
headerSize -= 4;
1640+
remainingHeaderBytes -= 4;
16401641
uint8_t flag = *(data + 4); // skip 1st flag
16411642
(void) flag;
1642-
headerSize--;
1643+
remainingHeaderBytes--;
16431644
compressed = (*(data + 5)) & 0x80; // Frame is compressed using [#ZLIB zlib] with 4 bytes for 'decompressed
16441645
// size' appended to the frame header.
1645-
headerSize--;
1646+
remainingHeaderBytes--;
16461647
uint32_t decompsize = 0;
16471648
if(compressed){
16481649
if(m_f_Log) log_i("iscompressed");
16491650
decompsize = bigEndian(data + 6, 4);
1650-
headerSize -= 4;
1651+
remainingHeaderBytes -= 4;
16511652
(void) decompsize;
16521653
if(m_f_Log) log_i("decompsize=%u", decompsize);
16531654
return 6 + 4;
@@ -1658,12 +1659,12 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
16581659
if(m_controlCounter == 5){ // If the frame is larger than 256 bytes, skip the rest
16591660
if(framesize > 256){
16601661
framesize -= 256;
1661-
headerSize -= 256;
1662+
remainingHeaderBytes -= 256;
16621663
return 256;
16631664
}
16641665
else {
16651666
m_controlCounter = 3; // check next frame
1666-
headerSize -= framesize;
1667+
remainingHeaderBytes -= framesize;
16671668
return framesize;
16681669
}
16691670
}
@@ -1682,7 +1683,7 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
16821683
isUnicode = false;
16831684
if(getDatamode() == AUDIO_LOCALFILE){
16841685
APIC_seen = true;
1685-
APIC_pos = id3Size - headerSize;
1686+
APIC_pos = id3Size - remainingHeaderBytes;
16861687
APIC_size = framesize;
16871688
}
16881689
return 0;
@@ -1694,7 +1695,7 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
16941695
value[i] = *(data + i);
16951696
}
16961697
framesize -= fs;
1697-
headerSize -= fs;
1698+
remainingHeaderBytes -= fs;
16981699
value[fs] = 0;
16991700
if(isUnicode && fs > 1) {
17001701
unicode2utf8(value, fs); // convert unicode to utf-8 U+0020...U+07FF
@@ -1718,40 +1719,59 @@ int Audio::read_ID3_Header(uint8_t *data, size_t len) {
17181719
}
17191720
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17201721

1721-
// -- section V2.2 only , greater Vers above ----
1722+
// --- section V2.2 only , greater Vers above ----
1723+
// see https://mutagen-specs.readthedocs.io/en/latest/id3/id3v2.2.html
17221724
if(m_controlCounter == 10){ // frames in V2.2, 3bytes identifier, 3bytes size descriptor
1725+
1726+
if(universal_tmp > 0){
1727+
if( universal_tmp > 256) { universal_tmp -= 256; return 256;}
1728+
else{ uint8_t t = universal_tmp; universal_tmp = 0; return t;}
1729+
}
1730+
17231731
frameid[0] = *(data + 0);
17241732
frameid[1] = *(data + 1);
17251733
frameid[2] = *(data + 2);
17261734
frameid[3] = 0;
17271735
for(uint8_t i = 0; i < 4; i++) tag[i] = frameid[i]; // tag = frameid
1728-
headerSize -= 3;
1736+
remainingHeaderBytes -= 3;
17291737
size_t len = bigEndian(data + 3, 3);
1730-
headerSize -= 3;
1731-
headerSize -= len;
1738+
universal_tmp = len;
1739+
remainingHeaderBytes -= 3;
17321740
char value[256];
1733-
size_t tmp = len;
1734-
if(tmp > 254) tmp = 254;
1735-
memcpy(value, (data + 7), tmp);
1736-
value[tmp+1] = 0;
1741+
if(len > 249) {len = 249; }
1742+
memcpy(value, (data + 7), len);
1743+
value[len + 1] = 0;
17371744
m_chbuf[0] = 0;
1745+
if(startsWith(tag, "PIC")) { // image embedded in header
1746+
if(getDatamode() == AUDIO_LOCALFILE){
1747+
APIC_seen = true; // #460
1748+
APIC_pos = id3Size - remainingHeaderBytes;
1749+
APIC_size = universal_tmp;
1750+
if(m_f_Log) log_i("Attached picture seen at pos %d length %d", APIC_pos, APIC_size);
1751+
}
1752+
}
1753+
else{
1754+
showID3Tag(tag, value);
1755+
}
1756+
remainingHeaderBytes -= universal_tmp;
1757+
universal_tmp -= len;
17381758

1739-
showID3Tag(tag, value);
17401759
if(len == 0) m_controlCounter = 98;
1760+
if(remainingHeaderBytes == 0) m_controlCounter = 98;
17411761

17421762
return 3 + 3 + len;
17431763
}
17441764
// -- end section V2.2 -----------
17451765

17461766
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17471767
if(m_controlCounter == 98){ // skip all ID3 metadata (mostly spaces)
1748-
if(headerSize > 256) {
1749-
headerSize -=256;
1768+
if(remainingHeaderBytes > 256) {
1769+
remainingHeaderBytes -=256;
17501770
return 256;
17511771
} // Throw it away
17521772
else {
17531773
m_controlCounter = 99;
1754-
return headerSize;
1774+
return remainingHeaderBytes;
17551775
} // Throw it away
17561776
}
17571777
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

src/Audio.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
* Audio.h
33
*
44
* Created on: Oct 28,2018
5-
* Updated on: Jan 10,2023
5+
*
6+
* Version 2.0.8c
7+
* Updated on: Jan 11,2023
68
* Author: Wolle (schreibfaul1)
79
*/
810

0 commit comments

Comments
 (0)