id3lib 3.8.3
header_tag.cpp
Go to the documentation of this file.
1// $Id: header_tag.cpp,v 1.25 2003/03/02 14:30:46 t1mpy Exp $
2
3// id3lib: a C++ library for creating and manipulating id3v1/v2 tags
4// Copyright 1999, 2000 Scott Thomas Haug
5// Copyright 2002 Thijmen Klok (thijmen@id3lib.org)
6
7// This library is free software; you can redistribute it and/or modify it
8// under the terms of the GNU Library General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or (at your
10// option) any later version.
11//
12// This library is distributed in the hope that it will be useful, but WITHOUT
13// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
15// License for more details.
16//
17// You should have received a copy of the GNU Library General Public License
18// along with this library; if not, write to the Free Software Foundation,
19// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21// The id3lib authors encourage improvements and optimisations to be sent to
22// the id3lib coordinator. Please see the README file for details on where to
23// send such submissions. See the AUTHORS file for a list of people who have
24// contributed to id3lib. See the ChangeLog file for a list of changes to
25// id3lib. These files are distributed with id3lib at
26// http://download.sourceforge.net/id3lib/
27
28
29#include "header_tag.h"
30#include "id3/utils.h" // has <config.h> "id3/id3lib_streams.h" "id3/globals.h" "id3/id3lib_strings.h"
31#include "tag.h"
32#include "io_helpers.h"
33#include "spec.h"
34#include <cstdlib> // for std::getenv (opt-in diagnostic below)
35
36using namespace dami;
37
38const char* const ID3_TagHeader::ID = "ID3";
39
41{
42 bool changed = this->ID3_Header::SetSpec(spec);
43 if (changed)
44 {
45 if (_info)
46 {
47 _flags.set(HEADER_FLAG_EXPERIMENTAL, _info->is_experimental);
48 _flags.set(HEADER_FLAG_EXTENDED, _info->is_extended);
49 }
50 }
51 return changed;
52}
53
54size_t ID3_TagHeader::Size() const
55{
56 size_t bytesUsed = ID3_TagHeader::SIZE;
57
58 if (_info && _info->is_extended)
59 {
60 bytesUsed += _info->extended_bytes;
61 }
62
63 return bytesUsed;
64}
65
66
68{
69 writer.writeChars((uchar *) ID, strlen(ID));
70
73
74 // set the flags byte in the header
75 writer.writeChar(static_cast<uchar>(_flags.get() & MASK8));
76 io::writeUInt28(writer, this->GetDataSize()); //now includes the extended header
77
78 // now we render the extended header
80 {
81 if (this->GetSpec() == ID3V2_4_0)
82 {
83 io::writeUInt28(writer, 6); //write 4 bytes of v2.4.0 ext header containing size '6'
84 io::writeBENumber(writer, 1, 1); //write that it has only one flag byte (value '1')
85 io::writeBENumber(writer, 0, 1); //write flag byte with value '0'
86 }
87 else if (this->GetSpec() == ID3V2_3_0)
88 {
89 io::writeBENumber(writer, 6, sizeof(uint32));
90 for (size_t i = 0; i < 6; ++i)
91 {
92 if (writer.writeChar('\0') == ID3_Writer::END_OF_WRITER)
93 {
94 break;
95 }
96 }
97 }
98 // else //not implemented
99 }
100}
101
103{
104 io::ExitTrigger et(reader);
105 if (!ID3_Tag::IsV2Tag(reader))
106 {
107 ID3D_NOTICE( "ID3_TagHeader::Parse(): not an id3v2 header" );
108 return false;
109 }
110
111 uchar id[3];
112 reader.readChars(id, 3);
113 // The spec version is determined with the MAJOR and MINOR OFFSETs
114 uchar major = reader.readChar();
115 uchar minor = reader.readChar();
116 this->SetSpec(ID3_VerRevToV2Spec(major, minor));
117
118 // id3lib only fully supports up to ID3v2.3.0 (ID3V2_LATEST). A higher or
119 // unrecognised v2 version (e.g. ID3v2.4) cannot be parsed correctly and is
120 // dropped without any indication (Debian bug #449186). When the
121 // ID3LIB_WARN_UNSUPPORTED environment variable is set, emit a single notice
122 // so this case can be made observable. The library stays silent by default
123 // (a shared library must not write to stderr unsolicited, and ID3v2.4 tags
124 // are common). Parsing behaviour is unchanged either way.
125 if ((this->GetSpec() == ID3V2_UNKNOWN || this->GetSpec() > ID3V2_LATEST) &&
126 std::getenv("ID3LIB_WARN_UNSUPPORTED") != NULL)
127 {
128 std::clog << "id3lib: ID3v2." << static_cast<int>(major) << "."
129 << static_cast<int>(minor)
130 << " tag found, but only ID3v2.3.0 and earlier are supported; "
131 << "this tag may not be read correctly" << std::endl;
132 }
133
134 // Get the flags at the appropriate offset
135 _flags.set(static_cast<ID3_Flags::TYPE>(reader.readChar()));
136
137 // set the data size
138 this->SetDataSize(io::readUInt28(reader));
139
140 if (_flags.test(HEADER_FLAG_EXTENDED) && this->GetSpec() == ID3V2_2_1)
141 {
142 //couldn't find anything about this in the draft specifying 2.2.1 -> http://www.id3.org/pipermail/id3v2/2000-April/000126.html
143 _flags.set(HEADER_FLAG_EXTENDED, false);
144 _info->extended_bytes = 0;
145 // rest is checked at ParseExtended()
146 }
147 et.setExitPos(reader.getCur());
148 return true;
149}
150
152{
153 if (this->GetSpec() == ID3V2_3_0)
154 {
155/*
156 Extended header size $xx xx xx xx
157 Extended Flags $xx xx
158 Size of padding $xx xx xx xx
159*/
160 // skip over header size, we are not using it anyway, we calculate it
161 reader.setCur(reader.getCur()+4); //Extended header size
162 //io::readBENumber(reader, 4); //Extended header size
163 uint16 tmpval = io::readBENumber(reader, 2); //Extended Flags
164 // skip over padding size, we are not using it anyway
165 reader.setCur(reader.getCur()+4); //Size of padding
166 // io::readBENumber(reader, 4); //Size of padding
167 if (tmpval != 0) //there is only one flag defined in ID3V2_3_0: crc
168 {
169 //skip over crc data, we are not using it anyway
170 reader.setCur(reader.getCur()+4); //Crc
171 //io::readBENumber(reader, 4); //Crc
172 _info->extended_bytes = 14;
173 }
174 else
175 _info->extended_bytes = 10;
176 }
177 if (this->GetSpec() == ID3V2_4_0)
178 {
179/*
180 Extended header size 4 * %0xxxxxxx
181 Number of flag bytes $01
182 Extended Flags $xx
183*/
184 uint16 i;
185 uint16 extrabytes;
186
187 io::readUInt28(reader);
188 const int extflagbytes = reader.readChar(); //Number of flag bytes
189 ID3_Flags* extflags[1]; // ID3V2_4_0 has 1 flag byte, extflagbytes should be equal to 1
190 for (i = 0; i < extflagbytes; ++i)
191 {
192 extflags[i] = new ID3_Flags;
193 extflags[i]->set(reader.readChar()); //flags
194 }
195 extrabytes = 0;
196 //extflags[0]->test(EXT_HEADER_FLAG_BIT1); // ID3V2_4_0 ext header flag bit 1 *should* be 0
197 if (extflags[0]->test(EXT_HEADER_FLAG_BIT2))
198 {
199 // ID3V2_4_0 ext header flag bit 2 = Tag is an update
200 // read size
201 extrabytes += 1; // add a byte for the char containing the extflagdatasize
202 const int extheaderflagdatasize = reader.readChar();
203 extrabytes += extheaderflagdatasize;
204 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
205 reader.setCur(reader.getCur() + extheaderflagdatasize);
206 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
207 }
208 if (extflags[0]->test(EXT_HEADER_FLAG_BIT3))
209 {
210 // ID3V2_4_0 ext header flag bit 3 = CRC data present
211 // read size
212 extrabytes += 1; // add a byte for the char containing the extflagdatasize
213 const int extheaderflagdatasize = reader.readChar();
214 extrabytes += extheaderflagdatasize;
215 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
216 reader.setCur(reader.getCur() + extheaderflagdatasize);
217 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
218 }
219 if (extflags[0]->test(EXT_HEADER_FLAG_BIT4))
220 {
221 // ID3V2_4_0 ext header flag bit 4 = Tag restrictions
222 // read size
223 extrabytes += 1; // add a byte for the char containing the extflagdatasize
224 const int extheaderflagdatasize = reader.readChar();
225 extrabytes += extheaderflagdatasize;
226 // Set the cursor right; we are not parsing the data, no-one is using extended flags anyway
227 reader.setCur(reader.getCur() + extheaderflagdatasize);
228 //reader.readChars(buf, extheaderflagdatasize); //buf should be at least 127 bytes = max extended header flagdata size
229 }
230 _info->extended_bytes = 5 + extflagbytes + extrabytes;
231 }
232 // a bit unorthodox, but since we are not using any of the extended header, but were merely
233 // parsing it to get the cursor right, we delete it. Be Gone !
234 _flags.set(HEADER_FLAG_EXTENDED, false);
235 if (_info)
236 {
237 _data_size -= _info->extended_bytes;
238 _info->extended_bytes = 0;
239 }//else there is a tag with a higher or lower version than supported
240}
241
flags_t TYPE
Definition flags.h:36
bool set(TYPE f)
Definition flags.h:43
bool SetDataSize(size_t size)
Definition header.h:64
ID3_V2Spec GetSpec() const
Definition header.h:62
size_t _data_size
Definition header.h:103
Info * _info
Definition header.h:105
size_t GetDataSize() const
Definition header.h:71
virtual bool SetSpec(ID3_V2Spec)
Definition header.cpp:34
ID3_Flags _flags
Definition header.h:104
virtual pos_type setCur(pos_type pos)=0
Set the value of the current position for reading.
virtual pos_type getCur()=0
Return the current position in the reader.
virtual size_type readChars(char_type buf[], size_type len)=0
Read up to len characters into buf and advance the internal position accordingly.
virtual int_type readChar()
Read a single character and advance the internal position.
Definition reader.h:65
@ HEADER_FLAG_EXPERIMENTAL
Definition header_tag.h:42
void Render(ID3_Writer &) const
bool Parse(ID3_Reader &)
void ParseExtended(ID3_Reader &)
static const char *const ID
Definition header_tag.h:100
size_t Size() const
bool SetSpec(ID3_V2Spec)
static size_t IsV2Tag(const uchar *)
Analyses a buffer to determine if we have a valid ID3v2 tag header.
Definition tag.cpp:955
virtual size_type writeChars(const char_type buf[], size_type len)=0
Write up to len characters into buf and advance the internal position accordingly.
virtual int_type writeChar(char_type ch)
Write a single character and advance the internal position.
Definition writer.h:71
static const int_type END_OF_WRITER
Definition writer.h:41
#define MASK8
Definition globals.h:713
#define NULL
Definition globals.h:751
ID3_V2Spec
Definition globals.h:170
@ ID3V2_LATEST
Definition globals.h:177
@ ID3V2_4_0
Definition globals.h:175
@ ID3V2_2_1
Definition globals.h:173
@ ID3V2_UNKNOWN
Definition globals.h:171
@ ID3V2_3_0
Definition globals.h:174
unsigned char uchar
Definition globals.h:122
uchar ID3_V2SpecToRev(ID3_V2Spec spec)
Definition spec.cpp:87
ID3_V2Spec ID3_VerRevToV2Spec(uchar ver, uchar rev)
Definition spec.cpp:34
uchar ID3_V2SpecToVer(ID3_V2Spec spec)
Definition spec.cpp:66